forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RBAC - SecurityAuditLogger (elastic#19571)
* Manually porting over the AuditLogger for use within the security audit logger * HasPrivileges now returns the user from the request * Has privileges returns username from privilegeCheck * Adding first eventType to the security audit logger * Adding authorization success message * Logging arguments when authorization success * Fixing test description * Logging args during audit failures
- Loading branch information
Showing
7 changed files
with
241 additions
and
24 deletions.
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