3131import solutions .bellatrix .core .configuration .ConfigurationService ;
3232import solutions .bellatrix .core .utilities .DebugInformation ;
3333import solutions .bellatrix .core .utilities .Log ;
34+ import solutions .bellatrix .core .utilities .SecretsResolver ;
3435import solutions .bellatrix .core .utilities .TimestampBuilder ;
3536import solutions .bellatrix .web .configuration .GridSettings ;
3637import solutions .bellatrix .web .configuration .WebSettings ;
4142import java .net .URI ;
4243import java .net .URISyntaxException ;
4344import java .time .Duration ;
45+ import java .util .ArrayList ;
4446import java .util .HashMap ;
47+ import java .util .List ;
4548import java .util .Properties ;
49+ import java .util .regex .Matcher ;
50+ import java .util .regex .Pattern ;
4651
4752import 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