Skip to content

Commit d3ceca4

Browse files
committed
fix(service-datasource): an unset env credential is a note after the driver's error, and a failed connect holds no claim on its variable
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FEkQ5m1ywz2SChxvNGPb5U
1 parent 57f8ca2 commit d3ceca4

3 files changed

Lines changed: 75 additions & 8 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
'@objectstack/service-datasource': patch
3+
---
4+
5+
fix(service-datasource): an unset env credential is a note after the driver's error, and a failed connect holds no claim on its variable (#28283)
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
Two corrections to the code-defined datasource's env-credential slot (`OS_DATASOURCE_<NAME>_PASSWORD`, #27687):
11+
12+
- **The driver's error explains the failure.** When a code datasource's connect failed with the variable unset, the reason read `<driver error> — datasource 'x' is defined in code and reads its credential from OS_DATASOURCE_X_PASSWORD at connect, which is not set on this host` for every failure, so an unreachable host, a missing driver module or a TLS failure read as a missing password. The sentence is now a trailing note: `<driver error> (note: datasource 'x' is defined in code and reads its credential from OS_DATASOURCE_X_PASSWORD at connect, which is not set on this host)`. A login refusal still names the variable to set. The reason is operator-facing text (the connect verdict, Setup → Datasources, the fail-fast boot message); no code or status changes.
13+
- **A failed connect releases its variable.** The guard that refuses two datasources deriving one variable recorded the owner before the build and released it only on `disconnect`, so a connect that failed kept it and a later datasource deriving the same variable was refused as a collision against one that never connected. The connect that claimed the variable now releases it when it fails. Two datasources that are both live are still refused.

‎packages/services/service-datasource/src/__tests__/datasource-env-credential.test.ts‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
* never read it — and a caller-set `origin: 'code'` is not provenance;
1414
* - a declared `credentialsRef` / `external.connector` wins, and a failing
1515
* one stays fail-closed rather than falling back;
16-
* - an unset variable keeps the verdict, and its reason names the variable;
17-
* - two names deriving one variable are refused;
16+
* - an unset variable keeps the verdict, and its reason names the variable —
17+
* as a trailing note after the driver's own error (#28283);
18+
* - two names deriving one variable are refused, and a failed connect holds
19+
* no claim on its variable (#28283);
1820
* - the value reaches no log line and no answer.
1921
* The kernel-level wiring (the admin plugin's switch, the provenance set the
2022
* runtime fills) is pinned in `@objectstack/runtime`'s
@@ -54,9 +56,10 @@ function engine(): ConnectionEngineLike {
5456
/**
5557
* A factory that records the `secret` each build received. `connectFails`
5658
* makes the connect throw the way a database refusing an unauthenticated
57-
* login does.
59+
* login does; `connectError` makes the connect of the named datasources
60+
* throw that error instead.
5861
*/
59-
function recordingFactory(opts: { connectFails?: boolean } = {}) {
62+
function recordingFactory(opts: { connectFails?: boolean; connectError?: { names: string[]; error: () => Error } } = {}) {
6063
const seen: Array<{ name?: string; secret?: string }> = [];
6164
const factory: IDatasourceDriverFactory = {
6265
supports: () => true,
@@ -66,6 +69,7 @@ function recordingFactory(opts: { connectFails?: boolean } = {}) {
6669
return {
6770
driver,
6871
connect: async () => {
72+
if (opts.connectError?.names.includes(spec.name)) throw opts.connectError.error();
6973
if (opts.connectFails) throw new Error('password authentication failed for user "svc"');
7074
},
7175
};
@@ -88,12 +92,13 @@ function service(opts: {
8892
env?: Record<string, string | undefined>;
8993
slot?: boolean;
9094
connectFails?: boolean;
95+
connectError?: { names: string[]; error: () => Error };
9196
secrets?: { resolve?: (ref: string) => Promise<string | undefined> };
9297
} = {}) {
9398
const codeNames = new Set(opts.codeNames ?? ['warehouse']);
9499
const env = opts.env ?? { [datasourceEnvCredentialVar('warehouse')]: VALUE };
95100
const read = vi.fn((variable: string) => env[variable]);
96-
const { factory, seen } = recordingFactory({ connectFails: opts.connectFails });
101+
const { factory, seen } = recordingFactory({ connectFails: opts.connectFails, connectError: opts.connectError });
97102
const { logger, lines } = capturingLogger();
98103
const svc = new DatasourceConnectionService({
99104
factory: () => factory,
@@ -239,6 +244,22 @@ describe('an unset variable keeps the verdict, and the reason names the variable
239244
expect(result.reason).toContain('OS_DATASOURCE_WAREHOUSE_PASSWORD');
240245
});
241246

247+
it("a failure that is not a login refusal leads with the driver's error; the variable is only a trailing note", async () => {
248+
// [#28283] An unreachable host, a missing driver module or a TLS failure
249+
// is not cured by a password: the variable rides along as a note, never as
250+
// the explanation of the failure.
251+
const unreachable = 'connect ECONNREFUSED 127.0.0.1:1';
252+
const { svc } = service({
253+
env: {},
254+
connectError: { names: ['warehouse'], error: () => Object.assign(new Error(unreachable), { code: 'ECONNREFUSED' }) },
255+
});
256+
const result = await svc.connect(warehouse, BOOT).catch((err: Error) => ({ status: 'threw', reason: err.message }));
257+
expect(result.status).toBe('failed-degraded');
258+
expect(result.reason!.startsWith(unreachable)).toBe(true);
259+
expect(result.reason!.slice(unreachable.length)).toMatch(/^ \(note: [^)]*OS_DATASOURCE_WAREHOUSE_PASSWORD[^)]*\)$/);
260+
expect(svc.getConnectionState('warehouse')?.reason).toBe(result.reason);
261+
});
262+
242263
it('a failure on a connect the slot did not apply to keeps its reason unchanged', async () => {
243264
const { svc } = service({ env: {}, connectFails: true });
244265
const result = await svc.connect(warehouse, { context: { origin: 'runtime', trigger: 'runtime-admin' } });
@@ -264,6 +285,23 @@ describe('two datasources that derive one variable are refused', () => {
264285
expect(seen.map((s) => s.name)).toEqual(['eu-sales']);
265286
});
266287

288+
it('a connect that failed holds no claim: a later name deriving the same variable connects', async () => {
289+
// [#28283] The owner is the datasource the variable was read for and
290+
// connected; a failed connect leaves no live pool to collide with.
291+
const { svc, seen } = service({
292+
codeNames: ['eu-sales', 'eu_sales'],
293+
env: { OS_DATASOURCE_EU_SALES_PASSWORD: VALUE },
294+
connectError: { names: ['eu-sales'], error: () => new Error('getaddrinfo ENOTFOUND db.internal') },
295+
});
296+
const first = await svc.connect({ ...warehouse, name: 'eu-sales' }, BOOT);
297+
expect(first.status).toBe('failed-degraded');
298+
const second = await svc
299+
.connect({ ...warehouse, name: 'eu_sales' }, BOOT)
300+
.catch((err: Error) => ({ status: 'threw', reason: err.message }));
301+
expect(second.status).toBe('connected');
302+
expect(seen.map((s) => s.name)).toEqual(['eu-sales', 'eu_sales']);
303+
});
304+
267305
it('a reconnect of the same name is not a collision, and a disconnect releases the variable', async () => {
268306
const { svc, seen } = service({ codeNames: ['eu-sales', 'eu_sales'], env: { OS_DATASOURCE_EU_SALES_PASSWORD: VALUE } });
269307
await svc.connect({ ...warehouse, name: 'eu-sales' }, BOOT);

‎packages/services/service-datasource/src/datasource-connection-service.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,14 @@ export class DatasourceConnectionService {
761761
// failure above never falls through to here. `envCredentialVar` stays set
762762
// when the slot applied but the variable was empty, so a failed connect
763763
// can name the variable to set.
764+
//
765+
// [#28283] The owner is claimed here, before the build, so the check and
766+
// the claim are one step with no await between them; `claimedVariable`
767+
// records that THIS attempt made the claim, and the failure path below
768+
// releases it. A connect that failed holds no pool, so it must not refuse
769+
// a later name as a collision against it.
764770
let envCredentialVar: string | undefined;
771+
let claimedVariable: string | undefined;
765772
if (!credentialsRef && connector === undefined && !opts.asDefault) {
766773
const variable = this.envCredentialVarFor(name, opts.context);
767774
if (variable !== undefined) {
@@ -777,7 +784,10 @@ export class DatasourceConnectionService {
777784
opts.mappedObjects,
778785
);
779786
}
780-
this.envCredentialOwners.set(variable, name);
787+
if (owner === undefined) {
788+
this.envCredentialOwners.set(variable, name);
789+
claimedVariable = variable;
790+
}
781791
const value = this.cfg.envCredentials?.read(variable);
782792
if (value) secret = value;
783793
else envCredentialVar = variable;
@@ -898,14 +908,20 @@ export class DatasourceConnectionService {
898908
// Before `handleFailure`, which records the unavailable mark: eviction
899909
// deliberately does not touch that map, and the mark must outlive it.
900910
if (registeredByThisAttempt) engine.unregisterDriver?.(registeredByThisAttempt);
911+
if (claimedVariable !== undefined) this.envCredentialOwners.delete(claimedVariable);
901912
// `err` itself is handed on, not just its message: the driver package's
902913
// build output being absent is reported as `err.code ===
903914
// 'ERR_MODULE_NOT_FOUND'`, and that structured signal is gone the moment
904915
// the error is stringified (#5794).
916+
//
917+
// [#28283] The unset variable is a note after the driver's own error,
918+
// never its explanation: an unreachable host, a missing driver module or
919+
// a TLS failure is not cured by a password, and the driver's error is
920+
// the one thing that says which failure this was.
905921
const reason = envCredentialVar === undefined
906922
? errMsg(err)
907-
: `${errMsg(err)} — datasource '${name}' is defined in code and reads its credential from `
908-
+ `${envCredentialVar} at connect, which is not set on this host`;
923+
: `${errMsg(err)} (note: datasource '${name}' is defined in code and reads its credential from `
924+
+ `${envCredentialVar} at connect, which is not set on this host)`;
909925
return this.handleFailure(record, 'failed-degraded', reason, opts.context, opts.objects, opts.mappedObjects, err);
910926
}
911927
}

0 commit comments

Comments
 (0)