mcafee
Version:
mcafee api via restful
186 lines (176 loc) • 5.68 kB
JavaScript
/**
* @fileOverview McAfee class
* @author Jeff Emershaw
* @version 0.0.1
* @module lib/mcafee
*/
;
var request = require('request');
var extend = require('util')._extend;
// Allow self-signed certificates
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
module.exports = mcafee;
/**
* Creates a new instance of class mcafee
* @public
* @class Represents mcafee
* @param {object} config - the object of this class
* @param {string} config.server - the server name or host name
* @param {string} config.username - the username
* @param {string} config.password - the password
* @param {number} [config.port=8443] - the port name
* @param {string} [config.protocol=https] - the protocol to use for the connection
* @param {string} [config.outputtype=json] - the requested format from the server
*/
function mcafee(config) {
/** The server name */
this.server = config && config.server ? config.server : null;
/** The username */
this.username = config && config.username ? config.username : null;
/** The password */
this.password = config && config.password ? config.password : null;
/** The port */
this.port = config && config.port ? config.port : 8443;
/** The protocol */
this.protocol = config && config.protocol ? config.protocol : "HTTPS";
/** The outputtype */
this.outputtype = config && config.outputtype ? config.outputtype : "json";
}
/**
* Generates the URL to connect to mcafee EPO api
* @protected
* @type Promise
* @return {Promise}
*/
mcafee.prototype._buildrequest = function () {
var self = this;
return new Promise(function (resolve, reject) {
if (!self.server || !self.port || !self.protocol) return reject(new Error('Please pass the required information'));
self.url = self.protocol + "://" + self.server + ":" + self.port + "/remote/";
self.query = { ":output": self.outputtype };
resolve();
});
};
/**
* Makes the request to the mcafee EPO server
* @protected
* @param {string} [command=core.help] - the command name
* @type Promise
* @return {promise}
* @throws {Error} rejection message
*/
mcafee.prototype._get_response = function (command, options) {
var self = this;
return new Promise(function (resolve, reject) {
if (!self.username || !self.password) return reject(new Error('Please input the correct information'));
if (!command) return reject(new Error('Command is required'));
if (options) self.query = extend(self.query, options);
request.get({
auth: {
'user': self.username,
'pass': self.password
},
url: self.url + command,
headers: {
'Content-Type': 'application/json'
},
json: true,
qs: self.query
}, function (err, res, body) {
if (err) return reject(err);
if (res.statusCode != 200) {
var output = new Error("Please correct");
output.statusCode = res.statusCode;
return reject(output);
}
resolve(body);
});
});
};
/**
* Makes the request to the mcafee EPO server
* @protected
* @type Promise
* @param {string} response - the response from url
* @return {Promise}
*/
mcafee.prototype._parse_response = function (response) {
return new Promise(function (resolve, reject) {
var status = response.split(':')[0].split(' ')[0]; // can be OK or Error
var result;
ConvertToJSON(response.slice(response.indexOf(':') + 1)).then(function (data) {
result = data;
var code = status == "Error" ? Number(response.split(':')[0].split(' ')[1]) : 0;
var results = { 'status': status, 'code': code, 'result': result };
resolve(results);
}).catch(function (err) {
result = response.split(':')[1];
var code = status == "Error" ? Number(response.split(':')[0].split(' ')[1]) : 0;
var results = { 'status': status, 'code': code, 'result': result, 'message': response.split(':')[1] };
resolve(results);
});
});
};
/**
* Help for all commands or a specified command, use this command to get help on mcafee commands.
* @public
* @param {string} [command=core.help] - the command name
* @return {promise}
* @example
* var mc = new mcafee({})
* mc.help('core.listUsers')
* .then(function(results){
* console.log(results)
* })
* .catch(function(err) {
* console.log(err)
* });
*/
mcafee.prototype.help = function (command) {
var self = this;
return new Promise(function (resolve, reject) {
self._buildrequest().then(function () {
return self._get_response('core.help', command);
}).then(function (data) {
return self._parse_response(data);
}).then(resolve).catch(reject);
});
};
/**
* Run for all commands or a specified command, use this command to get help on mcafee commands.
* @public
* @param {string} command - the command name
* @type Promise
* @return {Promise}
* @throws {Error} rejection message
* @example
* var mc = new mcafee({})
* mc.run()
* .then(function(results){
* console.log(results)
* })
* .catch(function(err) {
* console.log(err)
* });
*/
mcafee.prototype.run = function (command, options) {
var self = this;
return new Promise(function (resolve, reject) {
self._buildrequest().then(function () {
return self._get_response(command, options);
}).then(function (data) {
return self._parse_response(data);
}).then(resolve).catch(reject);
});
};
/**
* @private
* @type Promise
* @throws {Error}
*/
function ConvertToJSON(data) {
return new Promise(function (resolve, reject) {
if (!data) return reject(new Error("data is not set"));
return resolve(JSON.parse(data));
});
}