Skip to content

Commit 23ed83a

Browse files
[debug-certificate-manager] Option to skip automatically trusting and untrusting a certificate (#4933)
* Option to skip trusting and untrusting a certificate * rush change * Update common/changes/@rushstack/debug-certificate-manager/user-apc-skipcerttrust_2024-09-20-22-22.json Co-authored-by: Ian Clanton-Thuon <[email protected]> * Update libraries/debug-certificate-manager/src/CertificateManager.ts Co-authored-by: Ian Clanton-Thuon <[email protected]> * Update common/reviews/api/debug-certificate-manager.api.md Co-authored-by: Ian Clanton-Thuon <[email protected]> --------- Co-authored-by: apostolisms <[email protected]> Co-authored-by: Ian Clanton-Thuon <[email protected]>
1 parent c71df47 commit 23ed83a

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@rushstack/debug-certificate-manager",
5+
"comment": "Add a `skipCertificateTrust` option to `CertificateManager.ensureCertificateAsync` that skips automatically trusting the generated certificate and untrusting an existing certificate with issues.",
6+
"type": "minor"
7+
}
8+
],
9+
"packageName": "@rushstack/debug-certificate-manager"
10+
}

common/reviews/api/debug-certificate-manager.api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { ITerminal } from '@rushstack/terminal';
99
// @public
1010
export class CertificateManager {
1111
constructor();
12-
ensureCertificateAsync(canGenerateNewCertificate: boolean, terminal: ITerminal, generationOptions?: ICertificateGenerationOptions): Promise<ICertificate>;
12+
ensureCertificateAsync(canGenerateNewCertificate: boolean, terminal: ITerminal, options?: ICertificateGenerationOptions): Promise<ICertificate>;
1313
untrustCertificateAsync(terminal: ITerminal): Promise<boolean>;
1414
}
1515

@@ -39,6 +39,7 @@ export interface ICertificate {
3939

4040
// @public
4141
export interface ICertificateGenerationOptions {
42+
skipCertificateTrust?: boolean;
4243
subjectAltNames?: ReadonlyArray<string>;
4344
subjectIPAddresses?: ReadonlyArray<string>;
4445
validityInDays?: number;

libraries/debug-certificate-manager/src/CertificateManager.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ export interface ICertificateGenerationOptions {
110110
* How many days the certificate should be valid for.
111111
*/
112112
validityInDays?: number;
113+
/**
114+
* Skip trusting a certificate. Defaults to false.
115+
*/
116+
skipCertificateTrust?: boolean;
113117
}
114118

115119
const MAX_CERTIFICATE_VALIDITY_DAYS: 365 = 365;
@@ -135,10 +139,9 @@ export class CertificateManager {
135139
public async ensureCertificateAsync(
136140
canGenerateNewCertificate: boolean,
137141
terminal: ITerminal,
138-
generationOptions?: ICertificateGenerationOptions
142+
options?: ICertificateGenerationOptions
139143
): Promise<ICertificate> {
140-
const optionsWithDefaults: Required<ICertificateGenerationOptions> =
141-
applyDefaultOptions(generationOptions);
144+
const optionsWithDefaults: Required<ICertificateGenerationOptions> = applyDefaultOptions(options);
142145

143146
const { certificateData: existingCert, keyData: existingKey } = this._certificateStore;
144147

@@ -226,7 +229,9 @@ export class CertificateManager {
226229
if (canGenerateNewCertificate) {
227230
messages.push('Attempting to untrust the certificate and generate a new one.');
228231
terminal.writeWarningLine(messages.join(' '));
229-
await this.untrustCertificateAsync(terminal);
232+
if (!options?.skipCertificateTrust) {
233+
await this.untrustCertificateAsync(terminal);
234+
}
230235
return await this._ensureCertificateInternalAsync(optionsWithDefaults, terminal);
231236
} else {
232237
messages.push(
@@ -732,10 +737,9 @@ export class CertificateManager {
732737
});
733738
}
734739

735-
const trustCertificateResult: boolean = await this._tryTrustCertificateAsync(
736-
tempCertificatePath,
737-
terminal
738-
);
740+
const trustCertificateResult: boolean = options.skipCertificateTrust
741+
? true
742+
: await this._tryTrustCertificateAsync(tempCertificatePath, terminal);
739743

740744
let subjectAltNames: readonly string[] | undefined;
741745
if (trustCertificateResult) {
@@ -787,6 +791,7 @@ function applyDefaultOptions(
787791
): Required<ICertificateGenerationOptions> {
788792
const subjectNames: ReadonlyArray<string> | undefined = options?.subjectAltNames;
789793
const subjectIpAddresses: ReadonlyArray<string> | undefined = options?.subjectIPAddresses;
794+
const skipCertificateTrust: boolean | undefined = options?.skipCertificateTrust || false;
790795
return {
791796
subjectAltNames: subjectNames?.length ? subjectNames : DEFAULT_CERTIFICATE_SUBJECT_NAMES,
792797
subjectIPAddresses: subjectIpAddresses?.length
@@ -795,7 +800,8 @@ function applyDefaultOptions(
795800
validityInDays: Math.min(
796801
MAX_CERTIFICATE_VALIDITY_DAYS,
797802
options?.validityInDays ?? MAX_CERTIFICATE_VALIDITY_DAYS
798-
)
803+
),
804+
skipCertificateTrust: skipCertificateTrust
799805
};
800806
}
801807

0 commit comments

Comments
 (0)