Sha
Sha

Reputation: 117

jenkins password encryption, escaping ${} characters

I am trying to encrypt the string ${PASSWD} using the following groovy script. using \ for escaping $ and \ for { or }

import hudson.util.Secret

def secret = Secret.fromString("\$\\{PASSWD\\}")
println(secret.getEncryptedValue())

def decrypt = Secret.fromString("/WaEf5KeDpbhnjW+hBmV3kmpmQbwoTFh2oI1yFSuUf0=")
println(decrypt.getPlainText())

I get the following output:

/WaEf5KeDpbhnjW+hBmV3kmpmQbwoTFh2oI1yFSuUf0=
/WaEf5KeDpbhnjW+hBmV3kmpmQbwoTFh2oI1yFSuUf0=

However, the desired output should have been

/WaEf5KeDpbhnjW+hBmV3kmpmQbwoTFh2oI1yFSuUf0=
${PASSWD}

It seems that I am not using escape characters properly. How can I pass ${PASSWD} as a string?

Upvotes: 0

Views: 6595

Answers (2)

Vincent Gerris
Vincent Gerris

Reputation: 7528

Depending on where you want to escape things, this is very useful: https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4

For using the secret storage, I needed to use the 6 times backslash to get 1 backslash, which I eventually needed in a sed command to escape forward slashes.

Upvotes: 0

tim_yates
tim_yates

Reputation: 171084

Just use single quotes

def secret = Secret.fromString('${PASSWD}')

Upvotes: 2

Related Questions