Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installation of credentials via API #5

Closed
closconsultancy opened this issue Jun 5, 2016 · 6 comments
Closed

Installation of credentials via API #5

closconsultancy opened this issue Jun 5, 2016 · 6 comments

Comments

@closconsultancy
Copy link

As per this example:

arangamani/jenkins_api_client#162

Is it possible to install Google Service account credentials (Google Service Account from JSON or P12) via an API request such as cURL ?

Reason for asking is this would then allow full scripting of the Jenkins job creation (config.xml) and credentials associated with a GCP Git Repository.

@msheiny
Copy link

msheiny commented May 30, 2018

I'm not a native java/groovy person... so I cobbled this together after looking thru lots and LOTS of other groovy examples and pouring thru the source code:

import jenkins.model.*
import com.cloudbees.plugins.credentials.*
import org.apache.commons.fileupload.FileItem
import com.cloudbees.plugins.credentials.domains.*
import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials
import java.io.FileInputStream


domain = Domain.global()
store = Jenkins.instance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()

def filey = new FileInputStream('/tmp/google-creds-on-jenkins-master.json')
fileItem = [ getSize: { return 1L }, getInputStream: { return filey } ] as FileItem


def ServiceAccount = new JsonServiceAccountConfig(fileItem, null)

def GoogleAccount = new GoogleRobotPrivateKeyCredentials("project-id", ServiceAccount, null)


store.addCredentials(domain, GoogleAccount)

@chris-vest
Copy link

@msheiny This is exactly what I am looking for! How are you currently running that? As part of a set of startup scripts?

@msheiny
Copy link

msheiny commented Jun 8, 2018

How are you currently running that? As part of a set of startup scripts?

@chris-vest yay drops it into groovy.init.d .. see https://github.com/freedomofpress/ansible-role-jenkins-config/blob/b0edb027f474a004d91622a48269b9328b74a0f0/templates/creds.groovy#L73

@chris-vest
Copy link

@msheiny Very much appreciated, thank you!

@gvirtuoso
Copy link

gvirtuoso commented Jun 14, 2019

Hi @msheiny,

Your code didn't work for me, so I did some improvements now it's working. (Jun/2019).
Anyway, thank you so much. Your code was the inspiration. ;-)

import org.apache.commons.io.FileUtils
import org.apache.commons.fileupload.FileItem
import com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotPrivateKeyCredentials

def jenkinsInstance = Jenkins.getInstance()

// Google Credential
def googleCredentialId = "my-credential-id"
def googleCredentialFile = new File("/path/to/my/json/file.json")
def googleCredentialFileInputStream = new FileInputStream(googleCredentialFile)
def googleCredentialFileItem = [ getName: { return googleCredentialFile.getName() }, getSize: { return 1L }, getInputStream: { return googleCredentialFileInputStream }, get: { return FileUtils.readFileToByteArray(googleCredentialFile) } ] as FileItem
def googleJsonServiceAccountConfig = new JsonServiceAccountConfig(googleCredentialFileItem, null)
def googleCredential = new GoogleRobotPrivateKeyCredentials(googleCredentialId, googleJsonServiceAccountConfig, null)

// Saving
def domain = Domain.global()
def store = jenkinsInstance.getExtensionList('com.cloudbees.plugins.credentials.SystemCredentialsProvider')[0].getStore()
store.addCredentials(domain, googleCredential)

Hope that it helps you guys too.

@stephenashank
Copy link
Contributor

Resolved in #48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants