-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Entity Analytics] Internal API to upload asset criticality records via CSV file #179930
Merged
hop-dev
merged 43 commits into
elastic:main
from
hop-dev:asset-criticality-csv-upoad-api
Apr 11, 2024
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
57206a2
working e2e
hop-dev 142a742
rename results to stats
hop-dev 86b178b
unit tests
hop-dev 80c8835
use config for batch size and max file size
hop-dev 71f393c
move line validation to common folder for UI to use
hop-dev a744ac1
update validation to match architecture spec
hop-dev d2956d5
allow case in criticality level to be wrong
hop-dev 21c9741
add data client tests
hop-dev 84548a8
remove csv folder
hop-dev 5b1ebb2
fix config type error
hop-dev 5e002b0
move to constant for max file size
hop-dev 0143d76
AssetCriticalityCsvUploadResponse common type
hop-dev b5e4725
fix: errors not counting towards total
hop-dev ceb1615
fix error indexes
hop-dev 325cea3
API integration tests
hop-dev 49e25ce
add test file import
hop-dev 21ca9cd
add test for catastrophic es error
hop-dev 4440c62
fix test types
hop-dev bfd1f74
Add OpenAPI spec
hop-dev fd76077
Add telemetry event
hop-dev faab392
use generator in bulkUpsertFromStream
hop-dev 415ebfb
Merge branch 'main' into asset-criticality-csv-upoad-api
kibanamachine 9e93fb6
add missing awaits
hop-dev 1f4d866
check advanced setting is enabled on criticality API
hop-dev 511265b
make csv validation errors clearer
hop-dev e66f9d2
fix wrong status code for file too large
hop-dev addec44
Merge branch 'main' into asset-criticality-csv-upoad-api
hop-dev cdcc28c
Merge branch 'asset-criticality-csv-upoad-api' of github.com:hop-dev/…
hop-dev f3c2c96
remove redundant else
hop-dev 8a2d350
run batches in paralell
hop-dev 007bd8c
remove unused generator
hop-dev 5ee0498
use translations for csv parsing errors
hop-dev a29a775
fix types
hop-dev e391d0f
remove api from translation IDs
hop-dev 46fb379
send 403 if setting is disabled
hop-dev 9d975a7
use stream helper
hop-dev c142c8b
update config to match new client
hop-dev feb235b
remove fileSizeBytes telemetry field
hop-dev bc5198d
jareds error message feedback
hop-dev 9a70627
Merge branch 'main' into asset-criticality-csv-upoad-api
hop-dev e871b6a
report telemetry event on total error
hop-dev 8630d07
Merge branch 'main' into asset-criticality-csv-upoad-api
hop-dev 539ecc7
use helper for validation errors
hop-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 36 additions & 0 deletions
36
...olution/common/api/entity_analytics/asset_criticality/upload_asset_criticality_csv.gen.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,36 @@ | ||
/* | ||
* 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 { z } from 'zod'; | ||
|
||
/* | ||
* NOTICE: Do not edit this file manually. | ||
* This file is automatically generated by the OpenAPI Generator, @kbn/openapi-generator. | ||
* | ||
* info: | ||
* title: Asset Criticality Create Record Schema | ||
* version: 1.0.0 | ||
*/ | ||
|
||
export type ErrorItem = z.infer<typeof ErrorItem>; | ||
export const ErrorItem = z.object({ | ||
message: z.string(), | ||
index: z.number().int(), | ||
}); | ||
|
||
export type Stats = z.infer<typeof Stats>; | ||
export const Stats = z.object({ | ||
successful: z.number().int(), | ||
failed: z.number().int(), | ||
total: z.number().int(), | ||
}); | ||
|
||
export type AssetCriticalityCsvUploadResponse = z.infer<typeof AssetCriticalityCsvUploadResponse>; | ||
export const AssetCriticalityCsvUploadResponse = z.object({ | ||
errors: z.array(ErrorItem), | ||
stats: Stats, | ||
}); |
73 changes: 73 additions & 0 deletions
73
...on/common/api/entity_analytics/asset_criticality/upload_asset_criticality_csv.schema.yaml
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,73 @@ | ||
openapi: 3.0.0 | ||
info: | ||
version: 1.0.0 | ||
title: Asset Criticality Create Record Schema | ||
servers: | ||
- url: 'http://{kibana_host}:{port}' | ||
variables: | ||
kibana_host: | ||
default: localhost | ||
port: | ||
default: '5601' | ||
paths: | ||
/internal/asset_criticality/upload_csv: | ||
post: | ||
summary: Uploads a CSV file containing asset criticality data | ||
requestBody: | ||
content: | ||
multipart/form-data: | ||
schema: | ||
type: object | ||
properties: | ||
file: | ||
type: string | ||
format: binary | ||
description: The CSV file to upload. | ||
required: | ||
- file | ||
responses: | ||
'200': | ||
description: CSV upload successful | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/AssetCriticalityCsvUploadResponse' | ||
'413': | ||
description: File too large | ||
components: | ||
schemas: | ||
ErrorItem: | ||
type: object | ||
properties: | ||
message: | ||
type: string | ||
index: | ||
type: integer | ||
required: | ||
- message | ||
- index | ||
Stats: | ||
type: object | ||
properties: | ||
successful: | ||
type: integer | ||
failed: | ||
type: integer | ||
total: | ||
type: integer | ||
required: | ||
- successful | ||
- failed | ||
- total | ||
AssetCriticalityCsvUploadResponse: | ||
type: object | ||
properties: | ||
errors: | ||
type: array | ||
items: | ||
$ref: '#/components/schemas/ErrorItem' | ||
stats: | ||
$ref: '#/components/schemas/Stats' | ||
required: | ||
- errors | ||
- stats |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ | |
|
||
export * from './indices'; | ||
export * from './constants'; | ||
export * from './parse_asset_criticality_csv_row'; |
133 changes: 133 additions & 0 deletions
133
...olution/common/entity_analytics/asset_criticality/parse_asset_criticality_csv_row.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,133 @@ | ||
/* | ||
* 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 { parseAssetCriticalityCsvRow } from './parse_asset_criticality_csv_row'; | ||
|
||
describe('parseAssetCriticalityCsvRow', () => { | ||
it('should return valid false if the row has no columns', () => { | ||
const result = parseAssetCriticalityCsvRow([]); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot(`"Expected 3 columns, got 0"`); | ||
}); | ||
|
||
it('should return valid false if the row has 2 columns', () => { | ||
const result = parseAssetCriticalityCsvRow(['host', 'host-1']); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot(`"Expected 3 columns, got 2"`); | ||
}); | ||
|
||
it('should return valid false if the row has 4 columns', () => { | ||
const result = parseAssetCriticalityCsvRow(['host', 'host-1', 'low_impact', 'extra']); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot(`"Expected 3 columns, got 4"`); | ||
}); | ||
|
||
it('should return valid false if the entity type is missing', () => { | ||
const result = parseAssetCriticalityCsvRow(['', 'host-1', 'low_impact']); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot(`"Missing entity type"`); | ||
}); | ||
|
||
it('should return valid false if the entity type is invalid', () => { | ||
const result = parseAssetCriticalityCsvRow(['invalid', 'host-1', 'low_impact']); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot( | ||
`"Invalid entity type \\"invalid\\", expected host or user"` | ||
); | ||
}); | ||
|
||
it('should return valid false if the entity type is invalid and only log 1000 characters', () => { | ||
const invalidEntityType = 'x'.repeat(1001); | ||
const result = parseAssetCriticalityCsvRow([invalidEntityType, 'host-1', 'low_impact']); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot( | ||
`"Invalid entity type \\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...\\", expected host or user"` | ||
); | ||
}); | ||
|
||
it('should return valid false if the ID is missing', () => { | ||
const result = parseAssetCriticalityCsvRow(['host', '', 'low_impact']); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot(`"Missing identifier"`); | ||
}); | ||
|
||
it('should return valid false if the criticality level is missing', () => { | ||
const result = parseAssetCriticalityCsvRow(['host', 'host-1', '']); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot(`"Missing criticality level"`); | ||
}); | ||
|
||
it('should return valid false if the criticality level is invalid', () => { | ||
const result = parseAssetCriticalityCsvRow(['host', 'host-1', 'invalid']); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot( | ||
`"Invalid criticality level \\"invalid\\", expected one of extreme_impact, high_impact, medium_impact, low_impact"` | ||
); | ||
}); | ||
|
||
it('should return valid false if the criticality level is invalid and only log 1000 characters', () => { | ||
const invalidCriticalityLevel = 'x'.repeat(1001); | ||
const result = parseAssetCriticalityCsvRow(['host', 'host-1', invalidCriticalityLevel]); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot( | ||
`"Invalid criticality level \\"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...\\", expected one of extreme_impact, high_impact, medium_impact, low_impact"` | ||
); | ||
}); | ||
|
||
it('should return valid false if the ID is too long', () => { | ||
const idValue = 'x'.repeat(1001); | ||
const result = parseAssetCriticalityCsvRow(['host', idValue, 'low_impact']); | ||
expect(result.valid).toBe(false); | ||
|
||
// @ts-ignore result can now only be InvalidRecord | ||
expect(result.error).toMatchInlineSnapshot( | ||
`"Identifier is too long, expected less than 1000 characters, got 1001"` | ||
); | ||
}); | ||
|
||
it('should return the parsed row', () => { | ||
expect(parseAssetCriticalityCsvRow(['host', 'host-1', 'low_impact'])).toEqual({ | ||
valid: true, | ||
record: { | ||
idField: 'host.name', | ||
idValue: 'host-1', | ||
criticalityLevel: 'low_impact', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should return the parsed row if criticality level is the wrong case', () => { | ||
expect(parseAssetCriticalityCsvRow(['host', 'host-1', 'LOW_IMPACT'])).toEqual({ | ||
valid: true, | ||
record: { | ||
idField: 'host.name', | ||
idValue: 'host-1', | ||
criticalityLevel: 'low_impact', | ||
}, | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend to move
ASSET_CRITICALITY...
constants into Asset criticality domain. Createcommon/asset_criticality
folder withconstants.ts
. It will help to define proper ownership avoid expandingcommon/constants.ts
with not really common constants.