8
8
import com .cloudbees .plugins .credentials .common .UsernamePasswordCredentials ;
9
9
import com .cloudbees .plugins .credentials .domains .DomainRequirement ;
10
10
import com .cloudbees .plugins .credentials .domains .URIRequirementBuilder ;
11
+ import edu .umd .cs .findbugs .annotations .CheckForNull ;
12
+ import hudson .AbortException ;
11
13
import hudson .EnvVars ;
12
14
import hudson .Extension ;
13
15
import hudson .FilePath ;
22
24
import hudson .util .Secret ;
23
25
import jenkins .model .Jenkins ;
24
26
import jenkins .tasks .SimpleBuildWrapper ;
27
+ import org .apache .commons .lang .StringUtils ;
25
28
import org .kohsuke .stapler .AncestorInPath ;
26
29
import org .kohsuke .stapler .DataBoundConstructor ;
27
30
import org .kohsuke .stapler .QueryParameter ;
@@ -65,13 +68,15 @@ public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher
65
68
final StandardCredentials c = getCredentials ();
66
69
67
70
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 ) {
69
74
login = "--token=" + ((TokenProducer ) c ).getToken (serverUrl , null , true );
70
75
} else if (c instanceof UsernamePasswordCredentials ) {
71
76
UsernamePasswordCredentials upc = (UsernamePasswordCredentials ) c ;
72
77
login = "--username=" + upc .getUsername () + " --password=" + Secret .toString (upc .getPassword ());
73
78
} else {
74
- throw new IllegalStateException ("Unsupported Credentials type " + c .getClass ().getName ());
79
+ throw new AbortException ("Unsupported Credentials type " + c .getClass ().getName ());
75
80
}
76
81
77
82
status = launcher .launch ()
@@ -95,13 +100,26 @@ public void setUp(Context context, Run<?, ?> build, FilePath workspace, Launcher
95
100
context .env ("KUBECONFIG" , configFile .getRemote ());
96
101
}
97
102
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 (
100
115
CredentialsProvider .lookupCredentials (StandardCredentials .class ,
101
116
Jenkins .getInstance (), ACL .SYSTEM , Collections .<DomainRequirement >emptyList ()),
102
117
CredentialsMatchers .withId (credentialsId )
103
118
);
104
-
119
+ if (result == null ) {
120
+ throw new AbortException ("No credentials found for id \" " + credentialsId + "\" " );
121
+ }
122
+ return result ;
105
123
}
106
124
107
125
@ Extension
0 commit comments