Open History to see the last commit
1import * as NodeCrypto from "node:crypto";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Schema from "effect/Schema";
5
6import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
7
8const CLOUD_LINK_KEY_PAIR = "cloud-link-ed25519-key-pair";
9const CLOUD_LINK_PRIVATE_KEY = "cloud-link-ed25519-private-key";
10const CLOUD_LINK_PUBLIC_KEY = "cloud-link-ed25519-public-key";
11
12const EnvironmentKeyPair = Schema.Struct({
13 privateKey: Schema.String,
14 publicKey: Schema.String,
15});
16type EnvironmentKeyPair = typeof EnvironmentKeyPair.Type;
17
18const EnvironmentKeyPairJson = Schema.fromJsonString(EnvironmentKeyPair);
19const decodeEnvironmentKeyPair = Schema.decodeUnknownEffect(EnvironmentKeyPairJson);
20const encodeEnvironmentKeyPair = Schema.encodeEffect(EnvironmentKeyPairJson);
21
22function bytesToString(bytes: Uint8Array): string {
23 return new TextDecoder().decode(bytes);
24}
25
26function stringToBytes(value: string): Uint8Array {
27 return new TextEncoder().encode(value);
28}
29
30const KEY_PAIR_RESOURCE = "environment signing key pair";
31
32const keyPairDecodeError = (cause: unknown): ServerSecretStore.SecretStoreDecodeError =>
33 new ServerSecretStore.SecretStoreDecodeError({ resource: KEY_PAIR_RESOURCE, cause });
34
35const keyPairEncodeError = (cause: unknown): ServerSecretStore.SecretStoreEncodeError =>
36 new ServerSecretStore.SecretStoreEncodeError({ resource: KEY_PAIR_RESOURCE, cause });
37
38const keyPairConcurrentReadError = (): ServerSecretStore.SecretStoreConcurrentReadError =>
39 new ServerSecretStore.SecretStoreConcurrentReadError({ resource: KEY_PAIR_RESOURCE });
40
41const readEnvironmentKeyPair = Effect.fn("readEnvironmentKeyPair")(function* (
42 secrets: ServerSecretStore.ServerSecretStore["Service"],
43) {
44 const encoded = yield* secrets.get(CLOUD_LINK_KEY_PAIR);
45 if (Option.isNone(encoded)) {
46 return Option.none<EnvironmentKeyPair>();
47 }
48 const decoded = yield* decodeEnvironmentKeyPair(bytesToString(encoded.value)).pipe(
49 Effect.mapError(keyPairDecodeError),
50 );
51 return Option.some(decoded);
52});
53
54const persistEnvironmentKeyPair = Effect.fn("persistEnvironmentKeyPair")(function* (
55 secrets: ServerSecretStore.ServerSecretStore["Service"],
56 keyPair: EnvironmentKeyPair,
57) {
58 const encoded = yield* encodeEnvironmentKeyPair(keyPair).pipe(
59 Effect.mapError(keyPairEncodeError),
60 );
61 return yield* secrets.create(CLOUD_LINK_KEY_PAIR, stringToBytes(encoded)).pipe(
62 Effect.as(keyPair),
63 Effect.catchIf(ServerSecretStore.isSecretStoreError, (error) =>
64 ServerSecretStore.isSecretAlreadyExistsError(error)
65 ? readEnvironmentKeyPair(secrets).pipe(
66 Effect.flatMap(
67 Option.match({
68 onSome: Effect.succeed,
69 onNone: () => Effect.fail(keyPairConcurrentReadError()),
70 }),
71 ),
72 )
73 : Effect.fail(error),
74 ),
75 );
76});
77
78export const getOrCreateEnvironmentKeyPairFromSecretStore = Effect.fn(function* (
79 secrets: ServerSecretStore.ServerSecretStore["Service"],
80) {
81 const existing = yield* readEnvironmentKeyPair(secrets);
82 if (Option.isSome(existing)) {
83 return existing.value;
84 }
85
86 const existingPrivate = yield* secrets.get(CLOUD_LINK_PRIVATE_KEY);
87 const existingPublic = yield* secrets.get(CLOUD_LINK_PUBLIC_KEY);
88 if (Option.isSome(existingPrivate) && Option.isSome(existingPublic)) {
89 return yield* persistEnvironmentKeyPair(secrets, {
90 privateKey: bytesToString(existingPrivate.value),
91 publicKey: bytesToString(existingPublic.value),
92 });
93 }
94
95 const keyPair = NodeCrypto.generateKeyPairSync("ed25519", {
96 privateKeyEncoding: { format: "pem", type: "pkcs8" },
97 publicKeyEncoding: { format: "pem", type: "spki" },
98 });
99 return yield* persistEnvironmentKeyPair(secrets, {
100 privateKey: keyPair.privateKey,
101 publicKey: keyPair.publicKey,
102 });
103});