Skip to content
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

Fixing the spaces audit logger when security is explicitly disabled #23878

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion x-pack/plugins/spaces/server/lib/audit_logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
*/
import { SpacesAuditLogger } from './audit_logger';

const createMockConfig = (settings: any) => {
const createMockConfig = (settings: { [key: string]: any } = {}) => {
const mockConfig = {
get: jest.fn(),
};

mockConfig.get.mockImplementation(key => {
if (!settings.hasOwnProperty(key)) {
throw new Error('Undefined key, mock schema error');
}

return settings[key];
});

Expand All @@ -24,8 +28,21 @@ const createMockAuditLogger = () => {
};

describe(`#savedObjectsAuthorizationFailure`, () => {
test(`doesn't log anything when xpack.security.enabled is false`, () => {
const config = createMockConfig({
'xpack.security.enabled': false,
});
const auditLogger = createMockAuditLogger();

const securityAuditLogger = new SpacesAuditLogger(config, auditLogger);
securityAuditLogger.spacesAuthorizationFailure('foo-user', 'foo-action');

expect(auditLogger.log).toHaveBeenCalledTimes(0);
});

test(`doesn't log anything when xpack.security.audit.enabled is false`, () => {
const config = createMockConfig({
'xpack.security.enabled': true,
'xpack.security.audit.enabled': false,
});
const auditLogger = createMockAuditLogger();
Expand All @@ -38,6 +55,7 @@ describe(`#savedObjectsAuthorizationFailure`, () => {

test('logs with spaceIds via auditLogger when xpack.security.audit.enabled is true', () => {
const config = createMockConfig({
'xpack.security.enabled': true,
'xpack.security.audit.enabled': true,
});
const auditLogger = createMockAuditLogger();
Expand All @@ -61,6 +79,7 @@ describe(`#savedObjectsAuthorizationFailure`, () => {

test('logs without spaceIds via auditLogger when xpack.security.audit.enabled is true', () => {
const config = createMockConfig({
'xpack.security.enabled': true,
'xpack.security.audit.enabled': true,
});
const auditLogger = createMockAuditLogger();
Expand All @@ -82,8 +101,21 @@ describe(`#savedObjectsAuthorizationFailure`, () => {
});

describe(`#savedObjectsAuthorizationSuccess`, () => {
test(`doesn't log anything when xpack.security.enabled is false`, () => {
const config = createMockConfig({
'xpack.security.enabled': false,
});
const auditLogger = createMockAuditLogger();

const securityAuditLogger = new SpacesAuditLogger(config, auditLogger);
securityAuditLogger.spacesAuthorizationSuccess('foo-user', 'foo-action');

expect(auditLogger.log).toHaveBeenCalledTimes(0);
});

test(`doesn't log anything when xpack.security.audit.enabled is false`, () => {
const config = createMockConfig({
'xpack.security.enabled': true,
'xpack.security.audit.enabled': false,
});
const auditLogger = createMockAuditLogger();
Expand All @@ -96,6 +128,7 @@ describe(`#savedObjectsAuthorizationSuccess`, () => {

test('logs with spaceIds via auditLogger when xpack.security.audit.enabled is true', () => {
const config = createMockConfig({
'xpack.security.enabled': true,
'xpack.security.audit.enabled': true,
});
const auditLogger = createMockAuditLogger();
Expand All @@ -119,6 +152,7 @@ describe(`#savedObjectsAuthorizationSuccess`, () => {

test('logs without spaceIds via auditLogger when xpack.security.audit.enabled is true', () => {
const config = createMockConfig({
'xpack.security.enabled': true,
'xpack.security.audit.enabled': true,
});
const auditLogger = createMockAuditLogger();
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/spaces/server/lib/audit_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export class SpacesAuditLogger {
private readonly auditLogger: any;

constructor(config: any, auditLogger: any) {
this.enabled = config.get('xpack.security.audit.enabled');
this.enabled =
config.get('xpack.security.enabled') && config.get('xpack.security.audit.enabled');
this.auditLogger = auditLogger;
}
public spacesAuthorizationFailure(username: string, action: string, spaceIds?: string[]) {
Expand Down