@@ -3,8 +3,6 @@ package audit
3
3
import (
4
4
"reflect"
5
5
6
- "golang.org/x/xerrors"
7
-
8
6
"github.com/coder/coder/coderd/database"
9
7
)
10
8
@@ -16,17 +14,17 @@ type Auditable interface {
16
14
database.Workspace
17
15
}
18
16
19
- type Action int
17
+ type Action string
20
18
21
19
const (
22
20
// 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"
26
24
// ActionSecret includes a zero value of the same type if the value changed.
27
25
// It lets you indicate that a value changed, but without leaking its
28
26
// contents.
29
- ActionSecret
27
+ ActionSecret = "secret"
30
28
)
31
29
32
30
// 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
37
35
// which fields are auditable.
38
36
var AuditableResources = auditMap (map [any ]map [string ]Action {
39
37
& 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.
49
47
},
50
48
& 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.
60
58
},
61
59
})
62
60
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.
66
64
func auditMap (m map [any ]map [string ]Action ) Map {
67
65
out := make (Map , len (m ))
68
66
@@ -74,35 +72,5 @@ func auditMap(m map[any]map[string]Action) Map {
74
72
}
75
73
76
74
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 )
108
76
}
0 commit comments