From fb7a22c2c74d82aa2de12d4a3c3f2b6b288efc69 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Fri, 21 Dec 2018 10:02:04 -0500 Subject: [PATCH] Add support for loading token by filepath or environment var --- README.md | 12 ++++++++++-- src/main/java/net/conjur/api/Token.java | 11 +++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 23d7417..059d6d3 100644 --- a/README.md +++ b/README.md @@ -135,8 +135,16 @@ Conjur conjur = new Conjur(credentials); #### Authorization Token ```java -String authTokenString = new String(Files.readAllBytes(Paths.get('path/to/conjur/authentication/token.json'))); -Token token = Token.fromJson(authTokenString); +Token token = Token.fromFile(Paths.get('path/to/conjur/authentication/token.json')); +Conjur conjur = new Conjur(token); +``` + +Alternatively, to use the `CONJUR_AUTHN_TOKEN_FILE` environment variable: +```bash +export CONJUR_AUTHN_TOKEN_FILE="path/to/conjur/authentication/token.json" +``` +```java +Token token = Token.fromEnv(); Conjur conjur = new Conjur(token); ``` diff --git a/src/main/java/net/conjur/api/Token.java b/src/main/java/net/conjur/api/Token.java index 29cf886..bf0347a 100644 --- a/src/main/java/net/conjur/api/Token.java +++ b/src/main/java/net/conjur/api/Token.java @@ -17,6 +17,7 @@ public class Token { private static final DateTimeFormatter DATE_TIME_FORMATTER = // tokens use dates like 2013-10-01 18:48:32 UTC DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss ZZZ"); + private static final String TOKEN_FILE_ENV_VARIABLE = "CONJUR_AUTHN_TOKEN_FILE"; // Hold our fields in here to facilitate json serialization/deserialization private static class Fields { @@ -123,6 +124,16 @@ public static Token fromJson(String json){ return new Token(json); } + public static Token fromFile(Path filepath) { + String json = File.readAllBytes(filepath); + return fromJson(json); + } + + public static Token fromEnv() { + String tokenFilePath = System.getenv(TOKEN_FILE_ENV_VARIABLE); + return fromFile(Paths.get(tokenFilePath)); + } + private String fromBase64(String base64){ return new String(Base64.decodeBase64(base64), StandardCharsets.UTF_8); }