Skip to content

Commit

Permalink
feat(sonar): respect existing SONAR_SCANNER_OPTS definition (#5230)
Browse files Browse the repository at this point in the history
* Don't overwrite SONAR_SCANNER_OPTS

* Add tests

* Reset SONAR_SCANNER_OPTS after testing it

* Reset SONAR_SCANNER_OPTS after testing it

---------

Co-authored-by: Ivan Nikiforov <nikiforr@gmail.com>
  • Loading branch information
inf2381 and niki4 authored Feb 10, 2025
1 parent 5561d2a commit fad1286
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
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
6 changes: 5 additions & 1 deletion cmd/sonarExecuteScan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ func TestRunSonar(t *testing.T) {
PullRequestProvider: "GitHub",
}
fileUtilsExists = mockFileUtilsExists(true)
os.Setenv("SONAR_SCANNER_OPTS", "-Xmx42m")
defer os.Setenv("SONAR_SCANNER_OPTS", "")
// test
err := runSonar(options, &mockDownloadClient, &mockRunner, apiClient, &mock.FilesMock{}, &sonarExecuteScanInflux{})
// assert
Expand All @@ -184,7 +186,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 Expand Up @@ -456,6 +458,7 @@ func TestSonarLoadCertificates(t *testing.T) {
}
fileUtilsExists = mockFileUtilsExists(true)
defer func() { fileUtilsExists = piperutils.FileExists }()
defer os.Setenv("SONAR_SCANNER_OPTS", "")
// test
err := loadCertificates([]string{}, &mockClient, &mockRunner)
// assert
Expand All @@ -471,6 +474,7 @@ func TestSonarLoadCertificates(t *testing.T) {
options: []string{},
}
fileUtilsExists = mockFileUtilsExists(false)
defer os.Setenv("SONAR_SCANNER_OPTS", "")
// test
err := loadCertificates([]string{"https://sap.com/custom-1.crt", "https://sap.com/custom-2.crt"}, &mockClient, &mockRunner)
// assert
Expand Down

0 comments on commit fad1286

Please sign in to comment.