Skip to content

Commit

Permalink
feat(docker): add support for authenticating at ECR with session toke…
Browse files Browse the repository at this point in the history
…ns (#11967)
  • Loading branch information
fgreinacher committed Oct 1, 2021
1 parent 624fad0 commit b4a8406
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lib/datasource/docker/common.ts
Expand Up @@ -33,8 +33,10 @@ async function getECRAuthToken(
config.credentials = {
accessKeyId: opts.username,
secretAccessKey: opts.password,
...(opts.token && { sessionToken: opts.token }),
};
}

const ecr = new ECR(config);
try {
const data = await ecr.getAuthorizationToken({});
Expand Down
58 changes: 48 additions & 10 deletions lib/datasource/docker/index.spec.ts
Expand Up @@ -201,11 +201,12 @@ describe('datasource/docker/index', () => {
.reply(401, '', {
'www-authenticate': 'Basic realm="My Private Docker Registry Server"',
})
.head('/library/some-dep/manifests/some-tag', undefined, {
reqheaders: {
authorization: 'Basic c29tZS11c2VybmFtZTpzb21lLXBhc3N3b3Jk',
},
})

.head('/library/some-dep/manifests/some-tag')
.matchHeader(
'authorization',
'Basic c29tZS11c2VybmFtZTpzb21lLXBhc3N3b3Jk'
)
.reply(200, '', { 'docker-content-digest': 'some-digest' });
const res = await getDigest(
{ datasource: 'docker', depName: 'some-dep' },
Expand Down Expand Up @@ -237,11 +238,48 @@ describe('datasource/docker/index', () => {
.reply(401, '', {
'www-authenticate': 'Basic realm="My Private Docker Registry Server"',
})
.head('/node/manifests/some-tag', undefined, {
reqheaders: { authorization: 'Basic abc' },
.head('/node/manifests/some-tag')
.matchHeader('authorization', 'Basic test_token')
.reply(200, '', { 'docker-content-digest': 'some-digest' });

mockEcrAuthResolve({
authorizationData: [{ authorizationToken: 'test_token' }],
});

await getDigest(
{
datasource: 'docker',
depName: '123456789.dkr.ecr.us-east-1.amazonaws.com/node',
},
'some-tag'
);

expect(AWS.ECR).toHaveBeenCalledWith({
credentials: {
accessKeyId: 'some-username',
secretAccessKey: 'some-password',
},
region: 'us-east-1',
});
});

it('passes session token to ECR client', async () => {
httpMock
.scope(amazonUrl)
.get('/')
.reply(401, '', {
'www-authenticate': 'Basic realm="My Private Docker Registry Server"',
})
.head('/node/manifests/some-tag')
.matchHeader('authorization', 'Basic test_token')
.reply(200, '', { 'docker-content-digest': 'some-digest' });

hostRules.find.mockReturnValue({
username: 'some-username',
password: 'some-password',
token: 'some-session-token',
});

mockEcrAuthResolve({
authorizationData: [{ authorizationToken: 'test_token' }],
});
Expand All @@ -258,6 +296,7 @@ describe('datasource/docker/index', () => {
credentials: {
accessKeyId: 'some-username',
secretAccessKey: 'some-password',
sessionToken: 'some-session-token',
},
region: 'us-east-1',
});
Expand All @@ -270,9 +309,8 @@ describe('datasource/docker/index', () => {
.reply(401, '', {
'www-authenticate': 'Basic realm="My Private Docker Registry Server"',
})
.head('/node/manifests/some-tag', undefined, {
reqheaders: { authorization: 'Basic abc' },
})
.head('/node/manifests/some-tag')
.matchHeader('authorization', 'Basic test')
.reply(200, '', { 'docker-content-digest': 'some-digest' });

mockEcrAuthResolve({
Expand Down

0 comments on commit b4a8406

Please sign in to comment.