@evenicanpm/common-core 1.0.0-alpha

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.
Files changed (2) hide show
  1. package/keyvault.ts +61 -0
  2. package/package.json +14 -0
package/keyvault.ts ADDED
@@ -0,0 +1,61 @@
1
+ import { DefaultAzureCredential } from "@azure/identity";
2
+ import { SecretClient } from "@azure/keyvault-secrets";
3
+
4
+ // The KeyVault information
5
+ const vaultName = process.env.KEYVAULT_NAME;
6
+ const vaultEndpoint = process.env.KEYVAULT_ENDPOINT;
7
+ const url = `https://${vaultName}.${vaultEndpoint}`;
8
+
9
+ // Initialize local secrets cache object
10
+ let secrets = new Map();
11
+
12
+ // A Key and Secret class to Cache Secrets
13
+ class Secret {
14
+ key: string;
15
+ secret: string | undefined;
16
+ initTime: Date;
17
+
18
+ constructor(key: string, secret: string | undefined) {
19
+ this.key = key;
20
+ this.secret = secret;
21
+ this.initTime = new Date();
22
+ }
23
+ }
24
+
25
+ /**
26
+ * @description - Check for all available secret names in the KeyVault, and then get their secrets,
27
+ * set all secrets in the "secrets" map binding which is exported from this module.
28
+ */
29
+ const initializeKeyVaultCache = async () => {
30
+ if (process.env.NODE_ENV == "production") {
31
+ const credential = new DefaultAzureCredential();
32
+ const client = new SecretClient(url, credential);
33
+
34
+ for await (let secretProperties of client.listPropertiesOfSecrets()) {
35
+ const result = await client.getSecret(secretProperties.name);
36
+ secrets.set(
37
+ secretProperties.name,
38
+ new Secret(secretProperties.name, result.value),
39
+ );
40
+ }
41
+ return secrets;
42
+ }
43
+ };
44
+
45
+ /**
46
+ * @description - Gets Secret from .env variables if development environment or secretVault if production
47
+ * @param {string} key - Unique key to identify the secret, the secret name in Azure KeyVault
48
+ * @returns Returns the secret string.
49
+ */
50
+ const getSecret = (key: string): string | undefined => {
51
+ if (process.env.NODE_ENV != "production") {
52
+ const fixedName = key.replace(/-/g, "_");
53
+ return process.env[fixedName];
54
+ } else {
55
+ const fixedName = key.replace(/_/g, "-");
56
+ const secret = secrets.get(fixedName).secret;
57
+ return secret;
58
+ }
59
+ };
60
+
61
+ export { getSecret, initializeKeyVaultCache, secrets };
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@evenicanpm/common-core",
3
+ "version": "1.0.0-alpha",
4
+ "description": "Common module for Headless Storefront and Admin panel",
5
+ "author": "Evenica",
6
+ "license": "ISC",
7
+ "scripts": {
8
+ "test": "echo \"Error: no test specified\" && exit 1"
9
+ },
10
+ "dependencies": {
11
+ "@azure/identity": "^4.10.1",
12
+ "@azure/keyvault-secrets": "^4.10.0"
13
+ }
14
+ }