-
Notifications
You must be signed in to change notification settings - Fork 8.1k
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
6aa74a1
commit 9247b6a
Showing
4 changed files
with
329 additions
and
64 deletions.
There are no files selected for viewing
231 changes: 231 additions & 0 deletions
231
x-pack/plugins/security_solution/server/lib/telemetry/endpoint_metadata_telemetry.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,231 @@ | ||
/* | ||
* 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 { AgentEventSOAttributes } from './../../../../ingest_manager/common/types/models/agent'; | ||
import { | ||
AGENT_SAVED_OBJECT_TYPE, | ||
AGENT_EVENT_SAVED_OBJECT_TYPE, | ||
} from './../../../../ingest_manager/common/constants/agent'; | ||
import { Agent } from '../../../../ingest_manager/common'; | ||
import { savedObjectsClientMock } from 'src/core/server/mocks'; | ||
import { SavedObjectsClientContract, SavedObjectsFindResponse } from 'src/core/server'; | ||
import * as endpointTelemetry from './endpoint_metadata_telemetry'; | ||
import * as endpointTelemetrySavedObjects from './endpoint_telemetry_saved_objects'; | ||
|
||
const testAgentId = 'testAgentId'; | ||
const testConfigId = 'testConfigId'; | ||
const osPlatform = 'somePlatform'; | ||
const osName = 'somePlatformName'; | ||
const osFullName = 'somePlatformFullName'; | ||
const osVersion = '1'; | ||
|
||
const mockFleetObjectsResponse = ( | ||
lastCheckIn = new Date().toISOString() | ||
): SavedObjectsFindResponse<Agent> => ({ | ||
page: 1, | ||
per_page: 20, | ||
total: 1, | ||
saved_objects: [ | ||
{ | ||
type: AGENT_SAVED_OBJECT_TYPE, | ||
id: testAgentId, | ||
attributes: { | ||
active: true, | ||
id: testAgentId, | ||
config_id: 'randoConfigId', | ||
type: 'PERMANENT', | ||
user_provided_metadata: {}, | ||
enrolled_at: '2020-07-08T20:07:44.083Z', | ||
current_error_events: [], | ||
local_metadata: { | ||
elastic: { | ||
agent: { | ||
id: testAgentId, | ||
}, | ||
}, | ||
host: { | ||
hostname: 'testDesktop', | ||
name: 'testDesktop', | ||
id: 'randoHostId', | ||
}, | ||
os: { | ||
platform: osPlatform, | ||
version: osVersion, | ||
name: osName, | ||
full: osFullName, | ||
}, | ||
}, | ||
packages: [endpointTelemetrySavedObjects.ENDPOINT_PACKAGE_CONSTANT, 'system'], | ||
last_checkin: lastCheckIn, | ||
}, | ||
references: [], | ||
updated_at: '2020-07-08T20:55:09.216Z', | ||
version: 'WzI4MSwxXQ==', | ||
score: 0, | ||
}, | ||
], | ||
}); | ||
|
||
const mockFleetEventsObjectsResponse = ( | ||
running?: boolean, | ||
updatedDate = new Date().toISOString() | ||
): SavedObjectsFindResponse<AgentEventSOAttributes> => { | ||
return { | ||
page: 1, | ||
per_page: 20, | ||
total: 2, | ||
saved_objects: [ | ||
{ | ||
type: AGENT_EVENT_SAVED_OBJECT_TYPE, | ||
id: 'id1', | ||
attributes: { | ||
agent_id: testAgentId, | ||
type: running ? 'STATE' : 'ERROR', | ||
timestamp: updatedDate, | ||
subtype: running ? 'RUNNING' : 'FAILED', | ||
message: `Application: endpoint-security--8.0.0[d8f7f6e8-9375-483c-b456-b479f1d7a4f2]: State changed to ${ | ||
running ? 'RUNNING' : 'FAILED' | ||
}: `, | ||
config_id: testConfigId, | ||
}, | ||
references: [], | ||
updated_at: updatedDate, | ||
version: 'WzExOCwxXQ==', | ||
score: 0, | ||
}, | ||
{ | ||
type: AGENT_EVENT_SAVED_OBJECT_TYPE, | ||
id: 'id2', | ||
attributes: { | ||
agent_id: testAgentId, | ||
type: 'STATE', | ||
timestamp: updatedDate, | ||
subtype: 'STARTING', | ||
message: | ||
'Application: endpoint-security--8.0.0[d8f7f6e8-9375-483c-b456-b479f1d7a4f2]: State changed to STARTING: Starting', | ||
config_id: testConfigId, | ||
}, | ||
references: [], | ||
updated_at: updatedDate, | ||
version: 'WzExNywxXQ==', | ||
score: 0, | ||
}, | ||
], | ||
}; | ||
}; | ||
|
||
describe('test security solution endpoint telemetry', () => { | ||
let mockSavedObjectsClient: jest.Mocked<SavedObjectsClientContract>; | ||
let getFleetSavedObjectsMetadataSpy; | ||
let getFleetEventsSavedObjectsSpy; | ||
|
||
beforeAll(() => { | ||
getFleetEventsSavedObjectsSpy = jest.spyOn( | ||
endpointTelemetrySavedObjects, | ||
'getFleetEventsSavedObjects' | ||
); | ||
getFleetSavedObjectsMetadataSpy = jest.spyOn( | ||
endpointTelemetrySavedObjects, | ||
'getFleetSavedObjectsMetadata' | ||
); | ||
mockSavedObjectsClient = savedObjectsClientMock.create(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should have a default shape', () => { | ||
expect(endpointTelemetry.getDefaultEndpointTelemetry()).toMatchInlineSnapshot(` | ||
Object { | ||
"active_within_last_24_hours": 0, | ||
"os": Object {}, | ||
"total_installed": 0, | ||
} | ||
`); | ||
}); | ||
|
||
describe('when agents have not been installed', () => { | ||
it('should return the default shape if no agents are found', async () => { | ||
getFleetSavedObjectsMetadataSpy.mockImplementation(() => | ||
Promise.resolve({ saved_objects: [], total: 0, per_page: 0, page: 0 }) | ||
); | ||
|
||
const emptyEndpointTelemetryData = await endpointTelemetry.getEndpointMetadataTelemetryFromFleet( | ||
mockSavedObjectsClient | ||
); | ||
expect(getFleetSavedObjectsMetadataSpy).toHaveBeenCalled(); | ||
expect(emptyEndpointTelemetryData).toEqual({ | ||
total_installed: 0, | ||
active_within_last_24_hours: 0, | ||
os: {}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when agents have been installed', () => { | ||
let withinLastDay; | ||
let olderThanADay; | ||
beforeEach(() => { | ||
const currentTime = new Date(); | ||
|
||
withinLastDay = new Date(); | ||
withinLastDay.setHours(currentTime.getHours() - 10); | ||
withinLastDay = withinLastDay.toISOString(); | ||
|
||
olderThanADay = new Date(); | ||
olderThanADay.setDate(currentTime.getDate() - 2); | ||
olderThanADay = olderThanADay.toISOString(); | ||
}); | ||
|
||
it('should show one installed but no active endpoint', async () => { | ||
getFleetSavedObjectsMetadataSpy.mockImplementation(() => | ||
Promise.resolve(mockFleetObjectsResponse()) | ||
); | ||
getFleetEventsSavedObjectsSpy.mockImplementation(() => | ||
Promise.resolve(mockFleetEventsObjectsResponse()) | ||
); | ||
|
||
const emptyEndpointTelemetryData = await endpointTelemetry.getEndpointMetadataTelemetryFromFleet( | ||
mockSavedObjectsClient | ||
); | ||
expect(emptyEndpointTelemetryData).toEqual({ | ||
total_installed: 1, | ||
active_within_last_24_hours: 0, | ||
os: { | ||
[osName]: { | ||
name: osFullName, | ||
version: osVersion, | ||
count: 1, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('should show one installed with an active endpoint', async () => { | ||
getFleetSavedObjectsMetadataSpy.mockImplementation(() => | ||
Promise.resolve(mockFleetObjectsResponse()) | ||
); | ||
getFleetEventsSavedObjectsSpy.mockImplementation(() => | ||
Promise.resolve(mockFleetEventsObjectsResponse(true)) | ||
); | ||
|
||
const emptyEndpointTelemetryData = await endpointTelemetry.getEndpointMetadataTelemetryFromFleet( | ||
mockSavedObjectsClient | ||
); | ||
expect(emptyEndpointTelemetryData).toEqual({ | ||
total_installed: 1, | ||
active_within_last_24_hours: 1, | ||
os: { | ||
[osName]: { | ||
name: osFullName, | ||
version: osVersion, | ||
count: 1, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
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.