Skip to content

feat: add description to audit log responses #3949

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 5 commits into from
Sep 8, 2022
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
feat: add description to audit log responses
  • Loading branch information
coadler committed Sep 7, 2022
commit af63c44c183c32a8843eb5faeb1dfb519b014c87
10 changes: 9 additions & 1 deletion coderd/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package coderd

import (
"encoding/json"
"fmt"
"net"
"net/http"
"net/netip"
Expand Down Expand Up @@ -167,7 +168,14 @@ func convertAuditLog(dblog database.GetAuditLogsOffsetRow) codersdk.AuditLog {
Diff: diff,
StatusCode: dblog.StatusCode,
AdditionalFields: dblog.AdditionalFields,
Description: "",
Description: auditLogDescription(dblog),
User: user,
}
}

func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
return fmt.Sprintf("{user} %s %s {target}",
codersdk.AuditAction(alog.Action).FriendlyString(),
codersdk.ResourceType(alog.ResourceType).FriendlyString(),
)
}
2 changes: 2 additions & 0 deletions coderd/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/coderdtest"
Expand All @@ -29,6 +30,7 @@ func TestAuditLogs(t *testing.T) {
alogs, err := client.AuditLogs(ctx, codersdk.Pagination{Limit: 1})
require.NoError(t, err)

spew.Dump(alogs.AuditLogs)
require.Equal(t, int64(1), count.Count)
require.Len(t, alogs.AuditLogs, 1)
})
Expand Down
2 changes: 1 addition & 1 deletion coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -2325,7 +2325,7 @@ func (q *fakeQuerier) GetAuditLogsOffset(ctx context.Context, arg database.GetAu
OrganizationID: alog.OrganizationID,
Ip: alog.Ip,
UserAgent: alog.UserAgent,
ResourceType: database.ResourceType(alog.UserAgent),
ResourceType: alog.ResourceType,
ResourceID: alog.ResourceID,
ResourceTarget: alog.ResourceTarget,
ResourceIcon: alog.ResourceIcon,
Expand Down
4 changes: 3 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
@@ -1,9 +1,9 @@
-- It's not possible to drop enum values from enum types, so the UP has "IF NOT
-- EXISTS".

-- Delete all jobs that use the new enum value.
-- Delete all audit logs that use the new enum values.
DELETE FROM
provisioner_jobs
audit_logs
WHERE
type = 'template_version_dry_run'
;
resource_type = 'git_ssh_key' OR
resource_type = 'api_key';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- It's not possible to drop enum values from enum types, so the UP has "IF NOT
-- EXISTS".

-- Delete all jobs that use the new enum value.
DELETE FROM
provisioner_jobs
WHERE
type = 'template_version_dry_run';
2 changes: 2 additions & 0 deletions coderd/database/migrations/000045_more_resource_types.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'git_ssh_key';
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'api_key';
2 changes: 2 additions & 0 deletions coderd/database/models.go

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

36 changes: 36 additions & 0 deletions codersdk/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,31 @@ const (
ResourceTypeTemplateVersion ResourceType = "template_version"
ResourceTypeUser ResourceType = "user"
ResourceTypeWorkspace ResourceType = "workspace"
ResourceTypeGitSSHKey ResourceType = "git_ssh_key"
ResourceTypeAPIKey ResourceType = "api_key"
)

func (r ResourceType) FriendlyString() string {
switch r {
case ResourceTypeOrganization:
return "organization"
case ResourceTypeTemplate:
return "template"
case ResourceTypeTemplateVersion:
return "template version"
case ResourceTypeUser:
return "user"
case ResourceTypeWorkspace:
return "workspace"
case ResourceTypeGitSSHKey:
return "git ssh key"
case ResourceTypeAPIKey:
return "api key"
default:
return "unknown"
}
}

type AuditAction string

const (
Expand All @@ -28,6 +51,19 @@ const (
AuditActionDelete AuditAction = "delete"
)

func (a AuditAction) FriendlyString() string {
switch a {
case AuditActionCreate:
return "created"
case AuditActionWrite:
return "updated"
case AuditActionDelete:
return "deleted"
default:
return "unknown"
}
}

type AuditDiff map[string]AuditDiffField

type AuditDiffField struct {
Expand Down
9 changes: 8 additions & 1 deletion site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,14 @@ export type ProvisionerStorageMethod = "file"
export type ProvisionerType = "echo" | "terraform"

// From codersdk/audit.go
export type ResourceType = "organization" | "template" | "template_version" | "user" | "workspace"
export type ResourceType =
| "api_key"
| "git_ssh_key"
| "organization"
| "template"
| "template_version"
| "user"
| "workspace"

// From codersdk/users.go
export type UserStatus = "active" | "suspended"
Expand Down