From cac595b4bbb85f5151bd650b5d85aaac80a90535 Mon Sep 17 00:00:00 2001 From: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com> Date: Tue, 12 Nov 2019 17:40:59 +0100 Subject: [PATCH] Use credentialsId in step abapEnvironmentPullGitRepo (#974) * Add option for credentialsId * Remove mandatory username and password * Add null checks --- .../docs/steps/abapEnvironmentPullGitRepo.md | 16 ++++++--------- .../AbapEnvironmentPullGitRepoTest.groovy | 16 ++++++++------- vars/abapEnvironmentPullGitRepo.groovy | 20 +++++++++---------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/documentation/docs/steps/abapEnvironmentPullGitRepo.md b/documentation/docs/steps/abapEnvironmentPullGitRepo.md index aa6f7503f..b199d9ca6 100644 --- a/documentation/docs/steps/abapEnvironmentPullGitRepo.md +++ b/documentation/docs/steps/abapEnvironmentPullGitRepo.md @@ -6,7 +6,6 @@ * A SAP Cloud Platform ABAP Environment system is available. * On this system, a [Communication User](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/0377adea0401467f939827242c1f4014.html), a [Communication System](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/1bfe32ae08074b7186e375ab425fb114.html) and a [Communication Arrangement](https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/a0771f6765f54e1c8193ad8582a32edb.html) is setup for the Communication Scenario "SAP Cloud Platform ABAP Environment - Software Component Test Integration (SAP_COM_0510)". -* It is recommended to use the Jenkins credentials configuration for user and password handling and wrap the call to "abapEnvironmentPullGitRepo" with the Jenkins Step "withCredentials". ## ${docGenParameters} @@ -17,13 +16,10 @@ ## Example ```groovy -withCredentials([usernamePassword(credentialsId: 'myCredentialsId', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) { - abapEnvironmentPullGitRepo( - host : ' 1234-abcd-5678-efgh-ijk.abap.eu10.hana.ondemand.com', - repositoryName : '/DMO/GIT_REPOSITORY', - username : "\$USER", - password : "\$PASSWORD", - script : this - ) -} +abapEnvironmentPullGitRepo ( + host : '1234-abcd-5678-efgh-ijk.abap.eu10.hana.ondemand.com', + repositoryName : '/DMO/GIT_REPOSITORY', + credentialsId : "myCredentialsId", + script : this +) ``` diff --git a/test/groovy/AbapEnvironmentPullGitRepoTest.groovy b/test/groovy/AbapEnvironmentPullGitRepoTest.groovy index 244de7418..7e98dd344 100644 --- a/test/groovy/AbapEnvironmentPullGitRepoTest.groovy +++ b/test/groovy/AbapEnvironmentPullGitRepoTest.groovy @@ -27,6 +27,7 @@ public class AbapEnvironmentPullGitRepoTest extends BasePiperTest { private JenkinsStepRule stepRule = new JenkinsStepRule(this) private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this) + private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule(this).withCredentials('test_credentialsId', 'user', 'password') @Rule public RuleChain ruleChain = Rules.getCommonRules(this) @@ -34,6 +35,7 @@ public class AbapEnvironmentPullGitRepoTest extends BasePiperTest { .around(thrown) .around(stepRule) .around(loggingRule) + .around(credentialsRule) .around(shellRule) @Before @@ -57,7 +59,7 @@ public class AbapEnvironmentPullGitRepoTest extends BasePiperTest { loggingRule.expect("[abapEnvironmentPullGitRepo] Entity URI: https://example.com/URI") loggingRule.expect("[abapEnvironmentPullGitRepo] Pull Status: SUCCESS") - stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', username: 'user', password: 'password') + stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', credentialsId: 'test_credentialsId') assertThat(shellRule.shell[0], containsString(/#!\/bin\/bash curl -I -X GET https:\/\/example.com\/sap\/opu\/odata\/sap\/MANAGE_GIT_REPOSITORY\/Pull -H 'Authorization: Basic dXNlcjpwYXNzd29yZA==' -H 'Accept: application\/json' -H 'x-csrf-token: fetch' -D headerFileAuth-1.txt/)) assertThat(shellRule.shell[1], containsString(/#!\/bin\/bash curl -X POST "https:\/\/example.com\/sap\/opu\/odata\/sap\/MANAGE_GIT_REPOSITORY\/Pull" -H 'Authorization: Basic dXNlcjpwYXNzd29yZA==' -H 'Accept: application\/json' -H 'Content-Type: application\/json' -H 'x-csrf-token: TOKEN' --cookie headerFileAuth-1.txt -D headerFilePost-1.txt -d '{ "sc_name": "Z_DEMO_DM" }'/)) @@ -83,7 +85,7 @@ public class AbapEnvironmentPullGitRepoTest extends BasePiperTest { thrown.expect(Exception) thrown.expectMessage("[abapEnvironmentPullGitRepo] Pull Failed") - stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', username: 'user', password: 'password') + stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', credentialsId: 'test_credentialsId') } @@ -104,7 +106,7 @@ public class AbapEnvironmentPullGitRepoTest extends BasePiperTest { thrown.expect(Exception) thrown.expectMessage("[abapEnvironmentPullGitRepo] Pull Failed") - stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', username: 'user', password: 'password') + stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', credentialsId: 'test_credentialsId') } @@ -122,7 +124,7 @@ public class AbapEnvironmentPullGitRepoTest extends BasePiperTest { thrown.expect(Exception) thrown.expectMessage("[abapEnvironmentPullGitRepo] Error: text") - stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', username: 'user', password: 'password') + stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', credentialsId: 'test_credentialsId') } @@ -139,7 +141,7 @@ public class AbapEnvironmentPullGitRepoTest extends BasePiperTest { thrown.expect(Exception) thrown.expectMessage("[abapEnvironmentPullGitRepo] Error: 401 Unauthorized") - stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', username: 'user', password: 'password') + stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', repositoryName: 'Z_DEMO_DM', credentialsId: 'test_credentialsId') } @@ -147,14 +149,14 @@ public class AbapEnvironmentPullGitRepoTest extends BasePiperTest { public void checkRepositoryProvided() { thrown.expect(IllegalArgumentException) thrown.expectMessage("Repository / Software Component not provided") - stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', username: 'user', password: 'password') + stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, host: 'example.com', credentialsId: 'test_credentialsId') } @Test public void checkHostProvided() { thrown.expect(IllegalArgumentException) thrown.expectMessage("Host not provided") - stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, repositoryName: 'REPO', username: 'user', password: 'password') + stepRule.step.abapEnvironmentPullGitRepo(script: nullScript, repositoryName: 'REPO', credentialsId: 'test_credentialsId') } @Test diff --git a/vars/abapEnvironmentPullGitRepo.groovy b/vars/abapEnvironmentPullGitRepo.groovy index fa5d7f3d9..af1705ba5 100644 --- a/vars/abapEnvironmentPullGitRepo.groovy +++ b/vars/abapEnvironmentPullGitRepo.groovy @@ -22,13 +22,9 @@ import java.util.UUID ] @Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([ /** - * Specifies the communication user of the communication scenario SAP_COM_0510 + * Jenkins CredentialsId containing the communication user and password of the communciation scenario SAP_COM_0510 */ - 'username', - /** - * Specifies the password of the communication user - */ - 'password' + 'credentialsId' ]) @Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS /** @@ -58,12 +54,15 @@ void call(Map parameters = [:]) { .collectValidationFailures() .withMandatoryProperty('host', 'Host not provided') .withMandatoryProperty('repositoryName', 'Repository / Software Component not provided') - .withMandatoryProperty('username') - .withMandatoryProperty('password') + .withMandatoryProperty('credentialsId') .use() - String usernameColonPassword = configuration.username + ":" + configuration.password - String authToken = usernameColonPassword.bytes.encodeBase64().toString() + String authToken + withCredentials([usernamePassword(credentialsId: configuration.credentialsId, usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) { + String userColonPassword = "${USER}:${PASSWORD}" + authToken = userColonPassword.bytes.encodeBase64().toString() + } + String urlString = 'https://' + configuration.host + '/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull' echo "[${STEP_NAME}] General Parameters: URL = \"${urlString}\", repositoryName = \"${configuration.repositoryName}\"" HeaderFiles headerFiles = new HeaderFiles() @@ -133,7 +132,6 @@ private String triggerPull(Map configuration, String url, String authToken, Head private String pollPullStatus(String url, String authToken, HeaderFiles headerFiles) { - String headerFile = "headerPoll.txt" String status = "R"; while(status == "R") {