|
| 1 | +import enquirer from 'enquirer'; |
| 2 | +const { prompt } = enquirer; |
| 3 | +import { getIAMConfig } from '@auth/iam.js'; |
| 4 | +import { |
| 5 | + detachPolicyFromAccessKey, |
| 6 | + listPolicies, |
| 7 | + listPoliciesForAccessKey, |
| 8 | +} from '@tigrisdata/iam'; |
| 9 | +import { failWithError } from '@utils/exit.js'; |
| 10 | +import { confirm, requireInteractive } from '@utils/interactive.js'; |
| 11 | +import { msg, printStart, printSuccess } from '@utils/messages.js'; |
| 12 | +import { getFormat, getOption } from '@utils/options.js'; |
| 13 | + |
| 14 | +const context = msg('access-keys', 'detach-policy'); |
| 15 | + |
| 16 | +export default async function detachPolicy(options: Record<string, unknown>) { |
| 17 | + printStart(context); |
| 18 | + |
| 19 | + const format = getFormat(options); |
| 20 | + |
| 21 | + const id = getOption<string>(options, ['id']); |
| 22 | + let policyArn = getOption<string>(options, ['policyArn', 'policy-arn']); |
| 23 | + const force = getOption<boolean>(options, ['yes', 'y', 'force']); |
| 24 | + |
| 25 | + if (!id) { |
| 26 | + failWithError(context, 'Access key ID is required'); |
| 27 | + } |
| 28 | + |
| 29 | + const config = await getIAMConfig(context); |
| 30 | + |
| 31 | + if (!policyArn) { |
| 32 | + requireInteractive('Use --policy-arn to specify the policy ARN'); |
| 33 | + |
| 34 | + // Fetch assigned policy names and all policies to resolve ARNs |
| 35 | + const [assignedResult, allPoliciesResult] = await Promise.all([ |
| 36 | + listPoliciesForAccessKey(id, { config }), |
| 37 | + listPolicies({ config }), |
| 38 | + ]); |
| 39 | + |
| 40 | + if (assignedResult.error) { |
| 41 | + failWithError(context, assignedResult.error); |
| 42 | + } |
| 43 | + |
| 44 | + if (allPoliciesResult.error) { |
| 45 | + failWithError(context, allPoliciesResult.error); |
| 46 | + } |
| 47 | + |
| 48 | + const assignedNames = new Set(assignedResult.data.policies); |
| 49 | + const assigned = allPoliciesResult.data.policies.filter((p) => |
| 50 | + assignedNames.has(p.name) |
| 51 | + ); |
| 52 | + |
| 53 | + if (assigned.length === 0) { |
| 54 | + failWithError(context, 'No policies are attached to this access key.'); |
| 55 | + } |
| 56 | + |
| 57 | + const { selected } = await prompt<{ selected: string }>({ |
| 58 | + type: 'select', |
| 59 | + name: 'selected', |
| 60 | + message: 'Select a policy to detach:', |
| 61 | + choices: assigned.map((p) => ({ |
| 62 | + name: p.resource, |
| 63 | + message: `${p.name} (${p.resource})`, |
| 64 | + })), |
| 65 | + }); |
| 66 | + |
| 67 | + policyArn = selected; |
| 68 | + } |
| 69 | + |
| 70 | + if (!force) { |
| 71 | + requireInteractive('Use --yes to skip confirmation'); |
| 72 | + const confirmed = await confirm( |
| 73 | + `Detach policy '${policyArn}' from access key '${id}'?` |
| 74 | + ); |
| 75 | + if (!confirmed) { |
| 76 | + console.log('Aborted'); |
| 77 | + return; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + const { error } = await detachPolicyFromAccessKey(id, policyArn, { config }); |
| 82 | + |
| 83 | + if (error) { |
| 84 | + failWithError(context, error); |
| 85 | + } |
| 86 | + |
| 87 | + if (format === 'json') { |
| 88 | + console.log(JSON.stringify({ action: 'detached', id, policyArn })); |
| 89 | + } |
| 90 | + |
| 91 | + printSuccess(context); |
| 92 | +} |
0 commit comments