Skip to content

Commit

Permalink
Fix use the right resource Url.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlagorce committed Mar 29, 2022
1 parent 851dba6 commit a1d294d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
69 changes: 36 additions & 33 deletions lib/modules/datasource/docker/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const http = new Http(DockerDatasource.id);

jest.mock('@aws-sdk/client-ecr');
jest.mock('../../../util/host-rules');

jest.setTimeout(500000);
type ECR = _AWS.ECR;
type GetAuthorizationTokenCommandOutput =
_AWS.GetAuthorizationTokenCommandOutput;
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('modules/datasource/docker/index', () => {
it('use a HEAD request to the real resource and use the auth header values to get a token', async () => {
httpMock
.scope('https://my.local.registry')
.head('/v2/real/resource')
.head('/v2/my/node/resource')
.reply(401, '', {
'www-authenticate':
'Bearer realm="https://my.local.registry/oauth2/token",service="my.local.registry",scope="repository:my/node:whatever"',
Expand All @@ -190,7 +190,7 @@ describe('modules/datasource/docker/index', () => {
'https://my.local.registry',
'my/node/prefix',
'head',
'/v2/real/resource'
'https://my.local.registry/v2/my/node/resource'
);

// do not inline, otherwise we get false positive from codeql
Expand Down Expand Up @@ -565,7 +565,7 @@ describe('modules/datasource/docker/index', () => {
it('returns null if no token', async () => {
httpMock
.scope(baseUrl)
.head('/tags/list')
.head('/library/node/tags/list?n=10000')
.reply(200, '', {})
.get('/library/node/tags/list?n=10000')
.reply(403);
Expand All @@ -581,7 +581,7 @@ describe('modules/datasource/docker/index', () => {
const tags = ['1.0.0'];
httpMock
.scope('https://registry.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(
Expand Down Expand Up @@ -612,7 +612,7 @@ describe('modules/datasource/docker/index', () => {
const tags = ['1.0.0'];
httpMock
.scope('https://registry.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(200, { tags }, {})
Expand Down Expand Up @@ -670,7 +670,7 @@ describe('modules/datasource/docker/index', () => {
it('uses lower tag limit for ECR deps', async () => {
httpMock
.scope(amazonUrl)
.head('/tags/list')
.head('/node/tags/list?n=1000')
.reply(200, '', {})
// The tag limit parameter `n` needs to be limited to 1000 for ECR
// See https://docs.aws.amazon.com/AmazonECR/latest/APIReference/API_DescribeRepositories.html#ECR-DescribeRepositories-request-maxResults
Expand All @@ -695,7 +695,7 @@ describe('modules/datasource/docker/index', () => {
it('resolves requests to ECR proxy', async () => {
httpMock
.scope('https://ecr-proxy.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(
Expand Down Expand Up @@ -759,7 +759,7 @@ describe('modules/datasource/docker/index', () => {

httpMock
.scope('https://ecr-proxy.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(405, maxResultsErrorBody, {
Expand All @@ -780,7 +780,7 @@ describe('modules/datasource/docker/index', () => {
it('returns null when the response code is not 405', async () => {
httpMock
.scope('https://ecr-proxy.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(
Expand Down Expand Up @@ -811,7 +811,7 @@ describe('modules/datasource/docker/index', () => {
it('returns null when no response headers are present', async () => {
httpMock
.scope('https://ecr-proxy.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(405, {
Expand All @@ -834,7 +834,7 @@ describe('modules/datasource/docker/index', () => {
it('returns null when the expected docker header is missing', async () => {
httpMock
.scope('https://ecr-proxy.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(
Expand Down Expand Up @@ -863,7 +863,7 @@ describe('modules/datasource/docker/index', () => {
it('returns null when the response body does not contain an errors object', async () => {
httpMock
.scope('https://ecr-proxy.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(
Expand All @@ -884,7 +884,7 @@ describe('modules/datasource/docker/index', () => {
it('returns null when the response body does not contain errors', async () => {
httpMock
.scope('https://ecr-proxy.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(
Expand All @@ -907,7 +907,7 @@ describe('modules/datasource/docker/index', () => {
it('returns null when the the response errors does not have a message property', async () => {
httpMock
.scope('https://ecr-proxy.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(
Expand All @@ -934,7 +934,7 @@ describe('modules/datasource/docker/index', () => {
it('returns null when the the error message does not have the expected max results error', async () => {
httpMock
.scope('https://ecr-proxy.company.com/v2')
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200, '', {})
.get('/node/tags/list?n=10000')
.reply(
Expand Down Expand Up @@ -964,7 +964,7 @@ describe('modules/datasource/docker/index', () => {
const tags = ['1.0.0'];
httpMock
.scope(baseUrl)
.head('/tags/list')
.head('/library/node/tags/list?n=10000')
.reply(401, '', {
'www-authenticate':
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/node:pull "',
Expand Down Expand Up @@ -992,7 +992,7 @@ describe('modules/datasource/docker/index', () => {
const tags = ['1.0.0'];
httpMock
.scope(baseUrl)
.head('/tags/list')
.head('/library/node/tags/list?n=10000')
.reply(401, '', {
'www-authenticate':
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:library/node:pull "',
Expand Down Expand Up @@ -1020,7 +1020,7 @@ describe('modules/datasource/docker/index', () => {
const tags = ['1.0.0'];
httpMock
.scope('https://k8s.gcr.io/v2/')
.head('/tags/list')
.head('/kubernetes-dashboard-amd64/tags/list?n=10000')
.reply(401, '', {
'www-authenticate':
'Bearer realm="https://k8s.gcr.io/v2/token",service="k8s.gcr.io"',
Expand All @@ -1045,7 +1045,7 @@ describe('modules/datasource/docker/index', () => {
it('returns null on error', async () => {
httpMock
.scope(baseUrl)
.head('/tags/list')
.head('/my/node/tags/list?n=10000')
.reply(200, null)
.get('/my/node/tags/list?n=10000')
.replyWithError('error');
Expand All @@ -1059,7 +1059,7 @@ describe('modules/datasource/docker/index', () => {
it('strips trailing slash from registry', async () => {
httpMock
.scope(baseUrl)
.head('/tags/list')
.head('/my/node/tags/list?n=10000')
.reply(401, '', {
'www-authenticate':
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",scope="repository:my/node:pull "',
Expand All @@ -1084,9 +1084,12 @@ describe('modules/datasource/docker/index', () => {

it('returns null if no auth', async () => {
hostRules.find.mockReturnValue({});
httpMock.scope(baseUrl).head('/tags/list').reply(401, undefined, {
'www-authenticate': 'Basic realm="My Private Docker Registry Server"',
});
httpMock
.scope(baseUrl)
.head('/library/node/tags/list?n=10000')
.reply(401, undefined, {
'www-authenticate': 'Basic realm="My Private Docker Registry Server"',
});
const res = await getPkgReleases({
datasource: DockerDatasource.id,
depName: 'node',
Expand All @@ -1100,7 +1103,7 @@ describe('modules/datasource/docker/index', () => {
.get('/')
.times(2)
.reply(200)
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200)
.get('/node/tags/list?n=10000')
.reply(200, {
Expand Down Expand Up @@ -1165,7 +1168,7 @@ describe('modules/datasource/docker/index', () => {
.get('/')
.times(3)
.reply(200)
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200)
.get('/node/tags/list?n=10000')
.reply(200, { tags: ['abc'] })
Expand Down Expand Up @@ -1206,7 +1209,7 @@ describe('modules/datasource/docker/index', () => {
.scope('https://registry.company.com/v2')
.get('/')
.reply(200)
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200)
.get('/node/tags/list?n=10000')
.reply(200, { tags: ['latest'] })
Expand All @@ -1231,7 +1234,7 @@ describe('modules/datasource/docker/index', () => {
.scope('https://registry.company.com/v2')
.get('/')
.reply(200)
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200)
.get('/node/tags/list?n=10000')
.reply(200, { tags: ['latest'] })
Expand All @@ -1255,7 +1258,7 @@ describe('modules/datasource/docker/index', () => {
.scope('https://registry.company.com/v2')
.get('/')
.reply(200)
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200)
.get('/node/tags/list?n=10000')
.reply(200, { tags: ['latest'] })
Expand All @@ -1277,7 +1280,7 @@ describe('modules/datasource/docker/index', () => {
.get('/')
.times(3)
.reply(200)
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200)
.get('/node/tags/list?n=10000')
.reply(200, { tags: ['1'] })
Expand Down Expand Up @@ -1323,7 +1326,7 @@ describe('modules/datasource/docker/index', () => {
.get('/')
.times(3)
.reply(200)
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200)
.get('/node/tags/list?n=10000')
.reply(200, { tags: ['1'] })
Expand Down Expand Up @@ -1367,7 +1370,7 @@ describe('modules/datasource/docker/index', () => {
.scope('https://registry.company.com/v2')
.get('/')
.reply(200)
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(200)
.get('/node/tags/list?n=10000')
.reply(200, { tags: ['latest'] })
Expand Down Expand Up @@ -1397,7 +1400,7 @@ describe('modules/datasource/docker/index', () => {
.reply(401, '', {
'www-authenticate': 'Basic realm="My Private Docker Registry Server"',
})
.head('/tags/list')
.head('/node/tags/list?n=10000')
.reply(401, '', {
'www-authenticate': 'Basic realm="My Private Docker Registry Server"',
});
Expand Down
5 changes: 2 additions & 3 deletions lib/modules/datasource/docker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ export async function getAuthHeaders(
registryHost: string,
dockerRepository: string,
apiCheckMode: 'head' | 'get' = 'get',
apiCheckPath = '/v2/'
apiCheckUrl = `${registryHost}/v2/`
): Promise<OutgoingHttpHeaders | null> {
try {
const apiCheckUrl = `${registryHost}${apiCheckPath}`;
const apiCheckResponse = await http[apiCheckMode](apiCheckUrl, {
throwHttpErrors: false,
noAuth: true,
Expand Down Expand Up @@ -689,7 +688,7 @@ export class DockerDatasource extends Datasource {
registryHost,
dockerRepository,
'head',
'/v2/tags/list'
url
);
if (!headers) {
logger.debug('Failed to get authHeaders for getTags lookup');
Expand Down

0 comments on commit a1d294d

Please sign in to comment.