Skip to content

Commit 39a9845

Browse files
fonsecas72jetersen
authored andcommitted
Fix JENKINS-56322 - local domain validation (#173)
1 parent 868676d commit 39a9845

File tree

6 files changed

+137
-80
lines changed

6 files changed

+137
-80
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,35 @@ private static String getRootURL(@NonNull Run<?, ?> build) {
6565
throw new IllegalStateException("Could not determine Jenkins URL.");
6666
}
6767

68-
String url = DisplayURLProvider.get().getRunURL(build);
69-
return checkURL(url);
68+
return DisplayURLProvider.get().getRunURL(build);
7069
}
7170

7271
/**
7372
* Check if the build URL is compatible with Bitbucket API.
74-
* For example, Bitbucket API doesn't accept simple hostnames as URLs host value
73+
* For example, Bitbucket Cloud API requires fully qualified or IP
74+
* Where we actively do not allow localhost
7575
* Throws an IllegalStateException if it is not valid, or return the url otherwise
7676
*
7777
* @param url the URL of the build to check
78-
* @return the url if it is valid
78+
* @param bitbucket the bitbucket client we are facing.
7979
*/
80-
static String checkURL(@NonNull String url) {
81-
if (url.startsWith("http://unconfigured-jenkins-location/")) {
82-
throw new IllegalStateException("Could not determine Jenkins URL.");
83-
}
80+
static String checkURL(@NonNull String url, BitbucketApi bitbucket) {
8481
try {
85-
URL u = new URL(url);
86-
if (!u.getHost().contains(".")) {
87-
throw new IllegalStateException("Please use a fully qualified name or an IP address for Jenkins URL");
82+
URL anURL = new URL(url);
83+
if ("localhost".equals(anURL.getHost())) {
84+
throw new IllegalStateException("Jenkins URL cannot start with http://localhost");
85+
}
86+
if ("unconfigured-jenkins-location".equals(anURL.getHost())) {
87+
throw new IllegalStateException("Could not determine Jenkins URL.");
88+
}
89+
if (bitbucket instanceof BitbucketCloudApiClient && !anURL.getHost().contains(".")) {
90+
throw new IllegalStateException(
91+
"Please use a fully qualified name or an IP address for Jenkins URL, this is required by Bitbucket cloud");
8892
}
93+
return url;
8994
} catch (MalformedURLException e) {
9095
throw new IllegalStateException("Bad Jenkins URL");
9196
}
92-
return url;
9397
}
9498

9599
private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener listener,
@@ -99,6 +103,7 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
99103
String url;
100104
try {
101105
url = getRootURL(build);
106+
checkURL(url, bitbucket);
102107
} catch (IllegalStateException e) {
103108
listener.getLogger().println("Can not determine Jenkins root URL " +
104109
"or Jenkins URL is not a valid URL regarding Bitbucket API. " +

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketBuildStatusNotificationsTest.java

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.cloudbees.jenkins.plugins.bitbucket;
2+
3+
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
4+
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
5+
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration;
6+
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketServerEndpoint;
7+
import java.util.Arrays;
8+
import org.junit.BeforeClass;
9+
import org.junit.ClassRule;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.junit.rules.ExpectedException;
13+
import org.junit.runner.RunWith;
14+
import org.junit.runners.Parameterized;
15+
import org.junit.runners.Parameterized.Parameters;
16+
import org.jvnet.hudson.test.JenkinsRule;
17+
18+
import static com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory.getApiMockClient;
19+
import static org.junit.Assert.assertNotNull;
20+
21+
@RunWith(Parameterized.class)
22+
public class BitbucketCheckURLTest {
23+
24+
private static final String BITBUCKET_SERVER_URL = "https://bitbucket.server";
25+
private static final Class<IllegalStateException> ISE = IllegalStateException.class;
26+
private static final String localHost = "Jenkins URL cannot start with http://localhost";
27+
private static final String CLOUD = "cloud";
28+
private static final String SERVER = "server";
29+
private static final String BOTH = String.format("%s and %s", CLOUD, SERVER);
30+
private static final String FQDN = "Please use a fully qualified name or an IP address for Jenkins URL, this is required by Bitbucket cloud";
31+
private static final String DETERMINE = "Could not determine Jenkins URL.";
32+
private final String jenkinsUrl;
33+
private final Class<? extends Exception> expectedException;
34+
private final String expectedExceptionMsg;
35+
private final String serverOrCloud;
36+
private static BitbucketApi bitbucketServer;
37+
private static BitbucketApi bitbucketCloud;
38+
39+
@ClassRule
40+
public static JenkinsRule r = new JenkinsRule();
41+
42+
@Rule
43+
public ExpectedException thrown = ExpectedException.none();
44+
45+
public BitbucketCheckURLTest(String jenkinsUrl,
46+
String expectedExceptionMsg,
47+
Class<? extends Exception> expectedException, String serverOrCloud) {
48+
this.jenkinsUrl = jenkinsUrl;
49+
this.expectedException = expectedException;
50+
this.expectedExceptionMsg = expectedExceptionMsg;
51+
this.serverOrCloud = serverOrCloud;
52+
}
53+
54+
@Parameters(name = "check {0} URL against {3}")
55+
public static Iterable<Object[]> data() {
56+
return Arrays.asList(new Object[][]{
57+
{"localhost", localHost, ISE, BOTH},
58+
{"unconfigured-jenkins-location", DETERMINE, ISE, BOTH},
59+
{"intranet", FQDN, ISE, CLOUD},
60+
{"intranet:8080", FQDN, ISE, CLOUD},
61+
{"localhost.local", null, null, BOTH},
62+
{"intranet.local:8080", null, null, BOTH},
63+
{"www.mydomain.com:8000", null, null, BOTH},
64+
{"www.mydomain.com", null, null, BOTH}
65+
});
66+
}
67+
68+
@BeforeClass
69+
public static void setUp() {
70+
BitbucketEndpointConfiguration instance = BitbucketEndpointConfiguration.get();
71+
instance.setEndpoints(Arrays.asList(
72+
new BitbucketServerEndpoint(
73+
"Bitbucket Server",
74+
BITBUCKET_SERVER_URL,
75+
true,
76+
"dummy"),
77+
new BitbucketCloudEndpoint(
78+
true,
79+
"second")
80+
));
81+
bitbucketServer = getApiMockClient(BITBUCKET_SERVER_URL);
82+
bitbucketCloud = getApiMockClient(BitbucketCloudEndpoint.SERVER_URL);
83+
}
84+
85+
private void setupException() {
86+
if (expectedException != null) {
87+
thrown.expect(expectedException);
88+
thrown.expectMessage(expectedExceptionMsg);
89+
}
90+
}
91+
92+
private void doubleTrouble(BitbucketApi bitbucketApi) {
93+
Arrays.asList("http://", "https://").forEach(
94+
url -> assertNotNull(BitbucketBuildStatusNotifications
95+
.checkURL(url + jenkinsUrl + "/build/sample", bitbucketApi)));
96+
}
97+
98+
@Test
99+
public void checkURLForBitbucketServer() {
100+
if (serverOrCloud.contains(SERVER)) {
101+
setupException();
102+
}
103+
doubleTrouble(bitbucketServer);
104+
}
105+
106+
@Test
107+
public void checkURLForBitbucketCloud() {
108+
if (serverOrCloud.contains(CLOUD)) {
109+
setupException();
110+
}
111+
doubleTrouble(bitbucketCloud);
112+
}
113+
114+
}

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMRevisionTest.java

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

26-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
27-
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory;
2826
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
2927
import hudson.Util;
3028
import hudson.model.TaskListener;
@@ -44,6 +42,7 @@
4442
import org.jvnet.hudson.test.JenkinsRule;
4543
import org.mockito.Mockito;
4644

45+
import static com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory.getApiMockClient;
4746
import static org.hamcrest.CoreMatchers.notNullValue;
4847
import static org.junit.Assert.assertThat;
4948

@@ -111,8 +110,4 @@ private void assertRevision(BitbucketGitSCMRevision revision) {
111110
assertThat("commit date is not valued for revision " + revision.getHash(), revision.getDate(), notNullValue());
112111
}
113112

114-
private BitbucketApi getApiMockClient(String serverURL) {
115-
return BitbucketIntegrationClientFactory.getClient(serverURL, "amuniz", "test-repos");
116-
}
117-
118113
}

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/SCMTraitCommitTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
*/
2424
package com.cloudbees.jenkins.plugins.bitbucket;
2525

26-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
2726
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketBranch;
2827
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCommit;
2928
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
30-
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory;
3129
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
3230
import edu.umd.cs.findbugs.annotations.NonNull;
3331
import hudson.Extension;
@@ -59,6 +57,7 @@
5957
import org.jvnet.hudson.test.JenkinsRule;
6058
import org.mockito.Mockito;
6159

60+
import static com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory.getApiMockClient;
6261
import static org.hamcrest.CoreMatchers.equalTo;
6362
import static org.hamcrest.CoreMatchers.notNullValue;
6463
import static org.junit.Assert.assertThat;
@@ -190,8 +189,4 @@ public void verify_commit_info_are_valued() throws Exception {
190189
assertThat(observer.getRevisions().size(), equalTo(commitTrait.getMatches()));
191190
}
192191

193-
private BitbucketApi getApiMockClient(String serverURL) {
194-
return BitbucketIntegrationClientFactory.getClient(serverURL, "amuniz", "test-repos");
195-
}
196-
197192
}

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/client/BitbucketIntegrationClientFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,8 @@ protected String getRequest(String path) throws IOException, InterruptedExceptio
112112
}
113113
}
114114
}
115+
116+
public static BitbucketApi getApiMockClient(String serverURL) {
117+
return BitbucketIntegrationClientFactory.getClient(serverURL, "amuniz", "test-repos");
118+
}
115119
}

0 commit comments

Comments
 (0)