Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

environmentKeys.test.ts

97 lines3.9 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as PlatformError from "effect/PlatformError";
8​
9import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
10import * as ServerConfig from "../config.ts";
11import { getOrCreateEnvironmentKeyPairFromSecretStore } from "./environmentKeys.ts";
12​
13const layerServerSecretStore = () =>
14 ServerSecretStore.layer.pipe(
15 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-environment-keys-test-" })),
16 );
17​
18const unusedSecretStoreOperation = () => Effect.die("unused secret-store operation");
19​
20it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))(
21 "getOrCreateEnvironmentKeyPairFromSecretStore",
22 (it) => {
23 it.effect("persists one atomic keypair secret and reuses it", () =>
24 Effect.gen(function* () {
25 const secretStore = yield* ServerSecretStore.ServerSecretStore;
26​
27 const first = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secretStore);
28 const second = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secretStore);
29​
30 assert.deepEqual(second, first);
31 assert.isTrue(Option.isSome(yield* secretStore.get("cloud-link-ed25519-key-pair")));
32 assert.isTrue(Option.isNone(yield* secretStore.get("cloud-link-ed25519-private-key")));
33 assert.isTrue(Option.isNone(yield* secretStore.get("cloud-link-ed25519-public-key")));
34 }).pipe(Effect.provide(layerServerSecretStore())),
35 );
36​
37 it.effect("migrates a legacy keypair into the atomic secret", () =>
38 Effect.gen(function* () {
39 const secretStore = yield* ServerSecretStore.ServerSecretStore;
40 yield* secretStore.set(
41 "cloud-link-ed25519-private-key",
42 new TextEncoder().encode("private"),
43 );
44 yield* secretStore.set("cloud-link-ed25519-public-key", new TextEncoder().encode("public"));
45​
46 assert.deepEqual(yield* getOrCreateEnvironmentKeyPairFromSecretStore(secretStore), {
47 privateKey: "private",
48 publicKey: "public",
49 });
50 assert.isTrue(Option.isSome(yield* secretStore.get("cloud-link-ed25519-key-pair")));
51 }).pipe(Effect.provide(layerServerSecretStore())),
52 );
53​
54 it.effect("uses the persisted keypair when a concurrent creator wins", () =>
55 Effect.gen(function* () {
56 const winner = new TextEncoder().encode(
57 '{"privateKey":"winner-private","publicKey":"winner-public"}',
58 );
59 let createAttempted = false;
60 const secretStore = {
61 get: (name) =>
62 Effect.sync(() =>
63 name === "cloud-link-ed25519-key-pair" && createAttempted
64 ? Option.some(winner)
65 : Option.none(),
66 ),
67 set: unusedSecretStoreOperation,
68 create: () =>
69 Effect.sync(() => {
70 createAttempted = true;
71 }).pipe(
72 Effect.flatMap(() =>
73 Effect.fail(
74 new ServerSecretStore.SecretStorePersistError({
75 resource: "environment signing key pair",
76 cause: PlatformError.systemError({
77 _tag: "AlreadyExists",
78 module: "FileSystem",
79 method: "open",
80 pathOrDescriptor: "cloud-link-ed25519-key-pair.bin",
81 }),
82 }),
83 ),
84 ),
85 ),
86 getOrCreateRandom: unusedSecretStoreOperation,
87 remove: unusedSecretStoreOperation,
88 } satisfies ServerSecretStore.ServerSecretStore["Service"];
89​
90 assert.deepEqual(yield* getOrCreateEnvironmentKeyPairFromSecretStore(secretStore), {
91 privateKey: "winner-private",
92 publicKey: "winner-public",
93 });
94 }),
95 );
96 },
97);