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
Move code around
  • Loading branch information
Emyrk committed Mar 10, 2023
commit 87cda86b954d8fc664864e14027518df1d5299b6
68 changes: 34 additions & 34 deletions enterprise/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,6 @@ func (t *Table) Add(key string, value map[string]Action) *Table {
return t
}

// entry is a helper function that ensures all entries in the table are valid
// audit.Auditable types. It also ensures all json tags have a corresponding
// action.
func entry[A audit.Auditable](v A, f map[string]Action) (string, map[string]Action) {
vt := reflect.TypeOf(v)
for vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}

// This should never happen because audit.Audible only allows structs in
// its union.
if vt.Kind() != reflect.Struct {
panic(fmt.Sprintf("audit table entry value must be a struct, got %T", v))
}

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") == "-" {
// 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))
}
}

return structName(vt), f
}

// AuditableResources contains a definitive list of all auditable resources and
// which fields are auditable. All resource types must be valid audit.Auditable
// types.
Expand Down Expand Up @@ -210,6 +176,40 @@ var AuditableResources = (&Table{}).
"uuid": ActionTrack,
}))

// entry is a helper function that ensures all entries in the table are valid
// audit.Auditable types. It also ensures all json tags have a corresponding
// action.
func entry[A audit.Auditable](v A, f map[string]Action) (string, map[string]Action) {
vt := reflect.TypeOf(v)
for vt.Kind() == reflect.Ptr {
vt = vt.Elem()
}

// This should never happen because audit.Audible only allows structs in
// its union.
if vt.Kind() != reflect.Struct {
panic(fmt.Sprintf("audit table entry value must be a struct, got %T", v))
}

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") == "-" {
// 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))
}
}

return structName(vt), f
}

// auditMap converts a map of struct pointers to a map of struct names as
// strings. It's a convenience wrapper so that structs can be passed in by value
// instead of manually typing struct names as strings.
Expand Down