@@ -8,6 +8,7 @@ import { resolveURL } from "./util";
8
8
import { TotpVaultClient } from "./engines/totp" ;
9
9
import { KVVaultClient } from "./engines/kv" ;
10
10
import { KV2VaultClient } from "./engines" ;
11
+ import { promises as fs } from "fs" ;
11
12
12
13
export type VaultHTTPMethods = "GET" | "POST" | "DELETE" | "LIST" ;
13
14
export interface HTTPGETParameters {
@@ -18,6 +19,7 @@ export interface IVaultConfig {
18
19
vaultAddress ?: string ;
19
20
vaultToken ?: string ;
20
21
vaultCaCertificate ?: string ;
22
+ vaultCaCertificatePath ?: string ;
21
23
vaultNamespace ?: string ;
22
24
apiVersion ?: string ;
23
25
}
@@ -133,6 +135,10 @@ export class Vault {
133
135
}
134
136
const uri = resolveURL ( this . config . vaultAddress ! , this . config . apiVersion ! , ...path ) ;
135
137
138
+ if ( this . config . vaultCaCertificatePath && ! this . config . vaultCaCertificate ) {
139
+ await this . loadCACert ( ) ;
140
+ }
141
+
136
142
const requestOptions : request . Options = {
137
143
method,
138
144
uri : uri . toString ( ) ,
@@ -149,18 +155,32 @@ export class Vault {
149
155
qs : parameters ,
150
156
} ;
151
157
152
- let res = await request ( requestOptions ) ;
158
+ let res ;
159
+ let retry = false ;
160
+ try {
161
+ res = await request ( requestOptions ) ;
162
+ } catch ( e ) {
163
+ if ( e . error && e . error . code === "CERT_SIGNATURE_FAILURE" && this . config . vaultCaCertificatePath ) {
164
+ await this . loadCACert ( ) ;
165
+ requestOptions . ca = this . config . vaultCaCertificate ;
166
+ retry = true ;
167
+ } else {
168
+ throw e ;
169
+ }
170
+ }
153
171
154
172
if ( this . tokenClient && options . retryWithTokenRenew && res . statusCode === 403 ) {
155
173
// token could be expired, try a new one
156
174
await this . tokenClient . login ( ) ;
157
- res = await request ( {
158
- ...requestOptions ,
159
- headers : {
160
- ...requestOptions . headers ,
161
- "X-Vault-Token" : this . token ,
162
- } ,
163
- } ) ;
175
+ requestOptions . headers = {
176
+ ...requestOptions . headers ,
177
+ "X-Vault-Token" : this . token ,
178
+ } ;
179
+ retry = true ;
180
+ }
181
+
182
+ if ( retry ) {
183
+ res = await request ( requestOptions ) ;
164
184
}
165
185
166
186
if ( ! options . acceptedReturnCodes ?. includes ( res . statusCode ) ) {
@@ -202,4 +222,11 @@ export class Vault {
202
222
203
223
return errors . some ( ( e ) => e . includes ( expectedMsg ) ) ;
204
224
}
225
+
226
+ private async loadCACert ( ) : Promise < void > {
227
+ if ( this . config . vaultCaCertificatePath ) {
228
+ const cert = await fs . readFile ( this . config . vaultCaCertificatePath , "utf8" ) ;
229
+ this . config . vaultCaCertificate = cert ;
230
+ }
231
+ }
205
232
}
0 commit comments