-
-
Notifications
You must be signed in to change notification settings - Fork 47
feat(aws): add IAM to Cloud Explorer #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thomhurst
wants to merge
2
commits into
floci-io:main
Choose a base branch
from
thomhurst:agent/aws-iam-cloud-explorer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import {describe, expect, test} from 'bun:test' | ||
| import { | ||
| CreateUserCommand, | ||
| DeleteUserCommand, | ||
| GetUserCommand, | ||
| ListUsersCommand, | ||
| type IAMClient, | ||
| } from '@aws-sdk/client-iam' | ||
| import {AwsIamAdapter} from './AwsIamAdapter' | ||
|
|
||
| function fakeClient(handler: (command: unknown) => unknown): IAMClient { | ||
| return { | ||
| send: async (command: unknown) => handler(command), | ||
| } as unknown as IAMClient | ||
| } | ||
|
|
||
| const alice = { | ||
| UserName: 'alice', | ||
| UserId: 'AIDAALICE', | ||
| Arn: 'arn:aws:iam::000000000000:user/team/alice', | ||
| Path: '/team/', | ||
| CreateDate: new Date('2026-01-02T03:04:05.000Z'), | ||
| } | ||
|
|
||
| describe('AwsIamAdapter', () => { | ||
| test('lists every page and maps IAM users to normalized resources', async () => { | ||
| const adapter = new AwsIamAdapter(fakeClient((command) => { | ||
| if (!(command instanceof ListUsersCommand)) throw new Error('Unexpected command') | ||
| if (!command.input.Marker) return {Users: [alice], IsTruncated: true, Marker: 'next'} | ||
| return {Users: [{...alice, UserName: 'bob', UserId: 'AIDABOB'}], IsTruncated: false} | ||
| })) | ||
|
|
||
| const result = await adapter.list() | ||
|
|
||
| expect(result).toHaveLength(2) | ||
| expect(result[0]).toMatchObject({ | ||
| id: 'alice', | ||
| name: 'alice', | ||
| cloud: 'aws', | ||
| service: 'identity', | ||
| type: 'iam-user', | ||
| region: null, | ||
| createdAt: '2026-01-02T03:04:05.000Z', | ||
| }) | ||
| expect(result[0].metadata).toMatchObject({ | ||
| identityService: 'iam', | ||
| userId: 'AIDAALICE', | ||
| path: '/team/', | ||
| }) | ||
| }) | ||
|
|
||
| test('filters users by search term', async () => { | ||
| const adapter = new AwsIamAdapter(fakeClient(() => ({ | ||
| Users: [alice, {...alice, UserName: 'bob'}], | ||
| IsTruncated: false, | ||
| }))) | ||
|
|
||
| const result = await adapter.list({search: 'ALI'}) | ||
|
|
||
| expect(result.map((resource) => resource.name)).toEqual(['alice']) | ||
| }) | ||
|
|
||
| test('gets and maps one IAM user', async () => { | ||
| const adapter = new AwsIamAdapter(fakeClient((command) => { | ||
| expect(command).toBeInstanceOf(GetUserCommand) | ||
| return {User: alice} | ||
| })) | ||
|
|
||
| const result = await adapter.get('alice') | ||
|
|
||
| expect(result?.id).toBe('alice') | ||
| expect(result?.metadata.arn).toBe(alice.Arn) | ||
| }) | ||
|
|
||
| test('returns null when IAM reports a missing user', async () => { | ||
| const adapter = new AwsIamAdapter(fakeClient(() => { | ||
| throw Object.assign(new Error('missing'), {name: 'NoSuchEntityException'}) | ||
| })) | ||
|
|
||
| await expect(adapter.get('missing')).resolves.toBeNull() | ||
| }) | ||
|
|
||
| test('creates an IAM user with an optional path', async () => { | ||
| const adapter = new AwsIamAdapter(fakeClient((command) => { | ||
| expect(command).toBeInstanceOf(CreateUserCommand) | ||
| expect((command as CreateUserCommand).input).toEqual({UserName: 'alice', Path: '/team/'}) | ||
| return {User: alice} | ||
| })) | ||
|
|
||
| const result = await adapter.create({values: {userName: 'alice', path: '/team/'}}) | ||
|
|
||
| expect(result.id).toBe('alice') | ||
| }) | ||
|
|
||
| test('rejects invalid user names before calling IAM', async () => { | ||
| let called = false | ||
| const adapter = new AwsIamAdapter(fakeClient(() => { | ||
| called = true | ||
| return {} | ||
| })) | ||
|
|
||
| await expect(adapter.create({values: {userName: 'not valid'}})).rejects.toThrow('Use 1-64') | ||
| expect(called).toBeFalse() | ||
| }) | ||
|
|
||
| test('deletes the requested IAM user', async () => { | ||
| const adapter = new AwsIamAdapter(fakeClient((command) => { | ||
| expect(command).toBeInstanceOf(DeleteUserCommand) | ||
| expect((command as DeleteUserCommand).input.UserName).toBe('alice') | ||
| return {} | ||
| })) | ||
|
|
||
| await adapter.delete('alice') | ||
| }) | ||
|
|
||
| test('returns the AWS IAM schema', () => { | ||
| const adapter = new AwsIamAdapter(fakeClient(() => ({}))) | ||
|
|
||
| expect(adapter.schema()).toMatchObject({ | ||
| cloud: 'aws', | ||
| service: 'identity', | ||
| displayName: 'AWS IAM users', | ||
| actions: ['list', 'create', 'delete', 'inspect'], | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { | ||
| CreateUserCommand, | ||
| DeleteUserCommand, | ||
| GetUserCommand, | ||
| ListUsersCommand, | ||
| type IAMClient, | ||
| type User, | ||
| } from '@aws-sdk/client-iam' | ||
| import {iam as defaultIam} from '../aws' | ||
| import {awsIamSchema} from '../cloud-spi/iamSchema' | ||
| import type {CloudResource, CloudServiceAdapter, CreateResourceInput, ResourceQuery, ServiceSchema} from '../cloud-spi/types' | ||
|
|
||
| export class AwsIamAdapter implements CloudServiceAdapter { | ||
| readonly cloud = 'aws' as const | ||
| readonly service = 'identity' as const | ||
|
|
||
| constructor(private readonly iam: IAMClient = defaultIam) {} | ||
|
|
||
| schema(): ServiceSchema { | ||
| return awsIamSchema() | ||
| } | ||
|
|
||
| async list(query: ResourceQuery = {}): Promise<CloudResource[]> { | ||
| const users: User[] = [] | ||
| let marker: string | undefined | ||
|
|
||
| do { | ||
| const res = await this.iam.send(new ListUsersCommand({Marker: marker})) | ||
| users.push(...(res.Users ?? [])) | ||
| marker = res.IsTruncated ? res.Marker : undefined | ||
| } while (marker) | ||
|
|
||
| return filterBySearch(users.map(toResource), query.search) | ||
| } | ||
|
|
||
| async get(id: string): Promise<CloudResource | null> { | ||
| try { | ||
| const res = await this.iam.send(new GetUserCommand({UserName: id})) | ||
| return res.User ? toResource(res.User) : null | ||
| } catch (error) { | ||
| if (isNotFound(error)) return null | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| async create(input: CreateResourceInput): Promise<CloudResource> { | ||
| const userName = stringValue(input.values.userName) | ||
| const path = stringValue(input.values.path) | ||
| if (!userName) throw new Error('userName is required') | ||
| if (!/^[A-Za-z0-9_+=,.@-]{1,64}$/.test(userName)) { | ||
| throw new Error('Use 1-64 letters, numbers, and +=,.@_- characters.') | ||
| } | ||
| if (path && (path.length > 512 || !/^\/(?:[!-~]+\/)?$/.test(path))) { | ||
| throw new Error('Path must begin and end with / and contain only printable ASCII characters.') | ||
| } | ||
|
|
||
| const res = await this.iam.send(new CreateUserCommand({ | ||
| UserName: userName, | ||
| Path: path || undefined, | ||
| })) | ||
| if (!res.User) throw new Error('AWS IAM did not return the created user') | ||
| return toResource(res.User) | ||
| } | ||
|
|
||
| async delete(id: string): Promise<void> { | ||
| await this.iam.send(new DeleteUserCommand({UserName: id})) | ||
| } | ||
| } | ||
|
|
||
| function toResource(user: User): CloudResource { | ||
| const userName = user.UserName ?? '' | ||
| return { | ||
| id: userName, | ||
| name: userName, | ||
| cloud: 'aws', | ||
| service: 'identity', | ||
| type: 'iam-user', | ||
| region: null, | ||
| createdAt: user.CreateDate?.toISOString() ?? null, | ||
| metadata: { | ||
| provider: 'aws', | ||
| identityService: 'iam', | ||
| userId: user.UserId, | ||
| arn: user.Arn, | ||
| path: user.Path, | ||
| permissionsBoundary: user.PermissionsBoundary, | ||
| tags: user.Tags, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| function stringValue(value: unknown): string { | ||
| return typeof value === 'string' ? value.trim() : '' | ||
| } | ||
|
|
||
| function filterBySearch(resources: CloudResource[], search?: string): CloudResource[] { | ||
| const normalized = search?.trim().toLowerCase() | ||
| if (!normalized) return resources | ||
| return resources.filter((resource) => resource.name.toLowerCase().includes(normalized)) | ||
| } | ||
|
|
||
| function isNotFound(error: unknown): boolean { | ||
| if (typeof error !== 'object' || error === null) return false | ||
| const candidate = error as {name?: string; $metadata?: {httpStatusCode?: number}} | ||
| return candidate.name === 'NoSuchEntityException' || candidate.$metadata?.httpStatusCode === 404 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type {CapabilitySchema, CloudProvider, ResourceActionName, ServiceSchema} from './types' | ||
|
|
||
| const resourceActions: CapabilitySchema<ResourceActionName>[] = [ | ||
| {name: 'list', label: 'List users', enabled: true, status: 'available', runtimeRequired: true}, | ||
| {name: 'create', label: 'Create user', enabled: true, status: 'available', runtimeRequired: true}, | ||
| {name: 'delete', label: 'Delete user', enabled: true, status: 'available', runtimeRequired: true}, | ||
| {name: 'inspect', label: 'Inspect user', enabled: true, status: 'available', runtimeRequired: false}, | ||
| ] | ||
|
|
||
| export function awsIamSchema(): ServiceSchema { | ||
| return { | ||
| cloud: 'aws', | ||
| service: 'identity', | ||
| displayName: 'AWS IAM users', | ||
| fields: [ | ||
| { | ||
| name: 'userName', | ||
| label: 'User Name', | ||
| type: 'text', | ||
| required: true, | ||
| description: '1-64 letters, numbers, and +=,.@_- characters.', | ||
| validation: { | ||
| pattern: '^[A-Za-z0-9_+=,.@-]+$', | ||
| minLength: 1, | ||
| maxLength: 64, | ||
| message: 'Use 1-64 letters, numbers, and +=,.@_- characters.', | ||
| }, | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| }, | ||
| { | ||
| name: 'path', | ||
| label: 'Path', | ||
| type: 'text', | ||
| required: false, | ||
| description: 'Optional IAM path, beginning and ending with /. Defaults to /.', | ||
| validation: { | ||
| pattern: '^/(?:[!-~]+/)?$', | ||
| maxLength: 512, | ||
| message: 'Path must begin and end with / and contain only printable ASCII characters.', | ||
| }, | ||
| }, | ||
| ], | ||
| actions: ['list', 'create', 'delete', 'inspect'], | ||
| capabilities: {resourceActions}, | ||
| filters: [{name: 'search', label: 'Search', type: 'text', required: false}], | ||
| columns: [ | ||
| {name: 'name', label: 'User Name'}, | ||
| {name: 'type', label: 'Type'}, | ||
| {name: 'createdAt', label: 'Created At'}, | ||
| ], | ||
| } | ||
| } | ||
|
|
||
| export function iamSchemaFor(cloud: CloudProvider): ServiceSchema | null { | ||
| return cloud === 'aws' ? awsIamSchema() : null | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deletefails silently for users with attached resourcesAWS IAM
DeleteUserraisesDeleteConflictExceptionif the user still has group memberships, access keys, signing certificates, MFA devices, or inline/attached policies. The adapter issuesDeleteUserCommandwith no pre-flight cleanup and no dedicated error handling.normalizeRuntimeErrorwon't match the exception and maps it to a generic 502 response, so the UI shows "Runtime request failed" rather than anything actionable. Even in a local emulator context, users imported from fixtures or created outside this UI will often have access keys, making delete consistently unusable without a clearer error path.