package com.okta.tools;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.Properties;

final class OktaAwsConfig {

    private static final String CONFIG_FILENAME = "config.properties";

    static OktaAwsCliEnvironment loadEnvironment() {
        return loadEnvironment(null);
    }

    static OktaAwsCliEnvironment loadEnvironment(String profile) {
        Properties properties = new Properties();
        Optional<Path> path = getConfigFile();
        if (path.isPresent()) {
            try (InputStream config = new FileInputStream(path.get().toFile())) {
                properties.load(new InputStreamReader(config));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            try (InputStream config = properties.getClass().getResourceAsStream("/config.properties")) {
                // Don't fail if no config.properties found in classpath as we will fallback to env variables
                if (config != null) {
                    properties.load(new InputStreamReader(config));
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return new OktaAwsCliEnvironment(
                Boolean.valueOf(getEnvOrConfig(properties, "OKTA_BROWSER_AUTH")),
                getEnvOrConfig(properties, "OKTA_ORG"),
                getEnvOrConfig(properties, "OKTA_USERNAME"),
                getEnvOrConfig(properties, "OKTA_PASSWORD"),
                getProfile(profile, getEnvOrConfig(properties, "OKTA_PROFILE")),
                getEnvOrConfig(properties, "OKTA_AWS_APP_URL"),
                getEnvOrConfig(properties, "OKTA_AWS_ROLE_TO_ASSUME"),
                getStsDurationOrDefault(getEnvOrConfig(properties, "OKTA_STS_DURATION")),
                getAwsRegionOrDefault(getEnvOrConfig(properties, "OKTA_AWS_REGION")),
                getEnvOrConfig(properties, "OKTA_PROFILE_PREFIX"),
                getEnvOrConfig(properties, "OKTA_CREDENTIALS_SUFFIX")
        );
    }

    private static Optional<Path> getConfigFile() {
        Path configInWorkingDir = Paths.get(CONFIG_FILENAME);
        if (Files.isRegularFile(configInWorkingDir)) {
            return Optional.of(configInWorkingDir);
        }
        Path userHome = Paths.get(System.getProperty("user.home"));
        Path oktaDir = userHome.resolve(".okta");
        Path configInOktaDir = oktaDir.resolve(CONFIG_FILENAME);
        if (Files.isRegularFile(configInOktaDir)) {
            return Optional.of(configInOktaDir);
        }
        return Optional.empty();
    }

    private static String getEnvOrConfig(Properties properties, String propertyName) {
        String envValue = System.getenv(propertyName);
        return envValue != null ?
                envValue : properties.getProperty(propertyName);
    }

    private static String getProfile(String profileFromCmdLine, String profileFromEnvOrConfig) {
        return profileFromCmdLine != null ?
                profileFromCmdLine : profileFromEnvOrConfig;
    }

    private static Integer getStsDurationOrDefault(String stsDuration) {
        return (stsDuration == null) ? 3600 : Integer.parseInt(stsDuration);
    }

    private static String getAwsRegionOrDefault(String region) {
        return (region == null) ? "us-east-1" : region;
    }
}

@@ -18,16 +18,158 @@
 tools;%0A%0A
+import com.okta.tools.authentication.OktaAuthentication;%0Aimport org.apache.logging.log4j.LogManager;%0Aimport org.apache.logging.log4j.Logger;%0A%0A
 import j
@@ -451,16 +451,101 @@
 nfig %7B%0A%0A
+    private static final Logger logger = LogManager.getLogger(OktaAwsConfig.class);%0A%0A
     priv
@@ -980,24 +980,134 @@
 oFile())) %7B%0A
+                logger.debug(%22Reading config settings from file: %22 + path.get().toAbsolutePath().toString());%0A
             
@@ -1375,24 +1375,159 @@
 erties%22)) %7B%0A
+                if (config != null) %7B%0A                    properties.load(new InputStreamReader(config));%0A                %7D else %7B%0A    
             
@@ -1644,95 +1644,109 @@
     
-if (config != null) %7B%0A                    properties.load(new InputStreamReader(config)
+    logger.debug(%22No config.properties file found in working directory, ~/.okta, or the class loader%22
 );%0A 
@@ -3436,23 +3436,20 @@
         
-return 
+if (
 envValue
@@ -3448,34 +3448,35 @@
 envValue != null
- ?
+) %7B
 %0A               
@@ -3476,22 +3476,231 @@
     
-    envValue :
+logger.debug(%22Using %22 + propertyName + %22  value from the environment.%22);%0A            return envValue;%0A        %7D else %7B%0A            logger.debug(%22Using %22 + propertyName + %22  value from the config file.%22 );%0A            return
  pro
@@ -3730,24 +3730,34 @@
 pertyName);%0A
+        %7D%0A
     %7D%0A%0A    p

Add debug log events for config loader.