Open History to see the last commit
1import * as Crypto from "node:crypto";
2
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Path from "effect/Path";
7import * as Predicate from "effect/Predicate";
8import * as PlatformError from "effect/PlatformError";
9
10import { ServerConfig } from "../../config.ts";
11import {
12 SecretStoreError,
13 ServerSecretStore,
14 type ServerSecretStoreShape,
15} from "../Services/ServerSecretStore.ts";
16
17export const makeServerSecretStore = Effect.gen(function* () {
18 const fileSystem = yield* FileSystem.FileSystem;
19 const path = yield* Path.Path;
20 const serverConfig = yield* ServerConfig;
21
22 yield* fileSystem.makeDirectory(serverConfig.secretsDir, { recursive: true });
23 yield* fileSystem.chmod(serverConfig.secretsDir, 0o700).pipe(
24 Effect.mapError(
25 (cause) =>
26 new SecretStoreError({
27 message: `Failed to secure secrets directory ${serverConfig.secretsDir}.`,
28 cause,
29 }),
30 ),
31 );
32
33 const resolveSecretPath = (name: string) => path.join(serverConfig.secretsDir, `${name}.bin`);
34
35 const isPlatformError = (u: unknown): u is PlatformError.PlatformError =>
36 Predicate.isTagged(u, "PlatformError");
37
38 const get: ServerSecretStoreShape["get"] = (name) =>
39 fileSystem.readFile(resolveSecretPath(name)).pipe(
40 Effect.map((bytes) => Uint8Array.from(bytes)),
41 Effect.catch((cause) =>
42 cause.reason._tag === "NotFound"
43 ? Effect.succeed(null)
44 : Effect.fail(
45 new SecretStoreError({
46 message: `Failed to read secret ${name}.`,
47 cause,
48 }),
49 ),
50 ),
51 );
52
53 const set: ServerSecretStoreShape["set"] = (name, value) => {
54 const secretPath = resolveSecretPath(name);
55 const tempPath = `${secretPath}.${Crypto.randomUUID()}.tmp`;
56 return Effect.gen(function* () {
57 yield* fileSystem.writeFile(tempPath, value);
58 yield* fileSystem.chmod(tempPath, 0o600);
59 yield* fileSystem.rename(tempPath, secretPath);
60 yield* fileSystem.chmod(secretPath, 0o600);
61 }).pipe(
62 Effect.catch((cause) =>
63 fileSystem.remove(tempPath).pipe(
64 Effect.ignore,
65 Effect.flatMap(() =>
66 Effect.fail(
67 new SecretStoreError({
68 message: `Failed to persist secret ${name}.`,
69 cause,
70 }),
71 ),
72 ),
73 ),
74 ),
75 );
76 };
77
78 const create: ServerSecretStoreShape["set"] = (name, value) => {
79 const secretPath = resolveSecretPath(name);
80 return Effect.scoped(
81 Effect.gen(function* () {
82 const file = yield* fileSystem.open(secretPath, {
83 flag: "wx",
84 mode: 0o600,
85 });
86 yield* file.writeAll(value);
87 yield* file.sync;
88 yield* fileSystem.chmod(secretPath, 0o600);
89 }),
90 ).pipe(
91 Effect.mapError(
92 (cause) =>
93 new SecretStoreError({
94 message: `Failed to persist secret ${name}.`,
95 cause,
96 }),
97 ),
98 );
99 };
100
101 const getOrCreateRandom: ServerSecretStoreShape["getOrCreateRandom"] = (name, bytes) =>
102 get(name).pipe(
103 Effect.flatMap((existing) => {
104 if (existing) {
105 return Effect.succeed(existing);
106 }
107
108 const generated = Crypto.randomBytes(bytes);
109 return create(name, generated).pipe(
110 Effect.as(Uint8Array.from(generated)),
111 Effect.catchTag("SecretStoreError", (error) =>
112 isPlatformError(error.cause) && error.cause.reason._tag === "AlreadyExists"
113 ? get(name).pipe(
114 Effect.flatMap((created) =>
115 created !== null
116 ? Effect.succeed(created)
117 : Effect.fail(
118 new SecretStoreError({
119 message: `Failed to read secret ${name} after concurrent creation.`,
120 }),
121 ),
122 ),
123 )
124 : Effect.fail(error),
125 ),
126 );
127 }),
128 );
129
130 const remove: ServerSecretStoreShape["remove"] = (name) =>
131 fileSystem.remove(resolveSecretPath(name)).pipe(
132 Effect.catch((cause) =>
133 cause.reason._tag === "NotFound"
134 ? Effect.void
135 : Effect.fail(
136 new SecretStoreError({
137 message: `Failed to remove secret ${name}.`,
138 cause,
139 }),
140 ),
141 ),
142 );
143
144 return {
145 get,
146 set,
147 getOrCreateRandom,
148 remove,
149 } satisfies ServerSecretStoreShape;
150});
151
152export const ServerSecretStoreLive = Layer.effect(ServerSecretStore, makeServerSecretStore);