|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +import { SavedObjectsClientContract } from 'kibana/server'; |
| 8 | +import uuid from 'uuid'; |
| 9 | + |
| 10 | +import { |
| 11 | + ENDPOINT_TRUSTED_APPS_LIST_DESCRIPTION, |
| 12 | + ENDPOINT_TRUSTED_APPS_LIST_ID, |
| 13 | + ENDPOINT_TRUSTED_APPS_LIST_NAME, |
| 14 | +} from '../../../common/constants'; |
| 15 | +import { ExceptionListSchema, ExceptionListSoSchema, Version } from '../../../common/schemas'; |
| 16 | + |
| 17 | +import { getSavedObjectType, transformSavedObjectToExceptionList } from './utils'; |
| 18 | + |
| 19 | +interface CreateEndpointListOptions { |
| 20 | + savedObjectsClient: SavedObjectsClientContract; |
| 21 | + user: string; |
| 22 | + tieBreaker?: string; |
| 23 | + version: Version; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Creates the Endpoint Trusted Apps agnostic list if it does not yet exist |
| 28 | + * |
| 29 | + * @param savedObjectsClient |
| 30 | + * @param user |
| 31 | + * @param tieBreaker |
| 32 | + * @param version |
| 33 | + */ |
| 34 | +export const createEndpointTrustedAppsList = async ({ |
| 35 | + savedObjectsClient, |
| 36 | + user, |
| 37 | + tieBreaker, |
| 38 | + version, |
| 39 | +}: CreateEndpointListOptions): Promise<ExceptionListSchema | null> => { |
| 40 | + const savedObjectType = getSavedObjectType({ namespaceType: 'agnostic' }); |
| 41 | + const dateNow = new Date().toISOString(); |
| 42 | + try { |
| 43 | + const savedObject = await savedObjectsClient.create<ExceptionListSoSchema>( |
| 44 | + savedObjectType, |
| 45 | + { |
| 46 | + _tags: [], |
| 47 | + comments: undefined, |
| 48 | + created_at: dateNow, |
| 49 | + created_by: user, |
| 50 | + description: ENDPOINT_TRUSTED_APPS_LIST_DESCRIPTION, |
| 51 | + entries: undefined, |
| 52 | + immutable: false, |
| 53 | + item_id: undefined, |
| 54 | + list_id: ENDPOINT_TRUSTED_APPS_LIST_ID, |
| 55 | + list_type: 'list', |
| 56 | + meta: undefined, |
| 57 | + name: ENDPOINT_TRUSTED_APPS_LIST_NAME, |
| 58 | + tags: [], |
| 59 | + tie_breaker_id: tieBreaker ?? uuid.v4(), |
| 60 | + type: 'endpoint', |
| 61 | + updated_by: user, |
| 62 | + version, |
| 63 | + }, |
| 64 | + { |
| 65 | + // We intentionally hard coding the id so that there can only be one Trusted apps list within the space |
| 66 | + id: ENDPOINT_TRUSTED_APPS_LIST_ID, |
| 67 | + } |
| 68 | + ); |
| 69 | + return transformSavedObjectToExceptionList({ savedObject }); |
| 70 | + } catch (err) { |
| 71 | + if (savedObjectsClient.errors.isConflictError(err)) { |
| 72 | + return null; |
| 73 | + } else { |
| 74 | + throw err; |
| 75 | + } |
| 76 | + } |
| 77 | +}; |
0 commit comments