-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RBAC - SecurityAuditLogger #19571
RBAC - SecurityAuditLogger #19571
Changes from 8 commits
9c3ce6d
f4c1833
f041606
756d213
87c9325
aba377a
536933c
762833f
a2b51b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
export class SecurityAuditLogger { | ||
constructor(config, auditLogger) { | ||
this._enabled = config.get('xpack.security.audit.enabled'); | ||
this._auditLogger = auditLogger; | ||
} | ||
|
||
savedObjectsAuthorizationFailure(username, action, types, missing) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
|
||
this._auditLogger.log( | ||
'saved_objects_authorization_failure', | ||
`${username} unauthorized to ${action} ${types.join(',')}, missing ${missing.join(',')}`, | ||
{ | ||
username, | ||
action, | ||
types, | ||
missing | ||
} | ||
); | ||
} | ||
|
||
savedObjectsAuthorizationSuccess(username, action, types, args) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
|
||
this._auditLogger.log( | ||
'saved_objects_authorization_success', | ||
`${username} authorized to ${action} ${types.join(',')}`, | ||
{ | ||
username, | ||
action, | ||
types, | ||
args, | ||
} | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { SecurityAuditLogger } from './audit_logger'; | ||
|
||
|
||
const createMockConfig = (settings) => { | ||
const mockConfig = { | ||
get: jest.fn() | ||
}; | ||
|
||
const defaultSettings = {}; | ||
|
||
mockConfig.get.mockImplementation(key => { | ||
return key in settings ? settings[key] : defaultSettings[key]; | ||
}); | ||
|
||
return mockConfig; | ||
}; | ||
|
||
const createMockAuditLogger = () => { | ||
return { | ||
log: jest.fn() | ||
}; | ||
}; | ||
|
||
describe(`#savedObjectsAuthorizationFailure`, () => { | ||
test(`doesn't log anything when xpack.security.audit.enabled is false`, () => { | ||
const config = createMockConfig({ | ||
'xpack.security.audit.enabled': false | ||
}); | ||
const auditLogger = createMockAuditLogger(); | ||
|
||
const securityAuditLogger = new SecurityAuditLogger(config, auditLogger); | ||
securityAuditLogger.savedObjectsAuthorizationFailure(); | ||
|
||
expect(auditLogger.log).toHaveBeenCalledTimes(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels fragile to me. I think it's strange that we have to rely on the I don't have any recommendations here, but what's your take? Am I overthinking this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I've always struggled with this when it comes to writing "unit tests". In this situation, the "unit under test" is specifically the SecurityAuditLogger, so we're mocking out the AuditLogger itself, so we're forced to write the current style of tests where we ensure the security audit logger is calling the correct methods on the audit logger. The other approach that I've taken before is to test the security audit logger and the audit logger as the "unit under test" and expand the boundaries so we'd be stubbing out the Kibana server itself and ensuring we're calling the appropriate "log" functions on the server itself. If we we were to do this, we'd have duplicate code between each of the implementations of the plugin specific audit loggers that ensure the appropriate tags are added, etc. and when we changed the internal implementation of the base AuditLogger in accordance with the new platform we'd have to rework a lot more tests. Given the intent of the introduction of the base AuditLogger abstracting away the internal way that tags are used, I think the current approach is the best, but it definitely leaves something to be desired. |
||
}); | ||
|
||
test('logs via auditLogger when xpack.security.audit.enabled is true', () => { | ||
const config = createMockConfig({ | ||
'xpack.security.audit.enabled': true | ||
}); | ||
const auditLogger = createMockAuditLogger(); | ||
const securityAuditLogger = new SecurityAuditLogger(config, auditLogger); | ||
const username = 'foo-user'; | ||
const action = 'foo-action'; | ||
const types = [ 'foo-type-1', 'foo-type-2' ]; | ||
const missing = [`action:saved-objects/${types[0]}/foo-action`, `action:saved-objects/${types[1]}/foo-action`]; | ||
|
||
securityAuditLogger.savedObjectsAuthorizationFailure(username, action, types, missing); | ||
|
||
expect(auditLogger.log).toHaveBeenCalledWith( | ||
'saved_objects_authorization_failure', | ||
expect.stringContaining(`${username} unauthorized to ${action}`), | ||
{ | ||
username, | ||
action, | ||
types, | ||
missing, | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe(`#savedObjectsAuthorizationSuccess`, () => { | ||
test(`doesn't log anything when xpack.security.audit.enabled is false`, () => { | ||
const config = createMockConfig({ | ||
'xpack.security.audit.enabled': false | ||
}); | ||
const auditLogger = createMockAuditLogger(); | ||
|
||
const securityAuditLogger = new SecurityAuditLogger(config, auditLogger); | ||
securityAuditLogger.savedObjectsAuthorizationSuccess(); | ||
|
||
expect(auditLogger.log).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('logs via auditLogger when xpack.security.audit.enabled is true', () => { | ||
const config = createMockConfig({ | ||
'xpack.security.audit.enabled': true | ||
}); | ||
const auditLogger = createMockAuditLogger(); | ||
const securityAuditLogger = new SecurityAuditLogger(config, auditLogger); | ||
const username = 'foo-user'; | ||
const action = 'foo-action'; | ||
const types = [ 'foo-type-1', 'foo-type-2' ]; | ||
const args = { | ||
'foo': 'bar', | ||
'dude': 'yup', | ||
'women': 'yay!', | ||
}; | ||
|
||
securityAuditLogger.savedObjectsAuthorizationSuccess(username, action, types, args); | ||
|
||
expect(auditLogger.log).toHaveBeenCalledWith( | ||
'saved_objects_authorization_success', | ||
expect.stringContaining(`${username} authorized to ${action}`), | ||
{ | ||
username, | ||
action, | ||
types, | ||
args, | ||
} | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
args
intentionally omitted from the failure case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was intentional, but it might not be what we really want. My reasoning was that in the case where we're denying the user from performing an action, the specifics of that action aren't relevant if they weren't able to perform it. So we're only logging the information that we used to deny the request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a fair argument. My counter-argument is:
args
will make parsing the failure entries more consistent with thesuccess
entriesI don't have a strong opinion either way here, so I'm fine with whatever you decide. This is something that's easily added later on if we want it.
edit: I missed an
args
/argument
pun opportunity here