Skip to content

chore: Ensure all audit types in ResourceTable match APGL #6563

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 8 commits into from
Mar 10, 2023
Merged
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
Prev Previous commit
Next Next commit
Implement more checks to ensure all tracked fields are present
  • Loading branch information
Emyrk committed Mar 10, 2023
commit 00837a5f12abdd14fe0902bdebbd6d2a82da665b
35 changes: 23 additions & 12 deletions enterprise/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (t *Table) Add(key string, value map[string]Action) *Table {
// AuditableResources contains a definitive list of all auditable resources and
// which fields are auditable. All resource types must be valid audit.Auditable
// types.
var AuditableResources = (&Table{}).
var AuditableResources = *(&Table{}).
Add(entry(database.GitSSHKey{}, map[string]Action{
"user_id": ActionTrack,
"created_at": ActionIgnore, // Never changes, but is implicit and not helpful in a diff.
Expand Down Expand Up @@ -80,9 +80,7 @@ var AuditableResources = (&Table{}).
"description": ActionTrack,
"icon": ActionTrack,
"default_ttl": ActionTrack,
"min_autostart_interval": ActionTrack,
"created_by": ActionTrack,
"is_private": ActionTrack,
"group_acl": ActionTrack,
"user_acl": ActionTrack,
"allow_user_cancel_workspace_jobs": ActionTrack,
Expand Down Expand Up @@ -192,19 +190,32 @@ func entry[A audit.Auditable](v A, f map[string]Action) (string, map[string]Acti
}

name := structName(vt)
// Ensure all json tags have a corresponding action.
for i := 0; i < vt.NumField(); i++ {
field := vt.Field(i)
if !field.IsExported() {
continue
}
if field.Tag.Get("json") == "-" {

// Use the flattenStructFields to recurse anonymously embedded structs
vv := reflect.ValueOf(v)
diffs, err := flattenStructFields(vv, vv)
if err != nil {
panic(fmt.Sprintf("audit table entry type %T failed to flatten", v))
}

fcpy := make(map[string]Action, len(f))
for k, v := range f {
fcpy[k] = v
}
for _, d := range diffs {
jsonTag := d.FieldType.Tag.Get("json")
if jsonTag == "-" {
// This field is explicitly ignored.
continue
}
if _, ok := f[field.Name]; !ok {
panic(fmt.Sprintf("audit table entry missing action for field %q in type %q", field.Name, name))
if _, ok := fcpy[jsonTag]; !ok {
panic(fmt.Sprintf("audit table entry missing action for field %q in type %q", d.FieldType.Name, name))
}
delete(fcpy, jsonTag)
}

if len(fcpy) > 0 {
panic(fmt.Sprintf("audit table entry has extra actions for type %q: %v", name, fcpy))
}

return structName(vt), f
Expand Down