Skip to content

Commit

Permalink
Rename constants to CAPS_SNAKE_CASE (davewasmer#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-north committed Mar 14, 2021
1 parent e56a0c6 commit 88aed40
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 90 deletions.
8 changes: 7 additions & 1 deletion .eslintrc
Expand Up @@ -24,5 +24,11 @@
"@typescript-eslint/explicit-member-accessibility": 0,
"@typescript-eslint/prefer-includes": 0,
"@typescript-eslint/require-await": 1
}
},
"overrides": [{
"files": ["test/**/*.ts"],
"parserOptions": {
"project": "./test/tsconfig.json"
}
}]
}
8 changes: 4 additions & 4 deletions cli/commands/remote.ts
@@ -1,5 +1,5 @@
import { Argv, Arguments } from 'yargs';
import { rootCACertPath, DEFAULT_REMOTE_PORT } from '../../src/constants';
import { ROOT_CA_CERT_PATH, DEFAULT_REMOTE_PORT } from '../../src/constants';
import * as express from 'express';
import * as https from 'https';
import * as fs from 'fs';
Expand Down Expand Up @@ -50,12 +50,12 @@ export const handler = (argv: Arguments): void => {
cert: cert.replace(/\\n/g, '\n')
};
app.get('/get_remote_certificate', (req, res) => {
if (!fs.existsSync(rootCACertPath)) {
if (!fs.existsSync(ROOT_CA_CERT_PATH)) {
throw new Error(
`Could not read the public certificate file ${rootCACertPath}, please check the file exists and try again.`
`Could not read the public certificate file ${ROOT_CA_CERT_PATH}, please check the file exists and try again.`
);
}
res.send(fs.readFileSync(rootCACertPath, 'utf8'));
res.send(fs.readFileSync(ROOT_CA_CERT_PATH, 'utf8'));
});

const httpsServer = https.createServer(credentials, app);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -12,7 +12,7 @@
"clean": "rimraf dist",
"build": "yarn clean && yarn build:ts && yarn build:api-extract && yarn build:api-docs",
"build:ts": "tsc",
"lint": "eslint src --ext ts",
"lint": "eslint . --ext ts",
"prepublishOnly": "yarn build",
"build:api-docs": "yarn api-documenter markdown -i ./temp -o ./docs",
"build:api-extract": "yarn api-extractor run"
Expand Down
46 changes: 23 additions & 23 deletions src/certificate-authority.ts
Expand Up @@ -6,16 +6,16 @@ import {
import * as createDebug from 'debug';

import {
domainsDir,
rootCADir,
DOMAINS_DIR,
ROOT_CA_DIR,
ensureConfigDirs,
getLegacyConfigDir,
rootCAKeyPath,
rootCACertPath,
caSelfSignConfig,
opensslSerialFilePath,
opensslDatabaseFilePath,
caVersionFile
ROOT_CA_KEY_PATH,
ROOT_CA_CERT_PATH,
CA_SELF_SIGN_CONFIG_PATH,
OPENSSL_SERIAL_FILE_PATH,
OPENSSL_DB_PATH,
CA_VERSION_FILE_PATH
} from './constants';
import currentPlatform from './platforms';
import { openssl, tmpDir } from './utils';
Expand Down Expand Up @@ -53,15 +53,15 @@ export default async function installCertificateAuthority(

debug(`Generating a CA certificate`);
openssl(
`req -new -x509 -config "${caSelfSignConfig}" -key "${rootKeyPath}" -out "${rootCACertPath}" -days ${certOptions.caCertExpiry}`,
`req -new -x509 -config "${CA_SELF_SIGN_CONFIG_PATH}" -key "${rootKeyPath}" -out "${ROOT_CA_CERT_PATH}" -days ${certOptions.caCertExpiry}`,
'generating CA CSR'
);

debug('Saving certificate authority credentials');
await saveCertificateAuthorityCredentials(rootKeyPath);

debug(`Adding the root certificate authority to trust stores`);
await currentPlatform.addToTrustStores(rootCACertPath, options);
await currentPlatform.addToTrustStores(ROOT_CA_CERT_PATH, options);
}

/**
Expand All @@ -70,10 +70,10 @@ export default async function installCertificateAuthority(
*/
function seedConfigFiles(): void {
// This is v2 of the devcert certificate authority setup
writeFile(caVersionFile, '2');
writeFile(CA_VERSION_FILE_PATH, '2');
// OpenSSL CA files
writeFile(opensslDatabaseFilePath, '');
writeFile(opensslSerialFilePath, '01');
writeFile(OPENSSL_DB_PATH, '');
writeFile(OPENSSL_SERIAL_FILE_PATH, '01');
}

export async function withCertificateAuthorityCredentials(
Expand All @@ -89,8 +89,8 @@ export async function withCertificateAuthorityCredentials(
const tmp = tmpDir();
const caKeyPath = join(tmp.name, 'ca.key');
const caCertPath = join(caKeyPath, '..', 'ca.crt');
const caKey = await currentPlatform.readProtectedFile(rootCAKeyPath);
const caCrt = await currentPlatform.readProtectedFile(rootCACertPath);
const caKey = await currentPlatform.readProtectedFile(ROOT_CA_KEY_PATH);
const caCrt = await currentPlatform.readProtectedFile(ROOT_CA_CERT_PATH);
writeFile(caKeyPath, caKey);
writeFile(caCertPath, caCrt);
await cb({ caKeyPath, caCertPath });
Expand All @@ -104,13 +104,13 @@ async function saveCertificateAuthorityCredentials(
): Promise<void> {
debug(`Saving devcert's certificate authority credentials`);
const key = readFile(keypath, 'utf-8');
await currentPlatform.writeProtectedFile(rootCAKeyPath, key);
await currentPlatform.writeProtectedFile(ROOT_CA_KEY_PATH, key);
}

function certErrors(): string {
try {
openssl(
`x509 -in "${rootCACertPath}" -noout`,
`x509 -in "${ROOT_CA_CERT_PATH}" -noout`,
'checking for certificate errors'
);
return '';
Expand Down Expand Up @@ -143,10 +143,10 @@ export async function ensureCACertReadable(
*/
try {
const caFileContents = await currentPlatform.readProtectedFile(
rootCACertPath
ROOT_CA_CERT_PATH
);
currentPlatform.deleteProtectedFiles(rootCACertPath);
writeFile(rootCACertPath, caFileContents);
currentPlatform.deleteProtectedFiles(ROOT_CA_CERT_PATH);
writeFile(ROOT_CA_CERT_PATH, caFileContents);
} catch (e) {
return installCertificateAuthority(options, certOptions);
}
Expand Down Expand Up @@ -174,8 +174,8 @@ export async function ensureCACertReadable(
* @public
*/
export function uninstall(): void {
currentPlatform.removeFromTrustStores(rootCACertPath);
currentPlatform.deleteProtectedFiles(domainsDir);
currentPlatform.deleteProtectedFiles(rootCADir);
currentPlatform.removeFromTrustStores(ROOT_CA_CERT_PATH);
currentPlatform.deleteProtectedFiles(DOMAINS_DIR);
currentPlatform.deleteProtectedFiles(ROOT_CA_DIR);
currentPlatform.deleteProtectedFiles(getLegacyConfigDir());
}
61 changes: 31 additions & 30 deletions src/constants.ts
Expand Up @@ -12,37 +12,38 @@ import applicationConfigPath = require('application-config-path');
import * as _createDebug from 'debug';

const debug = _createDebug('devcert:constants');

// Platform shortcuts
export const isMac = process.platform === 'darwin';
export const isLinux = process.platform === 'linux';
export const isWindows = process.platform === 'win32';
export const IS_MAC = process.platform === 'darwin';
export const IS_LINUX = process.platform === 'linux';
export const IS_WINDOWS = process.platform === 'win32';

// Common paths
export const configDir = applicationConfigPath('devcert');
export const configPath: (...pathSegments: string[]) => string = path.join.bind(
path,
configDir
);
export const CONFIG_DIR = applicationConfigPath('devcert');

export const makeConfigPath: (
...pathSegments: string[]
) => string = path.join.bind(path, CONFIG_DIR);

export const DEFAULT_REMOTE_PORT = 2702;

export const domainsDir = configPath('domains');
export const DOMAINS_DIR = makeConfigPath('domains');

export const caVersionFile = configPath('devcert-ca-version');
export const opensslSerialFilePath = configPath(
export const CA_VERSION_FILE_PATH = makeConfigPath('devcert-ca-version');
export const OPENSSL_SERIAL_FILE_PATH = makeConfigPath(
'certificate-authority',
'serial'
);
export const opensslDatabaseFilePath = configPath(
export const OPENSSL_DB_PATH = makeConfigPath(
'certificate-authority',
'index.txt'
);
export const opensslConfigDir = path.join(
export const OPENSSL_CONFIG_DIR = path.join(
__dirname,
'../../openssl-configurations/'
);
export const caSelfSignConfig = path.join(
opensslConfigDir,
export const CA_SELF_SIGN_CONFIG_PATH = path.join(
OPENSSL_CONFIG_DIR,
'certificate-authority-self-signing.conf'
);

Expand All @@ -64,7 +65,7 @@ export async function withDomainSigningRequestConfig(
'domain-certificate-signing-requests.conf'
);
const source = readFile(
path.join(opensslConfigDir, 'domain-certificate-signing-requests.conf'),
path.join(OPENSSL_CONFIG_DIR, 'domain-certificate-signing-requests.conf'),
'utf-8'
);
const template = makeTemplate(source);
Expand All @@ -86,15 +87,15 @@ export async function withDomainCertificateConfig(
const tmp = tmpDir();
const tmpFile = path.join(tmp.name, 'ca.cfg');
const source = readFile(
path.join(opensslConfigDir, 'domain-certificates.conf'),
path.join(OPENSSL_CONFIG_DIR, 'domain-certificates.conf'),
'utf-8'
);
const template = makeTemplate(source);
const result = template({
commonName,
altNames: includeWildcards([commonName, ...alternativeNames]),
serialFile: opensslSerialFilePath,
databaseFile: opensslDatabaseFilePath,
serialFile: OPENSSL_SERIAL_FILE_PATH,
databaseFile: OPENSSL_DB_PATH,
domainDir: pathForDomain(commonName)
});
writeFile(tmpFile, eol.auto(result));
Expand All @@ -107,32 +108,32 @@ export async function withDomainCertificateConfig(
// confTemplate = confTemplate.replace(/SERIAL_PATH/, configPath('serial').replace(/\\/g, '\\\\'));
// confTemplate = eol.auto(confTemplate);

export const rootCADir = configPath('certificate-authority');
export const rootCAKeyPath = path.join(rootCADir, 'private-key.key');
export const rootCACertPath = path.join(rootCADir, 'certificate.cert');
export const ROOT_CA_DIR = makeConfigPath('certificate-authority');
export const ROOT_CA_KEY_PATH = path.join(ROOT_CA_DIR, 'private-key.key');
export const ROOT_CA_CERT_PATH = path.join(ROOT_CA_DIR, 'certificate.cert');

debug('rootCACertPath', rootCACertPath);
debug('rootCAKeyPath', rootCAKeyPath);
debug('rootCADir', rootCADir);
debug('rootCACertPath', ROOT_CA_CERT_PATH);
debug('rootCAKeyPath', ROOT_CA_KEY_PATH);
debug('rootCADir', ROOT_CA_DIR);

// Exposed for uninstallation purposes.
export function getLegacyConfigDir(): string {
if (isWindows && process.env.LOCALAPPDATA) {
if (IS_WINDOWS && process.env.LOCALAPPDATA) {
return path.join(process.env.LOCALAPPDATA, 'devcert', 'config');
} else {
const uid = process.getuid && process.getuid();
const userHome =
isLinux && uid === 0
IS_LINUX && uid === 0
? path.resolve('/usr/local/share')
: require('os').homedir();
return path.join(userHome, '.config', 'devcert');
}
}

export function ensureConfigDirs(): void {
mkdirp(configDir);
mkdirp(domainsDir);
mkdirp(rootCADir);
mkdirp(CONFIG_DIR);
mkdirp(DOMAINS_DIR);
mkdirp(ROOT_CA_DIR);
}

ensureConfigDirs();
23 changes: 12 additions & 11 deletions src/index.ts
Expand Up @@ -18,12 +18,12 @@ import { sync as commandExists } from 'command-exists';
import * as rimraf from 'rimraf';
import { version } from '../package.json';
import {
isMac,
isLinux,
isWindows,
domainsDir,
rootCAKeyPath,
rootCACertPath,
IS_MAC,
IS_LINUX,
IS_WINDOWS,
DOMAINS_DIR,
ROOT_CA_KEY_PATH,
ROOT_CA_CERT_PATH,
DEFAULT_REMOTE_PORT
} from './constants';
import currentPlatform from './platforms';
Expand Down Expand Up @@ -320,7 +320,7 @@ async function certificateForImpl<
Object.assign(UI, options.ui);
}

if (!isMac && !isLinux && !isWindows) {
if (!IS_MAC && !IS_LINUX && !IS_WINDOWS) {
throw new Error(`Platform not supported: "${process.platform}"`);
}

Expand All @@ -333,7 +333,7 @@ async function certificateForImpl<
const domainKeyPath = keyPathForDomain(commonName);
const domainCertPath = certPathForDomain(commonName);

if (!exists(rootCAKeyPath)) {
if (!exists(ROOT_CA_KEY_PATH)) {
debug(
'Root CA is not installed yet, so it must be our first run. Installing root CA ...'
);
Expand Down Expand Up @@ -389,8 +389,9 @@ async function certificateForImpl<
cert: readFile(domainCertPath)
} as IReturnData<O>;
if (options.getCaBuffer)
((ret as unknown) as CaBuffer).ca = readFile(rootCACertPath);
if (options.getCaPath) ((ret as unknown) as CaPath).caPath = rootCACertPath;
((ret as unknown) as CaBuffer).ca = readFile(ROOT_CA_CERT_PATH);
if (options.getCaPath)
((ret as unknown) as CaPath).caPath = ROOT_CA_CERT_PATH;

return ret;
}
Expand Down Expand Up @@ -677,7 +678,7 @@ export function hasCertificateFor(commonName: string): boolean {
* @alpha
*/
export function configuredDomains(): string[] {
return readdir(domainsDir);
return readdir(DOMAINS_DIR);
}

/**
Expand Down
26 changes: 15 additions & 11 deletions src/platforms/shared.ts
Expand Up @@ -8,19 +8,23 @@ import { existsSync } from 'fs';
import { sync as glob } from 'glob';
import { readFileSync as readFile, existsSync as exists } from 'fs';
import { run } from '../utils';
import { isMac, isLinux, configDir, getLegacyConfigDir } from '../constants';
import { IS_MAC, IS_LINUX, CONFIG_DIR, getLegacyConfigDir } from '../constants';
import UI from '../user-interface';
import { execSync as exec } from 'child_process';

import { homedir } from 'os';
const debug = createDebug('devcert:platforms:shared');

export const HOME = process.env.HOME
? process.env.HOME
: (function(): never {
throw new Error(
'HOME environment variable was not set. It should be something like "/Users/exampleName"'
);
})();
function determineHomeDir(): string {
if (process.env.HOME) return process.env.HOME;
if (typeof process.env.HOME !== 'undefined') {
throw new Error(
'HOME environment variable was not set. It should be something like "/Users/exampleName"'
);
}
return homedir();
}

export const HOME = determineHomeDir();

/**
* Given a directory or glob pattern of directories, run a callback for each db
Expand Down Expand Up @@ -120,7 +124,7 @@ function isFirefoxOpen(): boolean {
// never needs to check this, because it doesn't update the NSS DB
// automaticaly.
assert(
isMac || isLinux,
IS_MAC || IS_LINUX,
'checkForOpenFirefox was invoked on a platform other than Mac or Linux'
);
return exec('ps aux').indexOf('firefox') > -1;
Expand Down Expand Up @@ -193,7 +197,7 @@ export function assertNotTouchingFiles(
operation: string
): void {
if (
!filepath.startsWith(configDir) &&
!filepath.startsWith(CONFIG_DIR) &&
!filepath.startsWith(getLegacyConfigDir())
) {
throw new Error(
Expand Down

0 comments on commit 88aed40

Please sign in to comment.