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
5 changes: 5 additions & 0 deletions .changeset/source-connectors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@open-iga/aws': patch
---

Differentiate source and sink connector to support onboarding HR systems
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ bun run typecheck # type-check app and test sources
## Writing a connector

A connector is plain TypeScript that defines its capabilities against `@open-iga/connector-sdk` and
is compiled to WASM by the same toolchain. It declares:
is compiled to WASM by the same toolchain. Its `type` is either `sink` (a system OpenIGA provisions
into) or `source` (a system of record OpenIGA reads people from).

A `sink` connector declares:

- **Account actions** — lifecycle operations on a managed resource (`create`, `read`, `update`,
`enable`, `disable`, `delete`).
- **Entitlements** — `discover`, `grant`, `revoke` and `read` for assignable permissions (e.g. IAM groups).

A `source` connector declares:

- **Identities** — a paged `list` of people (e.g. employees).

See `connectors/aws` for a complete reference implementation.

## Roadmap
Expand Down
1 change: 1 addition & 0 deletions connectors/aws/src/aws-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { registerIamUserGroupsRevoke } from './entitlements/iam-user-groups/revo
import { registerIamUserGroupsRead } from './entitlements/iam-user-groups/read.ts';

export const awsConnector = new OpenIgaConnector({
type: 'sink',
name: 'AWS',
description: 'AWS plugin for OpenIGA core',
config: [
Expand Down
51 changes: 48 additions & 3 deletions packages/connector-sdk/lib/bun-plugin/validation/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { describe, it, expect } from 'bun:test';
import { validateConfigPlaceHolder, validateAndGenerateConnectorManifest } from './manifest.ts';
import { OpenIgaConnector } from '../../iga/connector/builder.ts';
import type { ConnectorConfig } from '../../iga/connector/validation-schema/connector.config.schema.ts';
import type { ConnectorAccountAction } from '../../iga/connector/validation-schema/connector.account-action.schema.ts';
import type { ConnectorOperation } from '../../iga/connector/validation-schema/connector.operation.schema.ts';

const config = (entries: { name: string; required: boolean }[]): ConnectorConfig =>
entries.map(({ name, required }) => ({ name, description: name, required }));

const endpoints = (urls: string[]): ConnectorAccountAction['endpoints'] =>
const endpoints = (urls: string[]): ConnectorOperation['endpoints'] =>
urls.map((url) => ({ method: 'GET', url, description: 'endpoint' }));

describe('validateConfigPlaceHolder', () => {
Expand Down Expand Up @@ -55,18 +55,22 @@ describe('validateAndGenerateConnectorManifest', () => {

it('should throw error when connector settings is invalid', () => {
const connector = new OpenIgaConnector({
type: 'sink',
// Invalid: name must be a non-empty string
name: '',
description: 'desc',
config: [],
allowedDomains: ['domain.com'],
});

expect(() => validateAndGenerateConnectorManifest(connector)).toThrow(/Validation Failed for connector setting/);
expect(() => validateAndGenerateConnectorManifest(connector)).toThrow(
/Validation Failed for connector setting/,
);
});

it('should throw error when connector action details are invalid', () => {
const connector = new OpenIgaConnector({
type: 'sink',
name: 'connector',
description: 'desc',
config: [{ name: 'REGION', description: 'region', required: true }],
Expand All @@ -84,8 +88,49 @@ describe('validateAndGenerateConnectorManifest', () => {
);
});

it('should enforce the operations allowed for each connector type', () => {
const settings = { name: 'connector', description: 'desc', config: [], allowedDomains: ['domain.com'] };
const operation = {
type: 'list' as const,
description: 'list',
endpoints: endpoints(['https://domain.com']),
handler: () => ({}) as never,
};
const source = new OpenIgaConnector({ ...settings, type: 'source' });
const sink = new OpenIgaConnector({ ...settings, type: 'sink' });

// @ts-expect-error a source connector cannot register account actions
source.registerAccountActions('employee', operation);
// @ts-expect-error a sink connector cannot register identities
sink.registerIdentities('employee', operation);

expect(() => validateAndGenerateConnectorManifest(source)).toThrow(/source connector cannot register/);
expect(() => validateAndGenerateConnectorManifest(sink)).toThrow(/sink connector cannot register/);
});

it('should list the identities of a source connector in the manifest', () => {
const connector = new OpenIgaConnector({
type: 'source',
name: 'connector',
description: 'desc',
config: [],
allowedDomains: ['domain.com'],
}).registerIdentities('employee', {
type: 'list',
description: 'list employees',
endpoints: endpoints(['https://domain.com']),
handler: () => ({ identities: [] }),
});

const manifest = validateAndGenerateConnectorManifest(connector);

expect(manifest.type).toBe('source');
expect(Object.keys(manifest.identities)).toEqual(['employee']);
});

it('should generate a manifest for a valid connector', () => {
const connector = new OpenIgaConnector({
type: 'sink',
name: 'connector',
description: 'desc',
config: [{ name: 'REGION', description: 'region', required: true }],
Expand Down
129 changes: 79 additions & 50 deletions packages/connector-sdk/lib/bun-plugin/validation/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import {
type ConnectorAccountAction,
connectorAccountActionSchema,
} from '../../iga/connector/validation-schema/connector.account-action.schema.ts';
import { connectorEntitlementSchema } from '../../iga/connector/validation-schema/connector.entitlement.schema.ts';
connectorEntitlementSchema,
connectorIdentitySchema,
type ConnectorOperation,
} from '../../iga/connector/validation-schema/connector.operation.schema.ts';
import { z } from 'zod/mini';
import type { ConnectorConfig } from '../../iga/connector/validation-schema/connector.config.schema.ts';
import { OpenIgaConnector } from '../../iga/connector/builder.ts';
import { connectorSettingsSchema } from '../../iga/connector/validation-schema/connector.settings.schema.ts';
import {
type ConnectorSettings,
type ConnectorType,
connectorSettingsSchema,
} from '../../iga/connector/validation-schema/connector.settings.schema.ts';

/**
* Config Template Placeholders regex
Expand All @@ -15,17 +20,19 @@ const CONFIG_PLACEHOLDERS = /\{\{\s*([A-Za-z0-9_-]+)\s*\}\}/g;

// One primitive (create/enable/.../grant/...) as exposed in the manifest. The managed
// resource and the operation type are the keys, so they are not repeated inside.
type ManifestOperation = Pick<z.infer<typeof connectorAccountActionSchema>, 'description' | 'endpoints' | 'config'>;
type ManifestOperation = Pick<ConnectorOperation, 'description' | 'endpoints' | 'config'>;

// Keyed by managed resource, then by operation type:
// actions: { "iam-user": { "create": {...}, "enable": {...} } }
type Manifest = {
type: ConnectorType;
name: string;
description: string;
config: ConnectorConfig;
allowedDomains: string[];
actions: Record<string, Record<string, ManifestOperation>>;
entitlements: Record<string, Record<string, ManifestOperation>>;
identities: Record<string, Record<string, ManifestOperation>>;
};

/**
Expand All @@ -36,7 +43,7 @@ export const validateConfigPlaceHolder = ({
endpoints,
config,
}: {
endpoints: ConnectorAccountAction['endpoints'];
endpoints: ConnectorOperation['endpoints'];
config: ConnectorConfig;
}) => {
const userDefinedConfigSet = new Map(config.map(({ name, required }) => [name, required]));
Expand Down Expand Up @@ -65,6 +72,41 @@ export const validateConfigPlaceHolder = ({
}
};

// Validates one registry and shapes it as managed resource → operation type → operation.
const buildManifestOperations = ({
kind,
registry,
schema,
connectorConfig,
}: {
kind: string;
registry: Map<string, Map<string, unknown>>;
schema: typeof connectorAccountActionSchema | typeof connectorEntitlementSchema | typeof connectorIdentitySchema;
connectorConfig: ConnectorSettings['config'];
}): Record<string, Record<string, ManifestOperation>> => {
// Null-prototype records: managedResource is an arbitrary key, so `actions["__proto__"]` etc.
// must not resolve to an inherited value (would drop the op and mutate Object.prototype).
const operations: Record<string, Record<string, ManifestOperation>> = Object.create(null);

for (const [managedResource, operationByType] of registry) {
for (const [type, operation] of operationByType) {
const result = schema.safeParse(operation);
if (!result.success) {
throw new Error(
`Validation failed for ${kind} type ${type} in the managed resource ${managedResource}. Reason: ${z.prettifyError(result.error)}`,
);
}

const { endpoints, description, config } = result.data;

validateConfigPlaceHolder({ endpoints, config: [...connectorConfig, ...(config ?? [])] });
(operations[managedResource] ??= {})[type] = { endpoints, description, config: config ?? [] };
}
}

return operations;
};

/**
* There are two levels of validation: build and runtime
* Build-time validates the registry. Runtime is part of dispatcher that sits between the host and the connector
Expand All @@ -79,53 +121,40 @@ export const validateAndGenerateConnectorManifest = (connector: unknown): Manife
throw new Error(`Validation Failed for connector setting. Reason: ${z.prettifyError(settingsResult.error)}`);
}

const { name, config, description, allowedDomains } = settingsResult.data;
// Null-prototype records: managedResource is an arbitrary key, so `actions["__proto__"]` etc.
// must not resolve to an inherited value (would drop the op and mutate Object.prototype).
const manifest: Manifest = {
const { type, name, config, description, allowedDomains } = settingsResult.data;

// The register methods are typed per connector type; this catches casts and untyped connectors.
const sinkOperationCount = connector.accountActionsRegistry.size + connector.entitlementRegistry.size;
const mismatchedCount = type === 'source' ? sinkOperationCount : connector.identityRegistry.size;
if (mismatchedCount > 0) {
throw new Error(
`A ${type} connector cannot register ${type === 'source' ? 'account actions or entitlements' : 'identities'}`,
);
}

return {
type,
name,
description,
config,
allowedDomains,
actions: Object.create(null),
entitlements: Object.create(null),
actions: buildManifestOperations({
kind: 'action',
registry: connector.accountActionsRegistry,
schema: connectorAccountActionSchema,
connectorConfig: config,
}),
entitlements: buildManifestOperations({
kind: 'entitlement',
registry: connector.entitlementRegistry,
schema: connectorEntitlementSchema,
connectorConfig: config,
}),
identities: buildManifestOperations({
kind: 'identity',
registry: connector.identityRegistry,
schema: connectorIdentitySchema,
connectorConfig: config,
}),
};

for (const [managedResource, accountActionConfig] of connector.accountActionsRegistry) {
for (const [actionType, action] of accountActionConfig) {
const result = connectorAccountActionSchema.safeParse(action);
if (!result.success) {
throw new Error(
`Validation failed for action type ${actionType} in the managed resource ${managedResource}. Reason: ${z.prettifyError(result.error)}`,
);
}

const { endpoints, description, config } = result.data;

validateConfigPlaceHolder({ endpoints, config: [...settingsResult.data.config, ...(config ?? [])] });
(manifest.actions[managedResource] ??= {})[actionType] = { endpoints, description, config: config ?? [] };
}
}

for (const [managedResource, entitlementConfig] of connector.entitlementRegistry) {
for (const [entitlementType, entitlement] of entitlementConfig) {
const result = connectorEntitlementSchema.safeParse(entitlement);
if (!result.success) {
throw new Error(
`Validation failed for entitlement type ${entitlementType} in the managed resource ${managedResource}. Reason: ${z.prettifyError(result.error)}`,
);
}

const { endpoints, description, config } = result.data;

validateConfigPlaceHolder({ endpoints, config: [...settingsResult.data.config, ...(config ?? [])] });
(manifest.entitlements[managedResource] ??= {})[entitlementType] = {
endpoints,
description,
config: config ?? [],
};
}
}

return manifest;
};
Loading
Loading