Skip to content

Commit

Permalink
feat: Make it possible to provide certificates as a file path or as a…
Browse files Browse the repository at this point in the history
… raw content (#99)
  • Loading branch information
mykola-mokhnach committed Feb 13, 2020
1 parent 217c4dd commit 77848e2
Showing 1 changed file with 56 additions and 8 deletions.
64 changes: 56 additions & 8 deletions lib/subcommands/keychain.js
@@ -1,35 +1,83 @@
import { fs, tempDir } from 'appium-support';
import path from 'path';
import _ from 'lodash';


const commands = {};

async function handleRawPayload (payload, onPayloadStored) {
const tmpRoot = await tempDir.openDir();
try {
const filePath = path.resolve(tmpRoot, 'certificate.pem');
if (_.isBuffer(payload)) {
await fs.writeFile(filePath, payload);
} else {
await fs.writeFile(filePath, payload, 'utf8');
}
await onPayloadStored(filePath);
} finally {
await fs.rimraf(tmpRoot);
}
}


/**
* @typedef {Object} CertOptions
* @property {boolean} raw [false] - whether the `cert` argument
* is the path to the certificate on the local file system or
* a raw certificate content
*/

/**
* Adds the given certificate to the Trusted Root Store on the simulator
*
* @since Xcode 11.4 SDK
* @param {string} certPath the full path to a valid .cert file containing
* the certificate content
* @param {string} cert the full path to a valid .cert file containing
* the certificate content or the certificate content itself, depending on
* options
* @param {CertOptions} opts
* @throws {Error} if the current SDK version does not support the command
* or there was an error while adding the certificate
* @throws {Error} If the `udid` instance property is unset
*/
commands.addRootCertificate = async function addRootCertificate (certPath) {
await this.exec('keychain', {
commands.addRootCertificate = async function addRootCertificate (cert, opts = {}) {
const {
raw = false,
} = opts;
const execMethod = async (certPath) => await this.exec('keychain', {
args: [this.requireUdid('keychain add-root-cert'), 'add-root-cert', certPath],
});
if (raw) {
await handleRawPayload(cert, execMethod);
} else {
await execMethod(cert);
}
};

/**
* Adds the given certificate to the Keychain Store on the simulator
*
* @since Xcode 11.4 SDK
* @param {string} certPath the full path to a valid .cert file containing
* the certificate content
* @param {string} cert the full path to a valid .cert file containing
* the certificate content or the certificate content itself, depending on
* options
* @param {CertOptions} opts
* @throws {Error} if the current SDK version does not support the command
* or there was an error while adding the certificate
* @throws {Error} If the `udid` instance property is unset
*/
commands.addCertificate = async function addCertificate (certPath) {
await this.exec('keychain', {
commands.addCertificate = async function addCertificate (cert, opts = {}) {
const {
raw = false,
} = opts;
const execMethod = async (certPath) => await this.exec('keychain', {
args: [this.requireUdid('keychain add-cert'), 'add-cert', certPath],
});
if (raw) {
await handleRawPayload(cert, execMethod);
} else {
await execMethod(cert);
}
};

/**
Expand Down

0 comments on commit 77848e2

Please sign in to comment.