Files
node-vault/lib/node-vault.js
T

192 lines
4.8 KiB
JavaScript

// Generated by CoffeeScript 1.10.0
var Vault, debug, tv4;
debug = require('debug')('vault');
tv4 = require('tv4');
Vault = (function() {
var commands;
commands = require(__dirname + "/routes");
function Vault(opts) {
var k, v;
if (opts == null) {
opts = {};
}
this.mustache = opts.mustache || require('mustache');
this.request = opts.request || require('request');
this.apiVersion = opts.apiVersion || 'v1';
this.endpoint = opts.endpoint || process.env['VAULT_ADDR'] || "http://127.0.0.1:8200";
this.token = opts.token || process.env['VAULT_TOKEN'];
for (k in commands) {
v = commands[k];
this._generate(k, v);
}
}
Vault.prototype.help = function(path, opts, done) {
var ref;
if (opts == null) {
opts = {};
}
debug("help for " + path);
ref = this._handleCallback(opts, done), opts = ref[0], done = ref[1];
opts.path = '/' + path + '?help=1';
opts.json = null;
opts.method = 'GET';
return this._request(opts, this._handleErrors(done));
};
Vault.prototype.write = function(path, data, opts, done) {
var ref;
if (opts == null) {
opts = {};
}
debug("write " + path);
ref = this._handleCallback(opts, done), opts = ref[0], done = ref[1];
opts.path = '/' + path;
opts.json = data;
opts.method = 'PUT';
return this._request(opts, this._handleErrors(done));
};
Vault.prototype.read = function(path, opts, done) {
var ref;
if (opts == null) {
opts = {};
}
debug("read " + path);
ref = this._handleCallback(opts, done), opts = ref[0], done = ref[1];
opts.path = '/' + path;
opts.json = null;
opts.method = 'GET';
return this._request(opts, this._handleErrors(done));
};
Vault.prototype["delete"] = function(path, opts, done) {
var ref;
if (opts == null) {
opts = {};
}
debug("delete " + path);
ref = this._handleCallback(opts, done), opts = ref[0], done = ref[1];
opts.path = '/' + path;
opts.json = null;
opts.method = 'DELETE';
return this._request(opts, this._handleErrors(done));
};
Vault.prototype._handleCallback = function(opts, done) {
var json;
if (typeof opts === 'function') {
done = opts;
opts = {};
} else {
json = opts;
opts = {};
opts.json = json;
}
return [opts, done];
};
Vault.prototype._handleErrors = function(done) {
var extend;
extend = exports.extend = function(object, properties) {
var key, val;
for (key in properties) {
val = properties[key];
object[key] = val;
}
return object;
};
return function(err, res, body) {
if (err) {
debug(err);
return done(err);
}
if ((body != null ? body.errors : void 0) != null) {
err = new Error(body.errors[0]);
}
if (err) {
if (res.statusCode) {
extend(err, {
statusCode: res.statusCode
});
extend(err, {
statusMessage: res.statusMessage
});
}
}
if (err) {
return done(err);
}
return done(null, body);
};
};
Vault.prototype._generate = function(name, config) {
return this[name] = (function(_this) {
return function() {
var done, opts, ref, valid;
debug("" + name);
opts = arguments[0], done = arguments[1];
ref = _this._handleCallback(opts, done), opts = ref[0], done = ref[1];
opts.method = config.method;
opts.path = config.path;
if (config.schema != null) {
valid = tv4.validate(opts.json, config.schema.req);
if (!valid) {
debug(tv4.error.dataPath);
debug(tv4.error.message);
return done(tv4.error);
}
}
return _this._request(opts, _this._handleErrors(done));
};
})(this);
};
Vault.prototype._request = function(opts, done) {
var uri;
if (opts == null) {
opts = {};
}
uri = this.endpoint + "/" + this.apiVersion + opts.path;
uri = this.mustache.render(uri, opts.json);
uri = uri.replace(///g, '/');
debug(opts.method + " " + uri);
if (!opts['headers']) {
opts.headers = {};
}
if (this.token != null) {
opts.headers['X-Vault-Token'] = this.token;
}
opts.uri = uri;
return this.request(opts, function(err, res, body) {
if (err) {
debug(err);
return done(err);
}
debug("RES " + (res != null ? res.statusCode : void 0));
if (body) {
if (typeof body !== 'object') {
body = JSON.parse(body);
}
debug(body);
}
return done(err, res, body);
});
};
return Vault;
})();
module.exports = function(opts) {
return new Vault(opts);
};
//# sourceMappingURL=node-vault.js.map