Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<String, String>> CACHED_SECRETS;

static {
CACHED_SECRETS = ThreadLocal.withInitial(HashMap::new);
CACHED_SECRETS.set(new HashMap<>());
}

public static String getSecret(Supplier<String> 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -52,7 +57,7 @@ public class DriverService {
private static final ThreadLocal<HashMap<String, String>> CUSTOM_DRIVER_OPTIONS;
private static final ThreadLocal<WebDriver> WRAPPED_DRIVER;
private static boolean isBuildNameSet = false;
private static String buildName;
private static String buildName;

static {
CUSTOM_DRIVER_OPTIONS = new ThreadLocal<>();
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -316,10 +320,9 @@ private static <TOption extends MutableCapabilities> 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 {
Expand All @@ -333,9 +336,12 @@ private static <TOption extends MutableCapabilities> 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"));
Expand All @@ -353,8 +359,8 @@ private static <TOption extends MutableCapabilities> void addGridOptions(HashMap
private static <TOption extends MutableCapabilities> 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());
Expand Down Expand Up @@ -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<String> allMatches = new ArrayList<String>();

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;
Expand Down