Block Fetch Rest API call when instanceLabel is missing
Bug: Issue 12728
Change-Id: I547b587f8ccad9676739ba9565381b622ddae3ce
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 502d3b7..bdfc1c3 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
@@ -14,6 +14,9 @@
package com.googlesource.gerrit.plugins.replication.pull.client;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.Strings;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.entities.Project;
import com.google.gerrit.extensions.restapi.Url;
@@ -64,7 +67,11 @@
this.httpClientFactory = httpClientFactory;
this.source = source;
this.instanceLabel =
- replicationConfig.getConfig().getString("replication", null, "instanceLabel");
+ Strings.nullToEmpty(
+ replicationConfig.getConfig().getString("replication", null, "instanceLabel"))
+ .trim();
+ requireNonNull(
+ Strings.emptyToNull(instanceLabel), "replication.instanceLabel cannot be null or empty");
}
public HttpResult callFetch(Project.NameKey project, String refName, URIish targetUri)
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 669161f..c62ddab 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
@@ -15,6 +15,7 @@
package com.googlesource.gerrit.plugins.replication.pull.client;
import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
import static javax.servlet.http.HttpServletResponse.SC_CREATED;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
@@ -140,6 +141,30 @@
.isEqualTo(expectedHeader.getValue());
}
+ @Test
+ public void shouldThrowExceptionWhenInstanceLabelIsNull() {
+ when(config.getString("replication", null, "instanceLabel")).thenReturn(null);
+ assertThrows(
+ NullPointerException.class,
+ () -> new FetchRestApiClient(credentials, httpClientFactory, replicationConfig, source));
+ }
+
+ @Test
+ public void shouldTrimInstanceLabel() {
+ when(config.getString("replication", null, "instanceLabel")).thenReturn(" ");
+ assertThrows(
+ NullPointerException.class,
+ () -> new FetchRestApiClient(credentials, httpClientFactory, replicationConfig, source));
+ }
+
+ @Test
+ public void shouldThrowExceptionWhenInstanceLabelIsEmpty() {
+ when(config.getString("replication", null, "instanceLabel")).thenReturn("");
+ assertThrows(
+ NullPointerException.class,
+ () -> new FetchRestApiClient(credentials, httpClientFactory, replicationConfig, source));
+ }
+
public String readPayload(HttpPost entity) throws UnsupportedOperationException, IOException {
ByteBuffer buf = IO.readWholeStream(entity.getEntity().getContent(), 1024);
return RawParseUtils.decode(buf.array(), buf.arrayOffset(), buf.limit()).trim();