Skip to content

Commit

Permalink
add license check
Browse files Browse the repository at this point in the history
  • Loading branch information
thomheymann committed Aug 25, 2020
1 parent 0bfd19a commit 707ab34
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
47 changes: 47 additions & 0 deletions x-pack/plugins/audit_trail/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { BehaviorSubject } from 'rxjs';
import { first } from 'rxjs/operators';
import { AuditTrailPlugin } from './plugin';
import { coreMock } from '../../../../src/core/server/mocks';
Expand All @@ -26,6 +27,18 @@ describe('AuditTrail plugin', () => {
pluginInitContextMock = coreMock.createPluginInitializerContext();
plugin = new AuditTrailPlugin(pluginInitContextMock);
coreSetup = coreMock.createSetup();
deps.security.license.features$ = new BehaviorSubject({
showLogin: true,
allowLogin: true,
showLinks: true,
showRoleMappingsManagement: true,
allowAccessAgreement: true,
allowAuditLogging: true,
allowRoleDocumentLevelSecurity: true,
allowRoleFieldLevelSecurity: true,
allowRbac: true,
allowSubFeaturePrivileges: true,
});
});

afterEach(async () => {
Expand All @@ -39,6 +52,40 @@ describe('AuditTrail plugin', () => {
expect(coreSetup.auditTrail.register).toHaveBeenCalledTimes(1);
});

it('logs to audit trail if license allows', async () => {
pluginInitContextMock = coreMock.createPluginInitializerContext();
plugin = new AuditTrailPlugin(pluginInitContextMock);

const subscribeMock = jest.spyOn((plugin as any).event$, 'subscribe');

plugin.setup(coreSetup, deps);

expect(subscribeMock).toHaveBeenCalled();
});

it('does not log to audit trail if license does not allow', async () => {
pluginInitContextMock = coreMock.createPluginInitializerContext();
plugin = new AuditTrailPlugin(pluginInitContextMock);

const subscribeMock = jest.spyOn((plugin as any).event$, 'subscribe');

deps.security.license.features$ = new BehaviorSubject({
showLogin: true,
allowLogin: true,
showLinks: true,
showRoleMappingsManagement: true,
allowAccessAgreement: true,
allowAuditLogging: false,
allowRoleDocumentLevelSecurity: true,
allowRoleFieldLevelSecurity: true,
allowRbac: true,
allowSubFeaturePrivileges: true,
});

plugin.setup(coreSetup, deps);
expect(subscribeMock).not.toHaveBeenCalled();
});

describe('logger', () => {
it('registers a custom logger', async () => {
pluginInitContextMock = coreMock.createPluginInitializerContext();
Expand Down
16 changes: 14 additions & 2 deletions x-pack/plugins/audit_trail/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Observable, Subject } from 'rxjs';
import { Observable, Subject, Subscription } from 'rxjs';
import { map } from 'rxjs/operators';
import {
AppenderConfigType,
Expand Down Expand Up @@ -50,7 +50,19 @@ export class AuditTrailPlugin implements Plugin {
getSpaceId: deps.spaces?.spacesService.getSpaceId,
};

this.event$.subscribe(({ message, ...other }) => this.logger.debug(message, other));
if (deps.security) {
let subscription: Subscription;
deps.security.license.features$.subscribe(({ allowAuditLogging }) => {
if (subscription && !subscription.closed) {
subscription.unsubscribe();
}
if (allowAuditLogging) {
subscription = this.event$
.asObservable()
.subscribe(({ message, ...other }) => this.logger.debug(message, other));
}
});
}

core.auditTrail.register({
asScoped: (request: KibanaRequest) => {
Expand Down

0 comments on commit 707ab34

Please sign in to comment.