1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Prepare credentials for forwarding as environment variables to piper go (#2432)

Prepare credentials for forwarding as environment variables to piper go
This commit is contained in:
Marcus Holl 2021-01-12 09:17:34 +01:00 committed by GitHub
parent 834dd1a1de
commit 93330d5ed2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 0 deletions

View File

@ -1336,11 +1336,13 @@ class CloudFoundryDeployTest extends BasePiperTest {
void testGoStepFeatureToggleOn() {
String calledStep = ''
String usedMetadataFile = ''
List credInfo = []
helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
Map parameters, String stepName,
String metadataFile, List credentialInfo ->
calledStep = stepName
usedMetadataFile = metadataFile
credInfo = credentialInfo
})
stepRule.step.cloudFoundryDeploy([
@ -1352,10 +1354,20 @@ class CloudFoundryDeployTest extends BasePiperTest {
cfOrg: 'irrelevant',
cfSpace: 'irrelevant',
cfCredentialsId: 'irrelevant',
mtaExtensionCredentials: [myCred: 'Mta.ExtensionCredential~Credential_Id1'],
])
assertEquals('cloudFoundryDeploy', calledStep)
assertEquals('metadata/cloudFoundryDeploy.yaml', usedMetadataFile)
// contains assertion does not work apparently when comparing a list of lists agains an expected list.
boolean found = false
credInfo.each { entry ->
if (entry == [type:'token', id:'Mta.ExtensionCredential~Credential_Id1', env:['MTA_EXTENSION_CREDENTIAL_CREDENTIAL_ID1'], resolveCredentialsId:false]) {
found = true
}
}
assertTrue(found)
}
@Test

View File

@ -243,6 +243,13 @@ void call(Map parameters = [:]) {
[type: 'usernamePassword', id: 'cfCredentialsId', env: ['PIPER_username', 'PIPER_password']],
[type: 'usernamePassword', id: 'dockerCredentialsId', env: ['PIPER_dockerUsername', 'PIPER_dockerPassword']]
]
if (config.mtaExtensionCredentials) {
config.mtaExtensionCredentials.each { key, credentialsId ->
echo "[INFO]${STEP_NAME}] Preparing credential for being used by piper-go. key: ${key}, credentialsId is: ${credentialsId}, exposed as environment variable ${toEnvVarKey(credentialsId)}"
credentials << [type: 'token', id: credentialsId, env: [toEnvVarKey(credentialsId)], resolveCredentialsId: false]
}
}
piperExecuteBin(parameters, STEP_NAME, 'metadata/cloudFoundryDeploy.yaml', credentials)
return
}
@ -316,6 +323,17 @@ void call(Map parameters = [:]) {
}
}
/*
* Inserts underscores before all upper case letters which are not already
* have an underscore before, replaces any non letters/digits with underscore
* and transforms all lower case letters to upper case.
*/
private static String toEnvVarKey(String key) {
key = key.replaceAll(/[^A-Za-z0-9]/, "_")
key = key.replaceAll(/(.)(?<!_)([A-Z])/, "\$1_\$2")
return key.toUpperCase()
}
private void handleMTADeployment(Map config, script) {
// set default mtar path
if(!config.mtaPath) {