Browse Source

User PRIVATE-TOKEN Header instead of query parameter

pull/76/head
Uldis Ansmits 5 years ago
parent
commit
fd55574e54
  1. 20
      src/main/java/com/englishtown/bitbucket/hook/MirrorRemoteAdmin.java
  2. 4
      src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryServlet.java
  3. 25
      src/test/java/com/englishtown/bitbucket/hook/MirrorRemoteAdminTest.java

20
src/main/java/com/englishtown/bitbucket/hook/MirrorRemoteAdmin.java

@ -69,14 +69,13 @@ public class MirrorRemoteAdmin {
WebResource webResource = client WebResource webResource = client
.resource(settings.restApiURL + "/api/v4/projects") .resource(settings.restApiURL + "/api/v4/projects")
.queryParam("search", repository.getName()); .queryParam("search", repository.getName());
if (!settings.privateToken.isEmpty()) {
webResource = webResource.queryParam("private_token", plainPrivateToken); WebResource.Builder webResourceBuilder= webResource.getRequestBuilder().accept(MediaType.APPLICATION_JSON);
if (!settings.privateToken.isEmpty()) {
webResourceBuilder = webResourceBuilder.header("PRIVATE-TOKEN",plainPrivateToken);
} }
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON) ClientResponse response = webResourceBuilder.get(ClientResponse.class);
.get(ClientResponse.class);
if (response.getStatus() != 200) { if (response.getStatus() != 200) {
addToStream(passwordHandler, response.toString()); addToStream(passwordHandler, response.toString());
throw new RuntimeException("Failed : HTTP error code : " throw new RuntimeException("Failed : HTTP error code : "
@ -91,7 +90,10 @@ public class MirrorRemoteAdmin {
} }
Integer repoId = null; Integer repoId = null;
for (JsonNode project : output) { for (JsonNode project : output) {
if (project.get("path_with_namespace").asText().equals(repository.getProject().getKey() + "/" + repository.getName())) { if (!project.has("name_with_namespace")) {
continue;
}
if (project.get("name_with_namespace").asText().equals(repository.getProject().getName() + " / " + repository.getName())) {
repoId = project.get("id").asInt(); repoId = project.get("id").asInt();
break; break;
} }
@ -103,11 +105,11 @@ public class MirrorRemoteAdmin {
webResource = client webResource = client
.resource(settings.restApiURL + "/api/v4/projects/" + repoId); .resource(settings.restApiURL + "/api/v4/projects/" + repoId);
webResourceBuilder= webResource.getRequestBuilder().accept(MediaType.APPLICATION_JSON);
if (!settings.privateToken.isEmpty()) { if (!settings.privateToken.isEmpty()) {
webResource = webResource.queryParam("private_token", plainPrivateToken); webResourceBuilder = webResourceBuilder.header("PRIVATE-TOKEN",plainPrivateToken);
} }
response = webResource.accept(MediaType.APPLICATION_JSON) response = webResourceBuilder.delete(ClientResponse.class);
.delete(ClientResponse.class);
if (response.getStatus() != 202) { if (response.getStatus() != 202) {
addToStream(passwordHandler, response.toString()); addToStream(passwordHandler, response.toString());
throw new RuntimeException("Failed : HTTP error code : " throw new RuntimeException("Failed : HTTP error code : "

4
src/main/java/com/englishtown/bitbucket/hook/MirrorRepositoryServlet.java

@ -88,8 +88,8 @@ public class MirrorRepositoryServlet extends HttpServlet {
mirrorRemoteAdmin.delete(mirrorSettings, repository, outputHandler); mirrorRemoteAdmin.delete(mirrorSettings, repository, outputHandler);
allSettings.put("stdout" + mirrorSettings.suffix, outputHandler.getOutput()); allSettings.put("stdout" + mirrorSettings.suffix, outputHandler.getOutput());
} catch (Exception e) { } catch (Exception e) {
log.debug("Deleting Mirroring failed with " + e); log.debug("Deleting Mirroring failed with " + e.getMessage());
allSettings.put("stderr" + mirrorSettings.suffix, e + "\n" + outputHandler.getOutput()); allSettings.put("stderr" + mirrorSettings.suffix, e.getMessage() + "\n" + outputHandler.getOutput());
} }
} }
if (req.getParameter("trigger" + mirrorSettings.suffix) != null) { if (req.getParameter("trigger" + mirrorSettings.suffix) != null) {

25
src/test/java/com/englishtown/bitbucket/hook/MirrorRemoteAdminTest.java

@ -58,6 +58,7 @@ public class MirrorRemoteAdminTest {
when(repo.getName()).thenReturn("test"); when(repo.getName()).thenReturn("test");
project = mock(Project.class); project = mock(Project.class);
when(project.getKey()).thenReturn("PROJECT"); when(project.getKey()).thenReturn("PROJECT");
when(project.getName()).thenReturn("PROJECT");
when(repo.getProject()).thenReturn(project); when(repo.getProject()).thenReturn(project);
when(passwordEncryptor.decrypt(anyString())).then(AdditionalAnswers.returnsFirstArg()); when(passwordEncryptor.decrypt(anyString())).then(AdditionalAnswers.returnsFirstArg());
} }
@ -112,8 +113,9 @@ public class MirrorRemoteAdminTest {
mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port(); mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port();
mirrorSettings.privateToken = "PRIVATETOKEN"; mirrorSettings.privateToken = "PRIVATETOKEN";
stubFor(get(urlEqualTo("/api/v4/projects?search=test&private_token=PRIVATETOKEN")) stubFor(get(urlEqualTo("/api/v4/projects?search=test"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON)) .withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(200) .withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON) .withHeader("Content-Type", MediaType.APPLICATION_JSON)
@ -131,12 +133,13 @@ public class MirrorRemoteAdminTest {
mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port(); mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port();
mirrorSettings.privateToken = "PRIVATETOKEN"; mirrorSettings.privateToken = "PRIVATETOKEN";
stubFor(get(urlEqualTo("/api/v4/projects?search=test&private_token=PRIVATETOKEN")) stubFor(get(urlEqualTo("/api/v4/projects?search=test"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON)) .withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(200) .withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON) .withHeader("Content-Type", MediaType.APPLICATION_JSON)
.withBody("[{\"id\":1,\"path_with_namespace\":\"PROJECT/testing\"},{\"id\":3,\"path_with_namespace\":\"OTHER/test\"}]"))); .withBody("[{\"id\":1,\"name_with_namespace\":\"PROJECT / testing\"},{\"id\":3,\"name_with_namespace\":\"OTHER / test\"}]")));
try { try {
mirrorRemoteAdmin.delete(mirrorSettings, repo, handler); mirrorRemoteAdmin.delete(mirrorSettings, repo, handler);
Assert.fail("Exception not thrown"); Assert.fail("Exception not thrown");
@ -150,15 +153,17 @@ public class MirrorRemoteAdminTest {
mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port(); mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port();
mirrorSettings.privateToken = "PRIVATETOKEN"; mirrorSettings.privateToken = "PRIVATETOKEN";
stubFor(get(urlEqualTo("/api/v4/projects?search=test&private_token=PRIVATETOKEN")) stubFor(get(urlEqualTo("/api/v4/projects?search=test"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON)) .withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(200) .withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON) .withHeader("Content-Type", MediaType.APPLICATION_JSON)
.withBody("[{\"id\":1,\"path_with_namespace\":\"PROJECT/testing\"},{\"id\":2,\"path_with_namespace\":\"PROJECT/test\"},{\"id\":3,\"path_with_namespace\":\"OTHER/test\"}]"))); .withBody("[{\"id\":1,\"path_with_namespace\":\"PROJECT / testing\"},{\"id\":2,\"name_with_namespace\":\"PROJECT / test\"},{\"id\":3,\"name_with_namespace\":\"OTHER/test\"}]")));
stubFor(delete(urlEqualTo("/api/v4/projects/2?private_token=PRIVATETOKEN")) stubFor(delete(urlEqualTo("/api/v4/projects/2"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON)) .withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(403) .withStatus(403)
.withHeader("Content-Type", MediaType.TEXT_PLAIN) .withHeader("Content-Type", MediaType.TEXT_PLAIN)
@ -177,15 +182,17 @@ public class MirrorRemoteAdminTest {
mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port(); mirrorSettings.restApiURL = "http://localhost:" + wireMockRule.port();
mirrorSettings.privateToken = "PRIVATETOKEN"; mirrorSettings.privateToken = "PRIVATETOKEN";
stubFor(get(urlEqualTo("/api/v4/projects?search=test&private_token=PRIVATETOKEN")) stubFor(get(urlEqualTo("/api/v4/projects?search=test"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON)) .withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(200) .withStatus(200)
.withHeader("Content-Type", MediaType.APPLICATION_JSON) .withHeader("Content-Type", MediaType.APPLICATION_JSON)
.withBody("[{\"id\":1,\"path_with_namespace\":\"PROJECT/testing\"},{\"id\":2,\"path_with_namespace\":\"PROJECT/test\"},{\"id\":3,\"path_with_namespace\":\"OTHER/test\"}]"))); .withBody("[{\"id\":1,\"name_with_namespace\":\"PROJECT / testing\"},{\"id\":2,\"name_with_namespace\":\"PROJECT / test\"},{\"id\":3,\"name_with_namespace\":\"OTHER / test\"}]")));
stubFor(delete(urlEqualTo("/api/v4/projects/2?private_token=PRIVATETOKEN")) stubFor(delete(urlEqualTo("/api/v4/projects/2"))
.withHeader("Accept", equalTo(MediaType.APPLICATION_JSON)) .withHeader("Accept", equalTo(MediaType.APPLICATION_JSON))
.withHeader("PRIVATE-TOKEN", equalTo(mirrorSettings.privateToken))
.willReturn(aResponse() .willReturn(aResponse()
.withStatus(202))); .withStatus(202)));

Loading…
Cancel
Save