import { SavedObjectsClientContract } from 'kibana/server' ; import { ExceptionListItemSchema , ExceptionListSchema , ExceptionListSummarySchema , FoundExceptionListItemSchema , FoundExceptionListSchema , ImportExceptionsResponseSchema , createExceptionListItemSchema , updateExceptionListItemSchema , } from '@kbn/securitysolution-io-ts-list-types' ; import { ENDPOINT_LIST_ID } from '@kbn/securitysolution-list-constants' ; import type { ExtensionPointStorageClientInterface } from '../extension_points' ; import { ConstructorOptions , CreateEndpointListItemOptions , CreateExceptionListItemOptions , CreateExceptionListOptions , DeleteEndpointListItemOptions , DeleteExceptionListItemByIdOptions , DeleteExceptionListItemOptions , DeleteExceptionListOptions , ExportExceptionListAndItemsOptions , FindEndpointListItemOptions , FindExceptionListItemOptions , FindExceptionListOptions , FindExceptionListsItemOptions , FindValueListExceptionListsItems , GetEndpointListItemOptions , GetExceptionListItemOptions , GetExceptionListOptions , GetExceptionListSummaryOptions , ImportExceptionListAndItemsAsArrayOptions , ImportExceptionListAndItemsOptions , UpdateEndpointListItemOptions , UpdateExceptionListItemOptions , UpdateExceptionListOptions , } from './exception_list_client_types' ; import { getExceptionList } from './get_exception_list' ; import { ExportExceptionListAndItemsReturn , exportExceptionListAndItems , } from './export_exception_list_and_items' ; import { getExceptionListSummary } from './get_exception_list_summary' ; import { createExceptionList } from './create_exception_list' ; import { getExceptionListItem } from './get_exception_list_item' ; import { createExceptionListItem } from './create_exception_list_item' ; import { updateExceptionList } from './update_exception_list' ; import { updateExceptionListItem } from './update_exception_list_item' ; import { deleteExceptionList } from './delete_exception_list' ; import { deleteExceptionListItem , deleteExceptionListItemById } from './delete_exception_list_item' ; import { findExceptionListItem } from './find_exception_list_item' ; import { findExceptionList } from './find_exception_list' ; import { findExceptionListsItem , findValueListExceptionListItems , } from './find_exception_list_items' ; import { createEndpointList } from './create_endpoint_list' ; import { createEndpointTrustedAppsList } from './create_endpoint_trusted_apps_list' ; import { importExceptionsAsArray , importExceptionsAsStream , } from './import_exception_list_and_items' ; import { transformCreateExceptionListItemOptionsToCreateExceptionListItemSchema , transformUpdateExceptionListItemOptionsToUpdateExceptionListItemSchema , validateData , } from './utils' ; export class ExceptionListClient { private readonly user : string ; private readonly savedObjectsClient : SavedObjectsClientContract ; private readonly serverExtensionsClient : ExtensionPointStorageClientInterface ; private readonly enableServerExtensionPoints : boolean ; constructor ( { user , savedObjectsClient , serverExtensionsClient , enableServerExtensionPoints = true , } ) { this . user = user ; this . savedObjectsClient = savedObjectsClient ; this . serverExtensionsClient = serverExtensionsClient ; this . enableServerExtensionPoints = enableServerExtensionPoints ; } params params params return { ExceptionListSchema | null } public getExceptionList = async ( { listId , id , namespaceType , } ) : Promise < ExceptionListSchema | null > => { const { savedObjectsClient } = this ; return getExceptionList ( { id , listId , namespaceType , savedObjectsClient } ) ; } ; params params params return { ExceptionListSummarySchema | null } public getExceptionListSummary = async ( { listId , id , namespaceType , } ) : Promise < ExceptionListSummarySchema | null > => { const { savedObjectsClient } = this ; return getExceptionListSummary ( { id , listId , namespaceType , savedObjectsClient } ) ; } ; params params params return { ExceptionListSummarySchema | null } public getExceptionListItem = async ( { itemId , id , namespaceType , } ) : Promise < ExceptionListItemSchema | null > => { const { savedObjectsClient } = this ; return getExceptionListItem ( { id , itemId , namespaceType , savedObjectsClient } ) ; } ; returns public createEndpointList = async ( ) : Promise < ExceptionListSchema | null > => { const { savedObjectsClient , user } = this ; return createEndpointList ( { savedObjectsClient , user , version : 1 , } ) ; } ; /**
     * Create the Trusted Apps Agnostic list if it does not yet exist (`null` is returned if it does exist)
     */ public createTrustedAppsList = async ( ) : Promise < ExceptionListSchema | null > => { 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
     * being there and existing before the item is inserted into the agnostic endpoint list.
     */ public createEndpointListItem = async ( { comments , description , entries , itemId , meta , name , osTypes , tags , type , } ) : Promise < ExceptionListItemSchema > => { const { savedObjectsClient , user } = this ; await this . createEndpointList ( ) ; return createExceptionListItem ( { comments , description , entries , itemId , listId : ENDPOINT_LIST_ID , meta , name , namespaceType : 'agnostic' , osTypes , savedObjectsClient , tags , type , user , } ) ; } ; /**
     * This is the same as "updateExceptionListItem" except it applies specifically to the endpoint list and will
     * auto-call the "createEndpointList" for you so that you have the best chance of the endpoint
     * being there if it did not exist before. If the list did not exist before, then creating it here will still cause a
     * return of null but at least the list exists again.
     */ public updateEndpointListItem = async ( { _version , comments , description , entries , id , itemId , meta , name , osTypes , tags , type , } ) : Promise < ExceptionListItemSchema | null > => { const { savedObjectsClient , user } = this ; await this . createEndpointList ( ) ; return updateExceptionListItem ( { _version , comments , description , entries , id , itemId , meta , name , namespaceType : 'agnostic' , osTypes , savedObjectsClient , tags , type , user , } ) ; } ; /**
     * This is the same as "getExceptionListItem" except it applies specifically to the endpoint list.
     */ public getEndpointListItem = async ( { itemId , id , } ) : Promise < ExceptionListItemSchema | null > => { const { savedObjectsClient } = this ; return getExceptionListItem ( { id , itemId , namespaceType : 'agnostic' , savedObjectsClient } ) ; } ; params params params params params params params params params return { ExceptionListSchema } public createExceptionList = async ( { description , immutable , listId , meta , name , namespaceType , tags , type , version , } ) : Promise < ExceptionListSchema > => { const { savedObjectsClient , user } = this ; return createExceptionList ( { description , immutable , listId , meta , name , namespaceType , savedObjectsClient , tags , type , user , version , } ) ; } ; params params params params params params params params params params return { ExceptionListSchema | null } public updateExceptionList = async ( { _version , id , description , listId , meta , name , namespaceType , tags , type , version , } ) : Promise < ExceptionListSchema | null > => { const { savedObjectsClient , user } = this ; return updateExceptionList ( { _version , description , id , listId , meta , name , namespaceType , savedObjectsClient , tags , type , user , version , } ) ; } ; params params params return { ExceptionListSchema | null } public deleteExceptionList = async ( { id , listId , namespaceType , } ) : Promise < ExceptionListSchema | null > => { const { savedObjectsClient } = this ; return deleteExceptionList ( { id , listId , namespaceType , savedObjectsClient , } ) ; } ; params params params params params params params params params params return { ExceptionListItemSchema } public createExceptionListItem = async ( { comments , description , entries , itemId , listId , meta , name , namespaceType , osTypes , tags , type , } ) : Promise < ExceptionListItemSchema > => { const { savedObjectsClient , user } = this ; let itemData = { comments , description , entries , itemId , listId , meta , name , namespaceType , osTypes , tags , type , } ; if ( this . enableServerExtensionPoints ) { itemData = await this . serverExtensionsClient . pipeRun ( 'exceptionsListPreCreateItem' , itemData , ( data ) => { return validateData ( createExceptionListItemSchema , transformCreateExceptionListItemOptionsToCreateExceptionListItemSchema ( data ) ) ; } ) ; } return createExceptionListItem ( { ... itemData , savedObjectsClient , user , } ) ; } ; params params params params params params params params params params params params return { ExceptionListItemSchema | null } public updateExceptionListItem = async ( { _version , comments , description , entries , id , itemId , meta , name , namespaceType , osTypes , tags , type , } ) : Promise < ExceptionListItemSchema | null > => { const { savedObjectsClient , user } = this ; let updatedItem = { _version , comments , description , entries , id , itemId , meta , name , namespaceType , osTypes , tags , type , } ; if ( this . enableServerExtensionPoints ) { updatedItem = await this . serverExtensionsClient . pipeRun ( 'exceptionsListPreUpdateItem' , updatedItem , ( data ) => { return validateData ( updateExceptionListItemSchema , transformUpdateExceptionListItemOptionsToUpdateExceptionListItemSchema ( data ) ) ; } ) ; } return updateExceptionListItem ( { ... updatedItem , savedObjectsClient , user , } ) ; } ; params params params return { ExceptionListItemSchema | null } public deleteExceptionListItem = async ( { id , itemId , namespaceType , } ) : Promise < ExceptionListItemSchema | null > => { const { savedObjectsClient } = this ; return deleteExceptionListItem ( { id , itemId , namespaceType , savedObjectsClient , } ) ; } ; params params return { void } public deleteExceptionListItemById = async ( { id , namespaceType , } ) : Promise < void > => { const { savedObjectsClient } = this ; return deleteExceptionListItemById ( { id , namespaceType , savedObjectsClient , } ) ; } ; /**
     * This is the same as "deleteExceptionListItem" except it applies specifically to the endpoint list.
     */ public deleteEndpointListItem = async ( { id , itemId , } ) : Promise < ExceptionListItemSchema | null > => { const { savedObjectsClient } = this ; return deleteExceptionListItem ( { id , itemId , namespaceType : 'agnostic' , savedObjectsClient , } ) ; } ; public findExceptionListItem = async ( { listId , filter , perPage , page , sortField , sortOrder , namespaceType , } ) : Promise < FoundExceptionListItemSchema | null > => { const { savedObjectsClient } = this ; return findExceptionListItem ( { filter , listId , namespaceType , page , perPage , savedObjectsClient , sortField , sortOrder , } ) ; } ; public findExceptionListsItem = async ( { listId , filter , perPage , page , sortField , sortOrder , namespaceType , } ) : Promise < FoundExceptionListItemSchema | null > => { const { savedObjectsClient } = this ; return findExceptionListsItem ( { filter , listId , namespaceType , page , perPage , savedObjectsClient , sortField , sortOrder , } ) ; } ; public findValueListExceptionListItems = async ( { perPage , page , sortField , sortOrder , valueListId , } ) : Promise < FoundExceptionListItemSchema | null > => { const { savedObjectsClient } = this ; return findValueListExceptionListItems ( { page , perPage , savedObjectsClient , sortField , sortOrder , valueListId , } ) ; } ; public findExceptionList = async ( { filter , perPage , page , sortField , sortOrder , namespaceType , } ) : Promise < FoundExceptionListSchema > => { const { savedObjectsClient } = this ; return findExceptionList ( { filter , namespaceType , page , perPage , savedObjectsClient , sortField , sortOrder , } ) ; } ; /**
     * This is the same as "findExceptionList" except it applies specifically to the  endpoint list and will
     * auto-call the "createEndpointList" for you so that you have the best chance of the  endpoint
     * being there if it did not exist before. If the list did not exist before, then creating it here should give you
     * a good guarantee that you will get an empty record set rather than null. I keep the null as the return value in
     * the off chance that you still might somehow not get into a race condition where the  endpoint list does
     * not exist because someone deleted it in-between the initial create and then the find.
     */ public findEndpointListItem = async ( { filter , perPage , page , sortField , sortOrder , } ) : Promise < FoundExceptionListItemSchema | null > => { const { savedObjectsClient } = this ; await this . createEndpointList ( ) ; return findExceptionListItem ( { filter , listId : ENDPOINT_LIST_ID , namespaceType : 'agnostic' , page , perPage , savedObjectsClient , sortField , sortOrder , } ) ; } ; params params params return { ExportExceptionListAndItemsReturn | null } public exportExceptionListAndItems = async ( { listId , id , namespaceType , } ) : Promise < ExportExceptionListAndItemsReturn | null > => { const { savedObjectsClient } = this ; return exportExceptionListAndItems ( { id , listId , namespaceType , savedObjectsClient , } ) ; } ; params params params return { ImportExceptionsResponseSchema } public importExceptionListAndItems = async ( { exceptionsToImport , maxExceptionsImportSize , overwrite , } ) : Promise < ImportExceptionsResponseSchema > => { const { savedObjectsClient , user } = this ; return importExceptionsAsStream ( { exceptionsToImport , maxExceptionsImportSize , overwrite , savedObjectsClient , user , } ) ; } ; params params params return { ImportExceptionsResponseSchema } public importExceptionListAndItemsAsArray = async ( { exceptionsToImport , maxExceptionsImportSize , overwrite , } ) : Promise < ImportExceptionsResponseSchema > => { const { savedObjectsClient , user } = this ; return importExceptionsAsArray ( { exceptionsToImport , maxExceptionsImportSize , overwrite , savedObjectsClient , user , } ) ; } ; }