Skip to content

Commit 1bcbfa2

Browse files
committed
Upgrade to credentials:1.6 and ssh-credentials:1.0
1 parent eeab261 commit 1bcbfa2

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@
118118
<dependency>
119119
<groupId>org.jenkins-ci.plugins</groupId>
120120
<artifactId>credentials</artifactId>
121-
<version>1.3</version>
121+
<version>1.6</version>
122122
</dependency>
123123
<dependency>
124124
<groupId>org.jenkins-ci.plugins</groupId>
125125
<artifactId>ssh-credentials</artifactId>
126-
<version>0.3</version>
126+
<version>1.0</version>
127127
</dependency>
128128
<!-- jenkins dependencies -->
129129
<!-- test dependencies -->

src/main/java/com/cloudbees/jenkins/plugins/sshagent/SSHAgentBuildWrapper.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424
package com.cloudbees.jenkins.plugins.sshagent;
2525

2626
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUser;
27+
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserListBoxModel;
2728
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
2829
import com.cloudbees.plugins.credentials.CredentialsProvider;
30+
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
31+
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
2932
import edu.umd.cs.findbugs.annotations.NonNull;
3033
import hudson.Extension;
3134
import hudson.Launcher;
35+
import hudson.Util;
3236
import hudson.model.AbstractBuild;
3337
import hudson.model.AbstractProject;
3438
import hudson.model.BuildListener;
@@ -39,11 +43,11 @@
3943
import hudson.tasks.BuildWrapperDescriptor;
4044
import hudson.util.ListBoxModel;
4145
import hudson.util.Secret;
42-
import org.apache.commons.lang.StringUtils;
4346
import org.kohsuke.stapler.DataBoundConstructor;
4447
import org.kohsuke.stapler.Stapler;
4548

4649
import java.io.IOException;
50+
import java.util.Collections;
4751
import java.util.LinkedHashMap;
4852
import java.util.Map;
4953

@@ -59,7 +63,7 @@ public class SSHAgentBuildWrapper extends BuildWrapper {
5963
/**
6064
* Constructs a new instance.
6165
*
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.
6367
*/
6468
@DataBoundConstructor
6569
@SuppressWarnings("unused") // used via stapler
@@ -68,9 +72,9 @@ public SSHAgentBuildWrapper(String user) {
6872
}
6973

7074
/**
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.
7276
*
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.
7478
*/
7579
@SuppressWarnings("unused") // used via stapler
7680
public String getUser() {
@@ -85,7 +89,7 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, BuildList
8589
throws IOException, InterruptedException {
8690
SSHUserPrivateKey userPrivateKey = null;
8791
for (SSHUserPrivateKey u : CredentialsProvider
88-
.lookupCredentials(SSHUserPrivateKey.class, build.getProject(), ACL.SYSTEM)) {
92+
.lookupCredentials(SSHUserPrivateKey.class, build.getProject(), ACL.SYSTEM, null)) {
8993
if (user.equals(u.getId())) {
9094
userPrivateKey = u;
9195
break;
@@ -107,12 +111,13 @@ public Environment setUp(AbstractBuild build, final Launcher launcher, BuildList
107111
/**
108112
* Helper method that returns a safe description of a {@link SSHUser}.
109113
*
110-
* @param sshUser the credentials.
114+
* @param c the credentials.
111115
* @return the description.
112116
*/
113117
@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 + ")" : "");
116121
}
117122

118123
/**
@@ -144,15 +149,11 @@ public String getDisplayName() {
144149
*/
145150
@SuppressWarnings("unused") // used by stapler
146151
public ListBoxModel doFillUserItems() {
147-
ListBoxModel m = new ListBoxModel();
148-
149152
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+
);
156157
}
157158

158159
}
@@ -179,7 +180,7 @@ public SSHAgentEnvironment(Launcher launcher, final BuildListener listener,
179180
final SSHUserPrivateKey sshUserPrivateKey) throws Throwable {
180181
RemoteAgent agent = null;
181182
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>();
183184
for (RemoteAgentFactory factory : Hudson.getInstance().getExtensionList(RemoteAgentFactory.class)) {
184185
if (factory.isSupported(launcher, listener)) {
185186
try {
@@ -194,17 +195,18 @@ public SSHAgentEnvironment(Launcher launcher, final BuildListener listener,
194195
if (agent == null) {
195196
listener.getLogger().println("[ssh-agent] FATAL: Could not find a suitable ssh-agent provider");
196197
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()) {
198199
listener.getLogger().println("[ssh-agent] * " + fault.getKey());
199200
fault.getValue().printStackTrace(listener.getLogger());
200201
}
201202
throw new RuntimeException("[ssh-agent] Could not find a suitable ssh-agent provider.");
202203
}
203204
this.agent = agent;
204205
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+
}
208210
listener.getLogger().println(Messages.SSHAgentBuildWrapper_Started());
209211
}
210212

0 commit comments

Comments
 (0)