Skip to content

Commit

Permalink
Merge pull request #235 from apigee-internal/cli-cert-oauth
Browse files Browse the repository at this point in the history
edgemicro-cert -t token option
  • Loading branch information
srinandan authored May 23, 2019
2 parents 64d10ed + 315ffd6 commit 8fdd05e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 35 deletions.
66 changes: 45 additions & 21 deletions cli/cmd-cert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,79 @@ const setup = function setup() {
.command('install')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-t, --token <token>', 'OAuth token to use with management API')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.option('-f, --force', 'replace any existing keys')
.description('install a certificate for your organization')
.action((options) => {
options.error = optionError;
if (!options.username) { return options.error('username is required'); }
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
cert.installCert(options)
});
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
if (options.token) {
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
cert.installCert(options);
} else {
if (!options.username) { return options.error('username is required'); }
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
cert.installCert(options)
});
}
});

commander
.command('delete')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-t, --token <token>', 'OAuth token to use with management API')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.description('delete the certificate for your organization')
.action((options) => {
options.error = optionError;
if (!options.username) { return options.error('username is required'); }
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
cert.deleteCert(options)
});
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
if (options.token) {
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
cert.deleteCert(options);
} else {
if (!options.username) { return options.error('username is required'); }
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
cert.deleteCert(options)
});
}
});

commander
.command('check')
.option('-o, --org <org>', 'the organization')
.option('-e, --env <env>', 'the environment')
.option('-t, --token <token>', 'OAuth token to use with management API')
.option('-u, --username <user>', 'username of the organization admin')
.option('-p, --password <password>', 'password of the organization admin')
.description('check that your organization has a certificate installed')
.action((options) => {
options.error = optionError;
if (!options.username) { return options.error('username is required'); }
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
cert.checkCert(options)
});
options.token = options.token || process.env.EDGEMICRO_SAML_TOKEN;
if (options.token) {
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
cert.checkCert(options);
} else {
if (!options.username) { return options.error('username is required'); }
if (!options.org) { return options.error('org is required'); }
if (!options.env) { return options.error('env is required'); }
promptForPassword(options,(options)=>{
if (!options.password) { return options.error('password is required'); }
cert.checkCert(options)
});
}
});

commander
Expand Down
12 changes: 6 additions & 6 deletions cli/lib/cert.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function() {
};

Cert.prototype.installCert = function(options, cb) {
if ( !options.username ){
if ( !options.username && !options.token){
return options.error('username is required');
}
if ( !options.org ) {
Expand All @@ -27,7 +27,7 @@ Cert.prototype.installCert = function(options, cb) {
if ( !options.env ) {
return options.error('env is required');
}
if ( !options.password ) {
if ( !options.password && !options.token) {
return options.error('password is required');
}
const config = edgeconfig.load({ source: configLocations.getSourcePath(options.org, options.env) });
Expand All @@ -47,8 +47,8 @@ Cert.prototype.checkCert = function(options, cb) {
assert(options.org,"org is required");
assert(options.env,"env is required")

assert(options.username,"username is required");
assert(options.password,"password is required")
assert(options.username || options.token,"username is required");
assert(options.password || options.token,"password is required")

const config = edgeconfig.load({ source: configLocations.getSourcePath(options.org, options.env) });
if (options.url) {
Expand Down Expand Up @@ -80,8 +80,8 @@ Cert.prototype.deleteCert = function(options,cb) {
assert(options.org,"org is required");
assert(options.env,"env is required")

assert(options.username,"username is required");
assert(options.password,"password is required")
assert(options.username || options.token,"username is required");
assert(options.password || options.token,"password is required")



Expand Down
36 changes: 36 additions & 0 deletions tests/cli-cert-oauth.integ.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { spawnSync } = require('child_process');
const path = require('path');
const assert = require('assert');
const { org, env, user, password, key, secret, otoken } = require('./env.js');

const cliPath = path.join(__dirname, '..', 'cli', 'edgemicro-cert');
describe('OAuth token option for management API calls', done => {
it('edgemicro-cert check -t', done => {
let bash = spawnSync(cliPath, ['check', '-e', env, '-o', org, '-t', otoken]);
let outString = Buffer.from(bash.stdout).toString();
assert.equal(outString.includes('checked cert successfully'), true);
done();
});

it('edgemicro-cert delete -t', done => {
let bash = spawnSync(cliPath, ['delete', '-e', env, '-o', org, '-t', otoken]);
let outString = Buffer.from(bash.stdout).toString();
assert.equal(outString.includes('KVM deleted'), true);
done();
});

it('edgemicro-cert install -t', done => {
let bash = spawnSync(cliPath, ['install', '-e', env, '-o', org, '-t', otoken]);
let outString = Buffer.from(bash.stdout).toString();
assert.equal(outString.includes('installed cert'), true);
done();
});

it('recheck edgemicro-cert check -t', done => {
let bash = spawnSync(cliPath, ['check', '-e', env, '-o', org, '-t', otoken]);
let outString = Buffer.from(bash.stdout).toString();
console.log('outString', outString);
assert.equal(outString.includes('checked cert successfully'), true);
done();
});
});
19 changes: 11 additions & 8 deletions tests/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const org = process.env.MOCHA_ORG;
const env = process.env.MOCHA_ENV;
const tokenSecret = process.env.MOCHA_TOKEN_SECRET;
const tokenId = process.env.MOCHA_TOKEN_ID;
const otoken = process.env.MOCHA_SAML_TOKEN || '';


assert(password, 'Must set environment variable MOCHA_PASSWORD');
assert(user, 'Must set environment variable MOCHA_USER');
Expand All @@ -19,12 +21,13 @@ assert(tokenId, 'Must set environment variable MOCHA_TOKEN_ID');
assert(tokenSecret, 'Must set environment variable MOCHA_TOKEN_SECRET');

module.exports = {
password,
key,
secret,
user,
org,
env,
tokenSecret,
tokenId
password:password,
key:key,
secret:secret,
user:user,
org:org,
env:env,
tokenSecret:tokenSecret,
tokenId:tokenId,
otoken:otoken
};

0 comments on commit 8fdd05e

Please sign in to comment.