775ac7b58c
you must login with an BTP account in order to see the app
103 lines
3.1 KiB
JavaScript
103 lines
3.1 KiB
JavaScript
'use strict';
|
|
|
|
var utils = require('./lib/utils');
|
|
var fetch = require('node-fetch');
|
|
var format = require('util').format;
|
|
var debug = require('debug')('audit/rest-api-versions');
|
|
var deprecate = require('util').deprecate;
|
|
var NodeCache = require('node-cache');
|
|
var auditCache = new NodeCache();
|
|
|
|
var auditLog = deprecate(require('./lib/v1/audit-log'), 'Audit log v1 APIs are being deprecated. Use V2');
|
|
var auditLogV2 = require('./lib/v2/audit-log');
|
|
|
|
const AUDIT_V2_OPTIONS_CACHE_KEY = 'AUDIT_V2_OPTIONS';
|
|
|
|
function _getServiceOptions(credentials) {
|
|
var uri = credentials.url + '/audit-log';
|
|
var status = null;
|
|
|
|
let cachedv2ServiceOptions = auditCache.get(`${AUDIT_V2_OPTIONS_CACHE_KEY}-${uri}`);
|
|
if (cachedv2ServiceOptions) {
|
|
return Promise.resolve(cachedv2ServiceOptions);
|
|
}
|
|
|
|
let options = {
|
|
method: 'get',
|
|
headers: { 'Content-Type': 'application/json' }
|
|
};
|
|
|
|
debug('Sending request to %s', uri);
|
|
return fetch(uri, options).then(function (res) {
|
|
status = res.status;
|
|
return res.text();
|
|
}).then(function (body) {
|
|
try {
|
|
body = JSON.parse(body);
|
|
} catch (e) {
|
|
body = null;
|
|
}
|
|
|
|
debug('Response status code: %d, Body: %j',status, body);
|
|
if (status === 404) {
|
|
throw new Error('URL not found');
|
|
}
|
|
|
|
if (status !== 200) {
|
|
throw new Error(format('Status code %d received when checking supported REST API versions of Audit log server: %j', status, body));
|
|
}
|
|
|
|
var v2ServiceOptions = body.versions.find(function (element) {
|
|
return credentials.user && credentials.password ? element.version === 'v2' : element.version === 'v2' && element.url.indexOf('oauth2') >= 0;
|
|
});
|
|
|
|
if (!v2ServiceOptions) {
|
|
throw new Error('Audit log server does not support REST API v2');
|
|
}
|
|
|
|
let cachingResult = auditCache.set(`${AUDIT_V2_OPTIONS_CACHE_KEY}-${uri}`, v2ServiceOptions);
|
|
if (!cachingResult) {
|
|
debug('Failed to cache v2 service options. Retrying: %s', !cachingResult);
|
|
cachingResult = auditCache.set(`${AUDIT_V2_OPTIONS_CACHE_KEY}-${uri}`, v2ServiceOptions);
|
|
}
|
|
|
|
return v2ServiceOptions;
|
|
});
|
|
}
|
|
|
|
function _getAuditLogV2(credentials, securityContext) {
|
|
if (credentials.logToConsole === true) {
|
|
return Promise.resolve(auditLogV2(credentials, {}, securityContext));
|
|
}
|
|
return _getServiceOptions(credentials).then((serviceOptions) => {
|
|
return auditLogV2(credentials, serviceOptions, securityContext);
|
|
});
|
|
}
|
|
|
|
auditLog.v2 = function (credentials, securityContext, callback) {
|
|
utils.validate.object(credentials, 'Credentials');
|
|
if (arguments.length === 2){
|
|
if (securityContext && !callback && typeof (securityContext) === 'function') {
|
|
callback = securityContext;
|
|
securityContext = null;
|
|
}
|
|
if (!securityContext) {
|
|
securityContext = null;
|
|
}
|
|
}
|
|
if (securityContext) {
|
|
utils.validate.object(securityContext, 'SecurityContext');
|
|
}
|
|
if (callback) {
|
|
|
|
utils.validate.callback(callback);
|
|
_getAuditLogV2(credentials, securityContext).then((auditLogV2) => {
|
|
return callback(null, auditLogV2);
|
|
}).catch(callback);
|
|
} else {
|
|
return _getAuditLogV2(credentials, securityContext);
|
|
}
|
|
};
|
|
|
|
|
|
module.exports = auditLog;
|