@@ -25,9 +25,12 @@ public BoxEnvironments(string fileName, IBoxHome home)
2525 _boxHome = home ;
2626 _boxHomeEnvironmentsFileName = fileName ;
2727 }
28- public bool VerifyBoxConfigFile ( string filePath )
28+ public bool VerifyBoxConfigFile ( string filePath , bool ignoreFilePathTranslation = false )
2929 {
30- filePath = GeneralUtilities . TranslatePath ( filePath ) ;
30+ if ( ! ignoreFilePathTranslation )
31+ {
32+ filePath = GeneralUtilities . TranslatePath ( filePath ) ;
33+ }
3134 Reporter . WriteInformation ( $ "Looking for file at this path { filePath } ") ;
3235 if ( File . Exists ( filePath ) )
3336 {
@@ -71,6 +74,7 @@ public bool VerifyBoxConfigFile(string filePath)
7174 }
7275 else
7376 {
77+ Reporter . WriteError ( $ "Couldn't open config file at this path: { filePath } ") ;
7478 return false ;
7579 }
7680 }
@@ -95,28 +99,35 @@ public BoxHomeConfigModel RevalidateExistingConfigFile(string filePath, string p
9599 {
96100 return TranslateConfigFileToEnvironment ( filePath , privateKeyPath ) ;
97101 }
98- public BoxHomeConfigModel TranslateConfigFileToEnvironment ( string filePath , string privateKeyPath = "" )
102+ public BoxHomeConfigModel TranslateConfigFileToEnvironment ( string filePath , string privateKeyPath = "" , bool ignoreFilePathTranslation = false )
99103 {
100- filePath = GeneralUtilities . TranslatePath ( filePath ) ;
104+ if ( ! ignoreFilePathTranslation )
105+ {
106+ filePath = GeneralUtilities . TranslatePath ( filePath ) ;
107+ }
101108 var translatedConfig = new BoxHomeConfigModel ( ) ;
102109 if ( File . Exists ( filePath ) )
103110 {
104111 var config = DeserializeBoxConfigFile ( filePath ) ;
105112 if ( ! string . IsNullOrEmpty ( privateKeyPath ) )
106113 {
107- var potentialPathFromOptions = GeneralUtilities . TranslatePath ( privateKeyPath ) ;
108- if ( File . Exists ( potentialPathFromOptions ) )
114+ if ( ! ignoreFilePathTranslation )
109115 {
110- translatedConfig . PrivateKeyPath = potentialPathFromOptions ;
116+ privateKeyPath = GeneralUtilities . TranslatePath ( privateKeyPath ) ;
117+ }
118+ if ( File . Exists ( privateKeyPath ) )
119+ {
120+ translatedConfig . PrivateKeyPath = privateKeyPath ;
111121 }
112122 else
113123 {
114- throw new Exception ( "Couldn't access the private key file from the path provided." ) ;
124+ throw new Exception ( $ "Couldn't access the private key file from the path provided: { privateKeyPath } .") ;
115125 }
116126 }
117127 else if ( ! string . IsNullOrEmpty ( config . AppSettings . AppAuth . PrivateKey ) )
118128 {
119129 Reporter . WriteInformation ( "Detected private key value in config..." ) ;
130+ Reporter . WriteInformation ( "Calculating between in-line private key or separate private key file..." ) ;
120131 var pattern = @"^-----BEGIN ENCRYPTED PRIVATE KEY-----\n" ;
121132 var regex = new Regex ( pattern ) ;
122133 if ( regex . IsMatch ( config . AppSettings . AppAuth . PrivateKey ) )
@@ -127,11 +138,19 @@ public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, stri
127138 else
128139 {
129140 Reporter . WriteInformation ( "Attempting to resolve file path for private key." ) ;
130- var potentialPath = GeneralUtilities . TranslateDependentPath ( config . AppSettings . AppAuth . PrivateKey , filePath ) ;
131- Reporter . WriteInformation ( $ "Found { potentialPath } .") ;
132- if ( File . Exists ( potentialPath ) )
141+ var privateKeyPathInline = config . AppSettings . AppAuth . PrivateKey ;
142+ if ( ! ignoreFilePathTranslation )
143+ {
144+ privateKeyPathInline = GeneralUtilities . TranslateDependentPath ( privateKeyPathInline , filePath ) ;
145+ }
146+ Reporter . WriteInformation ( $ "Path to private key file identified: { privateKeyPathInline } .") ;
147+ if ( File . Exists ( privateKeyPathInline ) )
133148 {
134- translatedConfig . PrivateKeyPath = potentialPath ;
149+ translatedConfig . PrivateKeyPath = privateKeyPathInline ;
150+ }
151+ else
152+ {
153+ throw new Exception ( $ "Unable to open private key file at { privateKeyPathInline } ") ;
135154 }
136155 }
137156 }
@@ -145,7 +164,7 @@ public BoxHomeConfigModel TranslateConfigFileToEnvironment(string filePath, stri
145164 }
146165 else
147166 {
148- Reporter . WriteError ( "Couldn't open file... " ) ;
167+ throw new Exception ( $ "Couldn't open config file at { filePath } ") ;
149168 }
150169 return translatedConfig ;
151170 }
@@ -303,7 +322,8 @@ public bool UpdatePrivateKeyPath(string existingName, string newPath)
303322 this . SerializeBoxEnvironmentFile ( environments ) ;
304323 return true ;
305324 }
306- public bool UpdateConfigFilePath ( string existingName , string newPath , string newPemPath = "" )
325+ public bool UpdateConfigFilePath ( string existingName , string newPath , string newPemPath = "" ,
326+ bool ignoreFilePathTranslation = false )
307327 {
308328 var environments = DeserializeBoxEnvironmentFile ( ) ;
309329 var foundEnv = new BoxHomeConfigModel ( ) ;
@@ -312,18 +332,24 @@ public bool UpdateConfigFilePath(string existingName, string newPath, string new
312332 {
313333 throw new Exception ( "Couldn't find that environment" ) ;
314334 }
315- var translatePath = GeneralUtilities . TranslatePath ( newPath ) ;
316- if ( this . VerifyBoxConfigFile ( translatePath ) )
335+ if ( ! ignoreFilePathTranslation )
336+ {
337+ newPath = GeneralUtilities . TranslatePath ( newPath ) ;
338+ }
339+ if ( this . VerifyBoxConfigFile ( newPath ) )
317340 {
318341 BoxHomeConfigModel env ;
319342 if ( ! string . IsNullOrEmpty ( newPemPath ) )
320343 {
321- var translatePemPath = GeneralUtilities . TranslatePath ( newPemPath ) ;
322- env = this . TranslateConfigFileToEnvironment ( translatePath , translatePemPath ) ;
344+ if ( ! ignoreFilePathTranslation )
345+ {
346+ newPemPath = GeneralUtilities . TranslatePath ( newPemPath ) ;
347+ }
348+ env = this . TranslateConfigFileToEnvironment ( newPath , newPemPath , ignoreFilePathTranslation : ignoreFilePathTranslation ) ;
323349 }
324350 else
325351 {
326- env = this . TranslateConfigFileToEnvironment ( translatePath ) ;
352+ env = this . TranslateConfigFileToEnvironment ( newPath , ignoreFilePathTranslation : ignoreFilePathTranslation ) ;
327353 }
328354 foundEnv . BoxConfigFilePath = env . BoxConfigFilePath ;
329355 foundEnv . ClientId = env . ClientId ;
@@ -500,7 +526,7 @@ public bool DeleteEnvironment(string name)
500526 throw new Exception ( "Couldn't find this environment." ) ;
501527 }
502528 }
503- public bool UpdateEnvironmentFilePath ( string path , string envName )
529+ public bool UpdateEnvironmentFilePath ( string path , string envName , bool ignoreFilePathTranslation = false )
504530 {
505531 var environments = DeserializeBoxEnvironmentFile ( ) ;
506532 var foundEnv = new BoxHomeConfigModel ( ) ;
@@ -511,7 +537,7 @@ public bool UpdateEnvironmentFilePath(string path, string envName)
511537 }
512538 if ( this . VerifyBoxConfigFile ( path ) )
513539 {
514- var newEnv = this . TranslateConfigFileToEnvironment ( path ) ;
540+ var newEnv = this . TranslateConfigFileToEnvironment ( path , ignoreFilePathTranslation : ignoreFilePathTranslation ) ;
515541 newEnv . AdminAsUserId = ( string . IsNullOrEmpty ( foundEnv . AdminAsUserId ) ) ? "" : foundEnv . AdminAsUserId ;
516542 newEnv . DefaultAsUserId = ( string . IsNullOrEmpty ( foundEnv . DefaultAsUserId ) ) ? "" : foundEnv . DefaultAsUserId ;
517543 newEnv . UseDefaultAsUser = foundEnv . UseDefaultAsUser ;
0 commit comments