Skip to content

Commit 01ee528

Browse files
committed
Expose the certificate content from the API
1 parent e1d4936 commit 01ee528

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

src/config.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { HttpsPathOptions } from "mockttp/dist/util/tls";
2-
31
export interface HtkConfig {
42
configPath: string;
5-
https: HttpsPathOptions
3+
https: {
4+
keyPath: string;
5+
certPath: string;
6+
certContent: string;
7+
keyLength: number;
8+
}
69
}

src/httptoolkit-server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const typeDefs = `
3535
3636
type InterceptionConfig {
3737
certificatePath: String!
38+
certificateContent: String!
3839
}
3940
4041
type Interceptor {
@@ -60,7 +61,8 @@ const buildResolvers = (
6061
version: () => packageJson.version,
6162
interceptors: () => _.values(interceptors),
6263
config: () => ({
63-
certificatePath: config.https.certPath
64+
certificatePath: config.https.certPath,
65+
certificateContent: config.https.certContent
6466
}),
6567
networkInterfaces: () => os.networkInterfaces()
6668
},

src/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ async function generateHTTPSConfig(configPath: string) {
2626
const keyPath = path.join(configPath, 'ca.key');
2727
const certPath = path.join(configPath, 'ca.pem');
2828

29-
await Promise.all([
29+
const [ certContent ] = await Promise.all([
30+
readFile(certPath, 'utf8').then((certContent) => {
31+
checkCertExpiry(certContent);
32+
return certContent;
33+
}),
3034
canAccess(keyPath, fs.constants.R_OK),
31-
readFile(certPath, 'utf8').then(checkCertExpiry)
3235
]).catch(async () => {
3336
// Cert doesn't exist, or is too close/past expiry. Generate a new one:
3437

@@ -37,14 +40,15 @@ async function generateHTTPSConfig(configPath: string) {
3740
});
3841

3942
return Promise.all([
40-
writeFile(keyPath, newCertPair.key),
41-
writeFile(certPath, newCertPair.cert)
43+
writeFile(certPath, newCertPair.cert).then(() => newCertPair.cert),
44+
writeFile(keyPath, newCertPair.key)
4245
]);
4346
});
4447

4548
return {
4649
keyPath,
4750
certPath,
51+
certContent,
4852
keyLength: 2048 // Reasonably secure keys please
4953
};
5054
}

test/interceptors/interceptor-test-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const getCertificateDetails = _.memoize(async (configPath: string) => {
1818
fs.writeFileSync(keyPath, newCertPair.key);
1919
fs.writeFileSync(certPath, newCertPair.cert);
2020

21-
return { certPath, keyPath };
21+
return { certPath, keyPath, certContent: newCertPair.cert, keyLength: 2048};
2222
});
2323

2424
type InterceptorSetup = Promise<{

0 commit comments

Comments
 (0)