Skip to content

Commit e681978

Browse files
committed
kyle comments
1 parent d548842 commit e681978

File tree

2 files changed

+28
-60
lines changed

2 files changed

+28
-60
lines changed

coderd/audit/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func diffValues[T any](old, new T) DiffMap {
7373

7474
if !reflect.DeepEqual(oldI, newI) {
7575
switch atype {
76-
case ActionAuditable:
76+
case ActionTrack:
7777
baseDiff[diffName] = newI
7878
case ActionSecret:
7979
baseDiff[diffName] = reflect.Zero(newF.Type()).Interface()

coderd/audit/table.go

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package audit
33
import (
44
"reflect"
55

6-
"golang.org/x/xerrors"
7-
86
"github.com/coder/coder/coderd/database"
97
)
108

@@ -16,17 +14,17 @@ type Auditable interface {
1614
database.Workspace
1715
}
1816

19-
type Action int
17+
type Action string
2018

2119
const (
2220
// ActionIgnore ignores diffing for the field.
23-
ActionIgnore = iota
24-
// ActionAuditable includes the value in the diff if the value changed.
25-
ActionAuditable
21+
ActionIgnore = "ignore"
22+
// ActionTrack includes the value in the diff if the value changed.
23+
ActionTrack = "track"
2624
// ActionSecret includes a zero value of the same type if the value changed.
2725
// It lets you indicate that a value changed, but without leaking its
2826
// contents.
29-
ActionSecret
27+
ActionSecret = "secret"
3028
)
3129

3230
// Map is a map of struct names to a map of field names that indicate that
@@ -37,32 +35,32 @@ type Map map[string]map[string]Action
3735
// which fields are auditable.
3836
var AuditableResources = auditMap(map[any]map[string]Action{
3937
&database.User{}: {
40-
"id": ActionIgnore, // Never changes.
41-
"email": ActionAuditable, // A user can edit their email.
42-
"name": ActionAuditable, // A user can edit their name.
43-
"revoked": ActionAuditable, // An admin can revoke a user. This is different from deletion, which is implicit.
44-
"login_type": ActionAuditable, // An admin can update the login type of a user.
45-
"hashed_password": ActionSecret, // A user can change their own password.
46-
"created_at": ActionIgnore, // Never changes.
47-
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
48-
"username": ActionIgnore, // A user cannot change their username.
38+
"id": ActionIgnore, // Never changes.
39+
"email": ActionTrack, // A user can edit their email.
40+
"name": ActionTrack, // A user can edit their name.
41+
"revoked": ActionTrack, // An admin can revoke a user. This is different from deletion, which is implicit.
42+
"login_type": ActionTrack, // An admin can update the login type of a user.
43+
"hashed_password": ActionSecret, // A user can change their own password.
44+
"created_at": ActionIgnore, // Never changes.
45+
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
46+
"username": ActionIgnore, // A user cannot change their username.
4947
},
5048
&database.Workspace{}: {
51-
"id": ActionIgnore, // Never changes.
52-
"created_at": ActionIgnore, // Never changes.
53-
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
54-
"owner_id": ActionIgnore, // We don't allow workspaces to change ownership.
55-
"template_id": ActionIgnore, // We don't allow workspaces to change templates.
56-
"deleted": ActionIgnore, // Changes, but is implicit when a delete event is fired.
57-
"name": ActionIgnore, // We don't allow workspaces to change names.
58-
"autostart_schedule": ActionAuditable, // Autostart schedules are directly editable by users.
59-
"autostop_schedule": ActionAuditable, // Autostart schedules are directly editable by users.
49+
"id": ActionIgnore, // Never changes.
50+
"created_at": ActionIgnore, // Never changes.
51+
"updated_at": ActionIgnore, // Changes, but is implicit and not helpful in a diff.
52+
"owner_id": ActionIgnore, // We don't allow workspaces to change ownership.
53+
"template_id": ActionIgnore, // We don't allow workspaces to change templates.
54+
"deleted": ActionIgnore, // Changes, but is implicit when a delete event is fired.
55+
"name": ActionIgnore, // We don't allow workspaces to change names.
56+
"autostart_schedule": ActionTrack, // Autostart schedules are directly editable by users.
57+
"autostop_schedule": ActionTrack, // Autostart schedules are directly editable by users.
6058
},
6159
})
6260

63-
// auditMap converts a map of pointers to a map of struct names as strings. It's
64-
// a convenience wrapper so that structs can be passed in by value instead of
65-
// manually typing struct names as strings.
61+
// auditMap converts a map of struct pointers to a map of struct names as
62+
// strings. It's a convenience wrapper so that structs can be passed in by value
63+
// instead of manually typing struct names as strings.
6664
func auditMap(m map[any]map[string]Action) Map {
6765
out := make(Map, len(m))
6866

@@ -74,35 +72,5 @@ func auditMap(m map[any]map[string]Action) Map {
7472
}
7573

7674
func (t Action) String() string {
77-
switch t {
78-
case ActionIgnore:
79-
return "ignore"
80-
case ActionAuditable:
81-
return "auditable"
82-
case ActionSecret:
83-
return "secret"
84-
default:
85-
return "unknown"
86-
}
87-
}
88-
89-
func (t Action) MarshalJSON() ([]byte, error) {
90-
return []byte(t.String()), nil
91-
}
92-
93-
func (t *Action) UnmarshalJSON(b []byte) error {
94-
str := string(b)
95-
96-
switch str {
97-
case "ignore":
98-
*t = ActionIgnore
99-
case "auditable":
100-
*t = ActionAuditable
101-
case "secret":
102-
*t = ActionSecret
103-
default:
104-
return xerrors.Errorf("unknown AuditType %q", str)
105-
}
106-
107-
return nil
75+
return string(t)
10876
}

0 commit comments

Comments
 (0)