-
Notifications
You must be signed in to change notification settings - Fork 2
feat: access key rotation and policy management commands #81
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
11c2a2d
chore: update the iam package
designcode 9e8472f
feat: implement rotate key
designcode 30504fb
feat: implement key to policy commands
designcode 318c37d
fix: attached users in policy get
designcode 97d1b63
feat: implement policy link/unlink to keys commands
designcode f1868e8
refactor: extract shared selectPolicy helper for iam policy commands
designcode 89570b8
fix: guard against undefined users in link-key policy lookup
designcode 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,83 @@ | ||
| import enquirer from 'enquirer'; | ||
| const { prompt } = enquirer; | ||
| import { getIAMConfig } from '@auth/iam.js'; | ||
| import { | ||
| attachPolicyToAccessKey, | ||
| listPolicies, | ||
| listPoliciesForAccessKey, | ||
| } from '@tigrisdata/iam'; | ||
| import { failWithError } from '@utils/exit.js'; | ||
| import { requireInteractive } from '@utils/interactive.js'; | ||
| import { msg, printStart, printSuccess } from '@utils/messages.js'; | ||
| import { getFormat, getOption } from '@utils/options.js'; | ||
|
|
||
| const context = msg('access-keys', 'attach-policy'); | ||
|
|
||
| export default async function attachPolicy(options: Record<string, unknown>) { | ||
| printStart(context); | ||
|
|
||
| const format = getFormat(options); | ||
|
|
||
| const id = getOption<string>(options, ['id']); | ||
| let policyArn = getOption<string>(options, ['policyArn', 'policy-arn']); | ||
|
|
||
| if (!id) { | ||
| failWithError(context, 'Access key ID is required'); | ||
| } | ||
|
|
||
| const config = await getIAMConfig(context); | ||
|
|
||
| if (!policyArn) { | ||
| requireInteractive('Use --policy-arn to specify the policy ARN'); | ||
|
|
||
| // Fetch all policies and assigned policies in parallel | ||
| const [allPoliciesResult, assignedResult] = await Promise.all([ | ||
| listPolicies({ config }), | ||
| listPoliciesForAccessKey(id, { config }), | ||
| ]); | ||
|
|
||
| if (allPoliciesResult.error) { | ||
| failWithError(context, allPoliciesResult.error); | ||
| } | ||
|
|
||
| if (assignedResult.error) { | ||
| failWithError(context, assignedResult.error); | ||
| } | ||
|
|
||
| const assignedNames = new Set(assignedResult.data.policies); | ||
| const available = allPoliciesResult.data.policies.filter( | ||
| (p) => !assignedNames.has(p.name) | ||
| ); | ||
|
|
||
| if (available.length === 0) { | ||
| failWithError( | ||
| context, | ||
| 'No unassigned policies available. All policies are already attached to this access key.' | ||
| ); | ||
| } | ||
|
|
||
| const { selected } = await prompt<{ selected: string }>({ | ||
| type: 'select', | ||
| name: 'selected', | ||
| message: 'Select a policy to attach:', | ||
| choices: available.map((p) => ({ | ||
| name: p.resource, | ||
| message: `${p.name} (${p.resource})`, | ||
| })), | ||
| }); | ||
|
|
||
| policyArn = selected; | ||
| } | ||
|
|
||
| const { error } = await attachPolicyToAccessKey(id, policyArn, { config }); | ||
|
|
||
| if (error) { | ||
| failWithError(context, error); | ||
| } | ||
|
|
||
| if (format === 'json') { | ||
| console.log(JSON.stringify({ action: 'attached', id, policyArn })); | ||
| } | ||
|
|
||
| printSuccess(context); | ||
| } | ||
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,92 @@ | ||
| import enquirer from 'enquirer'; | ||
| const { prompt } = enquirer; | ||
| import { getIAMConfig } from '@auth/iam.js'; | ||
| import { | ||
| detachPolicyFromAccessKey, | ||
| listPolicies, | ||
| listPoliciesForAccessKey, | ||
| } from '@tigrisdata/iam'; | ||
| import { failWithError } from '@utils/exit.js'; | ||
| import { confirm, requireInteractive } from '@utils/interactive.js'; | ||
| import { msg, printStart, printSuccess } from '@utils/messages.js'; | ||
| import { getFormat, getOption } from '@utils/options.js'; | ||
|
|
||
| const context = msg('access-keys', 'detach-policy'); | ||
|
|
||
| export default async function detachPolicy(options: Record<string, unknown>) { | ||
| printStart(context); | ||
|
|
||
| const format = getFormat(options); | ||
|
|
||
| const id = getOption<string>(options, ['id']); | ||
| let policyArn = getOption<string>(options, ['policyArn', 'policy-arn']); | ||
| const force = getOption<boolean>(options, ['yes', 'y', 'force']); | ||
|
|
||
| if (!id) { | ||
| failWithError(context, 'Access key ID is required'); | ||
| } | ||
|
|
||
| const config = await getIAMConfig(context); | ||
|
|
||
| if (!policyArn) { | ||
| requireInteractive('Use --policy-arn to specify the policy ARN'); | ||
|
|
||
| // Fetch assigned policy names and all policies to resolve ARNs | ||
| const [assignedResult, allPoliciesResult] = await Promise.all([ | ||
| listPoliciesForAccessKey(id, { config }), | ||
| listPolicies({ config }), | ||
| ]); | ||
|
|
||
| if (assignedResult.error) { | ||
| failWithError(context, assignedResult.error); | ||
| } | ||
|
|
||
| if (allPoliciesResult.error) { | ||
| failWithError(context, allPoliciesResult.error); | ||
| } | ||
|
|
||
| const assignedNames = new Set(assignedResult.data.policies); | ||
| const assigned = allPoliciesResult.data.policies.filter((p) => | ||
| assignedNames.has(p.name) | ||
| ); | ||
|
|
||
| if (assigned.length === 0) { | ||
| failWithError(context, 'No policies are attached to this access key.'); | ||
| } | ||
|
|
||
| const { selected } = await prompt<{ selected: string }>({ | ||
| type: 'select', | ||
| name: 'selected', | ||
| message: 'Select a policy to detach:', | ||
| choices: assigned.map((p) => ({ | ||
| name: p.resource, | ||
| message: `${p.name} (${p.resource})`, | ||
| })), | ||
| }); | ||
|
|
||
| policyArn = selected; | ||
| } | ||
|
|
||
| if (!force) { | ||
| requireInteractive('Use --yes to skip confirmation'); | ||
| const confirmed = await confirm( | ||
| `Detach policy '${policyArn}' from access key '${id}'?` | ||
| ); | ||
| if (!confirmed) { | ||
| console.log('Aborted'); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| const { error } = await detachPolicyFromAccessKey(id, policyArn, { config }); | ||
|
|
||
| if (error) { | ||
| failWithError(context, error); | ||
| } | ||
|
|
||
| if (format === 'json') { | ||
| console.log(JSON.stringify({ action: 'detached', id, policyArn })); | ||
| } | ||
|
|
||
| printSuccess(context); | ||
| } |
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,73 @@ | ||
| import { getIAMConfig } from '@auth/iam.js'; | ||
| import { listPoliciesForAccessKey } from '@tigrisdata/iam'; | ||
| import { failWithError } from '@utils/exit.js'; | ||
| import { formatPaginatedOutput } from '@utils/format.js'; | ||
| import { | ||
| msg, | ||
| printEmpty, | ||
| printPaginationHint, | ||
| printStart, | ||
| printSuccess, | ||
| } from '@utils/messages.js'; | ||
| import { getFormat, getPaginationOptions } from '@utils/options.js'; | ||
| import { getOption } from '@utils/options.js'; | ||
|
|
||
| const context = msg('access-keys', 'list-policies'); | ||
|
|
||
| export default async function listPolicies(options: Record<string, unknown>) { | ||
| printStart(context); | ||
|
|
||
| const format = getFormat(options); | ||
| const { limit, pageToken } = getPaginationOptions(options); | ||
|
|
||
| const id = getOption<string>(options, ['id']); | ||
|
|
||
| if (!id) { | ||
| failWithError(context, 'Access key ID is required'); | ||
| } | ||
|
|
||
| const config = await getIAMConfig(context); | ||
|
|
||
| const { data, error } = await listPoliciesForAccessKey(id, { | ||
| ...(limit !== undefined ? { limit } : {}), | ||
| ...(pageToken ? { paginationToken: pageToken } : {}), | ||
| config, | ||
| }); | ||
|
|
||
| if (error) { | ||
| failWithError(context, error); | ||
| } | ||
|
|
||
| if (!data.policies || data.policies.length === 0) { | ||
| printEmpty(context); | ||
| return; | ||
| } | ||
|
|
||
| const policies = data.policies.map((name) => ({ policy: name })); | ||
|
|
||
| const columns = [ | ||
| { | ||
| key: 'policy', | ||
| header: policies.length > 1 ? 'Attached Policies' : 'Attached Policy', | ||
| }, | ||
| ]; | ||
|
|
||
| const nextToken = data.paginationToken || undefined; | ||
|
|
||
| const output = formatPaginatedOutput( | ||
| policies, | ||
| format!, | ||
| 'policies', | ||
| 'policy', | ||
| columns, | ||
| { paginationToken: nextToken } | ||
| ); | ||
|
|
||
| console.log(output); | ||
|
|
||
| if (format !== 'json' && format !== 'xml') { | ||
| printPaginationHint(nextToken); | ||
| } | ||
|
|
||
| printSuccess(context, { count: policies.length }); | ||
| } |
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,65 @@ | ||
| import { getIAMConfig } from '@auth/iam.js'; | ||
| import { rotateAccessKey } from '@tigrisdata/iam'; | ||
| import { | ||
| failWithError, | ||
| getSuccessNextActions, | ||
| printNextActions, | ||
| } from '@utils/exit.js'; | ||
| import { confirm, requireInteractive } from '@utils/interactive.js'; | ||
| import { msg, printStart, printSuccess } from '@utils/messages.js'; | ||
| import { getFormat, getOption } from '@utils/options.js'; | ||
|
|
||
| const context = msg('access-keys', 'rotate'); | ||
|
|
||
| export default async function rotate(options: Record<string, unknown>) { | ||
| printStart(context); | ||
|
|
||
| const format = getFormat(options); | ||
|
|
||
| const id = getOption<string>(options, ['id']); | ||
| const force = getOption<boolean>(options, ['yes', 'y', 'force']); | ||
|
|
||
| if (!id) { | ||
| failWithError(context, 'Access key ID is required'); | ||
| } | ||
|
|
||
| if (!force) { | ||
| requireInteractive('Use --yes to skip confirmation'); | ||
| const confirmed = await confirm( | ||
| `Rotate access key '${id}'? The current secret will be invalidated.` | ||
| ); | ||
| if (!confirmed) { | ||
| console.log('Aborted'); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| const config = await getIAMConfig(context); | ||
|
|
||
| const { data, error } = await rotateAccessKey(id, { config }); | ||
|
|
||
| if (error) { | ||
| failWithError(context, error); | ||
| } | ||
|
|
||
| if (format === 'json') { | ||
| const nextActions = getSuccessNextActions(context, { id: data.id }); | ||
| const output: Record<string, unknown> = { | ||
| action: 'rotated', | ||
| id: data.id, | ||
| secret: data.newSecret, | ||
| }; | ||
| if (nextActions.length > 0) output.nextActions = nextActions; | ||
| console.log(JSON.stringify(output)); | ||
| } else { | ||
| console.log(` Access Key ID: ${data.id}`); | ||
| console.log(` New Secret Access Key: ${data.newSecret}`); | ||
| console.log(''); | ||
| console.log( | ||
| ' Save these credentials securely. The secret will not be shown again.' | ||
| ); | ||
| } | ||
|
|
||
| printSuccess(context); | ||
| printNextActions(context, { id: data.id }); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.