Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions lib/util/git/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,80 @@ describe('util/git/auth', () => {
GIT_CONFIG_VALUE_2: 'https://git.mycompany.com/scm/',
});
});

it('returns url with /a/ prefix for gerrit with username and password', () => {
expect(
getGitAuthenticatedEnvironmentVariables('https://gerrit.example.com/', {
username: 'user',
password: 'pass',
hostType: 'gerrit',
matchHost: 'gerrit.example.com',
}),
).toStrictEqual({
GIT_CONFIG_COUNT: '4',
GIT_CONFIG_KEY_0:
'url.https://user:pass@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://user:pass@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_KEY_2:
'url.https://user:pass@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_KEY_3:
'url.https://user:pass@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@gerrit.example.com/',
GIT_CONFIG_VALUE_1: 'git@gerrit.example.com:',
GIT_CONFIG_VALUE_2: 'https://gerrit.example.com/',
GIT_CONFIG_VALUE_3: 'https://gerrit.example.com/a/',
});
});

it('returns url with /a/ prefix for gerrit with token', () => {
expect(
getGitAuthenticatedEnvironmentVariables('https://gerrit.example.com/', {
token: 'token1234',
hostType: 'gerrit',
matchHost: 'gerrit.example.com',
}),
).toStrictEqual({
GIT_CONFIG_COUNT: '4',
GIT_CONFIG_KEY_0:
'url.https://ssh:token1234@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://git:token1234@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_KEY_2:
'url.https://token1234@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_KEY_3:
'url.https://token1234@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@gerrit.example.com/',
GIT_CONFIG_VALUE_1: 'git@gerrit.example.com:',
GIT_CONFIG_VALUE_2: 'https://gerrit.example.com/',
GIT_CONFIG_VALUE_3: 'https://gerrit.example.com/a/',
});
});

it('returns url with /a/ prefix for gerrit hosted on a subpath', () => {
expect(
getGitAuthenticatedEnvironmentVariables('https://example.com/gerrit/', {
username: 'user',
password: 'pass',
hostType: 'gerrit',
matchHost: 'https://example.com/gerrit',
}),
).toStrictEqual({
GIT_CONFIG_COUNT: '4',
GIT_CONFIG_KEY_0:
'url.https://user:pass@example.com/gerrit/a/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://user:pass@example.com/gerrit/a/.insteadOf',
GIT_CONFIG_KEY_2:
'url.https://user:pass@example.com/gerrit/a/.insteadOf',
GIT_CONFIG_KEY_3:
'url.https://user:pass@example.com/gerrit/a/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@example.com/gerrit',
GIT_CONFIG_VALUE_1: 'git@example.com:gerrit',
GIT_CONFIG_VALUE_2: 'https://example.com/gerrit',
GIT_CONFIG_VALUE_3: 'https://example.com/gerrit/a/',
});
});
});

describe('getGitEnvironmentVariables()', () => {
Expand Down Expand Up @@ -579,5 +653,53 @@ describe('util/git/auth', () => {
GIT_CONFIG_VALUE_2: 'https://git.mycompany.com/scm/',
});
});

it('returns environment variables with /a/ prefix for gerrit with username and password', () => {
add({
hostType: 'gerrit',
matchHost: 'https://gerrit.example.com',
username: 'user',
password: 'pass',
});
expect(getGitEnvironmentVariables()).toStrictEqual({
GIT_CONFIG_COUNT: '4',
GIT_CONFIG_KEY_0:
'url.https://user:pass@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://user:pass@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_KEY_2:
'url.https://user:pass@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_KEY_3:
'url.https://user:pass@gerrit.example.com/a/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@gerrit.example.com/',
GIT_CONFIG_VALUE_1: 'git@gerrit.example.com:',
GIT_CONFIG_VALUE_2: 'https://gerrit.example.com/',
GIT_CONFIG_VALUE_3: 'https://gerrit.example.com/a/',
});
});

it('returns environment variables with /a/ prefix for gerrit on subpath', () => {
add({
hostType: 'gerrit',
matchHost: 'https://example.com/gerrit',
username: 'user',
password: 'pass',
});
expect(getGitEnvironmentVariables()).toStrictEqual({
GIT_CONFIG_COUNT: '4',
GIT_CONFIG_KEY_0:
'url.https://user:pass@example.com/gerrit/a/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://user:pass@example.com/gerrit/a/.insteadOf',
GIT_CONFIG_KEY_2:
'url.https://user:pass@example.com/gerrit/a/.insteadOf',
GIT_CONFIG_KEY_3:
'url.https://user:pass@example.com/gerrit/a/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@example.com/gerrit',
GIT_CONFIG_VALUE_1: 'git@example.com:gerrit',
GIT_CONFIG_VALUE_2: 'https://example.com/gerrit',
GIT_CONFIG_VALUE_3: 'https://example.com/gerrit/a/',
});
});
});
});
20 changes: 20 additions & 0 deletions lib/util/git/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,29 @@ export function getAuthenticationRules(
insteadOf: insteadUrl.toString(protocol),
});

if (hostType === 'gerrit') {
// Gerrit requires /a/ prefix in the URL path for authenticated HTTP access.
// Modify replacement URLs to include /a/ and add an extra rule so URLs
// that already contain /a/ are not doubled (git uses longest-prefix match).
const httpsInsteadOf = authenticationRules[2].insteadOf;
for (const rule of authenticationRules) {
rule.url = addGerritAuthPrefix(rule.url);
}
authenticationRules.push({
url: authenticationRules[2].url,
insteadOf: addGerritAuthPrefix(httpsInsteadOf),
});
}

return authenticationRules;
}

function addGerritAuthPrefix(urlStr: string): string {
const u = new URL(urlStr);
u.pathname = u.pathname.replace(/\/?$/, '/a/');
return u.href;
}

export function getGitEnvironmentVariables(
additionalHostTypes: string[] = [],
): NodeJS.ProcessEnv {
Expand Down
Loading