Skip to content
Permalink
Browse files
Merge pull request #16 from jenkinsci/Evildethow-feature/JENKINS-35983
feature/ [JENKINS-35983] merged changes from PR12
  • Loading branch information
escoem committed Dec 1, 2016
2 parents d1ed035 + 594157a commit e6e6a535de1debbbc8483271c3aaa705988f5a81
14 pom.xml
@@ -45,7 +45,7 @@
<url>http://wiki.jenkins-ci.org/display/JENKINS/Job+Import+Plugin</url>

<properties>
<jenkins.version>1.580.3</jenkins.version>
<jenkins.version>1.609.1</jenkins.version>
<java.level>6</java.level>
<findbugs.failOnError>false</findbugs.failOnError>
</properties>
@@ -103,6 +103,18 @@
<artifactId>credentials</artifactId>
<version>1.24</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>5.12</version>
</dependency>

<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>1.58</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
@@ -0,0 +1,59 @@
package org.jenkins.ci.plugins.jobimport;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.google.common.base.Strings;
import hudson.model.Item;
import hudson.security.ACL;

import java.util.Collections;
import java.util.List;

import static com.google.inject.internal.guava.base.$Preconditions.checkNotNull;

/**
* Created by evildethow on 28/06/2016.
*/
final class CredentialsUtils {

private CredentialsUtils() {
throw new UnsupportedOperationException("Cannot instantiate utility class");
}

static NullSafeCredentials getCredentials(String credentialId) {
if (!Strings.isNullOrEmpty(credentialId)) {
StandardUsernamePasswordCredentials cred = CredentialsMatchers.firstOrNull(allCredentials(), CredentialsMatchers.withId(credentialId));
if (cred != null) {
return new NullSafeCredentials(cred.getUsername(), cred.getPassword().getPlainText());
}
}
return new NullSafeCredentials();
}

static List<StandardUsernamePasswordCredentials> allCredentials() {
return CredentialsProvider.lookupCredentials(
StandardUsernamePasswordCredentials.class,
(Item) null,
ACL.SYSTEM,
Collections.<DomainRequirement>emptyList()
);
}

static final class NullSafeCredentials {

final String username;
final String password;

NullSafeCredentials(String username, String password) {
this.username = checkNotNull(username);
this.password = checkNotNull(password);
}

NullSafeCredentials() {
this.username = "";
this.password = "";
}
}
}
Loading

0 comments on commit e6e6a53

Please sign in to comment.