Skip to content

Commit

Permalink
Merge branch 'main' into task/update_list_api_summary_endpoint_to_use…
Browse files Browse the repository at this point in the history
…_filter
  • Loading branch information
kibanamachine authored Jan 25, 2022
2 parents 140ef7f + 607feec commit e24628d
Show file tree
Hide file tree
Showing 36 changed files with 107 additions and 197 deletions.
6 changes: 5 additions & 1 deletion examples/locator_explorer/public/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ const ActionsExplorer = ({ share }: Props) => {
if (!locator) return;
let params: HelloLocatorV1Params | HelloLocatorV2Params = savedLink.params;
if (savedLink.version === '0.0.1') {
const migration = locator.migrations['0.0.2'];
const migrations =
typeof locator.migrations === 'function'
? locator.migrations()
: locator.migrations || {};
const migration = migrations['0.0.2'];
if (migration) {
params = migration(params) as HelloLocatorV2Params;
}
Expand Down
14 changes: 12 additions & 2 deletions src/plugins/embeddable/common/lib/get_all_migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
import { getAllMigrations } from './get_all_migrations';

describe('embeddable getAllMigratons', () => {
const factories = [{ migrations: { '7.11.0': (state: unknown) => state } }];
const enhacements = [{ migrations: { '7.12.0': (state: unknown) => state } }];
const factories = [
{ migrations: { '7.11.0': (state: unknown) => state } },
{ migrations: () => ({ '7.13.0': (state: unknown) => state }) },
];
const enhacements = [
{ migrations: { '7.12.0': (state: unknown) => state } },
{ migrations: () => ({ '7.14.0': (state: unknown) => state }) },
];
const migrateFn = jest.fn();

test('returns base migrations', () => {
Expand All @@ -19,16 +25,20 @@ describe('embeddable getAllMigratons', () => {

test('returns embeddable factory migrations', () => {
expect(getAllMigrations(factories, [], migrateFn)).toHaveProperty(['7.11.0']);
expect(getAllMigrations(factories, [], migrateFn)).toHaveProperty(['7.13.0']);
});

test('returns enhancement migrations', () => {
const migrations = getAllMigrations([], enhacements, migrateFn);
expect(migrations).toHaveProperty(['7.12.0']);
expect(migrations).toHaveProperty(['7.14.0']);
});

test('returns all migrations', () => {
const migrations = getAllMigrations(factories, enhacements, migrateFn);
expect(migrations).toHaveProperty(['7.11.0']);
expect(migrations).toHaveProperty(['7.12.0']);
expect(migrations).toHaveProperty(['7.13.0']);
expect(migrations).toHaveProperty(['7.14.0']);
});
});
12 changes: 6 additions & 6 deletions src/plugins/embeddable/common/lib/get_all_migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ export const getAllMigrations = (
uniqueVersions.add(baseMigrationVersion);
}
for (const factory of factories) {
Object.keys((factory as PersistableState).migrations).forEach((version) =>
uniqueVersions.add(version)
);
const migrations = (factory as PersistableState).migrations;
const factoryMigrations = typeof migrations === 'function' ? migrations() : migrations;
Object.keys(factoryMigrations).forEach((version) => uniqueVersions.add(version));
}
for (const enhancement of enhancements) {
Object.keys((enhancement as PersistableState).migrations).forEach((version) =>
uniqueVersions.add(version)
);
const migrations = (enhancement as PersistableState).migrations;
const enhancementMigrations = typeof migrations === 'function' ? migrations() : migrations;
Object.keys(enhancementMigrations).forEach((version) => uniqueVersions.add(version));
}

const migrations: MigrateFunctionsObject = {};
Expand Down
14 changes: 10 additions & 4 deletions src/plugins/embeddable/common/lib/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ export const getMigrateFunction = (embeddables: CommonEmbeddableStartContract) =
? baseEmbeddableMigrations[version](state)
: state;

if (factory?.migrations[version]) {
updatedInput = factory.migrations[version](updatedInput);
const factoryMigrations =
typeof factory?.migrations === 'function' ? factory?.migrations() : factory?.migrations || {};
if (factoryMigrations[version]) {
updatedInput = factoryMigrations[version](updatedInput);
}

if (factory?.isContainerType) {
Expand All @@ -35,8 +37,12 @@ export const getMigrateFunction = (embeddables: CommonEmbeddableStartContract) =
Object.keys(enhancements).forEach((key) => {
if (!enhancements[key]) return;
const enhancementDefinition = embeddables.getEnhancement(key);
const migratedEnhancement = enhancementDefinition?.migrations?.[version]
? enhancementDefinition.migrations[version](enhancements[key] as SerializableRecord)
const enchantmentMigrations =
typeof enhancementDefinition?.migrations === 'function'
? enhancementDefinition?.migrations()
: enhancementDefinition?.migrations || {};
const migratedEnhancement = enchantmentMigrations[version]
? enchantmentMigrations[version](enhancements[key] as SerializableRecord)
: enhancements[key];
(updatedInput.enhancements! as Record<string, {}>)[key] = migratedEnhancement;
});
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/expressions/common/executor/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,13 @@ export class Executor<Context extends Record<string, unknown> = Record<string, u

private migrate(ast: SerializableRecord, version: string) {
return this.walkAstAndTransform(cloneDeep(ast) as ExpressionAstExpression, (fn, link) => {
if (!fn.migrations[version]) {
const migrations =
typeof fn.migrations === 'function' ? fn.migrations() : fn.migrations || {};
if (!migrations[version]) {
return link;
}

return fn.migrations[version](link) as ExpressionAstExpression;
return migrations[version](link) as ExpressionAstExpression;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
*/

import { identity } from 'lodash';
import type { SerializableRecord } from '@kbn/utility-types';
import { AnyExpressionFunctionDefinition } from './types';
import { ExpressionFunctionParameter } from './expression_function_parameter';
import { ExpressionValue } from '../expression_types/types';
import { ExpressionAstFunction } from '../ast';
import { SavedObjectReference } from '../../../../core/types';
import { PersistableState } from '../../../kibana_utils/common';
import {
MigrateFunctionsObject,
GetMigrationFunctionObjectFn,
PersistableState,
} from '../../../kibana_utils/common';

export class ExpressionFunction implements PersistableState<ExpressionAstFunction['arguments']> {
/**
Expand Down Expand Up @@ -70,9 +73,7 @@ export class ExpressionFunction implements PersistableState<ExpressionAstFunctio
state: ExpressionAstFunction['arguments'],
references: SavedObjectReference[]
) => ExpressionAstFunction['arguments'];
migrations: {
[key: string]: (state: SerializableRecord) => SerializableRecord;
};
migrations: MigrateFunctionsObject | GetMigrationFunctionObjectFn;

constructor(functionDefinition: AnyExpressionFunctionDefinition) {
const {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/kibana_utils/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export type {
PersistableStateMigrateFn,
MigrateFunction,
MigrateFunctionsObject,
GetMigrationFunctionObjectFn,
PersistableState,
PersistableStateDefinition,
} from './persistable_state';
Expand Down
1 change: 1 addition & 0 deletions src/plugins/kibana_utils/common/persistable_state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type {
PersistableStateService,
MigrateFunctionsObject,
MigrateFunction,
GetMigrationFunctionObjectFn,
} from './types';
export { migrateToLatest } from './migrate_to_latest';
export { mergeMigrationFunctionMaps } from './merge_migration_function_map';
4 changes: 3 additions & 1 deletion src/plugins/kibana_utils/common/persistable_state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ export interface PersistableState<P extends SerializableRecord = SerializableRec
* keyed by the Kibana version using semver, where the version indicates to
* which version the state will be migrated to.
*/
migrations: MigrateFunctionsObject;
migrations: MigrateFunctionsObject | GetMigrationFunctionObjectFn;
}

export type GetMigrationFunctionObjectFn = () => MigrateFunctionsObject;

/**
* Collection of migrations that a given type of persistable state object has
* accumulated over time. Migration functions are keyed using semver version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class LocatorClient implements ILocatorClient {
const migrations: { [locatorId: string]: MigrateFunctionsObject } = {};

for (const locator of this.locators.values()) {
migrations[locator.id] = locator.migrations;
migrations[locator.id] =
typeof locator.migrations === 'function' ? locator.migrations() : locator.migrations;
}

return migrations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export class RedirectManager {
throw error;
}

const migratedParams = migrateToLatest(locator.migrations, {
const locatorMigrations =
typeof locator.migrations === 'function' ? locator.migrations() : locator.migrations;
const migratedParams = migrateToLatest(locatorMigrations, {
state: options.params,
version: options.version,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('filters migrations', () => {
const expression = 'filters group="1" group="3" ungrouped=true';
const ast = fromExpression(expression);
it('8.1.0. Should migrate `filters` expression to `kibana | selectFilter`', () => {
const migratedAst = migrations?.['8.1.0'](ast.chain[0]);
const migrationObj = typeof migrations === 'function' ? migrations() : migrations || {};
const migratedAst = migrationObj['8.1.0'](ast.chain[0]);
expect(migratedAst !== null && typeof migratedAst === 'object').toBeTruthy();
expect(migratedAst.type).toBe('expression');
expect(Array.isArray(migratedAst.chain)).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ describe('audit_logger', () => {
describe('log function', () => {
const mockLogger: jest.Mocked<AuditLogger> = {
log: jest.fn(),
enabled: true,
};

let logger: AuthorizationAuditLogger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe('authorization', () => {
request = httpServerMock.createKibanaRequest();
mockLogger = {
log: jest.fn(),
enabled: true,
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ export interface CreateCommentsMigrationsDeps {
export const createCommentsMigrations = (
migrationDeps: CreateCommentsMigrationsDeps
): SavedObjectMigrationMap => {
const lensMigrations = migrationDeps.lensEmbeddableFactory().migrations;
const lensMigrationObject =
typeof lensMigrations === 'function' ? lensMigrations() : lensMigrations || {};
const embeddableMigrations = mapValues<
MigrateFunctionsObject,
SavedObjectMigrationFn<{ comment?: string }>
>(
migrationDeps.lensEmbeddableFactory().migrations,
migrateByValueLensVisualizations
) as MigrateFunctionsObject;
>(lensMigrationObject, migrateByValueLensVisualizations) as MigrateFunctionsObject;

const commentsMigrations = {
'7.11.0': (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ describe('embeddable migrations', () => {
},
})()?.migrations;

const migratedLensDoc = embeddableMigrationVersions?.[migrationVersion](lensVisualizationDoc);
const migrations =
typeof embeddableMigrationVersions === 'function'
? embeddableMigrationVersions()
: embeddableMigrationVersions || {};
const migratedLensDoc = migrations[migrationVersion](lensVisualizationDoc);

expect(migratedLensDoc).toEqual({
attributes: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const fakeRequest = {

const auditLogger = {
log: jest.fn(),
enabled: true,
} as jest.Mocked<AuditLogger>;

describe('AlertsClientFactory', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const alertingAuthMock = alertingAuthorizationMock.create();
const esClientMock = elasticsearchClientMock.createElasticsearchClient();
const auditLogger = {
log: jest.fn(),
enabled: true,
} as jest.Mocked<AuditLogger>;

const alertsClientParams: jest.Mocked<ConstructorOptions> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const alertingAuthMock = alertingAuthorizationMock.create();
const esClientMock = elasticsearchClientMock.createElasticsearchClient();
const auditLogger = {
log: jest.fn(),
enabled: true,
} as jest.Mocked<AuditLogger>;

const alertsClientParams: jest.Mocked<ConstructorOptions> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const alertingAuthMock = alertingAuthorizationMock.create();
const esClientMock = elasticsearchClientMock.createElasticsearchClient();
const auditLogger = {
log: jest.fn(),
enabled: true,
} as jest.Mocked<AuditLogger>;

const alertsClientParams: jest.Mocked<ConstructorOptions> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const alertingAuthMock = alertingAuthorizationMock.create();
const esClientMock = elasticsearchClientMock.createElasticsearchClient();
const auditLogger = {
log: jest.fn(),
enabled: true,
} as jest.Mocked<AuditLogger>;

const alertsClientParams: jest.Mocked<ConstructorOptions> = {
Expand Down
6 changes: 6 additions & 0 deletions x-pack/plugins/security/server/audit/audit_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ export enum SavedObjectAction {
UPDATE = 'saved_object_update',
DELETE = 'saved_object_delete',
FIND = 'saved_object_find',
ADD_TO_SPACES = 'saved_object_add_to_spaces',
DELETE_FROM_SPACES = 'saved_object_delete_from_spaces',
REMOVE_REFERENCES = 'saved_object_remove_references',
OPEN_POINT_IN_TIME = 'saved_object_open_point_in_time',
CLOSE_POINT_IN_TIME = 'saved_object_close_point_in_time',
Expand All @@ -234,6 +236,8 @@ const savedObjectAuditVerbs: Record<SavedObjectAction, VerbsTuple> = {
saved_object_update: ['update', 'updating', 'updated'],
saved_object_delete: ['delete', 'deleting', 'deleted'],
saved_object_find: ['access', 'accessing', 'accessed'],
saved_object_add_to_spaces: ['update', 'updating', 'updated'],
saved_object_delete_from_spaces: ['update', 'updating', 'updated'],
saved_object_open_point_in_time: [
'open point-in-time',
'opening point-in-time',
Expand Down Expand Up @@ -268,6 +272,8 @@ const savedObjectAuditTypes: Record<SavedObjectAction, EcsEventType> = {
saved_object_update: 'change',
saved_object_delete: 'deletion',
saved_object_find: 'access',
saved_object_add_to_spaces: 'change',
saved_object_delete_from_spaces: 'change',
saved_object_open_point_in_time: 'creation',
saved_object_close_point_in_time: 'deletion',
saved_object_remove_references: 'change',
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/security/server/audit/audit_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ describe('#setup', () => {
Object {
"asScoped": [Function],
"withoutRequest": Object {
"enabled": true,
"log": [Function],
},
}
Expand Down
14 changes: 2 additions & 12 deletions x-pack/plugins/security/server/audit/audit_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ export interface AuditLogger {
* ```
*/
log: (event: AuditEvent | undefined) => void;

/**
* Indicates whether audit logging is enabled or not.
*
* Useful for skipping resource-intense operations that don't need to be performed when audit
* logging is disabled.
*/
readonly enabled: boolean;
}

export interface AuditServiceSetup {
Expand Down Expand Up @@ -130,8 +122,7 @@ export class AuditService {
);

// Record feature usage at a regular interval if enabled and license allows
const enabled = !!(config.enabled && config.appender);
if (enabled) {
if (config.enabled && config.appender) {
license.features$.subscribe((features) => {
clearInterval(this.usageIntervalId!);
if (features.allowAuditLogging) {
Expand Down Expand Up @@ -178,7 +169,6 @@ export class AuditService {
trace: { id: request.id },
});
},
enabled,
});

http.registerOnPostAuth((request, response, t) => {
Expand All @@ -190,7 +180,7 @@ export class AuditService {

return {
asScoped,
withoutRequest: { log, enabled },
withoutRequest: { log },
};
}

Expand Down
2 changes: 0 additions & 2 deletions x-pack/plugins/security/server/audit/index.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ export const auditServiceMock = {
getLogger: jest.fn(),
asScoped: jest.fn().mockReturnValue({
log: jest.fn(),
enabled: true,
}),
withoutRequest: {
log: jest.fn(),
enabled: true,
},
} as jest.Mocked<ReturnType<AuditService['setup']>>;
},
Expand Down
Loading

0 comments on commit e24628d

Please sign in to comment.