Use preemptive basic authentication in Pull Replication Plugin Issue 16307 Change-Id: Ib45bd361cb7b12eccc812b69ccc8730991c9c67a
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/FetchRestApiClient.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/FetchRestApiClient.java index 9b33c81..09139f0 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/FetchRestApiClient.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/FetchRestApiClient.java
@@ -45,17 +45,16 @@ import java.util.Optional; import org.apache.http.HttpResponse; import org.apache.http.ParseException; -import org.apache.http.auth.AuthScope; +import org.apache.http.auth.AuthenticationException; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.CredentialsProvider; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.auth.BasicScheme; import org.apache.http.message.BasicHeader; import org.apache.http.util.EntityUtils; import org.eclipse.jgit.transport.CredentialItem; @@ -123,7 +122,7 @@ post.addHeader( PullReplicationApiRequestMetrics.HTTP_HEADER_X_START_TIME_NANOS, Long.toString(startTimeNanos)); - return httpClientFactory.create(source).execute(post, this, getContext(targetUri)); + return httpClientFactory.create(source).execute(withBasicAuthentication(targetUri, post), this); } /* (non-Javadoc) @@ -137,7 +136,7 @@ HttpPut put = new HttpPut(url); put.addHeader(new BasicHeader("Accept", MediaType.ANY_TEXT_TYPE.toString())); put.addHeader(new BasicHeader("Content-Type", MediaType.PLAIN_TEXT_UTF_8.toString())); - return httpClientFactory.create(source).execute(put, this, getContext(uri)); + return httpClientFactory.create(source).execute(withBasicAuthentication(uri, put), this); } /* (non-Javadoc) @@ -148,7 +147,7 @@ String url = String.format("%s/%s", apiUri.toASCIIString(), getProjectDeletionUrl(project.get())); HttpDelete delete = new HttpDelete(url); - return httpClientFactory.create(source).execute(delete, this, getContext(apiUri)); + return httpClientFactory.create(source).execute(withBasicAuthentication(apiUri, delete), this); } /* (non-Javadoc) @@ -164,7 +163,7 @@ req.setEntity( new StringEntity(String.format("{\"ref\": \"%s\"}", newHead), StandardCharsets.UTF_8)); req.addHeader(new BasicHeader("Content-Type", MediaType.JSON_UTF_8.toString())); - return httpClientFactory.create(source).execute(req, this, getContext(apiUri)); + return httpClientFactory.create(source).execute(withBasicAuthentication(apiUri, req), this); } /* (non-Javadoc) @@ -192,7 +191,7 @@ HttpPost post = new HttpPost(url); post.setEntity(new StringEntity(GSON.toJson(input))); post.addHeader(new BasicHeader("Content-Type", MediaType.JSON_UTF_8.toString())); - return httpClientFactory.create(source).execute(post, this, getContext(targetUri)); + return httpClientFactory.create(source).execute(withBasicAuthentication(targetUri, post), this); } @Override @@ -210,7 +209,7 @@ HttpPost post = new HttpPost(url); post.setEntity(new StringEntity(GSON.toJson(input))); post.addHeader(new BasicHeader("Content-Type", MediaType.JSON_UTF_8.toString())); - return httpClientFactory.create(source).execute(post, this, getContext(targetUri)); + return httpClientFactory.create(source).execute(withBasicAuthentication(targetUri, post), this); } private String formatUrl(Project.NameKey project, URIish targetUri, String api) { @@ -246,23 +245,21 @@ return new HttpResult(response.getStatusLine().getStatusCode(), responseBody); } - private HttpClientContext getContext(URIish targetUri) { - HttpClientContext ctx = HttpClientContext.create(); - ctx.setCredentialsProvider(adapt(credentials.create(source.getRemoteConfigName()), targetUri)); - return ctx; - } - - private CredentialsProvider adapt(org.eclipse.jgit.transport.CredentialsProvider cp, URIish uri) { + private HttpRequestBase withBasicAuthentication(URIish targetUri, HttpRequestBase req) { + org.eclipse.jgit.transport.CredentialsProvider cp = + credentials.create(source.getRemoteConfigName()); CredentialItem.Username user = new CredentialItem.Username(); CredentialItem.Password pass = new CredentialItem.Password(); - if (cp.supports(user, pass) && cp.get(uri, user, pass)) { - CredentialsProvider adapted = new BasicCredentialsProvider(); - adapted.setCredentials( - AuthScope.ANY, - new UsernamePasswordCredentials(user.getValue(), new String(pass.getValue()))); - return adapted; + if (cp.supports(user, pass) && cp.get(targetUri, user, pass)) { + UsernamePasswordCredentials creds = + new UsernamePasswordCredentials(user.getValue(), new String(pass.getValue())); + try { + req.addHeader(new BasicScheme().authenticate(creds, req, null)); + } catch (AuthenticationException e) { + logger.atFine().log(String.format("Anonymous Basic Authentication for uri: %s", targetUri)); + } } - return null; + return req; } String getProjectDeletionUrl(String projectName) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/HttpClient.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/HttpClient.java index 7bfc7d1..6254a42 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/HttpClient.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/HttpClient.java
@@ -18,14 +18,11 @@ import org.apache.http.client.ClientProtocolException; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpUriRequest; -import org.apache.http.protocol.HttpContext; /** HTTP client for executing URI requests to a remote site */ public interface HttpClient { public <T> T execute( - final HttpUriRequest request, - final ResponseHandler<? extends T> responseHandler, - final HttpContext context) + final HttpUriRequest request, final ResponseHandler<? extends T> responseHandler) throws ClientProtocolException, IOException; }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/SourceHttpClient.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/SourceHttpClient.java index ee0fe79..fa700e3 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/SourceHttpClient.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/client/SourceHttpClient.java
@@ -25,7 +25,6 @@ import org.apache.http.conn.HttpClientConnectionManager; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.apache.http.protocol.HttpContext; /** Apache HTTP client implementation based on Source-specific parameters */ public class SourceHttpClient implements HttpClient { @@ -41,8 +40,7 @@ } @Override - public <T> T execute( - HttpUriRequest request, ResponseHandler<? extends T> responseHandler, HttpContext context) + public <T> T execute(HttpUriRequest request, ResponseHandler<? extends T> responseHandler) throws ClientProtocolException, IOException { return source .memoize( @@ -51,7 +49,7 @@ .setConnectionManager(customConnectionManager(source)) .setDefaultRequestConfig(customRequestConfig(source)) .build()) - .execute(request, responseHandler, context); + .execute(request, responseHandler); } private static RequestConfig customRequestConfig(Source source) {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ActionITBase.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ActionITBase.java index 7cef485..55ad15c 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ActionITBase.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ActionITBase.java
@@ -43,17 +43,16 @@ import java.util.List; import java.util.Optional; import org.apache.http.HttpResponse; -import org.apache.http.auth.AuthScope; +import org.apache.http.auth.AuthenticationException; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.CredentialsProvider; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; -import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.auth.BasicScheme; import org.apache.http.message.BasicHeader; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.storage.file.FileBasedConfig; @@ -170,17 +169,22 @@ }; } - protected HttpClientContext getContext() { - return getContextForAccount(admin); + protected HttpRequestBase withBasicAuthenticationAsAdmin(HttpRequestBase httpRequest) + throws AuthenticationException { + return withBasicAuthentication(httpRequest, admin); } - protected HttpClientContext getUserContext() { - return getContextForAccount(user); + protected HttpRequestBase withBasicAuthenticationAsUser(HttpRequestBase httpRequest) + throws AuthenticationException { + return withBasicAuthentication(httpRequest, user); } - protected HttpClientContext getAnonymousContext() { - HttpClientContext ctx = HttpClientContext.create(); - return ctx; + private HttpRequestBase withBasicAuthentication(HttpRequestBase httpRequest, TestAccount account) + throws AuthenticationException { + UsernamePasswordCredentials creds = + new UsernamePasswordCredentials(account.username(), account.httpPassword()); + httpRequest.addHeader(new BasicScheme().authenticate(creds, httpRequest, null)); + return httpRequest; } private Project.NameKey createTestProject(String name) throws Exception { @@ -216,13 +220,4 @@ secureConfig.setString("remote", remoteName, "password", password); secureConfig.save(); } - - private HttpClientContext getContextForAccount(TestAccount account) { - HttpClientContext ctx = HttpClientContext.create(); - CredentialsProvider adapted = new BasicCredentialsProvider(); - adapted.setCredentials( - AuthScope.ANY, new UsernamePasswordCredentials(account.username(), account.httpPassword())); - ctx.setCredentialsProvider(adapted); - return ctx; - } }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ApplyObjectActionIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ApplyObjectActionIT.java index aad3903..0de3d03 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ApplyObjectActionIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ApplyObjectActionIT.java
@@ -22,7 +22,6 @@ import com.google.gerrit.extensions.restapi.Url; import com.googlesource.gerrit.plugins.replication.pull.api.data.RevisionData; import java.util.Optional; -import org.apache.http.client.methods.HttpPost; import org.junit.Test; public class ApplyObjectActionIT extends ActionITBase { @@ -41,8 +40,11 @@ RevisionData revisionData = revisionDataOption.get(); String sendObjectPayload = createPayload(payloadWithAsyncFieldTemplate, refName, revisionData); - HttpPost post = createRequest(sendObjectPayload); - httpClientFactory.create(source).execute(post, assertHttpResponseCode(201), getContext()); + httpClientFactory + .create(source) + .execute( + withBasicAuthenticationAsAdmin(createRequest(sendObjectPayload)), + assertHttpResponseCode(201)); } @Test @@ -60,8 +62,11 @@ String sendObjectPayload = createPayload(payloadWithoutAsyncFieldTemplate, refName, revisionData); - HttpPost post = createRequest(sendObjectPayload); - httpClientFactory.create(source).execute(post, assertHttpResponseCode(201), getContext()); + httpClientFactory + .create(source) + .execute( + withBasicAuthenticationAsAdmin(createRequest(sendObjectPayload)), + assertHttpResponseCode(201)); } @Test @@ -80,8 +85,11 @@ String sendObjectPayload = createPayload(payloadWithoutAsyncFieldTemplate, refName, revisionData); - HttpPost post = createRequest(sendObjectPayload); - httpClientFactory.create(source).execute(post, assertHttpResponseCode(201), getContext()); + httpClientFactory + .create(source) + .execute( + withBasicAuthenticationAsAdmin(createRequest(sendObjectPayload)), + assertHttpResponseCode(201)); } @Test @@ -103,8 +111,11 @@ String.format( "%s/a/projects/%s/pull-replication~apply-object", adminRestSession.url(), Url.encode(projectName.get())); - HttpPost post = createRequest(sendObjectPayload); - httpClientFactory.create(source).execute(post, assertHttpResponseCode(201), getContext()); + httpClientFactory + .create(source) + .execute( + withBasicAuthenticationAsAdmin(createRequest(sendObjectPayload)), + assertHttpResponseCode(201)); } @Test @@ -123,10 +134,9 @@ String sendObjectPayload = createPayload(payloadWithoutAsyncFieldTemplate, refName, revisionData); - HttpPost post = createRequest(sendObjectPayload); httpClientFactory .create(source) - .execute(post, assertHttpResponseCode(401), getAnonymousContext()); + .execute(createRequest(sendObjectPayload), assertHttpResponseCode(401)); } @Test @@ -142,8 +152,11 @@ String sendObjectPayload = createPayload(payloadWithoutLabelFieldTemplate, refName, revisionData); - HttpPost post = createRequest(sendObjectPayload); - httpClientFactory.create(source).execute(post, assertHttpResponseCode(400), getContext()); + httpClientFactory + .create(source) + .execute( + withBasicAuthenticationAsAdmin(createRequest(sendObjectPayload)), + assertHttpResponseCode(400)); } @Test @@ -160,8 +173,11 @@ RevisionData revisionData = revisionDataOption.get(); String sendObjectPayload = createPayload(wrongPayloadTemplate, refName, revisionData); - HttpPost post = createRequest(sendObjectPayload); - httpClientFactory.create(source).execute(post, assertHttpResponseCode(400), getContext()); + httpClientFactory + .create(source) + .execute( + withBasicAuthenticationAsAdmin(createRequest(sendObjectPayload)), + assertHttpResponseCode(400)); } private String createPayload(
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchActionIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchActionIT.java index 4ad8ce6..36f4361 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchActionIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/FetchActionIT.java
@@ -35,7 +35,9 @@ httpClientFactory .create(source) - .execute(createRequest(sendObjectPayload), assertHttpResponseCode(201), getContext()); + .execute( + withBasicAuthenticationAsAdmin(createRequest(sendObjectPayload)), + assertHttpResponseCode(201)); } @Test @@ -55,7 +57,9 @@ adminRestSession.url(), Url.encode(projectName.get())); httpClientFactory .create(source) - .execute(createRequest(sendObjectPayload), assertHttpResponseCode(201), getContext()); + .execute( + withBasicAuthenticationAsAdmin(createRequest(sendObjectPayload)), + assertHttpResponseCode(201)); } @Test @@ -71,8 +75,7 @@ httpClientFactory .create(source) - .execute( - createRequest(sendObjectPayload), assertHttpResponseCode(401), getAnonymousContext()); + .execute(createRequest(sendObjectPayload), assertHttpResponseCode(401)); } @Override
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ProjectDeletionActionIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ProjectDeletionActionIT.java index f5c8d4c..1a37517 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ProjectDeletionActionIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ProjectDeletionActionIT.java
@@ -22,6 +22,7 @@ import com.google.gerrit.server.group.SystemGroupBackend; import com.google.inject.Inject; import javax.servlet.http.HttpServletResponse; +import org.apache.http.client.methods.HttpRequestBase; import org.junit.Test; public class ProjectDeletionActionIT extends ActionITBase { @@ -35,9 +36,7 @@ httpClientFactory .create(source) .execute( - createDeleteRequest(), - assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED), - getAnonymousContext()); + createDeleteRequest(), assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED)); } @Test @@ -47,9 +46,8 @@ httpClientFactory .create(source) .execute( - createDeleteRequest(), - assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN), - getUserContext()); + withBasicAuthenticationAsUser(createDeleteRequest()), + assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN)); projectOperations .project(allProjects) @@ -60,9 +58,8 @@ httpClientFactory .create(source) .execute( - createDeleteRequest(), - assertHttpResponseCode(HttpServletResponse.SC_OK), - getUserContext()); + withBasicAuthenticationAsUser(createDeleteRequest()), + assertHttpResponseCode(HttpServletResponse.SC_OK)); } @Test @@ -73,7 +70,8 @@ httpClientFactory .create(source) .execute( - createDeleteRequest(), assertHttpResponseCode(HttpServletResponse.SC_OK), getContext()); + withBasicAuthenticationAsAdmin(createDeleteRequest()), + assertHttpResponseCode(HttpServletResponse.SC_OK)); } @Test @@ -83,9 +81,8 @@ httpClientFactory .create(source) .execute( - createDeleteRequest(), - assertHttpResponseCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), - getContext()); + withBasicAuthenticationAsAdmin(createDeleteRequest()), + assertHttpResponseCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } @Test @@ -94,9 +91,7 @@ httpClientFactory .create(source) .execute( - createDeleteRequest(), - assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED), - getAnonymousContext()); + createDeleteRequest(), assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED)); } @Test @@ -108,7 +103,8 @@ httpClientFactory .create(source) .execute( - createDeleteRequest(), assertHttpResponseCode(HttpServletResponse.SC_OK), getContext()); + withBasicAuthenticationAsAdmin(createDeleteRequest()), + assertHttpResponseCode(HttpServletResponse.SC_OK)); } @Test @@ -117,12 +113,11 @@ throws Exception { String testProjectName = project.get(); url = getURL(testProjectName); + HttpRequestBase deleteRequest = withBasicAuthenticationAsUser(createDeleteRequest()); + httpClientFactory .create(source) - .execute( - createDeleteRequest(), - assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN), - getUserContext()); + .execute(deleteRequest, assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN)); projectOperations .project(allProjects) @@ -132,10 +127,7 @@ httpClientFactory .create(source) - .execute( - createDeleteRequest(), - assertHttpResponseCode(HttpServletResponse.SC_OK), - getUserContext()); + .execute(deleteRequest, assertHttpResponseCode(HttpServletResponse.SC_OK)); } @Test @@ -147,9 +139,8 @@ httpClientFactory .create(source) .execute( - createDeleteRequest(), - assertHttpResponseCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), - getContext()); + withBasicAuthenticationAsAdmin(createDeleteRequest()), + assertHttpResponseCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } @Override
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ProjectInitializationActionIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ProjectInitializationActionIT.java index ca4fa31..8bcb9ea 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ProjectInitializationActionIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/ProjectInitializationActionIT.java
@@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPut; +import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.message.BasicHeader; import org.junit.Test; @@ -40,8 +41,7 @@ .create(source) .execute( createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED), - getAnonymousContext()); + assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED)); } @Test @@ -49,9 +49,8 @@ httpClientFactory .create(source) .execute( - createPutRequestWithoutHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_BAD_REQUEST), - getContext()); + withBasicAuthenticationAsAdmin(createPutRequestWithoutHeaders()), + assertHttpResponseCode(HttpServletResponse.SC_BAD_REQUEST)); } @Test @@ -61,28 +60,26 @@ httpClientFactory .create(source) .execute( - createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_CREATED), - getContext()); + withBasicAuthenticationAsAdmin(createPutRequestWithHeaders()), + assertHttpResponseCode(HttpServletResponse.SC_CREATED)); - HttpGet getNewProjectRequest = - new HttpGet(userRestSession.url() + "/a/projects/" + Url.encode(newProjectName)); + HttpRequestBase getNewProjectRequest = + withBasicAuthenticationAsAdmin( + new HttpGet(userRestSession.url() + "/a/projects/" + Url.encode(newProjectName))); + httpClientFactory .create(source) - .execute( - getNewProjectRequest, assertHttpResponseCode(HttpServletResponse.SC_OK), getContext()); + .execute(getNewProjectRequest, assertHttpResponseCode(HttpServletResponse.SC_OK)); } @Test public void shouldCreateRepositoryWhenUserHasProjectCreationCapabilities() throws Exception { String newProjectName = "new/newProjectForUserWithCapabilities"; url = getURL(newProjectName); + HttpRequestBase put = withBasicAuthenticationAsUser(createPutRequestWithHeaders()); httpClientFactory .create(source) - .execute( - createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN), - getUserContext()); + .execute(put, assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN)); projectOperations .project(allProjects) @@ -94,10 +91,7 @@ httpClientFactory .create(source) - .execute( - createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_CREATED), - getUserContext()); + .execute(put, assertHttpResponseCode(HttpServletResponse.SC_CREATED)); } @Test @@ -105,9 +99,8 @@ httpClientFactory .create(source) .execute( - createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN), - getUserContext()); + withBasicAuthenticationAsUser(createPutRequestWithHeaders()), + assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN)); } @Test @@ -118,9 +111,8 @@ httpClientFactory .create(source) .execute( - createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_CREATED), - getContext()); + withBasicAuthenticationAsAdmin(createPutRequestWithHeaders()), + assertHttpResponseCode(HttpServletResponse.SC_CREATED)); } @Test @@ -129,9 +121,8 @@ httpClientFactory .create(source) .execute( - createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN), - getUserContext()); + withBasicAuthenticationAsUser(createPutRequestWithHeaders()), + assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN)); } @Test @@ -140,12 +131,10 @@ throws Exception { String newProjectName = "new/newProjectForUserWithCapabilitiesReplica"; url = getURL(newProjectName); + HttpRequestBase put = withBasicAuthenticationAsUser(createPutRequestWithHeaders()); httpClientFactory .create(source) - .execute( - createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN), - getUserContext()); + .execute(put, assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN)); projectOperations .project(allProjects) @@ -157,10 +146,7 @@ httpClientFactory .create(source) - .execute( - createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_CREATED), - getUserContext()); + .execute(put, assertHttpResponseCode(HttpServletResponse.SC_CREATED)); } @Test @@ -168,13 +154,11 @@ public void shouldReturnInternalServerErrorIfProjectCannotBeCreatedWhenNodeIsAReplica() throws Exception { url = getURL(INVALID_TEST_PROJECT_NAME); - httpClientFactory .create(source) .execute( - createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), - getContext()); + withBasicAuthenticationAsAdmin(createPutRequestWithHeaders()), + assertHttpResponseCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR)); } @Test @@ -183,9 +167,8 @@ httpClientFactory .create(source) .execute( - createPutRequestWithoutHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_BAD_REQUEST), - getContext()); + withBasicAuthenticationAsAdmin(createPutRequestWithoutHeaders()), + assertHttpResponseCode(HttpServletResponse.SC_BAD_REQUEST)); } @Test @@ -196,8 +179,7 @@ .create(source) .execute( createPutRequestWithHeaders(), - assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED), - getAnonymousContext()); + assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED)); } @Override
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/UpdateHeadActionIT.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/UpdateHeadActionIT.java index aa07a7c..5355251 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/UpdateHeadActionIT.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/UpdateHeadActionIT.java
@@ -26,6 +26,7 @@ import com.google.gson.Gson; import com.google.inject.Inject; import javax.servlet.http.HttpServletResponse; +import org.apache.http.client.methods.HttpRequestBase; import org.junit.Test; public class UpdateHeadActionIT extends ActionITBase { @@ -39,8 +40,7 @@ .create(source) .execute( createPutRequest(headInput("some/branch")), - assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED), - getAnonymousContext()); + assertHttpResponseCode(HttpServletResponse.SC_UNAUTHORIZED)); } @Test @@ -48,9 +48,8 @@ httpClientFactory .create(source) .execute( - createPutRequest(headInput("")), - assertHttpResponseCode(HttpServletResponse.SC_BAD_REQUEST), - getContext()); + withBasicAuthenticationAsAdmin(createPutRequest(headInput(""))), + assertHttpResponseCode(HttpServletResponse.SC_BAD_REQUEST)); } @Test @@ -61,13 +60,11 @@ BranchInput input = new BranchInput(); input.revision = master; gApi.projects().name(testProjectName).branch(newBranch).create(input); - httpClientFactory .create(source) .execute( - createPutRequest(headInput(newBranch)), - assertHttpResponseCode(HttpServletResponse.SC_OK), - getContext()); + withBasicAuthenticationAsAdmin(createPutRequest(headInput(newBranch))), + assertHttpResponseCode(HttpServletResponse.SC_OK)); assertThat(gApi.projects().name(testProjectName).head()).isEqualTo(newBranch); } @@ -78,9 +75,8 @@ httpClientFactory .create(source) .execute( - createPutRequest(headInput("")), - assertHttpResponseCode(HttpServletResponse.SC_BAD_REQUEST), - getContext()); + withBasicAuthenticationAsAdmin(createPutRequest(headInput(""))), + assertHttpResponseCode(HttpServletResponse.SC_BAD_REQUEST)); } @Test @@ -92,13 +88,11 @@ BranchInput input = new BranchInput(); input.revision = master; gApi.projects().name(testProjectName).branch(newBranch).create(input); - httpClientFactory .create(source) .execute( - createPutRequest(headInput(newBranch)), - assertHttpResponseCode(HttpServletResponse.SC_OK), - getContext()); + withBasicAuthenticationAsAdmin(createPutRequest(headInput(newBranch))), + assertHttpResponseCode(HttpServletResponse.SC_OK)); assertThat(gApi.projects().name(testProjectName).head()).isEqualTo(newBranch); } @@ -108,9 +102,8 @@ httpClientFactory .create(source) .execute( - createPutRequest(headInput("some/new/head")), - assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN), - getUserContext()); + withBasicAuthenticationAsUser(createPutRequest(headInput("some/new/head"))), + assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN)); } @Test @@ -121,13 +114,10 @@ BranchInput input = new BranchInput(); input.revision = master; gApi.projects().name(testProjectName).branch(newBranch).create(input); - + HttpRequestBase put = withBasicAuthenticationAsUser(createPutRequest(headInput(newBranch))); httpClientFactory .create(source) - .execute( - createPutRequest(headInput(newBranch)), - assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN), - getUserContext()); + .execute(put, assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN)); projectOperations .project(project) @@ -137,10 +127,7 @@ httpClientFactory .create(source) - .execute( - createPutRequest(headInput(newBranch)), - assertHttpResponseCode(HttpServletResponse.SC_OK), - getUserContext()); + .execute(put, assertHttpResponseCode(HttpServletResponse.SC_OK)); } @Test @@ -149,9 +136,8 @@ httpClientFactory .create(source) .execute( - createPutRequest(headInput("some/new/head")), - assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN), - getUserContext()); + withBasicAuthenticationAsUser(createPutRequest(headInput("some/new/head"))), + assertHttpResponseCode(HttpServletResponse.SC_FORBIDDEN)); } private String headInput(String ref) {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/client/FetchRestApiClientTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/client/FetchRestApiClientTest.java index 00904c7..a6886f1 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/client/FetchRestApiClientTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/client/FetchRestApiClientTest.java
@@ -174,7 +174,7 @@ when(source.getRemoteConfigName()).thenReturn("Replication"); HttpResult httpResult = new HttpResult(SC_CREATED, Optional.of("result message")); - when(httpClient.execute(any(HttpPost.class), any(), any())).thenReturn(httpResult); + when(httpClient.execute(any(HttpPost.class), any())).thenReturn(httpResult); when(httpClientFactory.create(any())).thenReturn(httpClient); syncRefsFilter = new SyncRefsFilter(replicationConfig); objectUnderTest = @@ -194,7 +194,7 @@ objectUnderTest.callFetch(Project.nameKey("test_repo"), refName, new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(httpPost.getURI().getHost()).isEqualTo("gerrit-host"); @@ -219,7 +219,7 @@ objectUnderTest.callFetch(Project.nameKey("test_repo"), refName, new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(readPayload(httpPost)).isEqualTo(expectedPayload); @@ -244,7 +244,7 @@ objectUnderTest.callFetch(Project.nameKey("test_repo"), refName, new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(readPayload(httpPost)).isEqualTo(expectedAsyncPayload); @@ -271,12 +271,12 @@ source); objectUnderTest.callFetch(Project.nameKey("test_repo"), refName, new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(readPayload(httpPost)).isEqualTo(expectedAsyncPayload); objectUnderTest.callFetch(Project.nameKey("test_repo"), metaRefName, new URIish(api)); - verify(httpClient, times(2)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(2)).execute(httpPostCaptor.capture(), any()); httpPost = httpPostCaptor.getValue(); assertThat(readPayload(httpPost)).isEqualTo(expectedMetaRefPayload); } @@ -287,7 +287,7 @@ objectUnderTest.callFetch(Project.nameKey("test_repo"), refName, new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(readPayload(httpPost)).isEqualTo(expectedPayload); @@ -299,7 +299,7 @@ objectUnderTest.callFetch(Project.nameKey("test_repo"), refName, new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(httpPost.getLastHeader("Content-Type").getValue()) @@ -317,7 +317,7 @@ createSampleRevisionData(), new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(httpPost.getURI().getHost()).isEqualTo("gerrit-host"); @@ -336,7 +336,7 @@ createSampleRevisionData(), new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(readPayload(httpPost)).isEqualTo(expectedSendObjectPayload); @@ -348,7 +348,7 @@ objectUnderTest.callFetch(Project.nameKey("test_repo"), refName, new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(httpPost.getLastHeader("Content-Type").getValue()) @@ -415,7 +415,7 @@ source); objectUnderTest.callFetch(Project.nameKey("test_repo"), refName, new URIish(api)); - verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPostCaptor.capture(), any()); HttpPost httpPost = httpPostCaptor.getValue(); assertThat(readPayload(httpPost)).isEqualTo(expectedPayload); @@ -426,7 +426,7 @@ objectUnderTest.initProject(Project.nameKey("test_repo"), new URIish(api)); - verify(httpClient, times(1)).execute(httpPutCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPutCaptor.capture(), any()); HttpPut httpPut = httpPutCaptor.getValue(); assertThat(httpPut.getURI().getHost()).isEqualTo("gerrit-host"); @@ -439,7 +439,7 @@ objectUnderTest.deleteProject(Project.nameKey("test_repo"), new URIish(api)); - verify(httpClient, times(1)).execute(httpDeleteCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpDeleteCaptor.capture(), any()); HttpDelete httpDelete = httpDeleteCaptor.getValue(); assertThat(httpDelete.getURI().getHost()).isEqualTo("gerrit-host"); @@ -453,7 +453,7 @@ String projectName = "aProject"; objectUnderTest.updateHead(Project.nameKey(projectName), newHead, new URIish(api)); - verify(httpClient, times(1)).execute(httpPutCaptor.capture(), any(), any()); + verify(httpClient, times(1)).execute(httpPutCaptor.capture(), any()); HttpPut httpPut = httpPutCaptor.getValue(); String payload =