-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Merged
Merged
RBAC - SecurityAuditLogger #19571
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9c3ce6d
Manually porting over the AuditLogger for use within the security audit
kobelb f4c1833
HasPrivileges now returns the user from the request
kobelb f041606
Has privileges returns username from privilegeCheck
kobelb 756d213
Adding first eventType to the security audit logger
kobelb 87c9325
Adding authorization success message
kobelb aba377a
Logging arguments when authorization success
kobelb 536933c
Fixing test description
kobelb 762833f
Merge branch 'rbac-phase-1' into security-audit-logger
kobelb a2b51b5
Logging args during audit failures
kobelb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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, args) { | ||
if (!this._enabled) { | ||
return; | ||
} | ||
|
||
this._auditLogger.log( | ||
'saved_objects_authorization_failure', | ||
`${username} unauthorized to ${action} ${types.join(',')}, missing ${missing.join(',')}`, | ||
{ | ||
username, | ||
action, | ||
types, | ||
missing, | ||
args | ||
} | ||
); | ||
} | ||
|
||
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, | ||
} | ||
); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
x-pack/plugins/security/server/lib/audit_logger.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* 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); | ||
}); | ||
|
||
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`]; | ||
const args = { | ||
'foo': 'bar', | ||
'baz': 'quz', | ||
}; | ||
|
||
securityAuditLogger.savedObjectsAuthorizationFailure(username, action, types, missing, args); | ||
|
||
expect(auditLogger.log).toHaveBeenCalledWith( | ||
'saved_objects_authorization_failure', | ||
expect.stringContaining(`${username} unauthorized to ${action}`), | ||
{ | ||
username, | ||
action, | ||
types, | ||
missing, | ||
args, | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
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', | ||
'baz': 'quz', | ||
}; | ||
|
||
securityAuditLogger.savedObjectsAuthorizationSuccess(username, action, types, args); | ||
|
||
expect(auditLogger.log).toHaveBeenCalledWith( | ||
'saved_objects_authorization_success', | ||
expect.stringContaining(`${username} authorized to ${action}`), | ||
{ | ||
username, | ||
action, | ||
types, | ||
args, | ||
} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This feels fragile to me. I think it's strange that we have to rely on the
securityAuditLogger
using a dependency in a very particular way in order to verify that thesecurityAuditLogger
works as intended.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 comment
The 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.