diff options
author | Martin Polden <mpolden@mpolden.no> | 2023-09-04 12:36:50 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2023-09-04 12:38:51 +0200 |
commit | a7b91304a577d856828e6088342dc3b2cf162b9c (patch) | |
tree | e56fec879d9ffb234ea774ac7ef7fe04afc0b1ca /configserver | |
parent | 3e4a2ecdd9ad0b3dab563fd8572644bb3cfae2e3 (diff) |
Add auth method to ApplicationClusterEndpoint
Diffstat (limited to 'configserver')
2 files changed, 72 insertions, 24 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/ContainerEndpointSerializer.java b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/ContainerEndpointSerializer.java index b813d56b345..33f47bbc154 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/ContainerEndpointSerializer.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/ContainerEndpointSerializer.java @@ -11,6 +11,7 @@ import com.yahoo.slime.SlimeUtils; import java.util.ArrayList; import java.util.List; +import java.util.OptionalInt; /** * Contains all methods for de-/serializing ContainerEndpoints to/from JSON. @@ -32,48 +33,48 @@ public class ContainerEndpointSerializer { private static final String namesField = "names"; private static final String weightField = "weight"; private static final String routingMethodField = "routingMethod"; + private static final String authMethodField = "authMethod"; private ContainerEndpointSerializer() {} public static ContainerEndpoint endpointFromSlime(Inspector inspector) { - final var clusterId = inspector.field(clusterIdField).asString(); - final var scope = inspector.field(scopeField).asString(); - final var namesInspector = inspector.field(namesField); - final var weight = SlimeUtils.optionalInteger(inspector.field(weightField)); + String clusterId = inspector.field(clusterIdField).asString(); + String scope = inspector.field(scopeField).asString(); + Inspector namesInspector = inspector.field(namesField); + OptionalInt weight = SlimeUtils.optionalInteger(inspector.field(weightField)); // assign default routingmethod. Remove when 7.507 is latest version - // Cannot be used before all endpoints are assigned explicit routingmethod (from controller) - final var routingMethod = SlimeUtils.optionalString(inspector.field(routingMethodField)).orElse(ApplicationClusterEndpoint.RoutingMethod.sharedLayer4.name()); + // Cannot be used before all endpoints are assigned explicit routing method (from controller) + ApplicationClusterEndpoint.RoutingMethod routingMethod = SlimeUtils.optionalString(inspector.field(routingMethodField)) + .map(ContainerEndpointSerializer::routingMethodFrom) + .orElse(ApplicationClusterEndpoint.RoutingMethod.sharedLayer4); + ApplicationClusterEndpoint.AuthMethod authMethod = SlimeUtils.optionalString(inspector.field(authMethodField)) + .map(ContainerEndpointSerializer::authMethodFrom) + .orElse(ApplicationClusterEndpoint.AuthMethod.mtls); if (clusterId.isEmpty()) { throw new IllegalStateException("'clusterId' missing on serialized ContainerEndpoint"); } - if (scope.isEmpty()) { throw new IllegalStateException("'scope' missing on serialized ContainerEndpoint"); } - - if (! namesInspector.valid()) { + if (!namesInspector.valid()) { throw new IllegalStateException("'names' missing on serialized ContainerEndpoint"); } - if(routingMethod.isEmpty()) { - throw new IllegalStateException("'routingMethod' missing on serialized ContainerEndpoint"); - } - - final var names = new ArrayList<String>(); + List<String> names = new ArrayList<>(); namesInspector.traverse((ArrayTraverser) (idx, nameInspector) -> { final var containerName = nameInspector.asString(); names.add(containerName); }); - return new ContainerEndpoint(clusterId, ApplicationClusterEndpoint.Scope.valueOf(scope), names, weight, - ApplicationClusterEndpoint.RoutingMethod.valueOf(routingMethod)); + return new ContainerEndpoint(clusterId, scopeFrom(scope), names, weight, routingMethod, authMethod); } public static List<ContainerEndpoint> endpointListFromSlime(Slime slime) { final var inspector = slime.get(); return endpointListFromSlime(inspector); } + public static List<ContainerEndpoint> endpointListFromSlime(Inspector inspector) { final var endpoints = new ArrayList<ContainerEndpoint>(); @@ -88,11 +89,12 @@ public class ContainerEndpointSerializer { public static void endpointToSlime(Cursor cursor, ContainerEndpoint endpoint) { cursor.setString(clusterIdField, endpoint.clusterId()); - cursor.setString(scopeField, endpoint.scope().name()); + cursor.setString(scopeField, asString(endpoint.scope())); endpoint.weight().ifPresent(w -> cursor.setLong(weightField, w)); final var namesInspector = cursor.setArray(namesField); endpoint.names().forEach(namesInspector::addString); - cursor.setString(routingMethodField, endpoint.routingMethod().name()); + cursor.setString(routingMethodField, asString(endpoint.routingMethod())); + cursor.setString(authMethodField, asString(endpoint.authMethod())); } public static Slime endpointListToSlime(List<ContainerEndpoint> endpoints) { @@ -107,4 +109,53 @@ public class ContainerEndpointSerializer { return slime; } + private static ApplicationClusterEndpoint.RoutingMethod routingMethodFrom(String s) { + return switch (s) { + case "shared" -> ApplicationClusterEndpoint.RoutingMethod.shared; + case "sharedLayer4" -> ApplicationClusterEndpoint.RoutingMethod.sharedLayer4; + case "exclusive" -> ApplicationClusterEndpoint.RoutingMethod.exclusive; + default -> throw new IllegalArgumentException("Unknown routing method '" + s + "'"); + }; + } + + private static ApplicationClusterEndpoint.AuthMethod authMethodFrom(String s) { + return switch (s) { + case "mtls" -> ApplicationClusterEndpoint.AuthMethod.mtls; + case "token" -> ApplicationClusterEndpoint.AuthMethod.token; + default -> throw new IllegalArgumentException("Unknown auth method '" + s + "'"); + }; + } + + private static ApplicationClusterEndpoint.Scope scopeFrom(String s) { + return switch (s) { + case "global" -> ApplicationClusterEndpoint.Scope.global; + case "application" -> ApplicationClusterEndpoint.Scope.application; + case "zone" -> ApplicationClusterEndpoint.Scope.zone; + default -> throw new IllegalArgumentException("Unknown scope '" + s + "'"); + }; + } + + private static String asString(ApplicationClusterEndpoint.RoutingMethod routingMethod) { + return switch (routingMethod) { + case shared -> "shared"; + case sharedLayer4 -> "sharedLayer4"; + case exclusive -> "exclusive"; + }; + } + + private static String asString(ApplicationClusterEndpoint.AuthMethod authMethod) { + return switch (authMethod) { + case mtls -> "mtls"; + case token -> "token"; + }; + } + + private static String asString(ApplicationClusterEndpoint.Scope scope) { + return switch (scope) { + case global -> "global"; + case application -> "application"; + case zone -> "zone"; + }; + } + } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/ContainerEndpointSerializerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/ContainerEndpointSerializerTest.java index c8f31697c5e..5e7fe64f998 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/ContainerEndpointSerializerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/ContainerEndpointSerializerTest.java @@ -8,7 +8,6 @@ import org.junit.Test; import java.util.List; import java.util.OptionalInt; -import java.util.OptionalLong; import static org.junit.Assert.assertEquals; @@ -46,11 +45,9 @@ public class ContainerEndpointSerializerTest { @Test public void writeReadEndpoints() { - final var endpoints = List.of(new ContainerEndpoint("foo", ApplicationClusterEndpoint.Scope.global, List.of("a", "b"), OptionalInt.of(3), ApplicationClusterEndpoint.RoutingMethod.shared)); - final var serialized = ContainerEndpointSerializer.endpointListToSlime(endpoints); - final var deserialized = ContainerEndpointSerializer.endpointListFromSlime(serialized); - - assertEquals(endpoints, deserialized); + List<ContainerEndpoint> endpoints = List.of(new ContainerEndpoint("foo", ApplicationClusterEndpoint.Scope.global, List.of("a", "b"), OptionalInt.of(3), + ApplicationClusterEndpoint.RoutingMethod.shared, ApplicationClusterEndpoint.AuthMethod.token)); + assertEquals(endpoints, ContainerEndpointSerializer.endpointListFromSlime(ContainerEndpointSerializer.endpointListToSlime(endpoints))); } } |