Skip to content

Commit 5e75517

Browse files
Fix JENKINS-31076 - [kubernetes cli] proper message if credentials are not defined or not found
1 parent 09f8690 commit 5e75517

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubectlBuildWrapper.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import com.cloudbees.plugins.credentials.common.UsernamePasswordCredentials;
99
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
1010
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
11+
import edu.umd.cs.findbugs.annotations.CheckForNull;
12+
import hudson.AbortException;
1113
import hudson.EnvVars;
1214
import hudson.Extension;
1315
import hudson.FilePath;
@@ -22,6 +24,7 @@
2224
import hudson.util.Secret;
2325
import jenkins.model.Jenkins;
2426
import jenkins.tasks.SimpleBuildWrapper;
27+
import org.apache.commons.lang.StringUtils;
2528
import org.kohsuke.stapler.AncestorInPath;
2629
import org.kohsuke.stapler.DataBoundConstructor;
2730
import org.kohsuke.stapler.QueryParameter;
@@ -65,13 +68,15 @@ public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher
6568
final StandardCredentials c = getCredentials();
6669

6770
String login;
68-
if (c instanceof TokenProducer) {
71+
if (c == null) {
72+
throw new AbortException("No credentials defined to setup Kubernetes CLI");
73+
} else if (c instanceof TokenProducer) {
6974
login = "--token=" + ((TokenProducer) c).getToken(serverUrl, null, true);
7075
} else if (c instanceof UsernamePasswordCredentials) {
7176
UsernamePasswordCredentials upc = (UsernamePasswordCredentials) c;
7277
login = "--username=" + upc.getUsername() + " --password=" + Secret.toString(upc.getPassword());
7378
} else {
74-
throw new IllegalStateException("Unsupported Credentials type "+c.getClass().getName());
79+
throw new AbortException("Unsupported Credentials type " + c.getClass().getName());
7580
}
7681

7782
status = launcher.launch()
@@ -95,13 +100,26 @@ public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher
95100
context.env("KUBECONFIG", configFile.getRemote());
96101
}
97102

98-
private StandardCredentials getCredentials() {
99-
return CredentialsMatchers.firstOrNull(
103+
/**
104+
* Get the {@link StandardCredentials}.
105+
*
106+
* @return the credentials matching the {@link #credentialsId} or {@code null} is {@code #credentialsId} is blank
107+
* @throws AbortException if no {@link StandardCredentials} matching {@link #credentialsId} is found
108+
*/
109+
@CheckForNull
110+
private StandardCredentials getCredentials() throws AbortException {
111+
if (StringUtils.isBlank(credentialsId)) {
112+
return null;
113+
}
114+
StandardCredentials result = CredentialsMatchers.firstOrNull(
100115
CredentialsProvider.lookupCredentials(StandardCredentials.class,
101116
Jenkins.getInstance(), ACL.SYSTEM, Collections.<DomainRequirement>emptyList()),
102117
CredentialsMatchers.withId(credentialsId)
103118
);
104-
119+
if (result == null) {
120+
throw new AbortException("No credentials found for id \"" + credentialsId + "\"");
121+
}
122+
return result;
105123
}
106124

107125
@Extension

0 commit comments

Comments
 (0)