mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: implement audit log for custom role edits (#13494)
* chore: implement audit log for custom role edits
This commit is contained in:
@@ -244,7 +244,7 @@ func TestUpsertCustomRoles(t *testing.T) {
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify we can fetch the role
|
||||
// Verify the role is fetched with the lookup filter.
|
||||
roles, err := az.CustomRoles(ctx, database.CustomRolesParams{
|
||||
LookupRoles: []database.NameOrganizationPair{
|
||||
{
|
||||
|
||||
@@ -8415,6 +8415,7 @@ func (q *FakeQuerier) UpsertCustomRole(_ context.Context, arg database.UpsertCus
|
||||
}
|
||||
|
||||
role := database.CustomRole{
|
||||
ID: uuid.New(),
|
||||
Name: arg.Name,
|
||||
DisplayName: arg.DisplayName,
|
||||
OrganizationID: arg.OrganizationID,
|
||||
|
||||
Generated
+8
-2
@@ -147,7 +147,8 @@ CREATE TYPE resource_type AS ENUM (
|
||||
'convert_login',
|
||||
'health_settings',
|
||||
'oauth2_provider_app',
|
||||
'oauth2_provider_app_secret'
|
||||
'oauth2_provider_app_secret',
|
||||
'custom_role'
|
||||
);
|
||||
|
||||
CREATE TYPE startup_script_behavior AS ENUM (
|
||||
@@ -417,13 +418,16 @@ CREATE TABLE custom_roles (
|
||||
user_permissions jsonb DEFAULT '[]'::jsonb NOT NULL,
|
||||
created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
|
||||
organization_id uuid
|
||||
organization_id uuid,
|
||||
id uuid DEFAULT gen_random_uuid() NOT NULL
|
||||
);
|
||||
|
||||
COMMENT ON TABLE custom_roles IS 'Custom roles allow dynamic roles expanded at runtime';
|
||||
|
||||
COMMENT ON COLUMN custom_roles.organization_id IS 'Roles can optionally be scoped to an organization';
|
||||
|
||||
COMMENT ON COLUMN custom_roles.id IS 'Custom roles ID is used purely for auditing purposes. Name is a better unique identifier.';
|
||||
|
||||
CREATE TABLE dbcrypt_keys (
|
||||
number integer NOT NULL,
|
||||
active_key_digest text,
|
||||
@@ -1642,6 +1646,8 @@ CREATE INDEX idx_audit_log_user_id ON audit_logs USING btree (user_id);
|
||||
|
||||
CREATE INDEX idx_audit_logs_time_desc ON audit_logs USING btree ("time" DESC);
|
||||
|
||||
CREATE INDEX idx_custom_roles_id ON custom_roles USING btree (id);
|
||||
|
||||
CREATE UNIQUE INDEX idx_custom_roles_name_lower ON custom_roles USING btree (lower(name));
|
||||
|
||||
CREATE INDEX idx_organization_member_organization_id_uuid ON organization_members USING btree (organization_id);
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
DROP INDEX idx_custom_roles_id;
|
||||
ALTER TABLE custom_roles DROP COLUMN id;
|
||||
@@ -0,0 +1,8 @@
|
||||
-- (name) is the primary key, this column is almost exclusively for auditing.
|
||||
-- Audit logs require a uuid as the unique identifier for a resource.
|
||||
ALTER TABLE custom_roles ADD COLUMN id uuid DEFAULT gen_random_uuid() NOT NULL;
|
||||
COMMENT ON COLUMN custom_roles.id IS 'Custom roles ID is used purely for auditing purposes. Name is a better unique identifier.';
|
||||
|
||||
-- Ensure unique uuids.
|
||||
CREATE INDEX idx_custom_roles_id ON custom_roles (id);
|
||||
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'custom_role';
|
||||
@@ -1222,6 +1222,7 @@ const (
|
||||
ResourceTypeHealthSettings ResourceType = "health_settings"
|
||||
ResourceTypeOauth2ProviderApp ResourceType = "oauth2_provider_app"
|
||||
ResourceTypeOauth2ProviderAppSecret ResourceType = "oauth2_provider_app_secret"
|
||||
ResourceTypeCustomRole ResourceType = "custom_role"
|
||||
)
|
||||
|
||||
func (e *ResourceType) Scan(src interface{}) error {
|
||||
@@ -1275,7 +1276,8 @@ func (e ResourceType) Valid() bool {
|
||||
ResourceTypeConvertLogin,
|
||||
ResourceTypeHealthSettings,
|
||||
ResourceTypeOauth2ProviderApp,
|
||||
ResourceTypeOauth2ProviderAppSecret:
|
||||
ResourceTypeOauth2ProviderAppSecret,
|
||||
ResourceTypeCustomRole:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@@ -1298,6 +1300,7 @@ func AllResourceTypeValues() []ResourceType {
|
||||
ResourceTypeHealthSettings,
|
||||
ResourceTypeOauth2ProviderApp,
|
||||
ResourceTypeOauth2ProviderAppSecret,
|
||||
ResourceTypeCustomRole,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1792,6 +1795,8 @@ type CustomRole struct {
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||
// Roles can optionally be scoped to an organization
|
||||
OrganizationID uuid.NullUUID `db:"organization_id" json:"organization_id"`
|
||||
// Custom roles ID is used purely for auditing purposes. Name is a better unique identifier.
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
}
|
||||
|
||||
// A table used to store the keys used to encrypt the database.
|
||||
|
||||
@@ -5618,7 +5618,7 @@ func (q *sqlQuerier) UpdateReplica(ctx context.Context, arg UpdateReplicaParams)
|
||||
|
||||
const customRoles = `-- name: CustomRoles :many
|
||||
SELECT
|
||||
name, display_name, site_permissions, org_permissions, user_permissions, created_at, updated_at, organization_id
|
||||
name, display_name, site_permissions, org_permissions, user_permissions, created_at, updated_at, organization_id, id
|
||||
FROM
|
||||
custom_roles
|
||||
WHERE
|
||||
@@ -5667,6 +5667,7 @@ func (q *sqlQuerier) CustomRoles(ctx context.Context, arg CustomRolesParams) ([]
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.OrganizationID,
|
||||
&i.ID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -5711,7 +5712,7 @@ ON CONFLICT (name)
|
||||
org_permissions = $5,
|
||||
user_permissions = $6,
|
||||
updated_at = now()
|
||||
RETURNING name, display_name, site_permissions, org_permissions, user_permissions, created_at, updated_at, organization_id
|
||||
RETURNING name, display_name, site_permissions, org_permissions, user_permissions, created_at, updated_at, organization_id, id
|
||||
`
|
||||
|
||||
type UpsertCustomRoleParams struct {
|
||||
@@ -5742,6 +5743,7 @@ func (q *sqlQuerier) UpsertCustomRole(ctx context.Context, arg UpsertCustomRoleP
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.OrganizationID,
|
||||
&i.ID,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user