-
Notifications
You must be signed in to change notification settings - Fork 894
Add audit links/kira pilot #5156
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
Changes from 10 commits
a630982
c988b1a
34a527f
b101417
ddc0dc0
709e42e
d5eb06a
221f089
8177557
d1ba2b9
9353342
6465f3e
23e5d29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package coderd | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
|
@@ -14,6 +15,7 @@ import ( | |
"github.com/google/uuid" | ||
"github.com/tabbed/pqtype" | ||
|
||
"cdr.dev/slog" | ||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/coderd/httpapi" | ||
"github.com/coder/coder/coderd/httpmw" | ||
|
@@ -69,7 +71,7 @@ func (api *API) auditLogs(rw http.ResponseWriter, r *http.Request) { | |
} | ||
|
||
httpapi.Write(ctx, rw, http.StatusOK, codersdk.AuditLogResponse{ | ||
AuditLogs: convertAuditLogs(dblogs), | ||
AuditLogs: api.convertAuditLogs(ctx, dblogs), | ||
Count: dblogs[0].Count, | ||
}) | ||
} | ||
|
@@ -147,17 +149,17 @@ func (api *API) generateFakeAuditLog(rw http.ResponseWriter, r *http.Request) { | |
rw.WriteHeader(http.StatusNoContent) | ||
} | ||
|
||
func convertAuditLogs(dblogs []database.GetAuditLogsOffsetRow) []codersdk.AuditLog { | ||
func (api *API) convertAuditLogs(ctx context.Context, dblogs []database.GetAuditLogsOffsetRow) []codersdk.AuditLog { | ||
alogs := make([]codersdk.AuditLog, 0, len(dblogs)) | ||
|
||
for _, dblog := range dblogs { | ||
alogs = append(alogs, convertAuditLog(dblog)) | ||
alogs = append(alogs, api.convertAuditLog(ctx, dblog)) | ||
} | ||
|
||
return alogs | ||
} | ||
|
||
func convertAuditLog(dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog { | ||
func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog { | ||
ip, _ := netip.AddrFromSlice(dblog.Ip.IPNet.IP) | ||
|
||
diff := codersdk.AuditDiff{} | ||
|
@@ -197,34 +199,112 @@ func convertAuditLog(dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog { | |
Diff: diff, | ||
StatusCode: dblog.StatusCode, | ||
AdditionalFields: dblog.AdditionalFields, | ||
Description: auditLogDescription(dblog), | ||
User: user, | ||
Description: auditLogDescription(dblog), | ||
ResourceLink: api.auditLogResourceLink(ctx, dblog), | ||
IsDeleted: api.auditLogIsResourceDeleted(ctx, dblog), | ||
} | ||
} | ||
|
||
func auditLogDescription(alog database.GetAuditLogsOffsetRow) string { | ||
str := fmt.Sprintf("{user} %s %s", | ||
str := fmt.Sprintf("{user} %s", | ||
codersdk.AuditAction(alog.Action).FriendlyString(), | ||
codersdk.ResourceType(alog.ResourceType).FriendlyString(), | ||
) | ||
|
||
// Strings for workspace_builds follow the below format: | ||
// "{user} started workspace build for {target}" | ||
// where target is a workspace instead of the workspace build, | ||
// Strings for starting/stopping workspace builds follow the below format: | ||
// "{user} started build for workspace {target}" | ||
// where target is a workspace instead of a workspace build | ||
// passed in on the FE via AuditLog.AdditionalFields rather than derived in request.go:35 | ||
if alog.ResourceType == database.ResourceTypeWorkspaceBuild { | ||
str += " for" | ||
if alog.ResourceType == database.ResourceTypeWorkspaceBuild && alog.Action != database.AuditActionDelete { | ||
str += " build for" | ||
} | ||
|
||
// We don't display the name for git ssh keys. It's fairly long and doesn't | ||
// We don't display the name (target) for git ssh keys. It's fairly long and doesn't | ||
// make too much sense to display. | ||
if alog.ResourceType != database.ResourceTypeGitSshKey { | ||
str += " {target}" | ||
if alog.ResourceType == database.ResourceTypeGitSshKey { | ||
str += fmt.Sprintf(" the %s", | ||
codersdk.ResourceType(alog.ResourceType).FriendlyString()) | ||
return str | ||
} | ||
|
||
str += fmt.Sprintf(" %s", | ||
codersdk.ResourceType(alog.ResourceType).FriendlyString()) | ||
|
||
str += " {target}" | ||
|
||
return str | ||
} | ||
|
||
func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.GetAuditLogsOffsetRow) bool { | ||
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. @coadler I'm concerned the queries in this switch will take a while to resolve. Is there a better way to determine if a resource has been deleted? 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 don't think there's a great way to do so without something hacky like reflection here. 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 guess the only way to really fix this would be to return if the resource is deleted in the 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. As per our Slack convo, will try to add that as a fast follow! |
||
switch alog.ResourceType { | ||
case database.ResourceTypeTemplate: | ||
template, err := api.Database.GetTemplateByID(ctx, alog.ResourceID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "could not fetch template", slog.Error(err)) | ||
} | ||
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 think we should also check |
||
return template.Deleted | ||
case database.ResourceTypeUser: | ||
user, err := api.Database.GetUserByID(ctx, alog.ResourceID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "could not fetch user", slog.Error(err)) | ||
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. small nit; normally we don't prefix log lines with |
||
} | ||
return user.Deleted | ||
case database.ResourceTypeWorkspace: | ||
workspace, err := api.Database.GetWorkspaceByID(ctx, alog.ResourceID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "could not fetch workspace", slog.Error(err)) | ||
} | ||
return workspace.Deleted | ||
case database.ResourceTypeWorkspaceBuild: | ||
workspaceBuild, err := api.Database.GetWorkspaceBuildByID(ctx, alog.ResourceID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "could not fetch workspace build", slog.Error(err)) | ||
} | ||
// We use workspace as a proxy for workspace build here | ||
workspace, err := api.Database.GetWorkspaceByID(ctx, workspaceBuild.WorkspaceID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "could not fetch workspace", slog.Error(err)) | ||
} | ||
return workspace.Deleted | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
type AdditionalFields struct { | ||
WorkspaceName string | ||
BuildNumber string | ||
} | ||
|
||
func (api *API) auditLogResourceLink(ctx context.Context, alog database.GetAuditLogsOffsetRow) string { | ||
if api.auditLogIsResourceDeleted(ctx, alog) { | ||
return "" | ||
} | ||
|
||
switch alog.ResourceType { | ||
case database.ResourceTypeTemplate: | ||
Kira-Pilot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return fmt.Sprintf("/templates/%s", | ||
alog.ResourceTarget) | ||
case database.ResourceTypeUser: | ||
return fmt.Sprintf("/users?filter=%s", | ||
alog.ResourceTarget) | ||
case database.ResourceTypeWorkspace: | ||
return fmt.Sprintf("/@%s/%s", | ||
alog.UserUsername.String, alog.ResourceTarget) | ||
case database.ResourceTypeWorkspaceBuild: | ||
additionalFieldsBytes := []byte(alog.AdditionalFields) | ||
var additionalFields AdditionalFields | ||
err := json.Unmarshal(additionalFieldsBytes, &additionalFields) | ||
if err != nil { | ||
api.Logger.Error(ctx, "could not unmarshal workspace name", slog.Error(err)) | ||
} | ||
return fmt.Sprintf("/@%s/%s/builds/%s", | ||
alog.UserUsername.String, additionalFields.WorkspaceName, additionalFields.BuildNumber) | ||
default: | ||
return "" | ||
} | ||
} | ||
|
||
// auditSearchQuery takes a query string and returns the auditLog filter. | ||
// It also can return the list of validation errors to return to the api. | ||
func auditSearchQuery(query string) (database.GetAuditLogsOffsetParams, []codersdk.ValidationError) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { | ||
MockAuditLog, | ||
MockAuditLogWithWorkspaceBuild, | ||
} from "testHelpers/entities" | ||
import { AuditLogDescription } from "./AuditLogDescription" | ||
import { render } from "../../testHelpers/renderHelpers" | ||
import { screen } from "@testing-library/react" | ||
|
||
const getByTextContent = (text: string) => { | ||
return screen.getByText((_, element) => { | ||
const hasText = (element: Element | null) => element?.textContent === text | ||
const elementHasText = hasText(element) | ||
const childrenDontHaveText = Array.from(element?.children || []).every( | ||
(child) => !hasText(child), | ||
) | ||
return elementHasText && childrenDontHaveText | ||
}) | ||
} | ||
describe("AuditLogDescription", () => { | ||
it("renders the correct string for a workspace create audit log", async () => { | ||
render(<AuditLogDescription auditLog={MockAuditLog} />) | ||
|
||
expect( | ||
getByTextContent("TestUser created workspace bruno-dev"), | ||
).toBeDefined() | ||
}) | ||
|
||
it("renders the correct string for a workspace_build stop audit log", async () => { | ||
render(<AuditLogDescription auditLog={MockAuditLogWithWorkspaceBuild} />) | ||
|
||
expect( | ||
getByTextContent("TestUser stopped build for workspace test2"), | ||
).toBeDefined() | ||
}) | ||
|
||
it("renders the correct string for a workspace_build audit log with a duplicate word", async () => { | ||
const AuditLogWithRepeat = { | ||
...MockAuditLogWithWorkspaceBuild, | ||
additional_fields: { | ||
workspaceName: "workspace", | ||
}, | ||
} | ||
render(<AuditLogDescription auditLog={AuditLogWithRepeat} />) | ||
|
||
expect( | ||
getByTextContent("TestUser stopped build for workspace workspace"), | ||
).toBeDefined() | ||
}) | ||
}) |
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.
Because this calls
api.auditLogIsResourceDeleted
as well, you are making the same database calls twice here. One way to speed this up and save the work would be to callapi.auditLogIsResourceDeleted
before this struct and pass it into bothIsDeleted
and also as an arg intoapi.auditLogResourceLink
.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.
Woops, you're totally right! Pushing a fix now.