-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56ab1f6
commit 4fa4454
Showing
7 changed files
with
248 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
{ | ||
"name": "@cribadvisor/aws-param-cache", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "Cache un/encrypted expiring values as AWS SSM parameters with a TTL", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"main": "dist/index.js", | ||
"types": "dist", | ||
"files": [ | ||
"dist" | ||
], | ||
"repository": "https://github.com/CribAdvisor/aws-param-cache.git", | ||
"author": "Lawrence Thorpe <[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "tsc" | ||
}, | ||
"peerDependencies": { | ||
"aws-sdk": "^2.648.0" | ||
}, | ||
|
@@ -15,5 +21,10 @@ | |
}, | ||
"dependencies": { | ||
"ow": "^0.17.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^14.14.20", | ||
"aws-sdk": "^2.648.0", | ||
"typescript": "^4.1.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { SSM } from "aws-sdk"; | ||
import ow from "ow"; | ||
|
||
import type { PromiseResult } from "aws-sdk/lib/request"; | ||
import type { AWSError } from "aws-sdk/lib/error"; | ||
|
||
interface SSMCacheOptions { | ||
/** | ||
* Sets the parameter type: `true` to use `SecureString` `false` to use `String` | ||
*/ | ||
secret?: boolean; | ||
/** | ||
* Where the parameters are stored within SSM *(excluding trailing slash)* | ||
* Be sure to update the IAM policy (see `README.md`) if changed | ||
*/ | ||
basePath?: string; | ||
/** | ||
* ARN of KMS key ID to use to encrypt parameter value | ||
*/ | ||
keyId?: string; | ||
} | ||
|
||
class SSMCache { | ||
options: SSMCacheOptions; | ||
_ssm: SSM; | ||
|
||
constructor(options: SSMCacheOptions = {}) { | ||
const defaults: SSMCacheOptions = { | ||
secret: true, | ||
basePath: "/cache", | ||
keyId: undefined, | ||
}; | ||
this.options = Object.assign({}, defaults, options); | ||
this._ssm = new SSM(); | ||
} | ||
|
||
_escapeName(name: string) { | ||
return name.replace(/[^a-zA-Z0-9_.-\/]/g, "_"); | ||
} | ||
|
||
/** | ||
* Get a parameter from SSM with the given key (excluding `basePath`) | ||
* @param key Name of the SSM parameter (within `basePath`) | ||
*/ | ||
async get(key: string): Promise<string | null> { | ||
ow(key, ow.string.not.empty); | ||
try { | ||
const name = this._escapeName(`${this.options.basePath}/${key}`); | ||
const result = await this._ssm | ||
.getParameter({ | ||
Name: name, | ||
WithDecryption: this.options.secret, | ||
}) | ||
.promise(); | ||
const param = result.Parameter; | ||
if (!param?.LastModifiedDate || !param?.Value) { | ||
return null; | ||
} | ||
const now = new Date().getTime() / 1000; | ||
const timestamp = new Date(param.LastModifiedDate).getTime() / 1000; | ||
const { TTL: ttl, Value: value } = JSON.parse(param.Value); | ||
if (now > timestamp + ttl) { | ||
await this._ssm | ||
.deleteParameter({ | ||
Name: name, | ||
}) | ||
.promise(); | ||
return null; | ||
} | ||
return value; | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* `PUT` a parameter to SSM for the given key (excluding `basePath`) | ||
* @param key Name of the SSM parameter to store (within `basePath`) | ||
* @param value Value to store in SSM for the key | ||
* @param ttl How long in seconds after setting the parameter it should expire | ||
*/ | ||
async set( | ||
key: string, | ||
value: string, | ||
ttl: number | ||
): Promise<PromiseResult<SSM.PutParameterResult, AWSError>> { | ||
ow(key, ow.string.not.empty); | ||
ow(value, ow.string); | ||
ow( | ||
ttl, | ||
ow.number.is((x) => x > 0 || `Expected \`${x}\` to be greater than 0`) | ||
); | ||
const param = JSON.stringify({ | ||
TTL: ttl, | ||
Value: value, | ||
}); | ||
return await this._ssm | ||
.putParameter({ | ||
Name: this._escapeName(`${this.options.basePath}/${key}`), | ||
Value: param, | ||
Type: this.options.secret ? "SecureString" : "String", | ||
Overwrite: true, | ||
KeyId: this.options.keyId, | ||
}) | ||
.promise(); | ||
} | ||
} | ||
|
||
export = SSMCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"declaration": true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,124 @@ | |
# yarn lockfile v1 | ||
|
||
|
||
"@types/node@^14.14.20": | ||
version "14.14.20" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.20.tgz#f7974863edd21d1f8a494a73e8e2b3658615c340" | ||
integrity sha512-Y93R97Ouif9JEOWPIUyU+eyIdyRqQR0I8Ez1dzku4hDx34NWh4HbtIc3WNzwB1Y9ULvNGeu5B8h8bVL5cAk4/A== | ||
|
||
aws-sdk@^2.648.0: | ||
version "2.824.0" | ||
resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.824.0.tgz#a67747d4d0b53d09c6c121e93f44d8f6e76fc44b" | ||
integrity sha512-9KNRQBkIMPn+6DWb4gR+RzqTMNyGLEwOgXbE4dDehOIAflfLnv3IFwLnzrhxJnleB4guYrILIsBroJFBzjiekg== | ||
dependencies: | ||
buffer "4.9.2" | ||
events "1.1.1" | ||
ieee754 "1.1.13" | ||
jmespath "0.15.0" | ||
querystring "0.2.0" | ||
sax "1.2.1" | ||
url "0.10.3" | ||
uuid "3.3.2" | ||
xml2js "0.4.19" | ||
|
||
base64-js@^1.0.2: | ||
version "1.5.1" | ||
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a" | ||
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA== | ||
|
||
[email protected]: | ||
version "4.9.2" | ||
resolved "https://registry.yarnpkg.com/buffer/-/buffer-4.9.2.tgz#230ead344002988644841ab0244af8c44bbe3ef8" | ||
integrity sha512-xq+q3SRMOxGivLhBNaUdC64hDTQwejJ+H0T/NB1XMtTVEwNTrfFF3gAxiyW0Bu/xWEGhjVKgUcMhCrUy2+uCWg== | ||
dependencies: | ||
base64-js "^1.0.2" | ||
ieee754 "^1.1.4" | ||
isarray "^1.0.0" | ||
|
||
[email protected]: | ||
version "1.1.1" | ||
resolved "https://registry.yarnpkg.com/events/-/events-1.1.1.tgz#9ebdb7635ad099c70dcc4c2a1f5004288e8bd924" | ||
integrity sha1-nr23Y1rQmccNzEwqH1AEKI6L2SQ= | ||
|
||
[email protected]: | ||
version "1.1.13" | ||
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.13.tgz#ec168558e95aa181fd87d37f55c32bbcb6708b84" | ||
integrity sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg== | ||
|
||
ieee754@^1.1.4: | ||
version "1.2.1" | ||
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352" | ||
integrity sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA== | ||
|
||
isarray@^1.0.0: | ||
version "1.0.0" | ||
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" | ||
integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE= | ||
|
||
[email protected]: | ||
version "0.15.0" | ||
resolved "https://registry.yarnpkg.com/jmespath/-/jmespath-0.15.0.tgz#a3f222a9aae9f966f5d27c796510e28091764217" | ||
integrity sha1-o/Iiqarp+Wb10nx5ZRDigJF2Qhc= | ||
|
||
ow@^0.17.0: | ||
version "0.17.0" | ||
resolved "https://registry.yarnpkg.com/ow/-/ow-0.17.0.tgz#4f938999fed6264c9048cd6254356e0f1e7f688c" | ||
integrity sha512-i3keDzDQP5lWIe4oODyDFey1qVrq2hXKTuTH2VpqwpYtzPiKZt2ziRI4NBQmgW40AnV5Euz17OyWweCb+bNEQA== | ||
dependencies: | ||
type-fest "^0.11.0" | ||
|
||
[email protected]: | ||
version "1.3.2" | ||
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d" | ||
integrity sha1-llOgNvt8HuQjQvIyXM7v6jkmxI0= | ||
|
||
[email protected]: | ||
version "0.2.0" | ||
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620" | ||
integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA= | ||
|
||
[email protected]: | ||
version "1.2.1" | ||
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.1.tgz#7b8e656190b228e81a66aea748480d828cd2d37a" | ||
integrity sha1-e45lYZCyKOgaZq6nSEgNgozS03o= | ||
|
||
sax@>=0.6.0: | ||
version "1.2.4" | ||
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" | ||
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== | ||
|
||
type-fest@^0.11.0: | ||
version "0.11.0" | ||
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.11.0.tgz#97abf0872310fed88a5c466b25681576145e33f1" | ||
integrity sha512-OdjXJxnCN1AvyLSzeKIgXTXxV+99ZuXl3Hpo9XpJAv9MBcHrrJOQ5kV7ypXOuQie+AmWG25hLbiKdwYTifzcfQ== | ||
|
||
typescript@^4.1.3: | ||
version "4.1.3" | ||
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.3.tgz#519d582bd94cba0cf8934c7d8e8467e473f53bb7" | ||
integrity sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg== | ||
|
||
[email protected]: | ||
version "0.10.3" | ||
resolved "https://registry.yarnpkg.com/url/-/url-0.10.3.tgz#021e4d9c7705f21bbf37d03ceb58767402774c64" | ||
integrity sha1-Ah5NnHcF8hu/N9A861h2dAJ3TGQ= | ||
dependencies: | ||
punycode "1.3.2" | ||
querystring "0.2.0" | ||
|
||
[email protected]: | ||
version "3.3.2" | ||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.3.2.tgz#1b4af4955eb3077c501c23872fc6513811587131" | ||
integrity sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA== | ||
|
||
[email protected]: | ||
version "0.4.19" | ||
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.19.tgz#686c20f213209e94abf0d1bcf1efaa291c7827a7" | ||
integrity sha512-esZnJZJOiJR9wWKMyuvSE1y6Dq5LCuJanqhxslH2bxM6duahNZ+HMpCLhBQGZkbX6xRf8x1Y2eJlgt2q3qo49Q== | ||
dependencies: | ||
sax ">=0.6.0" | ||
xmlbuilder "~9.0.1" | ||
|
||
xmlbuilder@~9.0.1: | ||
version "9.0.7" | ||
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-9.0.7.tgz#132ee63d2ec5565c557e20f4c22df9aca686b10d" | ||
integrity sha1-Ey7mPS7FVlxVfiD0wi35rKaGsQ0= |