| @@ -1,6 +1,7 @@ |
| 1 | import { Effect, Schema } from "effect"; | 1 | import { randomUUID } from "node:crypto"; |
| 2 | import * as fs from "node:fs"; | 2 | import * as fs from "node:fs"; |
| 3 | import * as path from "node:path"; | 3 | import * as path from "node:path"; |
| | 4 | import { Deferred, Effect, Exit, Schema } from "effect"; |
| 4 | | 5 | |
| 5 | import { | 6 | import { |
| 6 | definePlugin, | 7 | definePlugin, |
| @@ -71,13 +72,29 @@ const resolveAuthLocation = (config: FileSecretsPluginConfig | undefined): AuthL · 64 unmodified lines |
| 71 | const FlatAuthFile = Schema.Record(Schema.String, Schema.String); | 72 | const FlatAuthFile = Schema.Record(Schema.String, Schema.String); |
| 72 | const decodeFlatAuthFile = Schema.decodeUnknownEffect(Schema.fromJsonString(FlatAuthFile)); | 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 | // File I/O with restricted permissions | 92 | // File I/O with restricted permissions |
| 76 | // | 93 | // |
| 77 | // These helpers keep real I/O and decode failures in the Effect error | 94 | // These helpers keep real I/O and decode failures distinct internally, then |
| 78 | // channel as `StorageError`. Missing files are still treated as an empty | 95 | // project both to `StorageError` at the provider boundary. Missing files are |
| 79 | // auth file, but malformed JSON, schema decode failures, and permission | 96 | // still treated as an empty auth file; malformed content and permission errors |
| 80 | // errors no longer collapse into "empty file". | 97 | // do not collapse into "empty file" during ordinary provider reads. |
| 81 | // --------------------------------------------------------------------------- | 98 | // --------------------------------------------------------------------------- |
| 82 | | 99 | |
| 83 | const isFileNotFoundCause = (cause: unknown): cause is NodeJS.ErrnoException => | 100 | const isFileNotFoundCause = (cause: unknown): cause is NodeJS.ErrnoException => |
| @@ -88,7 +105,9 @@ const toStorageError = · 4 unmodified lines |
| 88 | (cause: unknown): StorageError => | 105 | (cause: unknown): StorageError => |
| 89 | new StorageError({ message, cause }); | 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 | if (!fs.existsSync(filePath)) return Effect.succeed({}); | 111 | if (!fs.existsSync(filePath)) return Effect.succeed({}); |
| 93 | return Effect.try({ | 112 | return Effect.try({ |
| 94 | try: () => fs.readFileSync(filePath, "utf-8"), | 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 | raw === "" | 121 | raw === "" |
| 103 | ? Effect.succeed<Record<string, string>>({}) | 122 | ? Effect.succeed<Record<string, string>>({}) |
| 104 | : decodeFlatAuthFile(raw).pipe( | 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 | const writeAll = ( | 137 | const writeAll = ( |
| 112 | filePath: string, | 138 | filePath: string, |
| 113 | secrets: Record<string, string>, | 139 | secrets: Record<string, string>, |
| 114 | ): Effect.Effect<void, StorageError> => { | 140 | ): Effect.Effect<void, StorageError> => { |
| 115 | const dir = path.dirname(filePath); | 141 | const dir = path.dirname(filePath); |
| 116 | const tmp = `${filePath}.tmp`; | 142 | const tmp = `${filePath}.${process.pid}.${randomUUID()}.tmp`; |
| 117 | return Effect.gen(function* () { | 143 | return Effect.gen(function* () { |
| 118 | if (!fs.existsSync(dir)) { | 144 | if (!fs.existsSync(dir)) { |
| 119 | yield* Effect.try({ | 145 | yield* Effect.try({ |
| @@ -124,6 +150,8 @@ const writeAll = ( · 4 unmodified lines |
| 124 | yield* Effect.try({ | 150 | yield* Effect.try({ |
| 125 | try: () => { | 151 | try: () => { |
| 126 | fs.writeFileSync(tmp, JSON.stringify(secrets, null, 2), { mode: 0o600 }); | 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 | fs.chmodSync(tmp, 0o600); | 155 | fs.chmodSync(tmp, 0o600); |
| 128 | }, | 156 | }, |
| 129 | catch: toStorageError("Failed to write temporary auth file"), | 157 | catch: toStorageError("Failed to write temporary auth file"), |
| @@ -143,13 +171,11 @@ const migrateLegacyAuthFile = ({ · 13 unmodified lines |
| 143 | return Effect.void; | 171 | return Effect.void; |
| 144 | } | 172 | } |
| 145 | | 173 | |
| 146 | return readAll(legacyFilePath).pipe( | 174 | return readAuthFile(legacyFilePath).pipe( |
| 147 | Effect.matchEffect({ | 175 | Effect.flatMap((secrets) => writeAll(filePath, secrets)), |
| 148 | // A legacy file that cannot be read or decoded must not block startup. | 176 | // Malformed legacy contents are deliberately skipped. Genuine read I/O |
| 149 | // The active data-dir store remains empty and all later I/O uses it only. | 177 | // failures stay visible and leave migration eligible for a later retry. |
| 150 | onFailure: () => Effect.void, | 178 | Effect.catchTag("AuthFileDecodeError", () => Effect.void), |
| 151 | onSuccess: (secrets) => writeAll(filePath, secrets), | | |
| 152 | }), | | |
| 153 | ); | 179 | ); |
| 154 | }; | 180 | }; |
| 155 | | 181 | |
| @@ -184,12 +210,21 @@ const FILE_PROVIDER_KEY = ProviderKey.make("file"); · 28 unmodified lines |
| 184 | | 210 | |
| 185 | const makeFileProvider = (location: AuthLocation): CredentialProvider => { | 211 | const makeFileProvider = (location: AuthLocation): CredentialProvider => { |
| 186 | let migrationComplete = false; | 212 | let migrationComplete = false; |
| | 213 | let migrationInFlight: Deferred.Deferred<void, StorageError> | null = null; |
| 187 | const ensureMigration = Effect.suspend(() => { | 214 | const ensureMigration = Effect.suspend(() => { |
| 188 | if (migrationComplete) return Effect.void; | 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 | return migrateLegacyAuthFile(location).pipe( | 222 | return migrateLegacyAuthFile(location).pipe( |
| 190 | Effect.tap(() => | 223 | Effect.onExit((exit) => |
| 191 | Effect.sync(() => { | 224 | Effect.gen(function* () { |
| 192 | migrationComplete = true; | 225 | if (Exit.isSuccess(exit)) migrationComplete = true; |
| | 226 | yield* Deferred.done(latch, exit); |
| | 227 | migrationInFlight = null; |
| 193 | }), | 228 | }), |
| 194 | ), | 229 | ), |
| 195 | ); | 230 | ); |