-
Notifications
You must be signed in to change notification settings - Fork 902
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a630982
got links working
Kira-Pilot c988b1a
added translations
Kira-Pilot 34a527f
fixed translation
Kira-Pilot b101417
added translation for unavailable ip
Kira-Pilot ddc0dc0
added support for group, template, user links
Kira-Pilot 709e42e
cleaned up string
Kira-Pilot d5eb06a
added deleted label
Kira-Pilot 221f089
querying for workspace id
Kira-Pilot 8177557
remove prints
Kira-Pilot d1ba2b9
fix/write tests
Kira-Pilot 9353342
PR feedback pt 1
Kira-Pilot 6465f3e
PR feedback part 2
Kira-Pilot 23e5d29
Merge remote-tracking branch 'origin/main' into add-audit-links/kira-…
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
added deleted label
- Loading branch information
commit d5eb06a13867370ef8f729cf9ebf88af8948a636
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 |
---|---|---|
|
@@ -199,10 +199,40 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs | |
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", | ||
codersdk.AuditAction(alog.Action).FriendlyString(), | ||
) | ||
|
||
// 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 && alog.Action != database.AuditActionDelete { | ||
str += " build for" | ||
} | ||
|
||
// 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 += fmt.Sprintf(" the %s", | ||
codersdk.ResourceType(alog.ResourceType).FriendlyString()) | ||
return str | ||
} | ||
|
||
str += fmt.Sprintf(" %s", | ||
codersdk.ResourceType(alog.ResourceType).FriendlyString()) | ||
|
||
str += " {target}" | ||
|
||
return str | ||
} | ||
|
||
type AdditionalFields struct { | ||
|
@@ -216,19 +246,19 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get | |
case database.ResourceTypeTemplate: | ||
Kira-Pilot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
template, err := api.Database.GetTemplateByID(ctx, alog.ResourceID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "could not get template", slog.Error(err)) | ||
api.Logger.Error(ctx, "could not fetch template", slog.Error(err)) | ||
} | ||
return template.Deleted | ||
case database.ResourceTypeUser: | ||
user, err := api.Database.GetUserByID(ctx, alog.ResourceID) | ||
if err != nil { | ||
api.Logger.Error(ctx, "could not get user", slog.Error(err)) | ||
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 get workspace", slog.Error(err)) | ||
api.Logger.Error(ctx, "could not fetch workspace", slog.Error(err)) | ||
} | ||
return workspace.Deleted | ||
case database.ResourceTypeWorkspaceBuild: | ||
|
@@ -245,7 +275,7 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get | |
// We use workspace as a proxy for workspace build here | ||
workspace, err := api.Database.GetWorkspaceByID(ctx, uuid.MustParse(additionalFields.WorkspaceID)) | ||
if err != nil { | ||
api.Logger.Error(ctx, "could not get workspace", slog.Error(err)) | ||
api.Logger.Error(ctx, "could not fetch workspace", slog.Error(err)) | ||
} | ||
return workspace.Deleted | ||
default: | ||
|
@@ -254,29 +284,21 @@ func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.Get | |
} | ||
|
||
func (api *API) auditLogResourceLink(ctx context.Context, alog database.GetAuditLogsOffsetRow) string { | ||
if api.auditLogIsResourceDeleted(ctx, alog) { | ||
return "" | ||
} | ||
|
||
switch alog.ResourceType { | ||
case database.ResourceTypeTemplate: | ||
if api.auditLogIsResourceDeleted(ctx, alog) { | ||
return "" | ||
} | ||
return fmt.Sprintf("/templates/%s", | ||
alog.ResourceTarget) | ||
case database.ResourceTypeUser: | ||
if api.auditLogIsResourceDeleted(ctx, alog) { | ||
return "" | ||
} | ||
return fmt.Sprintf("/users?filter=%s", | ||
alog.ResourceTarget) | ||
case database.ResourceTypeWorkspace: | ||
if api.auditLogIsResourceDeleted(ctx, alog) { | ||
return "" | ||
} | ||
return fmt.Sprintf("/@%s/%s", | ||
alog.UserUsername.String, alog.ResourceTarget) | ||
case database.ResourceTypeWorkspaceBuild: | ||
if api.auditLogIsResourceDeleted(ctx, alog) { | ||
return "" | ||
} | ||
additionalFieldsBytes := []byte(alog.AdditionalFields) | ||
var additionalFields AdditionalFields | ||
err := json.Unmarshal(additionalFieldsBytes, &additionalFields) | ||
|
@@ -290,35 +312,6 @@ func (api *API) auditLogResourceLink(ctx context.Context, alog database.GetAudit | |
} | ||
} | ||
|
||
func auditLogDescription(alog database.GetAuditLogsOffsetRow) string { | ||
str := fmt.Sprintf("{user} %s", | ||
codersdk.AuditAction(alog.Action).FriendlyString(), | ||
) | ||
|
||
// 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 && alog.Action != database.AuditActionDelete { | ||
str += " build for" | ||
} | ||
|
||
// 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 += fmt.Sprintf(" the %s", | ||
codersdk.ResourceType(alog.ResourceType).FriendlyString()) | ||
return str | ||
} | ||
|
||
str += fmt.Sprintf(" %s", | ||
codersdk.ResourceType(alog.ResourceType).FriendlyString()) | ||
|
||
str += " {target}" | ||
|
||
return str | ||
} | ||
|
||
// 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) { | ||
|
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
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
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
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
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.
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.