|
| 1 | +/** |
| 2 | + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +import * as ec2 from "@aws-cdk/aws-ec2"; |
| 15 | +import * as secretsmanager from "@aws-cdk/aws-secretsmanager"; |
| 16 | +// Note: To ensure CDKv2 compatibility, keep the import statement for Construct separate |
| 17 | +import { Construct } from "@aws-cdk/core"; |
| 18 | +import * as defaults from "@aws-solutions-constructs/core"; |
| 19 | +import * as ecs from "@aws-cdk/aws-ecs"; |
| 20 | + |
| 21 | +export interface FargateToSecretsmanagerProps { |
| 22 | + /** |
| 23 | + * Whether the construct is deploying a private or public API. This has implications for the VPC deployed |
| 24 | + * by this construct. |
| 25 | + * |
| 26 | + * @default - none |
| 27 | + */ |
| 28 | + readonly publicApi: boolean; |
| 29 | + /** |
| 30 | + * Optional custom properties for a VPC the construct will create. This VPC will |
| 31 | + * be used by the new Fargate service the construct creates (that's |
| 32 | + * why targetGroupProps can't include a VPC). Providing |
| 33 | + * both this and existingVpc is an error. A Secrets Manager Interface |
| 34 | + * endpoint will be included in this VPC. |
| 35 | + * |
| 36 | + * @default - none |
| 37 | + */ |
| 38 | + readonly vpcProps?: ec2.VpcProps; |
| 39 | + /** |
| 40 | + * An existing VPC in which to deploy the construct. Providing both this and |
| 41 | + * vpcProps is an error. If the client provides an existing Fargate service, |
| 42 | + * this value must be the VPC where the service is running. A Secrets Manager Interface |
| 43 | + * endpoint will be added to this VPC. |
| 44 | + * |
| 45 | + * @default - none |
| 46 | + */ |
| 47 | + readonly existingVpc?: ec2.IVpc; |
| 48 | + /** |
| 49 | + * Optional properties to create a new ECS cluster |
| 50 | + */ |
| 51 | + readonly clusterProps?: ecs.ClusterProps; |
| 52 | + /** |
| 53 | + * The arn of an ECR Repository containing the image to use |
| 54 | + * to generate the containers |
| 55 | + * |
| 56 | + * format: |
| 57 | + * arn:aws:ecr:[region]:[account number]:repository/[Repository Name] |
| 58 | + */ |
| 59 | + readonly ecrRepositoryArn?: string; |
| 60 | + /** |
| 61 | + * The version of the image to use from the repository |
| 62 | + * |
| 63 | + * @default - 'latest' |
| 64 | + */ |
| 65 | + readonly ecrImageVersion?: string; |
| 66 | + /* |
| 67 | + * Optional props to define the container created for the Fargate Service |
| 68 | + * |
| 69 | + * defaults - fargate-defaults.ts |
| 70 | + */ |
| 71 | + readonly containerDefinitionProps?: ecs.ContainerDefinitionProps | any; |
| 72 | + /* |
| 73 | + * Optional props to define the Fargate Task Definition for this construct |
| 74 | + * |
| 75 | + * defaults - fargate-defaults.ts |
| 76 | + */ |
| 77 | + readonly fargateTaskDefinitionProps?: ecs.FargateTaskDefinitionProps | any; |
| 78 | + /** |
| 79 | + * Optional values to override default Fargate Task definition properties |
| 80 | + * (fargate-defaults.ts). The construct will default to launching the service |
| 81 | + * is the most isolated subnets available (precedence: Isolated, Private and |
| 82 | + * Public). Override those and other defaults here. |
| 83 | + * |
| 84 | + * defaults - fargate-defaults.ts |
| 85 | + */ |
| 86 | + readonly fargateServiceProps?: ecs.FargateServiceProps | any; |
| 87 | + /** |
| 88 | + * A Fargate Service already instantiated (probably by another Solutions Construct). If |
| 89 | + * this is specified, then no props defining a new service can be provided, including: |
| 90 | + * existingImageObject, ecrImageVersion, containerDefintionProps, fargateTaskDefinitionProps, |
| 91 | + * ecrRepositoryArn, fargateServiceProps, clusterProps, existingClusterInterface. If this value |
| 92 | + * is provided, then existingContainerDefinitionObject must be provided as well. |
| 93 | + * |
| 94 | + * @default - none |
| 95 | + */ |
| 96 | + readonly existingFargateServiceObject?: ecs.FargateService; |
| 97 | + /* |
| 98 | + * A container definition already instantiated as part of a Fargate service. This must |
| 99 | + * be the container in the existingFargateServiceObject. |
| 100 | + * |
| 101 | + * @default - None |
| 102 | + */ |
| 103 | + readonly existingContainerDefinitionObject?: ecs.ContainerDefinition; |
| 104 | + /** |
| 105 | + * Existing instance of Secret object, providing both this and secretProps will cause an error. |
| 106 | + * |
| 107 | + * @default - Default props are used |
| 108 | + */ |
| 109 | + readonly existingSecretObj?: secretsmanager.Secret; |
| 110 | + /** |
| 111 | + * Optional user-provided props to override the default props for the Secret. |
| 112 | + * |
| 113 | + * @default - Default props are used |
| 114 | + */ |
| 115 | + readonly secretProps?: secretsmanager.SecretProps; |
| 116 | + |
| 117 | + /** |
| 118 | + * Optional Access granted to the Fargate service for the secret. 'Read' or 'ReadWrite |
| 119 | + * |
| 120 | + * @default - 'Read' |
| 121 | + */ |
| 122 | + readonly grantWriteAccess?: string |
| 123 | + /** |
| 124 | + * Optional Name for container environment variable containing the ARN of the secret. |
| 125 | + * |
| 126 | + * @default - SECRET_ARN |
| 127 | + */ |
| 128 | + readonly secretEnvironmentVariableName?: string; |
| 129 | +} |
| 130 | + |
| 131 | +export class FargateToSecretsmanager extends Construct { |
| 132 | + public readonly vpc: ec2.IVpc; |
| 133 | + public readonly service: ecs.FargateService; |
| 134 | + public readonly container: ecs.ContainerDefinition; |
| 135 | + public readonly secret: secretsmanager.Secret; |
| 136 | + |
| 137 | + constructor(scope: Construct, id: string, props: FargateToSecretsmanagerProps) { |
| 138 | + super(scope, id); |
| 139 | + defaults.CheckProps(props); |
| 140 | + defaults.CheckFargateProps(props); |
| 141 | + |
| 142 | + // Other permissions for constructs are accepted as arrays, turning grantWriteAccess into |
| 143 | + // an array to use the same validation function. |
| 144 | + if (props.grantWriteAccess) { |
| 145 | + const allowedPermissions = ['READ', 'READWRITE']; |
| 146 | + defaults.CheckListValues(allowedPermissions, [props.grantWriteAccess.toUpperCase()], 'grantWriteAccess'); |
| 147 | + } |
| 148 | + |
| 149 | + this.vpc = defaults.buildVpc(scope, { |
| 150 | + existingVpc: props.existingVpc, |
| 151 | + defaultVpcProps: props.publicApi ? defaults.DefaultPublicPrivateVpcProps() : defaults.DefaultIsolatedVpcProps(), |
| 152 | + userVpcProps: props.vpcProps, |
| 153 | + constructVpcProps: { enableDnsHostnames: true, enableDnsSupport: true } |
| 154 | + }); |
| 155 | + |
| 156 | + defaults.AddAwsServiceEndpoint(scope, this.vpc, defaults.ServiceEndpointTypes.SECRETS_MANAGER); |
| 157 | + |
| 158 | + if (props.existingFargateServiceObject) { |
| 159 | + this.service = props.existingFargateServiceObject; |
| 160 | + // CheckFargateProps confirms that the container is provided |
| 161 | + this.container = props.existingContainerDefinitionObject!; |
| 162 | + } else { |
| 163 | + [this.service, this.container] = defaults.CreateFargateService( |
| 164 | + scope, |
| 165 | + id, |
| 166 | + this.vpc, |
| 167 | + props.clusterProps, |
| 168 | + props.ecrRepositoryArn, |
| 169 | + props.ecrImageVersion, |
| 170 | + props.fargateTaskDefinitionProps, |
| 171 | + props.containerDefinitionProps, |
| 172 | + props.fargateServiceProps |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + if (props.existingSecretObj) { |
| 177 | + this.secret = props.existingSecretObj; |
| 178 | + } else { |
| 179 | + this.secret = defaults.buildSecretsManagerSecret(this, 'secret', props.secretProps); |
| 180 | + } |
| 181 | + |
| 182 | + // Enable read permissions for the Fargate service role by default |
| 183 | + this.secret.grantRead(this.service.taskDefinition.taskRole); |
| 184 | + |
| 185 | + if (props.grantWriteAccess) { |
| 186 | + const permission = props.grantWriteAccess.toUpperCase(); |
| 187 | + |
| 188 | + if (permission === 'READWRITE') { |
| 189 | + this.secret.grantWrite(this.service.taskDefinition.taskRole); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + // Configure environment variables |
| 194 | + const secretEnvironmentVariableName = props.secretEnvironmentVariableName || 'SECRET_ARN'; |
| 195 | + this.container.addEnvironment(secretEnvironmentVariableName, this.secret.secretArn); |
| 196 | + } |
| 197 | +} |
0 commit comments