Skip to content

Commit

Permalink
refactor(config): update applyLasp logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Svetlichny committed Apr 23, 2018
1 parent 2085956 commit bff6cef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
52 changes: 36 additions & 16 deletions lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ var CONFIG_FILE_LOCATIONS = [
process.env.HOME,
path.join(__dirname, '../../../..') // above node_modules
]
var LASP_MAP = {
record_sql: 'record_sql',
attributes_include: 'attributes.include_enabled',
// TODO: rename config key
allow_raw_exception_messages: 'strip_exception_messages.enabled',
custom_events: 'api.custom_events_enabled',
custom_parameters: 'api.custom_attributes_enabled',
live_instrumentation: null,
message_parameters: null,
job_arguments: null
}

// the REPL has no main module
if (process.mainModule && process.mainModule.filename) {
Expand Down Expand Up @@ -1230,29 +1241,35 @@ Config.prototype._applyHighSecurity = function _applyHighSecurity() {
}
}

var LASP_MAP = {
record_sql: 'record_sql',
attributes_include: 'attributes.include_enabled',
// TODO: rename config key
allow_raw_exception_messages: 'strip_exception_messages.enabled',
custom_events: 'api.custom_events_enabled',
custom_parameters: 'api.custom_attributes_enabled',
live_instrumentation: null,
message_parameters: null,
job_arguments: null
}

/**
* Checks policies received from preconnect against those expected
* by the agent, if LASP-enabled. Responds with an error to shut down
* the agent if necessary.
*
* @param {object} policies
* @param {function} callback
*
* @returns {object} known policies
*/
Config.prototype.applyLasp = function applyLasp(policies, callback) {
var config = this
var error = null
var keys = Object.keys(policies)

if (!config.security_policies_token) {
return callback(null, null)
if (keys.length) {
error = new Error(
'The agent received one or more unexpected security policies and will shut down.'
)
logger.error(error)
}
return callback(error, null)
}

var missingLASP = []
var missingRequired = []

var res = Object.keys(policies).reduce(function applyPolicy(obj, name) {
var res = keys.reduce(function applyPolicy(obj, name) {
var policy = policies[name]

if (!LASP_MAP[name]) {
Expand All @@ -1277,11 +1294,10 @@ Config.prototype.applyLasp = function applyLasp(policies, callback) {
}
})

var error = null
if (missingLASP.length) {
error = new Error(
'The agent did not receive one or more security policies that it ' +
'expected and will down: ' + missingLASP.join(', ') + '.'
'expected and will shut down: ' + missingLASP.join(', ') + '.'
)
} else if (missingRequired.length) {
error = new Error(
Expand All @@ -1292,6 +1308,10 @@ Config.prototype.applyLasp = function applyLasp(policies, callback) {
)
}

if (error) {
logger.error(error)
}

callback(error, res)
}

Expand Down
3 changes: 2 additions & 1 deletion test/unit/config/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1411,12 +1411,13 @@ describe('the agent configuration', function() {
config.applyLasp({}, cb)
})

it('returns error if required policy is not implemented', function(done) {
it('returns error if required policy is not implemented or unknown', function(done) {
var cb = function(err) {
expect(err.message).to.contain('received one or more required security policies')
done()
}

policies.job_arguments = { enabled: true, required: true }
policies.test = { enabled: true, required: true }

config.applyLasp(policies, cb)
Expand Down

0 comments on commit bff6cef

Please sign in to comment.