Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip authentication for fetching mirror urls #835

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@
@NonNull
public BitbucketMirroredRepository getMirroredRepository(@NonNull String url) throws IOException, InterruptedException {
HttpGet httpget = new HttpGet(url);
var response = getRequest(httpget);
var response = getRequest(httpget, false);
try {
return JsonParser.toJava(response, BitbucketMirroredRepository.class);
} catch (IOException e) {
Expand Down Expand Up @@ -954,12 +954,16 @@
}

private String getRequest(HttpGet httpget) throws IOException, InterruptedException {
return getRequest(httpget, true);
}

if (authenticator != null) {
private String getRequest(HttpGet httpget, boolean configureAuthentication) throws IOException, InterruptedException {

if (authenticator != null && configureAuthentication) {

Check warning on line 962 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 962 is only partially covered, one branch is missing
authenticator.configureRequest(httpget);
}

try(CloseableHttpClient client = getHttpClient(httpget);
try(CloseableHttpClient client = getHttpClient(httpget, configureAuthentication);
CloseableHttpResponse response = executeMethod(client, httpget)) {
String content;
long len = response.getEntity().getContentLength();
Expand Down Expand Up @@ -1044,6 +1048,10 @@
* @return CloseableHttpClient
*/
private CloseableHttpClient getHttpClient(final HttpRequestBase request) {
return getHttpClient(request, true);

Check warning on line 1051 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1051 is not covered by tests
}

private CloseableHttpClient getHttpClient(final HttpRequestBase request, boolean configureAuthentication) {
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.useSystemProperties();
httpClientBuilder.setRetryHandler(new StandardHttpRequestRetryHandler());
Expand All @@ -1060,7 +1068,7 @@

final String host = getMethodHost(request);

if (authenticator != null) {
if (authenticator != null && configureAuthentication) {

Check warning on line 1071 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1071 is only partially covered, one branch is missing
authenticator.configureBuilder(httpClientBuilder);

context = HttpClientContext.create();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package com.cloudbees.jenkins.plugins.bitbucket.server.client;

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketMirroredRepository;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory;
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory.BitbucketServerIntegrationClient;
import com.damnhandy.uri.template.UriTemplate;
import com.damnhandy.uri.template.impl.Operator;
import java.net.URI;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicStatusLine;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.WithoutJenkins;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;

import static com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient.API_BROWSE_PATH;
Expand All @@ -26,9 +36,13 @@
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

public class BitbucketServerAPIClientTest {
Expand Down Expand Up @@ -101,4 +115,26 @@ public void disableCookieManager() throws Exception {
verify(httpClientBuilder).disableCookieManagement();
}
}

@Test
public void getMirroredRepository() throws Exception {
try(MockedStatic<HttpClientBuilder> staticHttpClientBuilder = mockStatic(HttpClientBuilder.class)) {
BitbucketAuthenticator authenticator = mock(BitbucketAuthenticator.class);
HttpClientBuilder httpClientBuilder = mock(HttpClientBuilder.class);
CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
when(response.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK"));
when(response.getEntity()).thenReturn(new StringEntity("{}"));
when(httpClient.execute(any(HttpGet.class), isNull(HttpClientContext.class))).thenReturn(response);
staticHttpClientBuilder.when(HttpClientBuilder::create).thenReturn(httpClientBuilder);
when(httpClientBuilder.build()).thenReturn(httpClient);
BitbucketServerAPIClient client = new BitbucketServerAPIClient("localhost", "amuniz", "test-repos", authenticator, false);
BitbucketMirroredRepository mirroredRepository = client.getMirroredRepository("http://my-mirror");
assertThat(mirroredRepository, is(not(nullValue())));
verifyNoInteractions(authenticator);
ArgumentCaptor<HttpGet> requestCapture = ArgumentCaptor.forClass(HttpGet.class);
verify(httpClient).execute(requestCapture.capture(), isNull(HttpClientContext.class));
assertThat(requestCapture.getValue().getURI(), is(new URI("http://my-mirror")));
}
}
}