Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding per profile override feature #331

Merged
merged 1 commit into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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