@@ -8,7 +8,9 @@ import * as gestaltsUtils from 'polykey/dist/gestalts/utils';
88import * as networkUtils from 'polykey/dist/network/utils' ;
99import * as nodesUtils from 'polykey/dist/nodes/utils' ;
1010
11- const secretPathRegex = / ^ ( [ \w - ] + ) (?: : ( [ ^ \0 \\ = ] + ) ) ? $ / ;
11+ const vaultNameRegex = / ^ (? ! .* [: ] ) [ - ~ \t \n ] * $ / s;
12+ const secretPathRegex = / ^ (? ! .* [ = ] ) [ - ~ \t \n ] * $ / s;
13+ const vaultNameSecretPathRegex = / ^ ( [ \w - ] + ) (?: : ( [ ^ \0 \\ = ] + ) ) ? $ / ;
1214const secretPathValueRegex = / ^ ( [ a - z A - Z _ ] [ \w ] + ) ? $ / ;
1315const environmentVariableRegex = / ^ ( [ a - z A - Z _ ] + [ a - z A - Z 0 - 9 _ ] * ) ? $ / ;
1416
@@ -80,15 +82,26 @@ function parseSecretPathOptional(
8082 lastEqualIndex === - 1
8183 ? undefined
8284 : secretPath . substring ( lastEqualIndex + 1 ) ;
83- if ( ! secretPathRegex . test ( splitSecretPath ) ) {
85+ if ( ! vaultNameSecretPathRegex . test ( splitSecretPath ) ) {
8486 throw new commander . InvalidArgumentError (
8587 `${ secretPath } is not of the format <vaultName>[:<directoryPath>][=<value>]` ,
8688 ) ;
8789 }
88- const [ , vaultName , directoryPath ] = splitSecretPath . match ( secretPathRegex ) ! ;
90+ const [ , vaultName , directoryPath ] = splitSecretPath . match (
91+ vaultNameSecretPathRegex ,
92+ ) ! ;
8993 return [ vaultName , directoryPath , value ] ;
9094}
9195
96+ function parseVaultName ( vaultName : string ) : string {
97+ if ( ! vaultNameRegex . test ( vaultName ) ) {
98+ throw new commander . InvalidArgumentError (
99+ `${ vaultName } is not a valid vault name` ,
100+ ) ;
101+ }
102+ return vaultName ;
103+ }
104+
92105function parseSecretPath ( secretPath : string ) : [ string , string , string ?] {
93106 // E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
94107 // If 'vault1', an error is thrown
@@ -111,8 +124,40 @@ function parseSecretPathValue(secretPath: string): [string, string, string?] {
111124 return [ vaultName , directoryPath , value ] ;
112125}
113126
114- function parseSecretPathEnv ( secretPath : string ) : [ string , string , string ?] {
115- const [ vaultName , directoryPath , value ] = parseSecretPath ( secretPath ) ;
127+ function parseSecretPathEnv ( secretPath : string ) : [ string , string ?, string ?] {
128+ // The colon character `:` is prohibited in vaultName, so it's first occurence
129+ // means that this is the delimiter between vaultName and secretPath.
130+ const colonIndex = secretPath . indexOf ( ':' ) ;
131+ // If no colon exists, treat entire string as vault name
132+ if ( colonIndex === - 1 ) {
133+ return [ parseVaultName ( secretPath ) , undefined , undefined ] ;
134+ }
135+ // Calculate contents before the `=` separator
136+ const vaultNamePart = secretPath . substring ( 0 , colonIndex ) ;
137+ const secretPathPart = secretPath . substring ( colonIndex + 1 ) ;
138+ // Calculate contents after the `=` separator
139+ const equalIndex = secretPathPart . indexOf ( '=' ) ;
140+ const splitSecretPath =
141+ equalIndex === - 1
142+ ? secretPathPart
143+ : secretPathPart . substring ( 0 , equalIndex ) ;
144+ const valueData =
145+ equalIndex === - 1 ? undefined : secretPathPart . substring ( equalIndex + 1 ) ;
146+ if ( splitSecretPath != null && ! secretPathRegex . test ( splitSecretPath ) ) {
147+ throw new commander . InvalidArgumentError (
148+ `${ secretPath } is not of the format <vaultName>[:<secretPath>][=<value>]` ,
149+ ) ;
150+ }
151+ const parsedVaultName = parseVaultName ( vaultNamePart ) ;
152+ const parsedSecretPath = splitSecretPath . match ( secretPathRegex ) ?. [ 0 ] ;
153+ const [ vaultName , directoryPath , value ] = [
154+ parsedVaultName ,
155+ parsedSecretPath ,
156+ valueData ,
157+ ] ;
158+ console . error ( 'vaultName' , vaultName )
159+ console . error ( 'directoryPath' , directoryPath )
160+ console . error ( 'value' , value )
116161 if ( value != null && ! environmentVariableRegex . test ( value ) ) {
117162 throw new commander . InvalidArgumentError (
118163 `${ value } is not a valid environment variable name` ,
@@ -189,20 +234,26 @@ function parseEnvArgs(
189234 value : string ,
190235 prev : [ Array < [ string , string , string ?] > , Array < string > ] | undefined ,
191236) : [ Array < [ string , string , string ?] > , Array < string > ] {
237+ console . error ( 'original value' , value )
192238 const current : [ Array < [ string , string , string ?] > , Array < string > ] = prev ?? [
193239 [ ] ,
194240 [ ] ,
195241 ] ;
196242 if ( current [ 1 ] . length === 0 ) {
197243 // Parse a secret path
198244 try {
199- current [ 0 ] . push ( parseSecretPathEnv ( value ) ) ;
245+ const [ vaultName , secretPath , valueData ] = parseSecretPathEnv ( value ) ;
246+ const parsedSecretPath = secretPath == null ? '/' : secretPath ;
247+ console . error ( `parsed. [${ vaultName } , ${ parsedSecretPath } , ${ valueData } ]` )
248+ current [ 0 ] . push ( [ vaultName , parsedSecretPath , valueData ] ) ;
200249 } catch ( e ) {
201250 if ( ! ( e instanceof commander . InvalidArgumentError ) ) throw e ;
251+ console . error ( 'errored. value' , value )
202252 // If we get an invalid argument error then we switch over to parsing args verbatim
203253 current [ 1 ] . push ( value ) ;
204254 }
205255 } else {
256+ console . error ( 'added secret path. value' , value )
206257 // Otherwise we just have the cmd args
207258 current [ 1 ] . push ( value ) ;
208259 }
@@ -215,13 +266,15 @@ function parseEnvArgs(
215266}
216267
217268export {
269+ vaultNameRegex ,
218270 secretPathRegex ,
219271 secretPathValueRegex ,
220272 environmentVariableRegex ,
221273 validateParserToArgParser ,
222274 validateParserToArgListParser ,
223275 parseCoreCount ,
224276 parseSecretPathOptional ,
277+ parseVaultName ,
225278 parseSecretPath ,
226279 parseSecretPathValue ,
227280 parseSecretPathEnv ,
0 commit comments