serverless-kms-alias 1.0.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.
@@ -0,0 +1,10 @@
1
+ #!/bin/sh
2
+ # shellcheck disable=SC1090,SC1091
3
+ . "$(dirname "$0")/_/husky.sh"
4
+
5
+ PATH=$PATH:/usr/local/bin
6
+
7
+ echo 'NOTE: If node or npm is not found, you may need to run brew link for your specific node version'
8
+ npm run lint-staged
9
+ npm run build
10
+ git add dist/
@@ -0,0 +1,76 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to make participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ - Using welcoming and inclusive language
18
+ - Being respectful of differing viewpoints and experiences
19
+ - Gracefully accepting constructive criticism
20
+ - Focusing on what is best for the community
21
+ - Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ - The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ - Trolling, insulting/derogatory comments, and personal or political attacks
28
+ - Public or private harassment
29
+ - Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ - Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies within all project spaces, and it also applies when
49
+ an individual is representing the project or its community in public spaces.
50
+ Examples of representing a project or community include using an official
51
+ project e-mail address, posting via an official social media account, or acting
52
+ as an appointed representative at an online or offline event. Representation of
53
+ a project may be further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at opensource@github.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at <https://www.contributor-covenant.org/version/1/4/code-of-conduct.html>
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ <https://www.contributor-covenant.org/faq>
@@ -0,0 +1,22 @@
1
+ # Contributors
2
+
3
+ ## Checkin
4
+
5
+ - Do checkin source (src)
6
+ - Do not checkin build output (dist)
7
+ - Do not checkin node_modules
8
+
9
+ ## Development
10
+
11
+ In order to handle code style and static analysis, we run [Husky](https://github.com/typicode/husky) before each commit.
12
+ This step ensures that formatting and checkin rules are followed. To make sure Husky runs correctly, please use the
13
+ following workflow:
14
+
15
+ ```sh
16
+ npm install # installs all devDependencies including Husky
17
+ git add abc.ext # Add the files you've changed. This should include files in src, lib, and node_modules (see above)
18
+ git commit -m "Informative commit message" # Commit. This will run Husky
19
+ ```
20
+
21
+ During the commit step, Husky will take care of formatting all files with [Prettier](https://github.com/prettier/prettier).
22
+ It will also make sure these changes are appropriately included in your commit (no further work is needed)
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 James Geurts
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # serverless-kms-alias
2
+
3
+ Serverless plugin to expand a KMS alias variable to the ARN of the KMS key
4
+
5
+ ## Usage
6
+
7
+ `${kms:<alias>}` will expand to the ARN of the kms key. The variable can be used anywhere that other Serverless
8
+ variables are resolved.
9
+
10
+ ### Example - Provider kmsKeyArn
11
+
12
+ ```yaml
13
+ service: foo
14
+
15
+ provider:
16
+ name: aws
17
+ kmsKeyArn: '${kms:alias/aws/lambda}'
18
+ runtime: nodejs14.x
19
+
20
+ plugins:
21
+ - serverless-kms-alias
22
+
23
+ functions:
24
+ foo:
25
+ handler: foo.handler
26
+ ```
27
+
28
+ ### Example - Function kmsKeyArn
29
+
30
+ ```yaml
31
+ service: foo
32
+ provider:
33
+ name: aws
34
+ runtime: nodejs14.x
35
+
36
+ plugins:
37
+ - serverless-kms-alias
38
+
39
+ functions:
40
+ foo:
41
+ handler: foo.handler
42
+ kmsKeyArn: '${kms:arn:aws:kms:${aws:region}:${aws:accountId}:alias/aws/lambda}'
43
+ ```
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ const client_kms_1 = require("@aws-sdk/client-kms");
3
+ class KmsAliasPlugin {
4
+ constructor(serverless) {
5
+ this.configurationVariablesSources = {
6
+ kms: {
7
+ async resolve({ address }) {
8
+ if (!/^(alias\/[a-zA-Z]|arn:aws:kms:[\w-]*:[\d]*:alias)/i.test(address)) {
9
+ throw new Error(`Expected variable in the form of 'kms:alias/foo'`);
10
+ }
11
+ serverless.cli.log(`Info: Fetching KMS key for alias: ${address}`);
12
+ const client = new client_kms_1.KMSClient({
13
+ ...serverless.providers.aws.getCredentials(),
14
+ region: serverless.providers.aws.getRegion(),
15
+ });
16
+ const command = new client_kms_1.DescribeKeyCommand({
17
+ KeyId: address,
18
+ });
19
+ const response = await client.send(command);
20
+ const keyMetadata = response.KeyMetadata;
21
+ if (!keyMetadata) {
22
+ throw new Error(`Unable to get key metadata for kms alias: ${address}`);
23
+ }
24
+ if (!keyMetadata.Arn) {
25
+ throw new Error(`Unable to determine ARN for kms alias: ${address}`);
26
+ }
27
+ serverless.cli.log(`Info: Found KMS key for alias (${address}): ${keyMetadata.Arn}`);
28
+ return {
29
+ value: keyMetadata.Arn,
30
+ };
31
+ },
32
+ },
33
+ };
34
+ }
35
+ }
36
+ module.exports = KmsAliasPlugin;
37
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLG9EQUFvRTtBQWtCcEUsTUFBTSxjQUFjO0lBR2xCLFlBQW1CLFVBQThCO1FBQy9DLElBQUksQ0FBQ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UFDLEdBQUcsQ0FBQyxHQUFHLENBQUMsa0NBQWtDLE9BQU8sTUFBTSxXQUFXLENBQUMsR0FBRyxFQUFFLENBQUMsQ0FBQztvQkFFckYsT0FBTzt3QkFDTCxLQUFLLEVBQUUsV0FBVyxDQUFDLEdBQUc7cUJBQ3ZCLENBQUM7Z0JBQ0osQ0FBQzthQUNGO1NBQ0YsQ0FBQztJQUNKLENBQUM7Q0FDRjtBQUVELGlCQUFTLGNBQWMsQ0FBQyJ9
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiU2VydmVybGVzc0luc3RhbmNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3R5cGVzL1NlcnZlcmxlc3NJbnN0YW5jZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./ServerlessInstance"), exports);
14
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdHlwZXMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7Ozs7Ozs7O0FBQUEsdURBQXFDIn0=
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "serverless-kms-alias",
3
+ "version": "1.0.0",
4
+ "engines": {
5
+ "node": ">= 12"
6
+ },
7
+ "dependencies": {
8
+ "@aws-sdk/client-kms": "^3.47.0"
9
+ },
10
+ "devDependencies": {
11
+ "@types/lodash": "4.14.178",
12
+ "@types/serverless": "1.78.41",
13
+ "@typescript-eslint/eslint-plugin": "5.10.0",
14
+ "@typescript-eslint/parser": "5.10.0",
15
+ "eslint": "8.7.0",
16
+ "eslint-config-airbnb-base": "15.0.0",
17
+ "eslint-config-airbnb-typescript": "16.1.0",
18
+ "eslint-config-prettier": "8.3.0",
19
+ "eslint-plugin-import": "2.25.4",
20
+ "eslint-plugin-jsdoc": "37.6.1",
21
+ "eslint-plugin-prettier": "4.0.0",
22
+ "eslint-plugin-promise": "6.0.0",
23
+ "eslint-plugin-security": "1.4.0",
24
+ "husky": "7.0.4",
25
+ "lint-staged": "12.2.0",
26
+ "markdownlint-cli": "0.30.0",
27
+ "npm-run-all": "4.1.5",
28
+ "pinst": "2.1.6",
29
+ "prettier": "2.5.1",
30
+ "prettier-plugin-packagejson": "2.2.15",
31
+ "typescript": "4.5.4"
32
+ },
33
+ "main": "dist/index.js",
34
+ "license": "MIT",
35
+ "scripts": {
36
+ "build": "tsc",
37
+ "lint:markdown": "prettier --write '*.md' '!(node_modules|dist)/**/*.md' && markdownlint '*.md' '!(node_modules|dist)/**/*.md' --config=.github/linters/.markdown-lint.yml --fix",
38
+ "lint:code": "prettier --write *.json .eslintrc.js .prettierrc.js && eslint --fix --ext .ts src",
39
+ "lint": "run-p lint:*",
40
+ "lint-staged": "lint-staged",
41
+ "prepublishOnly": "rm -rf dist && npm run build && npm run lint && pinst --disable",
42
+ "_postinstall": "husky install",
43
+ "postpublish": "pinst --enable"
44
+ },
45
+ "lint-staged": {
46
+ "*.md": [
47
+ "prettier --write",
48
+ "markdownlint --config=.github/linters/.markdown-lint.yml --fix"
49
+ ],
50
+ "./*.js": [
51
+ "eslint --fix"
52
+ ],
53
+ "*.ts": [
54
+ "eslint --fix"
55
+ ]
56
+ },
57
+ "repository": "https://github.com/mpirik/serverless-kms-alias.git"
58
+ }
package/src/index.ts ADDED
@@ -0,0 +1,62 @@
1
+ import { DescribeKeyCommand, KMSClient } from '@aws-sdk/client-kms';
2
+
3
+ import type { ServerlessInstance } from './types';
4
+
5
+ interface ResolveParams {
6
+ address: string;
7
+ params?: string[];
8
+ resolveVariable: (name: string) => Promise<string>;
9
+ }
10
+
11
+ interface ResolveResult {
12
+ value: string;
13
+ }
14
+
15
+ interface ServerlessVariableSource {
16
+ resolve(params: ResolveParams): Promise<ResolveResult>;
17
+ }
18
+
19
+ class KmsAliasPlugin {
20
+ public configurationVariablesSources: Record<string, ServerlessVariableSource>;
21
+
22
+ public constructor(serverless: ServerlessInstance) {
23
+ this.configurationVariablesSources = {
24
+ kms: {
25
+ async resolve({ address }: ResolveParams): Promise<ResolveResult> {
26
+ if (!/^(alias\/[a-zA-Z]|arn:aws:kms:[\w-]*:[\d]*:alias)/i.test(address)) {
27
+ throw new Error(`Expected variable in the form of 'kms:alias/foo'`);
28
+ }
29
+
30
+ serverless.cli.log(`Info: Fetching KMS key for alias: ${address}`);
31
+
32
+ const client = new KMSClient({
33
+ ...serverless.providers.aws.getCredentials(),
34
+ region: serverless.providers.aws.getRegion(),
35
+ });
36
+ const command = new DescribeKeyCommand({
37
+ KeyId: address,
38
+ });
39
+ const response = await client.send(command);
40
+
41
+ const keyMetadata = response.KeyMetadata;
42
+
43
+ if (!keyMetadata) {
44
+ throw new Error(`Unable to get key metadata for kms alias: ${address}`);
45
+ }
46
+
47
+ if (!keyMetadata.Arn) {
48
+ throw new Error(`Unable to determine ARN for kms alias: ${address}`);
49
+ }
50
+
51
+ serverless.cli.log(`Info: Found KMS key for alias (${address}): ${keyMetadata.Arn}`);
52
+
53
+ return {
54
+ value: keyMetadata.Arn,
55
+ };
56
+ },
57
+ },
58
+ };
59
+ }
60
+ }
61
+
62
+ export = KmsAliasPlugin;
@@ -0,0 +1,13 @@
1
+ import type { AwsAuthInputConfig } from '@aws-sdk/middleware-signing/dist-types/configurations';
2
+
3
+ export interface ServerlessInstance {
4
+ providers: {
5
+ aws: {
6
+ getCredentials(): AwsAuthInputConfig['credentials'];
7
+ getRegion(): string;
8
+ };
9
+ };
10
+ cli: {
11
+ log(str: string, entity?: string): void;
12
+ };
13
+ }
@@ -0,0 +1 @@
1
+ export * from './ServerlessInstance';
package/tsconfig.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2018",
4
+ "lib": ["esnext"],
5
+ "module": "commonjs",
6
+ "moduleResolution": "node",
7
+
8
+ "inlineSourceMap": true,
9
+
10
+ "baseUrl": ".",
11
+ "outDir": "dist",
12
+
13
+ "strict": true,
14
+ "forceConsistentCasingInFileNames": true,
15
+ "noUnusedLocals": true,
16
+ "noUnusedParameters": true,
17
+ "noImplicitReturns": true,
18
+ "noFallthroughCasesInSwitch": true
19
+ },
20
+ "include": ["src"]
21
+ }