Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(git/auth): fix insteadOf for bitbucket-server #29951

Merged
merged 6 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions lib/util/git/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,27 @@ describe('util/git/auth', () => {
GIT_CONFIG_VALUE_2: 'https://github.com:89/org/repo.git',
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get data info and report bugs

Copy link

@Tripp1984 Tripp1984 Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iam

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iam


it('returns url with token for bitbucket-server', () => {
expect(
getGitAuthenticatedEnvironmentVariables('https://git.mycompany.com/', {
token: 'token1234',
hostType: 'bitbucket-server',
matchHost: 'git.mycompay.com',
}),
).toStrictEqual({
GIT_CONFIG_COUNT: '3',
GIT_CONFIG_KEY_0:
'url.https://ssh:token1234@git.mycompany.com/scm/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://git:token1234@git.mycompany.com/scm/.insteadOf',
GIT_CONFIG_KEY_2:
'url.https://token1234@git.mycompany.com/scm/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@git.mycompany.com:7999/',
GIT_CONFIG_VALUE_1: 'ssh://git@git.mycompany.com:7999/',
GIT_CONFIG_VALUE_2: 'https://git.mycompany.com/scm/',
});
});
});

describe('getGitEnvironmentVariables()', () => {
Expand Down Expand Up @@ -538,5 +559,25 @@ describe('util/git/auth', () => {
});
expect(getGitEnvironmentVariables(['custom'])).toStrictEqual({});
});

it('returns environment variables for bitbucket-server', () => {
add({
hostType: 'bitbucket-server',
matchHost: 'git.mycompany.com',
token: 'token123',
});
expect(getGitEnvironmentVariables()).toStrictEqual({
GIT_CONFIG_COUNT: '3',
GIT_CONFIG_KEY_0:
'url.https://ssh:token123@git.mycompany.com/scm/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://git:token123@git.mycompany.com/scm/.insteadOf',
GIT_CONFIG_KEY_2:
'url.https://token123@git.mycompany.com/scm/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@git.mycompany.com:7999/',
GIT_CONFIG_VALUE_1: 'ssh://git@git.mycompany.com:7999/',
GIT_CONFIG_VALUE_2: 'https://git.mycompany.com/scm/',
});
});
});
});
27 changes: 22 additions & 5 deletions lib/util/git/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

authenticationRules = getAuthenticationRules(
originalGitUrl,
hostType,
`${encodedUsername}:${encodedPassword}`,
);
}
Expand Down Expand Up @@ -106,7 +107,7 @@
if (type === 'gitlab') {
token = `gitlab-ci-token:${authToken}`;
}
return getAuthenticationRules(url, token);
return getAuthenticationRules(url, type, token);
}

/**
Expand All @@ -115,33 +116,49 @@
*/
export function getAuthenticationRules(
gitUrl: string,
hostType: string | undefined | null,
token: string,
): AuthenticationRule[] {
const authenticationRules = [];
const hasUser = token.split(':').length > 1;
const insteadUrl = parseGitUrl(gitUrl);
let type = hostType;
if (!type) {
type = detectPlatform(gitUrl);

Check warning on line 127 in lib/util/git/auth.ts

View check run for this annotation

Codecov / codecov/patch

lib/util/git/auth.ts#L127

Added line #L127 was not covered by tests
}

if (type === 'bitbucket-server') {
insteadUrl.source = 'bitbucket-server';
rarkins marked this conversation as resolved.
Show resolved Hide resolved
}

const url = { ...insteadUrl };
const protocol = regEx(/^https?$/).test(url.protocol)
? url.protocol
: 'https';

const sshUrl = { ...insteadUrl };
rarkins marked this conversation as resolved.
Show resolved Hide resolved
if (type === 'bitbucket-server' && !sshUrl.port) {
// By default, bitbucket-server SSH port is 7999.
// For non-default port, the generated auth config will likely be incorrect.
sshUrl.port = 7999;
}

// ssh protocol with user if empty
url.token = hasUser ? token : `ssh:${token}`;
authenticationRules.push({
url: url.toString(protocol),
// only edge case, need to stringify ourself because the exact syntax is not supported by the library
// https://github.com/IonicaBizau/git-url-parse/blob/246c9119fb42c2ea1c280028fe77c53eb34c190c/lib/index.js#L246
insteadOf: `ssh://git@${insteadUrl.resource}${
insteadUrl.port ? `:${insteadUrl.port}` : ''
}/${insteadUrl.full_name}${insteadUrl.git_suffix ? '.git' : ''}`,
insteadOf: `ssh://git@${sshUrl.resource}${
sshUrl.port ? `:${sshUrl.port}` : ''
}/${sshUrl.full_name}${sshUrl.git_suffix ? '.git' : ''}`,
});

// alternative ssh protocol with user if empty
url.token = hasUser ? token : `git:${token}`;
authenticationRules.push({
url: url.toString(protocol),
insteadOf: insteadUrl.toString('ssh'),
insteadOf: sshUrl.toString('ssh'),
});

// https protocol with no user as default fallback
Expand Down
Loading