|
| 1 | +package audit |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + |
| 6 | + "golang.org/x/xerrors" |
| 7 | + |
| 8 | + "github.com/coder/coder/coderd/database" |
| 9 | +) |
| 10 | + |
| 11 | +// Auditable is mostly a marker interface. It contains a definitive list of all |
| 12 | +// auditable types. If you want to audit a new type, first define it in |
| 13 | +// AuditableResources, then add it to this interface. |
| 14 | +type Auditable interface { |
| 15 | + database.User | |
| 16 | + database.Workspace |
| 17 | +} |
| 18 | + |
| 19 | +type Action int |
| 20 | + |
| 21 | +const ( |
| 22 | + // ActionIgnore ignores diffing for the field. |
| 23 | + ActionIgnore = iota |
| 24 | + // ActionAuditable includes the value in the diff if the value changed. |
| 25 | + ActionAuditable |
| 26 | + // ActionSecret includes a zero value of the same type if the value changed. |
| 27 | + // It lets you indicate that a value changed, but without leaking its |
| 28 | + // contents. |
| 29 | + ActionSecret |
| 30 | +) |
| 31 | + |
| 32 | +// Map is a map of struct names to a map of field names that indicate that |
| 33 | +// field's AuditType. |
| 34 | +type Map map[string]map[string]Action |
| 35 | + |
| 36 | +// AuditableResources contains a definitive list of all auditable resources and |
| 37 | +// which fields are auditable. |
| 38 | +var AuditableResources = auditMap(map[any]map[string]Action{ |
| 39 | + &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. |
| 49 | + }, |
| 50 | + &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. |
| 60 | + }, |
| 61 | +}) |
| 62 | + |
| 63 | +// auditMap converts a typed AuditMap into |
| 64 | +func auditMap(m map[any]map[string]Action) Map { |
| 65 | + out := make(Map, len(m)) |
| 66 | + |
| 67 | + for k, v := range m { |
| 68 | + out[reflect.TypeOf(k).Elem().Name()] = v |
| 69 | + } |
| 70 | + |
| 71 | + return out |
| 72 | +} |
| 73 | + |
| 74 | +func (t Action) String() string { |
| 75 | + switch t { |
| 76 | + case ActionIgnore: |
| 77 | + return "ignore" |
| 78 | + case ActionAuditable: |
| 79 | + return "auditable" |
| 80 | + case ActionSecret: |
| 81 | + return "secret" |
| 82 | + default: |
| 83 | + return "unknown" |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func (t Action) MarshalJSON() ([]byte, error) { |
| 88 | + return []byte(t.String()), nil |
| 89 | +} |
| 90 | + |
| 91 | +func (t *Action) UnmarshalJSON(b []byte) error { |
| 92 | + str := string(b) |
| 93 | + |
| 94 | + switch str { |
| 95 | + case "ignore": |
| 96 | + *t = ActionIgnore |
| 97 | + case "auditable": |
| 98 | + *t = ActionAuditable |
| 99 | + case "secret": |
| 100 | + *t = ActionSecret |
| 101 | + default: |
| 102 | + return xerrors.Errorf("unknown AuditType %q", str) |
| 103 | + } |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
0 commit comments