Skip to content

Commit 9be2898

Browse files
committed
Use Bearer authentication for Axios when contacting SonarQube Cloud
We can't do it for SonarQube Server as older versions were only supporting passing the token through BASIC authentication.
1 parent 2156309 commit 9be2898

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

src/common/latest/helpers/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const PROP_NAMES = {
44
HOST_URL: "sonar.host.url",
55
TOKEN: "sonar.token",
66
LOGIN: "sonar.login",
7-
PASSSWORD: "sonar.password",
7+
PASSWORD: "sonar.password",
88
ORG: "sonar.organization",
99
PROJECTKEY: "sonar.projectKey",
1010
PROJECTNAME: "sonar.projectName",

src/common/latest/helpers/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function stringifyScannerParams(scannerParams: ScannerParams) {
1616

1717
export function sanitizeScannerParams(scannerParams: ScannerParams) {
1818
delete scannerParams[PROP_NAMES.LOGIN];
19-
delete scannerParams[PROP_NAMES.PASSSWORD];
19+
delete scannerParams[PROP_NAMES.PASSWORD];
2020
return scannerParams;
2121
}
2222

src/common/latest/sonarqube/Endpoint.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export default class Endpoint {
4242
}
4343

4444
public get auth(): { username: string; password: string } {
45-
// If using user/password
4645
if (
4746
!this.data.token &&
4847
this.data.username &&
@@ -57,11 +56,19 @@ export default class Endpoint {
5756
toAxiosOptions(): AxiosRequestConfig {
5857
const options: AxiosRequestConfig = {
5958
timeout: Endpoint.REQUEST_TIMEOUT,
60-
auth: {
59+
};
60+
61+
const isSonarCloud = Boolean(this.data.token);
62+
if (isSonarCloud) {
63+
options.headers = {
64+
Authorization: `Bearer ${this.data.token}`
65+
}
66+
} else {
67+
options.auth = {
6168
username: this.auth.username,
6269
password: this.auth.password,
63-
},
64-
};
70+
}
71+
}
6572

6673
// Fetch proxy from environment
6774
// We ignore proxy set by agent proxy configuration, we need to discuss whether we want to itroduce it
@@ -100,8 +107,8 @@ export default class Endpoint {
100107

101108
return {
102109
[PROP_NAMES.HOST_URL]: this.data.url,
103-
[authKey]: this.data.token || this.data.username,
104-
[PROP_NAMES.PASSSWORD]:
110+
[authKey]: this.data.token ?? this.data.username,
111+
[PROP_NAMES.PASSWORD]:
105112
this.data.password && this.data.password.length > 0 ? this.data.password : null,
106113
[PROP_NAMES.ORG]: this.data.organization,
107114
};

src/common/latest/sonarqube/__tests__/Endpoint-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ it("On SonarQube Cloud, password is always null", () => {
3131

3232
const result = Endpoint.getEndpoint("sonarcloud", EndpointType.Cloud);
3333

34-
expect(result.toSonarProps("7.1.0")[PROP_NAMES.PASSSWORD]).toBeNull();
34+
expect(result.toSonarProps("7.1.0")[PROP_NAMES.PASSWORD]).toBeNull();
3535
expect(result.auth.password).toBe("");
3636
});
3737

@@ -45,7 +45,7 @@ it("On SonarQube Server, password is empty should not be intepreted", () => {
4545

4646
const result = Endpoint.getEndpoint("sonarqube", EndpointType.Server);
4747

48-
expect(result.toSonarProps("7.1.0")[PROP_NAMES.PASSSWORD]).toBeNull();
48+
expect(result.toSonarProps("7.1.0")[PROP_NAMES.PASSWORD]).toBeNull();
4949
expect(result.auth.password).toBe("");
5050
});
5151

@@ -58,7 +58,7 @@ it("On SonarQube Server password is not empty should be intepreted", () => {
5858

5959
const result = Endpoint.getEndpoint("sonarqube", EndpointType.Server);
6060

61-
expect(result.toSonarProps("7.1.0")[PROP_NAMES.PASSSWORD]).toEqual("P@ssword");
61+
expect(result.toSonarProps("7.1.0")[PROP_NAMES.PASSWORD]).toEqual("P@ssword");
6262
expect(result.auth.password).toEqual("P@ssword");
6363
});
6464

0 commit comments

Comments
 (0)