From e8d745052a035a5ab61675f65a96295d9bf90af6 Mon Sep 17 00:00:00 2001 From: Ashly Mathew Date: Tue, 3 Dec 2024 11:01:48 +0100 Subject: [PATCH 01/12] Modify document generator to include GHA (#5190) * include GHA in documentation * add some missing GHA places * add some missing GHA places * Correct step description --- pkg/documentation/generator.go | 5 +++-- pkg/documentation/generator/description.go | 16 +++++++++++++++- pkg/documentation/generator/main.go | 5 +++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/documentation/generator.go b/pkg/documentation/generator.go index 7f595aeb63..5e601068a4 100644 --- a/pkg/documentation/generator.go +++ b/pkg/documentation/generator.go @@ -32,12 +32,13 @@ func main() { var docTemplatePath string var customLibraryStepFile string var customDefaultFiles sliceFlags - var includeAzure bool + var includeAzure, includeGHA bool flag.StringVar(&metadataPath, "metadataDir", "./resources/metadata", "The directory containing the step metadata. Default points to \\'resources/metadata\\'.") flag.StringVar(&docTemplatePath, "docuDir", "./documentation/docs/steps/", "The directory containing the docu stubs. Default points to \\'documentation/docs/steps/\\'.") flag.StringVar(&customLibraryStepFile, "customLibraryStepFile", "", "") flag.Var(&customDefaultFiles, "customDefaultFile", "Path to a custom default configuration file.") flag.BoolVar(&includeAzure, "includeAzure", false, "Include Azure-specifics in step documentation.") + flag.BoolVar(&includeGHA, "includeGHA", false, "Include GitHub Actions-specifics in step documentation.") // flags for stage documentation var generateStageConfig bool @@ -85,7 +86,7 @@ func main() { OpenDocTemplateFile: openDocTemplateFile, DocFileWriter: writeFile, OpenFile: openFile, - }, includeAzure) + }, includeAzure, includeGHA) checkError(err) } } diff --git a/pkg/documentation/generator/description.go b/pkg/documentation/generator/description.go index 9d2301cedd..8e9e65cdc3 100644 --- a/pkg/documentation/generator/description.go +++ b/pkg/documentation/generator/description.go @@ -13,7 +13,8 @@ const ( headlineUsage = "## Usage\n\n" headlineJenkinsPipeline = " === \"Jenkins\"\n\n" headlineCommandLine = " === \"Command Line\"\n\n" - headlineAzure = " === \"Azure\"\n\n" + headlineAzure = " === \"Azure DevOps\"\n\n" + headlineGHA = " === \"GitHub Actions\"\n\n" spacingTabBox = " " ) @@ -67,6 +68,19 @@ func createDescriptionSection(stepData *config.StepData) string { description += fmt.Sprintf("%v```\n\n", spacingTabBox) } + // add GiHub Actions specific information if activated + if includeGHA { + description += headlineGHA + description += fmt.Sprintf("%v```\n", spacingTabBox) + description += fmt.Sprintf("%vsteps:\n", spacingTabBox) + description += fmt.Sprintf("%v - name: your preferred name for the step\n", spacingTabBox) + description += fmt.Sprintf("%v uses: SAP/project-piper-action@main\n", spacingTabBox) + description += fmt.Sprintf("%v with:\n", spacingTabBox) + description += fmt.Sprintf("%v step-name: %v\n", spacingTabBox, stepData.Metadata.Name) + description += fmt.Sprintf("%v flags: --anyStepParameter\n", spacingTabBox) + description += fmt.Sprintf("%v```\n\n", spacingTabBox) + } + // add command line information description += headlineCommandLine description += fmt.Sprintf("%v```sh\n", spacingTabBox) diff --git a/pkg/documentation/generator/main.go b/pkg/documentation/generator/main.go index 77328c2392..067ffd3cc7 100644 --- a/pkg/documentation/generator/main.go +++ b/pkg/documentation/generator/main.go @@ -25,7 +25,7 @@ type DocuHelperData struct { } var stepParameterNames []string -var includeAzure bool +var includeAzure, includeGHA bool func readStepConfiguration(stepMetadata config.StepData, customDefaultFiles []string, docuHelperData DocuHelperData) config.StepConfig { filters := stepMetadata.GetParameterFilters() @@ -58,8 +58,9 @@ func readStepConfiguration(stepMetadata config.StepData, customDefaultFiles []st } // GenerateStepDocumentation generates step coding based on step configuration provided in yaml files -func GenerateStepDocumentation(metadataFiles []string, customDefaultFiles []string, docuHelperData DocuHelperData, azure bool) error { +func GenerateStepDocumentation(metadataFiles []string, customDefaultFiles []string, docuHelperData DocuHelperData, azure bool, githubAction bool) error { includeAzure = azure + includeGHA = githubAction for key := range metadataFiles { stepMetadata := readStepMetadata(metadataFiles[key], docuHelperData) From 97049db02f995fc63f9aa4ed669240761300f5d4 Mon Sep 17 00:00:00 2001 From: maxcask Date: Mon, 9 Dec 2024 10:44:44 +0400 Subject: [PATCH 02/12] fix passing default params to Central Build stage steps (#5199) * duplicate config for Build name leave go changes only --- pkg/config/config.go | 7 ++----- src/com/sap/piper/ConfigurationHelper.groovy | 3 --- src/com/sap/piper/StageNameProvider.groovy | 16 ++-------------- vars/commonPipelineEnvironment.groovy | 3 --- 4 files changed, 4 insertions(+), 25 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index ddfaf50c4e..05bce93f7e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -72,9 +72,9 @@ func (c *Config) ApplyAliasConfig(parameters []StepParameters, secrets []StepSec if c.Steps[stepName] != nil { c.Steps[stepName] = setParamValueFromAlias(stepName, c.Steps[stepName], filters.Steps, p.Name, p.Aliases) } + //copy stage configuration with Build name if centralBuild, ok := c.Stages["Central Build"]; ok { c.Stages["Build"] = centralBuild - delete(c.Stages, "Central Build") } } for _, s := range secrets { @@ -85,9 +85,9 @@ func (c *Config) ApplyAliasConfig(parameters []StepParameters, secrets []StepSec if c.Steps[stepName] != nil { c.Steps[stepName] = setParamValueFromAlias(stepName, c.Steps[stepName], filters.Steps, s.Name, s.Aliases) } + //copy stage secrets configuration with Build name if centralBuild, ok := c.Stages["Central Build"]; ok { c.Stages["Build"] = centralBuild - delete(c.Stages, "Central Build") } } } @@ -193,9 +193,6 @@ func (c *Config) GetStepConfig(flagValues map[string]interface{}, paramJSON stri } c.ApplyAliasConfig(parameters, secrets, filters, stageName, stepName, stepAliases) - if stageName == "Central Build" { - stageName = "Build" - } // initialize with defaults from step.yaml stepConfig.mixInStepDefaults(parameters) diff --git a/src/com/sap/piper/ConfigurationHelper.groovy b/src/com/sap/piper/ConfigurationHelper.groovy index b6b1558ddf..2eeaaf0e6f 100644 --- a/src/com/sap/piper/ConfigurationHelper.groovy +++ b/src/com/sap/piper/ConfigurationHelper.groovy @@ -10,9 +10,6 @@ class ConfigurationHelper implements Serializable { } ConfigurationHelper loadStepDefaults(Map compatibleParameters = [:], String stageName = step.env.STAGE_NAME) { - if (stageName == 'Central Build'){ - stageName = 'Build' - } DefaultValueCache.prepare(step) this.config = ConfigurationLoader.defaultGeneralConfiguration() mixin(ConfigurationLoader.defaultGeneralConfiguration(), null, compatibleParameters) diff --git a/src/com/sap/piper/StageNameProvider.groovy b/src/com/sap/piper/StageNameProvider.groovy index cd2fa71f06..ceb7967629 100644 --- a/src/com/sap/piper/StageNameProvider.groovy +++ b/src/com/sap/piper/StageNameProvider.groovy @@ -3,8 +3,6 @@ package com.sap.piper @Singleton class StageNameProvider implements Serializable { static final long serialVersionUID = 1L - static final String CENTRAL_BUILD = "Central Build"; - static final String BUILD = "Build"; /** Stores a feature toggle for defaulting to technical names in stages */ boolean useTechnicalStageNames = false @@ -12,9 +10,7 @@ class StageNameProvider implements Serializable { String getStageName(Script script, Map parameters, Script step) { String stageName = null if (parameters.stageName in CharSequence) { - stageName = parameters.stageName - stageName = replaceCentralBuild(stageName); - return stageName + return parameters.stageName } if (this.useTechnicalStageNames) { String technicalStageName = getTechnicalStageName(step) @@ -22,15 +18,7 @@ class StageNameProvider implements Serializable { return technicalStageName } } - if (stageName == null) { - stageName = script.env.STAGE_NAME - stageName = replaceCentralBuild(stageName); - } - return stageName - } - - private String replaceCentralBuild(String stageName) { - return CENTRAL_BUILD.equals(stageName) ? BUILD : stageName; + return script.env.STAGE_NAME } static String getTechnicalStageName(Script step) { diff --git a/vars/commonPipelineEnvironment.groovy b/vars/commonPipelineEnvironment.groovy index 90e3a871d3..b44842d9ca 100644 --- a/vars/commonPipelineEnvironment.groovy +++ b/vars/commonPipelineEnvironment.groovy @@ -192,9 +192,6 @@ class commonPipelineEnvironment implements Serializable { Map getStepConfiguration(stepName, stageName = env.STAGE_NAME, includeDefaults = true) { Map defaults = [:] - if (stageName == 'Central Build') { - stageName = 'Build' - } if (includeDefaults) { defaults = ConfigurationLoader.defaultGeneralConfiguration() defaults = ConfigurationMerger.merge(ConfigurationLoader.defaultStepConfiguration(null, stepName), null, defaults) From a70422c93d4157f527abc2f6a345a73c381861ba Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Mon, 9 Dec 2024 08:31:36 +0100 Subject: [PATCH 03/12] Revert "Unstash class files for Compliance stage (#5192)" (#5201) This reverts commit 28fb2b77bf7ba6af5000955e1664b8d3ec12195d. --- resources/com.sap.piper/pipeline/stashSettings.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/com.sap.piper/pipeline/stashSettings.yml b/resources/com.sap.piper/pipeline/stashSettings.yml index 8460d5b2b3..e41cad8eca 100644 --- a/resources/com.sap.piper/pipeline/stashSettings.yml +++ b/resources/com.sap.piper/pipeline/stashSettings.yml @@ -31,7 +31,6 @@ Acceptance: Compliance: unstash: - source - - classFiles stashes: [] Performance: From ec77db1aa865ad7eaa69cd5c2027c141473caa80 Mon Sep 17 00:00:00 2001 From: Ashly Mathew Date: Mon, 9 Dec 2024 10:36:58 +0100 Subject: [PATCH 04/12] Align description of ADO and GHA (#5202) * Align description of ADO and GHA * Update description.go --------- Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com> --- pkg/documentation/generator/description.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/documentation/generator/description.go b/pkg/documentation/generator/description.go index 8e9e65cdc3..744a15d1b9 100644 --- a/pkg/documentation/generator/description.go +++ b/pkg/documentation/generator/description.go @@ -65,6 +65,7 @@ func createDescriptionSection(stepData *config.StepData) string { description += fmt.Sprintf("%v name: %v\n", spacingTabBox, stepData.Metadata.Name) description += fmt.Sprintf("%v inputs:\n", spacingTabBox) description += fmt.Sprintf("%v stepName: %v\n", spacingTabBox, stepData.Metadata.Name) + description += fmt.Sprintf("%v flags: --anyStepParameter\n", spacingTabBox) description += fmt.Sprintf("%v```\n\n", spacingTabBox) } @@ -73,8 +74,8 @@ func createDescriptionSection(stepData *config.StepData) string { description += headlineGHA description += fmt.Sprintf("%v```\n", spacingTabBox) description += fmt.Sprintf("%vsteps:\n", spacingTabBox) - description += fmt.Sprintf("%v - name: your preferred name for the step\n", spacingTabBox) - description += fmt.Sprintf("%v uses: SAP/project-piper-action@main\n", spacingTabBox) + description += fmt.Sprintf("%v - uses: SAP/project-piper-action@releaseCommitSHA\n", spacingTabBox) + description += fmt.Sprintf("%v name: %v\n", spacingTabBox, stepData.Metadata.Name) description += fmt.Sprintf("%v with:\n", spacingTabBox) description += fmt.Sprintf("%v step-name: %v\n", spacingTabBox, stepData.Metadata.Name) description += fmt.Sprintf("%v flags: --anyStepParameter\n", spacingTabBox) From a206df2fdf11266bfbafbe73b30c1d478c68f109 Mon Sep 17 00:00:00 2001 From: Ivan Nikiforov Date: Wed, 11 Dec 2024 09:04:58 +0100 Subject: [PATCH 05/12] fix(documentation): check param for jenkinsParams before appending Jenkins-specific text (#5203) * Check for jenkinsParams before appending Jenkins-specific text * go fmt --------- Co-authored-by: Ivan Nikiforov --- pkg/documentation/generator/parameters.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/documentation/generator/parameters.go b/pkg/documentation/generator/parameters.go index 1c9e9f6848..eeb35d6aee 100644 --- a/pkg/documentation/generator/parameters.go +++ b/pkg/documentation/generator/parameters.go @@ -17,6 +17,8 @@ const ( deprecatedBadge = "![deprecated](https://img.shields.io/badge/-deprecated-red)" ) +var jenkinsParams = []string{"containerCommand", "containerName", "containerShell", "dockerVolumeBind", "dockerWorkspace", "sidecarReadyCommand", "sidecarWorkspace", "stashContent"} + // Replaces the Parameters placeholder with the content from the yaml func createParametersSection(stepData *config.StepData) string { @@ -97,7 +99,6 @@ func parameterFurtherInfo(paramName string, stepData *config.StepData, execution } // handle non-step parameters (e.g. Jenkins-specific parameters as well as execution environment parameters) - jenkinsParams := []string{"containerCommand", "containerName", "containerShell", "dockerVolumeBind", "dockerWorkspace", "sidecarReadyCommand", "sidecarWorkspace", "stashContent"} if !contains(stepParameterNames, paramName) { for _, secret := range stepData.Spec.Inputs.Secrets { if paramName == secret.Name && secret.Type == "jenkins" { @@ -161,7 +162,7 @@ func createParameterDetails(stepData *config.StepData) string { for _, param := range stepData.Spec.Inputs.Parameters { details += fmt.Sprintf("#### %v\n\n", param.Name) - if !contains(stepParameterNames, param.Name) { + if !contains(stepParameterNames, param.Name) && contains(jenkinsParams, param.Name) { details += "**Jenkins-specific:** Used for proper environment setup.\n\n" } @@ -200,7 +201,7 @@ func createParameterDetails(stepData *config.StepData) string { for _, secret := range stepData.Spec.Inputs.Secrets { details += fmt.Sprintf("#### %v\n\n", secret.Name) - if !contains(stepParameterNames, secret.Name) { + if !contains(stepParameterNames, secret.Name) && contains(jenkinsParams, secret.Name) { details += "**Jenkins-specific:** Used for proper environment setup. See *[using credentials](https://www.jenkins.io/doc/book/using/using-credentials/)* for details.\n\n" } From 055a413d21be73bafd07a82b76613127dedc6323 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 11 Dec 2024 14:29:41 +0100 Subject: [PATCH 06/12] Unstash class files for Compliance stage (#5192) (#5204) --- resources/com.sap.piper/pipeline/stashSettings.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/resources/com.sap.piper/pipeline/stashSettings.yml b/resources/com.sap.piper/pipeline/stashSettings.yml index e41cad8eca..8460d5b2b3 100644 --- a/resources/com.sap.piper/pipeline/stashSettings.yml +++ b/resources/com.sap.piper/pipeline/stashSettings.yml @@ -31,6 +31,7 @@ Acceptance: Compliance: unstash: - source + - classFiles stashes: [] Performance: From e92760525484dce1a3e5d63860954abb3256224a Mon Sep 17 00:00:00 2001 From: Akramdzhon Azamov <900658008.akram@gmail.com> Date: Thu, 12 Dec 2024 18:24:03 +0500 Subject: [PATCH 07/12] Added validation for buildTool and buildDescriptorFile (#5193) * Added validation for buildTool and buildDescriptorFile on whitesourceExecuteScan step * fix pull request's comment --------- Co-authored-by: Dmitrii Pavlukhin --- cmd/whitesourceExecuteScan.go | 49 +++++++++++++++++++++ cmd/whitesourceExecuteScan_test.go | 68 ++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/cmd/whitesourceExecuteScan.go b/cmd/whitesourceExecuteScan.go index 14772ec8f6..16c9f09d2a 100644 --- a/cmd/whitesourceExecuteScan.go +++ b/cmd/whitesourceExecuteScan.go @@ -76,6 +76,9 @@ func (w *whitesourceUtilsBundle) FileOpen(name string, flag int, perm os.FileMod } func (w *whitesourceUtilsBundle) GetArtifactCoordinates(buildTool, buildDescriptorFile string, options *versioning.Options) (versioning.Coordinates, error) { + if err := validationBuildDescriptorFile(buildTool, buildDescriptorFile); err != nil { + return versioning.Coordinates{}, err + } artifact, err := versioning.GetArtifact(buildTool, buildDescriptorFile, options, w) if err != nil { return versioning.Coordinates{}, err @@ -83,6 +86,52 @@ func (w *whitesourceUtilsBundle) GetArtifactCoordinates(buildTool, buildDescript return artifact.GetCoordinates() } +func validationBuildDescriptorFile(buildTool, buildDescriptorFile string) error { + if buildDescriptorFile == "" { + return nil + } + switch buildTool { + case "dub": + if filepath.Ext(buildDescriptorFile) != ".json" { + return errors.New("extension of buildDescriptorFile must be in '*.json'") + } + case "gradle": + if filepath.Ext(buildDescriptorFile) != ".properties" { + return errors.New("extension of buildDescriptorFile must be in '*.properties'") + } + case "golang": + if !strings.HasSuffix(buildDescriptorFile, "go.mod") && + !strings.HasSuffix(buildDescriptorFile, "VERSION") && + !strings.HasSuffix(buildDescriptorFile, "version.txt") { + return errors.New("buildDescriptorFile must be one of [\"go.mod\",\"VERSION\", \"version.txt\"]") + } + case "maven": + if filepath.Ext(buildDescriptorFile) != ".xml" { + return errors.New("extension of buildDescriptorFile must be in '*.xml'") + } + case "mta": + if filepath.Ext(buildDescriptorFile) != ".yaml" { + return errors.New("extension of buildDescriptorFile must be in '*.yaml'") + } + case "npm", "yarn": + if filepath.Ext(buildDescriptorFile) != ".json" { + return errors.New("extension of buildDescriptorFile must be in '*.json'") + } + case "pip": + if !strings.HasSuffix(buildDescriptorFile, "setup.py") && + !strings.HasSuffix(buildDescriptorFile, "version.txt") && + !strings.HasSuffix(buildDescriptorFile, "VERSION") { + return errors.New("buildDescriptorFile must be one of [\"setup.py\",\"version.txt\", \"VERSION\"]") + } + case "sbt": + if !strings.HasSuffix(buildDescriptorFile, "sbtDescriptor.json") && + !strings.HasSuffix(buildDescriptorFile, "build.sbt") { + return errors.New("extension of buildDescriptorFile must be in '*.json' or '*sbt'") + } + } + return nil +} + func (w *whitesourceUtilsBundle) getNpmExecutor(config *ws.ScanOptions) npm.Executor { if w.npmExecutor == nil { w.npmExecutor = npm.NewExecutor(npm.ExecutorOptions{DefaultNpmRegistry: config.DefaultNpmRegistry}) diff --git a/cmd/whitesourceExecuteScan_test.go b/cmd/whitesourceExecuteScan_test.go index b9c7e22b59..d1383c17ae 100644 --- a/cmd/whitesourceExecuteScan_test.go +++ b/cmd/whitesourceExecuteScan_test.go @@ -871,3 +871,71 @@ func TestPersistScannedProjects(t *testing.T) { assert.Equal(t, []string{"project - 1"}, cpe.custom.whitesourceProjectNames) }) } + +func TestBuildToolFiles(t *testing.T) { + t.Parallel() + t.Run("buildTool = dub", func(t *testing.T) { + err := validationBuildDescriptorFile("dub", "/home/mta.yaml") + assert.ErrorContains(t, err, "extension of buildDescriptorFile must be in '*.json'") + err = validationBuildDescriptorFile("dub", "/home/dub.json") + assert.NoError(t, err) + }) + t.Run("buildTool = gradle", func(t *testing.T) { + err := validationBuildDescriptorFile("gradle", "/home/go.mod") + assert.ErrorContains(t, err, "extension of buildDescriptorFile must be in '*.properties'") + err = validationBuildDescriptorFile("gradle", "/home/gradle.properties") + assert.NoError(t, err) + }) + t.Run("buildTool = golang", func(t *testing.T) { + err := validationBuildDescriptorFile("golang", "/home/go.json") + assert.ErrorContains(t, err, "buildDescriptorFile must be one of [\"go.mod\",\"VERSION\", \"version.txt\"]") + err = validationBuildDescriptorFile("golang", "/home/go.mod") + assert.NoError(t, err) + err = validationBuildDescriptorFile("golang", "/home/VERSION") + assert.NoError(t, err) + err = validationBuildDescriptorFile("golang", "/home/version.txt") + assert.NoError(t, err) + }) + t.Run("buildTool = maven", func(t *testing.T) { + err := validationBuildDescriptorFile("maven", "/home/go.mod") + assert.ErrorContains(t, err, "extension of buildDescriptorFile must be in '*.xml'") + err = validationBuildDescriptorFile("maven", "/home/pom.xml") + assert.NoError(t, err) + }) + t.Run("buildTool = mta", func(t *testing.T) { + err := validationBuildDescriptorFile("mta", "/home/go.mod") + assert.ErrorContains(t, err, "extension of buildDescriptorFile must be in '*.yaml'") + err = validationBuildDescriptorFile("mta", "/home/mta.yaml") + assert.NoError(t, err) + }) + t.Run("buildTool = npm", func(t *testing.T) { + err := validationBuildDescriptorFile("npm", "/home/go.mod") + assert.ErrorContains(t, err, "extension of buildDescriptorFile must be in '*.json'") + err = validationBuildDescriptorFile("npm", "/home/package.json") + assert.NoError(t, err) + }) + t.Run("buildTool = yarn", func(t *testing.T) { + err := validationBuildDescriptorFile("yarn", "/home/go.mod") + assert.ErrorContains(t, err, "extension of buildDescriptorFile must be in '*.json'") + err = validationBuildDescriptorFile("yarn", "/home/package.json") + assert.NoError(t, err) + }) + t.Run("buildTool = pip", func(t *testing.T) { + err := validationBuildDescriptorFile("pip", "/home/go.mod") + assert.ErrorContains(t, err, "buildDescriptorFile must be one of [\"setup.py\",\"version.txt\", \"VERSION\"]") + err = validationBuildDescriptorFile("pip", "/home/setup.py") + assert.NoError(t, err) + err = validationBuildDescriptorFile("pip", "/home/version.txt") + assert.NoError(t, err) + err = validationBuildDescriptorFile("pip", "/home/VERSION") + assert.NoError(t, err) + }) + t.Run("buildTool = sbt", func(t *testing.T) { + err := validationBuildDescriptorFile("sbt", "/home/go.mod") + assert.ErrorContains(t, err, "extension of buildDescriptorFile must be in '*.json'") + err = validationBuildDescriptorFile("sbt", "/home/sbtDescriptor.json") + assert.NoError(t, err) + err = validationBuildDescriptorFile("sbt", "/home/build.sbt") + assert.NoError(t, err) + }) +} From dfd947b7e15b67656aa50d30b16514aa3b320fd2 Mon Sep 17 00:00:00 2001 From: Valentin <16714035+bitmaskit@users.noreply.github.com> Date: Thu, 12 Dec 2024 17:32:12 +0200 Subject: [PATCH 08/12] Update go version to 1.23 and removed some helper methods in favor of STD library (#5200) * update go version * fix go vet errors * replaces stringutils with slices std package * added slices package * 1.22.4 to 1.23.4 --------- Co-authored-by: Valentin --- .editorconfig | 1 - .github/workflows/documentation.yml | 2 +- .github/workflows/update-go-dependencies.yml | 2 +- .github/workflows/upload-go-master.yml | 2 +- .github/workflows/verify-go.yml | 10 ++--- Dockerfile | 2 +- cmd/cloudFoundryDeploy.go | 3 +- cmd/cnbBuild.go | 3 +- cmd/detectExecuteScan.go | 3 +- cmd/fortifyExecuteScan.go | 3 +- cmd/fortifyExecuteScan_test.go | 4 +- cmd/kanikoExecute.go | 3 +- cmd/newmanExecute_test.go | 4 +- cmd/terraformExecute.go | 7 ++-- go.mod | 2 +- .../golang-project1/go.mod | 2 +- .../golang-project2/go.mod | 2 +- pkg/blackduck/reporting.go | 5 ++- pkg/blackduck/reporting_test.go | 5 ++- pkg/checkmarx/checkmarx.go | 4 +- pkg/checkmarxone/checkmarxone.go | 4 +- pkg/config/evaluation.go | 3 +- pkg/docker/docker.go | 3 +- pkg/documentation/generator/description.go | 8 ++-- pkg/fortify/fortify.go | 8 ++-- pkg/generator/helper/helper.go | 4 +- pkg/maven/model.go | 4 +- pkg/maven/settings.go | 9 ++--- pkg/mock/fileUtils.go | 6 +-- pkg/multiarch/multiarch.go | 6 +-- pkg/npm/publish.go | 4 +- pkg/npm/publish_test.go | 6 +-- pkg/piperutils/maps_test.go | 17 +++++---- pkg/piperutils/slices.go | 38 +++---------------- pkg/piperutils/slices_test.go | 35 ----------------- pkg/whitesource/reporting.go | 3 +- src/com/sap/piper/PiperGoUtils.groovy | 2 +- 37 files changed, 89 insertions(+), 140 deletions(-) diff --git a/.editorconfig b/.editorconfig index 0aa9c24746..fb326c32c8 100644 --- a/.editorconfig +++ b/.editorconfig @@ -24,4 +24,3 @@ indent_style = none indent_size = none [{go.mod,go.sum,*.go,*.golden}] indent_style = tab -indent_size = 8 diff --git a/.github/workflows/documentation.yml b/.github/workflows/documentation.yml index 10febb1bb3..132997c45b 100644 --- a/.github/workflows/documentation.yml +++ b/.github/workflows/documentation.yml @@ -23,7 +23,7 @@ jobs: - uses: actions/setup-go@v5 with: - go-version: '1.22.4' + go-version: '1.23.4' - name: Install Groovy run: sudo apt-get update && sudo apt-get install groovy -y diff --git a/.github/workflows/update-go-dependencies.yml b/.github/workflows/update-go-dependencies.yml index fe7f350d3a..0157cbb697 100644 --- a/.github/workflows/update-go-dependencies.yml +++ b/.github/workflows/update-go-dependencies.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: '1.22.4' + go-version: '1.23.4' - name: Perform update run: | git checkout -B gh-action-update-golang-dependencies diff --git a/.github/workflows/upload-go-master.yml b/.github/workflows/upload-go-master.yml index 9f88021517..b56c1f8771 100644 --- a/.github/workflows/upload-go-master.yml +++ b/.github/workflows/upload-go-master.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: '1.22.4' + go-version: '1.23.4' - env: CGO_ENABLED: 0 run: | diff --git a/.github/workflows/verify-go.yml b/.github/workflows/verify-go.yml index 8226c83165..715e87f990 100644 --- a/.github/workflows/verify-go.yml +++ b/.github/workflows/verify-go.yml @@ -15,7 +15,7 @@ jobs: - uses: styfle/cancel-workflow-action@0.11.0 - uses: actions/setup-go@v5 with: - go-version: '1.22.4' + go-version: '1.23.4' - name: Cache Golang Packages uses: actions/cache@v3 with: @@ -43,7 +43,7 @@ jobs: steps: - uses: actions/setup-go@v5 with: - go-version: '1.22.4' + go-version: '1.23.4' - name: Cache Golang Packages uses: actions/cache@v3 with: @@ -63,7 +63,7 @@ jobs: steps: - uses: actions/setup-go@v5 with: - go-version: '1.22.4' + go-version: '1.23.4' - name: checkout uses: actions/checkout@v4 with: @@ -78,7 +78,7 @@ jobs: steps: - uses: actions/setup-go@v5 with: - go-version: '1.22.4' + go-version: '1.23.4' - name: Cache Golang Packages uses: actions/cache@v3 with: @@ -98,7 +98,7 @@ jobs: steps: - uses: actions/setup-go@v5 with: - go-version: '1.22.4' + go-version: '1.23.4' - name: Cache Golang Packages uses: actions/cache@v3 with: diff --git a/Dockerfile b/Dockerfile index 7a44ad5c56..1b6639dcb8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.22.4 AS build-env +FROM golang:1.23.4 AS build-env COPY . /build WORKDIR /build diff --git a/cmd/cloudFoundryDeploy.go b/cmd/cloudFoundryDeploy.go index 79a4e19aa2..6e9ac13662 100644 --- a/cmd/cloudFoundryDeploy.go +++ b/cmd/cloudFoundryDeploy.go @@ -6,6 +6,7 @@ import ( "io" "os" "regexp" + "slices" "sort" "strings" "time" @@ -414,7 +415,7 @@ func deployMta(config *cloudFoundryDeployOptions, mtarFilePath string, command c deployCommand = "bg-deploy" const noConfirmFlag = "--no-confirm" - if !piperutils.ContainsString(deployParams, noConfirmFlag) { + if !slices.Contains(deployParams, noConfirmFlag) { deployParams = append(deployParams, noConfirmFlag) } } diff --git a/cmd/cnbBuild.go b/cmd/cnbBuild.go index 1bc012df79..3fea419bf9 100644 --- a/cmd/cnbBuild.go +++ b/cmd/cnbBuild.go @@ -6,6 +6,7 @@ import ( "os" "path" "path/filepath" + "slices" "github.com/SAP/jenkins-library/pkg/buildpacks" "github.com/SAP/jenkins-library/pkg/buildsettings" @@ -594,7 +595,7 @@ func runCnbBuild(config *cnbBuildOptions, telemetry *buildpacks.Telemetry, image } for _, tag := range config.AdditionalTags { target := fmt.Sprintf("%s:%s", containerImage, tag) - if !piperutils.ContainsString(creatorArgs, target) { + if !slices.Contains(creatorArgs, target) { creatorArgs = append(creatorArgs, "-tag", target) } } diff --git a/cmd/detectExecuteScan.go b/cmd/detectExecuteScan.go index 5fc5561a10..9dd948f7a8 100644 --- a/cmd/detectExecuteScan.go +++ b/cmd/detectExecuteScan.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "path/filepath" + "slices" "sort" "strconv" "strings" @@ -467,7 +468,7 @@ func addDetectArgs(args []string, config detectExecuteScanOptions, utils detectU handleExcludedDirectories(&args, &config) if config.Unmap { - if !piperutils.ContainsString(config.ScanProperties, "--detect.project.codelocation.unmap=true") { + if !slices.Contains(config.ScanProperties, "--detect.project.codelocation.unmap=true") { args = append(args, "--detect.project.codelocation.unmap=true") } config.ScanProperties, _ = piperutils.RemoveAll(config.ScanProperties, "--detect.project.codelocation.unmap=false") diff --git a/cmd/fortifyExecuteScan.go b/cmd/fortifyExecuteScan.go index f2110fa552..ac8898a728 100644 --- a/cmd/fortifyExecuteScan.go +++ b/cmd/fortifyExecuteScan.go @@ -11,6 +11,7 @@ import ( "path/filepath" "regexp" "runtime" + "slices" "strconv" "strings" "time" @@ -211,7 +212,7 @@ func runFortifyScan(ctx context.Context, config fortifyExecuteScanOptions, sys f prID, prAuthor := determinePullRequestMerge(config) if prID != "0" { log.Entry().Debugf("Determined PR ID '%v' for merge check", prID) - if len(prAuthor) > 0 && !piperutils.ContainsString(config.Assignees, prAuthor) { + if len(prAuthor) > 0 && !slices.Contains(config.Assignees, prAuthor) { log.Entry().Debugf("Determined PR Author '%v' for result assignment", prAuthor) config.Assignees = append(config.Assignees, prAuthor) } else { diff --git a/cmd/fortifyExecuteScan_test.go b/cmd/fortifyExecuteScan_test.go index 3f47ec3a9d..b5933e46c7 100644 --- a/cmd/fortifyExecuteScan_test.go +++ b/cmd/fortifyExecuteScan_test.go @@ -13,6 +13,7 @@ import ( "os" "path/filepath" "reflect" + "slices" "strings" "testing" "time" @@ -21,7 +22,6 @@ import ( "github.com/SAP/jenkins-library/pkg/fortify" "github.com/SAP/jenkins-library/pkg/log" - "github.com/SAP/jenkins-library/pkg/piperutils" "github.com/SAP/jenkins-library/pkg/versioning" "github.com/google/go-github/v45/github" @@ -421,7 +421,7 @@ func (er *execRunnerMock) Stderr(err io.Writer) { func (er *execRunnerMock) RunExecutable(e string, p ...string) error { er.numExecutions++ er.currentExecution().executable = e - if len(p) > 0 && piperutils.ContainsString(p, "--failTranslate") { + if len(p) > 0 && slices.Contains(p, "--failTranslate") { return errors.New("Translate failed") } er.currentExecution().parameters = p diff --git a/cmd/kanikoExecute.go b/cmd/kanikoExecute.go index 1937dd66af..3eaf4c9180 100644 --- a/cmd/kanikoExecute.go +++ b/cmd/kanikoExecute.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "slices" "strings" "github.com/mitchellh/mapstructure" @@ -287,7 +288,7 @@ func runKanikoExecute(config *kanikoExecuteOptions, telemetryData *telemetry.Cus } return nil - case piperutils.ContainsString(config.BuildOptions, "--destination"): + case slices.Contains(config.BuildOptions, "--destination"): log.Entry().Infof("Running Kaniko build with destination defined via buildOptions: %v", config.BuildOptions) for i, o := range config.BuildOptions { diff --git a/cmd/newmanExecute_test.go b/cmd/newmanExecute_test.go index f209005dbf..ffb7429883 100644 --- a/cmd/newmanExecute_test.go +++ b/cmd/newmanExecute_test.go @@ -6,10 +6,10 @@ package cmd import ( "os" "path/filepath" + "slices" "strings" "testing" - sliceUtils "github.com/SAP/jenkins-library/pkg/piperutils" "github.com/google/uuid" "github.com/pkg/errors" "github.com/stretchr/testify/assert" @@ -317,7 +317,7 @@ func (e *newmanExecuteMockUtils) RunExecutable(executable string, params ...stri if e.errorOnNewmanExecution && strings.Contains(executable, "newman") { return errors.New("error on newman execution") } - if e.errorOnNewmanInstall && sliceUtils.ContainsString(params, "install") { + if e.errorOnNewmanInstall && slices.Contains(params, "install") { return errors.New("error on newman install") } diff --git a/cmd/terraformExecute.go b/cmd/terraformExecute.go index e85344b729..bd2b8fa4fe 100644 --- a/cmd/terraformExecute.go +++ b/cmd/terraformExecute.go @@ -3,6 +3,7 @@ package cmd import ( "bytes" "fmt" + "slices" "github.com/SAP/jenkins-library/pkg/command" "github.com/SAP/jenkins-library/pkg/log" @@ -53,15 +54,15 @@ func runTerraformExecute(config *terraformExecuteOptions, telemetryData *telemet args := []string{} - if piperutils.ContainsString([]string{"apply", "destroy"}, config.Command) { + if slices.Contains([]string{"apply", "destroy"}, config.Command) { args = append(args, "-auto-approve") } - if piperutils.ContainsString([]string{"apply", "plan"}, config.Command) && config.TerraformSecrets != "" { + if slices.Contains([]string{"apply", "plan"}, config.Command) && config.TerraformSecrets != "" { args = append(args, fmt.Sprintf("-var-file=%s", config.TerraformSecrets)) } - if piperutils.ContainsString([]string{"init", "validate", "plan", "apply", "destroy"}, config.Command) { + if slices.Contains([]string{"init", "validate", "plan", "apply", "destroy"}, config.Command) { args = append(args, "-no-color") } diff --git a/go.mod b/go.mod index 426478796e..847e98c055 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/SAP/jenkins-library -go 1.22.4 +go 1.23.4 require ( cloud.google.com/go/pubsub v1.36.1 diff --git a/integration/testdata/TestGolangIntegration/golang-project1/go.mod b/integration/testdata/TestGolangIntegration/golang-project1/go.mod index abe4430d88..c538e6fc76 100644 --- a/integration/testdata/TestGolangIntegration/golang-project1/go.mod +++ b/integration/testdata/TestGolangIntegration/golang-project1/go.mod @@ -1,5 +1,5 @@ module github.com/example/golang-app -go 1.22.4 +go 1.23.4 require github.com/gorilla/mux v1.8.0 diff --git a/integration/testdata/TestGolangIntegration/golang-project2/go.mod b/integration/testdata/TestGolangIntegration/golang-project2/go.mod index e0e21c40a1..d07e0384a1 100644 --- a/integration/testdata/TestGolangIntegration/golang-project2/go.mod +++ b/integration/testdata/TestGolangIntegration/golang-project2/go.mod @@ -1,3 +1,3 @@ module github.com/example/golang-app -go 1.22.4 +go 1.23.4 diff --git a/pkg/blackduck/reporting.go b/pkg/blackduck/reporting.go index 960c0c3357..a1faa41f90 100644 --- a/pkg/blackduck/reporting.go +++ b/pkg/blackduck/reporting.go @@ -6,6 +6,7 @@ import ( "fmt" "path/filepath" "runtime" + "slices" "strings" "github.com/SAP/jenkins-library/pkg/format" @@ -89,12 +90,12 @@ func CreateSarifResultFile(vulns *Vulnerabilities, projectName, projectVersion, results = append(results, result) // append taxonomies - if len(v.VulnerabilityWithRemediation.CweID) > 0 && !piperutils.ContainsString(cweIdsForTaxonomies, v.VulnerabilityWithRemediation.CweID) { + if len(v.VulnerabilityWithRemediation.CweID) > 0 && !slices.Contains(cweIdsForTaxonomies, v.VulnerabilityWithRemediation.CweID) { cweIdsForTaxonomies = append(cweIdsForTaxonomies, v.VulnerabilityWithRemediation.CweID) } // only create rule on new CVE - if !piperutils.ContainsString(collectedRules, result.RuleID) { + if !slices.Contains(collectedRules, result.RuleID) { collectedRules = append(collectedRules, result.RuleID) // set information about BlackDuck project diff --git a/pkg/blackduck/reporting_test.go b/pkg/blackduck/reporting_test.go index e0c86bc35e..6e29d3c952 100644 --- a/pkg/blackduck/reporting_test.go +++ b/pkg/blackduck/reporting_test.go @@ -6,6 +6,7 @@ package blackduck import ( "fmt" "path/filepath" + "slices" "testing" "github.com/SAP/jenkins-library/pkg/format" @@ -132,13 +133,13 @@ func TestCreateSarifResultFile(t *testing.T) { collectedRules := []string{} for _, rule := range sarif.Runs[0].Tool.Driver.Rules { - piperutils.ContainsString(vulnerabilities, rule.ID) + slices.Contains(vulnerabilities, rule.ID) collectedRules = append(collectedRules, rule.ID) } collectedResults := []string{} for _, result := range sarif.Runs[0].Results { - piperutils.ContainsString(vulnerabilities, result.RuleID) + slices.Contains(vulnerabilities, result.RuleID) collectedResults = append(collectedResults, result.RuleID) } diff --git a/pkg/checkmarx/checkmarx.go b/pkg/checkmarx/checkmarx.go index e8c4c552df..bccc87a463 100644 --- a/pkg/checkmarx/checkmarx.go +++ b/pkg/checkmarx/checkmarx.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "net/url" + "slices" "strconv" "strings" "time" @@ -15,7 +16,6 @@ import ( piperHttp "github.com/SAP/jenkins-library/pkg/http" "github.com/SAP/jenkins-library/pkg/log" - "github.com/SAP/jenkins-library/pkg/piperutils" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -301,7 +301,7 @@ func sendRequestInternal(sys *SystemInstance, method, url string, body io.Reader defer closer.Close() } response, err := sys.client.SendRequest(method, fmt.Sprintf("%v/cxrestapi%v", sys.serverURL, url), requestBody, header, nil) - if err != nil && (response == nil || !piperutils.ContainsInt(acceptedErrorCodes, response.StatusCode)) { + if err != nil && (response == nil || !slices.Contains(acceptedErrorCodes, response.StatusCode)) { sys.recordRequestDetailsInErrorCase(requestBodyCopy, response) sys.logger.Errorf("HTTP request failed with error: %s", err) return nil, err diff --git a/pkg/checkmarxone/checkmarxone.go b/pkg/checkmarxone/checkmarxone.go index 6d5d14ab54..8f63053fb2 100644 --- a/pkg/checkmarxone/checkmarxone.go +++ b/pkg/checkmarxone/checkmarxone.go @@ -8,13 +8,13 @@ import ( "net/http" "net/url" "os" + "slices" "strconv" "strings" "time" piperHttp "github.com/SAP/jenkins-library/pkg/http" "github.com/SAP/jenkins-library/pkg/log" - "github.com/SAP/jenkins-library/pkg/piperutils" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -410,7 +410,7 @@ func sendRequestInternal(sys *SystemInstance, method, url string, body io.Reader //header.Set("User-Agent", "Project-Piper.io cicd pipeline") // currently this causes some requests to fail due to unknown UA validation in the backend. response, err := sys.client.SendRequest(method, url, requestBody, header, nil) - if err != nil && (response == nil || !piperutils.ContainsInt(acceptedErrorCodes, response.StatusCode)) { + if err != nil && (response == nil || !slices.Contains(acceptedErrorCodes, response.StatusCode)) { var resBodyBytes []byte if response != nil && response.Body != nil { diff --git a/pkg/config/evaluation.go b/pkg/config/evaluation.go index 4482a2d296..af90ac9059 100644 --- a/pkg/config/evaluation.go +++ b/pkg/config/evaluation.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "path" + "slices" "strings" "github.com/pkg/errors" @@ -49,7 +50,7 @@ func (r *RunConfigV1) evaluateConditionsV1(config *Config, utils piperutils.File stepConfigCache := make(map[string]StepConfig, len(stage.Steps)) for _, step := range stage.Steps { // Consider only orchestrator-specific steps if the orchestrator limitation is set. - if len(step.Orchestrators) > 0 && !piperutils.ContainsString(step.Orchestrators, currentOrchestrator) { + if len(step.Orchestrators) > 0 && !slices.Contains(step.Orchestrators, currentOrchestrator) { continue } diff --git a/pkg/docker/docker.go b/pkg/docker/docker.go index d4f757e00c..30e287abf4 100644 --- a/pkg/docker/docker.go +++ b/pkg/docker/docker.go @@ -9,6 +9,7 @@ import ( "path" "path/filepath" "regexp" + "slices" "strings" "github.com/docker/cli/cli/config" @@ -306,7 +307,7 @@ func ImageListWithFilePath(imageName string, excludes []string, trimDir string, // ToDo: needs rework // dockerfilePath = strings.ReplaceAll(dockerfilePath, cwd, ".") - if piperutils.ContainsString(excludes, dockerfilePath) { + if slices.Contains(excludes, dockerfilePath) { log.Entry().Infof("Discard %v since it is in the exclude list %v", dockerfilePath, excludes) continue } diff --git a/pkg/documentation/generator/description.go b/pkg/documentation/generator/description.go index 744a15d1b9..022833caae 100644 --- a/pkg/documentation/generator/description.go +++ b/pkg/documentation/generator/description.go @@ -29,10 +29,10 @@ var CustomLibrarySteps = []CustomLibrary{} // CustomLibrary represents a custom library with it's custom step names, binary name and library name. type CustomLibrary struct { - Name string `yaml: "name,omitempty"` - BinaryName string `yaml: "binaryName,omitempty"` - LibraryName string `yaml: "libraryName,omitempty"` - Steps []string `yaml: "steps,omitempty"` + Name string `yaml:"name,omitempty"` + BinaryName string `yaml:"binaryName,omitempty"` + LibraryName string `yaml:"libraryName,omitempty"` + Steps []string `yaml:"steps,omitempty"` } // Replaces the StepName placeholder with the content from the yaml diff --git a/pkg/fortify/fortify.go b/pkg/fortify/fortify.go index 6d03e8e3fd..58952311d2 100644 --- a/pkg/fortify/fortify.go +++ b/pkg/fortify/fortify.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "os" + "slices" "strings" "time" @@ -30,7 +31,6 @@ import ( piperHttp "github.com/SAP/jenkins-library/pkg/http" "github.com/SAP/jenkins-library/pkg/log" - "github.com/SAP/jenkins-library/pkg/piperutils" "github.com/go-openapi/runtime" "github.com/go-openapi/strfmt" @@ -529,7 +529,7 @@ func (sys *SystemInstance) ReduceIssueFilterSelectorSet(issueFilterSelectorSet * groupingList := []*models.IssueSelector{} if issueFilterSelectorSet.GroupBySet != nil { for _, group := range issueFilterSelectorSet.GroupBySet { - if piperutils.ContainsString(names, *group.DisplayName) { + if slices.Contains(names, *group.DisplayName) { log.Entry().Debugf("adding new grouping '%v' to reduced list", *group.DisplayName) groupingList = append(groupingList, group) } @@ -538,7 +538,7 @@ func (sys *SystemInstance) ReduceIssueFilterSelectorSet(issueFilterSelectorSet * filterList := []*models.IssueFilterSelector{} if issueFilterSelectorSet.FilterBySet != nil { for _, filter := range issueFilterSelectorSet.FilterBySet { - if piperutils.ContainsString(names, filter.DisplayName) { + if slices.Contains(names, filter.DisplayName) { newFilter := &models.IssueFilterSelector{} newFilter.DisplayName = filter.DisplayName newFilter.Description = filter.Description @@ -548,7 +548,7 @@ func (sys *SystemInstance) ReduceIssueFilterSelectorSet(issueFilterSelectorSet * newFilter.Value = filter.Value newFilter.SelectorOptions = []*models.SelectorOption{} for _, option := range filter.SelectorOptions { - if (nil != options && piperutils.ContainsString(options, option.DisplayName)) || options == nil || len(options) == 0 { + if (nil != options && slices.Contains(options, option.DisplayName)) || options == nil || len(options) == 0 { log.Entry().Debugf("adding selector option '%v' to list for filter selector '%v'", option.DisplayName, newFilter.DisplayName) newFilter.SelectorOptions = append(newFilter.SelectorOptions, option) } diff --git a/pkg/generator/helper/helper.go b/pkg/generator/helper/helper.go index 00301040e4..1a29d49a3c 100644 --- a/pkg/generator/helper/helper.go +++ b/pkg/generator/helper/helper.go @@ -7,11 +7,11 @@ import ( "os" "path/filepath" "reflect" + "slices" "strings" "text/template" "github.com/Masterminds/sprig" - "github.com/SAP/jenkins-library/pkg/config" "github.com/SAP/jenkins-library/pkg/piperutils" ) @@ -924,7 +924,7 @@ func mustUniqName(list []config.StepParameters) ([]config.StepParameters, error) var item config.StepParameters for i := 0; i < l; i++ { item = l2.Index(i).Interface().(config.StepParameters) - if !piperutils.ContainsString(names, item.Name) { + if !slices.Contains(names, item.Name) { names = append(names, item.Name) dest = append(dest, item) } diff --git a/pkg/maven/model.go b/pkg/maven/model.go index 2a4f9b74b2..9c5d72c273 100644 --- a/pkg/maven/model.go +++ b/pkg/maven/model.go @@ -3,8 +3,8 @@ package maven import ( "encoding/xml" "fmt" - "github.com/SAP/jenkins-library/pkg/piperutils" "path/filepath" + "slices" ) // Project describes the Maven object model. @@ -71,7 +71,7 @@ type visitUtils interface { // VisitAllMavenModules ... func VisitAllMavenModules(path string, utils visitUtils, excludes []string, callback func(info ModuleInfo) error) error { pomXMLPath := filepath.Join(path, "pom.xml") - if piperutils.ContainsString(excludes, pomXMLPath) { + if slices.Contains(excludes, pomXMLPath) { return nil } diff --git a/pkg/maven/settings.go b/pkg/maven/settings.go index 151b516ca9..880039a08a 100644 --- a/pkg/maven/settings.go +++ b/pkg/maven/settings.go @@ -218,7 +218,7 @@ func addServerTagtoProjectSettingsXML(projectSettingsFile string, altDeploymentR settingsXml, err := xml.MarshalIndent(projectSettings, "", " ") if err != nil { - fmt.Errorf("failed to marshal maven project settings xml: %w", err) + return fmt.Errorf("failed to marshal maven project settings xml: %w", err) } settingsXmlString := string(settingsXml) Replacer := strings.NewReplacer(" ", "", " ", "") @@ -226,12 +226,11 @@ func addServerTagtoProjectSettingsXML(projectSettingsFile string, altDeploymentR xmlstring := []byte(xml.Header + settingsXmlString) - err = utils.FileWrite(projectSettingsFile, xmlstring, 0777) - if err != nil { - fmt.Errorf("failed to write maven Settings xml: %w", err) + if err = utils.FileWrite(projectSettingsFile, xmlstring, 0777); err != nil { + return fmt.Errorf("failed to write maven Settings xml: %w", err) } - log.Entry().Infof("Successfully updated details in maven project settings file : '%s'", projectSettingsFile) + log.Entry().Infof("Successfully updated details in maven project settings file : '%s'", projectSettingsFile) return nil } diff --git a/pkg/mock/fileUtils.go b/pkg/mock/fileUtils.go index b8d13ff6cd..3c368aa67a 100644 --- a/pkg/mock/fileUtils.go +++ b/pkg/mock/fileUtils.go @@ -13,11 +13,11 @@ import ( "io" "os" "path/filepath" + "slices" "sort" "strings" "time" - "github.com/SAP/jenkins-library/pkg/piperutils" "github.com/bmatcuk/doublestar" ) @@ -158,13 +158,13 @@ func (f *FilesMock) HasFile(path string) bool { // HasRemovedFile returns true if the virtual file system at one point contained an entry for the given path, // and it was removed via FileRemove(). func (f *FilesMock) HasRemovedFile(path string) bool { - return piperutils.ContainsString(f.removedFiles, f.toAbsPath(path)) + return slices.Contains(f.removedFiles, f.toAbsPath(path)) } // HasWrittenFile returns true if the virtual file system at one point contained an entry for the given path, // and it was written via FileWrite(). func (f *FilesMock) HasWrittenFile(path string) bool { - return piperutils.ContainsString(f.writtenFiles, f.toAbsPath(path)) + return slices.Contains(f.writtenFiles, f.toAbsPath(path)) } // HasCopiedFile returns true if the virtual file system at one point contained an entry for the given source and destination, diff --git a/pkg/multiarch/multiarch.go b/pkg/multiarch/multiarch.go index ce9c6cfa00..be2291651f 100644 --- a/pkg/multiarch/multiarch.go +++ b/pkg/multiarch/multiarch.go @@ -3,10 +3,10 @@ package multiarch import ( "fmt" "regexp" + "slices" "strings" "github.com/SAP/jenkins-library/pkg/log" - "github.com/SAP/jenkins-library/pkg/piperutils" ) var knownGoos = []string{"aix", "android", "darwin", "dragonfly", "freebsd", "hurd", "illumos", "ios", "js", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows", "zos"} @@ -42,13 +42,13 @@ func ParsePlatformString(s string) (Platform, error) { p.OS = strings.Trim(matches[1], " ") - if !piperutils.ContainsString(knownGoos, p.OS) { + if !slices.Contains(knownGoos, p.OS) { log.Entry().Warningf("OS '%s' is unknown to us", p.OS) } p.Arch = strings.Trim(matches[2], " ") - if !piperutils.ContainsString(knownGoarch, p.Arch) { + if !slices.Contains(knownGoarch, p.Arch) { log.Entry().Warningf("Architecture '%s' is unknown to us", p.Arch) } diff --git a/pkg/npm/publish.go b/pkg/npm/publish.go index a5626281b3..212d62cf84 100644 --- a/pkg/npm/publish.go +++ b/pkg/npm/publish.go @@ -16,8 +16,8 @@ import ( ) type npmMinimalPackageDescriptor struct { - Name string `json:version` - Version string `json:version` + Name string `json:"name"` + Version string `json:"version"` } func (pd *npmMinimalPackageDescriptor) Scope() string { diff --git a/pkg/npm/publish_test.go b/pkg/npm/publish_test.go index bdbdf8b655..a08d7e7db6 100644 --- a/pkg/npm/publish_test.go +++ b/pkg/npm/publish_test.go @@ -6,11 +6,11 @@ package npm import ( "io" "path/filepath" + "slices" "testing" "github.com/SAP/jenkins-library/pkg/mock" - "github.com/SAP/jenkins-library/pkg/piperutils" "github.com/SAP/jenkins-library/pkg/versioning" "github.com/stretchr/testify/assert" ) @@ -548,12 +548,12 @@ func TestNpmPublish(t *testing.T) { assert.Equal(t, "publish", publishCmd.Params[0]) if len(test.wants.tarballPath) > 0 && assert.Contains(t, publishCmd.Params, "--tarball") { - tarballPath := publishCmd.Params[piperutils.FindString(publishCmd.Params, "--tarball")+1] + tarballPath := publishCmd.Params[slices.Index(publishCmd.Params, "--tarball")+1] assert.Equal(t, test.wants.tarballPath, filepath.ToSlash(tarballPath)) } if assert.Contains(t, publishCmd.Params, "--userconfig") { - effectivePublishConfigPath := publishCmd.Params[piperutils.FindString(publishCmd.Params, "--userconfig")+1] + effectivePublishConfigPath := publishCmd.Params[slices.Index(publishCmd.Params, "--userconfig")+1] assert.Regexp(t, test.wants.publishConfigPath, filepath.ToSlash(effectivePublishConfigPath)) diff --git a/pkg/piperutils/maps_test.go b/pkg/piperutils/maps_test.go index f36ab43f5c..af8472ecb0 100644 --- a/pkg/piperutils/maps_test.go +++ b/pkg/piperutils/maps_test.go @@ -4,6 +4,7 @@ package piperutils import ( + "slices" "testing" "github.com/stretchr/testify/assert" @@ -15,10 +16,10 @@ func TestKeys(t *testing.T) { intList := Keys(intStringMap) assert.Equal(t, 4, len(intList)) - assert.Equal(t, true, ContainsInt(intList, 1)) - assert.Equal(t, true, ContainsInt(intList, 2)) - assert.Equal(t, true, ContainsInt(intList, 3)) - assert.Equal(t, true, ContainsInt(intList, 4)) + assert.Equal(t, true, slices.Contains(intList, 1)) + assert.Equal(t, true, slices.Contains(intList, 2)) + assert.Equal(t, true, slices.Contains(intList, 3)) + assert.Equal(t, true, slices.Contains(intList, 4)) } func TestValues(t *testing.T) { @@ -27,8 +28,8 @@ func TestValues(t *testing.T) { intList := Values(intStringMap) assert.Equal(t, 4, len(intList)) - assert.Equal(t, true, ContainsString(intList, "eins")) - assert.Equal(t, true, ContainsString(intList, "zwei")) - assert.Equal(t, true, ContainsString(intList, "drei")) - assert.Equal(t, true, ContainsString(intList, "vier")) + assert.Equal(t, true, slices.Contains(intList, "eins")) + assert.Equal(t, true, slices.Contains(intList, "zwei")) + assert.Equal(t, true, slices.Contains(intList, "drei")) + assert.Equal(t, true, slices.Contains(intList, "vier")) } diff --git a/pkg/piperutils/slices.go b/pkg/piperutils/slices.go index 5b7ea72131..c5f7bbf49b 100644 --- a/pkg/piperutils/slices.go +++ b/pkg/piperutils/slices.go @@ -5,32 +5,6 @@ import ( "strings" ) -// ContainsInt checks whether the element is part of the slice -func ContainsInt(s []int, e int) bool { - for _, a := range s { - if a == e { - return true - } - } - return false -} - -// ContainsString checks whether the element is part of the slice -func ContainsString(s []string, e string) bool { - return FindString(s, e) >= 0 -} - -// FindString returns the position of element e in the given slice or -1 if it's not in -func FindString(s []string, e string) int { - for i, a := range s { - if a == e { - return i - } - } - - return -1 -} - // ContainsStringPart checks whether the element is contained as part of one of the elements of the slice func ContainsStringPart(s []string, part string) bool { for _, a := range s { @@ -43,14 +17,14 @@ func ContainsStringPart(s []string, part string) bool { // RemoveAll removes all instances of element from the slice and returns a truncated slice as well as // a boolean to indicate whether at least one element was found and removed. -func RemoveAll(s []string, e string) ([]string, bool) { - var r []string - for _, a := range s { - if a != e { - r = append(r, a) +func RemoveAll(arr []string, item string) ([]string, bool) { + res := make([]string, 0, len(arr)) + for _, a := range arr { + if a != item { + res = append(res, a) } } - return r, len(s) != len(r) + return res, len(arr) != len(res) } // Prefix adds a prefix to each element of the slice diff --git a/pkg/piperutils/slices_test.go b/pkg/piperutils/slices_test.go index bd10429153..fd156ad019 100644 --- a/pkg/piperutils/slices_test.go +++ b/pkg/piperutils/slices_test.go @@ -9,41 +9,6 @@ import ( "github.com/stretchr/testify/assert" ) -func TestContainsInt(t *testing.T) { - var intList []int - assert.Equal(t, false, ContainsInt(intList, 4)) - - intList = append(intList, 1, 2, 3, 4, 5, 6, 20) - assert.Equal(t, true, ContainsInt(intList, 20)) - assert.Equal(t, true, ContainsInt(intList, 1)) - assert.Equal(t, true, ContainsInt(intList, 4)) - assert.Equal(t, false, ContainsInt(intList, 13)) -} - -func TestContainsString(t *testing.T) { - var stringList []string - assert.False(t, ContainsString(stringList, "test")) - assert.False(t, ContainsString(stringList, "")) - - stringList = append(stringList, "", "foo", "bar", "foo") - assert.True(t, ContainsString(stringList, "")) - assert.True(t, ContainsString(stringList, "bar")) - assert.True(t, ContainsString(stringList, "foo")) - assert.False(t, ContainsString(stringList, "baz")) -} - -func TestFindString(t *testing.T) { - var stringList []string - assert.Equal(t, -1, FindString(stringList, "test")) - assert.Equal(t, -1, FindString(stringList, "")) - - stringList = append(stringList, "", "foo", "bar", "foo") - assert.Equal(t, 0, FindString(stringList, "")) - assert.Equal(t, 2, FindString(stringList, "bar")) - assert.Equal(t, 1, FindString(stringList, "foo")) - assert.Equal(t, -1, FindString(stringList, "baz")) -} - func TestRemoveAll(t *testing.T) { t.Parallel() t.Run("empty array", func(t *testing.T) { diff --git a/pkg/whitesource/reporting.go b/pkg/whitesource/reporting.go index 56e5cefa0d..03c956f32f 100644 --- a/pkg/whitesource/reporting.go +++ b/pkg/whitesource/reporting.go @@ -8,6 +8,7 @@ import ( "fmt" "path/filepath" "runtime" + "slices" "sort" "strings" "time" @@ -217,7 +218,7 @@ func CreateSarifResultFile(scan *Scan, alerts *[]Alert) *format.SARIF { sarif.Runs[0].Results = append(sarif.Runs[0].Results, result) // only create rule on new CVE - if !piperutils.ContainsString(collectedRules, ruleId) { + if !slices.Contains(collectedRules, ruleId) { collectedRules = append(collectedRules, ruleId) sarifRule := *new(format.SarifRule) diff --git a/src/com/sap/piper/PiperGoUtils.groovy b/src/com/sap/piper/PiperGoUtils.groovy index 3c2f63036e..f4765de7da 100644 --- a/src/com/sap/piper/PiperGoUtils.groovy +++ b/src/com/sap/piper/PiperGoUtils.groovy @@ -30,7 +30,7 @@ class PiperGoUtils implements Serializable { if (steps.env.REPOSITORY_UNDER_TEST && steps.env.LIBRARY_VERSION_UNDER_TEST) { steps.echo("Running in a consumer test, building unit-under-test binary for verification.") - steps.dockerExecute(script: steps, dockerImage: 'golang:1.22.4', dockerOptions: '-u 0', dockerEnvVars: [ + steps.dockerExecute(script: steps, dockerImage: 'golang:1.23.4', dockerOptions: '-u 0', dockerEnvVars: [ REPOSITORY_UNDER_TEST: steps.env.REPOSITORY_UNDER_TEST, LIBRARY_VERSION_UNDER_TEST: steps.env.LIBRARY_VERSION_UNDER_TEST ]) { From 8205624a22920f2ebe1e7999bf1b384aeae04e5d Mon Sep 17 00:00:00 2001 From: Manjunath Date: Wed, 18 Dec 2024 10:21:56 +0100 Subject: [PATCH 09/12] Fix deployment failure with CF if password contains special char (#5197) * Fix deployment failure with CF if password contains special char * cf service test * combined darwin and linux test, remove duplicate test * escape username special characters * fix build errors * added missing build tags * refactor abaputils --- cmd/abapEnvironmentCreateSystem_test.go | 4 +- ...abapEnvironmentPushATCSystemConfig_test.go | 38 +++++------ cmd/abapEnvironmentRunATCCheck_test.go | 20 +++--- cmd/abapEnvironmentRunAUnitTest_test.go | 8 +-- ...pLandscapePortalUpdateAddOnProduct_test.go | 2 + cmd/cloudFoundryCreateServiceKey_test.go | 19 +++--- cmd/cloudFoundryCreateService_test.go | 41 ++++++------ cmd/gcpPublishEvent_test.go | 2 + cmd/integrationArtifactDeploy_test.go | 5 +- cmd/integrationArtifactTransport.go | 3 +- cmd/malwareExecuteScan_test.go | 36 +++++------ cmd/readPipelineEnv_test.go | 2 + pkg/abaputils/abaputils.go | 48 +++++++------- pkg/abaputils/abaputils_test.go | 2 +- pkg/abaputils/sap_com_0510.go | 4 +- pkg/abaputils/sap_com_0948.go | 4 +- pkg/cloudfoundry/Authentication.go | 56 ++++++++++------- pkg/cloudfoundry/Authentication_test.go | 63 +++++++++++++++++++ pkg/cloudfoundry/CloudFoundry_test.go | 21 +++---- pkg/cloudfoundry/ManifestUtils_test.go | 1 - pkg/cloudfoundry/Vars_test.go | 1 - pkg/cpi/mockingUtils.go | 12 ++-- 22 files changed, 234 insertions(+), 158 deletions(-) create mode 100644 pkg/cloudfoundry/Authentication_test.go diff --git a/cmd/abapEnvironmentCreateSystem_test.go b/cmd/abapEnvironmentCreateSystem_test.go index 770eafd59e..667c999c7d 100644 --- a/cmd/abapEnvironmentCreateSystem_test.go +++ b/cmd/abapEnvironmentCreateSystem_test.go @@ -32,7 +32,7 @@ func TestRunAbapEnvironmentCreateSystem(t *testing.T) { err := runAbapEnvironmentCreateSystem(&config, nil, cf, u) if assert.NoError(t, err) { assert.Equal(t, []mock.ExecCall{ - {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, + {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", config.CfService, config.CfServicePlan, config.CfServiceInstance, "-c", "{\"is_development_allowed\":false}", "--wait"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -79,7 +79,7 @@ func TestRunAbapEnvironmentCreateSystem(t *testing.T) { err = runAbapEnvironmentCreateSystem(&config, nil, cf, u) if assert.NoError(t, err) { assert.Equal(t, []mock.ExecCall{ - {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, + {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "customManifest.yml"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) diff --git a/cmd/abapEnvironmentPushATCSystemConfig_test.go b/cmd/abapEnvironmentPushATCSystemConfig_test.go index 66c6c7d65b..d57b626bf3 100644 --- a/cmd/abapEnvironmentPushATCSystemConfig_test.go +++ b/cmd/abapEnvironmentPushATCSystemConfig_test.go @@ -28,8 +28,8 @@ func TestFetchXcsrfTokenFromHead(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, error := fetchXcsrfTokenFromHead(con, client) - if error == nil { + token, err := fetchXcsrfTokenFromHead(con, client) + if err == nil { assert.Equal(t, tokenExpected, token) } }) @@ -46,8 +46,8 @@ func TestFetchXcsrfTokenFromHead(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, error := fetchXcsrfTokenFromHead(con, client) - if error == nil { + token, err := fetchXcsrfTokenFromHead(con, client) + if err == nil { assert.Equal(t, tokenExpected, token) } }) @@ -77,39 +77,39 @@ func TestHandleHttpResponse(t *testing.T) { --B772E21DAA42B9571C778276B829D6C20 Content-Type: multipart/mixed; boundary=B772E21DAA42B9571C778276B829D6C21 Content-Length: 1973 - + --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 646 content-transfer-encoding: binary content-id: 1 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 465 odata-version: 4.0 cache-control: no-cache, no-store, must-revalidate - + {"@odata.context":"$metadata#configuration/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","conf_name":"Z_CONFIG_VIA_PIPELINE_STEP","checkvariant":"ABAP_CLOUD_DEVELOPMENT_DEFAULT","pseudo_comment_policy":"SP","last_changed_by":"CC0000000017","last_changed_at":"2022-03-02T11:16:51.336172Z","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false,"SAP__Messages":[]} --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 428 content-transfer-encoding: binary content-id: 2 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 247 odata-version: 4.0 cache-control: no-cache, no-store, must-revalidate - + {"@odata.context":"$metadata#priority/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","test":"CL_CI_ARS_COMPATIBILITY_CHECK","message_id":"010","default_priority":1,"priority":2} --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 428 content-transfer-encoding: binary content-id: 3 - + HTTP/1.1 4** OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 247 @@ -118,7 +118,7 @@ cache-control: no-cache, no-store, must-revalidate {"Some Error Messages possible in here!"} --B772E21DAA42B9571C778276B829D6C21-- - + --B772E21DAA42B9571C778276B829D6C20--` client := &abaputils.ClientMock{ @@ -150,39 +150,39 @@ cache-control: no-cache, no-store, must-revalidate --B772E21DAA42B9571C778276B829D6C20 Content-Type: multipart/mixed; boundary=B772E21DAA42B9571C778276B829D6C21 Content-Length: 1973 - + --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 646 content-transfer-encoding: binary content-id: 1 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 465 odata-version: 4.0 cache-control: no-cache, no-store, must-revalidate - + {"@odata.context":"$metadata#configuration/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","conf_name":"Z_CONFIG_VIA_PIPELINE_STEP","checkvariant":"ABAP_CLOUD_DEVELOPMENT_DEFAULT","pseudo_comment_policy":"SP","last_changed_by":"CC0000000017","last_changed_at":"2022-03-02T11:16:51.336172Z","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false,"SAP__Messages":[]} --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 428 content-transfer-encoding: binary content-id: 2 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 247 odata-version: 4.0 cache-control: no-cache, no-store, must-revalidate - + {"@odata.context":"$metadata#priority/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","test":"CL_CI_ARS_COMPATIBILITY_CHECK","message_id":"010","default_priority":1,"priority":2} --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 428 content-transfer-encoding: binary content-id: 3 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 247 @@ -191,7 +191,7 @@ cache-control: no-cache, no-store, must-revalidate {"@odata.context":"$metadata#priority/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","test":"CL_CI_ARS_COMPATIBILITY_CHECK","message_id":"011","default_priority":2,"priority":1} --B772E21DAA42B9571C778276B829D6C21-- - + --B772E21DAA42B9571C778276B829D6C20--` client := &abaputils.ClientMock{ @@ -328,7 +328,7 @@ Content-Type: application/json "transport_check_policy": "C", "check_tasks": true, "check_requests": false, - "check_tocs": true, + "check_tocs": true, "_priorities": [ { "test": "CL_CI_TEST_AMDP_HDB_MIGRATION", diff --git a/cmd/abapEnvironmentRunATCCheck_test.go b/cmd/abapEnvironmentRunATCCheck_test.go index ea607c5f32..e0d6104747 100644 --- a/cmd/abapEnvironmentRunATCCheck_test.go +++ b/cmd/abapEnvironmentRunATCCheck_test.go @@ -29,9 +29,9 @@ func TestHostConfig(t *testing.T) { Exec: execRunner, } var con abaputils.ConnectionDetailsHTTP - con, error := autils.GetAbapCommunicationArrangementInfo(options.AbapEnvOptions, "") + con, err := autils.GetAbapCommunicationArrangementInfo(options.AbapEnvOptions, "") - if error == nil { + if err == nil { assert.Equal(t, "testUser", con.User) assert.Equal(t, "testPassword", con.Password) assert.Equal(t, "https://api.endpoint.com", con.URL) @@ -82,8 +82,8 @@ func TestHostConfig(t *testing.T) { Exec: execRunner, } var con abaputils.ConnectionDetailsHTTP - con, error := autils.GetAbapCommunicationArrangementInfo(options.AbapEnvOptions, "") - if error == nil { + con, err := autils.GetAbapCommunicationArrangementInfo(options.AbapEnvOptions, "") + if err == nil { assert.Equal(t, "", con.User) assert.Equal(t, "", con.Password) assert.Equal(t, "", con.URL) @@ -106,8 +106,8 @@ func TestATCTrigger(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - resp, error := runATC("GET", con, []byte(client.Body), client) - if error == nil { + resp, err := runATC("GET", con, []byte(client.Body), client) + if err == nil { assert.Equal(t, tokenExpected, resp.Header["X-Csrf-Token"][0]) assert.Equal(t, int64(0), resp.ContentLength) assert.Equal(t, []string([]string(nil)), resp.Header["Location"]) @@ -129,8 +129,8 @@ func TestFetchXcsrfToken(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, error := fetchXcsrfToken("GET", con, []byte(client.Body), client) - if error == nil { + token, err := fetchXcsrfToken("GET", con, []byte(client.Body), client) + if err == nil { assert.Equal(t, tokenExpected, token) } }) @@ -147,8 +147,8 @@ func TestFetchXcsrfToken(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, error := fetchXcsrfToken("GET", con, []byte(client.Body), client) - if error == nil { + token, err := fetchXcsrfToken("GET", con, []byte(client.Body), client) + if err == nil { assert.Equal(t, tokenExpected, token) } }) diff --git a/cmd/abapEnvironmentRunAUnitTest_test.go b/cmd/abapEnvironmentRunAUnitTest_test.go index 89c153e124..73f64006e5 100644 --- a/cmd/abapEnvironmentRunAUnitTest_test.go +++ b/cmd/abapEnvironmentRunAUnitTest_test.go @@ -582,8 +582,8 @@ func TestRunAbapEnvironmentRunAUnitTest(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, error := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) - if assert.Equal(t, nil, error) { + token, err := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) + if assert.Equal(t, nil, err) { assert.Equal(t, tokenExpected, token) } }) @@ -603,8 +603,8 @@ func TestRunAbapEnvironmentRunAUnitTest(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, error := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) - if assert.Equal(t, nil, error) { + token, err := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) + if assert.Equal(t, nil, err) { assert.Equal(t, tokenExpected, token) } }) diff --git a/cmd/abapLandscapePortalUpdateAddOnProduct_test.go b/cmd/abapLandscapePortalUpdateAddOnProduct_test.go index 7c1725db3f..d8d458d93f 100644 --- a/cmd/abapLandscapePortalUpdateAddOnProduct_test.go +++ b/cmd/abapLandscapePortalUpdateAddOnProduct_test.go @@ -1,3 +1,5 @@ +//go:build unit + package cmd import ( diff --git a/cmd/cloudFoundryCreateServiceKey_test.go b/cmd/cloudFoundryCreateServiceKey_test.go index dc886c7332..b2c1172e80 100644 --- a/cmd/cloudFoundryCreateServiceKey_test.go +++ b/cmd/cloudFoundryCreateServiceKey_test.go @@ -1,5 +1,4 @@ //go:build unit -// +build unit package cmd @@ -30,8 +29,8 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) { cfUtilsMock := cloudfoundry.CfUtilsMock{} defer cfUtilsMock.Cleanup() - error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) - if error == nil { + err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) + if err == nil { assert.Equal(t, "cf", execRunner.Calls[0].Exec) assert.Equal(t, []string{"create-service-key", "testInstance", "testKey"}, execRunner.Calls[0].Params) } @@ -52,8 +51,7 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) { cfUtilsMock := cloudfoundry.CfUtilsMock{} defer cfUtilsMock.Cleanup() - error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) - if error == nil { + if err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock); err == nil { assert.Equal(t, "cf", execRunner.Calls[0].Exec) assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "testconfig.yml"}, execRunner.Calls[0].Params) } @@ -74,8 +72,7 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) { cfUtilsMock := cloudfoundry.CfUtilsMock{} defer cfUtilsMock.Cleanup() - error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) - if error == nil { + if err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock); err == nil { assert.Equal(t, "cf", execRunner.Calls[0].Exec) assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}", cfCliSynchronousRequestFlag}, execRunner.Calls[0].Params) } @@ -103,8 +100,8 @@ func TestCloudFoundryCreateServiceKeyErrorMessages(t *testing.T) { } defer cfUtilsMock.Cleanup() - error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) - assert.Equal(t, error.Error(), "Error while logging in occurred: "+errorMessage, "Wrong error message") + err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) + assert.Equal(t, err.Error(), "Error while logging in occurred: "+errorMessage, "Wrong error message") }) t.Run("CF Logout Error", func(t *testing.T) { @@ -152,7 +149,7 @@ func TestCloudFoundryCreateServiceKeyErrorMessages(t *testing.T) { } defer cfUtilsMock.Cleanup() - error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) - assert.Equal(t, error.Error(), "Failed to Create Service Key: "+errorMessage, "Wrong error message") + err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) + assert.Equal(t, err.Error(), "Failed to Create Service Key: "+errorMessage, "Wrong error message") }) } diff --git a/cmd/cloudFoundryCreateService_test.go b/cmd/cloudFoundryCreateService_test.go index 1af31a4ebd..5ccab894ab 100644 --- a/cmd/cloudFoundryCreateService_test.go +++ b/cmd/cloudFoundryCreateService_test.go @@ -1,5 +1,4 @@ //go:build unit -// +build unit package cmd @@ -38,9 +37,9 @@ func TestCloudFoundryCreateService(t *testing.T) { CfServicePlan: "testPlan", CfAsync: false, } - error := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, error) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, + err := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, err) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "--wait"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -61,9 +60,9 @@ func TestCloudFoundryCreateService(t *testing.T) { CfServiceTags: "testTag, testTag2", CfAsync: true, } - error := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, error) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, + err := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, err) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "-t", "testTag, testTag2"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -83,9 +82,9 @@ func TestCloudFoundryCreateService(t *testing.T) { CfServiceBroker: "testBroker", CfAsync: true, } - error := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, error) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, + err := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, err) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "-b", "testBroker"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -105,9 +104,9 @@ func TestCloudFoundryCreateService(t *testing.T) { CfCreateServiceConfig: "testConfig.json", CfAsync: true, } - error := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, error) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, + err := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, err) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "-c", "testConfig.json"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -117,8 +116,8 @@ func TestCloudFoundryCreateService(t *testing.T) { t.Run("Create service: failure, no config", func(t *testing.T) { defer cfMockCleanup(m) config := cloudFoundryCreateServiceOptions{} - error := runCloudFoundryCreateService(&config, &telemetryData, cf) - assert.EqualError(t, error, "Error while logging in: Failed to login to Cloud Foundry: Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password") + err := runCloudFoundryCreateService(&config, &telemetryData, cf) + assert.EqualError(t, err, "Error while logging in: Failed to login to Cloud Foundry: Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password") }) t.Run("Create service: variable substitution in-line", func(t *testing.T) { @@ -163,9 +162,9 @@ func TestCloudFoundryCreateService(t *testing.T) { ManifestVariables: manifestVariables, CfAsync: false, // should be ignored } - error := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, error) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, + err = runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, err) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "manifestTest.yml", "--var", "name1=Test1", "--var", "name2=Test2"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -220,9 +219,9 @@ func TestCloudFoundryCreateService(t *testing.T) { ManifestVariablesFiles: manifestVariablesFiles, ManifestVariables: []string{"a=b", "x=y"}, } - error := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, error) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, + err = runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, err) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "manifestTest.yml", "--vars-file", "varsTest.yml", "--vars-file", "varsTest2.yml", "--var", "a=b", "--var", "x=y"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) diff --git a/cmd/gcpPublishEvent_test.go b/cmd/gcpPublishEvent_test.go index cf3b43530f..393d93af66 100644 --- a/cmd/gcpPublishEvent_test.go +++ b/cmd/gcpPublishEvent_test.go @@ -1,3 +1,5 @@ +//go:build unit + package cmd import ( diff --git a/cmd/integrationArtifactDeploy_test.go b/cmd/integrationArtifactDeploy_test.go index 2b38a18e81..f52169f47f 100644 --- a/cmd/integrationArtifactDeploy_test.go +++ b/cmd/integrationArtifactDeploy_test.go @@ -1,5 +1,4 @@ //go:build unit -// +build unit package cmd @@ -209,9 +208,9 @@ func (c *httpMockCpis) SendRequest(method string, url string, r io.Reader, heade } if c.CPIFunction == "" { c.CPIFunction = cpi.GetCPIFunctionNameByURLCheck(url, method, c.TestType) - resp, error := cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType) + resp, err := cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType) c.CPIFunction = "" - return resp, error + return resp, err } return cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType) diff --git a/cmd/integrationArtifactTransport.go b/cmd/integrationArtifactTransport.go index d2c67397fd..c285ede348 100644 --- a/cmd/integrationArtifactTransport.go +++ b/cmd/integrationArtifactTransport.go @@ -76,8 +76,7 @@ func CreateIntegrationArtifactTransportRequest(config *integrationArtifactTransp processId := jsonResponse.Path("processId").Data().(string) if processId != "" { - error := pollTransportStatus(processId, retryCount, config, httpClient, apistruct.Host) - return error + return pollTransportStatus(processId, retryCount, config, httpClient, apistruct.Host) } return errors.New("Invalid process id") } diff --git a/cmd/malwareExecuteScan_test.go b/cmd/malwareExecuteScan_test.go index 99f84c9b26..ff87911078 100644 --- a/cmd/malwareExecuteScan_test.go +++ b/cmd/malwareExecuteScan_test.go @@ -77,9 +77,9 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { SHA256: "96ca802fbd54d31903f1115a1d95590c685160637d9262bd340ab30d0f817e85", } - error := runMalwareScan(&malwareScanConfig, nil, &utils) + err := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.NoError(t, error) + assert.NoError(t, err) }) t.Run("Malware detected in file", func(t *testing.T) { @@ -92,8 +92,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { Finding: "Win.Test.EICAR_HDB-1", } - error := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, error, "Malware scan failed for file 'target/myFile'. Malware detected: true, encrypted content detected: false, finding: Win.Test.EICAR_HDB-1") + err := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, err, "Malware scan failed for file 'target/myFile'. Malware detected: true, encrypted content detected: false, finding: Win.Test.EICAR_HDB-1") }) t.Run("Encrypted content detected in file", func(t *testing.T) { @@ -105,8 +105,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { SHA256: "96ca802fbd54d31903f1115a1d95590c685160637d9262bd340ab30d0f817e85", } - error := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, error, "Malware scan failed for file 'target/myFile'. Malware detected: false, encrypted content detected: true, finding: ") + err := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, err, "Malware scan failed for file 'target/myFile'. Malware detected: false, encrypted content detected: true, finding: ") }) t.Run("Malware and encrypted content detected in file", func(t *testing.T) { @@ -119,8 +119,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { Finding: "Win.Test.EICAR_HDB-1", } - error := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, error, "Malware scan failed for file 'target/myFile'. Malware detected: true, encrypted content detected: true, finding: Win.Test.EICAR_HDB-1") + err := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, err, "Malware scan failed for file 'target/myFile'. Malware detected: true, encrypted content detected: true, finding: Win.Test.EICAR_HDB-1") }) t.Run("No file and no buildtool specified", func(t *testing.T) { @@ -131,8 +131,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { Timeout: "60", } - error := runMalwareScan(&malwareScanConfig, nil, nil) - assert.EqualError(t, error, "Please specify a file to be scanned") + err := runMalwareScan(&malwareScanConfig, nil, nil) + assert.EqualError(t, err, "Please specify a file to be scanned") }) t.Run("File to be scanned, can't be found", func(t *testing.T) { @@ -146,8 +146,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { ScanFile: "target/fileWhichDoesntExist", } - error := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, error, "the file 'target/fileWhichDoesntExist' does not exist: file does not exist") + err := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, err, "the file 'target/fileWhichDoesntExist' does not exist: file does not exist") }) } @@ -177,9 +177,9 @@ func TestMalwareScanWithDockerAsBuildtoolTests(t *testing.T) { ScanImage: "dockerimagename:latest", } - error := runMalwareScan(&malwareScanConfig, nil, &utils) + err := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.NoError(t, error) + assert.NoError(t, err) }) t.Run("No file and no buildtool specified", func(t *testing.T) { @@ -190,8 +190,8 @@ func TestMalwareScanWithDockerAsBuildtoolTests(t *testing.T) { Timeout: "60", } - error := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, error, "Please specify a file to be scanned") + err := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, err, "Please specify a file to be scanned") }) } @@ -221,9 +221,9 @@ func TestMalwareScanWithOtherBuildtoolTests(t *testing.T) { ScanImage: "dockerimagename:latest", } - error := runMalwareScan(&malwareScanConfig, nil, &utils) + err := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.NoError(t, error) + assert.NoError(t, err) }) } diff --git a/cmd/readPipelineEnv_test.go b/cmd/readPipelineEnv_test.go index 6efcb2ff61..3019d2eb78 100644 --- a/cmd/readPipelineEnv_test.go +++ b/cmd/readPipelineEnv_test.go @@ -1,3 +1,5 @@ +//go:build unit + package cmd import ( diff --git a/pkg/abaputils/abaputils.go b/pkg/abaputils/abaputils.go index fe805037a7..fff862c5de 100644 --- a/pkg/abaputils/abaputils.go +++ b/pkg/abaputils/abaputils.go @@ -38,42 +38,44 @@ type Communication interface { func (abaputils *AbapUtils) GetAbapCommunicationArrangementInfo(options AbapEnvironmentOptions, oDataURL string) (ConnectionDetailsHTTP, error) { c := abaputils.Exec var connectionDetails ConnectionDetailsHTTP - var error error + var err error - if options.Host != "" { - // Host, User and Password are directly provided -> check for host schema (double https) - match, err := regexp.MatchString(`^(https|HTTPS):\/\/.*`, options.Host) - if err != nil { - log.SetErrorCategory(log.ErrorConfiguration) - return connectionDetails, errors.Wrap(err, "Schema validation for host parameter failed. Check for https.") - } - var hostOdataURL = options.Host + oDataURL - if match { - connectionDetails.URL = hostOdataURL - connectionDetails.Host = options.Host - } else { - connectionDetails.URL = "https://" + hostOdataURL - connectionDetails.Host = "https://" + options.Host - } - connectionDetails.User = options.Username - connectionDetails.Password = options.Password - } else { + if options.Host == "" { if options.CfAPIEndpoint == "" || options.CfOrg == "" || options.CfSpace == "" || options.CfServiceInstance == "" || options.CfServiceKeyName == "" { var err = errors.New("Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry API Endpoint, Organization, Space, Service Instance and Service Key") log.SetErrorCategory(log.ErrorConfiguration) return connectionDetails, err } // Url, User and Password should be read from a cf service key - var abapServiceKey, error = ReadServiceKeyAbapEnvironment(options, c) - if error != nil { - return connectionDetails, errors.Wrap(error, "Read service key failed") + var abapServiceKey, err = ReadServiceKeyAbapEnvironment(options, c) + if err != nil { + return connectionDetails, errors.Wrap(err, "Read service key failed") } connectionDetails.Host = abapServiceKey.URL connectionDetails.URL = abapServiceKey.URL + oDataURL connectionDetails.User = abapServiceKey.Abap.Username connectionDetails.Password = abapServiceKey.Abap.Password + return connectionDetails, err + } + + // Host, User and Password are directly provided -> check for host schema (double https) + match, err := regexp.MatchString(`^(https|HTTPS):\/\/.*`, options.Host) + if err != nil { + log.SetErrorCategory(log.ErrorConfiguration) + return connectionDetails, errors.Wrap(err, "Schema validation for host parameter failed. Check for https.") } - return connectionDetails, error + var hostOdataURL = options.Host + oDataURL + if match { + connectionDetails.URL = hostOdataURL + connectionDetails.Host = options.Host + } else { + connectionDetails.URL = "https://" + hostOdataURL + connectionDetails.Host = "https://" + options.Host + } + connectionDetails.User = options.Username + connectionDetails.Password = options.Password + + return connectionDetails, err } // ReadServiceKeyAbapEnvironment from Cloud Foundry and returns it. Depending on user/developer requirements if he wants to perform further Cloud Foundry actions diff --git a/pkg/abaputils/abaputils_test.go b/pkg/abaputils/abaputils_test.go index d9b0cafa2d..6f36fc37a2 100644 --- a/pkg/abaputils/abaputils_test.go +++ b/pkg/abaputils/abaputils_test.go @@ -1,5 +1,4 @@ //go:build unit -// +build unit package abaputils @@ -73,6 +72,7 @@ func TestCloudFoundryGetAbapCommunicationInfo(t *testing.T) { assert.Equal(t, "", connectionDetails.Password) assert.Equal(t, "", connectionDetails.XCsrfToken) + assert.Error(t, err) assert.EqualError(t, err, "Read service key failed: Parsing the service key failed for all supported formats. Service key is empty") }) t.Run("CF GetAbapCommunicationArrangementInfo - Success V8", func(t *testing.T) { diff --git a/pkg/abaputils/sap_com_0510.go b/pkg/abaputils/sap_com_0510.go index 556fc1b74c..ed6e5b3d1b 100644 --- a/pkg/abaputils/sap_com_0510.go +++ b/pkg/abaputils/sap_com_0510.go @@ -396,8 +396,8 @@ func (api *SAP_COM_0510) getLogProtocolQuery(page int) string { // ConvertTime formats an ABAP timestamp string from format /Date(1585576807000+0000)/ into a UNIX timestamp and returns it func (api *SAP_COM_0510) ConvertTime(logTimeStamp string) time.Time { seconds := strings.TrimPrefix(strings.TrimSuffix(logTimeStamp, "000+0000)/"), "/Date(") - n, error := strconv.ParseInt(seconds, 10, 64) - if error != nil { + n, err := strconv.ParseInt(seconds, 10, 64) + if err != nil { return time.Unix(0, 0).UTC() } t := time.Unix(n, 0).UTC() diff --git a/pkg/abaputils/sap_com_0948.go b/pkg/abaputils/sap_com_0948.go index 5d8477c190..d790faa13a 100644 --- a/pkg/abaputils/sap_com_0948.go +++ b/pkg/abaputils/sap_com_0948.go @@ -452,8 +452,8 @@ func (api *SAP_COM_0948) getLogProtocolQuery(page int) string { // ConvertTime formats an ISO 8601 timestamp string from format 2024-05-02T09:25:40Z into a UNIX timestamp and returns it func (api *SAP_COM_0948) ConvertTime(logTimeStamp string) time.Time { - t, error := time.Parse(time.RFC3339, logTimeStamp) - if error != nil { + t, err := time.Parse(time.RFC3339, logTimeStamp) + if err != nil { return time.Unix(0, 0).UTC() } return t diff --git a/pkg/cloudfoundry/Authentication.go b/pkg/cloudfoundry/Authentication.go index 014a829909..0f6f4d8657 100644 --- a/pkg/cloudfoundry/Authentication.go +++ b/pkg/cloudfoundry/Authentication.go @@ -3,6 +3,8 @@ package cloudfoundry import ( "errors" "fmt" + "runtime" + "strings" "github.com/SAP/jenkins-library/pkg/command" "github.com/SAP/jenkins-library/pkg/log" @@ -11,14 +13,13 @@ import ( // LoginCheck checks if user is logged in to Cloud Foundry with the receiver provided // to the function call. func (cf *CFUtils) LoginCheck(options LoginOptions) (bool, error) { + // TODO: options are not used ? return cf.loggedIn, nil } // Login logs user in to Cloud Foundry via cf cli. // Checks if user is logged in first, if not perform 'cf login' command with appropriate parameters func (cf *CFUtils) Login(options LoginOptions) error { - var err error - _c := cf.Exec if _c == nil { @@ -29,31 +30,29 @@ func (cf *CFUtils) Login(options LoginOptions) error { return fmt.Errorf("Failed to login to Cloud Foundry: %w", errors.New("Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password")) } - var loggedIn bool - - loggedIn, err = cf.LoginCheck(options) - - if loggedIn == true { - return err + if cf.loggedIn { + return nil } - if err == nil { - log.Entry().Info("Logging in to Cloud Foundry") + log.Entry().Info("Logging in to Cloud Foundry") - var cfLoginScript = append([]string{ - "login", - "-a", options.CfAPIEndpoint, - "-o", options.CfOrg, - "-s", options.CfSpace, - "-u", options.Username, - "-p", options.Password, - }, options.CfLoginOpts...) + escapedUsername := escapeValuesForCLI(options.Username, getGOOS) + escapedPassword := escapeValuesForCLI(options.Password, getGOOS) - log.Entry().WithField("cfAPI:", options.CfAPIEndpoint).WithField("cfOrg", options.CfOrg).WithField("space", options.CfSpace).Info("Logging into Cloud Foundry..") - - err = _c.RunExecutable("cf", cfLoginScript...) + cfLoginScript := []string{ + "login", + "-a", options.CfAPIEndpoint, + "-o", options.CfOrg, + "-s", options.CfSpace, + "-u", escapedUsername, + "-p", escapedPassword, } + cfLoginScript = append(cfLoginScript, options.CfLoginOpts...) + + log.Entry().WithField("cfAPI:", options.CfAPIEndpoint).WithField("cfOrg", options.CfOrg).WithField("space", options.CfSpace).Info("Logging into Cloud Foundry..") + + err := _c.RunExecutable("cf", cfLoginScript...) if err != nil { return fmt.Errorf("Failed to login to Cloud Foundry: %w", err) } @@ -62,6 +61,17 @@ func (cf *CFUtils) Login(options LoginOptions) error { return nil } +func escapeValuesForCLI(password string, getGOOS func() string) string { + switch getGOOS() { + case "windows": + return fmt.Sprintf("'%s'", strings.ReplaceAll(password, "'", `''`)) + case "linux", "darwin": + return fmt.Sprintf("'%s'", strings.ReplaceAll(password, "'", `'\''`)) + default: + return fmt.Sprintf("'%s'", strings.ReplaceAll(password, "'", `'\''`)) + } +} + // Logout logs User out of Cloud Foundry // Logout can be perforned via 'cf logout' command regardless if user is logged in or not func (cf *CFUtils) Logout() error { @@ -133,3 +143,7 @@ func (cf *CfUtilsMock) Cleanup() { cf.LoginError = nil cf.LogoutError = nil } + +func getGOOS() string { + return runtime.GOOS +} diff --git a/pkg/cloudfoundry/Authentication_test.go b/pkg/cloudfoundry/Authentication_test.go new file mode 100644 index 0000000000..6d388c1841 --- /dev/null +++ b/pkg/cloudfoundry/Authentication_test.go @@ -0,0 +1,63 @@ +//go:build unit + +package cloudfoundry + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_escapeValuesForCLI(t *testing.T) { + tests := []struct { + name string + os string + input string + expected string + }{ + { + name: "Windows password without quotes", + os: "windows", + input: `mypassword`, + expected: `'mypassword'`, + }, + { + name: "Windows password with quotes", + os: "windows", + input: `my\"password`, + expected: `'my\"password'`, + }, + { + name: "Non-Windows password without single quotes", + os: "linux", + input: "mypassword", + expected: "'mypassword'", + }, + { + name: "Non-Windows password with single quotes", + os: "darwin", + input: `my'password`, + expected: `'my'\''password'`, + }, + { + name: "Linux password with all special characters", + os: "linux", + input: "~!@#$%^&*()_+{`}|:\"<>?-=[]\\;',./", + expected: "'~!@#$%^&*()_+{`}|:\"<>?-=[]\\;'\\'',./'", + }, + { + name: "Windows password with all special characters", + os: "windows", + input: "~!@#$%^&*()_+{`}|:\"<>?-=[]\\;',./", + expected: "'~!@#$%^&*()_+{`}|:\"<>?-=[]\\;'',./'", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := escapeValuesForCLI(tt.input, func() string { return tt.os }) + assert.Equal(t, tt.expected, result, fmt.Sprintf("Failed for OS: %s and password: %s", tt.os, tt.input)) + }) + } +} diff --git a/pkg/cloudfoundry/CloudFoundry_test.go b/pkg/cloudfoundry/CloudFoundry_test.go index 57aaf63629..e480f46e03 100644 --- a/pkg/cloudfoundry/CloudFoundry_test.go +++ b/pkg/cloudfoundry/CloudFoundry_test.go @@ -1,5 +1,4 @@ //go:build unit -// +build unit package cloudfoundry @@ -87,8 +86,8 @@ func TestCloudFoundryLogin(t *testing.T) { "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", - "-u", "testUser", - "-p", "testPassword", + "-u", "'testUser'", + "-p", "'testPassword'", }}, }, m.Calls) } @@ -117,8 +116,8 @@ func TestCloudFoundryLogin(t *testing.T) { "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", - "-u", "testUser", - "-p", "testPassword", + "-u", "'testUser'", + "-p", "'testPassword'", }}, }, m.Calls) } @@ -150,8 +149,8 @@ func TestCloudFoundryLogin(t *testing.T) { "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", - "-u", "testUser", - "-p", "testPassword", + "-u", "'testUser'", + "-p", "'testPassword'", "--skip-ssl-validation", "--origin", "ldap", }}, @@ -184,7 +183,7 @@ func TestCloudFoundryReadServiceKeyAbapEnvironment(t *testing.T) { const password = "test_password" const serviceKey = ` cf comment test \n\n - {"sap.cloud.service":"com.sap.cloud.abap","url": "` + testURL + `" ,"systemid":"H01","abap":{"username":"` + username + `","password":"` + password + `","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}` + {"sap.cloud.service":"com.sap.cloud.abap","url": "` + testURL + `" ,"systemid":"H01","abap":{"username":"'` + username + `'","password":"'` + password + `'","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}` m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey} @@ -213,13 +212,13 @@ func TestCloudFoundryReadServiceKeyAbapEnvironment(t *testing.T) { "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", - "-u", "testUser", - "-p", "testPassword", + "-u", "'testUser'", + "-p", "'testPassword'", }}, {Exec: "cf", Params: []string{"service-key", "testInstance", "testServiceKeyName"}}, {Exec: "cf", Params: []string{"logout"}}, }, m.Calls) } - assert.Equal(t, ` {"sap.cloud.service":"com.sap.cloud.abap","url": "`+testURL+`" ,"systemid":"H01","abap":{"username":"`+username+`","password":"`+password+`","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}`, abapServiceKey) + assert.Equal(t, ` {"sap.cloud.service":"com.sap.cloud.abap","url": "`+testURL+`" ,"systemid":"H01","abap":{"username":"'`+username+`'","password":"'`+password+`'","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}`, abapServiceKey) }) } diff --git a/pkg/cloudfoundry/ManifestUtils_test.go b/pkg/cloudfoundry/ManifestUtils_test.go index c7f27d62d3..abe5a58666 100644 --- a/pkg/cloudfoundry/ManifestUtils_test.go +++ b/pkg/cloudfoundry/ManifestUtils_test.go @@ -1,5 +1,4 @@ //go:build unit -// +build unit package cloudfoundry diff --git a/pkg/cloudfoundry/Vars_test.go b/pkg/cloudfoundry/Vars_test.go index 66b74ee751..11d4600c06 100644 --- a/pkg/cloudfoundry/Vars_test.go +++ b/pkg/cloudfoundry/Vars_test.go @@ -1,5 +1,4 @@ //go:build unit -// +build unit package cloudfoundry diff --git a/pkg/cpi/mockingUtils.go b/pkg/cpi/mockingUtils.go index 50f4217ed6..6c0fa13c04 100644 --- a/pkg/cpi/mockingUtils.go +++ b/pkg/cpi/mockingUtils.go @@ -309,9 +309,9 @@ func IntegrationDesigntimeArtifactUpdateMockResponse(testType string) (*http.Res // GetMockResponseByTestTypeAndMockFunctionName - Get mock response by testtype and mock function name func GetMockResponseByTestTypeAndMockFunctionName(mockFuntionName, testType string) (*http.Response, error) { - response, error := GetPositiveCaseResponseByTestType(testType) + response, err := GetPositiveCaseResponseByTestType(testType) - if response == nil && error == nil { + if response == nil && err == nil { switch mockFuntionName { @@ -355,7 +355,7 @@ func GetMockResponseByTestTypeAndMockFunctionName(mockFuntionName, testType stri return &res, errors.New("Unable to get integration artifact deploy error status, Response Status code:400") } } - return response, error + return response, err } // NegtiveResForIntegrationArtifactGenericCommandMockResponse -Nagative Case http response body @@ -377,9 +377,9 @@ func NegtiveResForIntegrationArtifactGenericCommandMockResponse(message string) // UpdateIntegrationDesigntimeArtifactMockResponse -Provide http respose body func UpdateIntegrationDesigntimeArtifactMockResponse(testType string) (*http.Response, error) { - response, error := GetRespBodyHTTPStatusCreated() + response, err := GetRespBodyHTTPStatusCreated() - if response == nil && error == nil { + if response == nil && err == nil { res := http.Response{ StatusCode: 400, @@ -393,7 +393,7 @@ func UpdateIntegrationDesigntimeArtifactMockResponse(testType string) (*http.Res } return &res, errors.New("Unable to get status of integration artifact, Response Status code:400") } - return response, error + return response, err } // IntegrationArtifactDownloadCommandMockResponsePositiveCaseRespBody -Provide http respose body for positive case From aa1e67547a4fdfeb0f829d1dfeee6578c15ec7d4 Mon Sep 17 00:00:00 2001 From: Jordi van Liempt <35920075+jliempt@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:31:42 +0100 Subject: [PATCH 10/12] chore(trustengine): Rename Trust Engine to System Trust in logs and docs (#5210) * rename Trust Engine to System Trust * Revert "rename Trust Engine to System Trust" This reverts commit 66d4680324310790d41b70eed7421d121aac0e03. * rename Trust Engine to System Trust in logs and docs --- pkg/config/config.go | 2 +- pkg/config/trustengine.go | 22 +++++++++--------- pkg/config/trustengine_test.go | 9 ++++---- pkg/documentation/generator/parameters.go | 6 ++--- pkg/trustengine/trustengine.go | 28 +++++++++++------------ 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 05bce93f7e..32e967271d 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -297,7 +297,7 @@ func (c *Config) GetStepConfig(flagValues map[string]interface{}, paramJSON stri // hooks need to have been loaded from the defaults before the server URL is known err = c.setTrustEngineConfiguration(stepConfig.HookConfig) if err != nil { - log.Entry().WithError(err).Debug("Trust Engine lookup skipped due to missing or incorrect configuration") + log.Entry().WithError(err).Debug("System Trust lookup skipped due to missing or incorrect configuration") } else { trustengineClient := trustengine.PrepareClient(&piperhttp.Client{}, c.trustEngineConfiguration) resolveAllTrustEngineReferences(&stepConfig, append(parameters, ReportingParameters.Parameters...), c.trustEngineConfiguration, trustengineClient) diff --git a/pkg/config/trustengine.go b/pkg/config/trustengine.go index 2cd3e00010..5bb4e238ef 100644 --- a/pkg/config/trustengine.go +++ b/pkg/config/trustengine.go @@ -11,57 +11,57 @@ import ( // const RefTypeTrustengineSecretFile = "trustengineSecretFile" const RefTypeTrustengineSecret = "trustengineSecret" -// resolveAllTrustEngineReferences retrieves all the step's secrets from the Trust Engine +// resolveAllTrustEngineReferences retrieves all the step's secrets from the System Trust func resolveAllTrustEngineReferences(config *StepConfig, params []StepParameters, trustEngineConfiguration trustengine.Configuration, client *piperhttp.Client) { for _, param := range params { if ref := param.GetReference(RefTypeTrustengineSecret); ref != nil { if config.Config[param.Name] == "" { - log.Entry().Infof("Getting '%s' from Trust Engine", param.Name) + log.Entry().Infof("Getting '%s' from System Trust", param.Name) token, err := trustengine.GetToken(ref.Default, client, trustEngineConfiguration) if err != nil { log.Entry().Info(" failed") - log.Entry().WithError(err).Debugf("Couldn't get '%s' token from Trust Engine", ref.Default) + log.Entry().WithError(err).Debugf("Couldn't get '%s' token from System Trust", ref.Default) continue } log.RegisterSecret(token) config.Config[param.Name] = token log.Entry().Info(" succeeded") } else { - log.Entry().Debugf("Skipping retrieval of '%s' from Trust Engine: parameter already set", param.Name) + log.Entry().Debugf("Skipping retrieval of '%s' from System Trust: parameter already set", param.Name) } } } } -// setTrustEngineConfiguration sets the server URL for the Trust Engine by taking it from the hooks +// setTrustEngineConfiguration sets the server URL for the System Trust by taking it from the hooks func (c *Config) setTrustEngineConfiguration(hookConfig map[string]interface{}) error { trustEngineHook, ok := hookConfig["trustengine"].(map[string]interface{}) if !ok { - return errors.New("no Trust Engine hook configuration found") + return errors.New("no System Trust hook configuration found") } if serverURL, ok := trustEngineHook["serverURL"].(string); ok { c.trustEngineConfiguration.ServerURL = serverURL } else { - return errors.New("no Trust Engine server URL found") + return errors.New("no System Trust server URL found") } if tokenEndPoint, ok := trustEngineHook["tokenEndPoint"].(string); ok { c.trustEngineConfiguration.TokenEndPoint = tokenEndPoint } else { - return errors.New("no Trust Engine service endpoint found") + return errors.New("no System Trust service endpoint found") } if tokenQueryParamName, ok := trustEngineHook["tokenQueryParamName"].(string); ok { c.trustEngineConfiguration.TokenQueryParamName = tokenQueryParamName } else { - return errors.New("no Trust Engine query parameter name found") + return errors.New("no System Trust query parameter name found") } if len(c.trustEngineConfiguration.Token) == 0 { - return errors.New("no Trust Engine token found and envvar is empty") + return errors.New("no System Trust token found and envvar is empty") } return nil } -// SetTrustEngineToken sets the token for the Trust Engine +// SetTrustEngineToken sets the token for the System Trust func (c *Config) SetTrustEngineToken(token string) { c.trustEngineConfiguration.Token = token } diff --git a/pkg/config/trustengine_test.go b/pkg/config/trustengine_test.go index c475a9e8b2..0ba9827fc7 100644 --- a/pkg/config/trustengine_test.go +++ b/pkg/config/trustengine_test.go @@ -5,11 +5,12 @@ package config import ( "fmt" + "net/http" + "testing" + piperhttp "github.com/SAP/jenkins-library/pkg/http" "github.com/SAP/jenkins-library/pkg/trustengine" "github.com/jarcoal/httpmock" - "net/http" - "testing" "github.com/stretchr/testify/assert" ) @@ -40,7 +41,7 @@ func TestTrustEngineConfig(t *testing.T) { client := &piperhttp.Client{} client.SetOptions(piperhttp.ClientOptions{MaxRetries: -1, UseDefaultTransport: true}) - t.Run("Load secret from Trust Engine - secret not set yet by Vault or config.yml", func(t *testing.T) { + t.Run("Load secret from System Trust - secret not set yet by Vault or config.yml", func(t *testing.T) { stepConfig := &StepConfig{Config: map[string]interface{}{ secretName: "", }} @@ -49,7 +50,7 @@ func TestTrustEngineConfig(t *testing.T) { assert.Equal(t, mockSonarToken, stepConfig.Config[secretName]) }) - t.Run("Load secret from Trust Engine - secret already by Vault or config.yml", func(t *testing.T) { + t.Run("Load secret from System Trust - secret already by Vault or config.yml", func(t *testing.T) { stepConfig := &StepConfig{Config: map[string]interface{}{ secretName: "aMockTokenFromVault", }} diff --git a/pkg/documentation/generator/parameters.go b/pkg/documentation/generator/parameters.go index eeb35d6aee..e7aa5bc547 100644 --- a/pkg/documentation/generator/parameters.go +++ b/pkg/documentation/generator/parameters.go @@ -13,7 +13,7 @@ const ( vaultBadge = "![Vault](https://img.shields.io/badge/-Vault-lightgrey)" jenkinsOnlyBadge = "![Jenkins only](https://img.shields.io/badge/-Jenkins%20only-yellowgreen)" secretBadge = "![Secret](https://img.shields.io/badge/-Secret-yellowgreen)" - trustengineBadge = "![Trust Engine](https://img.shields.io/badge/-Trust%20Engine-lightblue)" + trustengineBadge = "![System Trust](https://img.shields.io/badge/-System%20Trust-lightblue)" deprecatedBadge = "![deprecated](https://img.shields.io/badge/-deprecated-red)" ) @@ -124,7 +124,7 @@ func parameterFurtherInfo(paramName string, stepData *config.StepData, execution isVaultSecret := param.GetReference("vaultSecret") != nil || param.GetReference("vaultSecretFile") != nil isTrustengineSecret := param.GetReference(config.RefTypeTrustengineSecret) != nil if isVaultSecret && isTrustengineSecret { - secretInfo = fmt.Sprintf(" %s %s %s pass via ENV, Vault, Trust Engine or Jenkins credentials", vaultBadge, trustengineBadge, secretBadge) + secretInfo = fmt.Sprintf(" %s %s %s pass via ENV, Vault, System Trust or Jenkins credentials", vaultBadge, trustengineBadge, secretBadge) } else if isVaultSecret { secretInfo = fmt.Sprintf(" %s %s pass via ENV, Vault or Jenkins credentials", vaultBadge, secretBadge) } @@ -370,7 +370,7 @@ func addVaultResourceDetails(resource config.ResourceReference, resourceDetails } func addTrustEngineResourceDetails(resource config.ResourceReference, resourceDetails string) string { - resourceDetails += "
Trust Engine resource:
" + resourceDetails += "
System Trust resource:
" resourceDetails += fmt.Sprintf("  name: `%v`
", resource.Name) resourceDetails += fmt.Sprintf("  value: `%v`
", resource.Default) diff --git a/pkg/trustengine/trustengine.go b/pkg/trustengine/trustengine.go index 99caf500ae..a84738a1e3 100644 --- a/pkg/trustengine/trustengine.go +++ b/pkg/trustengine/trustengine.go @@ -35,17 +35,17 @@ type Configuration struct { func GetToken(refName string, client *piperhttp.Client, trustEngineConfiguration Configuration) (string, error) { secrets, err := GetSecrets([]string{refName}, client, trustEngineConfiguration) if err != nil { - return "", errors.Wrap(err, "couldn't get token from trust engine") + return "", errors.Wrap(err, "couldn't get token from System Trust") } for _, s := range secrets { if s.System == refName { return s.Token, nil } } - return "", errors.New("could not find token in trust engine response") + return "", errors.New("could not find token in System Trust response") } -// GetSecrets transforms the trust engine JSON response into trust engine secrets, and can be used to request multiple tokens +// GetSecrets transforms the System Trust JSON response into System Trust secrets, and can be used to request multiple tokens func GetSecrets(refNames []string, client *piperhttp.Client, trustEngineConfiguration Configuration) ([]Secret, error) { var secrets []Secret query := url.Values{ @@ -55,7 +55,7 @@ func GetSecrets(refNames []string, client *piperhttp.Client, trustEngineConfigur } response, err := getResponse(trustEngineConfiguration.ServerURL, trustEngineConfiguration.TokenEndPoint, query, client) if err != nil { - return secrets, errors.Wrap(err, "getting secrets from trust engine failed") + return secrets, errors.Wrap(err, "getting secrets from System Trust failed") } for k, v := range response { secrets = append(secrets, Secret{ @@ -66,13 +66,13 @@ func GetSecrets(refNames []string, client *piperhttp.Client, trustEngineConfigur return secrets, nil } -// getResponse returns a map of the JSON response that the trust engine puts out +// getResponse returns a map of the JSON response that the System Trust puts out func getResponse(serverURL, endpoint string, query url.Values, client *piperhttp.Client) (map[string]string, error) { var secrets map[string]string rawURL, err := parseURL(serverURL, endpoint, query) if err != nil { - return secrets, errors.Wrap(err, "parsing trust engine url failed") + return secrets, errors.Wrap(err, "parsing System Trust url failed") } header := make(http.Header) header.Add("Accept", "application/json") @@ -88,38 +88,38 @@ func getResponse(serverURL, endpoint string, query url.Values, client *piperhttp err = errors.Wrap(err, string(bodyBytes)) } } - return secrets, errors.Wrap(err, "getting response from trust engine failed") + return secrets, errors.Wrap(err, "getting response from System Trust failed") } defer response.Body.Close() err = json.NewDecoder(response.Body).Decode(&secrets) if err != nil { - return secrets, errors.Wrap(err, "getting response from trust engine failed") + return secrets, errors.Wrap(err, "getting response from System Trust failed") } return secrets, nil } -// parseURL creates the full URL for a Trust Engine GET request +// parseURL creates the full URL for a System Trust GET request func parseURL(serverURL, endpoint string, query url.Values) (string, error) { rawFullEndpoint, err := url.JoinPath(serverURL, endpoint) if err != nil { - return "", errors.New("error parsing trust engine URL") + return "", errors.New("error parsing System Trust URL") } fullURL, err := url.Parse(rawFullEndpoint) if err != nil { - return "", errors.New("error parsing trust engine URL") + return "", errors.New("error parsing System Trust URL") } - // commas and spaces shouldn't be escaped since the Trust Engine won't accept it + // commas and spaces shouldn't be escaped since the System Trust won't accept it unescapedRawQuery, err := url.QueryUnescape(query.Encode()) if err != nil { - return "", errors.New("error parsing trust engine URL") + return "", errors.New("error parsing System Trust URL") } fullURL.RawQuery = unescapedRawQuery return fullURL.String(), nil } -// PrepareClient adds the Trust Engine authentication token to the client +// PrepareClient adds the System Trust authentication token to the client func PrepareClient(client *piperhttp.Client, trustEngineConfiguration Configuration) *piperhttp.Client { var logEntry *logrus.Entry if logrus.GetLevel() < logrus.DebugLevel { From 93c4ea61aa8393fe5467a4cc9dc412f9a7b802bf Mon Sep 17 00:00:00 2001 From: Valentin <16714035+bitmaskit@users.noreply.github.com> Date: Mon, 23 Dec 2024 13:33:12 +0200 Subject: [PATCH 11/12] Revert "Fix deployment failure with CF if password contains special char (#5197)" (#5215) This reverts commit 8205624a22920f2ebe1e7999bf1b384aeae04e5d. Co-authored-by: Valentin Uchkunev --- cmd/abapEnvironmentCreateSystem_test.go | 4 +- ...abapEnvironmentPushATCSystemConfig_test.go | 38 +++++------ cmd/abapEnvironmentRunATCCheck_test.go | 20 +++--- cmd/abapEnvironmentRunAUnitTest_test.go | 8 +-- ...pLandscapePortalUpdateAddOnProduct_test.go | 2 - cmd/cloudFoundryCreateServiceKey_test.go | 19 +++--- cmd/cloudFoundryCreateService_test.go | 41 ++++++------ cmd/gcpPublishEvent_test.go | 2 - cmd/integrationArtifactDeploy_test.go | 5 +- cmd/integrationArtifactTransport.go | 3 +- cmd/malwareExecuteScan_test.go | 36 +++++------ cmd/readPipelineEnv_test.go | 2 - pkg/abaputils/abaputils.go | 48 +++++++------- pkg/abaputils/abaputils_test.go | 2 +- pkg/abaputils/sap_com_0510.go | 4 +- pkg/abaputils/sap_com_0948.go | 4 +- pkg/cloudfoundry/Authentication.go | 56 +++++++---------- pkg/cloudfoundry/Authentication_test.go | 63 ------------------- pkg/cloudfoundry/CloudFoundry_test.go | 21 ++++--- pkg/cloudfoundry/ManifestUtils_test.go | 1 + pkg/cloudfoundry/Vars_test.go | 1 + pkg/cpi/mockingUtils.go | 12 ++-- 22 files changed, 158 insertions(+), 234 deletions(-) delete mode 100644 pkg/cloudfoundry/Authentication_test.go diff --git a/cmd/abapEnvironmentCreateSystem_test.go b/cmd/abapEnvironmentCreateSystem_test.go index 667c999c7d..770eafd59e 100644 --- a/cmd/abapEnvironmentCreateSystem_test.go +++ b/cmd/abapEnvironmentCreateSystem_test.go @@ -32,7 +32,7 @@ func TestRunAbapEnvironmentCreateSystem(t *testing.T) { err := runAbapEnvironmentCreateSystem(&config, nil, cf, u) if assert.NoError(t, err) { assert.Equal(t, []mock.ExecCall{ - {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, + {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", config.CfService, config.CfServicePlan, config.CfServiceInstance, "-c", "{\"is_development_allowed\":false}", "--wait"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -79,7 +79,7 @@ func TestRunAbapEnvironmentCreateSystem(t *testing.T) { err = runAbapEnvironmentCreateSystem(&config, nil, cf, u) if assert.NoError(t, err) { assert.Equal(t, []mock.ExecCall{ - {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, + {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "customManifest.yml"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) diff --git a/cmd/abapEnvironmentPushATCSystemConfig_test.go b/cmd/abapEnvironmentPushATCSystemConfig_test.go index d57b626bf3..66c6c7d65b 100644 --- a/cmd/abapEnvironmentPushATCSystemConfig_test.go +++ b/cmd/abapEnvironmentPushATCSystemConfig_test.go @@ -28,8 +28,8 @@ func TestFetchXcsrfTokenFromHead(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, err := fetchXcsrfTokenFromHead(con, client) - if err == nil { + token, error := fetchXcsrfTokenFromHead(con, client) + if error == nil { assert.Equal(t, tokenExpected, token) } }) @@ -46,8 +46,8 @@ func TestFetchXcsrfTokenFromHead(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, err := fetchXcsrfTokenFromHead(con, client) - if err == nil { + token, error := fetchXcsrfTokenFromHead(con, client) + if error == nil { assert.Equal(t, tokenExpected, token) } }) @@ -77,39 +77,39 @@ func TestHandleHttpResponse(t *testing.T) { --B772E21DAA42B9571C778276B829D6C20 Content-Type: multipart/mixed; boundary=B772E21DAA42B9571C778276B829D6C21 Content-Length: 1973 - + --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 646 content-transfer-encoding: binary content-id: 1 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 465 odata-version: 4.0 cache-control: no-cache, no-store, must-revalidate - + {"@odata.context":"$metadata#configuration/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","conf_name":"Z_CONFIG_VIA_PIPELINE_STEP","checkvariant":"ABAP_CLOUD_DEVELOPMENT_DEFAULT","pseudo_comment_policy":"SP","last_changed_by":"CC0000000017","last_changed_at":"2022-03-02T11:16:51.336172Z","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false,"SAP__Messages":[]} --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 428 content-transfer-encoding: binary content-id: 2 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 247 odata-version: 4.0 cache-control: no-cache, no-store, must-revalidate - + {"@odata.context":"$metadata#priority/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","test":"CL_CI_ARS_COMPATIBILITY_CHECK","message_id":"010","default_priority":1,"priority":2} --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 428 content-transfer-encoding: binary content-id: 3 - + HTTP/1.1 4** OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 247 @@ -118,7 +118,7 @@ cache-control: no-cache, no-store, must-revalidate {"Some Error Messages possible in here!"} --B772E21DAA42B9571C778276B829D6C21-- - + --B772E21DAA42B9571C778276B829D6C20--` client := &abaputils.ClientMock{ @@ -150,39 +150,39 @@ cache-control: no-cache, no-store, must-revalidate --B772E21DAA42B9571C778276B829D6C20 Content-Type: multipart/mixed; boundary=B772E21DAA42B9571C778276B829D6C21 Content-Length: 1973 - + --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 646 content-transfer-encoding: binary content-id: 1 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 465 odata-version: 4.0 cache-control: no-cache, no-store, must-revalidate - + {"@odata.context":"$metadata#configuration/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","conf_name":"Z_CONFIG_VIA_PIPELINE_STEP","checkvariant":"ABAP_CLOUD_DEVELOPMENT_DEFAULT","pseudo_comment_policy":"SP","last_changed_by":"CC0000000017","last_changed_at":"2022-03-02T11:16:51.336172Z","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false,"SAP__Messages":[]} --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 428 content-transfer-encoding: binary content-id: 2 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 247 odata-version: 4.0 cache-control: no-cache, no-store, must-revalidate - + {"@odata.context":"$metadata#priority/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","test":"CL_CI_ARS_COMPATIBILITY_CHECK","message_id":"010","default_priority":1,"priority":2} --B772E21DAA42B9571C778276B829D6C21 Content-Type: application/http Content-Length: 428 content-transfer-encoding: binary content-id: 3 - + HTTP/1.1 200 OK Content-Type: application/json;odata.metadata=minimal;charset=utf-8 Content-Length: 247 @@ -191,7 +191,7 @@ cache-control: no-cache, no-store, must-revalidate {"@odata.context":"$metadata#priority/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","test":"CL_CI_ARS_COMPATIBILITY_CHECK","message_id":"011","default_priority":2,"priority":1} --B772E21DAA42B9571C778276B829D6C21-- - + --B772E21DAA42B9571C778276B829D6C20--` client := &abaputils.ClientMock{ @@ -328,7 +328,7 @@ Content-Type: application/json "transport_check_policy": "C", "check_tasks": true, "check_requests": false, - "check_tocs": true, + "check_tocs": true, "_priorities": [ { "test": "CL_CI_TEST_AMDP_HDB_MIGRATION", diff --git a/cmd/abapEnvironmentRunATCCheck_test.go b/cmd/abapEnvironmentRunATCCheck_test.go index e0d6104747..ea607c5f32 100644 --- a/cmd/abapEnvironmentRunATCCheck_test.go +++ b/cmd/abapEnvironmentRunATCCheck_test.go @@ -29,9 +29,9 @@ func TestHostConfig(t *testing.T) { Exec: execRunner, } var con abaputils.ConnectionDetailsHTTP - con, err := autils.GetAbapCommunicationArrangementInfo(options.AbapEnvOptions, "") + con, error := autils.GetAbapCommunicationArrangementInfo(options.AbapEnvOptions, "") - if err == nil { + if error == nil { assert.Equal(t, "testUser", con.User) assert.Equal(t, "testPassword", con.Password) assert.Equal(t, "https://api.endpoint.com", con.URL) @@ -82,8 +82,8 @@ func TestHostConfig(t *testing.T) { Exec: execRunner, } var con abaputils.ConnectionDetailsHTTP - con, err := autils.GetAbapCommunicationArrangementInfo(options.AbapEnvOptions, "") - if err == nil { + con, error := autils.GetAbapCommunicationArrangementInfo(options.AbapEnvOptions, "") + if error == nil { assert.Equal(t, "", con.User) assert.Equal(t, "", con.Password) assert.Equal(t, "", con.URL) @@ -106,8 +106,8 @@ func TestATCTrigger(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - resp, err := runATC("GET", con, []byte(client.Body), client) - if err == nil { + resp, error := runATC("GET", con, []byte(client.Body), client) + if error == nil { assert.Equal(t, tokenExpected, resp.Header["X-Csrf-Token"][0]) assert.Equal(t, int64(0), resp.ContentLength) assert.Equal(t, []string([]string(nil)), resp.Header["Location"]) @@ -129,8 +129,8 @@ func TestFetchXcsrfToken(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, err := fetchXcsrfToken("GET", con, []byte(client.Body), client) - if err == nil { + token, error := fetchXcsrfToken("GET", con, []byte(client.Body), client) + if error == nil { assert.Equal(t, tokenExpected, token) } }) @@ -147,8 +147,8 @@ func TestFetchXcsrfToken(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, err := fetchXcsrfToken("GET", con, []byte(client.Body), client) - if err == nil { + token, error := fetchXcsrfToken("GET", con, []byte(client.Body), client) + if error == nil { assert.Equal(t, tokenExpected, token) } }) diff --git a/cmd/abapEnvironmentRunAUnitTest_test.go b/cmd/abapEnvironmentRunAUnitTest_test.go index 73f64006e5..89c153e124 100644 --- a/cmd/abapEnvironmentRunAUnitTest_test.go +++ b/cmd/abapEnvironmentRunAUnitTest_test.go @@ -582,8 +582,8 @@ func TestRunAbapEnvironmentRunAUnitTest(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, err := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) - if assert.Equal(t, nil, err) { + token, error := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) + if assert.Equal(t, nil, error) { assert.Equal(t, tokenExpected, token) } }) @@ -603,8 +603,8 @@ func TestRunAbapEnvironmentRunAUnitTest(t *testing.T) { Password: "Test", URL: "https://api.endpoint.com/Entity/", } - token, err := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) - if assert.Equal(t, nil, err) { + token, error := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) + if assert.Equal(t, nil, error) { assert.Equal(t, tokenExpected, token) } }) diff --git a/cmd/abapLandscapePortalUpdateAddOnProduct_test.go b/cmd/abapLandscapePortalUpdateAddOnProduct_test.go index d8d458d93f..7c1725db3f 100644 --- a/cmd/abapLandscapePortalUpdateAddOnProduct_test.go +++ b/cmd/abapLandscapePortalUpdateAddOnProduct_test.go @@ -1,5 +1,3 @@ -//go:build unit - package cmd import ( diff --git a/cmd/cloudFoundryCreateServiceKey_test.go b/cmd/cloudFoundryCreateServiceKey_test.go index b2c1172e80..dc886c7332 100644 --- a/cmd/cloudFoundryCreateServiceKey_test.go +++ b/cmd/cloudFoundryCreateServiceKey_test.go @@ -1,4 +1,5 @@ //go:build unit +// +build unit package cmd @@ -29,8 +30,8 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) { cfUtilsMock := cloudfoundry.CfUtilsMock{} defer cfUtilsMock.Cleanup() - err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) - if err == nil { + error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) + if error == nil { assert.Equal(t, "cf", execRunner.Calls[0].Exec) assert.Equal(t, []string{"create-service-key", "testInstance", "testKey"}, execRunner.Calls[0].Params) } @@ -51,7 +52,8 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) { cfUtilsMock := cloudfoundry.CfUtilsMock{} defer cfUtilsMock.Cleanup() - if err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock); err == nil { + error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) + if error == nil { assert.Equal(t, "cf", execRunner.Calls[0].Exec) assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "testconfig.yml"}, execRunner.Calls[0].Params) } @@ -72,7 +74,8 @@ func TestCloudFoundryCreateServiceKey(t *testing.T) { cfUtilsMock := cloudfoundry.CfUtilsMock{} defer cfUtilsMock.Cleanup() - if err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock); err == nil { + error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) + if error == nil { assert.Equal(t, "cf", execRunner.Calls[0].Exec) assert.Equal(t, []string{"create-service-key", "testInstance", "testKey", "-c", "{\"scenario_id\":\"SAP_COM_0510\",\"type\":\"basic\"}", cfCliSynchronousRequestFlag}, execRunner.Calls[0].Params) } @@ -100,8 +103,8 @@ func TestCloudFoundryCreateServiceKeyErrorMessages(t *testing.T) { } defer cfUtilsMock.Cleanup() - err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) - assert.Equal(t, err.Error(), "Error while logging in occurred: "+errorMessage, "Wrong error message") + error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) + assert.Equal(t, error.Error(), "Error while logging in occurred: "+errorMessage, "Wrong error message") }) t.Run("CF Logout Error", func(t *testing.T) { @@ -149,7 +152,7 @@ func TestCloudFoundryCreateServiceKeyErrorMessages(t *testing.T) { } defer cfUtilsMock.Cleanup() - err := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) - assert.Equal(t, err.Error(), "Failed to Create Service Key: "+errorMessage, "Wrong error message") + error := runCloudFoundryCreateServiceKey(&config, &telemetryData, &execRunner, &cfUtilsMock) + assert.Equal(t, error.Error(), "Failed to Create Service Key: "+errorMessage, "Wrong error message") }) } diff --git a/cmd/cloudFoundryCreateService_test.go b/cmd/cloudFoundryCreateService_test.go index 5ccab894ab..1af31a4ebd 100644 --- a/cmd/cloudFoundryCreateService_test.go +++ b/cmd/cloudFoundryCreateService_test.go @@ -1,4 +1,5 @@ //go:build unit +// +build unit package cmd @@ -37,9 +38,9 @@ func TestCloudFoundryCreateService(t *testing.T) { CfServicePlan: "testPlan", CfAsync: false, } - err := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, err) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, + error := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, error) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "--wait"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -60,9 +61,9 @@ func TestCloudFoundryCreateService(t *testing.T) { CfServiceTags: "testTag, testTag2", CfAsync: true, } - err := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, err) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, + error := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, error) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "-t", "testTag, testTag2"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -82,9 +83,9 @@ func TestCloudFoundryCreateService(t *testing.T) { CfServiceBroker: "testBroker", CfAsync: true, } - err := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, err) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, + error := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, error) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "-b", "testBroker"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -104,9 +105,9 @@ func TestCloudFoundryCreateService(t *testing.T) { CfCreateServiceConfig: "testConfig.json", CfAsync: true, } - err := runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, err) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, + error := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, error) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service", "testService", "testPlan", "testName", "-c", "testConfig.json"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -116,8 +117,8 @@ func TestCloudFoundryCreateService(t *testing.T) { t.Run("Create service: failure, no config", func(t *testing.T) { defer cfMockCleanup(m) config := cloudFoundryCreateServiceOptions{} - err := runCloudFoundryCreateService(&config, &telemetryData, cf) - assert.EqualError(t, err, "Error while logging in: Failed to login to Cloud Foundry: Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password") + error := runCloudFoundryCreateService(&config, &telemetryData, cf) + assert.EqualError(t, error, "Error while logging in: Failed to login to Cloud Foundry: Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password") }) t.Run("Create service: variable substitution in-line", func(t *testing.T) { @@ -162,9 +163,9 @@ func TestCloudFoundryCreateService(t *testing.T) { ManifestVariables: manifestVariables, CfAsync: false, // should be ignored } - err = runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, err) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, + error := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, error) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "manifestTest.yml", "--var", "name1=Test1", "--var", "name2=Test2"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) @@ -219,9 +220,9 @@ func TestCloudFoundryCreateService(t *testing.T) { ManifestVariablesFiles: manifestVariablesFiles, ManifestVariables: []string{"a=b", "x=y"}, } - err = runCloudFoundryCreateService(&config, &telemetryData, cf) - if assert.NoError(t, err) { - assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "'testUser'", "-p", "'testPassword'"}}, + error := runCloudFoundryCreateService(&config, &telemetryData, cf) + if assert.NoError(t, error) { + assert.Equal(t, []mock.ExecCall{{Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"login", "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", "-u", "testUser", "-p", "testPassword"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"create-service-push", "--no-push", "--service-manifest", "manifestTest.yml", "--vars-file", "varsTest.yml", "--vars-file", "varsTest2.yml", "--var", "a=b", "--var", "x=y"}}, {Execution: (*mock.Execution)(nil), Async: false, Exec: "cf", Params: []string{"logout"}}}, m.Calls) diff --git a/cmd/gcpPublishEvent_test.go b/cmd/gcpPublishEvent_test.go index 393d93af66..cf3b43530f 100644 --- a/cmd/gcpPublishEvent_test.go +++ b/cmd/gcpPublishEvent_test.go @@ -1,5 +1,3 @@ -//go:build unit - package cmd import ( diff --git a/cmd/integrationArtifactDeploy_test.go b/cmd/integrationArtifactDeploy_test.go index f52169f47f..2b38a18e81 100644 --- a/cmd/integrationArtifactDeploy_test.go +++ b/cmd/integrationArtifactDeploy_test.go @@ -1,4 +1,5 @@ //go:build unit +// +build unit package cmd @@ -208,9 +209,9 @@ func (c *httpMockCpis) SendRequest(method string, url string, r io.Reader, heade } if c.CPIFunction == "" { c.CPIFunction = cpi.GetCPIFunctionNameByURLCheck(url, method, c.TestType) - resp, err := cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType) + resp, error := cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType) c.CPIFunction = "" - return resp, err + return resp, error } return cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType) diff --git a/cmd/integrationArtifactTransport.go b/cmd/integrationArtifactTransport.go index c285ede348..d2c67397fd 100644 --- a/cmd/integrationArtifactTransport.go +++ b/cmd/integrationArtifactTransport.go @@ -76,7 +76,8 @@ func CreateIntegrationArtifactTransportRequest(config *integrationArtifactTransp processId := jsonResponse.Path("processId").Data().(string) if processId != "" { - return pollTransportStatus(processId, retryCount, config, httpClient, apistruct.Host) + error := pollTransportStatus(processId, retryCount, config, httpClient, apistruct.Host) + return error } return errors.New("Invalid process id") } diff --git a/cmd/malwareExecuteScan_test.go b/cmd/malwareExecuteScan_test.go index ff87911078..99f84c9b26 100644 --- a/cmd/malwareExecuteScan_test.go +++ b/cmd/malwareExecuteScan_test.go @@ -77,9 +77,9 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { SHA256: "96ca802fbd54d31903f1115a1d95590c685160637d9262bd340ab30d0f817e85", } - err := runMalwareScan(&malwareScanConfig, nil, &utils) + error := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.NoError(t, err) + assert.NoError(t, error) }) t.Run("Malware detected in file", func(t *testing.T) { @@ -92,8 +92,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { Finding: "Win.Test.EICAR_HDB-1", } - err := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, err, "Malware scan failed for file 'target/myFile'. Malware detected: true, encrypted content detected: false, finding: Win.Test.EICAR_HDB-1") + error := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, error, "Malware scan failed for file 'target/myFile'. Malware detected: true, encrypted content detected: false, finding: Win.Test.EICAR_HDB-1") }) t.Run("Encrypted content detected in file", func(t *testing.T) { @@ -105,8 +105,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { SHA256: "96ca802fbd54d31903f1115a1d95590c685160637d9262bd340ab30d0f817e85", } - err := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, err, "Malware scan failed for file 'target/myFile'. Malware detected: false, encrypted content detected: true, finding: ") + error := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, error, "Malware scan failed for file 'target/myFile'. Malware detected: false, encrypted content detected: true, finding: ") }) t.Run("Malware and encrypted content detected in file", func(t *testing.T) { @@ -119,8 +119,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { Finding: "Win.Test.EICAR_HDB-1", } - err := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, err, "Malware scan failed for file 'target/myFile'. Malware detected: true, encrypted content detected: true, finding: Win.Test.EICAR_HDB-1") + error := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, error, "Malware scan failed for file 'target/myFile'. Malware detected: true, encrypted content detected: true, finding: Win.Test.EICAR_HDB-1") }) t.Run("No file and no buildtool specified", func(t *testing.T) { @@ -131,8 +131,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { Timeout: "60", } - err := runMalwareScan(&malwareScanConfig, nil, nil) - assert.EqualError(t, err, "Please specify a file to be scanned") + error := runMalwareScan(&malwareScanConfig, nil, nil) + assert.EqualError(t, error, "Please specify a file to be scanned") }) t.Run("File to be scanned, can't be found", func(t *testing.T) { @@ -146,8 +146,8 @@ func TestMalwareScanWithNoBuildtool(t *testing.T) { ScanFile: "target/fileWhichDoesntExist", } - err := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, err, "the file 'target/fileWhichDoesntExist' does not exist: file does not exist") + error := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, error, "the file 'target/fileWhichDoesntExist' does not exist: file does not exist") }) } @@ -177,9 +177,9 @@ func TestMalwareScanWithDockerAsBuildtoolTests(t *testing.T) { ScanImage: "dockerimagename:latest", } - err := runMalwareScan(&malwareScanConfig, nil, &utils) + error := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.NoError(t, err) + assert.NoError(t, error) }) t.Run("No file and no buildtool specified", func(t *testing.T) { @@ -190,8 +190,8 @@ func TestMalwareScanWithDockerAsBuildtoolTests(t *testing.T) { Timeout: "60", } - err := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.EqualError(t, err, "Please specify a file to be scanned") + error := runMalwareScan(&malwareScanConfig, nil, &utils) + assert.EqualError(t, error, "Please specify a file to be scanned") }) } @@ -221,9 +221,9 @@ func TestMalwareScanWithOtherBuildtoolTests(t *testing.T) { ScanImage: "dockerimagename:latest", } - err := runMalwareScan(&malwareScanConfig, nil, &utils) + error := runMalwareScan(&malwareScanConfig, nil, &utils) - assert.NoError(t, err) + assert.NoError(t, error) }) } diff --git a/cmd/readPipelineEnv_test.go b/cmd/readPipelineEnv_test.go index 3019d2eb78..6efcb2ff61 100644 --- a/cmd/readPipelineEnv_test.go +++ b/cmd/readPipelineEnv_test.go @@ -1,5 +1,3 @@ -//go:build unit - package cmd import ( diff --git a/pkg/abaputils/abaputils.go b/pkg/abaputils/abaputils.go index fff862c5de..fe805037a7 100644 --- a/pkg/abaputils/abaputils.go +++ b/pkg/abaputils/abaputils.go @@ -38,44 +38,42 @@ type Communication interface { func (abaputils *AbapUtils) GetAbapCommunicationArrangementInfo(options AbapEnvironmentOptions, oDataURL string) (ConnectionDetailsHTTP, error) { c := abaputils.Exec var connectionDetails ConnectionDetailsHTTP - var err error + var error error - if options.Host == "" { + if options.Host != "" { + // Host, User and Password are directly provided -> check for host schema (double https) + match, err := regexp.MatchString(`^(https|HTTPS):\/\/.*`, options.Host) + if err != nil { + log.SetErrorCategory(log.ErrorConfiguration) + return connectionDetails, errors.Wrap(err, "Schema validation for host parameter failed. Check for https.") + } + var hostOdataURL = options.Host + oDataURL + if match { + connectionDetails.URL = hostOdataURL + connectionDetails.Host = options.Host + } else { + connectionDetails.URL = "https://" + hostOdataURL + connectionDetails.Host = "https://" + options.Host + } + connectionDetails.User = options.Username + connectionDetails.Password = options.Password + } else { if options.CfAPIEndpoint == "" || options.CfOrg == "" || options.CfSpace == "" || options.CfServiceInstance == "" || options.CfServiceKeyName == "" { var err = errors.New("Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry API Endpoint, Organization, Space, Service Instance and Service Key") log.SetErrorCategory(log.ErrorConfiguration) return connectionDetails, err } // Url, User and Password should be read from a cf service key - var abapServiceKey, err = ReadServiceKeyAbapEnvironment(options, c) - if err != nil { - return connectionDetails, errors.Wrap(err, "Read service key failed") + var abapServiceKey, error = ReadServiceKeyAbapEnvironment(options, c) + if error != nil { + return connectionDetails, errors.Wrap(error, "Read service key failed") } connectionDetails.Host = abapServiceKey.URL connectionDetails.URL = abapServiceKey.URL + oDataURL connectionDetails.User = abapServiceKey.Abap.Username connectionDetails.Password = abapServiceKey.Abap.Password - return connectionDetails, err - } - - // Host, User and Password are directly provided -> check for host schema (double https) - match, err := regexp.MatchString(`^(https|HTTPS):\/\/.*`, options.Host) - if err != nil { - log.SetErrorCategory(log.ErrorConfiguration) - return connectionDetails, errors.Wrap(err, "Schema validation for host parameter failed. Check for https.") } - var hostOdataURL = options.Host + oDataURL - if match { - connectionDetails.URL = hostOdataURL - connectionDetails.Host = options.Host - } else { - connectionDetails.URL = "https://" + hostOdataURL - connectionDetails.Host = "https://" + options.Host - } - connectionDetails.User = options.Username - connectionDetails.Password = options.Password - - return connectionDetails, err + return connectionDetails, error } // ReadServiceKeyAbapEnvironment from Cloud Foundry and returns it. Depending on user/developer requirements if he wants to perform further Cloud Foundry actions diff --git a/pkg/abaputils/abaputils_test.go b/pkg/abaputils/abaputils_test.go index 6f36fc37a2..d9b0cafa2d 100644 --- a/pkg/abaputils/abaputils_test.go +++ b/pkg/abaputils/abaputils_test.go @@ -1,4 +1,5 @@ //go:build unit +// +build unit package abaputils @@ -72,7 +73,6 @@ func TestCloudFoundryGetAbapCommunicationInfo(t *testing.T) { assert.Equal(t, "", connectionDetails.Password) assert.Equal(t, "", connectionDetails.XCsrfToken) - assert.Error(t, err) assert.EqualError(t, err, "Read service key failed: Parsing the service key failed for all supported formats. Service key is empty") }) t.Run("CF GetAbapCommunicationArrangementInfo - Success V8", func(t *testing.T) { diff --git a/pkg/abaputils/sap_com_0510.go b/pkg/abaputils/sap_com_0510.go index ed6e5b3d1b..556fc1b74c 100644 --- a/pkg/abaputils/sap_com_0510.go +++ b/pkg/abaputils/sap_com_0510.go @@ -396,8 +396,8 @@ func (api *SAP_COM_0510) getLogProtocolQuery(page int) string { // ConvertTime formats an ABAP timestamp string from format /Date(1585576807000+0000)/ into a UNIX timestamp and returns it func (api *SAP_COM_0510) ConvertTime(logTimeStamp string) time.Time { seconds := strings.TrimPrefix(strings.TrimSuffix(logTimeStamp, "000+0000)/"), "/Date(") - n, err := strconv.ParseInt(seconds, 10, 64) - if err != nil { + n, error := strconv.ParseInt(seconds, 10, 64) + if error != nil { return time.Unix(0, 0).UTC() } t := time.Unix(n, 0).UTC() diff --git a/pkg/abaputils/sap_com_0948.go b/pkg/abaputils/sap_com_0948.go index d790faa13a..5d8477c190 100644 --- a/pkg/abaputils/sap_com_0948.go +++ b/pkg/abaputils/sap_com_0948.go @@ -452,8 +452,8 @@ func (api *SAP_COM_0948) getLogProtocolQuery(page int) string { // ConvertTime formats an ISO 8601 timestamp string from format 2024-05-02T09:25:40Z into a UNIX timestamp and returns it func (api *SAP_COM_0948) ConvertTime(logTimeStamp string) time.Time { - t, err := time.Parse(time.RFC3339, logTimeStamp) - if err != nil { + t, error := time.Parse(time.RFC3339, logTimeStamp) + if error != nil { return time.Unix(0, 0).UTC() } return t diff --git a/pkg/cloudfoundry/Authentication.go b/pkg/cloudfoundry/Authentication.go index 0f6f4d8657..014a829909 100644 --- a/pkg/cloudfoundry/Authentication.go +++ b/pkg/cloudfoundry/Authentication.go @@ -3,8 +3,6 @@ package cloudfoundry import ( "errors" "fmt" - "runtime" - "strings" "github.com/SAP/jenkins-library/pkg/command" "github.com/SAP/jenkins-library/pkg/log" @@ -13,13 +11,14 @@ import ( // LoginCheck checks if user is logged in to Cloud Foundry with the receiver provided // to the function call. func (cf *CFUtils) LoginCheck(options LoginOptions) (bool, error) { - // TODO: options are not used ? return cf.loggedIn, nil } // Login logs user in to Cloud Foundry via cf cli. // Checks if user is logged in first, if not perform 'cf login' command with appropriate parameters func (cf *CFUtils) Login(options LoginOptions) error { + var err error + _c := cf.Exec if _c == nil { @@ -30,29 +29,31 @@ func (cf *CFUtils) Login(options LoginOptions) error { return fmt.Errorf("Failed to login to Cloud Foundry: %w", errors.New("Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password")) } - if cf.loggedIn { - return nil - } - - log.Entry().Info("Logging in to Cloud Foundry") + var loggedIn bool - escapedUsername := escapeValuesForCLI(options.Username, getGOOS) - escapedPassword := escapeValuesForCLI(options.Password, getGOOS) + loggedIn, err = cf.LoginCheck(options) - cfLoginScript := []string{ - "login", - "-a", options.CfAPIEndpoint, - "-o", options.CfOrg, - "-s", options.CfSpace, - "-u", escapedUsername, - "-p", escapedPassword, + if loggedIn == true { + return err } - cfLoginScript = append(cfLoginScript, options.CfLoginOpts...) + if err == nil { + log.Entry().Info("Logging in to Cloud Foundry") - log.Entry().WithField("cfAPI:", options.CfAPIEndpoint).WithField("cfOrg", options.CfOrg).WithField("space", options.CfSpace).Info("Logging into Cloud Foundry..") + var cfLoginScript = append([]string{ + "login", + "-a", options.CfAPIEndpoint, + "-o", options.CfOrg, + "-s", options.CfSpace, + "-u", options.Username, + "-p", options.Password, + }, options.CfLoginOpts...) + + log.Entry().WithField("cfAPI:", options.CfAPIEndpoint).WithField("cfOrg", options.CfOrg).WithField("space", options.CfSpace).Info("Logging into Cloud Foundry..") + + err = _c.RunExecutable("cf", cfLoginScript...) + } - err := _c.RunExecutable("cf", cfLoginScript...) if err != nil { return fmt.Errorf("Failed to login to Cloud Foundry: %w", err) } @@ -61,17 +62,6 @@ func (cf *CFUtils) Login(options LoginOptions) error { return nil } -func escapeValuesForCLI(password string, getGOOS func() string) string { - switch getGOOS() { - case "windows": - return fmt.Sprintf("'%s'", strings.ReplaceAll(password, "'", `''`)) - case "linux", "darwin": - return fmt.Sprintf("'%s'", strings.ReplaceAll(password, "'", `'\''`)) - default: - return fmt.Sprintf("'%s'", strings.ReplaceAll(password, "'", `'\''`)) - } -} - // Logout logs User out of Cloud Foundry // Logout can be perforned via 'cf logout' command regardless if user is logged in or not func (cf *CFUtils) Logout() error { @@ -143,7 +133,3 @@ func (cf *CfUtilsMock) Cleanup() { cf.LoginError = nil cf.LogoutError = nil } - -func getGOOS() string { - return runtime.GOOS -} diff --git a/pkg/cloudfoundry/Authentication_test.go b/pkg/cloudfoundry/Authentication_test.go deleted file mode 100644 index 6d388c1841..0000000000 --- a/pkg/cloudfoundry/Authentication_test.go +++ /dev/null @@ -1,63 +0,0 @@ -//go:build unit - -package cloudfoundry - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" -) - -func Test_escapeValuesForCLI(t *testing.T) { - tests := []struct { - name string - os string - input string - expected string - }{ - { - name: "Windows password without quotes", - os: "windows", - input: `mypassword`, - expected: `'mypassword'`, - }, - { - name: "Windows password with quotes", - os: "windows", - input: `my\"password`, - expected: `'my\"password'`, - }, - { - name: "Non-Windows password without single quotes", - os: "linux", - input: "mypassword", - expected: "'mypassword'", - }, - { - name: "Non-Windows password with single quotes", - os: "darwin", - input: `my'password`, - expected: `'my'\''password'`, - }, - { - name: "Linux password with all special characters", - os: "linux", - input: "~!@#$%^&*()_+{`}|:\"<>?-=[]\\;',./", - expected: "'~!@#$%^&*()_+{`}|:\"<>?-=[]\\;'\\'',./'", - }, - { - name: "Windows password with all special characters", - os: "windows", - input: "~!@#$%^&*()_+{`}|:\"<>?-=[]\\;',./", - expected: "'~!@#$%^&*()_+{`}|:\"<>?-=[]\\;'',./'", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := escapeValuesForCLI(tt.input, func() string { return tt.os }) - assert.Equal(t, tt.expected, result, fmt.Sprintf("Failed for OS: %s and password: %s", tt.os, tt.input)) - }) - } -} diff --git a/pkg/cloudfoundry/CloudFoundry_test.go b/pkg/cloudfoundry/CloudFoundry_test.go index e480f46e03..57aaf63629 100644 --- a/pkg/cloudfoundry/CloudFoundry_test.go +++ b/pkg/cloudfoundry/CloudFoundry_test.go @@ -1,4 +1,5 @@ //go:build unit +// +build unit package cloudfoundry @@ -86,8 +87,8 @@ func TestCloudFoundryLogin(t *testing.T) { "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", - "-u", "'testUser'", - "-p", "'testPassword'", + "-u", "testUser", + "-p", "testPassword", }}, }, m.Calls) } @@ -116,8 +117,8 @@ func TestCloudFoundryLogin(t *testing.T) { "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", - "-u", "'testUser'", - "-p", "'testPassword'", + "-u", "testUser", + "-p", "testPassword", }}, }, m.Calls) } @@ -149,8 +150,8 @@ func TestCloudFoundryLogin(t *testing.T) { "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", - "-u", "'testUser'", - "-p", "'testPassword'", + "-u", "testUser", + "-p", "testPassword", "--skip-ssl-validation", "--origin", "ldap", }}, @@ -183,7 +184,7 @@ func TestCloudFoundryReadServiceKeyAbapEnvironment(t *testing.T) { const password = "test_password" const serviceKey = ` cf comment test \n\n - {"sap.cloud.service":"com.sap.cloud.abap","url": "` + testURL + `" ,"systemid":"H01","abap":{"username":"'` + username + `'","password":"'` + password + `'","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}` + {"sap.cloud.service":"com.sap.cloud.abap","url": "` + testURL + `" ,"systemid":"H01","abap":{"username":"` + username + `","password":"` + password + `","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}` m.StdoutReturn = map[string]string{"cf service-key testInstance testServiceKeyName": serviceKey} @@ -212,13 +213,13 @@ func TestCloudFoundryReadServiceKeyAbapEnvironment(t *testing.T) { "-a", "https://api.endpoint.com", "-o", "testOrg", "-s", "testSpace", - "-u", "'testUser'", - "-p", "'testPassword'", + "-u", "testUser", + "-p", "testPassword", }}, {Exec: "cf", Params: []string{"service-key", "testInstance", "testServiceKeyName"}}, {Exec: "cf", Params: []string{"logout"}}, }, m.Calls) } - assert.Equal(t, ` {"sap.cloud.service":"com.sap.cloud.abap","url": "`+testURL+`" ,"systemid":"H01","abap":{"username":"'`+username+`'","password":"'`+password+`'","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}`, abapServiceKey) + assert.Equal(t, ` {"sap.cloud.service":"com.sap.cloud.abap","url": "`+testURL+`" ,"systemid":"H01","abap":{"username":"`+username+`","password":"`+password+`","communication_scenario_id": "SAP_COM_0510","communication_arrangement_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_system_id": "SK_I6CBIRFZPPJDKYNATQA32W","communication_inbound_user_id": "CC0000000001","communication_inbound_user_auth_mode": "2"},"binding":{"env": "cf","version": "0.0.1.1","type": "basic","id": "i6cBiRfZppJdKynaTqa32W"},"preserve_host_header": true}`, abapServiceKey) }) } diff --git a/pkg/cloudfoundry/ManifestUtils_test.go b/pkg/cloudfoundry/ManifestUtils_test.go index abe5a58666..c7f27d62d3 100644 --- a/pkg/cloudfoundry/ManifestUtils_test.go +++ b/pkg/cloudfoundry/ManifestUtils_test.go @@ -1,4 +1,5 @@ //go:build unit +// +build unit package cloudfoundry diff --git a/pkg/cloudfoundry/Vars_test.go b/pkg/cloudfoundry/Vars_test.go index 11d4600c06..66b74ee751 100644 --- a/pkg/cloudfoundry/Vars_test.go +++ b/pkg/cloudfoundry/Vars_test.go @@ -1,4 +1,5 @@ //go:build unit +// +build unit package cloudfoundry diff --git a/pkg/cpi/mockingUtils.go b/pkg/cpi/mockingUtils.go index 6c0fa13c04..50f4217ed6 100644 --- a/pkg/cpi/mockingUtils.go +++ b/pkg/cpi/mockingUtils.go @@ -309,9 +309,9 @@ func IntegrationDesigntimeArtifactUpdateMockResponse(testType string) (*http.Res // GetMockResponseByTestTypeAndMockFunctionName - Get mock response by testtype and mock function name func GetMockResponseByTestTypeAndMockFunctionName(mockFuntionName, testType string) (*http.Response, error) { - response, err := GetPositiveCaseResponseByTestType(testType) + response, error := GetPositiveCaseResponseByTestType(testType) - if response == nil && err == nil { + if response == nil && error == nil { switch mockFuntionName { @@ -355,7 +355,7 @@ func GetMockResponseByTestTypeAndMockFunctionName(mockFuntionName, testType stri return &res, errors.New("Unable to get integration artifact deploy error status, Response Status code:400") } } - return response, err + return response, error } // NegtiveResForIntegrationArtifactGenericCommandMockResponse -Nagative Case http response body @@ -377,9 +377,9 @@ func NegtiveResForIntegrationArtifactGenericCommandMockResponse(message string) // UpdateIntegrationDesigntimeArtifactMockResponse -Provide http respose body func UpdateIntegrationDesigntimeArtifactMockResponse(testType string) (*http.Response, error) { - response, err := GetRespBodyHTTPStatusCreated() + response, error := GetRespBodyHTTPStatusCreated() - if response == nil && err == nil { + if response == nil && error == nil { res := http.Response{ StatusCode: 400, @@ -393,7 +393,7 @@ func UpdateIntegrationDesigntimeArtifactMockResponse(testType string) (*http.Res } return &res, errors.New("Unable to get status of integration artifact, Response Status code:400") } - return response, err + return response, error } // IntegrationArtifactDownloadCommandMockResponsePositiveCaseRespBody -Provide http respose body for positive case From 85dfe3e18dd02850fd803b336a6270cc9f44651f Mon Sep 17 00:00:00 2001 From: Ivan Nikiforov Date: Fri, 27 Dec 2024 15:09:00 +0100 Subject: [PATCH 12/12] fix(cloudFoundryDeploy): argument list too long (#5216) * Increate command line limits * Increase new limits only to cf-cli image * Increase execution environment limits with static values * Remove unsupported operation * Increase docker container limits * Show current environment limits on container start * Clean up * Display environment limits only in verbose mode --------- Co-authored-by: Ivan Nikiforov --- cmd/cloudFoundryDeploy_generated.go | 2 +- resources/metadata/cloudFoundryDeploy.yaml | 5 +++++ vars/piperExecuteBin.groovy | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/cloudFoundryDeploy_generated.go b/cmd/cloudFoundryDeploy_generated.go index 51c4c51d7c..3c5b774990 100644 --- a/cmd/cloudFoundryDeploy_generated.go +++ b/cmd/cloudFoundryDeploy_generated.go @@ -565,7 +565,7 @@ func cloudFoundryDeployMetadata() config.StepData { }, }, Containers: []config.Container{ - {Name: "cfDeploy", Image: "ppiper/cf-cli:latest"}, + {Name: "cfDeploy", Image: "ppiper/cf-cli:latest", Options: []config.Option{{Name: "--ulimit", Value: "stack=67108864:67108864"}, {Name: "--ulimit", Value: "nofile=65536:65536"}}}, }, Outputs: config.StepOutputs{ Resources: []config.StepResources{ diff --git a/resources/metadata/cloudFoundryDeploy.yaml b/resources/metadata/cloudFoundryDeploy.yaml index c49a5c5a30..921000fdd5 100644 --- a/resources/metadata/cloudFoundryDeploy.yaml +++ b/resources/metadata/cloudFoundryDeploy.yaml @@ -370,6 +370,11 @@ spec: containers: - name: cfDeploy image: ppiper/cf-cli:latest + options: + - name: --ulimit + value: "stack=67108864:67108864" # Stack Size (in bytes). 64mb + - name: --ulimit + value: "nofile=65536:65536" # Number of Open Files outputs: resources: - name: influx diff --git a/vars/piperExecuteBin.groovy b/vars/piperExecuteBin.groovy index 311242c81a..08dec74004 100644 --- a/vars/piperExecuteBin.groovy +++ b/vars/piperExecuteBin.groovy @@ -78,6 +78,11 @@ void call(Map parameters = [:], String stepName, String metadataFile, List crede try { try { credentialWrapper(config, credentialInfo) { + if (config.verbose) { // need to be set on step config + echo "[DEBUG] Current environment limits:" + sh "ulimit -a" + } + sh "${piperGoPath} ${stepName}${defaultConfigArgs}${customConfigArg}" } } finally {