@gradientedge/cdk-utils 8.146.1 → 8.147.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
- import { TerraformStack } from 'cdktf';
1
+ import { AwsProvider } from '@cdktf/provider-aws/lib/provider';
2
+ import { S3Backend, TerraformStack } from 'cdktf';
2
3
  import { Construct } from 'constructs';
3
4
  import { CloudflareAccessManager, CloudflareApiShieldManager, CloudflareArgoManager, CloudflareFilterManager, CloudflareFirewallManager, CloudflarePageManager, CloudflareRecordManager, CloudflareWorkerManager, CloudflareZoneManager } from '../services';
4
5
  import { CommonCloudflareStackProps } from './types';
@@ -15,6 +16,8 @@ export declare class CommonCloudflareConstruct extends TerraformStack {
15
16
  recordManager: CloudflareRecordManager;
16
17
  workerManager: CloudflareWorkerManager;
17
18
  zoneManager: CloudflareZoneManager;
19
+ awsProvider: AwsProvider;
20
+ s3Backend: S3Backend;
18
21
  constructor(scope: Construct, id: string, props: CommonCloudflareStackProps);
19
22
  /**
20
23
  * @summary Determine the fully qualified domain name based on domainName & subDomain
@@ -19,6 +19,8 @@ class CommonCloudflareConstruct extends cdktf_1.TerraformStack {
19
19
  recordManager;
20
20
  workerManager;
21
21
  zoneManager;
22
+ awsProvider;
23
+ s3Backend;
22
24
  constructor(scope, id, props) {
23
25
  super(scope, id);
24
26
  this.props = props;
@@ -62,11 +64,11 @@ class CommonCloudflareConstruct extends cdktf_1.TerraformStack {
62
64
  const debug = this.node.tryGetContext('debug');
63
65
  switch (this.props.remoteBackend?.type) {
64
66
  case constants_1.RemoteBackend.s3:
65
- new provider_1.AwsProvider(this, `${this.id}-aws-provider`, {
67
+ this.awsProvider = new provider_1.AwsProvider(this, `${this.id}-aws-provider`, {
66
68
  profile: process.env.AWS_PROFILE ?? 'default',
67
69
  region: this.props.remoteBackend.region,
68
70
  });
69
- new cdktf_1.S3Backend(this, {
71
+ this.s3Backend = new cdktf_1.S3Backend(this, {
70
72
  bucket: this.props.remoteBackend.bucketName,
71
73
  dynamodbTable: this.props.remoteBackend.tableName,
72
74
  key: `${this.id}`,
@@ -27,6 +27,18 @@ export declare class CloudflarePagesStaticSite extends CommonCloudflareConstruct
27
27
  sitePagesDomain: PagesDomain;
28
28
  sitePagesProject: PagesProject;
29
29
  siteZone: DataCloudflareZone | Zone;
30
+ sitePagesEnvironmentVariables: {
31
+ [key: string]: string;
32
+ };
33
+ sitePagesPreviewEnvironmentVariables: {
34
+ [key: string]: string;
35
+ };
36
+ sitePagesSecrets: {
37
+ [key: string]: string;
38
+ };
39
+ sitePagesPreviewSecrets: {
40
+ [key: string]: string;
41
+ };
30
42
  constructor(parent: Construct, id: string, props: CloudflarePagesStaticSiteProps);
31
43
  /**
32
44
  * @summary Initialise and provision resources
@@ -36,6 +48,21 @@ export declare class CloudflarePagesStaticSite extends CommonCloudflareConstruct
36
48
  * @summary Resolve the zone to use for the static site
37
49
  */
38
50
  protected resolveZone(): void;
51
+ /**
52
+ * @summary Resolve the secrets to use for the static site
53
+ */
54
+ protected resolveSecrets(): void;
55
+ /**
56
+ * @summary Resolve the environment variables to use for the static site
57
+ */
58
+ protected resolveEnvironmentVariables(): void;
59
+ /**
60
+ * @summary Resolve secrets from AWS Secrets Manager
61
+ * @param secretName the secret name
62
+ * @param secretKey the secret key
63
+ * @returns the secret value
64
+ */
65
+ protected resolveSecretFromAWS(secretName: string, secretKey: string): any;
39
66
  /**
40
67
  * @summary Create the pages project
41
68
  */
@@ -1,6 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CloudflarePagesStaticSite = void 0;
4
+ const data_aws_secretsmanager_secret_1 = require("@cdktf/provider-aws/lib/data-aws-secretsmanager-secret");
5
+ const data_aws_secretsmanager_secret_version_1 = require("@cdktf/provider-aws/lib/data-aws-secretsmanager-secret-version");
6
+ const cdktf_1 = require("cdktf");
4
7
  const common_1 = require("../../common");
5
8
  /**
6
9
  * @classdesc Provides a construct to create and deploy a cloudflare pages static site
@@ -23,6 +26,10 @@ class CloudflarePagesStaticSite extends common_1.CommonCloudflareConstruct {
23
26
  sitePagesDomain;
24
27
  sitePagesProject;
25
28
  siteZone;
29
+ sitePagesEnvironmentVariables;
30
+ sitePagesPreviewEnvironmentVariables;
31
+ sitePagesSecrets;
32
+ sitePagesPreviewSecrets;
26
33
  constructor(parent, id, props) {
27
34
  super(parent, id, props);
28
35
  this.props = props;
@@ -33,6 +40,8 @@ class CloudflarePagesStaticSite extends common_1.CommonCloudflareConstruct {
33
40
  */
34
41
  initResources() {
35
42
  this.resolveZone();
43
+ this.resolveSecrets();
44
+ this.resolveEnvironmentVariables();
36
45
  this.createProject();
37
46
  this.createDomain();
38
47
  this.createRecord();
@@ -49,6 +58,56 @@ class CloudflarePagesStaticSite extends common_1.CommonCloudflareConstruct {
49
58
  this.siteZone = this.zoneManager.createZone(`${this.id}-zone`, this, this.props.siteZone);
50
59
  }
51
60
  }
61
+ /**
62
+ * @summary Resolve the secrets to use for the static site
63
+ */
64
+ resolveSecrets() {
65
+ this.props.sitePagesProject = {
66
+ ...this.props.sitePagesProject,
67
+ deploymentConfigs: {
68
+ preview: {
69
+ secrets: this.sitePagesPreviewSecrets,
70
+ },
71
+ production: {
72
+ secrets: this.sitePagesSecrets,
73
+ },
74
+ },
75
+ };
76
+ }
77
+ /**
78
+ * @summary Resolve the environment variables to use for the static site
79
+ */
80
+ resolveEnvironmentVariables() {
81
+ this.props.sitePagesProject = {
82
+ ...this.props.sitePagesProject,
83
+ deploymentConfigs: {
84
+ preview: {
85
+ environmentVariables: this.sitePagesPreviewEnvironmentVariables,
86
+ },
87
+ production: {
88
+ environmentVariables: this.sitePagesEnvironmentVariables,
89
+ },
90
+ },
91
+ };
92
+ }
93
+ /**
94
+ * @summary Resolve secrets from AWS Secrets Manager
95
+ * @param secretName the secret name
96
+ * @param secretKey the secret key
97
+ * @returns the secret value
98
+ */
99
+ resolveSecretFromAWS(secretName, secretKey) {
100
+ if (!this.awsProvider)
101
+ return;
102
+ const secret = new data_aws_secretsmanager_secret_1.DataAwsSecretsmanagerSecret(this, `${this.id}-${secretName}-${secretKey}`, { name: secretName });
103
+ const secretVersion = new data_aws_secretsmanager_secret_version_1.DataAwsSecretsmanagerSecretVersion(this, `${this.id}-${secretName}-${secretKey}-ver`, {
104
+ provider: this.awsProvider,
105
+ secretId: secret.id,
106
+ });
107
+ if (!secretVersion)
108
+ throw new Error(`Unable to resolve secret:${secretName}`);
109
+ return cdktf_1.Fn.lookup(cdktf_1.Fn.jsondecode(secretVersion.secretString), secretKey);
110
+ }
52
111
  /**
53
112
  * @summary Create the pages project
54
113
  */
@@ -86,8 +86,7 @@ class CloudflarePageManager {
86
86
  deployPagesProject(id, scope, props) {
87
87
  if (!props)
88
88
  throw `Props undefined for ${id}`;
89
- const localExecProvider = new cdktf_local_exec_1.Provider(scope, `${id}`);
90
- const branch = scope.isProductionStage() ? 'main' : props.branch;
89
+ new cdktf_local_exec_1.Provider(scope, `${id}`);
91
90
  const message = process.env.BUILD_NUMBER ?? props.message;
92
91
  const deployment = new cdktf_local_exec_1.LocalExec(scope, `${id}-deploy-${new Date().toISOString()}`, {
93
92
  command: `CLOUDFLARE_ACCOUNT_ID=${scope.props.accountId} CLOUDFLARE_API_TOKEN=${scope.props.apiToken} npx wrangler pages deploy ${props.directory} --project-name=${props.projectName} --branch=${props.branch} --commit-message=${message}`,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "8.146.1",
3
+ "version": "8.147.0",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -30,6 +30,8 @@ export class CommonCloudflareConstruct extends TerraformStack {
30
30
  recordManager: CloudflareRecordManager
31
31
  workerManager: CloudflareWorkerManager
32
32
  zoneManager: CloudflareZoneManager
33
+ awsProvider: AwsProvider
34
+ s3Backend: S3Backend
33
35
 
34
36
  constructor(scope: Construct, id: string, props: CommonCloudflareStackProps) {
35
37
  super(scope, id)
@@ -80,11 +82,11 @@ export class CommonCloudflareConstruct extends TerraformStack {
80
82
  const debug = this.node.tryGetContext('debug')
81
83
  switch (this.props.remoteBackend?.type) {
82
84
  case RemoteBackend.s3:
83
- new AwsProvider(this, `${this.id}-aws-provider`, {
85
+ this.awsProvider = new AwsProvider(this, `${this.id}-aws-provider`, {
84
86
  profile: process.env.AWS_PROFILE ?? 'default',
85
87
  region: this.props.remoteBackend.region,
86
88
  })
87
- new S3Backend(this, {
89
+ this.s3Backend = new S3Backend(this, {
88
90
  bucket: this.props.remoteBackend.bucketName,
89
91
  dynamodbTable: this.props.remoteBackend.tableName,
90
92
  key: `${this.id}`,
@@ -1,8 +1,11 @@
1
+ import { DataAwsSecretsmanagerSecret } from '@cdktf/provider-aws/lib/data-aws-secretsmanager-secret'
2
+ import { DataAwsSecretsmanagerSecretVersion } from '@cdktf/provider-aws/lib/data-aws-secretsmanager-secret-version'
1
3
  import { DataCloudflareZone } from '@cdktf/provider-cloudflare/lib/data-cloudflare-zone'
2
4
  import { PagesDomain } from '@cdktf/provider-cloudflare/lib/pages-domain'
3
5
  import { PagesProject } from '@cdktf/provider-cloudflare/lib/pages-project'
4
6
  import { Record } from '@cdktf/provider-cloudflare/lib/record'
5
7
  import { Zone } from '@cdktf/provider-cloudflare/lib/zone'
8
+ import { Fn } from 'cdktf'
6
9
  import { Construct } from 'constructs'
7
10
  import { CommonCloudflareConstruct } from '../../common'
8
11
  import { CloudflarePagesStaticSiteProps } from './types'
@@ -30,6 +33,10 @@ export class CloudflarePagesStaticSite extends CommonCloudflareConstruct {
30
33
  sitePagesDomain: PagesDomain
31
34
  sitePagesProject: PagesProject
32
35
  siteZone: DataCloudflareZone | Zone
36
+ sitePagesEnvironmentVariables: { [key: string]: string }
37
+ sitePagesPreviewEnvironmentVariables: { [key: string]: string }
38
+ sitePagesSecrets: { [key: string]: string }
39
+ sitePagesPreviewSecrets: { [key: string]: string }
33
40
 
34
41
  constructor(parent: Construct, id: string, props: CloudflarePagesStaticSiteProps) {
35
42
  super(parent, id, props)
@@ -42,6 +49,8 @@ export class CloudflarePagesStaticSite extends CommonCloudflareConstruct {
42
49
  */
43
50
  protected initResources() {
44
51
  this.resolveZone()
52
+ this.resolveSecrets()
53
+ this.resolveEnvironmentVariables()
45
54
  this.createProject()
46
55
  this.createDomain()
47
56
  this.createRecord()
@@ -59,6 +68,57 @@ export class CloudflarePagesStaticSite extends CommonCloudflareConstruct {
59
68
  }
60
69
  }
61
70
 
71
+ /**
72
+ * @summary Resolve the secrets to use for the static site
73
+ */
74
+ protected resolveSecrets() {
75
+ this.props.sitePagesProject = {
76
+ ...this.props.sitePagesProject,
77
+ deploymentConfigs: {
78
+ preview: {
79
+ secrets: this.sitePagesPreviewSecrets,
80
+ },
81
+ production: {
82
+ secrets: this.sitePagesSecrets,
83
+ },
84
+ },
85
+ }
86
+ }
87
+
88
+ /**
89
+ * @summary Resolve the environment variables to use for the static site
90
+ */
91
+ protected resolveEnvironmentVariables() {
92
+ this.props.sitePagesProject = {
93
+ ...this.props.sitePagesProject,
94
+ deploymentConfigs: {
95
+ preview: {
96
+ environmentVariables: this.sitePagesPreviewEnvironmentVariables,
97
+ },
98
+ production: {
99
+ environmentVariables: this.sitePagesEnvironmentVariables,
100
+ },
101
+ },
102
+ }
103
+ }
104
+
105
+ /**
106
+ * @summary Resolve secrets from AWS Secrets Manager
107
+ * @param secretName the secret name
108
+ * @param secretKey the secret key
109
+ * @returns the secret value
110
+ */
111
+ protected resolveSecretFromAWS(secretName: string, secretKey: string) {
112
+ if (!this.awsProvider) return
113
+ const secret = new DataAwsSecretsmanagerSecret(this, `${this.id}-${secretName}-${secretKey}`, { name: secretName })
114
+ const secretVersion = new DataAwsSecretsmanagerSecretVersion(this, `${this.id}-${secretName}-${secretKey}-ver`, {
115
+ provider: this.awsProvider,
116
+ secretId: secret.id,
117
+ })
118
+ if (!secretVersion) throw new Error(`Unable to resolve secret:${secretName}`)
119
+ return Fn.lookup(Fn.jsondecode(secretVersion.secretString), secretKey)
120
+ }
121
+
62
122
  /**
63
123
  * @summary Create the pages project
64
124
  */
@@ -96,8 +96,7 @@ export class CloudflarePageManager {
96
96
  public deployPagesProject(id: string, scope: CommonCloudflareConstruct, props: PagesProjectDeployProps) {
97
97
  if (!props) throw `Props undefined for ${id}`
98
98
 
99
- const localExecProvider = new Provider(scope, `${id}`)
100
- const branch = scope.isProductionStage() ? 'main' : props.branch
99
+ new Provider(scope, `${id}`)
101
100
  const message = process.env.BUILD_NUMBER ?? props.message
102
101
  const deployment = new LocalExec(scope, `${id}-deploy-${new Date().toISOString()}`, {
103
102
  command: `CLOUDFLARE_ACCOUNT_ID=${scope.props.accountId} CLOUDFLARE_API_TOKEN=${scope.props.apiToken} npx wrangler pages deploy ${props.directory} --project-name=${props.projectName} --branch=${props.branch} --commit-message=${message}`,