Skip to content

Commit 5185cfa

Browse files
Merge pull request #32 from mittwald/fix/ca-cert-reload
Load new CA Certificate from file on certificate error
2 parents 1366ab7 + 11d53de commit 5185cfa

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ const client = new Vault({
3535
### Kubernetes In-Cluster Example
3636

3737
```js
38-
const cert = await fs.readFile("../vault-cacert", "utf8");
3938
const client = new Vault({
4039
vaultAddress: "https://vault:8200",
4140
vaultCaCertificate: cert,
41+
vaultCaCertificatePath: "../vault-cacert",
4242
});
4343

4444
const k8sauth = client.KubernetesAuth({

src/Vault.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { resolveURL } from "./util";
88
import { TotpVaultClient } from "./engines/totp";
99
import { KVVaultClient } from "./engines/kv";
1010
import { KV2VaultClient } from "./engines";
11+
import { promises as fs } from "fs";
1112

1213
export type VaultHTTPMethods = "GET" | "POST" | "DELETE" | "LIST";
1314
export interface HTTPGETParameters {
@@ -18,6 +19,7 @@ export interface IVaultConfig {
1819
vaultAddress?: string;
1920
vaultToken?: string;
2021
vaultCaCertificate?: string;
22+
vaultCaCertificatePath?: string;
2123
vaultNamespace?: string;
2224
apiVersion?: string;
2325
}
@@ -133,6 +135,10 @@ export class Vault {
133135
}
134136
const uri = resolveURL(this.config.vaultAddress!, this.config.apiVersion!, ...path);
135137

138+
if (this.config.vaultCaCertificatePath && !this.config.vaultCaCertificate) {
139+
await this.loadCACert();
140+
}
141+
136142
const requestOptions: request.Options = {
137143
method,
138144
uri: uri.toString(),
@@ -149,18 +155,32 @@ export class Vault {
149155
qs: parameters,
150156
};
151157

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+
}
153171

154172
if (this.tokenClient && options.retryWithTokenRenew && res.statusCode === 403) {
155173
// token could be expired, try a new one
156174
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);
164184
}
165185

166186
if (!options.acceptedReturnCodes?.includes(res.statusCode)) {
@@ -202,4 +222,11 @@ export class Vault {
202222

203223
return errors.some((e) => e.includes(expectedMsg));
204224
}
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+
}
205232
}

tests/engines/transit.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ describe("Transit Vault Client", () => {
117117
expect(dec).toEqual("hello");
118118
});
119119

120-
test("should respond with 500 if the keyID for decryption is invalid", async () => {
120+
test("should respond with 400 if the keyID for decryption is invalid", async () => {
121121
const encrypted = await client.encryptText("500test", "plainText");
122122
const invalidKeyID = "invalid";
123123
await client.create(invalidKeyID);
124124
try {
125125
await client.decryptText(invalidKeyID, encrypted);
126126
} catch (err) {
127-
expect(err.response.statusCode).toEqual(500);
127+
expect(err.response.statusCode).toEqual(400);
128128
}
129129
});
130130

0 commit comments

Comments
 (0)