From d46227421e361fd5ea29cbb6e512c9df66448d5e Mon Sep 17 00:00:00 2001 From: Paul Tavares <56442535+paul-tavares@users.noreply.github.com> Date: Wed, 19 Aug 2020 14:32:43 -0400 Subject: [PATCH] [SECURITY_SOLUTION][ENDPOINT] Add creation of Trusted Apps Agnostic List (#74868) * Add method to ExceptionsListClient for creating trusted apps list --- x-pack/plugins/lists/common/constants.ts | 9 +++ .../create_endpoint_trusted_apps_list.ts | 77 +++++++++++++++++++ .../exception_lists/exception_list_client.ts | 13 ++++ 3 files changed, 99 insertions(+) create mode 100644 x-pack/plugins/lists/server/services/exception_lists/create_endpoint_trusted_apps_list.ts diff --git a/x-pack/plugins/lists/common/constants.ts b/x-pack/plugins/lists/common/constants.ts index 6c73dc165630..1851487b824a 100644 --- a/x-pack/plugins/lists/common/constants.ts +++ b/x-pack/plugins/lists/common/constants.ts @@ -50,3 +50,12 @@ export const ENDPOINT_LIST_NAME = 'Elastic Endpoint Security Exception List'; export const ENDPOINT_LIST_DESCRIPTION = 'Elastic Endpoint Security Exception List'; export const MAX_EXCEPTION_LIST_SIZE = 10000; + +/** ID of trusted apps agnostic list */ +export const ENDPOINT_TRUSTED_APPS_LIST_ID = 'endpoint_trusted_apps'; + +/** Name of trusted apps agnostic list */ +export const ENDPOINT_TRUSTED_APPS_LIST_NAME = 'Elastic Endpoint Security Trusted Apps List'; + +/** Description of trusted apps agnostic list */ +export const ENDPOINT_TRUSTED_APPS_LIST_DESCRIPTION = 'Elastic Endpoint Security Trusted Apps List'; diff --git a/x-pack/plugins/lists/server/services/exception_lists/create_endpoint_trusted_apps_list.ts b/x-pack/plugins/lists/server/services/exception_lists/create_endpoint_trusted_apps_list.ts new file mode 100644 index 000000000000..c782cdd30266 --- /dev/null +++ b/x-pack/plugins/lists/server/services/exception_lists/create_endpoint_trusted_apps_list.ts @@ -0,0 +1,77 @@ +/* + * 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 { SavedObjectsClientContract } from 'kibana/server'; +import uuid from 'uuid'; + +import { + ENDPOINT_TRUSTED_APPS_LIST_DESCRIPTION, + ENDPOINT_TRUSTED_APPS_LIST_ID, + ENDPOINT_TRUSTED_APPS_LIST_NAME, +} from '../../../common/constants'; +import { ExceptionListSchema, ExceptionListSoSchema, Version } from '../../../common/schemas'; + +import { getSavedObjectType, transformSavedObjectToExceptionList } from './utils'; + +interface CreateEndpointListOptions { + savedObjectsClient: SavedObjectsClientContract; + user: string; + tieBreaker?: string; + version: Version; +} + +/** + * Creates the Endpoint Trusted Apps agnostic list if it does not yet exist + * + * @param savedObjectsClient + * @param user + * @param tieBreaker + * @param version + */ +export const createEndpointTrustedAppsList = async ({ + savedObjectsClient, + user, + tieBreaker, + version, +}: CreateEndpointListOptions): Promise => { + const savedObjectType = getSavedObjectType({ namespaceType: 'agnostic' }); + const dateNow = new Date().toISOString(); + try { + const savedObject = await savedObjectsClient.create( + savedObjectType, + { + _tags: [], + comments: undefined, + created_at: dateNow, + created_by: user, + description: ENDPOINT_TRUSTED_APPS_LIST_DESCRIPTION, + entries: undefined, + immutable: false, + item_id: undefined, + list_id: ENDPOINT_TRUSTED_APPS_LIST_ID, + list_type: 'list', + meta: undefined, + name: ENDPOINT_TRUSTED_APPS_LIST_NAME, + tags: [], + tie_breaker_id: tieBreaker ?? uuid.v4(), + type: 'endpoint', + updated_by: user, + version, + }, + { + // We intentionally hard coding the id so that there can only be one Trusted apps list within the space + id: ENDPOINT_TRUSTED_APPS_LIST_ID, + } + ); + return transformSavedObjectToExceptionList({ savedObject }); + } catch (err) { + if (savedObjectsClient.errors.isConflictError(err)) { + return null; + } else { + throw err; + } + } +}; diff --git a/x-pack/plugins/lists/server/services/exception_lists/exception_list_client.ts b/x-pack/plugins/lists/server/services/exception_lists/exception_list_client.ts index 83b44ababf9d..747458175e3b 100644 --- a/x-pack/plugins/lists/server/services/exception_lists/exception_list_client.ts +++ b/x-pack/plugins/lists/server/services/exception_lists/exception_list_client.ts @@ -46,6 +46,7 @@ import { findExceptionListItem } from './find_exception_list_item'; import { findExceptionList } from './find_exception_list'; import { findExceptionListsItem } from './find_exception_list_items'; import { createEndpointList } from './create_endpoint_list'; +import { createEndpointTrustedAppsList } from './create_endpoint_trusted_apps_list'; export class ExceptionListClient { private readonly user: string; @@ -90,6 +91,18 @@ export class ExceptionListClient { }); }; + /** + * Create the Trusted Apps Agnostic list if it does not yet exist (`null` is returned if it does exist) + */ + public createTrustedAppsList = async (): Promise => { + const { savedObjectsClient, user } = this; + return createEndpointTrustedAppsList({ + savedObjectsClient, + user, + version: 1, + }); + }; + /** * This is the same as "createListItem" except it applies specifically to the agnostic endpoint list and will * auto-call the "createEndpointList" for you so that you have the best chance of the agnostic endpoint