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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support api access even if access to org is not allowed #27

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions lib/apigee.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ Apigee.prototype.connect = promiseWrap(function(options, cb) {
// options = {user: "foo", password: "bar", org: "orgname", no_token: true}
// ==> will use HTTP Basic auth with the provided username and password (no token)
//
// options = {user: "foo", password: "bar", org: "orgname", no_token: true, no_organization_login: true}
// ==> will use HTTP Basic auth with the provided username and password (no token) and will not fetch the organization from api
//
// options = {user: "foo", org: "orgname"}
// ==> will prompt for password, then obtain a token
//
Expand Down Expand Up @@ -236,8 +239,12 @@ Apigee.prototype.connect = promiseWrap(function(options, cb) {
}
c.requestHeaders.authorization = 'Basic ' + common.base64Encode(user + ':' + options.password);
org = new Organization(c);
c.org = org;
return org.getProperties((e, result) => cb(e, org) );
if (!options.no_organization_login) {
c.org = org;
return org.getProperties((e, result) => cb(e, org) );
} else {
cb(null, org);
}
}
});

Expand Down
107 changes: 49 additions & 58 deletions lib/kvm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
//


const utility = require('./utility.js'),
common = require('./common.js'),
promiseWrap = require('./promiseWrap.js'),
request = require('request'),
urljoin = require('url-join'),
sprintf = require('sprintf-js').sprintf;
const utility = require('./utility.js'),
common = require('./common.js'),
promiseWrap = require('./promiseWrap.js'),
request = require('request'),
urljoin = require('url-join'),
sprintf = require('sprintf-js').sprintf;

function Kvm(conn) {this.conn = conn;}
function Kvm(conn) { this.conn = conn; }

function resolveKvmPath(conn, options) {
if (options && (options.env || options.environment)) {
Expand All @@ -38,19 +38,19 @@ function resolveKvmPath(conn, options) {
}

function putKvm0(conn, options, cb) {
common.insureFreshToken(conn, function(requestOptions) {
common.insureFreshToken(conn, function (requestOptions) {
let baseKvmPath = resolveKvmPath(conn, options);
let name = options.name || options.kvmName || options.kvm;

if (conn.orgProperties['features.isCpsEnabled']) {
if (conn?.orgProperties?.['features.isCpsEnabled']) {
if (!options.key || !options.value) {
throw new Error("missing key or value");
}
requestOptions.url = urljoin(baseKvmPath, name, 'entries', options.key);
if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite(sprintf('GET %s', requestOptions.url));
}
request.get(requestOptions, function(error, response, body) {
request.get(requestOptions, function (error, response, body) {
if (error) {
utility.logWrite(error);
return cb(error, body);
Expand All @@ -59,13 +59,13 @@ function putKvm0(conn, options, cb) {

if (response.statusCode == 200) {
// Update is required if the key already exists.
if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite('KVM entry update');
}
requestOptions.url = urljoin(requestOptions.url, options.key);
}
else if (response.statusCode == 404) {
if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite('KVM entry create');
}
}
Expand All @@ -81,17 +81,17 @@ function putKvm0(conn, options, cb) {
// "value" : "value_one_updated"
// }
requestOptions.headers['content-type'] = 'application/json';
requestOptions.body = JSON.stringify({ name: options.key, value : options.value });
if (conn.verbosity>0) {
requestOptions.body = JSON.stringify({ name: options.key, value: options.value });
if (conn.verbosity > 0) {
utility.logWrite(sprintf('POST %s', requestOptions.url));
}
request.post(requestOptions, common.callback(conn, [200, 201], cb));
}
else {
if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite(body);
}
cb({error: 'bad status', statusCode: response.statusCode });
cb({ error: 'bad status', statusCode: response.statusCode });
}
});
}
Expand All @@ -113,32 +113,32 @@ function putKvm0(conn, options, cb) {
requestOptions.headers['content-type'] = 'application/json';
var entry = options.entries ?
common.hashToArrayOfKeyValuePairs(options.entries) :
[{ name: options.key, value : options.value }] ;
[{ name: options.key, value: options.value }];

requestOptions.body = JSON.stringify({ name: name, entry: entry });
if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite(sprintf('POST %s', requestOptions.url));
}
request.post(requestOptions, common.callback(conn, [200, 201], cb));
}
});
}

Kvm.prototype.get = promiseWrap(function(options, cb) {
Kvm.prototype.get = promiseWrap(function (options, cb) {
var conn = this.conn;
if ( ! cb) { cb = options; options = {}; }
if (!cb) { cb = options; options = {}; }
var name = options.name || options.kvmName || options.kvm;
common.insureFreshToken(conn, function(requestOptions) {
common.insureFreshToken(conn, function (requestOptions) {
let baseKvmPath = resolveKvmPath(conn, options);
requestOptions.url = (name) ? urljoin(baseKvmPath, name) : baseKvmPath;
if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite(sprintf('GET %s', requestOptions.url));
}
request.get(requestOptions, common.callback(conn, [200], cb));
});
});

Kvm.prototype.create = promiseWrap(function(options, cb) {
Kvm.prototype.create = promiseWrap(function (options, cb) {
// POST :mgmtserver/v1/o/:orgname/keyvaluemaps
// POST :mgmtserver/v1/o/:orgname/e/:env/keyvaluemaps
// POST :mgmtserver/v1/o/:orgname/apis/:proxy/keyvaluemaps
Expand All @@ -156,19 +156,19 @@ Kvm.prototype.create = promiseWrap(function(options, cb) {
// ]
// }
let conn = this.conn;
if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite(sprintf('Create KVM %s', options.name));
}
const name = options.name || options.kvmName || options.kvm;
if ( ! name ) {
return cb({error:"missing KVM name"});
if (!name) {
return cb({ error: "missing KVM name" });
}

common.insureFreshToken(conn, function(requestOptions) {
common.insureFreshToken(conn, function (requestOptions) {
requestOptions.url = resolveKvmPath(conn, options);
requestOptions.headers['content-type'] = 'application/json';
const body = {
encrypted : options.encrypted ? "true" : "false",
encrypted: options.encrypted ? "true" : "false",
name
};
if (options.entries) {
Expand All @@ -177,68 +177,59 @@ Kvm.prototype.create = promiseWrap(function(options, cb) {
}
requestOptions.body = JSON.stringify(body);

if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite(sprintf('POST %s', requestOptions.url));
}
request.post(requestOptions, common.callback(conn, [201], cb));
});
});

Kvm.prototype.put = promiseWrap(function(options, cb) {
let conn = this.conn;
if ( ! conn.orgProperties) {
conn.org.getProperties(function(e, result) {
if (e) { return cb(e, result); }
putKvm0(conn, options, cb);
});
}
else {
return putKvm0(conn, options, cb);
}
Kvm.prototype.put = promiseWrap(function (options, cb) {
return putKvm0(this.conn, options, cb);
});

Kvm.prototype.del = promiseWrap(function(options, cb) {
Kvm.prototype.del = promiseWrap(function (options, cb) {
// DELETE :mgmtserver/v1/o/:orgname/keyvaluemaps/:kvmname
// DELETE :mgmtserver/v1/o/:orgname/e/:env/keyvaluemaps/:kvmname
// DELETE :mgmtserver/v1/o/:orgname/apis/:proxy/keyvaluemaps/:kvmname
// DELETE :mgmtserver/v1/o/:orgname/apis/:proxy/revisions/:rev/keyvaluemaps/:kvmname
let conn = this.conn,
name = options.name || options.kvmName || options.kvm;
if ( ! name ) {
return cb({error:"missing KVM name"});
name = options.name || options.kvmName || options.kvm;
if (!name) {
return cb({ error: "missing KVM name" });
}
common.insureFreshToken(conn, function(requestOptions) {
common.insureFreshToken(conn, function (requestOptions) {
requestOptions.url = urljoin(resolveKvmPath(conn, options), name);
if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite(sprintf('DELETE %s', requestOptions.url));
}
request.del(requestOptions, common.callback(conn, [200], cb));
});
});

Kvm.prototype.removeEntry = promiseWrap(function(options, cb) {
Kvm.prototype.removeEntry = promiseWrap(function (options, cb) {
// This results in a GET, then a PUT with the given entry removed.
// if the entry does not exist, then throw error.
// DELETE :mgmtserver/v1/o/:org/e/:env/keyvaluemaps/:mapname/entries/:entryname
let conn = this.conn,
name = options.name || options.kvmName || options.kvm,
key = options.entryName || options.key || options.entry;
if ( ! name ) {
return cb({error:"missing KVM name"});
name = options.name || options.kvmName || options.kvm,
key = options.entryName || options.key || options.entry;
if (!name) {
return cb({ error: "missing KVM name" });
}
if ( ! key ) {
return cb({error:"missing KVM entryName"});
if (!key) {
return cb({ error: "missing KVM entryName" });
}
common.insureFreshToken(conn, function(requestOptions) {
conn.org.getProperties(function(e, result) {
common.insureFreshToken(conn, function (requestOptions) {
conn.org.getProperties(function (e, result) {
if (e) { return cb(e, result); }
if ( ! conn.orgProperties['features.isCpsEnabled']) {
if (!conn?.orgProperties?.['features.isCpsEnabled']) {
return cb(new Error('not possible with non-CPS org'));
}
let baseKvmPath = resolveKvmPath(conn, options);
requestOptions.url = urljoin(baseKvmPath, name, 'entries', key);
requestOptions.method = 'DELETE';
if (conn.verbosity>0) {
if (conn.verbosity > 0) {
utility.logWrite(sprintf('DELETE %s', requestOptions.url));
}
request(requestOptions, common.callback(conn, [200], cb));
Expand Down