@@ -269,6 +269,16 @@ export interface ProbeInput {
269269 timeoutMs ?: number ;
270270}
271271
272+ /**
273+ * [#29879] What a {@link DatasourceAdminServiceConfig.reregisterPool} seam
274+ * reports about the rebuild it ran. `previousPoolKept: true` = the new
275+ * configuration failed to come up and the pool built from the previous one is
276+ * still the one serving, so the update must store none of the new one.
277+ */
278+ export interface ReregisterPoolOutcome {
279+ previousPoolKept : boolean ;
280+ }
281+
272282/**
273283 * Injected dependencies. The plugin supplies real implementations backed by the
274284 * driver registry, `IMetadataService` (runtime store), and the secret store;
@@ -370,8 +380,17 @@ export interface DatasourceAdminServiceConfig {
370380 * Optional: a host that wired only `registerPool` falls back to it, whose
371381 * idempotency guard retains the old pool — the pre-#13804 behaviour, and
372382 * the safe direction for a host that cannot express a swap.
383+ *
384+ * [#29879] Answer `{ previousPoolKept: true }` when the rebuild failed and
385+ * the previous pool is the one still serving: the update then stores
386+ * nothing of the new configuration and takes back the secret it sealed for
387+ * it. Answering nothing (a host written before this) keeps the old reading:
388+ * the new record is stored whatever the rebuild did.
373389 */
374- reregisterPool ?: ( previous : StoredDatasource , next : StoredDatasource ) => Promise < void > | void ;
390+ reregisterPool ?: (
391+ previous : StoredDatasource ,
392+ next : StoredDatasource ,
393+ ) => Promise < ReregisterPoolOutcome | void > | ReregisterPoolOutcome | void ;
375394 /**
376395 * [#13805] Converge THIS replica's live pool for `name` after a PEER wrote
377396 * the shared datasource record — the receive half of
@@ -490,11 +509,17 @@ export function datasourceNotFoundError(name: string): Error {
490509 * a script or an SDK call that clears the field is what reached it.
491510 *
492511 * `null` and an absent `secret` are unchanged: they keep the stored
493- * credential. A whitespace-only value is a value, sealed and used as sent.
494- * Only the empty string is judged, so the answer cannot echo a credential.
512+ * credential.
513+ *
514+ * [#29879] A whitespace-only value (`" "`, a tab and a newline) is refused
515+ * by this same guard: it seals nothing usable, and a rotation to it broke
516+ * the datasource at its next connect exactly as the empty string did. A
517+ * value with whitespace around other characters is a value, sealed as sent.
518+ * Only blank values are judged, so the answer cannot echo a credential.
495519 */
496520function assertSecretNotEmpty ( heading : string , secret : SecretInput | undefined ) : void {
497- if ( secret == null || ( secret as { value ?: unknown } ) . value !== '' ) return ;
521+ const value = ( secret as { value ?: unknown } | null | undefined ) ?. value ;
522+ if ( secret == null || typeof value !== 'string' || value . trim ( ) !== '' ) return ;
498523 throw new Error (
499524 `${ heading } \nThe \`secret\` is empty. Send the credential to store, or leave \`secret\` out (or null) `
500525 + 'to keep the stored one. Nothing was written.' ,
@@ -952,11 +977,16 @@ export class DatasourceAdminService implements IDatasourceAdminService {
952977 // `active: false` unreachable.
953978 if ( patch . external !== undefined ) await this . assertConnectorLendable ( merged ) ;
954979
980+ // [#29879] The new secret is sealed BEFORE the rebuild (the rebuild
981+ // dereferences the merged record's `credentialsRef`), but the working one
982+ // is retired only once the merged record is stored, below: a rebuild that
983+ // keeps the previous pool stores nothing, and the binding it keeps must
984+ // still resolve.
985+ const prevRef = existing . external ?. credentialsRef ;
986+ let newRef : string | undefined ;
955987 if ( secret ) {
956- const prevRef = existing . external ?. credentialsRef ;
957- const credentialsRef = await this . config . writeSecret ( secret , { name } ) ;
958- merged . external = { ...( merged . external ?? { } ) , credentialsRef } ;
959- if ( prevRef && prevRef !== credentialsRef ) await this . tryRemoveSecret ( prevRef ) ;
988+ newRef = await this . config . writeSecret ( secret , { name } ) ;
989+ merged . external = { ...( merged . external ?? { } ) , credentialsRef : newRef } ;
960990 }
961991
962992 // Carry forward the credential material `getDatasource()` redacts (#8081)
@@ -995,8 +1025,6 @@ export class DatasourceAdminService implements IDatasourceAdminService {
9951025 merged . ssl = { ...merged . ssl , [ SSL_PRIVATE_KEY ] : storedKey } ;
9961026 }
9971027
998- await this . config . putDatasourceRecord ( merged ) ;
999-
10001028 // [#13804] What "Save" means for the LIVE pool, decided by what actually
10011029 // changed — never unconditionally. The old tail called `registerPool` on
10021030 // every update, and the connect path's idempotency guard turned that into
@@ -1023,12 +1051,38 @@ export class DatasourceAdminService implements IDatasourceAdminService {
10231051 // cannot see: a rewrap-in-place keeps the `credentialsRef` string while
10241052 // changing the credential it dereferences to, and the pool reads the
10251053 // credential only at build time.
1054+ //
1055+ // [#29879] The rebuild runs BEFORE the record is stored, and a rebuild
1056+ // that keeps the previous pool stores nothing: not the record, not the
1057+ // new `credentialsRef`, not any other key of this patch. The answer says
1058+ // the new configuration is NOT in force, so the stored record has to be
1059+ // the one still serving, or the next restart connects the configuration
1060+ // that just failed — with the working credential already removed. One
1061+ // write path: the record is stored once, after the pool settles, and the
1062+ // working secret is retired only after that write.
10261063 const rebuildNeeded = secret !== undefined || datasourceConnectivityChanged ( existing , merged ) ;
1064+ if ( merged . active !== false && rebuildNeeded ) {
1065+ const { previousPoolKept } = await this . tryReregisterPool ( existing , merged ) ;
1066+ if ( previousPoolKept ) {
1067+ await this . discardUnstoredSecret ( name , newRef , prevRef ) ;
1068+ // Nothing was stored, so there is nothing for a peer to converge on.
1069+ return this . toSummary ( existing ) ;
1070+ }
1071+ }
1072+
1073+ try {
1074+ await this . config . putDatasourceRecord ( merged ) ;
1075+ } catch ( err ) {
1076+ // The stored record still names the working secret; the one sealed for
1077+ // this write has no referrer, so take it back out rather than orphan it.
1078+ if ( newRef && newRef !== prevRef ) await this . tryRemoveSecret ( newRef ) ;
1079+ throw err ;
1080+ }
1081+ if ( newRef && prevRef && prevRef !== newRef ) await this . tryRemoveSecret ( prevRef ) ;
1082+
10271083 if ( merged . active === false ) {
10281084 if ( rebuildNeeded ) await this . tryUnregisterPool ( name ) ;
1029- } else if ( rebuildNeeded ) {
1030- await this . tryReregisterPool ( existing , merged ) ;
1031- } else {
1085+ } else if ( ! rebuildNeeded ) {
10321086 await this . tryRegisterPool ( merged ) ;
10331087 }
10341088 // [#13805] Unconditionally — a label-only edit included. The peer, not the
@@ -1038,6 +1092,29 @@ export class DatasourceAdminService implements IDatasourceAdminService {
10381092 return this . toSummary ( merged ) ;
10391093 }
10401094
1095+ /**
1096+ * [#29879] Take back the secret an update sealed for a configuration that
1097+ * never came into force. A binder that mints a fresh reference per write
1098+ * (the shipped `sys_secret` binder) leaves the working row untouched, so
1099+ * removing the new one restores the stored binding exactly. A binder that
1100+ * rewrites in place under the SAME reference has already overwritten the
1101+ * working credential: that row is the binding the stored record still
1102+ * names, so it is never removed, and the loss is said once, at `error`.
1103+ */
1104+ private async discardUnstoredSecret ( name : string , newRef : string | undefined , prevRef : string | undefined ) : Promise < void > {
1105+ if ( ! newRef ) return ;
1106+ if ( newRef !== prevRef ) {
1107+ await this . tryRemoveSecret ( newRef ) ;
1108+ return ;
1109+ }
1110+ this . logger ?. error ?.(
1111+ `datasource '${ name } ': the new secret did not connect and the previous pool was kept, but the `
1112+ + `secret store rewrote credential '${ newRef } ' in place, so the stored record now names the `
1113+ + 'credential that failed and the datasource will not connect after a restart. Send the working '
1114+ + 'credential again as `secret` to restore it.' ,
1115+ ) ;
1116+ }
1117+
10411118 /**
10421119 * [#6504] Remove a runtime datasource, refusing when the bound-object count
10431120 * behind the safety guard cannot be trusted.
@@ -1523,15 +1600,20 @@ export class DatasourceAdminService implements IDatasourceAdminService {
15231600 * re-registered would honour the rebuild but lose the "keep the old pool on
15241601 * failure" half of the ruling.
15251602 */
1526- private async tryReregisterPool ( previous : StoredDatasource , next : StoredDatasource ) : Promise < void > {
1603+ private async tryReregisterPool (
1604+ previous : StoredDatasource ,
1605+ next : StoredDatasource ,
1606+ ) : Promise < { previousPoolKept : boolean } > {
15271607 if ( ! this . config . reregisterPool ) {
15281608 await this . tryRegisterPool ( next ) ;
1529- return ;
1609+ return { previousPoolKept : false } ;
15301610 }
15311611 try {
1532- await this . config . reregisterPool ( previous , next ) ;
1612+ const outcome = await this . config . reregisterPool ( previous , next ) ;
1613+ return { previousPoolKept : outcome ?. previousPoolKept === true } ;
15331614 } catch ( err ) {
15341615 this . logger ?. warn ( `reregisterPool('${ next . name } ') failed` , err ) ;
1616+ return { previousPoolKept : false } ;
15351617 }
15361618 }
15371619
0 commit comments