Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dev certs for mac #245

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/office-addin-dev-certs/src/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,24 @@ export function deleteCertificateFiles(certificateDirectory: string = defaults.c
}
}

export async function uninstallCaCertificate(machine: boolean = false, verbose: boolean = true) {
if (isCaCertificateInstalled()) {
export async function uninstallCaCertificate(machine: boolean = false, verbose: boolean = true, expiredCert = false) {
akrantz marked this conversation as resolved.
Show resolved Hide resolved
if (expiredCert) {
const command = getUninstallCommand(machine);

try {
console.log(`Uninstalling expired CA certificate "Developer CA for Microsoft Office Add-ins"...`);
execSync(command, { stdio: "pipe" });
console.log(`You no longer have trusted access to https://localhost.`);
} catch (error) {
throw new Error(`Unable to uninstall expired the CA certificate.\n${error.stderr.toString()}`);
}

} else if (isCaCertificateInstalled()) {
const command = getUninstallCommand(machine);

try {
console.log(`Uninstalling CA certificate "Developer CA for Microsoft Office Add-ins"...`);
execSync(command, {stdio : "pipe" });
execSync(command, { stdio: "pipe" });
console.log(`You no longer have trusted access to https://localhost.`);
} catch (error) {
throw new Error(`Unable to uninstall the CA certificate.\n${error.stderr.toString()}`);
Expand All @@ -49,3 +60,4 @@ export async function uninstallCaCertificate(machine: boolean = false, verbose:
}
}
}

16 changes: 14 additions & 2 deletions packages/office-addin-dev-certs/src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import * as crypto from "crypto";
import * as fs from "fs";
import * as path from "path";
import * as defaults from "./defaults";
import { deleteCertificateFiles, uninstallCaCertificate } from './uninstall'

function getVerifyCommand(): string {
switch (process.platform) {
case "win32":
const script = path.resolve(__dirname, "..\\scripts\\verify.ps1");
return `powershell -ExecutionPolicy Bypass -File "${script}" "${defaults.certificateName}"`;
case "darwin": // macOS
return `security find-certificate -c '${defaults.certificateName}' -p | openssl x509 -checkend 86400 -noout`;
return `security find-certificate -c '${defaults.certificateName}' -p | openssl x509 -noout`;
default:
throw new Error(`Platform not supported: ${process.platform}`);
}
Expand All @@ -25,7 +26,18 @@ export function isCaCertificateInstalled(): boolean {
try {
const output = execSync(command, {stdio : "pipe" }).toString();
if (process.platform === "darwin") {
return true;
// Ceritificate exists. Now check and see if it's expired and remove it if is.
asachin96 marked this conversation as resolved.
Show resolved Hide resolved
try {
const command = `security find-certificate -c '${defaults.certificateName}' -p | openssl x509 -checkend 86400 -noout`;
Copy link
Contributor

@akrantz akrantz Jan 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this try/catch into a function isCaCertificateExpired() which returns a boolean, and then have the code here be:

// on Mac, remove certificate if expired
if (process.platform === darwin) {
if (isCaCertificateExpired()) {
uninstallExpiredCertificate();
}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My biggest concern with this is that the isCaCertificateInstalled() function is adding a side-effect which removes a cert. That really shouldn't be done inside this function as a side-effect. Is there a way to move this out of this function and call it for mac from another place?

execSync(command, {stdio : "pipe" }).toString();
return true;
}
catch {
uninstallCaCertificate(false /* machine */, true /* verbose */, true /* expiredCert */);
deleteCertificateFiles(defaults.certificateDirectory);
return false;
}

} else if (output.length !== 0) {
return true; // powershell command return empty string if the certificate not-found/expired
}
Expand Down