From 16ce577fd45fd92d1d590ec17d43515073dd5666 Mon Sep 17 00:00:00 2001 From: Eric Giffon Date: Mon, 13 Mar 2023 17:56:23 +0100 Subject: [PATCH] SONAR-18591 Add sonar.token property and deprecate sonar.login --- build.gradle | 3 +- .../BitbucketPipelinesTutorial-it.tsx.snap | 4 +-- .../bitbucket-pipelines/commands/DotNet.ts | 4 +-- .../GithubActionTutorial-it.tsx.snap | 4 +-- .../github-action/commands/DotNet.tsx | 4 +-- .../GitLabCITutorial-it.tsx.snap | 4 +-- .../gitlabci/commands/PipeCommand.tsx | 4 +-- .../__snapshots__/OtherTutorial-it.tsx.snap | 12 +++---- .../tutorials/other/commands/DotNetCore.tsx | 4 +-- .../other/commands/DotNetFramework.tsx | 4 +-- .../tutorials/other/commands/ExecScanner.tsx | 2 +- .../tutorials/other/commands/JavaGradle.tsx | 2 +- .../tutorials/other/commands/JavaMaven.tsx | 2 +- .../bootstrap/DefaultScannerWsClient.java | 4 +-- .../bootstrap/ScannerWsClientProvider.java | 5 ++- .../DeprecatedPropertiesWarningGenerator.java | 12 ++++--- .../bootstrap/DefaultScannerWsClientTest.java | 2 +- .../ScannerWsClientProviderTest.java | 3 +- ...recatedPropertiesWarningGeneratorTest.java | 34 ++++++++++++++----- 19 files changed, 66 insertions(+), 47 deletions(-) diff --git a/build.gradle b/build.gradle index 95b3bde47cd..d831ea27bdf 100644 --- a/build.gradle +++ b/build.gradle @@ -353,7 +353,7 @@ subprojects { dependency 'org.reflections:reflections:0.10.2' dependency 'org.simpleframework:simple:5.1.6' dependency 'org.sonarsource.git.blame:git-files-blame:1.0.1.169' - dependency 'org.sonarsource.orchestrator:sonar-orchestrator:3.40.0.183' + dependency 'org.sonarsource.orchestrator:sonar-orchestrator:3.41.0.295' dependency 'org.sonarsource.update-center:sonar-update-center-common:1.29.0.1000' dependency("org.springframework:spring-context:${springVersion}") { exclude 'commons-logging:commons-logging' @@ -616,4 +616,3 @@ tasks.named('sonarqube') { outputFile.append(JsonOutput.toJson([category: "Analysis", suite: "Standalone", operation: "total", duration: taskDuration]) + '\n') } } - diff --git a/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/__tests__/__snapshots__/BitbucketPipelinesTutorial-it.tsx.snap b/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/__tests__/__snapshots__/BitbucketPipelinesTutorial-it.tsx.snap index fc048678545..4491112afda 100644 --- a/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/__tests__/__snapshots__/BitbucketPipelinesTutorial-it.tsx.snap +++ b/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/__tests__/__snapshots__/BitbucketPipelinesTutorial-it.tsx.snap @@ -15,9 +15,9 @@ definitions: - apt-get install --yes openjdk-11-jre - dotnet tool install --global dotnet-sonarscanner - export PATH="$PATH:/root/.dotnet/tools" - - dotnet sonarscanner begin /k:"my-project" /d:"sonar.login=\${SONAR_TOKEN}" /d:"sonar.host.url=\${SONAR_HOST_URL}" + - dotnet sonarscanner begin /k:"my-project" /d:"sonar.token=\${SONAR_TOKEN}" /d:"sonar.host.url=\${SONAR_HOST_URL}" - dotnet build - - dotnet sonarscanner end /d:"sonar.login=\${SONAR_TOKEN}" + - dotnet sonarscanner end /d:"sonar.token=\${SONAR_TOKEN}" caches: sonar: ~/.sonar diff --git a/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/commands/DotNet.ts b/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/commands/DotNet.ts index 02481e27c92..3b6e17fafe2 100644 --- a/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/commands/DotNet.ts +++ b/server/sonar-web/src/main/js/components/tutorials/bitbucket-pipelines/commands/DotNet.ts @@ -36,9 +36,9 @@ definitions: - apt-get install --yes openjdk-11-jre - dotnet tool install --global dotnet-sonarscanner - export PATH="$PATH:/root/.dotnet/tools" - - dotnet sonarscanner begin /k:"${projectKey}" /d:"sonar.login=\${SONAR_TOKEN}" /d:"sonar.host.url=\${SONAR_HOST_URL}" + - dotnet sonarscanner begin /k:"${projectKey}" /d:"sonar.token=\${SONAR_TOKEN}" /d:"sonar.host.url=\${SONAR_HOST_URL}" - dotnet build - - dotnet sonarscanner end /d:"sonar.login=\${SONAR_TOKEN}" + - dotnet sonarscanner end /d:"sonar.token=\${SONAR_TOKEN}" caches: sonar: ~/.sonar diff --git a/server/sonar-web/src/main/js/components/tutorials/github-action/__tests__/__snapshots__/GithubActionTutorial-it.tsx.snap b/server/sonar-web/src/main/js/components/tutorials/github-action/__tests__/__snapshots__/GithubActionTutorial-it.tsx.snap index e7a91a7df94..178e17d2273 100644 --- a/server/sonar-web/src/main/js/components/tutorials/github-action/__tests__/__snapshots__/GithubActionTutorial-it.tsx.snap +++ b/server/sonar-web/src/main/js/components/tutorials/github-action/__tests__/__snapshots__/GithubActionTutorial-it.tsx.snap @@ -46,9 +46,9 @@ jobs: GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any shell: powershell run: | - .\\.sonar\\scanner\\dotnet-sonarscanner begin /k:"my-project" /d:sonar.login="\${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="\${{ secrets.SONAR_HOST_URL }}" + .\\.sonar\\scanner\\dotnet-sonarscanner begin /k:"my-project" /d:sonar.token="\${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="\${{ secrets.SONAR_HOST_URL }}" dotnet build - .\\.sonar\\scanner\\dotnet-sonarscanner end /d:sonar.login="\${{ secrets.SONAR_TOKEN }}"" + .\\.sonar\\scanner\\dotnet-sonarscanner end /d:sonar.token="\${{ secrets.SONAR_TOKEN }}"" `; exports[`should follow and complete all steps: CFamily Linux: .github/workflows/build.yml 1`] = ` diff --git a/server/sonar-web/src/main/js/components/tutorials/github-action/commands/DotNet.tsx b/server/sonar-web/src/main/js/components/tutorials/github-action/commands/DotNet.tsx index 138fde63301..26580bbfc34 100644 --- a/server/sonar-web/src/main/js/components/tutorials/github-action/commands/DotNet.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/github-action/commands/DotNet.tsx @@ -61,9 +61,9 @@ function dotnetYamlSteps(projectKey: string) { GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any shell: powershell run: | - .\\.sonar\\scanner\\dotnet-sonarscanner begin /k:"${projectKey}" /d:sonar.login="\${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="\${{ secrets.SONAR_HOST_URL }}" + .\\.sonar\\scanner\\dotnet-sonarscanner begin /k:"${projectKey}" /d:sonar.token="\${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="\${{ secrets.SONAR_HOST_URL }}" dotnet build - .\\.sonar\\scanner\\dotnet-sonarscanner end /d:sonar.login="\${{ secrets.SONAR_TOKEN }}"`; + .\\.sonar\\scanner\\dotnet-sonarscanner end /d:sonar.token="\${{ secrets.SONAR_TOKEN }}"`; } export default function DotNet(props: DotNetProps) { diff --git a/server/sonar-web/src/main/js/components/tutorials/gitlabci/__tests__/__snapshots__/GitLabCITutorial-it.tsx.snap b/server/sonar-web/src/main/js/components/tutorials/gitlabci/__tests__/__snapshots__/GitLabCITutorial-it.tsx.snap index 13f3630f4ed..cc7c5e33d3d 100644 --- a/server/sonar-web/src/main/js/components/tutorials/gitlabci/__tests__/__snapshots__/GitLabCITutorial-it.tsx.snap +++ b/server/sonar-web/src/main/js/components/tutorials/gitlabci/__tests__/__snapshots__/GitLabCITutorial-it.tsx.snap @@ -15,9 +15,9 @@ exports[`should follow and complete all steps: .NET: gitlab-ci.yml 1`] = ` - "apt-get install --yes openjdk-11-jre" - "dotnet tool install --global dotnet-sonarscanner" - "export PATH=\\"$PATH:$HOME/.dotnet/tools\\"" - - "dotnet sonarscanner begin /k:\\"my-project\\" /d:sonar.login=\\"$SONAR_TOKEN\\" /d:\\"sonar.host.url=$SONAR_HOST_URL\\" " + - "dotnet sonarscanner begin /k:\\"my-project\\" /d:sonar.token=\\"$SONAR_TOKEN\\" /d:\\"sonar.host.url=$SONAR_HOST_URL\\" " - "dotnet build" - - "dotnet sonarscanner end /d:sonar.login=\\"$SONAR_TOKEN\\"" + - "dotnet sonarscanner end /d:sonar.token=\\"$SONAR_TOKEN\\"" allow_failure: true rules: - if: $CI_COMMIT_BRANCH == 'main' diff --git a/server/sonar-web/src/main/js/components/tutorials/gitlabci/commands/PipeCommand.tsx b/server/sonar-web/src/main/js/components/tutorials/gitlabci/commands/PipeCommand.tsx index a9766761161..323b6ad67cc 100644 --- a/server/sonar-web/src/main/js/components/tutorials/gitlabci/commands/PipeCommand.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/gitlabci/commands/PipeCommand.tsx @@ -44,9 +44,9 @@ const BUILD_TOOL_SPECIFIC = { - "apt-get install --yes openjdk-11-jre" - "dotnet tool install --global dotnet-sonarscanner" - "export PATH=\\"$PATH:$HOME/.dotnet/tools\\"" - - "dotnet sonarscanner begin /k:\\"${projectKey}\\" /d:sonar.login=\\"$SONAR_TOKEN\\" /d:\\"sonar.host.url=$SONAR_HOST_URL\\" " + - "dotnet sonarscanner begin /k:\\"${projectKey}\\" /d:sonar.token=\\"$SONAR_TOKEN\\" /d:\\"sonar.host.url=$SONAR_HOST_URL\\" " - "dotnet build" - - "dotnet sonarscanner end /d:sonar.login=\\"$SONAR_TOKEN\\""`, + - "dotnet sonarscanner end /d:sonar.token=\\"$SONAR_TOKEN\\""`, }, [BuildTools.Other]: { image: ` diff --git a/server/sonar-web/src/main/js/components/tutorials/other/__tests__/__snapshots__/OtherTutorial-it.tsx.snap b/server/sonar-web/src/main/js/components/tutorials/other/__tests__/__snapshots__/OtherTutorial-it.tsx.snap index 402d7e7f541..cee9bc50114 100644 --- a/server/sonar-web/src/main/js/components/tutorials/other/__tests__/__snapshots__/OtherTutorial-it.tsx.snap +++ b/server/sonar-web/src/main/js/components/tutorials/other/__tests__/__snapshots__/OtherTutorial-it.tsx.snap @@ -60,26 +60,26 @@ $env:SONAR_SCANNER_OPTS="-server" " `; -exports[`can choose build tools and copy provided settings: dotnet core: execute command 1 1`] = `"dotnet sonarscanner begin /k:"my-project" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="generatedtoken2""`; +exports[`can choose build tools and copy provided settings: dotnet core: execute command 1 1`] = `"dotnet sonarscanner begin /k:"my-project" /d:sonar.host.url="http://localhost:9000" /d:sonar.token="generatedtoken2""`; exports[`can choose build tools and copy provided settings: dotnet core: execute command 2 1`] = `"dotnet build"`; -exports[`can choose build tools and copy provided settings: dotnet core: execute command 3 1`] = `"dotnet sonarscanner end /d:sonar.login="generatedtoken2""`; +exports[`can choose build tools and copy provided settings: dotnet core: execute command 3 1`] = `"dotnet sonarscanner end /d:sonar.token="generatedtoken2""`; exports[`can choose build tools and copy provided settings: dotnet core: install scanner globally 1`] = `"dotnet tool install --global dotnet-sonarscanner"`; -exports[`can choose build tools and copy provided settings: dotnet framework: execute command 1 1`] = `"SonarScanner.MSBuild.exe begin /k:"my-project" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="generatedtoken2""`; +exports[`can choose build tools and copy provided settings: dotnet framework: execute command 1 1`] = `"SonarScanner.MSBuild.exe begin /k:"my-project" /d:sonar.host.url="http://localhost:9000" /d:sonar.token="generatedtoken2""`; exports[`can choose build tools and copy provided settings: dotnet framework: execute command 2 1`] = `"MsBuild.exe /t:Rebuild"`; -exports[`can choose build tools and copy provided settings: dotnet framework: execute command 3 1`] = `"SonarScanner.MSBuild.exe end /d:sonar.login="generatedtoken2""`; +exports[`can choose build tools and copy provided settings: dotnet framework: execute command 3 1`] = `"SonarScanner.MSBuild.exe end /d:sonar.token="generatedtoken2""`; exports[`can choose build tools and copy provided settings: gradle: execute scanner 1`] = ` "./gradlew sonar \\ -Dsonar.projectKey=my-project \\ -Dsonar.projectName='MyProject' \\ -Dsonar.host.url=http://localhost:9000 \\ - -Dsonar.login=generatedtoken2" + -Dsonar.token=generatedtoken2" `; exports[`can choose build tools and copy provided settings: gradle: sonarqube plugin 1`] = ` @@ -93,7 +93,7 @@ exports[`can choose build tools and copy provided settings: maven: execute scann -Dsonar.projectKey=my-project \\ -Dsonar.projectName='MyProject' \\ -Dsonar.host.url=http://localhost:9000 \\ - -Dsonar.login=generatedtoken2" + -Dsonar.token=generatedtoken2" `; exports[`can choose build tools and copy provided settings: other linux: execute scanner 1`] = ` diff --git a/server/sonar-web/src/main/js/components/tutorials/other/commands/DotNetCore.tsx b/server/sonar-web/src/main/js/components/tutorials/other/commands/DotNetCore.tsx index 94bead50656..ef3fae9e943 100644 --- a/server/sonar-web/src/main/js/components/tutorials/other/commands/DotNetCore.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/other/commands/DotNetCore.tsx @@ -28,9 +28,9 @@ export default function DotNetCore(props: DotNetProps) { const { baseUrl, component, token } = props; const commands = [ - `dotnet sonarscanner begin /k:"${component.key}" /d:sonar.host.url="${baseUrl}" /d:sonar.login="${token}"`, + `dotnet sonarscanner begin /k:"${component.key}" /d:sonar.host.url="${baseUrl}" /d:sonar.token="${token}"`, 'dotnet build', - `dotnet sonarscanner end /d:sonar.login="${token}"`, + `dotnet sonarscanner end /d:sonar.token="${token}"`, ]; return ( diff --git a/server/sonar-web/src/main/js/components/tutorials/other/commands/DotNetFramework.tsx b/server/sonar-web/src/main/js/components/tutorials/other/commands/DotNetFramework.tsx index 1bd7580cd0b..55362bb46e3 100644 --- a/server/sonar-web/src/main/js/components/tutorials/other/commands/DotNetFramework.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/other/commands/DotNetFramework.tsx @@ -28,9 +28,9 @@ export default function DotNetFramework(props: DotNetProps) { const { baseUrl, component, token } = props; const commands = [ - `SonarScanner.MSBuild.exe begin /k:"${component.key}" /d:sonar.host.url="${baseUrl}" /d:sonar.login="${token}"`, + `SonarScanner.MSBuild.exe begin /k:"${component.key}" /d:sonar.host.url="${baseUrl}" /d:sonar.token="${token}"`, 'MsBuild.exe /t:Rebuild', - `SonarScanner.MSBuild.exe end /d:sonar.login="${token}"`, + `SonarScanner.MSBuild.exe end /d:sonar.token="${token}"`, ]; return ( diff --git a/server/sonar-web/src/main/js/components/tutorials/other/commands/ExecScanner.tsx b/server/sonar-web/src/main/js/components/tutorials/other/commands/ExecScanner.tsx index 018c09ce925..b37c57abbb9 100644 --- a/server/sonar-web/src/main/js/components/tutorials/other/commands/ExecScanner.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/other/commands/ExecScanner.tsx @@ -47,7 +47,7 @@ export default function ExecScanner(props: ExecScannerProps) { '-D' + q('sonar.sources=.'), cfamily ? '-D' + q('sonar.cfamily.build-wrapper-output=bw-output') : undefined, '-D' + q(`sonar.host.url=${baseUrl}`), - isLocal ? '-D' + q(`sonar.login=${token}`) : undefined, + isLocal ? '-D' + q(`sonar.token=${token}`) : undefined, ]; return ( diff --git a/server/sonar-web/src/main/js/components/tutorials/other/commands/JavaGradle.tsx b/server/sonar-web/src/main/js/components/tutorials/other/commands/JavaGradle.tsx index 7fe32b990e6..929bb0e0904 100644 --- a/server/sonar-web/src/main/js/components/tutorials/other/commands/JavaGradle.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/other/commands/JavaGradle.tsx @@ -44,7 +44,7 @@ export default function JavaGradle(props: JavaGradleProps) { `-Dsonar.projectKey=${component.key}`, `-Dsonar.projectName='${component.name}'`, `-Dsonar.host.url=${baseUrl}`, - `-Dsonar.login=${token}`, + `-Dsonar.token=${token}`, ]; return ( diff --git a/server/sonar-web/src/main/js/components/tutorials/other/commands/JavaMaven.tsx b/server/sonar-web/src/main/js/components/tutorials/other/commands/JavaMaven.tsx index 869c9e66329..aa4ce669b68 100644 --- a/server/sonar-web/src/main/js/components/tutorials/other/commands/JavaMaven.tsx +++ b/server/sonar-web/src/main/js/components/tutorials/other/commands/JavaMaven.tsx @@ -39,7 +39,7 @@ export default function JavaMaven(props: JavaMavenProps) { `-Dsonar.projectKey=${component.key}`, `-Dsonar.projectName='${component.name}'`, `-Dsonar.host.url=${baseUrl}`, - `-Dsonar.login=${token}`, + `-Dsonar.token=${token}`, ]; return ( diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java index a53e81dc407..5dfc85f4dd2 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/DefaultScannerWsClient.java @@ -109,8 +109,8 @@ public class DefaultScannerWsClient implements ScannerWsClient { CoreProperties.LOGIN, CoreProperties.PASSWORD)); } // not authenticated - see https://jira.sonarsource.com/browse/SONAR-4048 - throw MessageException.of(format("Not authorized. Analyzing this project requires authentication. " - + "Please provide a user token in %s or other credentials in %s and %s.", CoreProperties.LOGIN, CoreProperties.LOGIN, CoreProperties.PASSWORD)); + throw MessageException.of(format("Not authorized. Analyzing this project requires authentication. Please provide a user token in %s" + + " or other credentials in %s and %s.", ScannerWsClientProvider.TOKEN_PROPERTY, CoreProperties.LOGIN, CoreProperties.PASSWORD)); } if (code == HTTP_FORBIDDEN) { throw MessageException.of("You're not authorized to analyze this project or the project doesn't exist on SonarQube" + diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java index a42e48a5054..c38ad36fd31 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/ScannerWsClientProvider.java @@ -34,6 +34,8 @@ import static org.apache.commons.lang.StringUtils.defaultIfBlank; public class ScannerWsClientProvider { static final int CONNECT_TIMEOUT_MS = 5_000; static final String READ_TIMEOUT_SEC_PROPERTY = "sonar.ws.timeout"; + public static final String TOKEN_PROPERTY = "sonar.token"; + private static final String TOKEN_ENV_VARIABLE = "SONAR_TOKEN"; static final int DEFAULT_READ_TIMEOUT_SEC = 60; @Bean("DefaultScannerWsClient") @@ -43,7 +45,8 @@ public class ScannerWsClientProvider { HttpConnector.Builder connectorBuilder = HttpConnector.newBuilder(); String timeoutSec = defaultIfBlank(scannerProps.property(READ_TIMEOUT_SEC_PROPERTY), valueOf(DEFAULT_READ_TIMEOUT_SEC)); - String token = defaultIfBlank(system.envVariable("SONAR_TOKEN"), null); + String envVarToken = defaultIfBlank(system.envVariable(TOKEN_ENV_VARIABLE), null); + String token = defaultIfBlank(scannerProps.property(TOKEN_PROPERTY), envVarToken); String login = defaultIfBlank(scannerProps.property(CoreProperties.LOGIN), token); connectorBuilder .readTimeoutMilliseconds(parseInt(timeoutSec) * 1_000) diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGenerator.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGenerator.java index 79f5d1eb527..54595d632d0 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGenerator.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGenerator.java @@ -26,13 +26,14 @@ import org.sonar.api.config.Configuration; import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; +import org.sonar.scanner.bootstrap.ScannerWsClientProvider; public class DeprecatedPropertiesWarningGenerator { private static final Logger LOG = Loggers.get(DeprecatedPropertiesWarningGenerator.class); @VisibleForTesting - public static final String PASSWORD_WARN_MESSAGE = "Property '" + CoreProperties.PASSWORD + "' is deprecated. It will not be supported " + - "in the future. Please instead use the 'sonar.login' parameter with a token."; + static final String CREDENTIALS_WARN_MESSAGE = String.format("The properties '%s' and '%s' are deprecated. They will not be supported " + + "in the future. Please instead use the '%s' parameter.", CoreProperties.LOGIN, CoreProperties.PASSWORD, ScannerWsClientProvider.TOKEN_PROPERTY); private final Configuration configuration; private final AnalysisWarnings analysisWarnings; @@ -43,10 +44,11 @@ public class DeprecatedPropertiesWarningGenerator { } public void execute() { + Optional login = configuration.get(CoreProperties.LOGIN); Optional password = configuration.get(CoreProperties.PASSWORD); - if (password.isPresent()) { - LOG.warn(PASSWORD_WARN_MESSAGE); - analysisWarnings.addUnique(PASSWORD_WARN_MESSAGE); + if (login.isPresent() || password.isPresent()) { + LOG.warn(CREDENTIALS_WARN_MESSAGE); + analysisWarnings.addUnique(CREDENTIALS_WARN_MESSAGE); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java index cf2c26e7a15..1d5aed37812 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/DefaultScannerWsClientTest.java @@ -101,7 +101,7 @@ public class DefaultScannerWsClientTest { assertThatThrownBy(() -> new DefaultScannerWsClient(wsClient, false, new GlobalAnalysisMode(new ScannerProperties(Collections.emptyMap())), analysisWarnings).call(request)) .isInstanceOf(MessageException.class) - .hasMessage("Not authorized. Analyzing this project requires authentication. Please provide a user token in sonar.login or other " + + .hasMessage("Not authorized. Analyzing this project requires authentication. Please provide a user token in sonar.token or other " + "credentials in sonar.login and sonar.password."); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java index 74117787648..e1c36390291 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ScannerWsClientProviderTest.java @@ -56,8 +56,7 @@ public class ScannerWsClientProviderTest { public void provide_client_with_custom_settings() { Map props = new HashMap<>(); props.put("sonar.host.url", "https://here/sonarqube"); - props.put("sonar.login", "theLogin"); - props.put("sonar.password", "thePassword"); + props.put("sonar.token", "testToken"); props.put("sonar.ws.timeout", "42"); ScannerProperties settings = new ScannerProperties(props); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGeneratorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGeneratorTest.java index fbcc86817a5..f8cbf8c944d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGeneratorTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DeprecatedPropertiesWarningGeneratorTest.java @@ -20,6 +20,7 @@ package org.sonar.scanner.scan; import org.assertj.core.api.Assertions; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.mockito.Mockito; @@ -32,7 +33,7 @@ import org.sonar.api.utils.log.LoggerLevel; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; -import static org.sonar.scanner.scan.DeprecatedPropertiesWarningGenerator.PASSWORD_WARN_MESSAGE; +import static org.sonar.scanner.scan.DeprecatedPropertiesWarningGenerator.CREDENTIALS_WARN_MESSAGE; public class DeprecatedPropertiesWarningGeneratorTest { @@ -42,26 +43,41 @@ public class DeprecatedPropertiesWarningGeneratorTest { private final MapSettings settings = new MapSettings(); private final AnalysisWarnings analysisWarnings = Mockito.spy(AnalysisWarnings.class); - private final DeprecatedPropertiesWarningGenerator underTest = new DeprecatedPropertiesWarningGenerator(settings.asConfig(), analysisWarnings); + private final DeprecatedPropertiesWarningGenerator underTest = new DeprecatedPropertiesWarningGenerator(settings.asConfig(), + analysisWarnings); + + @Before + public void setUp() throws Exception { + settings.removeProperty(CoreProperties.LOGIN); + settings.removeProperty(CoreProperties.PASSWORD); + } @Test - public void verify_warning_when_using_password() { - settings.setProperty(CoreProperties.PASSWORD, "winner winner chicken dinner"); + public void execute_whenUsingLogin_shouldAddWarning() { + settings.setProperty(CoreProperties.LOGIN, "test"); underTest.execute(); - verify(analysisWarnings, times(1)).addUnique(PASSWORD_WARN_MESSAGE); - Assertions.assertThat(logger.logs(LoggerLevel.WARN)).contains(PASSWORD_WARN_MESSAGE); + verify(analysisWarnings, times(1)).addUnique(CREDENTIALS_WARN_MESSAGE); + Assertions.assertThat(logger.logs(LoggerLevel.WARN)).contains(CREDENTIALS_WARN_MESSAGE); } @Test - public void verify_no_warning_when_not_using_password() { - settings.removeProperty(CoreProperties.PASSWORD); + public void execute_whenUsingPassword_shouldAddWarning() { + settings.setProperty(CoreProperties.PASSWORD, "winner winner chicken dinner"); underTest.execute(); + verify(analysisWarnings, times(1)).addUnique(CREDENTIALS_WARN_MESSAGE); + Assertions.assertThat(logger.logs(LoggerLevel.WARN)).contains(CREDENTIALS_WARN_MESSAGE); + } + + @Test + public void execute_whenNotUsingLoginOrPassword_shouldNotAddWarning() { + underTest.execute(); + verifyNoInteractions(analysisWarnings); Assertions.assertThat(logger.logs(LoggerLevel.WARN)).isEmpty(); } -} \ No newline at end of file +} -- 2.39.5