Skip to content

Commit

Permalink
chore: fix invalid URL when trying a registry using basic auth
Browse files Browse the repository at this point in the history
fixes podman-desktop#6344
Signed-off-by: Florent Benoit <[email protected]>
  • Loading branch information
benoitf committed Mar 11, 2024
1 parent bb64408 commit 9d0cd33
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/main/src/plugin/image-registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,36 @@ test('should map ecr', () => {
expect(registryServer).toBe('12345.dkr.ecr.us-east-1.amazonaws.com');
});

describe('extract auth info', () => {
beforeEach(() => {
vi.resetAllMocks();
});

test('getAuthInfo with basic auth', async () => {
// reply a 401 Unauthorized error with a custom header having the www-authenticate field
nock('https://my-podman-desktop-fake-registry.io').get('/v2/').reply(401, '', {
'Www-Authenticate': 'Basic realm="Registry Realm"',
});

const value = await imageRegistry.getAuthInfo('https://my-podman-desktop-fake-registry.io');
expect(value).toBeDefined();
expect(value?.authUrl).toBe('https://my-podman-desktop-fake-registry.io/v2/');
expect(value?.scheme).toBe('basic');
});

test('getAuthInfo from docker.io with bearer', async () => {
// reply a 401 Unauthorized error with a custom header having the www-authenticate field
nock('https://my-podman-desktop-fake-registry.io').get('/v2/').reply(401, '', {
'www-authenticate': 'Bearer realm="https://auth.docker.io/token",service="registry.docker.io"',
});

const value = await imageRegistry.getAuthInfo('https://my-podman-desktop-fake-registry.io');
expect(value).toBeDefined();
expect(value?.authUrl).toBe('https://auth.docker.io/token?service=registry.docker.io');
expect(value?.scheme).toBe('bearer');
});
});

describe('extractImageDataFromImageName', () => {
test('library image', () => {
const nameAndTag = imageRegistry.extractImageDataFromImageName('httpd');
Expand Down
2 changes: 1 addition & 1 deletion packages/main/src/plugin/image-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,12 +735,12 @@ export class ImageRegistry {
if (wwwAuthenticate) {
const authInfo = this.extractAuthData(wwwAuthenticate);
if (authInfo) {
const url = new URL(authInfo.authUrl);
scheme = authInfo.scheme?.toLowerCase();
// in case of basic auth, we use directly the registry URL
if (scheme === 'basic') {
return { authUrl: registryUrl, scheme };
}
const url = new URL(authInfo.authUrl);
if (authInfo.service) {
url.searchParams.set('service', authInfo.service);
}
Expand Down

0 comments on commit 9d0cd33

Please sign in to comment.