2022-04-25 13:57:59 -05:00
|
|
|
package audit
|
|
|
|
|
|
|
|
|
|
import (
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
2025-01-02 16:02:04 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/idpsync"
|
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.Template |
|
|
|
|
|
database.TemplateVersion |
|
|
|
|
|
database.User |
|
2024-10-22 09:20:54 -05:00
|
|
|
database.WorkspaceTable |
|
2022-10-19 02:00:45 -05:00
|
|
|
database.GitSSHKey |
|
2023-03-10 10:39:02 -06:00
|
|
|
database.WorkspaceBuild |
|
2023-02-14 16:34:13 -05:00
|
|
|
database.AuditableGroup |
|
2023-04-04 15:07:29 -05:00
|
|
|
database.License |
|
2023-06-30 08:38:48 -04:00
|
|
|
database.WorkspaceProxy |
|
2023-11-28 18:15:17 +01:00
|
|
|
database.AuditOAuthConvertState |
|
2024-02-26 15:52:08 -08:00
|
|
|
database.HealthSettings |
|
2024-07-10 16:15:06 +02:00
|
|
|
database.NotificationsSettings |
|
2024-02-26 15:52:08 -08:00
|
|
|
database.OAuth2ProviderApp |
|
2024-06-07 14:11:57 -05:00
|
|
|
database.OAuth2ProviderAppSecret |
|
2025-07-02 17:05:42 +02:00
|
|
|
database.PrebuildsSettings |
|
2024-06-24 09:19:32 -10:00
|
|
|
database.CustomRole |
|
2024-06-25 04:03:15 -10:00
|
|
|
database.AuditableOrganizationMember |
|
2024-08-05 16:18:45 +02:00
|
|
|
database.Organization |
|
2025-01-02 16:02:04 -05:00
|
|
|
database.NotificationTemplate |
|
|
|
|
|
idpsync.OrganizationSyncSettings |
|
|
|
|
|
idpsync.GroupSyncSettings |
|
2025-10-15 19:13:59 +03:00
|
|
|
idpsync.RoleSyncSettings |
|
|
|
|
|
database.TaskTable
|
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 {
|
2025-03-26 01:56:39 -05:00
|
|
|
DiffFn func(old, newVal any) Map
|
2022-04-25 13:57:59 -05:00
|
|
|
}
|
|
|
|
|
|
2022-08-31 16:12:54 -05:00
|
|
|
//nolint:unused
|
2025-03-26 01:56:39 -05:00
|
|
|
func (d Differ) diff(old, newVal any) Map {
|
|
|
|
|
return d.DiffFn(old, newVal)
|
2022-05-19 20:09:27 +01:00
|
|
|
}
|