use the registry host for the scoped buildx config dir

scopeToConfigDir joined the registry as given onto the Buildx config
dir. For a registry given as a URL this produced
config/https:/<host>/<scope>, and Buildx parses the first path element
as the registry host, so it never finds these credentials.

ECR always hits this: loginECR logs in to the proxy endpoint returned by
GetAuthorizationToken, which is a URL (https://<id>.dkr.ecr.<region>.amazonaws.com),
so a scoped ECR login wrote its credentials to a directory Buildx does
not read, and logout looked in a different directory than login wrote to.

Drop the URL scheme and trailing slashes before building the path.

Assisted-By: Claude
Signed-off-by: breken-ai <312387581+breken-ai@users.noreply.github.com>
This commit is contained in:
breken-ai 2026-09-25 18:50:52 -07:00
parent 783ae9cac5
commit 53ae646628
5 changed files with 54 additions and 7 deletions

View file

@ -3,7 +3,7 @@ import * as path from 'path';
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx.js';
import {getAuthList, getInputs} from '../src/context.js';
import {getAuthList, getInputs, scopeToConfigDir} from '../src/context.js';
afterEach(() => {
vi.restoreAllMocks();
@ -168,3 +168,15 @@ test('getAuthList masks registry-auth password when present', async () => {
expect(stdoutWriteSpy.mock.calls.map(call => call[0]).join('')).toContain('::add-mask::groundcontrol');
});
// prettier-ignore
test.each([
['ghcr.io', 'ghcr.io'],
['https://ghcr.io', 'ghcr.io'],
['https://ghcr.io/', 'ghcr.io'],
['http://localhost:5000', 'localhost:5000'],
['https://012345678910.dkr.ecr.eu-west-3.amazonaws.com', '012345678910.dkr.ecr.eu-west-3.amazonaws.com'],
['https://docker.io', 'registry-1.docker.io']
])('scopeToConfigDir uses the registry host for %s', async (registry, host) => {
expect(scopeToConfigDir(registry, 'myorg/myimage@push')).toBe(path.join(Buildx.configDir, 'config', host, 'myorg', 'myimage') + '@push');
});

View file

@ -1,8 +1,12 @@
import {afterEach, expect, test, vi} from 'vitest';
import * as path from 'path';
import {Buildx} from '@docker/actions-toolkit/lib/buildx/buildx.js';
import {Docker} from '@docker/actions-toolkit/lib/docker/docker.js';
import {login, loginStandard, logout} from '../src/docker.js';
import * as aws from '../src/aws.js';
import {getAuthList} from '../src/context.js';
import {login, loginECR, loginStandard, logout} from '../src/docker.js';
import * as dockerhub from '../src/dockerhub.js';
afterEach(() => {
@ -92,3 +96,31 @@ test('logout calls exec', async () => {
ignoreReturnCode: true
});
});
test('loginECR writes scoped credentials to the config dir of the registry host', async () => {
const execSpy = vi.spyOn(Docker, 'getExecOutput').mockResolvedValue({
exitCode: 0,
stdout: '',
stderr: ''
});
// ECR returns the proxy endpoint as a URL, e.g. https://012345678910.dkr.ecr.eu-west-3.amazonaws.com
vi.spyOn(aws, 'getRegistriesData').mockResolvedValue([
{
registry: 'https://012345678910.dkr.ecr.eu-west-3.amazonaws.com',
username: 'AWS',
password: 'world'
}
]);
const registry = '012345678910.dkr.ecr.eu-west-3.amazonaws.com';
const scope = 'myapp@push';
await loginECR(registry, 'CAFEBABE', 'DEADBEEF', scope);
const expectedConfigDir = path.join(Buildx.configDir, 'config', registry, 'myapp') + '@push';
expect(execSpy).toHaveBeenCalledTimes(1);
expect(execSpy.mock.calls[0][1]?.env?.DOCKER_CONFIG).toBe(expectedConfigDir);
// logout in the post step uses the config dir computed from the inputs
const [auth] = getAuthList({registry, username: 'CAFEBABE', password: 'DEADBEEF', scope, ecr: 'auto', logout: true, registryAuth: ''});
expect(auth.configDir).toBe(expectedConfigDir);
});

2
dist/index.cjs generated vendored

File diff suppressed because one or more lines are too long

6
dist/index.cjs.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -77,8 +77,11 @@ export function scopeToConfigDir(registry: string, scope?: string): string {
if (scopeDisabled() || !scope || scope === '') {
return '';
}
// Buildx looks up scoped credentials by registry host, so drop any URL
// scheme (e.g. the https:// in AWS ECR proxy endpoints) and trailing slash.
const registryHost = registry.replace(/^[a-z][a-z0-9+.-]*:\/\//i, '').replace(/\/+$/, '');
const configRoot = path.resolve(Buildx.configDir, 'config');
const registryDir = path.resolve(configRoot, registry === 'docker.io' ? 'registry-1.docker.io' : registry);
const registryDir = path.resolve(configRoot, registryHost === 'docker.io' ? 'registry-1.docker.io' : registryHost);
if (!isChildPath(configRoot, registryDir)) {
throw new Error(`Invalid registry '${registry}': resolved config path escapes the Buildx config directory`);
}