-
Notifications
You must be signed in to change notification settings - Fork 979
feat: audit login #5925
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
Merged
Merged
feat: audit login #5925
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0a0adfd
added migration for api key resource
Kira-Pilot 6ddead2
Merge remote-tracking branch 'origin/main' into audit-login-logout/ki…
Kira-Pilot 1acc73c
sort of working
Kira-Pilot a75392e
auditing login
Kira-Pilot 4c754d3
passing the correct user id
Kira-Pilot 622733c
added and fixed tests
Kira-Pilot d9480b6
gen documentation
Kira-Pilot 7308943
formatting and lint
Kira-Pilot e5dbe17
lint
Kira-Pilot e3a6587
audit Github oauth and write tests
Kira-Pilot 809dc7b
audit oauth and write tests
Kira-Pilot e0a0b38
added defer fn for login error auditing
Kira-Pilot 524e001
Merge remote-tracking branch 'origin/main' into audit-login-logout/ki…
Kira-Pilot b19ae71
fixed test
Kira-Pilot 244068f
feat: audit logout (#5998)
Kira-Pilot 3465617
Update coderd/userauth.go
Kira-Pilot 28f8060
Merge remote-tracking branch 'origin/main' into audit-login-logout/ki…
Kira-Pilot 5a25616
Merge remote-tracking branch 'origin/main' into audit-login-logout/ki…
Kira-Pilot e242716
fix test
Kira-Pilot cda0818
bypassing diff generation if login/logout
Kira-Pilot 9bfc125
lint
Kira-Pilot 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
audit Github oauth and write tests
- Loading branch information
commit e3a6587cbca25eb331fed0ac115e627a39916c7c
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -17,6 +17,7 @@ import ( | |
"golang.org/x/oauth2" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/coderd/audit" | ||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/coderd/httpapi" | ||
"github.com/coder/coder/coderd/httpmw" | ||
|
@@ -66,9 +67,18 @@ func (api *API) userAuthMethods(rw http.ResponseWriter, r *http.Request) { | |
// @Router /users/oauth2/github/callback [get] | ||
func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) { | ||
var ( | ||
ctx = r.Context() | ||
state = httpmw.OAuth2(r) | ||
ctx = r.Context() | ||
state = httpmw.OAuth2(r) | ||
auditor = api.Auditor.Load() | ||
aReq, commitAudit = audit.InitRequest[database.APIKey](rw, &audit.RequestParams{ | ||
Audit: *auditor, | ||
Log: api.Logger, | ||
Request: r, | ||
Action: database.AuditActionLogin, | ||
}) | ||
) | ||
aReq.Old = database.APIKey{} | ||
defer commitAudit() | ||
|
||
oauthClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(state.Token)) | ||
|
||
|
@@ -81,6 +91,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) { | |
Message: "Internal error fetching authenticated Github user organizations.", | ||
Detail: err.Error(), | ||
}) | ||
// We pass a disposable user ID just to force an audit diff | ||
// and generate a log for a failed login | ||
aReq.New = database.APIKey{UserID: uuid.New()} | ||
return | ||
} | ||
|
||
|
@@ -101,6 +114,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) { | |
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{ | ||
Message: "You aren't a member of the authorized Github organizations!", | ||
}) | ||
// We pass a disposable user ID just to force an audit diff | ||
// and generate a log for a failed login | ||
aReq.New = database.APIKey{UserID: uuid.New()} | ||
return | ||
} | ||
} | ||
|
@@ -111,6 +127,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) { | |
Message: "Internal error fetching authenticated Github user.", | ||
Detail: err.Error(), | ||
}) | ||
// We pass a disposable user ID just to force an audit diff | ||
// and generate a log for a failed login | ||
aReq.New = database.APIKey{UserID: uuid.New()} | ||
return | ||
} | ||
|
||
|
@@ -139,6 +158,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) { | |
httpapi.Write(ctx, rw, http.StatusUnauthorized, codersdk.Response{ | ||
Message: fmt.Sprintf("You aren't a member of an authorized team in the %v Github organization(s)!", organizationNames), | ||
}) | ||
// We pass a disposable user ID just to force an audit diff | ||
// and generate a log for a failed login | ||
aReq.New = database.APIKey{UserID: uuid.New()} | ||
return | ||
} | ||
} | ||
|
@@ -149,6 +171,9 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) { | |
Message: "Internal error fetching personal Github user.", | ||
Detail: err.Error(), | ||
}) | ||
// We pass a disposable user ID just to force an audit diff | ||
// and generate a log for a failed login | ||
aReq.New = database.APIKey{UserID: uuid.New()} | ||
return | ||
} | ||
|
||
|
@@ -164,10 +189,28 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) { | |
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Your primary email must be verified on GitHub!", | ||
}) | ||
// We pass a disposable user ID just to force an audit diff | ||
// and generate a log for a failed login | ||
aReq.New = database.APIKey{UserID: uuid.New()} | ||
return | ||
} | ||
|
||
cookie, err := api.oauthLogin(r, oauthLoginParams{ | ||
user, link, err := findLinkedUser(ctx, api.Database, githubLinkedID(ghUser), verifiedEmail.GetEmail()) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to find linked user.", | ||
Detail: err.Error(), | ||
}) | ||
// We pass a disposable user ID just to force an audit diff | ||
// and generate a log for a failed login | ||
aReq.New = database.APIKey{UserID: uuid.New()} | ||
return | ||
} | ||
aReq.UserID = user.ID | ||
|
||
cookie, key, err := api.oauthLogin(r, oauthLoginParams{ | ||
User: user, | ||
Link: link, | ||
State: state, | ||
LinkedID: githubLinkedID(ghUser), | ||
LoginType: database.LoginTypeGithub, | ||
|
@@ -182,16 +225,24 @@ func (api *API) userOAuth2Github(rw http.ResponseWriter, r *http.Request) { | |
Message: httpErr.msg, | ||
Detail: httpErr.detail, | ||
}) | ||
// We pass a disposable user ID just to force an audit diff | ||
// and generate a log for a failed login | ||
aReq.New = database.APIKey{UserID: uuid.New()} | ||
return | ||
} | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Failed to process OAuth login.", | ||
Detail: err.Error(), | ||
}) | ||
// We pass a disposable user ID just to force an audit diff | ||
// and generate a log for a failed login | ||
aReq.New = database.APIKey{UserID: uuid.New()} | ||
return | ||
} | ||
|
||
aReq.New = key | ||
|
||
http.SetCookie(rw, cookie) | ||
|
||
redirect := state.Redirect | ||
|
@@ -362,7 +413,7 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) { | |
picture, _ = pictureRaw.(string) | ||
} | ||
|
||
cookie, err := api.oauthLogin(r, oauthLoginParams{ | ||
cookie, _, err := api.oauthLogin(r, oauthLoginParams{ | ||
State: state, | ||
LinkedID: oidcLinkedID(idToken), | ||
LoginType: database.LoginTypeOIDC, | ||
|
@@ -397,6 +448,8 @@ func (api *API) userOIDC(rw http.ResponseWriter, r *http.Request) { | |
} | ||
|
||
type oauthLoginParams struct { | ||
User database.User | ||
Link database.UserLink | ||
State httpmw.OAuth2State | ||
LinkedID string | ||
LoginType database.LoginType | ||
|
@@ -423,7 +476,7 @@ func (e httpError) Error() string { | |
return e.msg | ||
} | ||
|
||
func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cookie, error) { | ||
func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cookie, database.APIKey, error) { | ||
var ( | ||
ctx = r.Context() | ||
user database.User | ||
|
@@ -435,10 +488,13 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook | |
err error | ||
) | ||
|
||
user, link, err = findLinkedUser(ctx, tx, params.LinkedID, params.Email) | ||
if err != nil { | ||
return xerrors.Errorf("find linked user: %w", err) | ||
} | ||
user = params.User | ||
link = params.Link | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought it was safe to lift finding the user outside of the transaction and into the callers so we can access |
||
// | ||
// user, link, err = findLinkedUser(ctx, tx, params.LinkedID, params.Email) | ||
// if err != nil { | ||
// return xerrors.Errorf("find linked user: %w", err) | ||
// } | ||
|
||
if user.ID == uuid.Nil && !params.AllowSignups { | ||
return httpError{ | ||
|
@@ -599,19 +655,19 @@ func (api *API) oauthLogin(r *http.Request, params oauthLoginParams) (*http.Cook | |
return nil | ||
}, nil) | ||
if err != nil { | ||
return nil, xerrors.Errorf("in tx: %w", err) | ||
return nil, database.APIKey{}, xerrors.Errorf("in tx: %w", err) | ||
} | ||
|
||
cookie, _, err := api.createAPIKey(ctx, createAPIKeyParams{ | ||
cookie, key, err := api.createAPIKey(ctx, createAPIKeyParams{ | ||
UserID: user.ID, | ||
LoginType: params.LoginType, | ||
RemoteAddr: r.RemoteAddr, | ||
}) | ||
if err != nil { | ||
return nil, xerrors.Errorf("create API key: %w", err) | ||
return nil, database.APIKey{}, xerrors.Errorf("create API key: %w", err) | ||
} | ||
|
||
return cookie, nil | ||
return cookie, *key, nil | ||
} | ||
|
||
// githubLinkedID returns the unique ID for a GitHub user. | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.