Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/lib/events/catalog/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { DomainEventMap, EventType } from '../domain-events';
import { codeReportEvents } from './code-report.events';
import { vaultEvents } from './vault.events';
import type {
EventCatalog,
EventDefinition,
Expand All @@ -11,6 +12,7 @@ import type {
export type * from './types';
export { OPERATORS } from './fields';
export { CODE_REPORT_TOOLS } from './code-report.events';
export { VAULT_SECRET_CHANGED_FIELDS } from './vault.events';

/**
* Builds the catalog. The parameter type makes it exhaustive (every `DomainEventMap` entry needs a
Expand All @@ -32,6 +34,7 @@ export function defineEventCatalog<const C extends EventCatalog>(catalog: C): C

export const EVENT_CATALOG: EventCatalog = defineEventCatalog({
...codeReportEvents,
...vaultEvents,
});

export function isEventType(value: string): value is EventType {
Expand Down
135 changes: 135 additions & 0 deletions src/lib/events/catalog/vault.events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { OPERATORS, enumOptions, eventText, fieldLabel } from './fields';
import type { EventDefinitions } from './types';

export const VAULT_SECRET_CHANGED_FIELDS = ['key', 'description', 'folder', 'values'] as const;

const READ_SECRETS = 'project:vault:secrets:read';
const READ_ENVIRONMENTS = 'project:vault:environments:read';

const SECRET_CREATED = 'vault.secret.created';
const SECRET_UPDATED = 'vault.secret.updated';
const SECRET_DELETED = 'vault.secret.deleted';
const ENV_IMPORTED = 'vault.env.imported';
const ENVIRONMENT_CREATED = 'vault.environment.created';
const ENVIRONMENT_DELETED = 'vault.environment.deleted';

type SecretEvent = typeof SECRET_CREATED | typeof SECRET_UPDATED | typeof SECRET_DELETED;
type EnvironmentEvent = typeof ENVIRONMENT_CREATED | typeof ENVIRONMENT_DELETED;

function secretFields<K extends SecretEvent>(type: K) {
return [
{ key: 'key', label: fieldLabel(type, 'key'), type: 'string', operators: OPERATORS.string },
{
key: 'environmentIds',
label: fieldLabel(type, 'environmentIds'),
type: 'reference',
reference: 'vault_environment',
operators: OPERATORS.list,
},
{
key: 'folderPath',
label: fieldLabel(type, 'folderPath'),
type: 'string',
operators: OPERATORS.string,
},
] as const;
}

function environmentFields<K extends EnvironmentEvent>(type: K) {
return [
{
key: 'environmentId',
label: fieldLabel(type, 'environmentId'),
type: 'reference',
reference: 'vault_environment',
operators: OPERATORS.reference,
},
{ key: 'name', label: fieldLabel(type, 'name'), type: 'string', operators: OPERATORS.string },
] as const;
}

export const vaultEvents: EventDefinitions<SecretEvent | typeof ENV_IMPORTED | EnvironmentEvent> = {
[SECRET_CREATED]: {
...eventText(SECRET_CREATED),
module: 'vault',
scope: 'project',
readPermission: READ_SECRETS,
alertable: true,
fields: secretFields(SECRET_CREATED),
},
[SECRET_UPDATED]: {
...eventText(SECRET_UPDATED),
module: 'vault',
scope: 'project',
readPermission: READ_SECRETS,
alertable: true,
fields: [
...secretFields(SECRET_UPDATED),
{
key: 'changedFields',
label: fieldLabel(SECRET_UPDATED, 'changedFields'),
type: 'enum',
options: enumOptions('secretChangedField', VAULT_SECRET_CHANGED_FIELDS),
operators: OPERATORS.list,
},
],
},
[SECRET_DELETED]: {
...eventText(SECRET_DELETED),
module: 'vault',
scope: 'project',
readPermission: READ_SECRETS,
alertable: true,
fields: secretFields(SECRET_DELETED),
},
[ENV_IMPORTED]: {
...eventText(ENV_IMPORTED),
module: 'vault',
scope: 'project',
readPermission: READ_SECRETS,
alertable: true,
fields: [
{
key: 'environmentId',
label: fieldLabel(ENV_IMPORTED, 'environmentId'),
type: 'reference',
reference: 'vault_environment',
operators: OPERATORS.reference,
},
{
key: 'folderPath',
label: fieldLabel(ENV_IMPORTED, 'folderPath'),
type: 'string',
operators: OPERATORS.string,
},
{
key: 'created',
label: fieldLabel(ENV_IMPORTED, 'created'),
type: 'number',
operators: OPERATORS.number,
},
{
key: 'updated',
label: fieldLabel(ENV_IMPORTED, 'updated'),
type: 'number',
operators: OPERATORS.number,
},
],
},
[ENVIRONMENT_CREATED]: {
...eventText(ENVIRONMENT_CREATED),
module: 'vault',
scope: 'project',
readPermission: READ_ENVIRONMENTS,
alertable: true,
fields: environmentFields(ENVIRONMENT_CREATED),
},
[ENVIRONMENT_DELETED]: {
...eventText(ENVIRONMENT_DELETED),
module: 'vault',
scope: 'project',
readPermission: READ_ENVIRONMENTS,
alertable: true,
fields: environmentFields(ENVIRONMENT_DELETED),
},
};
38 changes: 38 additions & 0 deletions src/lib/events/domain-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,50 @@ export type CodeReportServicePayload = {
slug: string;
};

export type VaultSecretChangedField = 'key' | 'description' | 'folder' | 'values';

/** Never carries secret values: only which environments hold (or changed) a value. */
export type VaultSecretPayload = {
secretId: string;
key: string;
folderId: string | null;
/** `/` for the vault root. */
folderPath: string;
/** Environments with a value (created/deleted) or whose value changed (updated). */
environmentIds: string[];
};

export type VaultSecretUpdatedPayload = VaultSecretPayload & {
changedFields: VaultSecretChangedField[];
};

export type VaultEnvImportedPayload = {
environmentId: string;
environmentSlug: string;
folderId: string | null;
folderPath: string;
created: number;
updated: number;
};

export type VaultEnvironmentPayload = {
environmentId: string;
name: string;
slug: string;
};

export interface DomainEventMap {
'code_report.analysis.completed': CodeReportAnalysisCompletedPayload;
'code_report.analysis.failed': CodeReportAnalysisFailedPayload;
'code_report.policy.violated': CodeReportPolicyViolatedPayload;
'code_report.service.created': CodeReportServicePayload;
'code_report.service.deleted': CodeReportServicePayload;
'vault.secret.created': VaultSecretPayload;
'vault.secret.updated': VaultSecretUpdatedPayload;
'vault.secret.deleted': VaultSecretPayload;
'vault.env.imported': VaultEnvImportedPayload;
'vault.environment.created': VaultEnvironmentPayload;
'vault.environment.deleted': VaultEnvironmentPayload;
}

export type EventType = Extract<keyof DomainEventMap, string>;
68 changes: 68 additions & 0 deletions src/lib/i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,74 @@
"enforcement": {
"warn": "Warning",
"block": "Blocking"
},
"secretChangedField": {
"key": "Key",
"description": "Description",
"folder": "Folder",
"values": "Values"
}
},
"vault": {
"secret": {
"created": {
"label": "Secret created",
"description": "A secret was created in the project vault.",
"fields": {
"key": "Secret key",
"environmentIds": "Environments",
"folderPath": "Folder"
}
},
"updated": {
"label": "Secret updated",
"description": "A secret of the project vault was modified.",
"fields": {
"key": "Secret key",
"environmentIds": "Environments",
"folderPath": "Folder",
"changedFields": "Changed fields"
}
},
"deleted": {
"label": "Secret deleted",
"description": "A secret was deleted from the project vault.",
"fields": {
"key": "Secret key",
"environmentIds": "Environments",
"folderPath": "Folder"
}
}
},
"env": {
"imported": {
"label": ".env file imported",
"description": "Secrets were imported from a .env file.",
"fields": {
"environmentId": "Environment",
"folderPath": "Folder",
"created": "Secrets created",
"updated": "Secrets updated"
}
}
},
"environment": {
"created": {
"label": "Environment created",
"description": "An environment was created in the project vault.",
"fields": {
"environmentId": "Environment",
"name": "Name"
}
},
"deleted": {
"label": "Environment deleted",
"description": "An environment was deleted from the project vault.",
"fields": {
"environmentId": "Environment",
"name": "Name"
}
}
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions src/lib/i18n/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,74 @@
"enforcement": {
"warn": "Aviso",
"block": "Bloqueante"
},
"secretChangedField": {
"key": "Key",
"description": "Descripción",
"folder": "Carpeta",
"values": "Valores"
}
},
"vault": {
"secret": {
"created": {
"label": "Secreto creado",
"description": "Se ha creado un secreto en el vault del proyecto.",
"fields": {
"key": "Key del secreto",
"environmentIds": "Entornos",
"folderPath": "Carpeta"
}
},
"updated": {
"label": "Secreto actualizado",
"description": "Se ha modificado un secreto del vault del proyecto.",
"fields": {
"key": "Key del secreto",
"environmentIds": "Entornos",
"folderPath": "Carpeta",
"changedFields": "Campos modificados"
}
},
"deleted": {
"label": "Secreto eliminado",
"description": "Se ha eliminado un secreto del vault del proyecto.",
"fields": {
"key": "Key del secreto",
"environmentIds": "Entornos",
"folderPath": "Carpeta"
}
}
},
"env": {
"imported": {
"label": "Fichero .env importado",
"description": "Se han importado secretos desde un fichero .env.",
"fields": {
"environmentId": "Entorno",
"folderPath": "Carpeta",
"created": "Secretos creados",
"updated": "Secretos actualizados"
}
}
},
"environment": {
"created": {
"label": "Entorno creado",
"description": "Se ha creado un entorno en el vault del proyecto.",
"fields": {
"environmentId": "Entorno",
"name": "Nombre"
}
},
"deleted": {
"label": "Entorno eliminado",
"description": "Se ha eliminado un entorno del vault del proyecto.",
"fields": {
"environmentId": "Entorno",
"name": "Nombre"
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib/server/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InProcessEventBus, type EventBus } from './event-bus';
export { InProcessEventBus, actorFromRequest } from './event-bus';
export type { EventBus, InProcessEventBusOptions } from './event-bus';
export type * from './types';
export { resolveProjectScope, type ProjectScopeLookup } from './scope';

const GLOBAL_KEY = Symbol.for('gitops.events.bus');

Expand Down
19 changes: 19 additions & 0 deletions src/lib/server/events/scope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { EventScope } from './types';

/** Project lookup used to add the organization to a project-scoped event. */
export type ProjectScopeLookup = {
getProject(id: string): Promise<{ organization?: { id: string } | null }>;
};

/**
* Scope of an event that happened inside a project. A failed lookup only drops the organization:
* resolving the scope must never fail the write that already succeeded.
*/
export async function resolveProjectScope(
projectId: string | null | undefined,
projects: ProjectScopeLookup | undefined,
): Promise<EventScope> {
if (!projectId) return { organizationId: null, projectId: null };
const project = projects ? await projects.getProject(projectId).catch(() => null) : null;
return { organizationId: project?.organization?.id ?? null, projectId };
}
Loading
Loading