How to use certificate files using withCredentials

350 views
Skip to first unread message

olivier blanc

unread,
Oct 4, 2018, 4:06:08 AM10/4/18
to Jenkins Users
I am trying to retrieve keystore from a PKCS12 file stored as credential.
Here is the code I use :

 
                                       withCredentials(bindings: [
                                                        certificate
(aliasVariable: 'alias', credentialsId: 'myId', keystoreVariable: 'keystoreName', passwordVariable: 'keyPassword')
                                               
]) {
                                               
def keystoreFormat = "PKCS12"
                                               
print "test : " + Secret(
                                               
try {

                                                       
try {
                                                                p12file
= new FileInputStream(keystoreName)
                                                       
} catch (error) {
                                                                println
"File p12 input stream error :"
                                                                println error
.toString()
                                                                println error
.getMessage()
                                                                 
print error.printStackTrace()
                                                               
throw error
                                                       
}
                                                       
                                                        println
"P12file opened"

                                                       
try {
                                                                keystore
= KeyStore.getInstance(keystoreFormat)
                                                                keystore
.load(p12file, keyPassword.toCharArray())
                                                       
} catch (error) {
                                                                println
"Loading KeyStore :"
                                                                println error
.toString()
                                                                println error
.getMessage()
                                                               
print error.printStackTrace()
                                                               
throw error
                                                       
}
                                                       
def key = keystore.getKey(alias, keyPassword.toCharArray())

                                                        println
"-----BEGIN PRIVATE KEY-----"
                                                        println key
.getEncoded().encodeBase64()
                                                        println
"-----END PRIVATE KEY-----"
                                               
} catch(error) {
                                                       
print "Error Reading p12 : "
                                                        println error
.toString()
                                                        println error
.getMessage()
                                                       
print error.printStackTrace()
                                                       
throw error
                                               
}




This code always result in 
File p12 input stream error :
[Pipeline] echo
java.io.FileNotFoundException: **** (No such file or directory)
[Pipeline] echo
**** (No such file or directory)

I could verify that the file exists, but I can't open it.

Any idea will be greatly appreciate.
Many thanks

olivier blanc

unread,
Oct 22, 2018, 1:54:35 AM10/22/18
to Jenkins Users
I finale use the kubernetes cli plugin that does the job.

Reply all
Reply to author
Forward
0 new messages