24
24
package com .cloudbees .jenkins .plugins .sshagent ;
25
25
26
26
import com .cloudbees .jenkins .plugins .sshcredentials .SSHUser ;
27
+ import com .cloudbees .jenkins .plugins .sshcredentials .SSHUserListBoxModel ;
27
28
import com .cloudbees .jenkins .plugins .sshcredentials .SSHUserPrivateKey ;
28
29
import com .cloudbees .plugins .credentials .CredentialsProvider ;
30
+ import com .cloudbees .plugins .credentials .common .StandardUsernameCredentials ;
31
+ import com .cloudbees .plugins .credentials .domains .DomainRequirement ;
29
32
import edu .umd .cs .findbugs .annotations .NonNull ;
30
33
import hudson .Extension ;
31
34
import hudson .Launcher ;
35
+ import hudson .Util ;
32
36
import hudson .model .AbstractBuild ;
33
37
import hudson .model .AbstractProject ;
34
38
import hudson .model .BuildListener ;
39
43
import hudson .tasks .BuildWrapperDescriptor ;
40
44
import hudson .util .ListBoxModel ;
41
45
import hudson .util .Secret ;
42
- import org .apache .commons .lang .StringUtils ;
43
46
import org .kohsuke .stapler .DataBoundConstructor ;
44
47
import org .kohsuke .stapler .Stapler ;
45
48
46
49
import java .io .IOException ;
50
+ import java .util .Collections ;
47
51
import java .util .LinkedHashMap ;
48
52
import java .util .Map ;
49
53
@@ -59,7 +63,7 @@ public class SSHAgentBuildWrapper extends BuildWrapper {
59
63
/**
60
64
* Constructs a new instance.
61
65
*
62
- * @param user the {@link com.cloudbees.jenkins.plugins.sshcredentials.SSHUser #getId()} of the credentials to use.
66
+ * @param user the {@link SSHUserPrivateKey #getId()} of the credentials to use.
63
67
*/
64
68
@ DataBoundConstructor
65
69
@ SuppressWarnings ("unused" ) // used via stapler
@@ -68,9 +72,9 @@ public SSHAgentBuildWrapper(String user) {
68
72
}
69
73
70
74
/**
71
- * Gets the {@link com.cloudbees.jenkins.plugins.sshcredentials.SSHUser #getId()} of the credentials to use.
75
+ * Gets the {@link SSHUserPrivateKey #getId()} of the credentials to use.
72
76
*
73
- * @return the {@link com.cloudbees.jenkins.plugins.sshcredentials.SSHUser #getId()} of the credentials to use.
77
+ * @return the {@link SSHUserPrivateKey #getId()} of the credentials to use.
74
78
*/
75
79
@ SuppressWarnings ("unused" ) // used via stapler
76
80
public String getUser () {
@@ -85,7 +89,7 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, BuildList
85
89
throws IOException , InterruptedException {
86
90
SSHUserPrivateKey userPrivateKey = null ;
87
91
for (SSHUserPrivateKey u : CredentialsProvider
88
- .lookupCredentials (SSHUserPrivateKey .class , build .getProject (), ACL .SYSTEM )) {
92
+ .lookupCredentials (SSHUserPrivateKey .class , build .getProject (), ACL .SYSTEM , null )) {
89
93
if (user .equals (u .getId ())) {
90
94
userPrivateKey = u ;
91
95
break ;
@@ -107,12 +111,13 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, BuildList
107
111
/**
108
112
* Helper method that returns a safe description of a {@link SSHUser}.
109
113
*
110
- * @param sshUser the credentials.
114
+ * @param c the credentials.
111
115
* @return the description.
112
116
*/
113
117
@ NonNull
114
- private static String description (@ NonNull SSHUser sshUser ) {
115
- return StringUtils .isEmpty (sshUser .getDescription ()) ? sshUser .getUsername () : sshUser .getDescription ();
118
+ private static String description (@ NonNull StandardUsernameCredentials c ) {
119
+ String description = Util .fixEmptyAndTrim (c .getDescription ());
120
+ return c .getUsername () + (description != null ? " (" + description + ")" : "" );
116
121
}
117
122
118
123
/**
@@ -144,15 +149,11 @@ public String getDisplayName() {
144
149
*/
145
150
@ SuppressWarnings ("unused" ) // used by stapler
146
151
public ListBoxModel doFillUserItems () {
147
- ListBoxModel m = new ListBoxModel ();
148
-
149
152
Item item = Stapler .getCurrentRequest ().findAncestorObject (Item .class );
150
- // we only want the users with private keys as they are the only ones valid for an agent
151
- for (SSHUser u : CredentialsProvider .lookupCredentials (SSHUserPrivateKey .class , item , ACL .SYSTEM )) {
152
- m .add (description (u ), u .getId ());
153
- }
154
-
155
- return m ;
153
+ return new SSHUserListBoxModel ().withAll (
154
+ CredentialsProvider .lookupCredentials (SSHUserPrivateKey .class , item , ACL .SYSTEM ,
155
+ Collections .<DomainRequirement >emptyList ())
156
+ );
156
157
}
157
158
158
159
}
@@ -179,7 +180,7 @@ public SSHAgentEnvironment(Launcher launcher, final BuildListener listener,
179
180
final SSHUserPrivateKey sshUserPrivateKey ) throws Throwable {
180
181
RemoteAgent agent = null ;
181
182
listener .getLogger ().println ("[ssh-agent] Looking for ssh-agent implementation..." );
182
- Map <String ,Throwable > faults = new LinkedHashMap <String , Throwable >();
183
+ Map <String , Throwable > faults = new LinkedHashMap <String , Throwable >();
183
184
for (RemoteAgentFactory factory : Hudson .getInstance ().getExtensionList (RemoteAgentFactory .class )) {
184
185
if (factory .isSupported (launcher , listener )) {
185
186
try {
@@ -194,17 +195,18 @@ public SSHAgentEnvironment(Launcher launcher, final BuildListener listener,
194
195
if (agent == null ) {
195
196
listener .getLogger ().println ("[ssh-agent] FATAL: Could not find a suitable ssh-agent provider" );
196
197
listener .getLogger ().println ("[ssh-agent] Diagnostic report" );
197
- for (Map .Entry <String ,Throwable > fault : faults .entrySet ()) {
198
+ for (Map .Entry <String , Throwable > fault : faults .entrySet ()) {
198
199
listener .getLogger ().println ("[ssh-agent] * " + fault .getKey ());
199
200
fault .getValue ().printStackTrace (listener .getLogger ());
200
201
}
201
202
throw new RuntimeException ("[ssh-agent] Could not find a suitable ssh-agent provider." );
202
203
}
203
204
this .agent = agent ;
204
205
final Secret passphrase = sshUserPrivateKey .getPassphrase ();
205
- agent .addIdentity (sshUserPrivateKey .getPrivateKey (),
206
- passphrase == null ? null : passphrase .getPlainText (),
207
- description (sshUserPrivateKey ));
206
+ final String effectivePassphrase = passphrase == null ? null : passphrase .getPlainText ();
207
+ for (String privateKey : sshUserPrivateKey .getPrivateKeys ()) {
208
+ agent .addIdentity (privateKey , effectivePassphrase , description (sshUserPrivateKey ));
209
+ }
208
210
listener .getLogger ().println (Messages .SSHAgentBuildWrapper_Started ());
209
211
}
210
212
0 commit comments