Skip to content
Snippets Groups Projects
Commit 3a623944 authored by Maximilian Gaß's avatar Maximilian Gaß Committed by Rhys Arkins
Browse files

feat: docker registry authentication (#2339)

Closes #798
parent a53eb1ad
No related branches found
No related tags found
No related merge requests found
const dockerRegistryClient = require('docker-registry-client');
const got = require('got');
const URL = require('url');
const parseLinkHeader = require('parse-link-header');
const wwwAuthenticate = require('www-authenticate');
const { isVersion, sortVersions } = require('../versioning/docker');
const endpoints = require('../util/endpoints');
module.exports = {
getDigest,
......@@ -38,10 +40,18 @@ async function getAuthHeaders(registry, repository) {
// prettier-ignore
const authUrl = `${authenticateHeader.parms.realm}?service=${authenticateHeader.parms.service}&scope=repository:${repository}:pull`;
const { host } = URL.parse(registry);
const opts = endpoints.find({ platform: 'docker', host }, { json: true });
if (opts.username && opts.password) {
const auth = Buffer.from(`${opts.username}:${opts.password}`).toString(
'base64'
);
opts.headers = { Authorization: `Basic ${auth}` };
}
logger.debug(
`Obtaining docker registry token for ${repository} using url ${authUrl}`
);
const { token } = (await got(authUrl, { json: true })).body;
const { token } = (await got(authUrl, opts)).body;
// istanbul ignore if
if (!token) {
logger.warn('Failed to obtain docker registry token');
......
const got = require('got');
const docker = require('../../lib/datasource/docker');
const endpoints = require('../../lib/util/endpoints');
jest.mock('got');
jest.mock('../../lib/util/endpoints');
describe('api/docker', () => {
describe('getDigest', () => {
beforeEach(() => {
jest.resetAllMocks();
endpoints.find.mockReturnValue({
username: 'some-username',
password: 'some-password',
});
});
it('returns null if no token', async () => {
got.mockReturnValueOnce({ body: {} });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment