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

environmentKeys.test.ts

87 lines3.6 KB

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