create-credentials-from-groovy

Issue

Could I create a credentials and append it to a folder through a groovy script?

Environment

  • CloudBees Jenkins Enterprise > 1.609.1.1
  • Jenkins LTS > 1.609.1
  • Jenkins > 1.600

Resolution

It is possible to manipulate credentials using groovy script, you could create/edit credentials and add it to Jenkins’s global scope or add it to a domain into a folder. Here there are two examples of how to do it.

Example groovy script to add credentials to Jenkins.

import com.cloudbees.plugins.credentials.impl.*;
import com.cloudbees.plugins.credentials.*;
import com.cloudbees.plugins.credentials.domains.*;

String keyfile = "/tmp/key"

Credentials c = (Credentials) new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL,java.util.UUID.randomUUID().toString(), "description", "user", "password")


def ksm1 = new CertificateCredentialsImpl.FileOnMasterKeyStoreSource(keyfile)
Credentials ck1 = new CertificateCredentialsImpl(CredentialsScope.GLOBAL,java.util.UUID.randomUUID().toString(), "description", "password", ksm1)

def ksm2 = new CertificateCredentialsImpl.UploadedKeyStoreSource(keyfile)
Credentials ck2 = new CertificateCredentialsImpl(CredentialsScope.GLOBAL,java.util.UUID.randomUUID().toString(), "description", "password", ksm2)

SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), c)
SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), ck1)
SystemCredentialsProvider.getInstance().getStore().addCredentials(Domain.global(), ck2)

Example groovy script to add credentials to Jenkins’s folder into global domain.

import jenkins.model.*
import com.cloudbees.hudson.plugins.folder.*;
import com.cloudbees.hudson.plugins.folder.properties.*;
import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider.FolderCredentialsProperty;
import com.cloudbees.plugins.credentials.impl.*;
import com.cloudbees.plugins.credentials.*;
import com.cloudbees.plugins.credentials.domains.*;

jenkins = Jenkins.instance

String id = java.util.UUID.randomUUID().toString()
Credentials c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, id, "description:"+id, "user", "password")

for (folder in jenkins.getAllItems(Folder.class)) {
  if(folder.name.equals('FolderName')){
	AbstractFolder<?> folderAbs = AbstractFolder.class.cast(folder)
    FolderCredentialsProperty property = folderAbs.getProperties().get(FolderCredentialsProperty.class)
    property.getStore().addCredentials(Domain.global(), c)
    println property.getCredentials().toString()
  }
}
Have more questions? Submit a request

5 Comments

  • 0
    Avatar
    Alexander Ost

    The folder example will only work if the folder already has a FolderCredentialsProperty (e.g., because some credentials were defined from GUI side before). Newly created folders do not have that, and the example will fail.

    How to proceed in that case?

  • 0
    Avatar
    Allan Burdajewicz

    In that case, you can create a new FolderCredentialsProperty - which can take a list of credentials as parameter:

     

    --------

    [...]

    if(property) {
        println "Add credentials in global store"
        property.getStore().addCredentials(Domain.global(), c)
    } else {
        println "Initialize Folder Credentials store and add credentials in global store"
        property = new FolderCredentialsProperty([c])
        folderAbs.addProperty(property)
    }

    --------

     

    See source code of FolderCredentialsProvider.FolderCredentialsProperty

    Edited by Allan Burdajewicz
  • 0
    Avatar
    Marc Sutter

     Hi,

    Thanks for the folder example. It works fine if used as init.groovy.d script. However it doesn't seems to work if used in a jenkinsfile or a groovy dsl file.

    following import fails

    import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider.FolderCredentialsProperty;

    Any hints how to use it from a groovy dsl script ?

  • 0
    Avatar
    Arnaud Heritier

    Hi Marc,

     

      You have the (CloudBees) Folder plugin installed ? In which version ?

  • 0
    Avatar
    Marc Sutter

    Hi Arnaud,

    I'm running jenkins LTS 2.32.2 with the folder plugin version 5.17. 

    Cheers

Please sign in to leave a comment.