Skip to content

Commit 097b394

Browse files
Merge pull request #34 from AutomateThePlanet/feature-update-driver-service
updated DriverService to use SecretsResolver for environment variables
2 parents d585e9c + a901b30 commit 097b394

File tree

2 files changed

+63
-34
lines changed

2 files changed

+63
-34
lines changed

bellatrix.core/src/main/java/solutions/bellatrix/core/utilities/SecretsResolver.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,32 @@
1212
*/
1313
package solutions.bellatrix.core.utilities;
1414

15+
import java.util.HashMap;
1516
import java.util.function.Supplier;
1617

1718
public class SecretsResolver {
19+
private static final ThreadLocal<HashMap<String, String>> CACHED_SECRETS;
20+
21+
static {
22+
CACHED_SECRETS = ThreadLocal.withInitial(HashMap::new);
23+
CACHED_SECRETS.set(new HashMap<>());
24+
}
25+
1826
public static String getSecret(Supplier<String> getConfigValue) {
19-
if (getConfigValue.get().contains("env_"))
20-
{
21-
String envName = getConfigValue.get().replace("{env_", "").replace("}", "");
22-
String environmentalVariable = System.getenv(envName);
23-
return environmentalVariable;
24-
}
25-
else
26-
{
27-
return getConfigValue.get();
28-
}
27+
return getSecret(getConfigValue.get());
2928
}
3029

3130
public static String getSecret(String configValue) {
32-
if (configValue.contains("env_"))
33-
{
34-
String envName = configValue.replace("{env_", "").replace("}", "");
31+
if (CACHED_SECRETS.get().containsKey(configValue)) {
32+
return CACHED_SECRETS.get().get(configValue);
33+
}
34+
35+
if (configValue.contains("env_")) {
36+
var envName = configValue.replace("{env_", "").replace("}", "").toLowerCase();
3537
String environmentalVariable = System.getenv(envName);
38+
CACHED_SECRETS.get().put(configValue, environmentalVariable);
3639
return environmentalVariable;
37-
}
38-
else
39-
{
40+
} else {
4041
return configValue;
4142
}
4243
}

bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/DriverService.java

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import solutions.bellatrix.core.configuration.ConfigurationService;
3232
import solutions.bellatrix.core.utilities.DebugInformation;
3333
import solutions.bellatrix.core.utilities.Log;
34+
import solutions.bellatrix.core.utilities.SecretsResolver;
3435
import solutions.bellatrix.core.utilities.TimestampBuilder;
3536
import solutions.bellatrix.web.configuration.GridSettings;
3637
import solutions.bellatrix.web.configuration.WebSettings;
@@ -41,8 +42,12 @@
4142
import java.net.URI;
4243
import java.net.URISyntaxException;
4344
import java.time.Duration;
45+
import java.util.ArrayList;
4446
import java.util.HashMap;
47+
import java.util.List;
4548
import java.util.Properties;
49+
import java.util.regex.Matcher;
50+
import java.util.regex.Pattern;
4651

4752
import static io.restassured.RestAssured.given;
4853

@@ -52,7 +57,7 @@ public class DriverService {
5257
private static final ThreadLocal<HashMap<String, String>> CUSTOM_DRIVER_OPTIONS;
5358
private static final ThreadLocal<WebDriver> WRAPPED_DRIVER;
5459
private static boolean isBuildNameSet = false;
55-
private static String buildName;
60+
private static String buildName;
5661

5762
static {
5863
CUSTOM_DRIVER_OPTIONS = new ThreadLocal<>();
@@ -94,8 +99,7 @@ public static WebDriver start(BrowserConfiguration configuration) {
9499
var gridSettings = webSettings.getGridSettings().stream().filter(g -> g.getProviderName().equals(executionType.toLowerCase())).findFirst();
95100
assert gridSettings.isPresent() : String.format("The specified execution type '%s' is not declared in the configuration", executionType);
96101
driver = initializeDriverGridMode(gridSettings.get());
97-
}
98-
else {
102+
} else {
99103
var gridSettings = webSettings.getGridSettings().stream().filter(g -> g.getProviderName().equals(executionType.toLowerCase())).findFirst();
100104
assert gridSettings.isPresent() : String.format("The specified execution type '%s' is not declared in the configuration", executionType);
101105
driver = initializeDriverCloudGridMode(gridSettings.get());
@@ -153,8 +157,10 @@ private static WebDriver initializeDriverCloudGridMode(GridSettings gridSettings
153157
caps.setCapability(gridSettings.getOptionsName(), options);
154158
WebDriver driver = null;
155159
try {
156-
driver = new RemoteWebDriver(new URI(gridSettings.getUrl()).toURL(), caps);
160+
var url = getUrl(gridSettings.getUrl());
161+
driver = new RemoteWebDriver(new URI(url).toURL(), caps);
157162
} catch (Exception e) {
163+
;
158164
DebugInformation.printStackTrace(e);
159165
}
160166

@@ -211,11 +217,9 @@ private static WebDriver initializeDriverGridMode(GridSettings gridSettings) {
211217
WebDriver driver = null;
212218
try {
213219
var gridUrl = gridSettings.getUrl();
214-
if (gridUrl.startsWith("env_")) {
215-
gridUrl = System.getProperty(gridSettings.getUrl()).replace("env_", "");
216-
}
220+
var url = getUrl(gridUrl);
217221

218-
driver = new RemoteWebDriver(new URI(gridUrl).toURL(), caps);
222+
driver = new RemoteWebDriver(new URI(url).toURL(), caps);
219223
} catch (MalformedURLException | URISyntaxException e) {
220224
DebugInformation.printStackTrace(e);
221225
}
@@ -236,10 +240,10 @@ private static WebDriver initializeDriverRegularMode() {
236240

237241
switch (BROWSER_CONFIGURATION.get().getBrowser()) {
238242
case CHROME -> {
239-
WebDriverManager.chromedriver().setup();
243+
//WebDriverManager.chromedriver().setup();
240244
var chromeOptions = new ChromeOptions();
241245
addDriverOptions(chromeOptions);
242-
chromeOptions.addArguments("--log-level=3","--remote-allow-origins=*");
246+
chromeOptions.addArguments("--log-level=3", "--remote-allow-origins=*");
243247
chromeOptions.setAcceptInsecureCerts(true);
244248
System.setProperty("webdriver.chrome.silentOutput", "true");
245249
if (shouldCaptureHttpTraffic) chromeOptions.setProxy(proxyConfig);
@@ -316,10 +320,9 @@ private static <TOption extends MutableCapabilities> void addGridOptions(HashMap
316320

317321
options.put(c.getKey(), buildName);
318322
Log.info(c.getKey() + " = " + buildName);
319-
}
320-
else {
321-
if (c.getValue() instanceof String && c.getValue().toString().startsWith("env_")) {
322-
var envValue = System.getProperty(c.getValue().toString().replace("env_", ""));
323+
} else {
324+
if (c.getValue() instanceof String && c.getValue().toString().startsWith("{env_")) {
325+
var envValue = SecretsResolver.getSecret(c.getValue().toString());
323326
options.put(c.getKey(), envValue);
324327
Log.info(c.getKey() + " = " + envValue);
325328
} else {
@@ -333,9 +336,12 @@ private static <TOption extends MutableCapabilities> void addGridOptions(HashMap
333336
options.put("lambdaMaskCommands", new String[]{"setValues", "setCookies", "getCookies"});
334337

335338
try {
336-
var userInfo = new URI(gridSettings.getUrl()).getUserInfo().split(":");
339+
var usernameSecret = gridSettings.getArguments().get(0).get("username").toString();
340+
var accessKeySecret = gridSettings.getArguments().get(0).get("accessKey").toString();
341+
var usernameValue = SecretsResolver.getSecret(usernameSecret);
342+
var accessKeyValue = SecretsResolver.getSecret(accessKeySecret);
337343

338-
var res = given().auth().preemptive().basic(userInfo[0], userInfo[1])
344+
var res = given().auth().preemptive().basic(usernameValue, accessKeyValue)
339345
.get("https://api.lambdatest.com/automation/api/v1/user-files");
340346

341347
options.put("lambda:userFiles", res.body().jsonPath().getList("data.key"));
@@ -353,8 +359,8 @@ private static <TOption extends MutableCapabilities> void addGridOptions(HashMap
353359
private static <TOption extends MutableCapabilities> void addGridOptions(TOption options, GridSettings gridSettings) {
354360
for (var entry : gridSettings.getArguments()) {
355361
for (var c : entry.entrySet()) {
356-
if (c.getValue() instanceof String && c.getValue().toString().startsWith("env_")) {
357-
var envValue = System.getProperty(c.getValue().toString().replace("env_", ""));
362+
if (c.getValue() instanceof String && c.getValue().toString().startsWith("{env_")) {
363+
var envValue = SecretsResolver.getSecret(c.getValue().toString());
358364
options.setCapability(c.getKey(), envValue);
359365
} else {
360366
options.setCapability(c.getKey(), c.getValue());
@@ -408,6 +414,28 @@ private static String getBuildName() {
408414
return buildName;
409415
}
410416

417+
private static String getUrl(String url) {
418+
String result = url;
419+
if (url.startsWith("{env_")) {
420+
result = SecretsResolver.getSecret(url);
421+
} else if (url.contains("{env_")) {
422+
String pattern = "\\{env_.*?\\}";
423+
Pattern compiledPattern = Pattern.compile(pattern);
424+
Matcher matcher = compiledPattern.matcher(url);
425+
List<String> allMatches = new ArrayList<String>();
426+
427+
while (matcher.find()) {
428+
allMatches.add(matcher.group());
429+
}
430+
431+
for (String match : allMatches) {
432+
result = result.replace(match, SecretsResolver.getSecret(match));
433+
}
434+
}
435+
436+
return result;
437+
}
438+
411439
public static void close() {
412440
if (DISPOSED.get()) {
413441
return;

0 commit comments

Comments
 (0)