|
| 1 | +import * as cxapi from '@aws-cdk/cx-api'; |
| 2 | +import { environmentsFromDescriptors } from './private'; |
| 3 | +import type { Tag } from '../../api/aws-cdk'; |
| 4 | +import type { ICloudAssemblySource } from '../../api/cloud-assembly'; |
| 5 | +import { ALL_STACKS } from '../../api/cloud-assembly/private'; |
| 6 | +import { assemblyFromSource } from '../../toolkit/private'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Create manage bootstrap environments |
| 10 | + */ |
| 11 | +export class BootstrapEnvironments { |
| 12 | + /** |
| 13 | + * Create from a list of environment descriptors |
| 14 | + * List of strings like `['aws://012345678912/us-east-1', 'aws://234567890123/eu-west-1']` |
| 15 | + */ |
| 16 | + static fromList(environments: string[]): BootstrapEnvironments { |
| 17 | + return new BootstrapEnvironments(environmentsFromDescriptors(environments)); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Create from a cloud assembly source |
| 22 | + */ |
| 23 | + static fromCloudAssemblySource(cx: ICloudAssemblySource): BootstrapEnvironments { |
| 24 | + return new BootstrapEnvironments(async () => { |
| 25 | + const assembly = await assemblyFromSource(cx); |
| 26 | + const stackCollection = assembly.selectStacksV2(ALL_STACKS); |
| 27 | + return stackCollection.stackArtifacts.map(stack => stack.environment); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + private constructor(private readonly envProvider: cxapi.Environment[] | (() => Promise<cxapi.Environment[]>)) { |
| 32 | + } |
| 33 | + |
| 34 | + async getEnvironments(): Promise<cxapi.Environment[]> { |
| 35 | + if (Array.isArray(this.envProvider)) { |
| 36 | + return this.envProvider; |
| 37 | + } |
| 38 | + return this.envProvider(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Options for Bootstrap |
| 44 | + */ |
| 45 | +export interface BootstrapOptions { |
| 46 | + |
| 47 | + /** |
| 48 | + * Bootstrap environment parameters for CloudFormation used when deploying the bootstrap stack |
| 49 | + * @default BootstrapEnvironmentParameters.onlyExisting() |
| 50 | + */ |
| 51 | + readonly parameters?: BootstrapStackParameters; |
| 52 | + |
| 53 | + /** |
| 54 | + * The template source of the bootstrap stack |
| 55 | + * |
| 56 | + * @default BootstrapSource.default() |
| 57 | + */ |
| 58 | + readonly source?: { source: 'default' } | { source: 'custom'; templateFile: string }; |
| 59 | + |
| 60 | + /** |
| 61 | + * Whether to execute the changeset or only create it and leave it in review |
| 62 | + * @default true |
| 63 | + */ |
| 64 | + readonly execute?: boolean; |
| 65 | + |
| 66 | + /** |
| 67 | + * Tags for cdktoolkit stack |
| 68 | + * |
| 69 | + * @default [] |
| 70 | + */ |
| 71 | + readonly tags?: Tag[]; |
| 72 | + |
| 73 | + /** |
| 74 | + * Whether the stacks created by the bootstrap process should be protected from termination |
| 75 | + * @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-protect-stacks.html |
| 76 | + * @default true |
| 77 | + */ |
| 78 | + readonly terminationProtection?: boolean; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Parameter values for the bootstrapping template |
| 83 | + */ |
| 84 | +export interface BootstrapParameters { |
| 85 | + /** |
| 86 | + * The name to be given to the CDK Bootstrap bucket |
| 87 | + * By default, a name is generated by CloudFormation |
| 88 | + * |
| 89 | + * @default - No value, optional argument |
| 90 | + */ |
| 91 | + readonly bucketName?: string; |
| 92 | + |
| 93 | + /** |
| 94 | + * The ID of an existing KMS key to be used for encrypting items in the bucket |
| 95 | + * By default, the default KMS key is used |
| 96 | + * |
| 97 | + * @default - No value, optional argument |
| 98 | + */ |
| 99 | + readonly kmsKeyId?: string; |
| 100 | + |
| 101 | + /** |
| 102 | + * Whether or not to create a new customer master key (CMK) |
| 103 | + * |
| 104 | + * Only applies to modern bootstrapping |
| 105 | + * Legacy bootstrapping will never create a CMK, only use the default S3 key |
| 106 | + * |
| 107 | + * @default false |
| 108 | + */ |
| 109 | + readonly createCustomerMasterKey?: boolean; |
| 110 | + |
| 111 | + /** |
| 112 | + * The list of AWS account IDs that are trusted to deploy into the environment being bootstrapped |
| 113 | + * |
| 114 | + * @default [] |
| 115 | + */ |
| 116 | + readonly trustedAccounts?: string[]; |
| 117 | + |
| 118 | + /** |
| 119 | + * The list of AWS account IDs that are trusted to look up values in the environment being bootstrapped |
| 120 | + * |
| 121 | + * @default [] |
| 122 | + */ |
| 123 | + readonly trustedAccountsForLookup?: string[]; |
| 124 | + |
| 125 | + /** |
| 126 | + * The list of AWS account IDs that should not be trusted by the bootstrapped environment |
| 127 | + * If these accounts are already trusted, they will be removed on bootstrapping |
| 128 | + * |
| 129 | + * @default [] |
| 130 | + */ |
| 131 | + readonly untrustedAccounts?: string[]; |
| 132 | + |
| 133 | + /** |
| 134 | + * The ARNs of the IAM managed policies that should be attached to the role performing CloudFormation deployments |
| 135 | + * In most cases, this will be the AdministratorAccess policy |
| 136 | + * At least one policy is required if `trustedAccounts` were passed |
| 137 | + * |
| 138 | + * @default [] |
| 139 | + */ |
| 140 | + readonly cloudFormationExecutionPolicies?: string[]; |
| 141 | + |
| 142 | + /** |
| 143 | + * Identifier to distinguish multiple bootstrapped environments |
| 144 | + * The default qualifier is an arbitrary but unique string |
| 145 | + * |
| 146 | + * @default - 'hnb659fds' |
| 147 | + */ |
| 148 | + readonly qualifier?: string; |
| 149 | + |
| 150 | + /** |
| 151 | + * Whether or not to enable S3 Staging Bucket Public Access Block Configuration |
| 152 | + * |
| 153 | + * @default true |
| 154 | + */ |
| 155 | + readonly publicAccessBlockConfiguration?: boolean; |
| 156 | + |
| 157 | + /** |
| 158 | + * Flag for using the default permissions boundary for bootstrapping |
| 159 | + * |
| 160 | + * @default - No value, optional argument |
| 161 | + */ |
| 162 | + readonly examplePermissionsBoundary?: boolean; |
| 163 | + |
| 164 | + /** |
| 165 | + * Name for the customer's custom permissions boundary for bootstrapping |
| 166 | + * |
| 167 | + * @default - No value, optional argument |
| 168 | + */ |
| 169 | + readonly customPermissionsBoundary?: string; |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Parameters of the bootstrapping template with flexible configuration options |
| 174 | + */ |
| 175 | +export class BootstrapStackParameters { |
| 176 | + /** |
| 177 | + * Use only existing parameters on the stack. |
| 178 | + */ |
| 179 | + public static onlyExisting() { |
| 180 | + return new BootstrapStackParameters({}, true); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Use exactly these parameters and remove any other existing parameters from the stack. |
| 185 | + */ |
| 186 | + public static exactly(params: BootstrapParameters) { |
| 187 | + return new BootstrapStackParameters(params, false); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Define additional parameters for the stack, while keeping existing parameters for unspecified values. |
| 192 | + */ |
| 193 | + public static withExisting(params: BootstrapParameters) { |
| 194 | + return new BootstrapStackParameters(params, true); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * The parameters as a Map for easy access and manipulation |
| 199 | + */ |
| 200 | + public readonly parameters?: BootstrapParameters; |
| 201 | + public readonly keepExistingParameters: boolean; |
| 202 | + |
| 203 | + private constructor(params?: BootstrapParameters, usePreviousParameters = true) { |
| 204 | + this.keepExistingParameters = usePreviousParameters; |
| 205 | + this.parameters = params; |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +/** |
| 210 | + * Source configuration for bootstrap operations |
| 211 | + */ |
| 212 | +export class BootstrapSource { |
| 213 | + /** |
| 214 | + * Use the default bootstrap template |
| 215 | + */ |
| 216 | + static default(): BootstrapOptions['source'] { |
| 217 | + return { source: 'default' }; |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * Use a custom bootstrap template |
| 222 | + */ |
| 223 | + static customTemplate(templateFile: string): BootstrapOptions['source'] { |
| 224 | + return { |
| 225 | + source: 'custom', |
| 226 | + templateFile, |
| 227 | + }; |
| 228 | + } |
| 229 | +} |
0 commit comments