From 92065179fa4f7afab3f161ba2efac61f2bf60cc0 Mon Sep 17 00:00:00 2001 From: Chris Robison Date: Thu, 15 Aug 2019 08:44:43 -0600 Subject: [PATCH] Adding per profile override feature --- Readme.MD | 7 ++- .../java/com/okta/tools/OktaAwsConfig.java | 53 +++++++++++++------ 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/Readme.MD b/Readme.MD index c32e8ac..a75be46 100644 --- a/Readme.MD +++ b/Readme.MD @@ -162,7 +162,12 @@ Here is the list of parameters that can be environment variables or settings in - Your link is located under ```EMBED LINK``` - Replace the example values in ```~/.okta/config.properties``` with your values -Note: environment variables take precedence over the config file. +You can specify configuration overrides for each profile by creating a ```~/.okta/config.{profilename}.properties``` file. The base settings +will be loaded first and the profile-specific settings will be loaded after, allowing you to only override specific settings that need to be different. +For example, if you want the ```prod``` profile to connect to a different Okta org, create a ```~/.okta/config.prod.properties```file and set +```OKTA_ORG``` to something different. + +Note: environment variables take precedence over any config file. ## Troubleshooting diff --git a/src/main/java/com/okta/tools/OktaAwsConfig.java b/src/main/java/com/okta/tools/OktaAwsConfig.java index 532da98..4b25401 100644 --- a/src/main/java/com/okta/tools/OktaAwsConfig.java +++ b/src/main/java/com/okta/tools/OktaAwsConfig.java @@ -15,12 +15,14 @@ */ package com.okta.tools; +import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.SystemUtils; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.Optional; import java.util.Properties; import java.util.logging.Logger; @@ -31,20 +33,23 @@ final class OktaAwsConfig { private static final Logger logger = Logger.getLogger(OktaAwsConfig.class.getName()); private static final String CONFIG_FILENAME = "config.properties"; + private static final String CONFIG_PROFILE_FILENAME_FORMAT = "config.%s.properties"; static OktaAwsCliEnvironment loadEnvironment() { - return loadEnvironment(null); + return loadEnvironment(System.getenv("OKTA_PROFILE")); } static OktaAwsCliEnvironment loadEnvironment(String profile) { Properties properties = new Properties(); - Optional path = getConfigFile(); - if (path.isPresent()) { - try (InputStream config = new FileInputStream(path.get().toFile())) { - logger.finer(() -> "Reading config settings from file: " + path.get().toAbsolutePath().toString()); - properties.load(new InputStreamReader(config)); - } catch (IOException e) { - throw new IllegalStateException(e); + Optional> paths = getConfigFile(profile); + if (paths.isPresent()) { + for (Path path : paths.get()) { + try (InputStream config = new FileInputStream(path.toFile())) { + logger.finer(() -> "Reading config settings from file: " + path.toAbsolutePath().toString()); + properties.load(new InputStreamReader(config)); + } catch (IOException e) { + throw new IllegalStateException(e); + } } } else { try (InputStream config = properties.getClass().getResourceAsStream("/config.properties")) { @@ -108,17 +113,31 @@ private static String getOutput(Process process) throws IOException { } } - private static Optional getConfigFile() { - Path configInWorkingDir = Paths.get(CONFIG_FILENAME); - if (configInWorkingDir.toFile().isFile()) { - return Optional.of(configInWorkingDir); + private static Optional> getConfigFile(String profile) { + ArrayList configFiles = new ArrayList<>(); + configFiles.add(CONFIG_FILENAME); + if (StringUtils.isNotBlank(profile)) { + configFiles.add(String.format(CONFIG_PROFILE_FILENAME_FORMAT, profile)); + } + + ArrayList paths = new ArrayList<>(); + for (String configFile : configFiles) { + Path configInWorkingDir = Paths.get(configFile); + if (configInWorkingDir.toFile().isFile()) { + paths.add(configInWorkingDir); + } + Path userHome = Paths.get(System.getProperty("user.home")); + Path oktaDir = userHome.resolve(".okta"); + Path configInOktaDir = oktaDir.resolve(configFile); + if (configInOktaDir.toFile().isFile()) { + paths.add(configInOktaDir); + } } - Path userHome = Paths.get(System.getProperty("user.home")); - Path oktaDir = userHome.resolve(".okta"); - Path configInOktaDir = oktaDir.resolve(CONFIG_FILENAME); - if (configInOktaDir.toFile().isFile()) { - return Optional.of(configInOktaDir); + + if (!paths.isEmpty()) { + return Optional.of(paths); } + return Optional.empty(); }