Skip to content

Commit

Permalink
Adding per profile override feature
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdrobison committed Aug 15, 2019
1 parent f7dce49 commit 9206517
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
7 changes: 6 additions & 1 deletion Readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 36 additions & 17 deletions src/main/java/com/okta/tools/OktaAwsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> 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<ArrayList<Path>> 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")) {
Expand Down Expand Up @@ -108,17 +113,31 @@ private static String getOutput(Process process) throws IOException {
}
}

private static Optional<Path> getConfigFile() {
Path configInWorkingDir = Paths.get(CONFIG_FILENAME);
if (configInWorkingDir.toFile().isFile()) {
return Optional.of(configInWorkingDir);
private static Optional<ArrayList<Path>> getConfigFile(String profile) {
ArrayList<String> configFiles = new ArrayList<>();
configFiles.add(CONFIG_FILENAME);
if (StringUtils.isNotBlank(profile)) {
configFiles.add(String.format(CONFIG_PROFILE_FILENAME_FORMAT, profile));
}

ArrayList<Path> 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();
}

Expand Down

0 comments on commit 9206517

Please sign in to comment.