2022-04-25 13:57:59 -05:00
|
|
|
package audit
|
|
|
|
|
|
|
|
|
|
import (
|
2022-08-31 16:12:54 -05:00
|
|
|
"github.com/coder/coder/coderd/database"
|
2022-04-25 13:57:59 -05:00
|
|
|
)
|
|
|
|
|
|
2022-08-31 16:12:54 -05:00
|
|
|
// Auditable is mostly a marker interface. It contains a definitive list of all
|
|
|
|
|
// auditable types. If you want to audit a new type, first define it in
|
|
|
|
|
// AuditableResources, then add it to this interface.
|
|
|
|
|
type Auditable interface {
|
|
|
|
|
database.APIKey |
|
|
|
|
|
database.Organization |
|
|
|
|
|
database.OrganizationMember |
|
|
|
|
|
database.Template |
|
|
|
|
|
database.TemplateVersion |
|
|
|
|
|
database.User |
|
|
|
|
|
database.Workspace |
|
2022-10-19 02:00:45 -05:00
|
|
|
database.GitSSHKey |
|
2022-10-25 15:34:48 -04:00
|
|
|
database.Group |
|
|
|
|
|
database.WorkspaceBuild
|
2022-08-31 16:12:54 -05:00
|
|
|
}
|
2022-04-25 13:57:59 -05:00
|
|
|
|
2022-08-31 16:12:54 -05:00
|
|
|
// Map is a map of changed fields in an audited resource. It maps field names to
|
|
|
|
|
// the old and new value for that field.
|
|
|
|
|
type Map map[string]OldNew
|
|
|
|
|
|
|
|
|
|
// OldNew is a pair of values representing the old value and the new value.
|
|
|
|
|
type OldNew struct {
|
|
|
|
|
Old any
|
|
|
|
|
New any
|
|
|
|
|
Secret bool
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Empty returns a default value of type T.
|
2022-04-25 13:57:59 -05:00
|
|
|
func Empty[T Auditable]() T {
|
|
|
|
|
var t T
|
|
|
|
|
return t
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Diff compares two auditable resources and produces a Map of the changed
|
|
|
|
|
// values.
|
2022-08-31 16:12:54 -05:00
|
|
|
func Diff[T Auditable](a Auditor, left, right T) Map { return a.diff(left, right) }
|
2022-04-25 13:57:59 -05:00
|
|
|
|
2022-08-31 16:12:54 -05:00
|
|
|
// Differ is used so the enterprise version can implement the diff function in
|
|
|
|
|
// the Auditor feature interface. Only types in the same package as the
|
|
|
|
|
// interface can implement unexported methods.
|
|
|
|
|
type Differ struct {
|
|
|
|
|
DiffFn func(old, new any) Map
|
2022-04-25 13:57:59 -05:00
|
|
|
}
|
|
|
|
|
|
2022-08-31 16:12:54 -05:00
|
|
|
//nolint:unused
|
|
|
|
|
func (d Differ) diff(old, new any) Map {
|
|
|
|
|
return d.DiffFn(old, new)
|
2022-05-19 20:09:27 +01:00
|
|
|
}
|