Use jenkins to inject masked password for use in code within the build - jenkins

My aim:
To use a password in jenkins which is masked after input and runtime.
I only need it for my job.
I can use it in my java code to login to a website.
Areas I have looked at:
The credentials plugin - this looks like the right area (but I'll need to get the sysadmins to add me as its locked down).
Also I can't find out how you can access the output?

Have a look into the EnvInject plugin, there you can define password parameters that are masked in console output and job configuration, they can be used like normal parameters.
The screenshot shows the configuration in the job to use a local password.
The shell script build step I used is
echo "myPassword = $myPassword"
The resulting console output is:
+ echo 'myPassword = [*******]'
myPassword = [*******]
To pass it to Java you can use it like any other parameter from the job configuration. The value itself is encrypted, so checking the job configuration as XML will not reveal the password either.

You can use the jenkins credentials binding plugin.
After installing the Credentials Bindings plugin, the Build Environment section of your build job Configuration page will include a new option:
Use secret text(s) or file(s)
Enabling this option allows you add credential bindings where the Variable value will be used as the name of the environment variable that your build can use to access the value of the credential.
See the full description of all steps here:
https://support.cloudbees.com/hc/en-us/articles/203802500-Injecting-Secrets-into-Jenkins-Build-Jobs

Related

how to use emailext-template in jenkins pipeline to get the template?

There is a good plugin, emailext-template. Basically, I do not have the access to Jenkins-server. It is not convenient to choose the option mentioned in emailext to create the email template.
I dont know how to use this emailext-template plugin to get the template in jenkins-pipeline. There is no guideline in emailext-template. Any documents?
// expected usage in jenkins-pipeline
template_str = emailext-template('template-id')
This can be done in the following way, In case you can deploy jenkins again (if it is done on the cloud), you could create and deploy your email template as part of jenkins installation and configuration.
In case you did not have access to the jenkins server even while deployment, you can just form the html template with the corresponding variables and then use them in the job configuration itself.
The below image shows how the template files are used in jenkins, Instead of the script tag as in the image, you can replace with your own html content so that jenkins will be able to send email with that content.

How to select password from dropdown and pass it as parameter to jenkins job?

I have a problem. I use Jenkins to deploy application on machines. Depending on which machine i want to deploy new version I need to use different database passwords to run db migrations. I want to store credentials in Jenkins and for each deploy job select credentials set from dropdown which will be passed (just password ) to powershell script. I have Credentials Binding Plugin and Extended Choice Parameter Plugin. I thought it might help me with my problem, but I cannot find solution for this. Do you have any ideas how to achieve this?
As I understand, you need only to do two things:
add Credential Parameter in This project is parameterized section for possibility to select credentials set from dropdown.
enable Use secret text(s) or file(s) option in Build Environment section. This will allow you to take credentials of various sorts and use them from shell build steps and the like. Each binding will define an environment variable.
If you have already created Jenkins credentials like these:
then you will be available to run your job using Build with Parameters button and select needed credentials:
So, after configuring, you don't need to dig in job configuration each time, all possible credentials will be automatically loaded, you need only select the needed one when run a job.

Jenkins2, managing configuration by puppet, credentials

I'd like to manage Jenkins 2 configuration by puppet.
I've copied configuration from deployed manually instance, and templated it.
But I've got problem with hashed passwords in configuration. When I'm putting old hash (from previous instance) jenkins is overwritting it, and password does not work. For example:
<managerDN><%= scope['ldap_bind_dn'] %></managerDN>
<managerPasswordSecret><%= scope['ldap_bind_pw'] %></managerPasswordSecret>
I've got BIND password and dn stored in hiera, and when I'm putting it from hiera and restart Jenkins, config is rewritten and new value does not work.
- <managerPasswordSecret>{HASH_FROM_HIERA=}</managerPasswordSecret>
+ <managerPasswordSecret>{NEW_NOT_WORKING_HASH=}</managerPasswordSecret>
I cannot figure out how to manage this credentials in a way Jenkins will accept them?
I've found solution, just instead of hash, puppet should place plain-text and jenkins automatically will replace it with proper hash value

Password not getting masked in jenkins

i am using jenkins masking plugin to mask the passwords.
I have an user (i054564) defined in credentials as username/password type
The user name /password is defined in the bindings section as well as in the mask password subsection of Build environment section.So whenever i am using that user/password in my build job as a variable, the password is not masked and logged in the console
Some snapshots have been attached to depict the problem
Snapshot -1 Build specific settings
Snapshot -2 Credentials settings
Snapshot -3 Manage Jenkins Mask password settings
Strange thing is initially it works and then after 2-3 builds it stops working.
please let me know if you need any further details
best Regards,
Saurav
You can use "Inject passwords to the build as environment variables" from Build Environment and defined its value as your "password parameter". ( password parameter - for which you can ask user to enter the password at run time). (e.g.: define inject password value as : ${password_parameter} ).
The password which you will pass while running job will display as [*******]

Hudson / Jenkins: share parameters between several jobs

I have about 20 jobs using common parameters (user, password), and sometimes the password expires... So I have to change it on all jobs, which is really time consuming (and error prone, I may forget one).
I thought about:
using a kind of magic property file if that exists to have directly lines like KEY, VALUE added into job parameters
adding the same kind of KEY, VALUE pair directly inside build.xml, but where ? And it's really ugly... Maybe with a dedicated XML embedded into the build.xml ?
calling a slave job that would (how ?) push up to the parent one the desired values...
As you can see I'm only starting in Hudson/Jenkins (I'm using Jenkins 1.424.2.2), thanks for your help !
EDIT: I'm not admin of the Jenkins instance, so I cannot have access to global properties...
Go to your Jenkins home and navigate :
Manage Jenkins >
Configure System >
Global properties >
Environment variables > ....
I can think of two approaches:
Use Global properties, found under Manage Jenkins -> Configure system. Here you can define environment variables that should be available to all jobs.
Write a small script that downloads, modifies and posts the job config: http://[jenkinshost]/job/[jobname]/config.xml.
You can read about the api capabilities under http://[jenkinshost]/job/[jobname]/api, here is what it says about reading and changing config.xml:
Fetch/Update config.xml
To programmatically obtain config.xml, hit
[http://[jenkinshost]/job/[jobname]/config.xml]. You can also POST
an updated config.xml to the same URL to programmatically update the
configuration of a job.
I eventually succeeded by:
keeping encrypted credentials in a web page
retrieving them in Hudson thanks to a shell script (wget), decrypt them, and creating a build.properties file in the workspace with lines name=value (in my case ssh.password=...)
This works, because Ant build steps detect this file and pass the variables inside into their context. Thanks to that I could centralize my credentials.

Resources