Skip to content

Commit

Permalink
refactor(docker): better url parsing (#10996)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Jul 28, 2021
1 parent 6d31560 commit 3d4d252
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/datasource/docker/common.ts
Expand Up @@ -196,7 +196,10 @@ export function getRegistryRepository(
}
let dockerRepository = split.join('/');
if (!registryHost) {
registryHost = registryUrl;
registryHost = registryUrl.replace(
'https://docker.io',
'https://index.docker.io'
);
}
if (registryHost === 'docker.io') {
registryHost = 'index.docker.io';
Expand Down
1 change: 1 addition & 0 deletions lib/datasource/docker/index.spec.ts
Expand Up @@ -391,6 +391,7 @@ describe(getName(), () => {
const res = await getPkgReleases({
datasource: id,
depName: 'node',
registryUrls: ['https://docker.io'],
});
expect(res).toBeNull();
expect(httpMock.getTrace()).toMatchSnapshot();
Expand Down
4 changes: 3 additions & 1 deletion lib/datasource/docker/index.ts
Expand Up @@ -3,6 +3,7 @@ import parseLinkHeader from 'parse-link-header';
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import * as packageCache from '../../util/cache/package';
import { ensurePathPrefix } from '../../util/url';
import {
api as dockerVersioning,
id as dockerVersioningId,
Expand Down Expand Up @@ -65,7 +66,8 @@ async function getDockerApiTags(
// AWS ECR limits the maximum number of results to 1000
// See https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_DescribeRepositories.html#ECR-DescribeRepositories-request-maxResults
const limit = ecrRegex.test(registryHost) ? 1000 : 10000;
let url = `${registryHost}/v2/${dockerRepository}/tags/list?n=${limit}`;
let url = `${registryHost}/${dockerRepository}/tags/list?n=${limit}`;
url = ensurePathPrefix(url, '/v2');
const headers = await getAuthHeaders(registryHost, dockerRepository);
if (!headers) {
logger.debug('Failed to get authHeaders for getTags lookup');
Expand Down
13 changes: 13 additions & 0 deletions lib/util/url.spec.ts
@@ -1,5 +1,6 @@
import { getName } from '../../test/util';
import {
ensurePathPrefix,
parseUrl,
resolveBaseUrl,
trimTrailingSlash,
Expand Down Expand Up @@ -77,4 +78,16 @@ describe(getName(), () => {
expect(trimTrailingSlash('foo/')).toBe('foo');
expect(trimTrailingSlash('foo//////')).toBe('foo');
});

it('ensures path prefix', () => {
expect(ensurePathPrefix('https://index.docker.io', '/v2')).toBe(
'https://index.docker.io/v2'
);
expect(ensurePathPrefix('https://index.docker.io/v2', '/v2')).toBe(
'https://index.docker.io/v2'
);
expect(
ensurePathPrefix('https://index.docker.io/v2/something', '/v2')
).toBe('https://index.docker.io/v2/something');
});
});
9 changes: 9 additions & 0 deletions lib/util/url.ts
@@ -1,5 +1,14 @@
import urlJoin from 'url-join';

export function ensurePathPrefix(url: string, prefix: string): string {
const parsed = new URL(url);
const fullPath = url.replace(parsed.origin, '');
if (fullPath.startsWith(prefix)) {
return url;
}
return parsed.origin + prefix + fullPath;
}

export function ensureTrailingSlash(url: string): string {
return url.replace(/\/?$/, '/');
}
Expand Down

0 comments on commit 3d4d252

Please sign in to comment.