diff options
author | Bjørn Christian Seime <bjorncs@oath.com> | 2018-04-18 12:30:55 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@oath.com> | 2018-04-18 13:25:23 +0200 |
commit | 142f82353c20d64cc00fb5c58a3e1cf874ee6678 (patch) | |
tree | 0e00f2c899cb40098d805e2df8fa3d455f30ba87 | |
parent | 631171a987081467d7d4fe9c8cbd6f46fdb1f31a (diff) |
Add type for provider unique instance id
6 files changed, 145 insertions, 4 deletions
diff --git a/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/identitydocument/IdentityDocumentGenerator.java b/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/identitydocument/IdentityDocumentGenerator.java index 8ce13111536..95e9713f335 100644 --- a/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/identitydocument/IdentityDocumentGenerator.java +++ b/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/identitydocument/IdentityDocumentGenerator.java @@ -57,7 +57,7 @@ public class IdentityDocumentGenerator { encodedIdentityDocument, signature, SignedIdentityDocument.DEFAULT_KEY_VERSION, - identityDocument.providerUniqueId.asString(), + identityDocument.providerUniqueId.toVespaUniqueInstanceId().asDottedString(), toZoneDnsSuffix(zone, zoneConfig.certDnsSuffix()), zoneConfig.domain() + "." + zoneConfig.serviceName(), zoneConfig.ztsUrl(), diff --git a/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/identitydocument/ProviderUniqueId.java b/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/identitydocument/ProviderUniqueId.java index 9736b9d48a9..c956ead5f8c 100644 --- a/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/identitydocument/ProviderUniqueId.java +++ b/athenz-identity-provider-service/src/main/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/identitydocument/ProviderUniqueId.java @@ -2,6 +2,7 @@ package com.yahoo.vespa.hosted.athenz.instanceproviderservice.identitydocument; import com.fasterxml.jackson.annotation.JsonProperty; +import com.yahoo.vespa.athenz.identityprovider.api.VespaUniqueInstanceId; import java.util.Objects; @@ -41,8 +42,8 @@ public class ProviderUniqueId { this.clusterIndex = clusterIndex; } - public String asString() { - return String.format("%d.%s.%s.%s.%s.%s.%s", clusterIndex, clusterId, instance, application, tenant, region, environment); + public VespaUniqueInstanceId toVespaUniqueInstanceId() { + return new VespaUniqueInstanceId(clusterIndex, clusterId, instance, application, tenant, region, environment); } @Override diff --git a/athenz-identity-provider-service/src/test/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/instanceconfirmation/InstanceValidatorTest.java b/athenz-identity-provider-service/src/test/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/instanceconfirmation/InstanceValidatorTest.java index 84105c5b551..ffd3265957f 100644 --- a/athenz-identity-provider-service/src/test/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/instanceconfirmation/InstanceValidatorTest.java +++ b/athenz-identity-provider-service/src/test/java/com/yahoo/vespa/hosted/athenz/instanceproviderservice/instanceconfirmation/InstanceValidatorTest.java @@ -135,7 +135,7 @@ public class InstanceValidatorTest { new SignedIdentityDocument(encodedIdentityDocument, Base64.getEncoder().encodeToString(sigGenerator.sign()), 0, - identityDocument.providerUniqueId.asString(), + identityDocument.providerUniqueId.toVespaUniqueInstanceId().asDottedString(), "dnssuffix", "service", "localhost/zts", diff --git a/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identityprovider/api/VespaUniqueInstanceId.java b/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identityprovider/api/VespaUniqueInstanceId.java new file mode 100644 index 00000000000..5539ba53882 --- /dev/null +++ b/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identityprovider/api/VespaUniqueInstanceId.java @@ -0,0 +1,109 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.athenz.identityprovider.api; + +import java.util.Objects; + +/** + * @author bjorncs + */ +public class VespaUniqueInstanceId { + + private final int clusterIndex; + private final String clusterId; + private final String instance; + private final String application; + private final String tenant; + private final String region; + private final String environment; + + public VespaUniqueInstanceId(int clusterIndex, + String clusterId, + String instance, + String application, + String tenant, + String region, + String environment) { + this.clusterIndex = clusterIndex; + this.clusterId = clusterId; + this.instance = instance; + this.application = application; + this.tenant = tenant; + this.region = region; + this.environment = environment; + } + + public static VespaUniqueInstanceId fromDottedString(String instanceId) { + String[] tokens = instanceId.split("\\."); + if (tokens.length != 7) { + throw new IllegalArgumentException("Invalid instance id: " + instanceId); + } + return new VespaUniqueInstanceId( + Integer.parseInt(tokens[0]), tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]); + } + + public String asDottedString() { + return String.format( + "%d.%s.%s.%s.%s.%s.%s", + clusterIndex, clusterId, instance, application, tenant, region, environment); + } + + public int clusterIndex() { + return clusterIndex; + } + + public String clusterId() { + return clusterId; + } + + public String instance() { + return instance; + } + + public String application() { + return application; + } + + public String tenant() { + return tenant; + } + + public String region() { + return region; + } + + public String environment() { + return environment; + } + + @Override + public String toString() { + return "VespaUniqueInstanceId{" + + "clusterIndex=" + clusterIndex + + ", clusterId='" + clusterId + '\'' + + ", instance='" + instance + '\'' + + ", application='" + application + '\'' + + ", tenant='" + tenant + '\'' + + ", region='" + region + '\'' + + ", environment='" + environment + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + VespaUniqueInstanceId that = (VespaUniqueInstanceId) o; + return clusterIndex == that.clusterIndex && + Objects.equals(clusterId, that.clusterId) && + Objects.equals(instance, that.instance) && + Objects.equals(application, that.application) && + Objects.equals(tenant, that.tenant) && + Objects.equals(region, that.region) && + Objects.equals(environment, that.environment); + } + + @Override + public int hashCode() { + return Objects.hash(clusterIndex, clusterId, instance, application, tenant, region, environment); + } +} diff --git a/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identityprovider/api/package-info.java b/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identityprovider/api/package-info.java new file mode 100644 index 00000000000..e5419b94a19 --- /dev/null +++ b/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identityprovider/api/package-info.java @@ -0,0 +1,8 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +/** + * @author bjorncs + */ +@ExportPackage +package com.yahoo.vespa.athenz.identityprovider.api; + +import com.yahoo.osgi.annotation.ExportPackage;
\ No newline at end of file diff --git a/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/identityprovider/api/VespaUniqueInstanceIdTest.java b/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/identityprovider/api/VespaUniqueInstanceIdTest.java new file mode 100644 index 00000000000..8c4e4c1262d --- /dev/null +++ b/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/identityprovider/api/VespaUniqueInstanceIdTest.java @@ -0,0 +1,23 @@ +package com.yahoo.vespa.athenz.identityprovider.api; + +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * @author bjorncs + */ +public class VespaUniqueInstanceIdTest { + + @Test + public void can_serialize_to_and_deserialize_from_string() { + VespaUniqueInstanceId id = + new VespaUniqueInstanceId(1, "cluster-id", "instance", "application", "tenant", "region", "environment"); + String stringRepresentation = id.asDottedString(); + String expectedStringRepresentation = "1.cluster-id.instance.application.tenant.region.environment"; + assertEquals(expectedStringRepresentation, stringRepresentation); + VespaUniqueInstanceId deserializedId = VespaUniqueInstanceId.fromDottedString(stringRepresentation); + assertEquals(id, deserializedId); + } + +}
\ No newline at end of file |