summaryrefslogtreecommitdiffstats
path: root/controller-api/src
diff options
context:
space:
mode:
Diffstat (limited to 'controller-api/src')
-rw-r--r--controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchive.java38
-rw-r--r--controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchiveTest.java20
-rw-r--r--controller-api/src/test/resources/system-flags-with-invalid-file-name/flags/my-test-flag/file-name-without-dot-json8
-rw-r--r--controller-api/src/test/resources/system-flags-with-unknown-file-name/flags/my-test-flag/main.prod.unknown-region.json8
4 files changed, 71 insertions, 3 deletions
diff --git a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchive.java b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchive.java
index 7e9da53b1c9..0cb4baab790 100644
--- a/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchive.java
+++ b/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchive.java
@@ -1,6 +1,7 @@
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.systemflags.v1;
+import com.yahoo.config.provision.SystemName;
import com.yahoo.vespa.flags.FlagId;
import com.yahoo.vespa.flags.json.FlagData;
@@ -13,11 +14,13 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
+import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@@ -47,7 +50,7 @@ public class SystemFlagsDataArchive {
ZipEntry entry;
while ((entry = zipIn.getNextEntry()) != null) {
String name = entry.getName();
- if (!entry.isDirectory() && name.startsWith("flags/") && name.endsWith(".json")) {
+ if (!entry.isDirectory() && name.startsWith("flags/")) {
Path filePath = Paths.get(name);
String rawData = new String(zipIn.readAllBytes(), StandardCharsets.UTF_8);
addFile(builder, rawData, filePath);
@@ -70,7 +73,7 @@ public class SystemFlagsDataArchive {
directoryStream.forEach(absolutePath -> {
Path relativePath = root.relativize(absolutePath);
if (!Files.isDirectory(absolutePath) &&
- relativePath.startsWith("flags") && relativePath.toString().endsWith(".json")) {
+ relativePath.startsWith("flags")) {
String rawData = uncheck(() -> Files.readString(absolutePath, StandardCharsets.UTF_8));
addFile(builder, rawData, relativePath);
}
@@ -86,7 +89,7 @@ public class SystemFlagsDataArchive {
files.forEach((flagId, fileMap) -> {
fileMap.forEach((filename, flagData) -> {
uncheck(() -> {
- zipOut.putNextEntry(new ZipEntry("flags/" + flagId.toString() + "/" + filename));
+ zipOut.putNextEntry(new ZipEntry(toFilePath(flagId, filename)));
zipOut.write(flagData.serializeToUtf8Json());
zipOut.closeEntry();
});
@@ -112,8 +115,33 @@ public class SystemFlagsDataArchive {
return targetData;
}
+ public void validateAllFilesAreForTargets(SystemName currentSystem, Set<FlagsTarget> targets) throws IllegalArgumentException {
+ Set<String> validFiles = targets.stream()
+ .flatMap(target -> target.flagDataFilesPrioritized().stream())
+ .collect(Collectors.toSet());
+ Set<SystemName> otherSystems = Arrays.stream(SystemName.values())
+ .filter(systemName -> systemName != currentSystem)
+ .collect(Collectors.toSet());
+ files.forEach((flagId, fileMap) -> {
+ for (String filename : fileMap.keySet()) {
+ boolean isFileForOtherSystem = otherSystems.stream()
+ .anyMatch(system -> filename.startsWith(system.value() + "."));
+ boolean isFileForCurrentSystem = validFiles.contains(filename);
+ if (!isFileForOtherSystem && !isFileForCurrentSystem) {
+ throw new IllegalArgumentException("Unknown flag file: " + toFilePath(flagId, filename));
+ }
+ }
+ });
+ }
+
private static void addFile(Builder builder, String rawData, Path filePath) {
String filename = filePath.getFileName().toString();
+ if (filename.startsWith(".")) {
+ return; // Ignore files starting with '.'
+ }
+ if (!filename.endsWith(".json")) {
+ throw new IllegalArgumentException(String.format("Only JSON files are allowed in 'flags/' directory (found '%s')", filePath.toString()));
+ }
FlagId directoryDeducedFlagId = new FlagId(filePath.getName(1).toString());
FlagData flagData;
if (rawData.isBlank()) {
@@ -129,6 +157,10 @@ public class SystemFlagsDataArchive {
builder.addFile(filename, flagData);
}
+ private static String toFilePath(FlagId flagId, String filename) {
+ return "flags/" + flagId.toString() + "/" + filename;
+ }
+
public static class Builder {
private final Map<FlagId, Map<String, FlagData>> files = new TreeMap<>();
diff --git a/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchiveTest.java b/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchiveTest.java
index 14584650d3b..6e78b8da91c 100644
--- a/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchiveTest.java
+++ b/controller-api/src/test/java/com/yahoo/vespa/hosted/controller/api/systemflags/v1/SystemFlagsDataArchiveTest.java
@@ -13,6 +13,7 @@ import com.yahoo.vespa.flags.RawFlag;
import com.yahoo.vespa.flags.json.FlagData;
import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import java.io.BufferedInputStream;
@@ -27,6 +28,7 @@ import java.net.URI;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import static java.util.stream.Collectors.toList;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,6 +45,9 @@ public class SystemFlagsDataArchiveTest {
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
+ @Rule
+ public final ExpectedException expectedException = ExpectedException.none();
+
private static FlagsTarget mainControllerTarget = FlagsTarget.forController(SYSTEM);
private static FlagsTarget prodUsWestCfgTarget = createConfigserverTarget(Environment.prod, "us-west-1");
private static FlagsTarget prodUsEast3CfgTarget = createConfigserverTarget(Environment.prod, "us-east-3");
@@ -84,6 +89,21 @@ public class SystemFlagsDataArchiveTest {
assertFlagDataHasValue(archive, FLAG_WITH_EMPTY_DATA, devUsEast1CfgTarget, "main");
}
+ @Test
+ public void throws_exception_on_non_json_file() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Only JSON files are allowed in 'flags/' directory (found 'flags/my-test-flag/file-name-without-dot-json')");
+ SystemFlagsDataArchive.fromDirectory(Paths.get("src/test/resources/system-flags-with-invalid-file-name/"));
+ }
+
+ @Test
+ public void throws_exception_on_unknown_file() {
+ SystemFlagsDataArchive archive = SystemFlagsDataArchive.fromDirectory(Paths.get("src/test/resources/system-flags-with-unknown-file-name/"));
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Unknown flag file: flags/my-test-flag/main.prod.unknown-region.json");
+ archive.validateAllFilesAreForTargets(SystemName.main, Set.of(mainControllerTarget, prodUsWestCfgTarget));
+ }
+
private static void assertArchiveReturnsCorrectTestFlagDataForTarget(SystemFlagsDataArchive archive) {
assertFlagDataHasValue(archive, MY_TEST_FLAG, mainControllerTarget, "main.controller");
assertFlagDataHasValue(archive, MY_TEST_FLAG, prodUsWestCfgTarget, "main.prod.us-west-1");
diff --git a/controller-api/src/test/resources/system-flags-with-invalid-file-name/flags/my-test-flag/file-name-without-dot-json b/controller-api/src/test/resources/system-flags-with-invalid-file-name/flags/my-test-flag/file-name-without-dot-json
new file mode 100644
index 00000000000..5924eb860c0
--- /dev/null
+++ b/controller-api/src/test/resources/system-flags-with-invalid-file-name/flags/my-test-flag/file-name-without-dot-json
@@ -0,0 +1,8 @@
+{
+ "id" : "my-test-flag",
+ "rules" : [
+ {
+ "value" : "default"
+ }
+ ]
+} \ No newline at end of file
diff --git a/controller-api/src/test/resources/system-flags-with-unknown-file-name/flags/my-test-flag/main.prod.unknown-region.json b/controller-api/src/test/resources/system-flags-with-unknown-file-name/flags/my-test-flag/main.prod.unknown-region.json
new file mode 100644
index 00000000000..5924eb860c0
--- /dev/null
+++ b/controller-api/src/test/resources/system-flags-with-unknown-file-name/flags/my-test-flag/main.prod.unknown-region.json
@@ -0,0 +1,8 @@
+{
+ "id" : "my-test-flag",
+ "rules" : [
+ {
+ "value" : "default"
+ }
+ ]
+} \ No newline at end of file