-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds specific endpoint_list REST API and API for abilities to autocre…
…ate the endpoint_list if it gets deleted
- Loading branch information
1 parent
d0c9fe9
commit 653f20b
Showing
37 changed files
with
1,201 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
x-pack/plugins/lists/common/schemas/request/create_endpoint_list_item_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/camelcase */ | ||
|
||
import * as t from 'io-ts'; | ||
|
||
import { | ||
ItemId, | ||
Tags, | ||
_Tags, | ||
_tags, | ||
description, | ||
exceptionListItemType, | ||
meta, | ||
name, | ||
tags, | ||
} from '../common/schemas'; | ||
import { Identity, RequiredKeepUndefined } from '../../types'; | ||
import { CreateCommentsArray, DefaultCreateCommentsArray, DefaultEntryArray } from '../types'; | ||
import { EntriesArray } from '../types/entries'; | ||
import { DefaultUuid } from '../../siem_common_deps'; | ||
|
||
export const createEndpointListItemSchema = t.intersection([ | ||
t.exact( | ||
t.type({ | ||
description, | ||
name, | ||
type: exceptionListItemType, | ||
}) | ||
), | ||
t.exact( | ||
t.partial({ | ||
_tags, // defaults to empty array if not set during decode | ||
comments: DefaultCreateCommentsArray, // defaults to empty array if not set during decode | ||
entries: DefaultEntryArray, // defaults to empty array if not set during decode | ||
item_id: DefaultUuid, // defaults to GUID (uuid v4) if not set during decode | ||
meta, // defaults to undefined if not set during decode | ||
tags, // defaults to empty array if not set during decode | ||
}) | ||
), | ||
]); | ||
|
||
export type CreateEndpointListItemSchemaPartial = Identity< | ||
t.TypeOf<typeof createEndpointListItemSchema> | ||
>; | ||
export type CreateEndpointListItemSchema = RequiredKeepUndefined< | ||
t.TypeOf<typeof createEndpointListItemSchema> | ||
>; | ||
|
||
// This type is used after a decode since some things are defaults after a decode. | ||
export type CreateEndpointListItemSchemaDecoded = Identity< | ||
Omit<CreateEndpointListItemSchema, '_tags' | 'tags' | 'item_id' | 'entries' | 'comments'> & { | ||
_tags: _Tags; | ||
comments: CreateCommentsArray; | ||
tags: Tags; | ||
item_id: ItemId; | ||
entries: EntriesArray; | ||
} | ||
>; |
23 changes: 23 additions & 0 deletions
23
x-pack/plugins/lists/common/schemas/request/delete_endpoint_list_item_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/camelcase */ | ||
|
||
import * as t from 'io-ts'; | ||
|
||
import { id, item_id } from '../common/schemas'; | ||
|
||
export const deleteEndpointListItemSchema = t.exact( | ||
t.partial({ | ||
id, | ||
item_id, | ||
}) | ||
); | ||
|
||
export type DeleteEndpointListItemSchema = t.TypeOf<typeof deleteEndpointListItemSchema>; | ||
|
||
// This type is used after a decode since some things are defaults after a decode. | ||
export type DeleteEndpointListItemSchemaDecoded = DeleteEndpointListItemSchema; |
37 changes: 37 additions & 0 deletions
37
x-pack/plugins/lists/common/schemas/request/find_endpoint_list_item_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/camelcase */ | ||
|
||
import * as t from 'io-ts'; | ||
|
||
import { filter, sort_field, sort_order } from '../common/schemas'; | ||
import { RequiredKeepUndefined } from '../../types'; | ||
import { StringToPositiveNumber } from '../types/string_to_positive_number'; | ||
|
||
export const findEndpointListItemSchema = t.exact( | ||
t.partial({ | ||
filter, // defaults to undefined if not set during decode | ||
page: StringToPositiveNumber, // defaults to undefined if not set during decode | ||
per_page: StringToPositiveNumber, // defaults to undefined if not set during decode | ||
sort_field, // defaults to undefined if not set during decode | ||
sort_order, // defaults to undefined if not set during decode | ||
}) | ||
); | ||
|
||
export type FindEndpointListItemSchemaPartial = t.OutputOf<typeof findEndpointListItemSchema>; | ||
|
||
// This type is used after a decode since some things are defaults after a decode. | ||
export type FindEndpointListItemSchemaPartialDecoded = t.TypeOf<typeof findEndpointListItemSchema>; | ||
|
||
// This type is used after a decode since some things are defaults after a decode. | ||
export type FindEndpointListItemSchemaDecoded = RequiredKeepUndefined< | ||
FindEndpointListItemSchemaPartialDecoded | ||
>; | ||
|
||
export type FindEndpointListItemSchema = RequiredKeepUndefined< | ||
t.TypeOf<typeof findEndpointListItemSchema> | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
x-pack/plugins/lists/common/schemas/request/read_endpoint_list_item_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/camelcase */ | ||
|
||
import * as t from 'io-ts'; | ||
|
||
import { id, item_id } from '../common/schemas'; | ||
import { RequiredKeepUndefined } from '../../types'; | ||
|
||
export const readEndpointListItemSchema = t.exact( | ||
t.partial({ | ||
id, | ||
item_id, | ||
}) | ||
); | ||
|
||
export type ReadEndpointListItemSchemaPartial = t.TypeOf<typeof readEndpointListItemSchema>; | ||
|
||
// This type is used after a decode since some things are defaults after a decode. | ||
export type ReadEndpointListItemSchemaPartialDecoded = ReadEndpointListItemSchemaPartial; | ||
|
||
// This type is used after a decode since some things are defaults after a decode. | ||
export type ReadEndpointListItemSchemaDecoded = RequiredKeepUndefined< | ||
ReadEndpointListItemSchemaPartialDecoded | ||
>; | ||
|
||
export type ReadEndpointListItemSchema = RequiredKeepUndefined<ReadEndpointListItemSchemaPartial>; |
71 changes: 71 additions & 0 deletions
71
x-pack/plugins/lists/common/schemas/request/update_endpoint_list_item_schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/camelcase */ | ||
|
||
import * as t from 'io-ts'; | ||
|
||
import { | ||
Tags, | ||
_Tags, | ||
_tags, | ||
description, | ||
exceptionListItemType, | ||
id, | ||
meta, | ||
name, | ||
namespace_type, | ||
tags, | ||
} from '../common/schemas'; | ||
import { Identity, RequiredKeepUndefined } from '../../types'; | ||
import { | ||
DefaultEntryArray, | ||
DefaultUpdateCommentsArray, | ||
EntriesArray, | ||
UpdateCommentsArray, | ||
} from '../types'; | ||
|
||
export const updateEndpointListItemSchema = t.intersection([ | ||
t.exact( | ||
t.type({ | ||
description, | ||
name, | ||
type: exceptionListItemType, | ||
}) | ||
), | ||
t.exact( | ||
t.partial({ | ||
_tags, // defaults to empty array if not set during decode | ||
comments: DefaultUpdateCommentsArray, // defaults to empty array if not set during decode | ||
entries: DefaultEntryArray, // defaults to empty array if not set during decode | ||
id, // defaults to undefined if not set during decode | ||
item_id: t.union([t.string, t.undefined]), | ||
meta, // defaults to undefined if not set during decode | ||
namespace_type, // defaults to 'single' if not set during decode | ||
tags, // defaults to empty array if not set during decode | ||
}) | ||
), | ||
]); | ||
|
||
export type UpdateEndpointListItemSchemaPartial = Identity< | ||
t.TypeOf<typeof updateEndpointListItemSchema> | ||
>; | ||
export type UpdateEndpointListItemSchema = RequiredKeepUndefined< | ||
t.TypeOf<typeof updateEndpointListItemSchema> | ||
>; | ||
|
||
// This type is used after a decode since some things are defaults after a decode. | ||
export type UpdateEndpointListItemSchemaDecoded = Identity< | ||
Omit< | ||
UpdateEndpointListItemSchema, | ||
'_tags' | 'tags' | 'entries' | 'namespace_type' | 'comments' | ||
> & { | ||
_tags: _Tags; | ||
comments: UpdateCommentsArray; | ||
tags: Tags; | ||
entries: EntriesArray; | ||
} | ||
>; |
86 changes: 86 additions & 0 deletions
86
x-pack/plugins/lists/server/routes/create_endpoint_list_item_route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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 { IRouter } from 'kibana/server'; | ||
|
||
import { ENDPOINT_LIST_ITEM_URL } from '../../common/constants'; | ||
import { buildRouteValidation, buildSiemResponse, transformError } from '../siem_server_deps'; | ||
import { validate } from '../../common/siem_common_deps'; | ||
import { | ||
CreateEndpointListItemSchemaDecoded, | ||
createEndpointListItemSchema, | ||
exceptionListItemSchema, | ||
} from '../../common/schemas'; | ||
|
||
import { getExceptionListClient } from './utils/get_exception_list_client'; | ||
|
||
export const createEndpointListItemRoute = (router: IRouter): void => { | ||
router.post( | ||
{ | ||
options: { | ||
tags: ['access:lists'], | ||
}, | ||
path: ENDPOINT_LIST_ITEM_URL, | ||
validate: { | ||
body: buildRouteValidation< | ||
typeof createEndpointListItemSchema, | ||
CreateEndpointListItemSchemaDecoded | ||
>(createEndpointListItemSchema), | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
const siemResponse = buildSiemResponse(response); | ||
try { | ||
const { | ||
name, | ||
_tags, | ||
tags, | ||
meta, | ||
comments, | ||
description, | ||
entries, | ||
item_id: itemId, | ||
type, | ||
} = request.body; | ||
const exceptionLists = getExceptionListClient(context); | ||
const exceptionListItem = await exceptionLists.getEndpointListItem({ | ||
id: undefined, | ||
itemId, | ||
}); | ||
if (exceptionListItem != null) { | ||
return siemResponse.error({ | ||
body: `exception list item id: "${itemId}" already exists`, | ||
statusCode: 409, | ||
}); | ||
} else { | ||
const createdList = await exceptionLists.createEndpointListItem({ | ||
_tags, | ||
comments, | ||
description, | ||
entries, | ||
itemId, | ||
meta, | ||
name, | ||
tags, | ||
type, | ||
}); | ||
const [validated, errors] = validate(createdList, exceptionListItemSchema); | ||
if (errors != null) { | ||
return siemResponse.error({ body: errors, statusCode: 500 }); | ||
} else { | ||
return response.ok({ body: validated ?? {} }); | ||
} | ||
} | ||
} catch (err) { | ||
const error = transformError(err); | ||
return siemResponse.error({ | ||
body: error.message, | ||
statusCode: error.statusCode, | ||
}); | ||
} | ||
} | ||
); | ||
}; |
Oops, something went wrong.