Skip to content

Commit b1a16de

Browse files
bitwisemanjetersen
authored andcommitted
[JENKINS-46094] Fix SSH Clone trait using agent credentials (#170)
1 parent fcbc835 commit b1a16de

File tree

5 files changed

+746
-37
lines changed

5 files changed

+746
-37
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilder.java

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public class BitbucketGitSCMBuilder extends GitSCMBuilder<BitbucketGitSCMBuilder
7575
@NonNull
7676
private List<BitbucketHref> cloneLinks = Collections.emptyList();
7777

78+
/**
79+
* The {@link BitbucketRepositoryProtocol} that should be used.
80+
* Enables support for blank SSH credentials.
81+
*/
82+
@NonNull
83+
private BitbucketRepositoryProtocol protocol = BitbucketRepositoryProtocol.HTTP;
84+
7885
/**
7986
* Constructor.
8087
*
@@ -115,6 +122,10 @@ public BitbucketGitSCMBuilder(@NonNull BitbucketSCMSource scmSource, @NonNull SC
115122
scmSource.getRepoOwner(),
116123
scmSource.getRepository()
117124
)));
125+
126+
// Test for protocol
127+
withCredentials(credentialsId, null);
128+
118129
}
119130

120131
/**
@@ -149,6 +160,43 @@ public List<BitbucketHref> cloneLinks() {
149160
return Collections.unmodifiableList(cloneLinks);
150161
}
151162

163+
/**
164+
* Configures the {@link IdCredentials#getId()} of the {@link Credentials} to use when connecting to the
165+
* {@link #remote()}
166+
*
167+
* @param credentialsId the {@link IdCredentials#getId()} of the {@link Credentials} to use when connecting to
168+
* the {@link #remote()} or {@code null} to let the git client choose between providing its own
169+
* credentials or connecting anonymously.
170+
* @param protocol the {@link BitbucketRepositoryProtocol} of the {@link Credentials} to use or {@code null}
171+
* to detect the the protocol based on the credentialsId. Defaults to HTTP if credentials are
172+
* {@code null}. Enables support for blank SSH credentials.
173+
* @return {@code this} for method chaining.
174+
*/
175+
@NonNull
176+
public BitbucketGitSCMBuilder withCredentials(String credentialsId, BitbucketRepositoryProtocol protocol) {
177+
if (StringUtils.isNotBlank(credentialsId)) {
178+
StandardCredentials credentials = BitbucketCredentials.lookupCredentials(
179+
scmSource.getServerUrl(),
180+
scmSource.getOwner(),
181+
credentialsId,
182+
StandardCredentials.class
183+
);
184+
185+
if (protocol == null) {
186+
protocol = credentials instanceof SSHUserPrivateKey
187+
? BitbucketRepositoryProtocol.SSH
188+
: BitbucketRepositoryProtocol.HTTP;
189+
}
190+
} else if (protocol == null) {
191+
// If we set credentials to empty reset the type to HTTP.
192+
// To set the build to use empty SSH credentials, call withProtocol after setting credentials
193+
protocol = BitbucketRepositoryProtocol.HTTP;
194+
}
195+
196+
this.protocol = protocol;
197+
return withCredentials(credentialsId);
198+
}
199+
152200
/**
153201
* Updates the {@link GitSCMBuilder#withRemote(String)} based on the current {@link #head()} and
154202
* {@link #revision()}.
@@ -159,18 +207,6 @@ public List<BitbucketHref> cloneLinks() {
159207
*/
160208
@NonNull
161209
public BitbucketGitSCMBuilder withBitbucketRemote() {
162-
// Apply clone links and credentials
163-
StandardCredentials credentials = StringUtils.isBlank(credentialsId())
164-
? null
165-
: BitbucketCredentials.lookupCredentials(
166-
scmSource().getServerUrl(),
167-
scmSource().getOwner(),
168-
credentialsId(),
169-
StandardCredentials.class
170-
);
171-
BitbucketRepositoryProtocol protocol = credentials instanceof SSHUserPrivateKey
172-
? BitbucketRepositoryProtocol.SSH
173-
: BitbucketRepositoryProtocol.HTTP;
174210
SCMHead h = head();
175211
String repoOwner;
176212
String repository;
@@ -261,5 +297,4 @@ public GitSCM build() {
261297
withRevision(r);
262298
}
263299
}
264-
265300
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketHgSCMBuilder.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ public class BitbucketHgSCMBuilder extends MercurialSCMBuilder<BitbucketHgSCMBui
7979
@NonNull
8080
private List<BitbucketHref> cloneLinks = Collections.emptyList();
8181

82+
/**
83+
* The {@link BitbucketRepositoryProtocol} that should be used.
84+
* Enables support for blank SSH credentials.
85+
*/
86+
@NonNull
87+
private BitbucketRepositoryProtocol protocol = BitbucketRepositoryProtocol.HTTP;
88+
8289
/**
8390
* Constructor.
8491
*
@@ -90,7 +97,7 @@ public class BitbucketHgSCMBuilder extends MercurialSCMBuilder<BitbucketHgSCMBui
9097
* credentials or connecting anonymously.
9198
*/
9299
public BitbucketHgSCMBuilder(@NonNull BitbucketSCMSource scmSource, @NonNull SCMHead head,
93-
@CheckForNull SCMRevision revision, String credentialsId) {
100+
@CheckForNull SCMRevision revision, @CheckForNull String credentialsId) {
94101
super(head, revision, /*dummy value*/scmSource.getServerUrl(), credentialsId);
95102
this.scmSource = scmSource;
96103
AbstractBitbucketEndpoint endpoint =
@@ -106,6 +113,9 @@ public BitbucketHgSCMBuilder(@NonNull BitbucketSCMSource scmSource, @NonNull SCM
106113
} catch (MalformedURLException e) {
107114
// ignore, we are providing a well formed URL and if we are not then we shouldn't apply a browser
108115
}
116+
117+
// Test for protocol
118+
withCredentialsId(credentialsId, null);
109119
}
110120

111121
/**
@@ -140,6 +150,43 @@ public List<BitbucketHref> cloneLinks() {
140150
return Collections.unmodifiableList(cloneLinks);
141151
}
142152

153+
/**
154+
* Configures the {@link IdCredentials#getId()} of the {@link Credentials} to use when connecting to the
155+
* {@link #source()}
156+
*
157+
* @param credentialsId the {@link IdCredentials#getId()} of the {@link Credentials} to use when connecting to
158+
* the {@link #source()} or {@code null} to let the git client choose between providing its own
159+
* credentials or connecting anonymously.
160+
* @param protocol the {@link BitbucketRepositoryProtocol} of the {@link Credentials} to use or {@code null}
161+
* to detect the the protocol based on the credentialsId. Defaults to HTTP if credentials are
162+
* {@code null}. Enables support for blank SSH credentials.
163+
* @return {@code this} for method chaining.
164+
*/
165+
@NonNull
166+
public BitbucketHgSCMBuilder withCredentialsId(String credentialsId, BitbucketRepositoryProtocol protocol) {
167+
if (StringUtils.isNotBlank(credentialsId)) {
168+
StandardCredentials credentials = BitbucketCredentials.lookupCredentials(
169+
scmSource.getServerUrl(),
170+
scmSource.getOwner(),
171+
credentialsId,
172+
StandardCredentials.class
173+
);
174+
175+
if (protocol == null) {
176+
protocol = credentials instanceof SSHUserPrivateKey
177+
? BitbucketRepositoryProtocol.SSH
178+
: BitbucketRepositoryProtocol.HTTP;
179+
}
180+
} else if (protocol == null) {
181+
// If we set credentials to empty reset the type to HTTP.
182+
// To set the build to use empty SSH credentials, call withProtocol after setting credentials
183+
protocol = BitbucketRepositoryProtocol.HTTP;
184+
}
185+
186+
this.protocol = protocol;
187+
return withCredentialsId(credentialsId);
188+
}
189+
143190
/**
144191
* Updates the {@link MercurialSCMBuilder#withSource(String)} based on the current {@link #head()} and
145192
* {@link #revision()}.
@@ -150,18 +197,6 @@ public List<BitbucketHref> cloneLinks() {
150197
*/
151198
@NonNull
152199
public BitbucketHgSCMBuilder withBitbucketSource() {
153-
// Apply clone links and credentials
154-
StandardCredentials credentials = StringUtils.isBlank(credentialsId())
155-
? null
156-
: BitbucketCredentials.lookupCredentials(
157-
scmSource().getServerUrl(),
158-
scmSource().getOwner(),
159-
credentialsId(),
160-
StandardCredentials.class
161-
);
162-
BitbucketRepositoryProtocol protocol = credentials instanceof SSHUserPrivateKey
163-
? BitbucketRepositoryProtocol.SSH
164-
: BitbucketRepositoryProtocol.HTTP;
165200
SCMHead h = head();
166201
String repoOwner;
167202
String repository;
@@ -249,5 +284,4 @@ public MercurialSCM build() {
249284
withRevision(r);
250285
}
251286
}
252-
253287
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/SSHCheckoutTrait.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.cloudbees.jenkins.plugins.bitbucket;
2525

26+
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepositoryProtocol;
2627
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
2728
import com.cloudbees.plugins.credentials.CredentialsMatchers;
2829
import com.cloudbees.plugins.credentials.CredentialsProvider;
@@ -105,9 +106,11 @@ public final String getCredentialsId() {
105106
@Override
106107
protected void decorateBuilder(SCMBuilder<?, ?> builder) {
107108
if (builder instanceof GitSCMBuilder) {
108-
((GitSCMBuilder) builder).withCredentials(credentialsId);
109+
((BitbucketGitSCMBuilder) builder)
110+
.withCredentials(credentialsId, BitbucketRepositoryProtocol.SSH);
109111
} else if (builder instanceof MercurialSCMBuilder) {
110-
((MercurialSCMBuilder) builder).withCredentialsId(credentialsId);
112+
((BitbucketHgSCMBuilder) builder)
113+
.withCredentialsId(credentialsId, BitbucketRepositoryProtocol.SSH);
111114
}
112115
}
113116

0 commit comments

Comments
 (0)