| @@ -1,6 +1,7 @@ |
| 1 | | −import { Effect, Schema } from "effect"; |
| 1 | +import { randomUUID } from "node:crypto"; |
| 2 | 2 | import * as fs from "node:fs"; |
| 3 | 3 | import * as path from "node:path"; |
| 4 | +import { Deferred, Effect, Exit, Schema } from "effect"; |
| 4 | 5 | |
| 5 | 6 | import { |
| 6 | 7 | definePlugin, |
| @@ -71,13 +72,29 @@ const resolveAuthLocation = (config: FileSecretsPluginConfig | undefined): AuthL · 64 unmodified lines |
| 71 | 72 | const FlatAuthFile = Schema.Record(Schema.String, Schema.String); |
| 72 | 73 | const decodeFlatAuthFile = Schema.decodeUnknownEffect(Schema.fromJsonString(FlatAuthFile)); |
| 73 | 74 | |
| 75 | +class AuthFileDecodeError extends Schema.TaggedErrorClass<AuthFileDecodeError>()( |
| 76 | + "AuthFileDecodeError", |
| 77 | + { |
| 78 | + filePath: Schema.String, |
| 79 | + cause: Schema.Defect, |
| 80 | + }, |
| 81 | + { |
| 82 | + description: |
| 83 | + "The auth file was readable but its contents were not a valid flat credential map. The caller must surface the failure or explicitly treat malformed legacy data as non-migratable.", |
| 84 | + }, |
| 85 | +) { |
| 86 | + override get message(): string { |
| 87 | + return `Failed to parse auth file: ${this.filePath}`; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 74 | 91 | // --------------------------------------------------------------------------- |
| 75 | 92 | // File I/O with restricted permissions |
| 76 | 93 | // |
| 77 | | −// These helpers keep real I/O and decode failures in the Effect error |
| 78 | | −// channel as `StorageError`. Missing files are still treated as an empty |
| 79 | | −// auth file, but malformed JSON, schema decode failures, and permission |
| 80 | | −// errors no longer collapse into "empty file". |
| 94 | +// These helpers keep real I/O and decode failures distinct internally, then |
| 95 | +// project both to `StorageError` at the provider boundary. Missing files are |
| 96 | +// still treated as an empty auth file; malformed content and permission errors |
| 97 | +// do not collapse into "empty file" during ordinary provider reads. |
| 81 | 98 | // --------------------------------------------------------------------------- |
| 82 | 99 | |
| 83 | 100 | const isFileNotFoundCause = (cause: unknown): cause is NodeJS.ErrnoException => |
| @@ -88,7 +105,9 @@ const toStorageError = · 4 unmodified lines |
| 88 | 105 | (cause: unknown): StorageError => |
| 89 | 106 | new StorageError({ message, cause }); |
| 90 | 107 | |
| 91 | | −const readAll = (filePath: string): Effect.Effect<Record<string, string>, StorageError> => { |
| 108 | +const readAuthFile = ( |
| 109 | + filePath: string, |
| 110 | +): Effect.Effect<Record<string, string>, StorageError | AuthFileDecodeError> => { |
| 92 | 111 | if (!fs.existsSync(filePath)) return Effect.succeed({}); |
| 93 | 112 | return Effect.try({ |
| 94 | 113 | try: () => fs.readFileSync(filePath, "utf-8"), |
| @@ -102,18 +121,25 @@ const readAll = (filePath: string): Effect.Effect<Record<string, string>, Storag · 7 unmodified lines |
| 102 | 121 | raw === "" |
| 103 | 122 | ? Effect.succeed<Record<string, string>>({}) |
| 104 | 123 | : decodeFlatAuthFile(raw).pipe( |
| 105 | | − Effect.mapError(toStorageError("Failed to parse auth file")), |
| 124 | + Effect.mapError((cause) => new AuthFileDecodeError({ filePath, cause })), |
| 106 | 125 | ), |
| 107 | 126 | ), |
| 108 | 127 | ); |
| 109 | 128 | }; |
| 110 | 129 | |
| 130 | +const readAll = (filePath: string): Effect.Effect<Record<string, string>, StorageError> => |
| 131 | + readAuthFile(filePath).pipe( |
| 132 | + Effect.catchTag("AuthFileDecodeError", (cause) => |
| 133 | + Effect.fail(new StorageError({ message: cause.message, cause })), |
| 134 | + ), |
| 135 | + ); |
| 136 | + |
| 111 | 137 | const writeAll = ( |
| 112 | 138 | filePath: string, |
| 113 | 139 | secrets: Record<string, string>, |
| 114 | 140 | ): Effect.Effect<void, StorageError> => { |
| 115 | 141 | const dir = path.dirname(filePath); |
| 116 | | − const tmp = `${filePath}.tmp`; |
| 142 | + const tmp = `${filePath}.${process.pid}.${randomUUID()}.tmp`; |
| 117 | 143 | return Effect.gen(function* () { |
| 118 | 144 | if (!fs.existsSync(dir)) { |
| 119 | 145 | yield* Effect.try({ |
| @@ -124,6 +150,8 @@ const writeAll = ( · 4 unmodified lines |
| 124 | 150 | yield* Effect.try({ |
| 125 | 151 | try: () => { |
| 126 | 152 | fs.writeFileSync(tmp, JSON.stringify(secrets, null, 2), { mode: 0o600 }); |
| 153 | + // `mode` only applies when the file is created; chmod afterwards covers |
| 154 | + // the case where a temporary file with broader permissions already exists. |
| 127 | 155 | fs.chmodSync(tmp, 0o600); |
| 128 | 156 | }, |
| 129 | 157 | catch: toStorageError("Failed to write temporary auth file"), |
| @@ -143,13 +171,11 @@ const migrateLegacyAuthFile = ({ · 13 unmodified lines |
| 143 | 171 | return Effect.void; |
| 144 | 172 | } |
| 145 | 173 | |
| 146 | | − return readAll(legacyFilePath).pipe( |
| 147 | | − Effect.matchEffect({ |
| 148 | | − // A legacy file that cannot be read or decoded must not block startup. |
| 149 | | − // The active data-dir store remains empty and all later I/O uses it only. |
| 150 | | − onFailure: () => Effect.void, |
| 151 | | − onSuccess: (secrets) => writeAll(filePath, secrets), |
| 152 | | − }), |
| 174 | + return readAuthFile(legacyFilePath).pipe( |
| 175 | + Effect.flatMap((secrets) => writeAll(filePath, secrets)), |
| 176 | + // Malformed legacy contents are deliberately skipped. Genuine read I/O |
| 177 | + // failures stay visible and leave migration eligible for a later retry. |
| 178 | + Effect.catchTag("AuthFileDecodeError", () => Effect.void), |
| 153 | 179 | ); |
| 154 | 180 | }; |
| 155 | 181 | |
| @@ -184,12 +210,21 @@ const FILE_PROVIDER_KEY = ProviderKey.make("file"); · 28 unmodified lines |
| 184 | 210 | |
| 185 | 211 | const makeFileProvider = (location: AuthLocation): CredentialProvider => { |
| 186 | 212 | let migrationComplete = false; |
| 213 | + let migrationInFlight: Deferred.Deferred<void, StorageError> | null = null; |
| 187 | 214 | const ensureMigration = Effect.suspend(() => { |
| 188 | 215 | if (migrationComplete) return Effect.void; |
| 216 | + if (migrationInFlight !== null) return Deferred.await(migrationInFlight); |
| 217 | + |
| 218 | + // Installing the latch inside Effect.suspend is synchronous, so a peer |
| 219 | + // fiber can only observe either no attempt or this fully initialized one. |
| 220 | + const latch = Deferred.makeUnsafe<void, StorageError>(); |
| 221 | + migrationInFlight = latch; |
| 189 | 222 | return migrateLegacyAuthFile(location).pipe( |
| 190 | | − Effect.tap(() => |
| 191 | | − Effect.sync(() => { |
| 192 | | − migrationComplete = true; |
| 223 | + Effect.onExit((exit) => |
| 224 | + Effect.gen(function* () { |
| 225 | + if (Exit.isSuccess(exit)) migrationComplete = true; |
| 226 | + yield* Deferred.done(latch, exit); |
| 227 | + migrationInFlight = null; |
| 193 | 228 | }), |
| 194 | 229 | ), |
| 195 | 230 | ); |