Skip to content

Commit 3830959

Browse files
Fix fetching registry credentials when using Gcloud cred helper (#764)
1 parent c7df5ff commit 3830959

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

packages/testcontainers/src/container-runtime/auth/credential-provider.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,23 @@ describe("CredentialProvider", () => {
5656
).toBeUndefined();
5757
});
5858

59+
it("should default to the registry url when the server url is not returned", async () => {
60+
mockExecReturns(JSON.stringify({ "https://registry.example.com": "username" }));
61+
mockSpawnReturns(
62+
0,
63+
JSON.stringify({
64+
Username: "username",
65+
Secret: "secret",
66+
})
67+
);
68+
69+
expect(await credentialProvider.getAuthConfig("https://registry.example.com", containerRuntimeConfig)).toEqual({
70+
registryAddress: "https://registry.example.com",
71+
username: "username",
72+
password: "secret",
73+
});
74+
});
75+
5976
it("should return undefined when no auth config found for registry", async () => {
6077
mockExecReturns(JSON.stringify({ registry2: "username" }));
6178

packages/testcontainers/src/container-runtime/auth/credential-provider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export abstract class CredentialProvider implements RegistryAuthLocator {
2424
log.debug(`Executing Docker credential provider "${programName}"`);
2525

2626
const credentials = await this.listCredentials(programName);
27-
if (!Object.keys(credentials).some((aRegistry) => registryMatches(aRegistry, registry))) {
27+
28+
const credentialForRegistry = Object.keys(credentials).find((aRegistry) => registryMatches(aRegistry, registry));
29+
if (!credentialForRegistry) {
2830
log.debug(`No credential found for registry "${registry}"`);
2931
return undefined;
3032
}
@@ -34,7 +36,7 @@ export abstract class CredentialProvider implements RegistryAuthLocator {
3436
return {
3537
username: response.Username,
3638
password: response.Secret,
37-
registryAddress: response.ServerURL,
39+
registryAddress: response.ServerURL ?? credentialForRegistry,
3840
};
3941
}
4042

0 commit comments

Comments
 (0)