@gradientedge/cdk-utils 8.98.0 → 8.99.1

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,3 @@
1
- import { SecretsManager as SM } from '@aws-sdk/client-secrets-manager';
2
1
  import * as cdk from 'aws-cdk-lib';
3
2
  import * as secretsManager from 'aws-cdk-lib/aws-secretsmanager';
4
3
  import { CommonConstruct } from '../../../common';
@@ -20,16 +19,12 @@ import { CommonConstruct } from '../../../common';
20
19
  */
21
20
  export declare class SecretsManager {
22
21
  /**
23
- *
24
- * @param region
25
- */
26
- getAwsSecretsManager(region: string): SM;
27
- /**
28
- * @summary Method to load a secret from secrets manager
29
- * @param secretName
30
- * @param region
22
+ * @summary Method to create a secret
23
+ * @param id scoped id of the resource
24
+ * @param scope scope in which this resource is defined
25
+ * @param props the secret properties
31
26
  */
32
- loadSecret(secretName: string, region: string): Promise<any>;
27
+ createSecret(id: string, scope: CommonConstruct, props: secretsManager.SecretProps): cdk.aws_secretsmanager.Secret;
33
28
  /**
34
29
  * @summary Method to retrieve a secret from secrets manager with a cloudformation export
35
30
  * @param id
@@ -39,10 +34,10 @@ export declare class SecretsManager {
39
34
  */
40
35
  retrieveSecretFromSecretsManager(id: string, scope: CommonConstruct, stackName: string, exportName: string): cdk.aws_secretsmanager.ISecret;
41
36
  /**
42
- * @summary Method to create a secret
43
- * @param id scoped id of the resource
37
+ * @summary Method to resolve secret value from a secret using AWS SDK
44
38
  * @param scope scope in which this resource is defined
45
- * @param props the secret properties
39
+ * @param secretId the secret name/ARN
40
+ * @param secretKey the secret key to resolve the value for
46
41
  */
47
- createSecret(id: string, scope: CommonConstruct, props: secretsManager.SecretProps): cdk.aws_secretsmanager.Secret;
42
+ resolveSecretValue(scope: CommonConstruct, secretId: string, secretKey: string): Promise<any>;
48
43
  }
@@ -46,21 +46,19 @@ const utils = __importStar(require("../../../utils"));
46
46
  */
47
47
  class SecretsManager {
48
48
  /**
49
- *
50
- * @param region
51
- */
52
- getAwsSecretsManager(region) {
53
- return new client_secrets_manager_1.SecretsManager({ region: region });
54
- }
55
- /**
56
- * @summary Method to load a secret from secrets manager
57
- * @param secretName
58
- * @param region
49
+ * @summary Method to create a secret
50
+ * @param id scoped id of the resource
51
+ * @param scope scope in which this resource is defined
52
+ * @param props the secret properties
59
53
  */
60
- async loadSecret(secretName, region) {
61
- const secretsManager = this.getAwsSecretsManager(region);
62
- const secret = await Promise.all([secretsManager.getSecretValue({ SecretId: secretName })]);
63
- return secret ? JSON.parse(secret[0].SecretString) : {};
54
+ createSecret(id, scope, props) {
55
+ const secret = new secretsManager.Secret(scope, `${id}`, {
56
+ ...props,
57
+ secretName: `${props.secretName}-${scope.props.stage}`,
58
+ });
59
+ utils.createCfnOutput(`${id}-secretName`, scope, secret.secretName);
60
+ utils.createCfnOutput(`${id}-secretArn`, scope, secret.secretArn);
61
+ return secret;
64
62
  }
65
63
  /**
66
64
  * @summary Method to retrieve a secret from secrets manager with a cloudformation export
@@ -73,19 +71,24 @@ class SecretsManager {
73
71
  return secretsManager.Secret.fromSecretNameV2(scope, `${id}`, cdk.Fn.importValue(`${stackName}-${scope.props.stage}-${exportName}`));
74
72
  }
75
73
  /**
76
- * @summary Method to create a secret
77
- * @param id scoped id of the resource
74
+ * @summary Method to resolve secret value from a secret using AWS SDK
78
75
  * @param scope scope in which this resource is defined
79
- * @param props the secret properties
76
+ * @param secretId the secret name/ARN
77
+ * @param secretKey the secret key to resolve the value for
80
78
  */
81
- createSecret(id, scope, props) {
82
- const secret = new secretsManager.Secret(scope, `${id}`, {
83
- ...props,
84
- secretName: `${props.secretName}-${scope.props.stage}`,
79
+ async resolveSecretValue(scope, secretId, secretKey) {
80
+ const client = new client_secrets_manager_1.SecretsManagerClient({
81
+ credentials: utils.determineCredentials(),
82
+ region: scope.props.region,
85
83
  });
86
- utils.createCfnOutput(`${id}-secretName`, scope, secret.secretName);
87
- utils.createCfnOutput(`${id}-secretArn`, scope, secret.secretArn);
88
- return secret;
84
+ const command = new client_secrets_manager_1.GetSecretValueCommand({
85
+ SecretId: secretId,
86
+ });
87
+ const response = await client.send(command);
88
+ if (!response.SecretString)
89
+ throw `Unable to resolve secret for ${secretId}`;
90
+ const secretString = JSON.parse(response.SecretString);
91
+ return secretString[secretKey];
89
92
  }
90
93
  }
91
94
  exports.SecretsManager = SecretsManager;
@@ -1,5 +1,6 @@
1
1
  import * as cdk from 'aws-cdk-lib';
2
2
  import { CommonConstruct } from '../../common';
3
+ import { AwsCredentialIdentityProvider } from '@aws-sdk/types';
3
4
  /**
4
5
  * @summary Helper method to add CloudFormation outputs from the construct
5
6
  * @param id scoped id of the resource
@@ -10,3 +11,4 @@ import { CommonConstruct } from '../../common';
10
11
  * @returns The CloudFormation output
11
12
  */
12
13
  export declare function createCfnOutput(id: string, scope: CommonConstruct, value?: string, description?: string, overrideId?: boolean): cdk.CfnOutput;
14
+ export declare function determineCredentials(): AwsCredentialIdentityProvider;
@@ -23,9 +23,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.createCfnOutput = void 0;
26
+ exports.determineCredentials = exports.createCfnOutput = void 0;
27
27
  const cdk = __importStar(require("aws-cdk-lib"));
28
28
  const _ = __importStar(require("lodash"));
29
+ const credential_providers_1 = require("@aws-sdk/credential-providers");
29
30
  /**
30
31
  * @summary Helper method to add CloudFormation outputs from the construct
31
32
  * @param id scoped id of the resource
@@ -48,3 +49,9 @@ function createCfnOutput(id, scope, value, description, overrideId = true) {
48
49
  return output;
49
50
  }
50
51
  exports.createCfnOutput = createCfnOutput;
52
+ function determineCredentials() {
53
+ if (process.env.AWS_PROFILE)
54
+ return (0, credential_providers_1.fromIni)();
55
+ return (0, credential_providers_1.fromEnv)();
56
+ }
57
+ exports.determineCredentials = determineCredentials;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradientedge/cdk-utils",
3
- "version": "8.98.0",
3
+ "version": "8.99.1",
4
4
  "description": "Utilities for AWS CDK provisioning",
5
5
  "main": "dist/index.js",
6
6
  "engines": {
@@ -30,10 +30,10 @@
30
30
  "build:production": "rimraf dist/ && npx tsc -p tsconfig.prd.json && pnpm -r build",
31
31
  "ci": "pnpm install --frozen-lockfile && pnpm build && pnpm validate && pnpm run docs",
32
32
  "cz": "npx cz",
33
- "override:plugin:docs": "cp theme/type-converter.js node_modules/better-docs/typescript",
34
33
  "docs": "npx rimraf api-docs && pnpm override:plugin:docs && npx jsdoc --pedantic -c jsdoc.json .",
35
- "lint": "pnpm prettify && eslint **/*.ts --cache --max-warnings=0",
36
34
  "fix": "pnpm prettify && eslint --fix **/*.ts",
35
+ "lint": "pnpm prettify && eslint **/*.ts --cache --max-warnings=0",
36
+ "override:plugin:docs": "cp theme/type-converter.js node_modules/better-docs/typescript",
37
37
  "prettier": "npx prettier --cache --check \"**/*.{ts,json,md}\"",
38
38
  "prettify": "npx prettier --cache --write \"**/*.{ts,json,md}\"",
39
39
  "test": "npx rimraf coverage && npx jest --ci --maxWorkers=100%",
@@ -46,13 +46,15 @@
46
46
  }
47
47
  },
48
48
  "dependencies": {
49
- "@aws-sdk/client-secrets-manager": "^3.354.0",
49
+ "@aws-sdk/client-secrets-manager": "^3.357.0",
50
+ "@aws-sdk/credential-providers": "^3.357.0",
51
+ "@aws-sdk/types": "^3.357.0",
50
52
  "@types/lodash": "^4.14.195",
51
53
  "@types/node": "^20.3.1",
52
54
  "@types/uuid": "^9.0.2",
53
55
  "app-root-path": "^3.1.0",
54
- "aws-cdk-lib": "^2.84.0",
55
- "constructs": "^10.2.54",
56
+ "aws-cdk-lib": "^2.85.0",
57
+ "constructs": "^10.2.56",
56
58
  "lodash": "^4.17.21",
57
59
  "moment": "^2.29.4",
58
60
  "nconf": "^0.12.0",
@@ -65,9 +67,9 @@
65
67
  "@babel/eslint-parser": "^7.22.5",
66
68
  "@babel/plugin-proposal-class-properties": "^7.18.6",
67
69
  "@types/jest": "^29.5.2",
68
- "@typescript-eslint/eslint-plugin": "^5.59.11",
69
- "@typescript-eslint/parser": "^5.59.11",
70
- "aws-cdk": "^2.84.0",
70
+ "@typescript-eslint/eslint-plugin": "^5.60.0",
71
+ "@typescript-eslint/parser": "^5.60.0",
72
+ "aws-cdk": "^2.85.0",
71
73
  "better-docs": "^2.7.2",
72
74
  "codecov": "^3.8.3",
73
75
  "commitizen": "^4.3.0",
@@ -84,8 +86,8 @@
84
86
  "jsdoc": "^4.0.2",
85
87
  "jsdoc-babel": "^0.5.0",
86
88
  "jsdoc-mermaid": "^1.0.0",
87
- "jsdoc-to-markdown": "^8.0.0",
88
89
  "jsdoc-plugin-typescript": "^2.2.1",
90
+ "jsdoc-to-markdown": "^8.0.0",
89
91
  "prettier": "^2.8.8",
90
92
  "prettier-plugin-organize-imports": "^3.2.2",
91
93
  "rimraf": "^5.0.1",
@@ -1,4 +1,4 @@
1
- import { SecretsManager as SM } from '@aws-sdk/client-secrets-manager'
1
+ import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager'
2
2
  import * as cdk from 'aws-cdk-lib'
3
3
  import * as secretsManager from 'aws-cdk-lib/aws-secretsmanager'
4
4
  import * as utils from '../../../utils'
@@ -22,22 +22,21 @@ import { CommonConstruct } from '../../../common'
22
22
  */
23
23
  export class SecretsManager {
24
24
  /**
25
- *
26
- * @param region
25
+ * @summary Method to create a secret
26
+ * @param id scoped id of the resource
27
+ * @param scope scope in which this resource is defined
28
+ * @param props the secret properties
27
29
  */
28
- public getAwsSecretsManager(region: string) {
29
- return new SM({ region: region })
30
- }
30
+ public createSecret(id: string, scope: CommonConstruct, props: secretsManager.SecretProps) {
31
+ const secret = new secretsManager.Secret(scope, `${id}`, {
32
+ ...props,
33
+ secretName: `${props.secretName}-${scope.props.stage}`,
34
+ })
31
35
 
32
- /**
33
- * @summary Method to load a secret from secrets manager
34
- * @param secretName
35
- * @param region
36
- */
37
- public async loadSecret(secretName: string, region: string) {
38
- const secretsManager = this.getAwsSecretsManager(region)
39
- const secret: any = await Promise.all([secretsManager.getSecretValue({ SecretId: secretName })])
40
- return secret ? JSON.parse(secret[0].SecretString) : {}
36
+ utils.createCfnOutput(`${id}-secretName`, scope, secret.secretName)
37
+ utils.createCfnOutput(`${id}-secretArn`, scope, secret.secretArn)
38
+
39
+ return secret
41
40
  }
42
41
 
43
42
  /**
@@ -56,20 +55,23 @@ export class SecretsManager {
56
55
  }
57
56
 
58
57
  /**
59
- * @summary Method to create a secret
60
- * @param id scoped id of the resource
58
+ * @summary Method to resolve secret value from a secret using AWS SDK
61
59
  * @param scope scope in which this resource is defined
62
- * @param props the secret properties
60
+ * @param secretId the secret name/ARN
61
+ * @param secretKey the secret key to resolve the value for
63
62
  */
64
- public createSecret(id: string, scope: CommonConstruct, props: secretsManager.SecretProps) {
65
- const secret = new secretsManager.Secret(scope, `${id}`, {
66
- ...props,
67
- secretName: `${props.secretName}-${scope.props.stage}`,
63
+ public async resolveSecretValue(scope: CommonConstruct, secretId: string, secretKey: string) {
64
+ const client = new SecretsManagerClient({
65
+ credentials: utils.determineCredentials(),
66
+ region: scope.props.region,
68
67
  })
68
+ const command = new GetSecretValueCommand({
69
+ SecretId: secretId,
70
+ })
71
+ const response = await client.send(command)
72
+ if (!response.SecretString) throw `Unable to resolve secret for ${secretId}`
73
+ const secretString = JSON.parse(response.SecretString)
69
74
 
70
- utils.createCfnOutput(`${id}-secretName`, scope, secret.secretName)
71
- utils.createCfnOutput(`${id}-secretArn`, scope, secret.secretArn)
72
-
73
- return secret
75
+ return secretString[secretKey]
74
76
  }
75
77
  }
@@ -1,6 +1,8 @@
1
1
  import * as cdk from 'aws-cdk-lib'
2
2
  import * as _ from 'lodash'
3
3
  import { CommonConstruct } from '../../common'
4
+ import { fromEnv, fromIni } from '@aws-sdk/credential-providers'
5
+ import { AwsCredentialIdentityProvider } from '@aws-sdk/types'
4
6
 
5
7
  /**
6
8
  * @summary Helper method to add CloudFormation outputs from the construct
@@ -29,3 +31,8 @@ export function createCfnOutput(
29
31
  }
30
32
  return output
31
33
  }
34
+
35
+ export function determineCredentials(): AwsCredentialIdentityProvider {
36
+ if (process.env.AWS_PROFILE) return fromIni()
37
+ return fromEnv()
38
+ }