Skip to content

fix: show audit logs for forgot password flow #15181

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 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: show audit logs for forgot password flow
  • Loading branch information
DanielleMaywood committed Oct 22, 2024
commit ecd43f12bab12a500b2bf167668df085f33aa198
6 changes: 4 additions & 2 deletions coderd/apidoc/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions coderd/apidoc/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,15 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs

func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
b := strings.Builder{}

// NOTE: WriteString always returns a nil error, so we never check it
_, _ = b.WriteString("{user} ")

// Requesting a one-time passcode can be performed by anyone that knows the email
// of a user so saying the user performed this action might be slightly misleading.
if alog.AuditLog.Action != database.AuditActionRequestOneTimePasscode {
_, _ = b.WriteString("{user} ")
}

if alog.AuditLog.StatusCode >= 400 {
_, _ = b.WriteString("unsuccessfully attempted to ")
_, _ = b.WriteString(string(alog.AuditLog.Action))
Expand All @@ -298,13 +305,16 @@ func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
return b.String()
}

_, _ = b.WriteString(" ")
_, _ = b.WriteString(codersdk.ResourceType(alog.AuditLog.ResourceType).FriendlyString())
if alog.AuditLog.Action == database.AuditActionRequestOneTimePasscode {
_, _ = b.WriteString(" for")
} else {
_, _ = b.WriteString(" ")
_, _ = b.WriteString(codersdk.ResourceType(alog.AuditLog.ResourceType).FriendlyString())
}

if alog.AuditLog.ResourceType == database.ResourceTypeConvertLogin {
_, _ = b.WriteString(" to")
}

_, _ = b.WriteString(" {target}")

return b.String()
Expand Down
3 changes: 2 additions & 1 deletion coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- It's not possible to drop enum values from enum types, so the UP has "IF NOT
-- EXISTS".
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TYPE audit_action
ADD VALUE IF NOT EXISTS 'request_one_time_passcode';
21 changes: 12 additions & 9 deletions coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion coderd/database/pubsub/psmock/psmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion coderd/userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Reque
Audit: *auditor,
Log: api.Logger,
Request: r,
Action: database.AuditActionWrite,
Action: database.AuditActionRequestOneTimePasscode,
})
)
defer commitAudit()
Expand Down Expand Up @@ -253,6 +253,7 @@ func (api *API) postRequestOneTimePasscode(rw http.ResponseWriter, r *http.Reque
}
// We continue if err == sql.ErrNoRows to help prevent a timing-based attack.
aReq.Old = user
aReq.UserID = user.ID

passcode := uuid.New()
passcodeExpiresAt := dbtime.Now().Add(api.OneTimePasscodeValidityPeriod)
Expand Down Expand Up @@ -365,6 +366,7 @@ func (api *API) postChangePasswordWithOneTimePasscode(rw http.ResponseWriter, r
}
// We continue if err == sql.ErrNoRows to help prevent a timing-based attack.
aReq.Old = user
aReq.UserID = user.ID

equal, err := userpassword.Compare(string(user.HashedOneTimePasscode), req.OneTimePasscode)
if err != nil {
Expand Down
19 changes: 11 additions & 8 deletions codersdk/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,15 @@ func (r ResourceType) FriendlyString() string {
type AuditAction string

const (
AuditActionCreate AuditAction = "create"
AuditActionWrite AuditAction = "write"
AuditActionDelete AuditAction = "delete"
AuditActionStart AuditAction = "start"
AuditActionStop AuditAction = "stop"
AuditActionLogin AuditAction = "login"
AuditActionLogout AuditAction = "logout"
AuditActionRegister AuditAction = "register"
AuditActionCreate AuditAction = "create"
AuditActionWrite AuditAction = "write"
AuditActionDelete AuditAction = "delete"
AuditActionStart AuditAction = "start"
AuditActionStop AuditAction = "stop"
AuditActionLogin AuditAction = "login"
AuditActionLogout AuditAction = "logout"
AuditActionRegister AuditAction = "register"
AuditActionRequestOneTimePasscode AuditAction = "request_one_time_passcode"
)

func (a AuditAction) Friendly() string {
Expand All @@ -114,6 +115,8 @@ func (a AuditAction) Friendly() string {
return "logged out"
case AuditActionRegister:
return "registered"
case AuditActionRequestOneTimePasscode:
return "one time passcode requested"
default:
return "unknown"
}
Expand Down
21 changes: 11 additions & 10 deletions docs/reference/api/schemas.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion enterprise/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ var auditableResourcesTypes = map[any]map[string]Action{
"theme_preference": ActionIgnore,
"name": ActionTrack,
"github_com_user_id": ActionIgnore,
"hashed_one_time_passcode": ActionSecret, // Do not expose a user's one time passcode.
"hashed_one_time_passcode": ActionIgnore,
"one_time_passcode_expires_at": ActionTrack,
"must_reset_password": ActionTrack,
},
Expand Down
4 changes: 2 additions & 2 deletions site/src/api/typesGenerated.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions site/src/pages/AuditPage/AuditLogRow/AuditLogDiff/AuditLogDiff.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ const getDiffValue = (value: unknown): string => {
return `"${value}"`;
}

if (isTimeObject(value)) {
if (!value.Valid) {
return "null";
}

return new Date(value.Time).toLocaleString();
}

if (Array.isArray(value)) {
const values = value.map((v) => getDiffValue(v));
return `[${values.join(", ")}]`;
Expand All @@ -21,6 +29,19 @@ const getDiffValue = (value: unknown): string => {
return String(value);
};

const isTimeObject = (
value: unknown,
): value is { Time: string; Valid: boolean } => {
return (
value !== null &&
typeof value === "object" &&
"Time" in value &&
typeof value.Time === "string" &&
"Valid" in value &&
typeof value.Valid === "boolean"
);
};

interface AuditLogDiffProps {
diff: AuditDiff;
}
Expand Down
Loading