diff --git a/bellatrix.core/src/main/java/solutions/bellatrix/core/utilities/SecretsResolver.java b/bellatrix.core/src/main/java/solutions/bellatrix/core/utilities/SecretsResolver.java index eacb1ceb..b7dbceea 100644 --- a/bellatrix.core/src/main/java/solutions/bellatrix/core/utilities/SecretsResolver.java +++ b/bellatrix.core/src/main/java/solutions/bellatrix/core/utilities/SecretsResolver.java @@ -12,31 +12,32 @@ */ package solutions.bellatrix.core.utilities; +import java.util.HashMap; import java.util.function.Supplier; public class SecretsResolver { + private static final ThreadLocal> CACHED_SECRETS; + + static { + CACHED_SECRETS = ThreadLocal.withInitial(HashMap::new); + CACHED_SECRETS.set(new HashMap<>()); + } + public static String getSecret(Supplier getConfigValue) { - if (getConfigValue.get().contains("env_")) - { - String envName = getConfigValue.get().replace("{env_", "").replace("}", ""); - String environmentalVariable = System.getenv(envName); - return environmentalVariable; - } - else - { - return getConfigValue.get(); - } + return getSecret(getConfigValue.get()); } public static String getSecret(String configValue) { - if (configValue.contains("env_")) - { - String envName = configValue.replace("{env_", "").replace("}", ""); + if (CACHED_SECRETS.get().containsKey(configValue)) { + return CACHED_SECRETS.get().get(configValue); + } + + if (configValue.contains("env_")) { + var envName = configValue.replace("{env_", "").replace("}", "").toLowerCase(); String environmentalVariable = System.getenv(envName); + CACHED_SECRETS.get().put(configValue, environmentalVariable); return environmentalVariable; - } - else - { + } else { return configValue; } } diff --git a/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/DriverService.java b/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/DriverService.java index b5b8c812..b821309e 100644 --- a/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/DriverService.java +++ b/bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/DriverService.java @@ -31,6 +31,7 @@ import solutions.bellatrix.core.configuration.ConfigurationService; import solutions.bellatrix.core.utilities.DebugInformation; import solutions.bellatrix.core.utilities.Log; +import solutions.bellatrix.core.utilities.SecretsResolver; import solutions.bellatrix.core.utilities.TimestampBuilder; import solutions.bellatrix.web.configuration.GridSettings; import solutions.bellatrix.web.configuration.WebSettings; @@ -41,8 +42,12 @@ import java.net.URI; import java.net.URISyntaxException; import java.time.Duration; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import static io.restassured.RestAssured.given; @@ -52,7 +57,7 @@ public class DriverService { private static final ThreadLocal> CUSTOM_DRIVER_OPTIONS; private static final ThreadLocal WRAPPED_DRIVER; private static boolean isBuildNameSet = false; - private static String buildName; + private static String buildName; static { CUSTOM_DRIVER_OPTIONS = new ThreadLocal<>(); @@ -94,8 +99,7 @@ public static WebDriver start(BrowserConfiguration configuration) { var gridSettings = webSettings.getGridSettings().stream().filter(g -> g.getProviderName().equals(executionType.toLowerCase())).findFirst(); assert gridSettings.isPresent() : String.format("The specified execution type '%s' is not declared in the configuration", executionType); driver = initializeDriverGridMode(gridSettings.get()); - } - else { + } else { var gridSettings = webSettings.getGridSettings().stream().filter(g -> g.getProviderName().equals(executionType.toLowerCase())).findFirst(); assert gridSettings.isPresent() : String.format("The specified execution type '%s' is not declared in the configuration", executionType); driver = initializeDriverCloudGridMode(gridSettings.get()); @@ -153,8 +157,10 @@ private static WebDriver initializeDriverCloudGridMode(GridSettings gridSettings caps.setCapability(gridSettings.getOptionsName(), options); WebDriver driver = null; try { - driver = new RemoteWebDriver(new URI(gridSettings.getUrl()).toURL(), caps); + var url = getUrl(gridSettings.getUrl()); + driver = new RemoteWebDriver(new URI(url).toURL(), caps); } catch (Exception e) { + ; DebugInformation.printStackTrace(e); } @@ -211,11 +217,9 @@ private static WebDriver initializeDriverGridMode(GridSettings gridSettings) { WebDriver driver = null; try { var gridUrl = gridSettings.getUrl(); - if (gridUrl.startsWith("env_")) { - gridUrl = System.getProperty(gridSettings.getUrl()).replace("env_", ""); - } + var url = getUrl(gridUrl); - driver = new RemoteWebDriver(new URI(gridUrl).toURL(), caps); + driver = new RemoteWebDriver(new URI(url).toURL(), caps); } catch (MalformedURLException | URISyntaxException e) { DebugInformation.printStackTrace(e); } @@ -236,10 +240,10 @@ private static WebDriver initializeDriverRegularMode() { switch (BROWSER_CONFIGURATION.get().getBrowser()) { case CHROME -> { - WebDriverManager.chromedriver().setup(); + //WebDriverManager.chromedriver().setup(); var chromeOptions = new ChromeOptions(); addDriverOptions(chromeOptions); - chromeOptions.addArguments("--log-level=3","--remote-allow-origins=*"); + chromeOptions.addArguments("--log-level=3", "--remote-allow-origins=*"); chromeOptions.setAcceptInsecureCerts(true); System.setProperty("webdriver.chrome.silentOutput", "true"); if (shouldCaptureHttpTraffic) chromeOptions.setProxy(proxyConfig); @@ -316,10 +320,9 @@ private static void addGridOptions(HashMap options.put(c.getKey(), buildName); Log.info(c.getKey() + " = " + buildName); - } - else { - if (c.getValue() instanceof String && c.getValue().toString().startsWith("env_")) { - var envValue = System.getProperty(c.getValue().toString().replace("env_", "")); + } else { + if (c.getValue() instanceof String && c.getValue().toString().startsWith("{env_")) { + var envValue = SecretsResolver.getSecret(c.getValue().toString()); options.put(c.getKey(), envValue); Log.info(c.getKey() + " = " + envValue); } else { @@ -333,9 +336,12 @@ private static void addGridOptions(HashMap options.put("lambdaMaskCommands", new String[]{"setValues", "setCookies", "getCookies"}); try { - var userInfo = new URI(gridSettings.getUrl()).getUserInfo().split(":"); + var usernameSecret = gridSettings.getArguments().get(0).get("username").toString(); + var accessKeySecret = gridSettings.getArguments().get(0).get("accessKey").toString(); + var usernameValue = SecretsResolver.getSecret(usernameSecret); + var accessKeyValue = SecretsResolver.getSecret(accessKeySecret); - var res = given().auth().preemptive().basic(userInfo[0], userInfo[1]) + var res = given().auth().preemptive().basic(usernameValue, accessKeyValue) .get("https://api.lambdatest.com/automation/api/v1/user-files"); options.put("lambda:userFiles", res.body().jsonPath().getList("data.key")); @@ -353,8 +359,8 @@ private static void addGridOptions(HashMap private static void addGridOptions(TOption options, GridSettings gridSettings) { for (var entry : gridSettings.getArguments()) { for (var c : entry.entrySet()) { - if (c.getValue() instanceof String && c.getValue().toString().startsWith("env_")) { - var envValue = System.getProperty(c.getValue().toString().replace("env_", "")); + if (c.getValue() instanceof String && c.getValue().toString().startsWith("{env_")) { + var envValue = SecretsResolver.getSecret(c.getValue().toString()); options.setCapability(c.getKey(), envValue); } else { options.setCapability(c.getKey(), c.getValue()); @@ -408,6 +414,28 @@ private static String getBuildName() { return buildName; } + private static String getUrl(String url) { + String result = url; + if (url.startsWith("{env_")) { + result = SecretsResolver.getSecret(url); + } else if (url.contains("{env_")) { + String pattern = "\\{env_.*?\\}"; + Pattern compiledPattern = Pattern.compile(pattern); + Matcher matcher = compiledPattern.matcher(url); + List allMatches = new ArrayList(); + + while (matcher.find()) { + allMatches.add(matcher.group()); + } + + for (String match : allMatches) { + result = result.replace(match, SecretsResolver.getSecret(match)); + } + } + + return result; + } + public static void close() { if (DISPOSED.get()) { return;