Accessing saved Credentials from a slave node

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Accessing saved Credentials from a slave node

Andrew Finley
Hi everyone,
I've run into an issue with the Jenkins plugin I'm working on and I could use some help.  The plugin uses a REST API with an API Token, and the API Token is stored in the Credentials plugin as a 'Secret Text' type.  
The plugin works fine when run from the master Jenkins instance, but if it's run on a slave node it crashes when trying to access the Credentials.

Below is the code we are using to access the credentials:

public String getCredentialWithId(String credentialId) {
 
List<StringCredentials> stringCredentials = fetchStringCredentials();
       
StringCredentials credential = CredentialsMatchers.firstOrNull(stringCredentials,
                                                                       
CredentialsMatchers.withId(credentialId));
       
String secret = null;
       
if (credential != null) {
            secret
= credential.getSecret().getPlainText();
       
}
       
return secret;
   
}


private List<StringCredentials> fetchStringCredentials() {
 
return CredentialsProvider.lookupCredentials(
 
StringCredentials.class,
 
Jenkins.getInstance(),
 ACL
.SYSTEM,
 
Collections.<DomainRequirement>emptyList()
 
);
}


The problem exists with 'Jenkins.getInstance()' in 'fetchStringCredentials()'.  This will return 'null' if it's not run on the master Jenkins instance.  Is there a way to access credentials stored in the Credentials
plugin when running on a slave node?  


I've tried using the 'CredentialsProvider.findCredentialById()' function to get the Credential in the below example.

    public String getCredentialsWithIdFromRun(String credentialsId, Run run) {
       
if (StringUtils.isBlank(credentialsId)) {
           
return null;
       
}
       
StringCredentials result = CredentialsProvider.findCredentialById(
                credentialsId
,
               
StringCredentials.class,
                run
,
               
Collections.<DomainRequirement>emptyList());

       
return result.getSecret().getPlainText();
   
}

However, this gives me the wrong value for the Credential ID (not null).  

Any advice would be appreciated.
Thanks,
Andrew

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/6cf5b508-155f-47f2-870d-502866d3cb1e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Accessing saved Credentials from a slave node

Jesse Glick-4
On Mon, Feb 11, 2019 at 6:24 PM Andrew Finley <[hidden email]> wrote:
> The problem exists with 'Jenkins.getInstance()' in 'fetchStringCredentials()'.  This will return 'null' if it's not run on the master Jenkins instance.  Is there a way to access credentials stored in the Credentials
> plugin when running on a slave node?

No, which is why you should look up the secret in the master-side
code, and then include that in a serializable field in your
`MasterToSlave[File]Callable`.

--
You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-dev/CANfRfr2wLAwW8XhM%3DgSRYvVCu6ou95bzAwOY-sxZoV9QySB29g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.