forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Create Entity Store index (elastic#175025)
**This PR is going to be merged to the [entity-store-poc](https://github.com/elastic/kibana/tree/security/feature/entity-store-poc) feature branch; it won't impact the main branch.** ## Summary * Create `entity_store/init` route that creates the Entity Store index. * Create FTR tests. ### Out of scope * User fields are out of scope. * API privileges are out of scope. ### How to test it? * Call API ``` KIBANA_URL="http://localhost:5601" USER_PASS="{USER}:{PASSWORD}" curl "$KIBANA_URL/internal/entity_store/init" \ -H 'kbn-xsrf:bleh' \ --user "$USER_PASS"\ -X 'POST' \ -H 'elastic-api-version: 1' ``` * Open the console and check if the index `.entities.entities-default` exists #### Run tests **serverless** `yarn run initialize-server:ea:default entity_store serverless` `yarn run run-tests:ea:default entity_store serverless serverlessEnv` **ess** `yarn run initialize-server:ea:default entity_store ess` `yarn run run-tests:ea:default entity_store ess essEnv` ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
Showing
18 changed files
with
500 additions
and
3 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
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
97 changes: 97 additions & 0 deletions
97
x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/constants.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,97 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { FieldMap } from '@kbn/alerts-as-data-utils'; | ||
|
||
export const entityStoreFieldMap: FieldMap = { | ||
'@timestamp': { | ||
type: 'date', | ||
array: false, | ||
required: false, | ||
}, | ||
// user or host | ||
entity_type: { | ||
type: 'keyword', | ||
array: false, | ||
required: true, | ||
}, | ||
// HOST | ||
'host.architecture': { | ||
type: 'keyword', | ||
required: false, | ||
array: true, | ||
}, | ||
'host.id': { | ||
type: 'keyword', | ||
required: false, | ||
array: true, | ||
}, | ||
'host.ip': { | ||
type: 'ip', | ||
required: false, | ||
array: true, | ||
}, | ||
'host.name': { | ||
type: 'keyword', | ||
required: true, | ||
array: false, | ||
}, | ||
'host.os.platform': { | ||
type: 'keyword', | ||
required: false, | ||
array: true, | ||
}, | ||
'host.os.version': { | ||
type: 'keyword', | ||
required: false, | ||
array: true, | ||
}, | ||
// AGENT | ||
'agent.type': { | ||
type: 'keyword', | ||
required: false, | ||
array: true, | ||
}, | ||
'agent.id': { | ||
type: 'keyword', | ||
required: false, | ||
array: true, | ||
}, | ||
// CLOUD | ||
'cloud.provider': { | ||
type: 'keyword', | ||
required: false, | ||
array: true, | ||
}, | ||
'cloud.region': { | ||
type: 'keyword', | ||
required: false, | ||
array: true, | ||
}, | ||
// RISK SCORE | ||
'host.risk.calculated_level': { | ||
type: 'keyword', | ||
array: false, | ||
required: false, | ||
}, | ||
'host.risk.calculated_score': { | ||
type: 'float', | ||
array: false, | ||
required: false, | ||
}, | ||
'host.risk.calculated_score_norm': { | ||
type: 'float', | ||
array: false, | ||
required: false, | ||
}, | ||
// ASSET CRITICALITY | ||
'host.asset.criticality': { | ||
type: 'keyword', | ||
array: false, | ||
required: false, | ||
}, | ||
} as const; |
18 changes: 18 additions & 0 deletions
18
...curity_solution/server/lib/entity_analytics/entity_store/entity_store_data_client.mock.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,18 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { EntityStoreDataClient } from './entity_store_data_client'; | ||
|
||
const createEntityStoreDataClientMock = () => | ||
({ | ||
doesIndexExist: jest.fn(), | ||
getStatus: jest.fn(), | ||
init: jest.fn(), | ||
search: jest.fn(), | ||
} as unknown as jest.Mocked<EntityStoreDataClient>); | ||
|
||
export const entityStoreDataClientMock = { create: createEntityStoreDataClientMock }; |
124 changes: 124 additions & 0 deletions
124
...curity_solution/server/lib/entity_analytics/entity_store/entity_store_data_client.test.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,124 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { loggingSystemMock, elasticsearchServiceMock } from '@kbn/core/server/mocks'; | ||
import { createOrUpdateIndex } from '../utils/create_or_update_index'; | ||
import { EntityStoreDataClient } from './entity_store_data_client'; | ||
|
||
jest.mock('../utils/create_or_update_index', () => ({ | ||
createOrUpdateIndex: jest.fn(), | ||
})); | ||
|
||
describe('EntityStoreDataClient', () => { | ||
let entityStoreDataClient: EntityStoreDataClient; | ||
let logger: ReturnType<typeof loggingSystemMock.createLogger>; | ||
const esClient = elasticsearchServiceMock.createScopedClusterClient().asCurrentUser; | ||
|
||
beforeEach(() => { | ||
logger = loggingSystemMock.createLogger(); | ||
const options = { | ||
logger, | ||
esClient, | ||
namespace: 'default', | ||
}; | ||
entityStoreDataClient = new EntityStoreDataClient(options); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should initialize entity store resources successfully', async () => { | ||
await entityStoreDataClient.init(); | ||
|
||
expect(createOrUpdateIndex).toHaveBeenCalledWith({ | ||
logger, | ||
esClient, | ||
options: { | ||
index: '.entities.entities-default', | ||
mappings: { | ||
dynamic: 'strict', | ||
properties: { | ||
'@timestamp': { | ||
ignore_malformed: false, | ||
type: 'date', | ||
}, | ||
agent: { | ||
properties: { | ||
id: { | ||
type: 'keyword', | ||
}, | ||
type: { | ||
type: 'keyword', | ||
}, | ||
}, | ||
}, | ||
cloud: { | ||
properties: { | ||
provider: { | ||
type: 'keyword', | ||
}, | ||
region: { | ||
type: 'keyword', | ||
}, | ||
}, | ||
}, | ||
entity_type: { | ||
type: 'keyword', | ||
}, | ||
host: { | ||
properties: { | ||
architecture: { | ||
type: 'keyword', | ||
}, | ||
asset: { | ||
properties: { | ||
criticality: { | ||
type: 'keyword', | ||
}, | ||
}, | ||
}, | ||
id: { | ||
type: 'keyword', | ||
}, | ||
ip: { | ||
type: 'ip', | ||
}, | ||
name: { | ||
type: 'keyword', | ||
}, | ||
os: { | ||
properties: { | ||
platform: { | ||
type: 'keyword', | ||
}, | ||
version: { | ||
type: 'keyword', | ||
}, | ||
}, | ||
}, | ||
risk: { | ||
properties: { | ||
calculated_level: { | ||
type: 'keyword', | ||
}, | ||
calculated_score: { | ||
type: 'float', | ||
}, | ||
calculated_score_norm: { | ||
type: 'float', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
...ns/security_solution/server/lib/entity_analytics/entity_store/entity_store_data_client.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,34 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import type { Logger, ElasticsearchClient } from '@kbn/core/server'; | ||
import { mappingFromFieldMap } from '@kbn/alerting-plugin/common'; | ||
import { getEntityStoreIndex } from '../../../../common/entity_analytics/entity_store'; | ||
import { createOrUpdateIndex } from '../utils/create_or_update_index'; | ||
import { entityStoreFieldMap } from './constants'; | ||
|
||
interface EntityStoreClientOpts { | ||
logger: Logger; | ||
esClient: ElasticsearchClient; | ||
namespace: string; | ||
} | ||
|
||
export class EntityStoreDataClient { | ||
constructor(private readonly options: EntityStoreClientOpts) {} | ||
/** | ||
* It creates the entity store index or update mappings if index exists | ||
*/ | ||
public async init() { | ||
await createOrUpdateIndex({ | ||
esClient: this.options.esClient, | ||
logger: this.options.logger, | ||
options: { | ||
index: getEntityStoreIndex(this.options.namespace), | ||
mappings: mappingFromFieldMap(entityStoreFieldMap, 'strict'), | ||
}, | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,6 +5,4 @@ | |
* 2.0. | ||
*/ | ||
|
||
// 🚧 TODO: make the entity store | ||
|
||
export {}; | ||
export { entityStoreInitRoute } from './init'; |
51 changes: 51 additions & 0 deletions
51
x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/routes/init.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,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils'; | ||
import { transformError } from '@kbn/securitysolution-es-utils'; | ||
import { ENTITY_STORE_INIT_URL } from '../../../../../common/constants'; | ||
import type { SecuritySolutionPluginRouter } from '../../../../types'; | ||
export const entityStoreInitRoute = (router: SecuritySolutionPluginRouter) => { | ||
router.versioned | ||
.post({ | ||
access: 'internal', | ||
path: ENTITY_STORE_INIT_URL, | ||
options: { | ||
tags: ['access:securitySolution'], // TODO entity store access `access:${APP_ID}-entity-analytics` | ||
}, | ||
}) | ||
.addVersion( | ||
{ version: '1', validate: {} }, | ||
// TODO Implement entity store privileges like `withRiskEnginePrivilegeCheck` in risk_engine_privileges.ts | ||
async (context, request, response) => { | ||
const siemResponse = buildSiemResponse(response); | ||
const securitySolution = await context.securitySolution; | ||
const entityStoreDataClient = securitySolution.getEntityStoreDataClient(); | ||
|
||
try { | ||
await entityStoreDataClient.init(); | ||
|
||
return response.ok({ | ||
body: { | ||
result: { | ||
entity_store_created: true, | ||
errors: [], | ||
}, | ||
}, | ||
}); | ||
} catch (e) { | ||
const error = transformError(e); | ||
|
||
return siemResponse.error({ | ||
statusCode: error.statusCode, | ||
body: { message: error.message, full_error: JSON.stringify(e) }, | ||
bypassErrorFormat: true, | ||
}); | ||
} | ||
} | ||
); | ||
}; |
Oops, something went wrong.