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/fuzzy-geckos-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@open-iga/aws': minor
---

Support list accounts with cursor, pass tenant info from core and support cursor for entitlement discovery
6 changes: 4 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
- uses: actions/checkout@v6

- uses: oven-sh/setup-bun@v2
with:
bun-version: 1.4.4

- name: Install extism-js
env:
Expand All @@ -34,8 +36,8 @@ jobs:
- name: Install dependencies
run: bun install --frozen-lockfile

- name: Lint, test and compile
run: bunx turbo run "//#lint" test compile
- name: Typecheck, Lint, test and compile
run: bunx turbo run typecheck "//#lint" test compile

changeset-check:
runs-on: ubuntu-latest
Expand Down
67 changes: 61 additions & 6 deletions connectors/aws/e2e/account-acctions.create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { createMockedHost, type MockedHost } from '@open-iga/connector-sdk/test'
import { awsConnector } from '../src/aws-connector.ts';
import { getLoginProfile } from '../src/utils/iam.login-profile.ts';

const tenant = { id: '000000000000' };

describe('account-actions.create', () => {
let host: MockedHost;

Expand All @@ -25,6 +27,7 @@ describe('account-actions.create', () => {
it('should create an iam user with temporary credentials', async () => {
const email = `test.user@openiga.dev`;
const result = await host.callAccountActions('iam-user', 'create', {
tenant,
email,
firstname: 'test',
lastname: 'user',
Expand Down Expand Up @@ -56,28 +59,79 @@ describe('account-actions.create', () => {
expect(profile.passwordResetRequired).toBe(true);
});

it('should return an error for a duplicate email', async () => {
it('should return the existing user without creating a new one for a duplicate email', async () => {
const email = `test.dup-user@openiga.dev`;
const firstname = 'test';
const lastname = 'dup-user';

const first = await host.callAccountActions('iam-user', 'create', { email, firstname, lastname });
const first = await host.callAccountActions('iam-user', 'create', { tenant, email, firstname, lastname });
expect(first.ok).toBe(true);
// for type-safety
if (!first.ok) return;
expect(first.output.created).toBe(true);

const second = await host.callAccountActions('iam-user', 'create', {
const emailsBefore = host.hostFunctions.sentEmails.length;
const second = await host.callAccountActions('iam-user', 'create', { tenant, email, firstname, lastname });

expect(second.ok).toBe(true);
if (second.ok) {
expect(second.output).toEqual({ id: first.output.id, created: false });
}
// No new password is sent for an existing user
expect(host.hostFunctions.sentEmails.length).toBe(emailsBefore);
});

it('should fail instead of reporting success when the existing user has no console access', async () => {
const email = 'no.access@openiga.dev';
const created = await host.callAccountActions('iam-user', 'create', {
tenant,
email,
firstname: 'no',
lastname: 'access',
});
expect(created.ok).toBe(true);
if (!created.ok) return;
await host.callAccountActions('iam-user', 'disable', { tenant, id: created.output.id });

const again = await host.callAccountActions('iam-user', 'create', {
tenant,
email,
firstname,
lastname,
firstname: 'no',
lastname: 'access',
});

expect(again.ok).toBe(false);
if (!again.ok) {
expect(again.error).toContain('without console access');
}
});

it('should not adopt an existing user that belongs to a different email', async () => {
const first = await host.callAccountActions('iam-user', 'create', {
tenant,
email: 'same.name@openiga.dev',
firstname: 'a',
lastname: 'b',
});
expect(first.ok).toBe(true);

// Same IAM username (the email's first part), different person
const second = await host.callAccountActions('iam-user', 'create', {
tenant,
email: 'same.name@other.dev',
firstname: 'c',
lastname: 'd',
});

expect(second.ok).toBe(false);
if (!second.ok) {
expect(second.error).toContain('409');
expect(second.error).toContain('not tagged');
}
});

it('should reject invalid input before calling the upstream', async () => {
const result = await host.callAccountActions('iam-user', 'create', {
tenant,
email: 'not-an-email',
firstname: 'Foo',
lastname: 'Bar',
Expand All @@ -102,6 +156,7 @@ describe('account-actions.create', () => {
});

const result = await hostWithoutRole.callAccountActions('iam-user', 'create', {
tenant,
email: 'no-role@openiga.dev',
firstname: 'Foo',
lastname: 'Bar',
Expand Down
15 changes: 9 additions & 6 deletions connectors/aws/e2e/account-actions.lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { createMockedHost, type MockedHost } from '@open-iga/connector-sdk/test'
import { awsConnector } from '../src/aws-connector.ts';
import { getLoginProfile } from '../src/utils/iam.login-profile.ts';

const tenant = { id: '000000000000' };

const endpoint = () => process.env.AWS_ENDPOINT_URL ?? '';
const credentials = { accessKeyId: 'test', secretAccessKey: 'test' };

Expand Down Expand Up @@ -43,6 +45,7 @@ describe('account-actions.lifecycle', () => {

// Create — user exists with a login profile.
const created = await host.callAccountActions('iam-user', 'create', {
tenant,
email,
firstname: 'life',
lastname: 'cycle',
Expand All @@ -53,36 +56,36 @@ describe('account-actions.lifecycle', () => {
expect(await hasLoginProfile(userName)).toBe(true);

// Read — exists and enabled.
const afterCreate = await host.callAccountActions('iam-user', 'read', { id });
const afterCreate = await host.callAccountActions('iam-user', 'read', { tenant, id });
expect(afterCreate.ok).toBe(true);
if (afterCreate.ok) expect(afterCreate.output).toEqual({ exists: true, enabled: true });

// Disable — console access revoked.
const disabled = await host.callAccountActions('iam-user', 'disable', { id });
const disabled = await host.callAccountActions('iam-user', 'disable', { tenant, id });
expect(disabled.ok).toBe(true);
if (disabled.ok) expect(disabled.output.disabled).toBe(true);
expect(await hasLoginProfile(userName)).toBe(false);

// Read — still exists, now disabled.
const afterDisable = await host.callAccountActions('iam-user', 'read', { id });
const afterDisable = await host.callAccountActions('iam-user', 'read', { tenant, id });
expect(afterDisable.ok).toBe(true);
if (afterDisable.ok) expect(afterDisable.output).toEqual({ exists: true, enabled: false });

// Enable — console access restored and credentials re-sent via the email capability.
const enabled = await host.callAccountActions('iam-user', 'enable', { id });
const enabled = await host.callAccountActions('iam-user', 'enable', { tenant, id });
expect(enabled.ok).toBe(true);
if (enabled.ok) expect(enabled.output.enabled).toBe(true);
expect(await hasLoginProfile(userName)).toBe(true);
expect(host.hostFunctions.sentEmails.at(-1)?.subject).toBe('Your AWS access has been re-enabled');

// Delete — user removed entirely.
const deleted = await host.callAccountActions('iam-user', 'delete', { id });
const deleted = await host.callAccountActions('iam-user', 'delete', { tenant, id });
expect(deleted.ok).toBe(true);
if (deleted.ok) expect(deleted.output.deleted).toBe(true);
expect(await hasLoginProfile(userName)).toBe(false);

// Read — no longer exists.
const afterDelete = await host.callAccountActions('iam-user', 'read', { id });
const afterDelete = await host.callAccountActions('iam-user', 'read', { tenant, id });
expect(afterDelete.ok).toBe(true);
if (afterDelete.ok) expect(afterDelete.output).toEqual({ exists: false, enabled: false });
});
Expand Down
13 changes: 9 additions & 4 deletions connectors/aws/e2e/entitlements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { createMockedHost, type MockedHost } from '@open-iga/connector-sdk/test'
import { awsConnector } from '../src/aws-connector.ts';
import { createGroup } from '../src/utils/iam.group.ts';

const tenant = { id: '000000000000' };

const endpoint = () => process.env.AWS_ENDPOINT_URL ?? '';
const credentials = { accessKeyId: 'test', secretAccessKey: 'test' };

Expand Down Expand Up @@ -36,7 +38,7 @@ describe('entitlements', () => {
});

it('should discover user groups, excluding operator-filtered ones', async () => {
const result = await host.callEntitlements('iam-user-group', 'discover');
const result = await host.callEntitlements('iam-user-group', 'discover', { tenant });
expect(result.ok).toBe(true);
if (!result.ok) return;

Expand All @@ -54,6 +56,7 @@ describe('entitlements', () => {
it('should grant, read, and revoke a group membership for a user', async () => {
// A user to receive the entitlement.
const created = await host.callAccountActions('iam-user', 'create', {
tenant,
email: 'grant.target@openiga.dev',
firstname: 'grant',
lastname: 'target',
Expand All @@ -63,34 +66,36 @@ describe('entitlements', () => {
const userArn = created.output.id;

// Discover to get a real group ARN to grant.
const discovered = await host.callEntitlements('iam-user-group', 'discover');
const discovered = await host.callEntitlements('iam-user-group', 'discover', { tenant });
expect(discovered.ok).toBe(true);
if (!discovered.ok) return;
const groupArn = discovered.output.entitlements.find((e) => e.name === 'ReadOnly')!.entitlementId;

// Grant — the user is added to the group.
const granted = await host.callEntitlements('iam-user-group', 'grant', {
tenant,
id: userArn,
entitlementId: groupArn,
});
expect(granted.ok).toBe(true);
if (granted.ok) expect(granted.output.granted).toBe(true);

// Read (drift detection) — the group ARN is reported back.
const readGranted = await host.callEntitlements('iam-user-group', 'read', { id: userArn });
const readGranted = await host.callEntitlements('iam-user-group', 'read', { tenant, id: userArn });
expect(readGranted.ok).toBe(true);
if (readGranted.ok) expect(readGranted.output.entitlementIds).toContain(groupArn);

// Revoke — the user is removed from the group.
const revoked = await host.callEntitlements('iam-user-group', 'revoke', {
tenant,
id: userArn,
entitlementId: groupArn,
});
expect(revoked.ok).toBe(true);
if (revoked.ok) expect(revoked.output.revoked).toBe(true);

// Read after revoke — no longer reported.
const readRevoked = await host.callEntitlements('iam-user-group', 'read', { id: userArn });
const readRevoked = await host.callEntitlements('iam-user-group', 'read', { tenant, id: userArn });
expect(readRevoked.ok).toBe(true);
if (readRevoked.ok) expect(readRevoked.output.entitlementIds).not.toContain(groupArn);
});
Expand Down
2 changes: 1 addition & 1 deletion connectors/aws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"compile": "open-iga compile --entryPoint src/aws-connector.ts",
"test": "bun test",
"typecheck": "tsc -p tsconfig.app.json && tsc -p tsconfig.test.json"
"typecheck": "bun --check"
},
"dependencies": {
"aws4fetch": "catalog:",
Expand Down
44 changes: 32 additions & 12 deletions connectors/aws/src/account-actions/create.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { sendEmail } from '@open-iga/connector-sdk';
import type awsConnector from '../aws-connector.ts';
import { USER_MANAGEMENT_ROLE } from '../utils/role-config.ts';
import { assumeRole } from '../utils/sts.ts';
import { IAM_ENDPOINT, stsEndpoint, IAM_ACTION_ENDPOINTS } from '../utils/aws-endpoints.ts';
import { resolveRegion } from '../utils/region.ts';
import { createUser } from '../utils/iam.create-user.ts';
import { createLoginProfile } from '../utils/iam.login-profile.ts';
import { createUser, EMAIL_TAG } from '../utils/iam.create-user.ts';
import { getUser } from '../utils/iam.get-user.ts';
import { createLoginProfile, hasLoginProfile } from '../utils/iam.login-profile.ts';
import { generateTemporaryPassword } from '../utils/password.ts';
import { userNameFromEmail } from '../utils/username.ts';

Expand All @@ -17,11 +19,7 @@ export const registerAccountActionCreation = (plugin: typeof awsConnector) => {
description: 'IAM User account creation',
endpoints: IAM_ACTION_ENDPOINTS,
config: [
{
name: 'AWS_USER_MANAGEMENT_ROLE',
description: 'ARN of the least-privilege role assumed (via STS) to manage IAM users',
required: true,
},
USER_MANAGEMENT_ROLE,
{
name: 'AWS_PASSWORD_PATTERN',
description:
Expand All @@ -43,11 +41,32 @@ export const registerAccountActionCreation = (plugin: typeof awsConnector) => {
roleSessionName: 'openiga-aws-iam-user-create',
});

const created = await createUser({
endpoint: IAM_ENDPOINT,
credentials: assumed,
userName: userNameFromEmail(input.email),
});
const userName = userNameFromEmail(input.email);
let created;
try {
created = await createUser({
endpoint: IAM_ENDPOINT,
credentials: assumed,
userName,
email: input.email,
});
} catch (error) {
if (!String(error).includes('EntityAlreadyExists')) throw error;

// Reuse the existing user only if its Email tag matches; the username is just the email's first part.
const existing = await getUser({ endpoint: IAM_ENDPOINT, credentials: assumed, userName });
if (existing.tags[EMAIL_TAG]?.toLowerCase() !== input.email.trim().toLowerCase()) {
throw new Error(
`IAM user "${userName}" already exists and is not tagged ${EMAIL_TAG}=${input.email}`,
);
}

// No login profile means disabled or half-created, so report failure instead of success
if (!(await hasLoginProfile({ endpoint: IAM_ENDPOINT, credentials: assumed, userName }))) {
throw new Error(`IAM user "${userName}" exists without console access; enable or delete it first`);
}
return { id: existing.arn, created: false };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

// To grant the console access to user with a one-time password that must be changed after the first login
const temporaryPassword = generateTemporaryPassword(config.AWS_PASSWORD_PATTERN);
Expand Down Expand Up @@ -77,6 +96,7 @@ export const registerAccountActionCreation = (plugin: typeof awsConnector) => {

return {
id: created.arn,
created: true,
};
},
});
Expand Down
9 changes: 2 additions & 7 deletions connectors/aws/src/account-actions/delete.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type awsConnector from '../aws-connector.ts';
import { USER_MANAGEMENT_ROLE } from '../utils/role-config.ts';
import { assumeRole } from '../utils/sts.ts';
import { IAM_ENDPOINT, stsEndpoint, IAM_ACTION_ENDPOINTS } from '../utils/aws-endpoints.ts';
import { resolveRegion } from '../utils/region.ts';
Expand All @@ -11,13 +12,7 @@ export const registerAccountActionDelete = (plugin: typeof awsConnector) => {
type: 'delete',
description: 'Delete an IAM user',
endpoints: IAM_ACTION_ENDPOINTS,
config: [
{
name: 'AWS_USER_MANAGEMENT_ROLE',
description: 'ARN of the least-privilege role assumed (via STS) to manage IAM users',
required: true,
},
],
config: [USER_MANAGEMENT_ROLE],
handler: async ({ config, input }) => {
const region = resolveRegion(config.AWS_REGION);

Expand Down
9 changes: 2 additions & 7 deletions connectors/aws/src/account-actions/disable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type awsConnector from '../aws-connector.ts';
import { USER_MANAGEMENT_ROLE } from '../utils/role-config.ts';
import { assumeRole } from '../utils/sts.ts';
import { IAM_ENDPOINT, stsEndpoint, IAM_ACTION_ENDPOINTS } from '../utils/aws-endpoints.ts';
import { resolveRegion } from '../utils/region.ts';
Expand All @@ -10,13 +11,7 @@ export const registerAccountActionDisable = (plugin: typeof awsConnector) => {
type: 'disable',
description: 'Disable an IAM user by revoking console access',
endpoints: IAM_ACTION_ENDPOINTS,
config: [
{
name: 'AWS_USER_MANAGEMENT_ROLE',
description: 'ARN of the least-privilege role assumed (via STS) to manage IAM users',
required: true,
},
],
config: [USER_MANAGEMENT_ROLE],
handler: async ({ config, input }) => {
const region = resolveRegion(config.AWS_REGION);

Expand Down
Loading
Loading