-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add sshcert auth #245
Merged
Merged
Add sshcert auth #245
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
465a03e
initial dump, server handlers and basic structs
cviecco 37f01d3
server side but no tests
cviecco 32c3d09
first set of tests, the easy ones
cviecco 873147d
Merge branch 'master' into add-sshcert-auth
cviecco 47cdbc2
add tests to develop branch
cviecco 2aa533d
cleanup
cviecco e480b72
forgot removal
cviecco e8dc19a
enhance testing
cviecco 999de5c
splt services
cviecco ae13937
addressing comments
cviecco 2900b16
cleanup
cviecco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/Cloud-Foundations/keymaster/lib/webapi/v0/proto" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
// This function can only be called after all known keymaster public keys | ||
// have been loaded, that is, after the server is ready | ||
func (state *RuntimeState) initialzeSelfSSHCertAuthenticator() error { | ||
|
||
// build ssh pubkey list | ||
var sshTrustedKeys []string | ||
for _, pubkey := range state.KeymasterPublicKeys { | ||
sshPubkey, err := ssh.NewPublicKey(pubkey) | ||
if err != nil { | ||
return err | ||
} | ||
authorizedKey := ssh.MarshalAuthorizedKey(sshPubkey) | ||
sshTrustedKeys = append(sshTrustedKeys, string(authorizedKey)) | ||
} | ||
return state.sshCertAuthenticator.UnsafeUpdateCaKeys(sshTrustedKeys) | ||
} | ||
|
||
func (state *RuntimeState) isSelfSSHCertAuthenticatorEnabled() bool { | ||
for _, certPref := range state.Config.Base.AllowedAuthBackendsForCerts { | ||
if certPref == proto.AuthTypeSSHCert { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// CreateChallengeHandler is an example of how to write a handler for | ||
// the path to create the challenge | ||
func (s *RuntimeState) sshCertAuthCreateChallengeHandler(w http.ResponseWriter, r *http.Request) { | ||
// TODO: add some rate limiting | ||
err := s.sshCertAuthenticator.CreateChallengeHandler(w, r) | ||
if err != nil { | ||
// we are assuming bad request | ||
s.logger.Debugf(1, | ||
"CreateSSHCertAuthChallengeHandler: there was an err computing challenge: %s", err) | ||
s.writeFailureResponse(w, r, http.StatusBadRequest, "Invalid Operation") | ||
return | ||
} | ||
} | ||
|
||
func (s *RuntimeState) sshCertAuthLoginWithChallengeHandler(w http.ResponseWriter, r *http.Request) { | ||
username, expiration, userErrString, err := s.sshCertAuthenticator.LoginWithChallenge(r) | ||
if err != nil { | ||
s.logger.Printf("error=%s", err) | ||
errorCode := http.StatusBadRequest | ||
if userErrString == "" { | ||
errorCode = http.StatusInternalServerError | ||
} | ||
s.writeFailureResponse(w, r, errorCode, userErrString) | ||
return | ||
} | ||
// Make new auth cookie | ||
_, err = s.setNewAuthCookieWithExpiration(w, username, AuthTypeKeymasterSSHCert, expiration) | ||
if err != nil { | ||
s.writeFailureResponse(w, r, http.StatusInternalServerError, | ||
"error internal") | ||
s.logger.Println(err) | ||
return | ||
} | ||
|
||
// TODO: The cert backend should depend also on per user preferences. | ||
loginResponse := proto.LoginResponse{Message: "success"} | ||
// TODO needs eventnotifier? | ||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(loginResponse) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/Cloud-Foundations/Dominator/lib/log/serverlogger" | ||
"github.com/Cloud-Foundations/keymaster/lib/webapi/v0/proto" | ||
"github.com/cviecco/webauth-sshcert/lib/server/sshcertauth" | ||
) | ||
|
||
func TestInitializeSSHAuthenticator(t *testing.T) { | ||
state, passwdFile, err := setupValidRuntimeStateSigner(t) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(passwdFile.Name()) // clean up | ||
state.sshCertAuthenticator = sshcertauth.NewAuthenticator( | ||
[]string{state.HostIdentity}, []string{}) | ||
err = state.initialzeSelfSSHCertAuthenticator() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestIsSelfSSHCertAuthenticatorEnabled(t *testing.T) { | ||
state := RuntimeState{} | ||
if state.isSelfSSHCertAuthenticatorEnabled() { | ||
t.Fatal("it should not be enabled on empty state") | ||
} | ||
state.Config.Base.AllowedAuthBackendsForCerts = append(state.Config.Base.AllowedAuthBackendsForCerts, proto.AuthTypeSSHCert) | ||
if !state.isSelfSSHCertAuthenticatorEnabled() { | ||
t.Fatal("it should be enabled on empty state") | ||
} | ||
} | ||
|
||
func TestSshCertAuthCreateChallengeHandlert(t *testing.T) { | ||
state, passwdFile, err := setupValidRuntimeStateSigner(t) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(passwdFile.Name()) // clean up | ||
state.sshCertAuthenticator = sshcertauth.NewAuthenticator( | ||
[]string{state.HostIdentity}, []string{}) | ||
err = state.initialzeSelfSSHCertAuthenticator() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// make call with bad data | ||
//initially the request should fail for lack of preconditions | ||
req, err := http.NewRequest("POST", redirectPath, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// oath2 config is invalid | ||
_, err = checkRequestHandlerCode(req, state.sshCertAuthCreateChallengeHandler, http.StatusBadRequest) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// simulaet good call, ignore result for now | ||
goodURL := "foobar?nonce1=12345678901234567890123456789" | ||
// TODO: replce this for a post | ||
req2, err := http.NewRequest("GET", goodURL, nil) | ||
_, err = checkRequestHandlerCode(req2, state.sshCertAuthCreateChallengeHandler, http.StatusOK) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestSshCertAuthLoginWithChallengeHandler(t *testing.T) { | ||
state, passwdFile, err := setupValidRuntimeStateSigner(t) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(passwdFile.Name()) // clean up | ||
state.Config.Base.AllowedAuthBackendsForCerts = append(state.Config.Base.AllowedAuthBackendsForCerts, proto.AuthTypeSSHCert) | ||
realLogger := serverlogger.New("") //TODO, we need to find a simulator for this | ||
startServerAfterLoad(state, realLogger) | ||
|
||
//TODO: write the actual test, at this point we only have the endpoints initalized | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😢