Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ in `docs/adr/0001-event-bus-and-alerts.md`.
(enforced by a test).
- Publish only **after** the GitDB write succeeded, with the right `scope`
(`organizationId` / `projectId`). `publish()` returns immediately; handlers run asynchronously,
in order per subscriber, and a failing handler is only logged.
in order per subscriber, and a failing handler is only logged. Use `publishAfterWrite`
(`#lib/server/events/publish`) so resolving the scope or publishing can never fail a write that
already succeeded; `resolveProjectScope` adds the organization to project events.
- Delivery is at-most-once and per process; events are not persisted.
- The actor comes from the request context (`userId` / `apiKeyId` set in `hooks.server.ts`),
falling back to `system`.
Expand Down
156 changes: 156 additions & 0 deletions src/lib/events/catalog/access.events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { OPERATORS, enumOptions, eventText, fieldLabel } from './fields';
import type { EventDefinitions } from './types';

export const ACCESS_SCOPES = ['cluster', 'organization', 'project'] as const;

const READ_USERS = 'organization:users:read';

const GRANTED = 'access.granted';
const UPDATED = 'access.updated';
const REVOKED = 'access.revoked';
const INVITED = 'user.invited';
const ACCEPTED = 'invitation.accepted';
const ROLE_UPDATED = 'role.updated';
const KEY_CREATED = 'api_key.created';
const KEY_REVOKED = 'api_key.revoked';

type AccessEvent = typeof GRANTED | typeof UPDATED | typeof REVOKED;
type InvitationEvent = typeof INVITED | typeof ACCEPTED;

function userField(type: AccessEvent | InvitationEvent) {
return {
key: 'userId',
label: fieldLabel(type, 'userId'),
type: 'reference',
reference: 'user',
operators: OPERATORS.reference,
} as const;
}

function roleField(type: AccessEvent | InvitationEvent | typeof ROLE_UPDATED) {
return {
key: 'roleId',
label: fieldLabel(type, 'roleId'),
type: 'reference',
reference: 'role',
operators: OPERATORS.reference,
} as const;
}

function scopeField(type: AccessEvent | typeof ACCEPTED) {
return {
key: 'scope',
label: fieldLabel(type, 'scope'),
type: 'enum',
options: enumOptions('accessScope', ACCESS_SCOPES),
operators: OPERATORS.enum,
} as const;
}

function accessFields(type: AccessEvent) {
return [userField(type), roleField(type), scopeField(type)] as const;
}

function permissionsField(key: 'addedPermissions' | 'removedPermissions') {
return {
key,
label: fieldLabel(ROLE_UPDATED, key),
type: 'string',
operators: OPERATORS.list,
} as const;
}

// access events also feed the fixed system notifications (#81) for the affected user
export const accessEvents: EventDefinitions<
AccessEvent | InvitationEvent | typeof ROLE_UPDATED | typeof KEY_CREATED | typeof KEY_REVOKED
> = {
[GRANTED]: {
...eventText(GRANTED),
module: 'access',
scope: 'organization',
readPermission: READ_USERS,
alertable: true,
fields: accessFields(GRANTED),
},
[UPDATED]: {
...eventText(UPDATED),
module: 'access',
scope: 'organization',
readPermission: READ_USERS,
alertable: true,
fields: accessFields(UPDATED),
},
[REVOKED]: {
...eventText(REVOKED),
module: 'access',
scope: 'organization',
readPermission: READ_USERS,
alertable: true,
fields: accessFields(REVOKED),
},
[INVITED]: {
...eventText(INVITED),
module: 'access',
scope: 'organization',
readPermission: READ_USERS,
alertable: true,
fields: [userField(INVITED), roleField(INVITED)],
},
[ACCEPTED]: {
...eventText(ACCEPTED),
module: 'access',
scope: 'organization',
readPermission: READ_USERS,
alertable: true,
fields: [userField(ACCEPTED), roleField(ACCEPTED), scopeField(ACCEPTED)],
},
[ROLE_UPDATED]: {
...eventText(ROLE_UPDATED),
module: 'access',
scope: 'organization',
readPermission: 'organization:roles:read',
alertable: true,
fields: [
roleField(ROLE_UPDATED),
permissionsField('addedPermissions'),
permissionsField('removedPermissions'),
],
},
[KEY_CREATED]: {
...eventText(KEY_CREATED),
module: 'access',
scope: 'project',
readPermission: 'project:server-keys:read',
alertable: true,
fields: [
{
key: 'name',
label: fieldLabel(KEY_CREATED, 'name'),
type: 'string',
operators: OPERATORS.string,
},
],
},
[KEY_REVOKED]: {
...eventText(KEY_REVOKED),
module: 'access',
scope: 'project',
readPermission: 'project:server-keys:read',
alertable: true,
fields: [
{
key: 'apiKeyId',
label: fieldLabel(KEY_REVOKED, 'apiKeyId'),
type: 'reference',
reference: 'api_key',
operators: OPERATORS.reference,
},
{
key: 'name',
label: fieldLabel(KEY_REVOKED, 'name'),
type: 'string',
operators: OPERATORS.string,
},
],
},
};
3 changes: 3 additions & 0 deletions src/lib/events/catalog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { codeReportEvents } from './code-report.events';
import { vaultEvents } from './vault.events';
import { projectEvents } from './projects.events';
import { organizationEvents } from './organization.events';
import { accessEvents } from './access.events';
import type {
EventCatalog,
EventDefinition,
Expand All @@ -17,6 +18,7 @@ export { CODE_REPORT_TOOLS } from './code-report.events';
export { VAULT_SECRET_CHANGED_FIELDS } from './vault.events';
export { PROJECT_CHANGED_FIELDS } from './projects.events';
export { ORGANIZATION_CHANGED_FIELDS } from './organization.events';
export { ACCESS_SCOPES } from './access.events';

/**
* Builds the catalog. The parameter type makes it exhaustive (every `DomainEventMap` entry needs a
Expand All @@ -41,6 +43,7 @@ export const EVENT_CATALOG: EventCatalog = defineEventCatalog({
...vaultEvents,
...projectEvents,
...organizationEvents,
...accessEvents,
});

export function isEventType(value: string): value is EventType {
Expand Down
59 changes: 59 additions & 0 deletions src/lib/events/domain-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,57 @@ export type OrganizationUpdatedPayload = {
changedFields: OrganizationChangedField[];
};

export type AccessScope = 'cluster' | 'organization' | 'project';

export type AccessStatus = 'active' | 'invited';

/** A role assignment: a cluster account role, or a membership in an organization or project. */
export type AccessPayload = {
/** The user-access entry id (the user id for cluster-level access). */
accessId: string;
userId: string;
roleId: string | null;
scope: AccessScope;
status: AccessStatus;
};

export type AccessUpdatedPayload = AccessPayload & {
previousRoleId: string | null;
previousStatus: AccessStatus;
};

/** Never carries the invitation token, its URL or the invitee's email. */
export type UserInvitedPayload = {
userId: string;
roleId: string;
organizationId: string;
};

export type InvitationAcceptedPayload = {
userId: string;
accessId: string;
roleId: string | null;
scope: AccessScope;
};

export type RoleUpdatedPayload = {
roleId: string;
name: string;
scope: AccessScope;
nameChanged: boolean;
addedPermissions: string[];
removedPermissions: string[];
};

/** Never carries the key or its hash: `keyPrefix` is the public, non-secret identifier. */
export type ApiKeyPayload = {
apiKeyId: string;
name: string;
keyPrefix: string;
projectId: string | null;
userId: string | null;
};

export interface DomainEventMap {
'code_report.analysis.completed': CodeReportAnalysisCompletedPayload;
'code_report.analysis.failed': CodeReportAnalysisFailedPayload;
Expand All @@ -137,6 +188,14 @@ export interface DomainEventMap {
'project.updated': ProjectUpdatedPayload;
'project.deleted': ProjectPayload;
'organization.updated': OrganizationUpdatedPayload;
'access.granted': AccessPayload;
'access.updated': AccessUpdatedPayload;
'access.revoked': AccessPayload;
'user.invited': UserInvitedPayload;
'invitation.accepted': InvitationAcceptedPayload;
'role.updated': RoleUpdatedPayload;
'api_key.created': ApiKeyPayload;
'api_key.revoked': ApiKeyPayload;
}

export type EventType = Extract<keyof DomainEventMap, string>;
83 changes: 83 additions & 0 deletions src/lib/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,11 @@
"name": "Name",
"slug": "Slug",
"description": "Description"
},
"accessScope": {
"cluster": "GitOps",
"organization": "Organization",
"project": "Project"
}
},
"vault": {
Expand Down Expand Up @@ -1154,6 +1159,84 @@
"changedFields": "Changed fields"
}
}
},
"access": {
"granted": {
"label": "Access granted",
"description": "A user was given access with a role.",
"fields": {
"userId": "User",
"roleId": "Role",
"scope": "Scope"
}
},
"updated": {
"label": "Access changed",
"description": "The role or status of a user access changed.",
"fields": {
"userId": "User",
"roleId": "Role",
"scope": "Scope"
}
},
"revoked": {
"label": "Access revoked",
"description": "A user access was removed.",
"fields": {
"userId": "User",
"roleId": "Role",
"scope": "Scope"
}
}
},
"user": {
"invited": {
"label": "User invited",
"description": "A user was invited to the organization.",
"fields": {
"userId": "User",
"roleId": "Role"
}
}
},
"invitation": {
"accepted": {
"label": "Invitation accepted",
"description": "A user accepted their invitation.",
"fields": {
"userId": "User",
"roleId": "Role",
"scope": "Scope"
}
}
},
"role": {
"updated": {
"label": "Role updated",
"description": "The name or permissions of a role changed.",
"fields": {
"roleId": "Role",
"addedPermissions": "Added permissions",
"removedPermissions": "Removed permissions"
}
}
},
"api_key": {
"created": {
"label": "API key created",
"description": "An API key was created.",
"fields": {
"name": "Name"
}
},
"revoked": {
"label": "API key revoked",
"description": "An API key was revoked.",
"fields": {
"apiKeyId": "API key",
"name": "Name"
}
}
}
}
}
Loading
Loading