jenkins password encryption, escaping ${} characters - encryption

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?

Just use single quotes
def secret = Secret.fromString('${PASSWD}')

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.

Related

Replace "[ with [ using shell script

while fetching the data using API, Json response file is created. but it is not standARD Json. file format We are getting extra double quote in ResponsejSON VARIABLE.
"Status":"S","Message":"Success","ResponseJson":**"**[{
so how to remove single double qoute.
Tried using the sed command on the whole string you provided and was able to replace using the below commands, essentially, we need to escape the special characters while using sed to substitute. Assuming you have these patterns on stored in a file, run the below and to escape all the special characters (*,",[) add a \ preceding it. for ex: * becomes \* .
given: "Status":"S","Message":"Success","ResponseJson":**"**[{
command: sed 's/\*\*\"\*\*\[/\[/g' test.txt
output: "Status":"S","Message":"Success","ResponseJson":[{
given: "[
command: sed 's/\"\[/\[/g' test.txt
Output: [

R Shiny: Is there any way to read a single backslash in user input?

I'm making a Shiny app that constructs a bash script to run on a cluster (basically just a txt file). One of the user inputs is a curl command (provided by the database where the files are stored) that they can copy/paste into a textInput field in the app. When run on the cluster, it will download the file for further processing. However, the curl command they provide contains several single backslashes. Example:
curl --cookie jgi_session=/api/sessions/ec32f2d578304a9e62b4646ae2bec6d4 --output download.20210731.211924.zip -d "{\"ids\":[\"5d94dc9fc0d65a87debccfd3\"]}" -H "Content-Type: application/json" https://files.jgi.doe.gov/filedownload/
It works fine if I paste this directly into a script or if I manually add in double backslashes, but I want to keep this as user friendly as possible. Every other post I've seen about this just says to use double backslashes, but I'd rather do this automatically if at all possible. So any ideas? I'm open to alternate solutions, less work for the user the better.
Your code is picking up the curl line as escaped characters. When you write to file, those escaped characters get converted to their actual character (i.e \" gets converted to literal ".
To avoid, replace special escaped characters by the character sequence that literally created the escape sequence. So to build \" in the final written string, you have to produce \\" as escaped character sequence (which is what the output of a print commmand should show).
Once way to achieve this for this particular character sequence is
escapedString = gsub('\"', '\\"', curlString)
Note that, in terms of string interpretation, \" is a single character (converting to "), while \\" is a sequence of two characters: an escaped \ and a literal ", converting to \" when written, which is the desired output.

How to quote string with backtick in zsh?

I need to mount an SMB share with a line like
mount_smbfs //username:password#server.com/folder/ mountpoint
But my password contains backticks!
How would I quote this?
I've tried:
mount_smbfs //username:\`123\`123#server.com/folder/ mountpoint
And single quotes:
mount_smbfs '//username:`123`123#server.com/folder/' mountpoint
And a variable:
pw='`123`123'
mount_smbfs //username:$pw#server.com/folder/ mountpoint
All of which give me
mount_smbfs: URL parsing failed, please correct the URL and try again: Invalid argument
The same way you do any string that contains characters that need to be escaped:
mount_smbfs //username:my\`password#server.com/folder/
or
mount_smbfs '//username:my`password#server.com/folder/'
You can't use double quotes alone, because the backtick in a double-quoted string is interpreted as the start of a command substitution. You could write
mount_smbfs "//username:my\`password#server.com/folder/"
Based on the error message, the problem isn't protecting the backtick from the shell, but from the URL parsing library. Try
mount_smbfs '//username:%60123%60123#server.com/folder/' mountpoint
where %60 is the URL-quoted form for a backtick. Quoting the URL anyway is good practice: don't expose anything you don't want processed to the shell, even if you are sure there's nothing that the shell would process.

How to handle non-printable ASCII character parameters?

I'm working on a project where we are dealing with importing/exporting data from database tables using ksh scripts and Perl scripts. We have an existing process to export data from a table to a file and it is then imported into another system.
Here's the catch - the export process dumps out pipe delimited files while the system that is doing the import expects files delimited by the ASCII group separator character which is decimal 29, hexidecimal 1d, or octal 35. It shows up in vi as ^] Right now, I'm converting the delimiter via a Perl script. What I'd like to do is tell our export process to just use the delimiter we are expecting. Something like:
export_table.ksh -d '\035'
The problem is I can't figure out how to pass this character to the export script.
I've tried all kinds of combinations of single quotes, double quotes, backslashes, and the octal and hex version of this character.
I'm on Solaris 10 using ksh and/or Perl.
have you tried:
$'\x29'
actually try this for ]:
echo $'\x5d'
and for ^
echo $'\x5e'
so you just need to do:
export_table.ksh -d $'\x5e\x5d'
In bash(1), one can prefix a character with ^v to enter that character verbatim. Perhaps ksh(1) does the same?

Why is command line computed base64 string different than curl computed base64 string?

Really confused - Guess it has to do with a single character placement at the end, or possible padding done with basic digest that I'm not aware of..?
So, if I execute this, you can see the product of the base64 encode:
echo 'host#mail.com:password' | openssl enc -base64
aG9zdEBtYWlsLmNvbTpwYXNzd29yZAo=
Now, if I make a curl request:
curl -v -u host#mail.com:password https://
aG9zdEBtYWlsLmNvbTpwYXNzd29yZA==
You'll notice that the base64 strings are NOT the same..haha what? The base64 command line one is actually incorrect - if you substitute that in the request, it fails. SO - does basic digest NOT truly use a base64 string? I'm noticing that is always doing a o= instead of == at the end of the string ...
And ideas?
EDIT: So, it was the trailing newline from echo:
-n do not output the trailing newline
Thanks!
>>> 'aG9zdEBtYWlsLmNvbTpwYXNzd29yZA=='.decode('base64')
'host#mail.com:password'
>>> 'aG9zdEBtYWlsLmNvbTpwYXNzd29yZAo='.decode('base64')
'host#mail.com:password\n'
Try echo -n instead.

Resources