convex-secret-store 0.1.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.
- package/LICENSE +201 -0
- package/README.md +604 -0
- package/dist/client/_generated/_ignore.d.ts +1 -0
- package/dist/client/_generated/_ignore.d.ts.map +1 -0
- package/dist/client/_generated/_ignore.js +3 -0
- package/dist/client/_generated/_ignore.js.map +1 -0
- package/dist/client/base64.d.ts +3 -0
- package/dist/client/base64.d.ts.map +1 -0
- package/dist/client/base64.js +32 -0
- package/dist/client/base64.js.map +1 -0
- package/dist/client/crypto.d.ts +43 -0
- package/dist/client/crypto.d.ts.map +1 -0
- package/dist/client/crypto.js +133 -0
- package/dist/client/crypto.js.map +1 -0
- package/dist/client/errors.d.ts +14 -0
- package/dist/client/errors.d.ts.map +1 -0
- package/dist/client/errors.js +29 -0
- package/dist/client/errors.js.map +1 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +3 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/operations.d.ts +88 -0
- package/dist/client/operations.d.ts.map +1 -0
- package/dist/client/operations.js +388 -0
- package/dist/client/operations.js.map +1 -0
- package/dist/client/options.d.ts +27 -0
- package/dist/client/options.d.ts.map +1 -0
- package/dist/client/options.js +66 -0
- package/dist/client/options.js.map +1 -0
- package/dist/client/types.d.ts +133 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +2 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +36 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +187 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/cleanup.d.ts +27 -0
- package/dist/component/cleanup.d.ts.map +1 -0
- package/dist/component/cleanup.js +96 -0
- package/dist/component/cleanup.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +3 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/lib.d.ts +177 -0
- package/dist/component/lib.d.ts.map +1 -0
- package/dist/component/lib.js +531 -0
- package/dist/component/lib.js.map +1 -0
- package/dist/component/schema.d.ts +73 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +45 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/shared.d.ts +7 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +12 -0
- package/dist/shared.js.map +1 -0
- package/package.json +115 -0
- package/src/client/_generated/_ignore.ts +1 -0
- package/src/client/base64.ts +37 -0
- package/src/client/crypto.ts +229 -0
- package/src/client/errors.ts +64 -0
- package/src/client/index.test.ts +480 -0
- package/src/client/index.ts +34 -0
- package/src/client/operations.ts +513 -0
- package/src/client/options.ts +110 -0
- package/src/client/setup.test.ts +26 -0
- package/src/client/types.ts +213 -0
- package/src/component/_generated/api.ts +52 -0
- package/src/component/_generated/component.ts +222 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/cleanup.test.ts +91 -0
- package/src/component/cleanup.ts +124 -0
- package/src/component/convex.config.ts +3 -0
- package/src/component/lib.test.ts +202 -0
- package/src/component/lib.ts +659 -0
- package/src/component/schema.ts +48 -0
- package/src/component/setup.test.ts +11 -0
- package/src/shared.ts +28 -0
- package/src/test.ts +18 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { metadataValidator, secretEventTypeValidator } from "../shared.js";
|
|
4
|
+
export const secretsFields = {
|
|
5
|
+
namespace: v.optional(v.string()),
|
|
6
|
+
name: v.string(),
|
|
7
|
+
encryptedValue: v.string(),
|
|
8
|
+
iv: v.string(),
|
|
9
|
+
wrappedDEK: v.string(),
|
|
10
|
+
dekIv: v.string(),
|
|
11
|
+
keyVersion: v.number(),
|
|
12
|
+
metadata: v.optional(metadataValidator),
|
|
13
|
+
expiresAt: v.optional(v.number()),
|
|
14
|
+
updatedAt: v.number(),
|
|
15
|
+
};
|
|
16
|
+
export const secretEventsFields = {
|
|
17
|
+
secretId: v.id("secrets"),
|
|
18
|
+
namespace: v.optional(v.string()),
|
|
19
|
+
name: v.string(),
|
|
20
|
+
type: secretEventTypeValidator,
|
|
21
|
+
metadata: v.optional(metadataValidator),
|
|
22
|
+
createdAt: v.number(),
|
|
23
|
+
};
|
|
24
|
+
export default defineSchema({
|
|
25
|
+
secrets: defineTable(secretsFields)
|
|
26
|
+
.index("by_namespace_and_name", ["namespace", "name"])
|
|
27
|
+
.index("by_namespace_and_updated_at", ["namespace", "updatedAt"])
|
|
28
|
+
.index("by_key_version", ["keyVersion"])
|
|
29
|
+
.index("by_expires_at", ["expiresAt"]),
|
|
30
|
+
secretEvents: defineTable(secretEventsFields)
|
|
31
|
+
.index("by_secret_id_and_created_at", ["secretId", "createdAt"])
|
|
32
|
+
.index("by_namespace_and_created_at", ["namespace", "createdAt"])
|
|
33
|
+
.index("by_namespace_and_name_and_created_at", [
|
|
34
|
+
"namespace",
|
|
35
|
+
"name",
|
|
36
|
+
"createdAt",
|
|
37
|
+
])
|
|
38
|
+
.index("by_namespace_and_type_and_created_at", [
|
|
39
|
+
"namespace",
|
|
40
|
+
"type",
|
|
41
|
+
"createdAt",
|
|
42
|
+
])
|
|
43
|
+
.index("by_created_at", ["createdAt"]),
|
|
44
|
+
});
|
|
45
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/component/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAClC,OAAO,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,cAAc,CAAC;AAE3E,MAAM,CAAC,MAAM,aAAa,GAAG;IAC3B,SAAS,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACjC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;IAChB,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE;IAC1B,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE;IACd,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;IACtB,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE;IACjB,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;IACtB,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,iBAAiB,CAAC;IACvC,SAAS,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACjC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,QAAQ,EAAE,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC;IACzB,SAAS,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC;IACjC,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;IAChB,IAAI,EAAE,wBAAwB;IAC9B,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,iBAAiB,CAAC;IACvC,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;CACtB,CAAC;AAEF,eAAe,YAAY,CAAC;IAC1B,OAAO,EAAE,WAAW,CAAC,aAAa,CAAC;SAChC,KAAK,CAAC,uBAAuB,EAAE,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;SACrD,KAAK,CAAC,6BAA6B,EAAE,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;SAChE,KAAK,CAAC,gBAAgB,EAAE,CAAC,YAAY,CAAC,CAAC;SACvC,KAAK,CAAC,eAAe,EAAE,CAAC,WAAW,CAAC,CAAC;IAExC,YAAY,EAAE,WAAW,CAAC,kBAAkB,CAAC;SAC1C,KAAK,CAAC,6BAA6B,EAAE,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;SAC/D,KAAK,CAAC,6BAA6B,EAAE,CAAC,WAAW,EAAE,WAAW,CAAC,CAAC;SAChE,KAAK,CAAC,sCAAsC,EAAE;QAC7C,WAAW;QACX,MAAM;QACN,WAAW;KACZ,CAAC;SACD,KAAK,CAAC,sCAAsC,EAAE;QAC7C,WAAW;QACX,MAAM;QACN,WAAW;KACZ,CAAC;SACD,KAAK,CAAC,eAAe,EAAE,CAAC,WAAW,CAAC,CAAC;CACzC,CAAC,CAAC"}
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const secretEventTypes: readonly ["created", "updated", "deleted", "rotated"];
|
|
2
|
+
export type SecretEventType = (typeof secretEventTypes)[number];
|
|
3
|
+
export declare const metadataValidator: import("convex/values").VRecord<Record<string, any>, import("convex/values").VString<string, "required">, import("convex/values").VAny<any, "required", string>, "required", string>;
|
|
4
|
+
export declare const secretEventTypeValidator: import("convex/values").VUnion<"created" | "updated" | "deleted" | "rotated", [import("convex/values").VLiteral<"created", "required">, import("convex/values").VLiteral<"updated", "required">, import("convex/values").VLiteral<"deleted", "required">, import("convex/values").VLiteral<"rotated", "required">], "required", never>;
|
|
5
|
+
export declare const orderValidator: import("convex/values").VUnion<"asc" | "desc" | undefined, [import("convex/values").VLiteral<"asc", "required">, import("convex/values").VLiteral<"desc", "required">], "optional", never>;
|
|
6
|
+
export declare const effectiveStateValidator: import("convex/values").VUnion<"active" | "expired", [import("convex/values").VLiteral<"active", "required">, import("convex/values").VLiteral<"expired", "required">], "required", never>;
|
|
7
|
+
//# sourceMappingURL=shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,gBAAgB,uDAKnB,CAAC;AAEX,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEhE,eAAO,MAAM,iBAAiB,sLAAgC,CAAC;AAE/D,eAAO,MAAM,wBAAwB,wUAKpC,CAAC;AAEF,eAAO,MAAM,cAAc,4LAE1B,CAAC;AAEF,eAAO,MAAM,uBAAuB,4LAGnC,CAAC"}
|
package/dist/shared.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
export const secretEventTypes = [
|
|
3
|
+
"created",
|
|
4
|
+
"updated",
|
|
5
|
+
"deleted",
|
|
6
|
+
"rotated",
|
|
7
|
+
];
|
|
8
|
+
export const metadataValidator = v.record(v.string(), v.any());
|
|
9
|
+
export const secretEventTypeValidator = v.union(v.literal("created"), v.literal("updated"), v.literal("deleted"), v.literal("rotated"));
|
|
10
|
+
export const orderValidator = v.optional(v.union(v.literal("asc"), v.literal("desc")));
|
|
11
|
+
export const effectiveStateValidator = v.union(v.literal("active"), v.literal("expired"));
|
|
12
|
+
//# sourceMappingURL=shared.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shared.js","sourceRoot":"","sources":["../src/shared.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,eAAe,CAAC;AAElC,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,SAAS;IACT,SAAS;IACT,SAAS;IACT,SAAS;CACD,CAAC;AAIX,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC;AAE/D,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,CAAC,KAAK,CAC7C,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,EACpB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CACrB,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAC,QAAQ,CACtC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAC7C,CAAC;AAEF,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,CAAC,KAAK,CAC5C,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,EACnB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,CACrB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "convex-secret-store",
|
|
3
|
+
"description": "A secret store component for Convex.",
|
|
4
|
+
"repository": "github:gaganref/convex-secret-store",
|
|
5
|
+
"homepage": "https://github.com/gaganref/convex-secret-store#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/gaganref/convex-secret-store/issues"
|
|
8
|
+
},
|
|
9
|
+
"version": "0.1.0",
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"convex",
|
|
13
|
+
"component",
|
|
14
|
+
"secret",
|
|
15
|
+
"encryption",
|
|
16
|
+
"vault",
|
|
17
|
+
"key-rotation",
|
|
18
|
+
"audit-log"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"scripts": {
|
|
22
|
+
"dev": "run-p -r 'dev:*'",
|
|
23
|
+
"dev:backend": "convex dev --typecheck-components",
|
|
24
|
+
"dev:frontend": "cd example && vite --clearScreen false",
|
|
25
|
+
"dev:build": "chokidar 'tsconfig*.json' 'src/**/*.ts' -i '**/*.test.ts' -c 'npm run build:codegen' --initial",
|
|
26
|
+
"predev": "path-exists .env.local dist || (npm run build && convex dev --once)",
|
|
27
|
+
"build": "tsc --project ./tsconfig.build.json",
|
|
28
|
+
"build:codegen": "npx convex codegen --component-dir ./src/component && npm run build",
|
|
29
|
+
"build:clean": "rm -rf dist *.tsbuildinfo && npm run build:codegen",
|
|
30
|
+
"typecheck": "tsc --noEmit && tsc -p example && tsc -p example/convex",
|
|
31
|
+
"lint": "eslint .",
|
|
32
|
+
"all": "run-p -r 'dev:*' 'test:watch'",
|
|
33
|
+
"test": "vitest run --typecheck",
|
|
34
|
+
"test:watch": "vitest --typecheck --clearScreen false",
|
|
35
|
+
"test:debug": "vitest --inspect-brk --no-file-parallelism",
|
|
36
|
+
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
37
|
+
"preversion": "npm ci && npm run build:clean && run-p test lint typecheck",
|
|
38
|
+
"prepublishOnly": "npm whoami || npm login",
|
|
39
|
+
"alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
|
|
40
|
+
"release": "npm version patch && npm publish && git push --follow-tags",
|
|
41
|
+
"version": "vim -c 'normal o' -c 'normal o## '$npm_package_version CHANGELOG.md && prettier -w CHANGELOG.md && git add CHANGELOG.md"
|
|
42
|
+
},
|
|
43
|
+
"files": [
|
|
44
|
+
"dist",
|
|
45
|
+
"src"
|
|
46
|
+
],
|
|
47
|
+
"exports": {
|
|
48
|
+
"./package.json": "./package.json",
|
|
49
|
+
".": {
|
|
50
|
+
"types": "./dist/client/index.d.ts",
|
|
51
|
+
"default": "./dist/client/index.js"
|
|
52
|
+
},
|
|
53
|
+
"./test": "./src/test.ts",
|
|
54
|
+
"./_generated/component.js": {
|
|
55
|
+
"types": "./dist/component/_generated/component.d.ts"
|
|
56
|
+
},
|
|
57
|
+
"./_generated/component": {
|
|
58
|
+
"types": "./dist/component/_generated/component.d.ts"
|
|
59
|
+
},
|
|
60
|
+
"./convex.config.js": {
|
|
61
|
+
"types": "./dist/component/convex.config.d.ts",
|
|
62
|
+
"default": "./dist/component/convex.config.js"
|
|
63
|
+
},
|
|
64
|
+
"./convex.config": {
|
|
65
|
+
"types": "./dist/component/convex.config.d.ts",
|
|
66
|
+
"default": "./dist/component/convex.config.js"
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"peerDependencies": {
|
|
70
|
+
"convex": "^1.32.0"
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@base-ui/react": "^1.2.0",
|
|
74
|
+
"@convex-dev/eslint-plugin": "^1.1.1",
|
|
75
|
+
"@edge-runtime/vm": "^5.0.0",
|
|
76
|
+
"@eslint/eslintrc": "^3.3.3",
|
|
77
|
+
"@eslint/js": "9.39.2",
|
|
78
|
+
"@fontsource-variable/jetbrains-mono": "^5.2.8",
|
|
79
|
+
"@phosphor-icons/react": "^2.1.10",
|
|
80
|
+
"@tailwindcss/vite": "^4.2.1",
|
|
81
|
+
"@types/node": "^24.10.11",
|
|
82
|
+
"@types/react": "^19.2.13",
|
|
83
|
+
"@types/react-dom": "^19.2.3",
|
|
84
|
+
"@vitejs/plugin-react": "^5.1.3",
|
|
85
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
86
|
+
"chokidar-cli": "3.0.0",
|
|
87
|
+
"class-variance-authority": "^0.7.1",
|
|
88
|
+
"clsx": "^2.1.1",
|
|
89
|
+
"convex": "1.32.0",
|
|
90
|
+
"convex-helpers": "0.1.106",
|
|
91
|
+
"convex-test": "0.0.41",
|
|
92
|
+
"date-fns": "^4.1.0",
|
|
93
|
+
"eslint": "9.39.2",
|
|
94
|
+
"eslint-plugin-react": "^7.37.5",
|
|
95
|
+
"eslint-plugin-react-hooks": "^7.0.1",
|
|
96
|
+
"eslint-plugin-react-refresh": "^0.5.0",
|
|
97
|
+
"globals": "^17.3.0",
|
|
98
|
+
"npm-run-all2": "8.0.4",
|
|
99
|
+
"path-exists-cli": "2.0.0",
|
|
100
|
+
"pkg-pr-new": "^0.0.63",
|
|
101
|
+
"prettier": "3.8.1",
|
|
102
|
+
"react": "^19.2.4",
|
|
103
|
+
"react-dom": "^19.2.4",
|
|
104
|
+
"shadcn": "^4.0.0",
|
|
105
|
+
"tailwind-merge": "^3.5.0",
|
|
106
|
+
"tailwindcss": "^4.2.1",
|
|
107
|
+
"tw-animate-css": "^1.4.0",
|
|
108
|
+
"typescript": "5.9.3",
|
|
109
|
+
"typescript-eslint": "8.54.0",
|
|
110
|
+
"vite": "7.3.1",
|
|
111
|
+
"vitest": "4.0.18"
|
|
112
|
+
},
|
|
113
|
+
"types": "./dist/client/index.d.ts",
|
|
114
|
+
"module": "./dist/client/index.js"
|
|
115
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// This is only here so convex-test can detect a _generated folder
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { invalidArgumentError, runtimeUnavailableError } from "./errors.js";
|
|
2
|
+
|
|
3
|
+
export function decodeBase64(input: string): Uint8Array {
|
|
4
|
+
const normalized = input.replace(/-/g, "+").replace(/_/g, "/");
|
|
5
|
+
const padded = normalized.padEnd(
|
|
6
|
+
normalized.length + ((4 - (normalized.length % 4 || 4)) % 4),
|
|
7
|
+
"=",
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
if (typeof atob === "function") {
|
|
12
|
+
const decoded = atob(padded);
|
|
13
|
+
return Uint8Array.from(decoded, (char) => char.charCodeAt(0));
|
|
14
|
+
}
|
|
15
|
+
if (typeof Buffer !== "undefined") {
|
|
16
|
+
return Uint8Array.from(Buffer.from(padded, "base64"));
|
|
17
|
+
}
|
|
18
|
+
} catch (error) {
|
|
19
|
+
throw invalidArgumentError(`invalid base64 data: ${String(error)}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
throw runtimeUnavailableError("base64 decoding is unavailable");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function encodeBase64(bytes: Uint8Array): string {
|
|
26
|
+
if (typeof btoa === "function") {
|
|
27
|
+
let binary = "";
|
|
28
|
+
for (const byte of bytes) {
|
|
29
|
+
binary += String.fromCharCode(byte);
|
|
30
|
+
}
|
|
31
|
+
return btoa(binary);
|
|
32
|
+
}
|
|
33
|
+
if (typeof Buffer !== "undefined") {
|
|
34
|
+
return Buffer.from(bytes).toString("base64");
|
|
35
|
+
}
|
|
36
|
+
throw runtimeUnavailableError("base64 encoding is unavailable");
|
|
37
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { invalidArgumentError, runtimeUnavailableError } from "./errors.js";
|
|
2
|
+
import { decodeBase64, encodeBase64 } from "./base64.js";
|
|
3
|
+
|
|
4
|
+
const textEncoder = new TextEncoder();
|
|
5
|
+
const textDecoder = new TextDecoder();
|
|
6
|
+
|
|
7
|
+
function asBufferSource(bytes: Uint8Array): Uint8Array<ArrayBuffer> {
|
|
8
|
+
const copy = new Uint8Array(bytes.byteLength);
|
|
9
|
+
copy.set(bytes);
|
|
10
|
+
return copy;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getRuntimeCrypto(): Crypto {
|
|
14
|
+
if (
|
|
15
|
+
typeof globalThis.crypto === "undefined" ||
|
|
16
|
+
typeof globalThis.crypto.subtle === "undefined"
|
|
17
|
+
) {
|
|
18
|
+
throw runtimeUnavailableError("Web Crypto API is unavailable");
|
|
19
|
+
}
|
|
20
|
+
return globalThis.crypto;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function base64UrlEncode(bytes: Uint8Array): string {
|
|
24
|
+
return encodeBase64(bytes)
|
|
25
|
+
.replace(/\+/g, "-")
|
|
26
|
+
.replace(/\//g, "_")
|
|
27
|
+
.replace(/=+$/g, "");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function base64UrlDecode(input: string): Uint8Array {
|
|
31
|
+
return decodeBase64(input);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function utf8Bytes(value: string): Uint8Array {
|
|
35
|
+
return textEncoder.encode(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function buildAadParts(namespace: string | undefined, name: string) {
|
|
39
|
+
return [namespace ?? null, name];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function buildValueAAD(
|
|
43
|
+
namespace: string | undefined,
|
|
44
|
+
name: string,
|
|
45
|
+
): Uint8Array {
|
|
46
|
+
return utf8Bytes(
|
|
47
|
+
JSON.stringify(["value", ...buildAadParts(namespace, name)]),
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function buildDekAAD(
|
|
52
|
+
namespace: string | undefined,
|
|
53
|
+
name: string,
|
|
54
|
+
keyVersion: number,
|
|
55
|
+
): Uint8Array {
|
|
56
|
+
return utf8Bytes(
|
|
57
|
+
JSON.stringify(["dek", ...buildAadParts(namespace, name), keyVersion]),
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export async function importKey(rawKeyBase64: string): Promise<CryptoKey> {
|
|
62
|
+
const bytes = decodeBase64(rawKeyBase64);
|
|
63
|
+
if (bytes.byteLength !== 32) {
|
|
64
|
+
throw invalidArgumentError("KEK must decode to exactly 32 bytes");
|
|
65
|
+
}
|
|
66
|
+
return await getRuntimeCrypto().subtle.importKey(
|
|
67
|
+
"raw",
|
|
68
|
+
asBufferSource(bytes),
|
|
69
|
+
"AES-GCM",
|
|
70
|
+
false,
|
|
71
|
+
["encrypt", "decrypt"],
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function generateDEK(): Uint8Array {
|
|
76
|
+
const bytes = new Uint8Array(32);
|
|
77
|
+
getRuntimeCrypto().getRandomValues(bytes);
|
|
78
|
+
return bytes;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function generateIV(): Uint8Array {
|
|
82
|
+
const bytes = new Uint8Array(12);
|
|
83
|
+
getRuntimeCrypto().getRandomValues(bytes);
|
|
84
|
+
return bytes;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function importDekKey(dek: Uint8Array): Promise<CryptoKey> {
|
|
88
|
+
return await getRuntimeCrypto().subtle.importKey(
|
|
89
|
+
"raw",
|
|
90
|
+
asBufferSource(dek),
|
|
91
|
+
"AES-GCM",
|
|
92
|
+
false,
|
|
93
|
+
["encrypt", "decrypt"],
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function encryptRaw(params: {
|
|
98
|
+
key: CryptoKey;
|
|
99
|
+
iv: Uint8Array;
|
|
100
|
+
plaintext: Uint8Array;
|
|
101
|
+
aad: Uint8Array;
|
|
102
|
+
}): Promise<Uint8Array> {
|
|
103
|
+
const ciphertext = await getRuntimeCrypto().subtle.encrypt(
|
|
104
|
+
{
|
|
105
|
+
name: "AES-GCM",
|
|
106
|
+
iv: asBufferSource(params.iv),
|
|
107
|
+
additionalData: asBufferSource(params.aad),
|
|
108
|
+
},
|
|
109
|
+
params.key,
|
|
110
|
+
asBufferSource(params.plaintext),
|
|
111
|
+
);
|
|
112
|
+
return new Uint8Array(ciphertext);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function decryptRaw(params: {
|
|
116
|
+
key: CryptoKey;
|
|
117
|
+
iv: Uint8Array;
|
|
118
|
+
ciphertext: Uint8Array;
|
|
119
|
+
aad: Uint8Array;
|
|
120
|
+
}): Promise<Uint8Array> {
|
|
121
|
+
const plaintext = await getRuntimeCrypto().subtle.decrypt(
|
|
122
|
+
{
|
|
123
|
+
name: "AES-GCM",
|
|
124
|
+
iv: asBufferSource(params.iv),
|
|
125
|
+
additionalData: asBufferSource(params.aad),
|
|
126
|
+
},
|
|
127
|
+
params.key,
|
|
128
|
+
asBufferSource(params.ciphertext),
|
|
129
|
+
);
|
|
130
|
+
return new Uint8Array(plaintext);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function encryptSecret(params: {
|
|
134
|
+
value: string;
|
|
135
|
+
namespace: string | undefined;
|
|
136
|
+
name: string;
|
|
137
|
+
keyVersion: number;
|
|
138
|
+
kek: CryptoKey;
|
|
139
|
+
}): Promise<{
|
|
140
|
+
encryptedValue: string;
|
|
141
|
+
iv: string;
|
|
142
|
+
wrappedDEK: string;
|
|
143
|
+
dekIv: string;
|
|
144
|
+
}> {
|
|
145
|
+
const dek = generateDEK();
|
|
146
|
+
const valueIv = generateIV();
|
|
147
|
+
const dekIv = generateIV();
|
|
148
|
+
const dekKey = await importDekKey(dek);
|
|
149
|
+
|
|
150
|
+
const encryptedValue = await encryptRaw({
|
|
151
|
+
key: dekKey,
|
|
152
|
+
iv: valueIv,
|
|
153
|
+
plaintext: utf8Bytes(params.value),
|
|
154
|
+
aad: buildValueAAD(params.namespace, params.name),
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const wrappedDEK = await encryptRaw({
|
|
158
|
+
key: params.kek,
|
|
159
|
+
iv: dekIv,
|
|
160
|
+
plaintext: dek,
|
|
161
|
+
aad: buildDekAAD(params.namespace, params.name, params.keyVersion),
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
encryptedValue: base64UrlEncode(encryptedValue),
|
|
166
|
+
iv: base64UrlEncode(valueIv),
|
|
167
|
+
wrappedDEK: base64UrlEncode(wrappedDEK),
|
|
168
|
+
dekIv: base64UrlEncode(dekIv),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export async function decryptSecret(params: {
|
|
173
|
+
encryptedValue: string;
|
|
174
|
+
iv: string;
|
|
175
|
+
wrappedDEK: string;
|
|
176
|
+
dekIv: string;
|
|
177
|
+
namespace: string | undefined;
|
|
178
|
+
name: string;
|
|
179
|
+
keyVersion: number;
|
|
180
|
+
kek: CryptoKey;
|
|
181
|
+
}): Promise<string> {
|
|
182
|
+
const dek = await decryptRaw({
|
|
183
|
+
key: params.kek,
|
|
184
|
+
iv: base64UrlDecode(params.dekIv),
|
|
185
|
+
ciphertext: base64UrlDecode(params.wrappedDEK),
|
|
186
|
+
aad: buildDekAAD(params.namespace, params.name, params.keyVersion),
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const dekKey = await importDekKey(dek);
|
|
190
|
+
const plaintext = await decryptRaw({
|
|
191
|
+
key: dekKey,
|
|
192
|
+
iv: base64UrlDecode(params.iv),
|
|
193
|
+
ciphertext: base64UrlDecode(params.encryptedValue),
|
|
194
|
+
aad: buildValueAAD(params.namespace, params.name),
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
return textDecoder.decode(plaintext);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export async function rewrapDEK(params: {
|
|
201
|
+
wrappedDEK: string;
|
|
202
|
+
dekIv: string;
|
|
203
|
+
namespace: string | undefined;
|
|
204
|
+
name: string;
|
|
205
|
+
fromVersion: number;
|
|
206
|
+
toVersion: number;
|
|
207
|
+
fromKek: CryptoKey;
|
|
208
|
+
toKek: CryptoKey;
|
|
209
|
+
}): Promise<{ wrappedDEK: string; dekIv: string }> {
|
|
210
|
+
const dek = await decryptRaw({
|
|
211
|
+
key: params.fromKek,
|
|
212
|
+
iv: base64UrlDecode(params.dekIv),
|
|
213
|
+
ciphertext: base64UrlDecode(params.wrappedDEK),
|
|
214
|
+
aad: buildDekAAD(params.namespace, params.name, params.fromVersion),
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
const nextIv = generateIV();
|
|
218
|
+
const wrappedDEK = await encryptRaw({
|
|
219
|
+
key: params.toKek,
|
|
220
|
+
iv: nextIv,
|
|
221
|
+
plaintext: dek,
|
|
222
|
+
aad: buildDekAAD(params.namespace, params.name, params.toVersion),
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
return {
|
|
226
|
+
wrappedDEK: base64UrlEncode(wrappedDEK),
|
|
227
|
+
dekIv: base64UrlEncode(nextIv),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export type SecretStoreClientErrorCode =
|
|
2
|
+
| "INVALID_OPTIONS"
|
|
3
|
+
| "INVALID_ARGUMENT"
|
|
4
|
+
| "KEY_VERSION_UNAVAILABLE"
|
|
5
|
+
| "RUNTIME_UNAVAILABLE"
|
|
6
|
+
| "OPERATION_FAILED";
|
|
7
|
+
|
|
8
|
+
export class SecretStoreClientError extends Error {
|
|
9
|
+
readonly name = "SecretStoreClientError";
|
|
10
|
+
readonly code: SecretStoreClientErrorCode;
|
|
11
|
+
readonly cause?: unknown;
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
code: SecretStoreClientErrorCode,
|
|
15
|
+
message: string,
|
|
16
|
+
cause?: unknown,
|
|
17
|
+
) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.code = code;
|
|
20
|
+
this.cause = cause;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function isSecretStoreClientError(
|
|
25
|
+
error: unknown,
|
|
26
|
+
): error is SecretStoreClientError {
|
|
27
|
+
return error instanceof SecretStoreClientError;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function optionsError(message: string) {
|
|
31
|
+
return new SecretStoreClientError(
|
|
32
|
+
"INVALID_OPTIONS",
|
|
33
|
+
`secret-store options: ${message}`,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function invalidArgumentError(message: string) {
|
|
38
|
+
return new SecretStoreClientError(
|
|
39
|
+
"INVALID_ARGUMENT",
|
|
40
|
+
`secret-store: ${message}`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function keyVersionUnavailableError(version: number) {
|
|
45
|
+
return new SecretStoreClientError(
|
|
46
|
+
"KEY_VERSION_UNAVAILABLE",
|
|
47
|
+
`secret-store: key version ${version} is not configured`,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function runtimeUnavailableError(message: string) {
|
|
52
|
+
return new SecretStoreClientError(
|
|
53
|
+
"RUNTIME_UNAVAILABLE",
|
|
54
|
+
`secret-store runtime: ${message}`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function operationFailedError(message: string, cause: unknown) {
|
|
59
|
+
return new SecretStoreClientError(
|
|
60
|
+
"OPERATION_FAILED",
|
|
61
|
+
`secret-store operation failed: ${message}`,
|
|
62
|
+
cause,
|
|
63
|
+
);
|
|
64
|
+
}
|