|
| 1 | +import * as iam from "@aws-sdk/client-iam"; |
| 2 | +import * as sts from "@aws-sdk/client-sts"; |
| 3 | +import * as cmd from "commander"; |
| 4 | +import { ELogLevel, getLogger } from "../../utils/logger_util.js"; |
| 5 | +import { spinnerError, spinnerSuccess, spinnerWarn, updateSpinnerText } from "../../utils/spinner_util.js"; |
| 6 | +import { addGlobalOptions } from "../../utils/options_util.js"; |
| 7 | +import { getIdToken } from "../../utils/auth_util.js"; |
| 8 | +import { BDIamRole, ERoleType } from "../../../integration/aws/iam_role.js"; |
| 9 | +import { BDAccount } from "../../../integration/boilingdata/account.js"; |
| 10 | +import { BDIntegration } from "../../../integration/bdIntegration.js"; |
| 11 | +import { combineOptsWithSettings } from "../../utils/config_util.js"; |
| 12 | + |
| 13 | +const logger = getLogger("bdcli-aws-taps-iam"); |
| 14 | +logger.setLogLevel(ELogLevel.WARN); |
| 15 | + |
| 16 | +async function iamrole(options: any, _command: cmd.Command): Promise<void> { |
| 17 | + try { |
| 18 | + options = await combineOptsWithSettings(options, logger); |
| 19 | + |
| 20 | + if (options.delete) { |
| 21 | + updateSpinnerText("Not implemented yet. Please delete the IAM Role from AWS Console"); |
| 22 | + spinnerWarn("Not implemented yet. Please delete the IAM Role from AWS Console"); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + updateSpinnerText("Authenticating"); |
| 27 | + const { idToken: token, cached, region } = await getIdToken(logger); |
| 28 | + updateSpinnerText(cached ? "Authenticating: cached" : "Authenticating: success"); |
| 29 | + spinnerSuccess(); |
| 30 | + |
| 31 | + updateSpinnerText("Creating TAPS IAM Role"); |
| 32 | + if (!region) throw new Error("Pass --region parameter or set AWS_REGION env"); |
| 33 | + const bdAccount = new BDAccount({ logger, authToken: token }); |
| 34 | + const stsClient = new sts.STSClient({ region }); |
| 35 | + const bdRole = new BDIamRole({ |
| 36 | + ...options, |
| 37 | + logger, |
| 38 | + roleType: ERoleType.TAP, |
| 39 | + iamClient: new iam.IAMClient({ region }), |
| 40 | + stsClient, |
| 41 | + username: await bdAccount.getUsername(), |
| 42 | + assumeAwsAccount: await bdAccount.getAssumeAwsAccount(), |
| 43 | + assumeCondExternalId: await bdAccount.getExtId(), |
| 44 | + }); |
| 45 | + const bdIntegration = new BDIntegration({ logger, bdAccount, bdRole, stsClient }); |
| 46 | + const policyDocument = await bdIntegration.getTapsPolicyDocument(); |
| 47 | + const iamRoleArn = await bdRole.upsertRole(JSON.stringify(policyDocument)); |
| 48 | + updateSpinnerText(`Creating TAPS IAM Role: ${iamRoleArn}`); |
| 49 | + spinnerSuccess(); |
| 50 | + |
| 51 | + if (!options.createRoleOnly) { |
| 52 | + updateSpinnerText(`Registering TAPS IAM Role: ${iamRoleArn}`); |
| 53 | + await bdAccount.setTapsIamRoleWithPayload(iamRoleArn); |
| 54 | + spinnerSuccess(); |
| 55 | + } |
| 56 | + } catch (err: any) { |
| 57 | + spinnerError(err?.message); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const program = new cmd.Command("bdcli aws taps-iam") |
| 62 | + .addHelpText( |
| 63 | + "beforeAll", |
| 64 | + "If you have an AWS account, you can use this command to create BoilingData assumable AWS IAM Role into " + |
| 65 | + "your AWS account. It is fully owned and controlled by you. The IAM Policy allows deploying Data Taps " + |
| 66 | + "(Lambda Functions with URL) into your account and creating the needed IAM Role for the Data Tap itself" + |
| 67 | + " (service role). \n\nSee the README.md in https://github.com/boilingdata/boilingdata-bdcli " + |
| 68 | + "for more information.\n", |
| 69 | + ) |
| 70 | + .addOption(new cmd.Option("-r, --region <region>", "AWS region")) |
| 71 | + .addOption(new cmd.Option("--delete", "Delete the IAM role")) |
| 72 | + .addOption(new cmd.Option("--create-role-only", "Create the IAM role only and do not update BoilingData")) |
| 73 | + .action(async (options, command) => await iamrole(options, command)); |
| 74 | + |
| 75 | +(async () => { |
| 76 | + await addGlobalOptions(program, logger); |
| 77 | + await program.parseAsync(process.argv); |
| 78 | +})(); |
0 commit comments