diff --git a/packages/testcontainers/src/container-runtime/auth/credential-provider.test.ts b/packages/testcontainers/src/container-runtime/auth/credential-provider.test.ts index ce8625ac7..cde52d230 100644 --- a/packages/testcontainers/src/container-runtime/auth/credential-provider.test.ts +++ b/packages/testcontainers/src/container-runtime/auth/credential-provider.test.ts @@ -56,6 +56,23 @@ describe("CredentialProvider", () => { ).toBeUndefined(); }); + it("should default to the registry url when the server url is not returned", async () => { + mockExecReturns(JSON.stringify({ "https://registry.example.com": "username" })); + mockSpawnReturns( + 0, + JSON.stringify({ + Username: "username", + Secret: "secret", + }) + ); + + expect(await credentialProvider.getAuthConfig("https://registry.example.com", containerRuntimeConfig)).toEqual({ + registryAddress: "https://registry.example.com", + username: "username", + password: "secret", + }); + }); + it("should return undefined when no auth config found for registry", async () => { mockExecReturns(JSON.stringify({ registry2: "username" })); diff --git a/packages/testcontainers/src/container-runtime/auth/credential-provider.ts b/packages/testcontainers/src/container-runtime/auth/credential-provider.ts index d50011da8..7a1a13df8 100644 --- a/packages/testcontainers/src/container-runtime/auth/credential-provider.ts +++ b/packages/testcontainers/src/container-runtime/auth/credential-provider.ts @@ -24,7 +24,9 @@ export abstract class CredentialProvider implements RegistryAuthLocator { log.debug(`Executing Docker credential provider "${programName}"`); const credentials = await this.listCredentials(programName); - if (!Object.keys(credentials).some((aRegistry) => registryMatches(aRegistry, registry))) { + + const credentialForRegistry = Object.keys(credentials).find((aRegistry) => registryMatches(aRegistry, registry)); + if (!credentialForRegistry) { log.debug(`No credential found for registry "${registry}"`); return undefined; } @@ -34,7 +36,7 @@ export abstract class CredentialProvider implements RegistryAuthLocator { return { username: response.Username, password: response.Secret, - registryAddress: response.ServerURL, + registryAddress: response.ServerURL ?? credentialForRegistry, }; }