Skip to content

Commit d26cde4

Browse files
committed
Create default audit log appender
1 parent 59815a0 commit d26cde4

File tree

2 files changed

+89
-37
lines changed

2 files changed

+89
-37
lines changed

x-pack/plugins/security/server/config.test.ts

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ jest.mock('crypto', () => ({
1010
constants: jest.requireActual('crypto').constants,
1111
}));
1212

13+
jest.mock('@kbn/utils', () => ({
14+
getDataPath: () => '/mock/kibana/data/path',
15+
}));
16+
1317
import { loggingSystemMock } from 'src/core/server/mocks';
1418

1519
import { ConfigSchema, createConfig } from './config';
@@ -1706,6 +1710,50 @@ describe('createConfig()', () => {
17061710
`);
17071711
});
17081712

1713+
it('creates a default audit appender when audit logging is enabled', () => {
1714+
expect(
1715+
createConfig(
1716+
ConfigSchema.validate({
1717+
audit: {
1718+
enabled: true,
1719+
},
1720+
}),
1721+
loggingSystemMock.create().get(),
1722+
{ isTLSEnabled: true }
1723+
).audit.appender
1724+
).toMatchInlineSnapshot(`
1725+
Object {
1726+
"fileName": "/mock/kibana/data/path/audit.log",
1727+
"layout": Object {
1728+
"type": "json",
1729+
},
1730+
"policy": Object {
1731+
"interval": "PT24H",
1732+
"type": "time-interval",
1733+
},
1734+
"strategy": Object {
1735+
"max": 10,
1736+
"type": "numeric",
1737+
},
1738+
"type": "rolling-file",
1739+
}
1740+
`);
1741+
});
1742+
1743+
it('does not create a default audit appender when audit logging is disabled', () => {
1744+
expect(
1745+
createConfig(
1746+
ConfigSchema.validate({
1747+
audit: {
1748+
enabled: false,
1749+
},
1750+
}),
1751+
loggingSystemMock.create().get(),
1752+
{ isTLSEnabled: true }
1753+
).audit.appender
1754+
).toBeUndefined();
1755+
});
1756+
17091757
it('accepts an audit appender', () => {
17101758
expect(
17111759
ConfigSchema.validate({
@@ -1744,19 +1792,6 @@ describe('createConfig()', () => {
17441792
).toThrow('[audit.appender.1.layout]: expected at least one defined value but got [undefined]');
17451793
});
17461794

1747-
it('rejects an ignore_filter when no appender is configured', () => {
1748-
expect(() =>
1749-
ConfigSchema.validate({
1750-
audit: {
1751-
enabled: true,
1752-
ignore_filters: [{ actions: ['some_action'] }],
1753-
},
1754-
})
1755-
).toThrow(
1756-
'[audit]: xpack.security.audit.ignore_filters can only be used with the ECS audit logger. To enable the ECS audit logger, specify where you want to write the audit events using xpack.security.audit.appender.'
1757-
);
1758-
});
1759-
17601795
describe('#getExpirationTimeouts', () => {
17611796
function createMockConfig(config: Record<string, any> = {}) {
17621797
return createConfig(ConfigSchema.validate(config), loggingSystemMock.createLogger(), {

x-pack/plugins/security/server/config.ts

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
import crypto from 'crypto';
99
import type { Duration } from 'moment';
10+
import path from 'path';
1011

1112
import type { Type, TypeOf } from '@kbn/config-schema';
1213
import { schema } from '@kbn/config-schema';
1314
import { i18n } from '@kbn/i18n';
15+
import { getDataPath } from '@kbn/utils';
1416
import type { Logger } from 'src/core/server';
1517

1618
import { config as coreConfig } from '../../../../src/core/server';
@@ -272,30 +274,21 @@ export const ConfigSchema = schema.object({
272274
schemes: schema.arrayOf(schema.string(), { defaultValue: ['apikey', 'bearer'] }),
273275
}),
274276
}),
275-
audit: schema.object(
276-
{
277-
enabled: schema.boolean({ defaultValue: false }),
278-
appender: schema.maybe(coreConfig.logging.appenders),
279-
ignore_filters: schema.maybe(
280-
schema.arrayOf(
281-
schema.object({
282-
actions: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
283-
categories: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
284-
types: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
285-
outcomes: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
286-
spaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
287-
})
288-
)
289-
),
290-
},
291-
{
292-
validate: (auditConfig) => {
293-
if (auditConfig.ignore_filters && !auditConfig.appender) {
294-
return 'xpack.security.audit.ignore_filters can only be used with the ECS audit logger. To enable the ECS audit logger, specify where you want to write the audit events using xpack.security.audit.appender.';
295-
}
296-
},
297-
}
298-
),
277+
audit: schema.object({
278+
enabled: schema.boolean({ defaultValue: false }),
279+
appender: schema.maybe(coreConfig.logging.appenders),
280+
ignore_filters: schema.maybe(
281+
schema.arrayOf(
282+
schema.object({
283+
actions: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
284+
categories: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
285+
types: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
286+
outcomes: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
287+
spaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
288+
})
289+
)
290+
),
291+
}),
299292
});
300293

301294
export function createConfig(
@@ -382,8 +375,32 @@ export function createConfig(
382375
sortedProviders.filter(({ type, name }) => providers[type]?.[name].showInSelector).length >
383376
1;
384377

378+
const auditLoggingEnabled = config.audit.enabled === true;
379+
const appender =
380+
config.audit.appender ?? auditLoggingEnabled
381+
? {
382+
type: 'rolling-file',
383+
fileName: path.join(getDataPath(), 'audit.log'),
384+
layout: {
385+
type: 'json',
386+
},
387+
policy: {
388+
type: 'time-interval',
389+
interval: schema.duration().validate('24h'),
390+
},
391+
strategy: {
392+
type: 'numeric',
393+
max: 10,
394+
},
395+
}
396+
: undefined;
397+
385398
return {
386399
...config,
400+
audit: {
401+
...config.audit,
402+
appender,
403+
},
387404
authc: {
388405
selector: { ...config.authc.selector, enabled: isLoginSelectorEnabled },
389406
providers,

0 commit comments

Comments
 (0)