-
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.
- Loading branch information
1 parent
7f4c183
commit 5c7b12c
Showing
13 changed files
with
272 additions
and
17 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
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
148 changes: 148 additions & 0 deletions
148
x-pack/plugins/asset_manager/server/lib/asset_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,148 @@ | ||
/* | ||
* 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 { | ||
ElasticsearchClientMock, | ||
elasticsearchClientMock, | ||
} from '@kbn/core-elasticsearch-client-server-mocks'; | ||
import { AssetClient } from './asset_client'; | ||
import { MetricsDataClient, MetricsDataClientMock } from '@kbn/metrics-data-access-plugin/server'; | ||
import { savedObjectsClientMock } from '@kbn/core-saved-objects-api-server-mocks'; | ||
import { SearchRequest } from '@elastic/elasticsearch/lib/api/types'; | ||
import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; | ||
|
||
function createAssetClient(source: 'signals' | 'assets', metricsDataClient: MetricsDataClient) { | ||
return new AssetClient({ | ||
source, | ||
sourceIndices: { | ||
logs: 'my-logs*', | ||
}, | ||
getApmIndices: jest.fn(), | ||
metricsClient: metricsDataClient, | ||
}); | ||
} | ||
|
||
describe('Public assets client', () => { | ||
let metricsDataClientMock: MetricsDataClient = MetricsDataClientMock.create(); | ||
let esClientMock: ElasticsearchClientMock = | ||
elasticsearchClientMock.createScopedClusterClient().asCurrentUser; | ||
let soClientMock: jest.Mocked<SavedObjectsClientContract>; | ||
|
||
beforeEach(() => { | ||
// Reset mocks | ||
esClientMock = elasticsearchClientMock.createScopedClusterClient().asCurrentUser; | ||
soClientMock = savedObjectsClientMock.create(); | ||
metricsDataClientMock = MetricsDataClientMock.create(); | ||
|
||
// ES returns no results, just enough structure to not blow up | ||
esClientMock.search.mockResolvedValueOnce({ | ||
took: 1, | ||
timed_out: false, | ||
_shards: { | ||
failed: 0, | ||
successful: 1, | ||
total: 1, | ||
}, | ||
hits: { | ||
hits: [], | ||
}, | ||
}); | ||
}); | ||
|
||
describe('class instantiation', () => { | ||
it('should successfully instantiate with source: signals', () => { | ||
createAssetClient('signals', metricsDataClientMock); | ||
}); | ||
|
||
it('should successfully instantiate with source: assets', () => { | ||
createAssetClient('assets', metricsDataClientMock); | ||
}); | ||
}); | ||
|
||
describe('getHosts', () => { | ||
it('should query Elasticsearch via signals correctly', async () => { | ||
const client = createAssetClient('signals', metricsDataClientMock); | ||
|
||
await client.getHosts({ | ||
from: 'now-5d', | ||
to: 'now-3d', | ||
elasticsearchClient: esClientMock, | ||
savedObjectsClient: soClientMock, | ||
}); | ||
|
||
expect(metricsDataClientMock.getMetricIndices).toHaveBeenCalledTimes(1); | ||
expect(metricsDataClientMock.getMetricIndices).toHaveBeenCalledWith({ | ||
savedObjectsClient: soClientMock, | ||
}); | ||
|
||
const dsl = esClientMock.search.mock.lastCall?.[0] as SearchRequest | undefined; | ||
const { bool } = dsl?.query || {}; | ||
expect(bool).toBeDefined(); | ||
|
||
expect(bool?.filter).toEqual([ | ||
{ | ||
range: { | ||
'@timestamp': { | ||
gte: 'now-5d', | ||
lte: 'now-3d', | ||
}, | ||
}, | ||
}, | ||
]); | ||
|
||
expect(bool?.must).toEqual([ | ||
{ | ||
exists: { | ||
field: 'host.hostname', | ||
}, | ||
}, | ||
]); | ||
|
||
expect(bool?.should).toEqual([ | ||
{ exists: { field: 'kubernetes.node.name' } }, | ||
{ exists: { field: 'kubernetes.pod.uid' } }, | ||
{ exists: { field: 'container.id' } }, | ||
]); | ||
}); | ||
|
||
it('should query Elasticsearch via assets correctly', async () => { | ||
const client = createAssetClient('assets', metricsDataClientMock); | ||
|
||
await client.getHosts({ | ||
from: 'now-5d', | ||
to: 'now-3d', | ||
elasticsearchClient: esClientMock, | ||
savedObjectsClient: soClientMock, | ||
}); | ||
|
||
expect(metricsDataClientMock.getMetricIndices).not.toHaveBeenCalled(); | ||
|
||
const dsl = esClientMock.search.mock.lastCall?.[0] as SearchRequest | undefined; | ||
const { bool } = dsl?.query || {}; | ||
expect(bool).toBeDefined(); | ||
|
||
expect(bool?.filter).toEqual([ | ||
{ | ||
range: { | ||
'@timestamp': { | ||
gte: 'now-5d', | ||
lte: 'now-3d', | ||
}, | ||
}, | ||
}, | ||
]); | ||
|
||
expect(bool?.must).toEqual([ | ||
{ | ||
terms: { | ||
'asset.kind': ['host'], | ||
}, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
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
17 changes: 17 additions & 0 deletions
17
x-pack/plugins/asset_manager/server/lib/validators/validate_es_date.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,17 @@ | ||
/* | ||
* 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 datemath from '@kbn/datemath'; | ||
import { AssetsValidationError } from './validation_error'; | ||
|
||
export function validateESDate(dateString: string) { | ||
try { | ||
datemath.parse(dateString); | ||
} catch (error: any) { | ||
throw new AssetsValidationError(`"${dateString}" is not a valid Elasticsearch date value`); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
x-pack/plugins/asset_manager/server/lib/validators/validation_error.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,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
interface ErrorOptions { | ||
statusCode?: number; | ||
} | ||
|
||
export class AssetsValidationError extends Error { | ||
public statusCode: number; | ||
|
||
constructor(message: string, { statusCode = 500 }: ErrorOptions = {}) { | ||
super(message); | ||
this.name = 'AssetsValidationError'; | ||
this.statusCode = statusCode; | ||
} | ||
} |
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
Oops, something went wrong.