diff options
author | Bjørn Christian Seime <bjorncs@oath.com> | 2018-07-26 17:44:38 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@oath.com> | 2018-07-26 17:45:54 +0200 |
commit | 518d53a5d5e08924280ef5da61db241f9f4cdf8b (patch) | |
tree | 94ae9be347854e13e94afd3cff15657052eb6047 /vespa-athenz | |
parent | 10d7cd86098937b8b559099e34dea365be70dea9 (diff) |
Add utility method to find all services from sia directory
Diffstat (limited to 'vespa-athenz')
-rw-r--r-- | vespa-athenz/src/main/java/com/yahoo/vespa/athenz/utils/SiaUtils.java | 24 | ||||
-rw-r--r-- | vespa-athenz/src/test/java/com/yahoo/vespa/athenz/utils/SiaUtilsTest.java | 40 |
2 files changed, 64 insertions, 0 deletions
diff --git a/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/utils/SiaUtils.java b/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/utils/SiaUtils.java index 55e9103b040..05459e5488b 100644 --- a/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/utils/SiaUtils.java +++ b/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/utils/SiaUtils.java @@ -7,13 +7,18 @@ import com.yahoo.vespa.athenz.tls.X509CertificateUtils; import java.io.IOException; import java.io.UncheckedIOException; +import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.security.PrivateKey; import java.security.cert.X509Certificate; +import java.util.List; import java.util.Optional; +import java.util.stream.StreamSupport; + +import static java.util.stream.Collectors.toList; /** * Misc utility methods for SIA provided credentials @@ -105,6 +110,25 @@ public class SiaUtils { } } + public static List<AthenzService> findSiaServices() { + return findSiaServices(DEFAULT_SIA_DIRECTORY); + } + + public static List<AthenzService> findSiaServices(Path root) { + String keyFileSuffix = ".key.pem"; + Path keysDirectory = root.resolve("keys"); + try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(keysDirectory)) { + return StreamSupport.stream(directoryStream.spliterator(), false) + .map(path -> path.getFileName().toString()) + .filter(fileName -> fileName.endsWith(keyFileSuffix)) + .map(fileName -> fileName.substring(0, fileName.length() - keyFileSuffix.length())) + .map(AthenzService::new) + .collect(toList()); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + private static Path toTempFile(Path file) { return Paths.get(file.toAbsolutePath().toString() + ".tmp"); } diff --git a/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/utils/SiaUtilsTest.java b/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/utils/SiaUtilsTest.java new file mode 100644 index 00000000000..0282373cdaf --- /dev/null +++ b/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/utils/SiaUtilsTest.java @@ -0,0 +1,40 @@ +package com.yahoo.vespa.athenz.utils; + +import com.yahoo.vespa.athenz.api.AthenzService; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.junit.Assert.assertThat; + +/** + * @author bjorncs + */ +public class SiaUtilsTest { + + @Rule + public TemporaryFolder tempDirectory = new TemporaryFolder(); + + @Test + public void it_finds_all_identity_names_from_files_in_sia_keys_directory() throws IOException { + Path siaRoot = tempDirectory.getRoot().toPath(); + Files.createDirectory(siaRoot.resolve("keys")); + AthenzService fooService = new AthenzService("my.domain.foo"); + Files.createFile(SiaUtils.getPrivateKeyFile(siaRoot, fooService)); + AthenzService barService = new AthenzService("my.domain.bar"); + Files.createFile(SiaUtils.getPrivateKeyFile(siaRoot, barService)); + + List<AthenzService> siaIdentities = SiaUtils.findSiaServices(siaRoot); + assertThat(siaIdentities.size(), equalTo(2)); + assertThat(siaIdentities, hasItem(fooService)); + assertThat(siaIdentities, hasItem(barService)); + } + +}
\ No newline at end of file |