@@ -10,7 +10,6 @@ import * as nodesUtils from 'polykey/dist/nodes/utils';
10
10
11
11
const vaultNameRegex = / ^ (? ! .* [: ] ) [ - ~ \t \n ] * $ / s;
12
12
const secretPathRegex = / ^ (? ! .* [ = ] ) [ - ~ \t \n ] * $ / s;
13
- const vaultNameSecretPathRegex = / ^ ( [ \w \- \. ] + ) (?: : ( [ ^ \0 \\ = ] + ) ) ? $ / ;
14
13
const secretPathValueRegex = / ^ ( [ a - z A - Z _ ] [ \w ] + ) ? $ / ;
15
14
const environmentVariableRegex = / ^ ( [ a - z A - Z _ ] + [ a - z A - Z 0 - 9 _ ] * ) ? $ / ;
16
15
@@ -76,52 +75,54 @@ function parseVaultName(vaultName: string): string {
76
75
return vaultName ;
77
76
}
78
77
79
- // E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
80
- // If 'vault1', ['vault1, undefined] is returned
78
+ // E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c', undefined] is returned
79
+ // If 'vault1', ['vault1', undefined, undefined] is returned
80
+ // If 'vault1:abc=xyz', ['vault1', 'abc', 'xyz'] is returned
81
81
// If 'vault1:', an error is thrown
82
82
// If 'a/b/c', an error is thrown
83
83
// Splits out everything after an `=` separator
84
- function parseSecretPath ( secretPath : string ) : [ string , string ?, string ?] {
84
+ function parseSecretPath ( inputPath : string ) : [ string , string ?, string ?] {
85
85
// The colon character `:` is prohibited in vaultName, so it's first occurence
86
86
// means that this is the delimiter between vaultName and secretPath.
87
- const colonIndex = secretPath . indexOf ( ':' ) ;
87
+ const colonIndex = inputPath . indexOf ( ':' ) ;
88
88
// If no colon exists, treat entire string as vault name
89
89
if ( colonIndex === - 1 ) {
90
- return [ parseVaultName ( secretPath ) , undefined , undefined ] ;
90
+ return [ parseVaultName ( inputPath ) , undefined , undefined ] ;
91
91
}
92
- // Calculate contents before the `=` separator
93
- const vaultNamePart = secretPath . substring ( 0 , colonIndex ) ;
94
- const secretPathPart = secretPath . substring ( colonIndex + 1 ) ;
95
- // Calculate contents after the `=` separator
92
+ // Calculate vaultName and secretPath
93
+ const vaultNamePart = inputPath . substring ( 0 , colonIndex ) ;
94
+ const secretPathPart = inputPath . substring ( colonIndex + 1 ) ;
95
+ // Calculate contents after the `=` separator (value)
96
96
const equalIndex = secretPathPart . indexOf ( '=' ) ;
97
- const splitSecretPath =
97
+ // If `=` isn't found, then the entire secret path section is the actual path.
98
+ // Otherwise, split the path section by the index of `=`. First half is the
99
+ // actual secret part, and the second half is the value.
100
+ const secretPath =
98
101
equalIndex === - 1
99
102
? secretPathPart
100
103
: secretPathPart . substring ( 0 , equalIndex ) ;
101
104
const value =
102
- equalIndex === - 1 ? undefined : secretPathPart . substring ( equalIndex + 1 ) ;
103
- if ( splitSecretPath != null && ! secretPathRegex . test ( splitSecretPath ) ) {
105
+ equalIndex !== - 1 ? secretPathPart . substring ( equalIndex + 1 ) : undefined ;
106
+ // If secretPath exists but it doesn't pass the regex test, then the path is
107
+ // malformed.
108
+ if ( secretPath != null && ! secretPathRegex . test ( secretPath ) ) {
104
109
throw new commander . InvalidArgumentError (
105
- `${ secretPath } is not of the format <vaultName>[:<secretPath>][=<value>]` ,
110
+ `${ inputPath } is not of the format <vaultName>[:<secretPath>][=<value>]` ,
106
111
) ;
107
112
}
108
- const parsedVaultName = parseVaultName ( vaultNamePart ) ;
109
- const parsedSecretPath = splitSecretPath . match ( secretPathRegex ) ?. [ 0 ] ;
110
- return [ parsedVaultName , parsedSecretPath , value ] ;
113
+ // We have already tested if the secretPath is valid, and we can return it
114
+ // as-is.
115
+ const vaultName = parseVaultName ( vaultNamePart ) ;
116
+ return [ vaultName , secretPath , value ] ;
111
117
}
112
118
113
- function parseSecretPathValue ( secretPath : string ) : [ string , string , string ?] {
119
+ function parseSecretPathValue ( secretPath : string ) : [ string , string ? , string ?] {
114
120
const [ vaultName , directoryPath , value ] = parseSecretPath ( secretPath ) ;
115
121
if ( value != null && ! secretPathValueRegex . test ( value ) ) {
116
122
throw new commander . InvalidArgumentError (
117
123
`${ value } is not a valid value name` ,
118
124
) ;
119
125
}
120
- if ( directoryPath == null ) {
121
- throw new commander . InvalidArgumentError (
122
- `${ secretPath } is not of the format <vaultName>:<directoryPath>[=<value>]` ,
123
- ) ;
124
- }
125
126
return [ vaultName , directoryPath , value ] ;
126
127
}
127
128
0 commit comments