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

Don't overwrite SONAR_SCANNER_OPTS #5230

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 12 additions & 2 deletions cmd/sonarExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,23 @@ func loadSonarScanner(url string, client piperhttp.Downloader) error {
return nil
}

func addSonarScannerOpts(opts string) {
tmpOpts := os.Getenv("SONAR_SCANNER_OPTS")
if len(tmpOpts) > 0 {
log.Entry().Debug("SONAR_SCANNER_OPTS already set. Appending to existing value: " + tmpOpts)
sonar.addEnvironment("SONAR_SCANNER_OPTS=" + tmpOpts + " " + opts)
} else {
sonar.addEnvironment("SONAR_SCANNER_OPTS=" + opts)
}
}

func loadCertificates(certificateList []string, client piperhttp.Downloader, runner command.ExecRunner) error {
truststorePath := filepath.Join(getWorkingDir(), ".certificates")
truststoreFile := filepath.Join(truststorePath, "cacerts")

if exists, _ := fileUtilsExists(truststoreFile); exists {
// use local existing trust store
sonar.addEnvironment("SONAR_SCANNER_OPTS=" + keytool.GetMavenOpts(truststoreFile))
addSonarScannerOpts(keytool.GetMavenOpts(truststoreFile))
log.Entry().WithField("trust store", truststoreFile).Info("Using local trust store")
} else if len(certificateList) > 0 {
// create download temp dir
Expand Down Expand Up @@ -451,7 +461,7 @@ func loadCertificates(certificateList []string, client piperhttp.Downloader, run
// return errors.Wrap(err, "Adding certificate to keystore failed")
}
}
sonar.addEnvironment("SONAR_SCANNER_OPTS=" + keytool.GetMavenOpts(truststoreFile))
addSonarScannerOpts(keytool.GetMavenOpts(truststoreFile))
log.Entry().WithField("trust store", truststoreFile).Info("Using local trust store")
} else {
log.Entry().Debug("Download of TLS certificates skipped")
Expand Down
3 changes: 2 additions & 1 deletion cmd/sonarExecuteScan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func TestRunSonar(t *testing.T) {
PullRequestProvider: "GitHub",
}
fileUtilsExists = mockFileUtilsExists(true)
os.Setenv("SONAR_SCANNER_OPTS", "-Xmx42m")
// test
err := runSonar(options, &mockDownloadClient, &mockRunner, apiClient, &mock.FilesMock{}, &sonarExecuteScanInflux{})
// assert
Expand All @@ -184,7 +185,7 @@ func TestRunSonar(t *testing.T) {
assert.Contains(t, sonar.options, "-Dsonar.organization=SAP")
assert.Contains(t, sonar.environment, "SONAR_HOST_URL="+sonarServerURL)
assert.Contains(t, sonar.environment, "SONAR_TOKEN=secret-ABC")
assert.Contains(t, sonar.environment, "SONAR_SCANNER_OPTS=-Djavax.net.ssl.trustStore="+filepath.Join(getWorkingDir(), ".certificates", "cacerts")+" -Djavax.net.ssl.trustStorePassword=changeit")
assert.Contains(t, sonar.environment, "SONAR_SCANNER_OPTS=-Xmx42m -Djavax.net.ssl.trustStore="+filepath.Join(getWorkingDir(), ".certificates", "cacerts")+" -Djavax.net.ssl.trustStorePassword=changeit")
})
t.Run("with custom options", func(t *testing.T) {
// init
Expand Down
Loading