-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathCertificateStore.ts
131 lines (110 loc) · 3.52 KB
/
CertificateStore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'path';
import { homedir } from 'os';
import { FileSystem } from '@rushstack/node-core-library';
/**
* Store to retrieve and save debug certificate data.
* @public
*/
export class CertificateStore {
private readonly _caCertificatePath: string;
private readonly _certificatePath: string;
private readonly _keyPath: string;
private _caCertificateData: string | undefined;
private _certificateData: string | undefined;
private _keyData: string | undefined;
public constructor() {
const unresolvedUserFolder: string = homedir();
const userProfilePath: string = path.resolve(unresolvedUserFolder);
if (!FileSystem.exists(userProfilePath)) {
throw new Error("Unable to determine the current user's home directory");
}
const serveDataPath: string = path.join(userProfilePath, '.rushstack');
FileSystem.ensureFolder(serveDataPath);
this._caCertificatePath = path.join(serveDataPath, 'rushstack-ca.pem');
this._certificatePath = path.join(serveDataPath, 'rushstack-serve.pem');
this._keyPath = path.join(serveDataPath, 'rushstack-serve.key');
}
/**
* Path to the saved debug CA certificate
*/
public get caCertificatePath(): string {
return this._caCertificatePath;
}
/**
* Path to the saved debug TLS certificate
*/
public get certificatePath(): string {
return this._certificatePath;
}
/**
* Debug Certificate Authority certificate pem file contents.
*/
public get caCertificateData(): string | undefined {
if (!this._caCertificateData) {
try {
this._caCertificateData = FileSystem.readFile(this._caCertificatePath);
} catch (err) {
if (!FileSystem.isNotExistError(err)) {
throw err;
}
}
}
return this._caCertificateData;
}
public set caCertificateData(certificate: string | undefined) {
if (certificate) {
FileSystem.writeFile(this._caCertificatePath, certificate);
} else if (FileSystem.exists(this._caCertificatePath)) {
FileSystem.deleteFile(this._caCertificatePath);
}
this._caCertificateData = certificate;
}
/**
* Debug TLS Server certificate pem file contents.
*/
public get certificateData(): string | undefined {
if (!this._certificateData) {
try {
this._certificateData = FileSystem.readFile(this._certificatePath);
} catch (err) {
if (!FileSystem.isNotExistError(err)) {
throw err;
}
}
}
return this._certificateData;
}
public set certificateData(certificate: string | undefined) {
if (certificate) {
FileSystem.writeFile(this._certificatePath, certificate);
} else if (FileSystem.exists(this._certificatePath)) {
FileSystem.deleteFile(this._certificatePath);
}
this._certificateData = certificate;
}
/**
* Key used to sign the debug pem certificate.
*/
public get keyData(): string | undefined {
if (!this._keyData) {
try {
this._keyData = FileSystem.readFile(this._keyPath);
} catch (err) {
if (!FileSystem.isNotExistError(err)) {
throw err;
}
}
}
return this._keyData;
}
public set keyData(key: string | undefined) {
if (key) {
FileSystem.writeFile(this._keyPath, key);
} else if (FileSystem.exists(this._keyPath)) {
FileSystem.deleteFile(this._keyPath);
}
this._keyData = key;
}
}