You've already forked sap-jenkins-library
							
							
				mirror of
				https://github.com/SAP/jenkins-library.git
				synced 2025-10-30 23:57:50 +02:00 
			
		
		
		
	Make sure detect.sh is removed after use (#2238)
Should avoid issues with this file being owned by root (perhaps via running in docker container), preventing the workspace from being cleaned properly.
This commit is contained in:
		| @@ -2,6 +2,7 @@ package cmd | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"os" | ||||
| 	"strings" | ||||
|  | ||||
| 	piperhttp "github.com/SAP/jenkins-library/pkg/http" | ||||
| @@ -16,7 +17,19 @@ import ( | ||||
| 	"github.com/SAP/jenkins-library/pkg/versioning" | ||||
| ) | ||||
|  | ||||
| func detectExecuteScan(config detectExecuteScanOptions, telemetryData *telemetry.CustomData) { | ||||
| type detectFileUtils interface { | ||||
| 	Abs(path string) (string, error) | ||||
| 	FileExists(filename string) (bool, error) | ||||
| 	FileRemove(filename string) error | ||||
| 	Copy(src, dest string) (int64, error) | ||||
| 	FileRead(path string) ([]byte, error) | ||||
| 	FileWrite(path string, content []byte, perm os.FileMode) error | ||||
| 	MkdirAll(path string, perm os.FileMode) error | ||||
| 	Chmod(path string, mode os.FileMode) error | ||||
| 	Glob(pattern string) (matches []string, err error) | ||||
| } | ||||
|  | ||||
| func detectExecuteScan(config detectExecuteScanOptions, _ *telemetry.CustomData) { | ||||
| 	c := command.Command{ | ||||
| 		ErrorCategoryMapping: map[string][]string{ | ||||
| 			log.ErrorCompliance.String(): { | ||||
| @@ -43,10 +56,19 @@ func detectExecuteScan(config detectExecuteScanOptions, telemetryData *telemetry | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func runDetect(config detectExecuteScanOptions, command command.ShellRunner, fileUtils piperutils.FileUtils, httpClient piperhttp.Downloader) error { | ||||
| func runDetect(config detectExecuteScanOptions, command command.ShellRunner, fileUtils detectFileUtils, httpClient piperhttp.Downloader) error { | ||||
| 	// detect execution details, see https://synopsys.atlassian.net/wiki/spaces/INTDOCS/pages/88440888/Sample+Synopsys+Detect+Scan+Configuration+Scenarios+for+Black+Duck | ||||
| 	httpClient.DownloadFile("https://detect.synopsys.com/detect.sh", "detect.sh", nil, nil) | ||||
| 	err := fileUtils.Chmod("detect.sh", 0700) | ||||
| 	err := httpClient.DownloadFile("https://detect.synopsys.com/detect.sh", "detect.sh", nil, nil) | ||||
| 	if err != nil { | ||||
| 		return fmt.Errorf("failed to download 'detect.sh' script: %w", err) | ||||
| 	} | ||||
| 	defer func() { | ||||
| 		err := fileUtils.FileRemove("detect.sh") | ||||
| 		if err != nil { | ||||
| 			log.Entry().Warnf("failed to delete 'detect.sh' script: %v", err) | ||||
| 		} | ||||
| 	}() | ||||
| 	err = fileUtils.Chmod("detect.sh", 0700) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|   | ||||
| @@ -45,9 +45,7 @@ func TestRunDetect(t *testing.T) { | ||||
| 		err := runDetect(detectExecuteScanOptions{}, &s, &fileUtilsMock, &httpClient) | ||||
|  | ||||
| 		assert.Equal(t, httpClient.downloadedFiles["https://detect.synopsys.com/detect.sh"], "detect.sh") | ||||
| 		fileStatus, err := fileUtilsMock.Stat("detect.sh") | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Equal(t, fileStatus.Mode(), os.FileMode(0700)) | ||||
| 		assert.True(t, fileUtilsMock.HasRemovedFile("detect.sh")) | ||||
| 		assert.NoError(t, err) | ||||
| 		assert.Equal(t, ".", s.Dir, "Wrong execution directory used") | ||||
| 		assert.Equal(t, "/bin/bash", s.Shell[0], "Bash shell expected") | ||||
| @@ -58,9 +56,11 @@ func TestRunDetect(t *testing.T) { | ||||
| 	t.Run("failure case", func(t *testing.T) { | ||||
| 		s := mock.ShellMockRunner{ShouldFailOnCommand: map[string]error{"./detect.sh --blackduck.url= --blackduck.api.token= --detect.project.name=\\\"\\\" --detect.project.version.name=\\\"\\\" --detect.code.location.name=\\\"\\\"": fmt.Errorf("Test Error")}} | ||||
| 		fileUtilsMock := mock.FilesMock{} | ||||
| 		fileUtilsMock.AddFile("detect.sh", []byte("")) | ||||
| 		httpClient := httpClientMock{} | ||||
| 		err := runDetect(detectExecuteScanOptions{}, &s, &fileUtilsMock, &httpClient) | ||||
| 		assert.NotNil(t, err) | ||||
| 		assert.EqualError(t, err, "Test Error") | ||||
| 		assert.True(t, fileUtilsMock.HasRemovedFile("detect.sh")) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("maven parameters", func(t *testing.T) { | ||||
|   | ||||
| @@ -9,7 +9,6 @@ import ( | ||||
| 	piperhttp "github.com/SAP/jenkins-library/pkg/http" | ||||
| 	"github.com/SAP/jenkins-library/pkg/maven" | ||||
| 	"github.com/SAP/jenkins-library/pkg/mock" | ||||
| 	"github.com/SAP/jenkins-library/pkg/piperutils" | ||||
| 	"github.com/ghodss/yaml" | ||||
| 	"github.com/stretchr/testify/assert" | ||||
| ) | ||||
| @@ -267,7 +266,7 @@ func TestMarBuild(t *testing.T) { | ||||
| 		downloadAndCopySettingsFiles = func( | ||||
| 			globalSettings string, | ||||
| 			projectSettings string, | ||||
| 			fileUtils piperutils.FileUtils, | ||||
| 			fileUtils maven.FileUtils, | ||||
| 			httpClient maven.SettingsDownloadUtils) error { | ||||
| 			projectSettingsFile = projectSettings | ||||
| 			globalSettingsFile = globalSettings | ||||
|   | ||||
| @@ -43,7 +43,7 @@ type mavenExecRunner interface { | ||||
| } | ||||
|  | ||||
| type mavenUtils interface { | ||||
| 	piperutils.FileUtils | ||||
| 	FileUtils | ||||
| 	DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -3,7 +3,6 @@ package maven | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"github.com/SAP/jenkins-library/pkg/log" | ||||
| 	"github.com/SAP/jenkins-library/pkg/piperutils" | ||||
| 	"net/http" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| @@ -12,11 +11,22 @@ import ( | ||||
|  | ||||
| var getenv = os.Getenv | ||||
|  | ||||
| // SettingsDownloadUtils defines an interface for downloading files. | ||||
| type SettingsDownloadUtils interface { | ||||
| 	DownloadFile(url, filename string, header http.Header, cookies []*http.Cookie) error | ||||
| } | ||||
|  | ||||
| func DownloadAndGetMavenParameters(globalSettingsFile string, projectSettingsFile string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils) ([]string, error) { | ||||
| // FileUtils defines the external file-related functionality needed by this package. | ||||
| type FileUtils interface { | ||||
| 	FileExists(filename string) (bool, error) | ||||
| 	Copy(src, dest string) (int64, error) | ||||
| 	MkdirAll(path string, perm os.FileMode) error | ||||
| 	Glob(pattern string) (matches []string, err error) | ||||
| } | ||||
|  | ||||
| // DownloadAndGetMavenParameters downloads the global or project settings file if the strings contain URLs. | ||||
| // It then constructs the arguments that need to be passed to maven in order to point to use these settings files. | ||||
| func DownloadAndGetMavenParameters(globalSettingsFile string, projectSettingsFile string, fileUtils FileUtils, httpClient SettingsDownloadUtils) ([]string, error) { | ||||
| 	mavenArgs := []string{} | ||||
| 	if len(globalSettingsFile) > 0 { | ||||
| 		globalSettingsFileName, err := downloadSettingsIfURL(globalSettingsFile, ".pipeline/mavenGlobalSettings.xml", fileUtils, httpClient, false) | ||||
| @@ -42,7 +52,10 @@ func DownloadAndGetMavenParameters(globalSettingsFile string, projectSettingsFil | ||||
| 	return mavenArgs, nil | ||||
| } | ||||
|  | ||||
| func DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils) error { | ||||
| // DownloadAndCopySettingsFiles downloads the global or project settings file if the strings contain URLs. | ||||
| // It copies the given files to either the locations specified in the environment variables M2_HOME and HOME | ||||
| // or the default locations where maven expects them. | ||||
| func DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile string, fileUtils FileUtils, httpClient SettingsDownloadUtils) error { | ||||
| 	if len(projectSettingsFile) > 0 { | ||||
| 		destination, err := getProjectSettingsFileDest() | ||||
| 		if err != nil { | ||||
| @@ -73,7 +86,7 @@ func DownloadAndCopySettingsFiles(globalSettingsFile string, projectSettingsFile | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func downloadAndCopySettingsFile(src string, dest string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils) error { | ||||
| func downloadAndCopySettingsFile(src string, dest string, fileUtils FileUtils, httpClient SettingsDownloadUtils) error { | ||||
| 	if len(src) == 0 { | ||||
| 		return fmt.Errorf("Settings file source location not provided") | ||||
| 	} | ||||
| @@ -115,7 +128,7 @@ func downloadAndCopySettingsFile(src string, dest string, fileUtils piperutils.F | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func downloadSettingsIfURL(settingsFileOption, settingsFile string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils, overwrite bool) (string, error) { | ||||
| func downloadSettingsIfURL(settingsFileOption, settingsFile string, fileUtils FileUtils, httpClient SettingsDownloadUtils, overwrite bool) (string, error) { | ||||
| 	result := settingsFileOption | ||||
| 	if strings.HasPrefix(settingsFileOption, "http:") || strings.HasPrefix(settingsFileOption, "https:") { | ||||
| 		err := downloadSettingsFromURL(settingsFileOption, settingsFile, fileUtils, httpClient, overwrite) | ||||
| @@ -127,7 +140,7 @@ func downloadSettingsIfURL(settingsFileOption, settingsFile string, fileUtils pi | ||||
| 	return result, nil | ||||
| } | ||||
|  | ||||
| func downloadSettingsFromURL(url, filename string, fileUtils piperutils.FileUtils, httpClient SettingsDownloadUtils, overwrite bool) error { | ||||
| func downloadSettingsFromURL(url, filename string, fileUtils FileUtils, httpClient SettingsDownloadUtils, overwrite bool) error { | ||||
| 	exists, _ := fileUtils.FileExists(filename) | ||||
| 	if exists && !overwrite { | ||||
| 		log.Entry().Infof("Not downloading maven settings file, because it already exists at '%s'", filename) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user