Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into total-number-cases-…
Browse files Browse the repository at this point in the history
…update
  • Loading branch information
adcoelho committed Jul 3, 2023
2 parents 54fdbef + 61b792f commit 3fca220
Show file tree
Hide file tree
Showing 62 changed files with 855 additions and 473 deletions.
1 change: 1 addition & 0 deletions x-pack/performance/journeys/cloud_security_dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const journey = new Journey({
const response = await kibanaServer.request({
path: '/internal/cloud_security_posture/status?check=init',
method: 'GET',
headers: { 'elastic-api-version': '1' },
});
expect(response.status).to.eql(200);
expect(response.data).to.eql({ isPluginInitialized: true });
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/cloud_security_posture/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import { PostureTypes, VulnSeverity } from './types';

export const STATUS_ROUTE_PATH = '/internal/cloud_security_posture/status';
export const STATUS_API_CURRENT_VERSION = '1';

export const STATS_ROUTE_PATH = '/internal/cloud_security_posture/stats/{policy_template}';

export const VULNERABILITIES_DASHBOARD_ROUTE_PATH =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { useQuery, type UseQueryOptions } from '@tanstack/react-query';
import { useKibana } from '../hooks/use_kibana';
import { type CspSetupStatus } from '../../../common/types';
import { STATUS_ROUTE_PATH } from '../../../common/constants';
import { STATUS_API_CURRENT_VERSION, STATUS_ROUTE_PATH } from '../../../common/constants';

const getCspSetupStatusQueryKey = 'csp_status_key';

Expand All @@ -18,7 +18,7 @@ export const useCspSetupStatusApi = (
const { http } = useKibana().services;
return useQuery<CspSetupStatus, unknown, CspSetupStatus>(
[getCspSetupStatusQueryKey],
() => http.get<CspSetupStatus>(STATUS_ROUTE_PATH),
() => http.get<CspSetupStatus>(STATUS_ROUTE_PATH, { version: STATUS_API_CURRENT_VERSION }),
options
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import { NewPackagePolicy } from '@kbn/fleet-plugin/common';
import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';
import pMap from 'p-map';
import { transformError } from '@kbn/securitysolution-es-utils';
import { GetCspRuleTemplateRequest, GetCspRuleTemplateResponse } from '../../../common/types';
import { CspRuleTemplate } from '../../../common/schemas';
Expand Down Expand Up @@ -61,13 +60,9 @@ const findCspRuleTemplateHandler = async (
filter: getBenchmarkTypeFilter(benchmarkId),
});

const cspRulesTemplates = await pMap(
cspRulesTemplatesSo.saved_objects,
async (cspRuleTemplate) => {
return { ...cspRuleTemplate.attributes };
},
{ concurrency: 50 }
);
const cspRulesTemplates = cspRulesTemplatesSo.saved_objects.map((cspRuleTemplate) => {
return { ...cspRuleTemplate.attributes };
});

return {
items: cspRulesTemplates,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
import moment from 'moment';
import { Installation, PackagePolicy } from '@kbn/fleet-plugin/common';
import { schema } from '@kbn/config-schema';
import { VersionedRoute } from '@kbn/core-http-server/src/versioning/types';
import {
CLOUD_SECURITY_POSTURE_PACKAGE_NAME,
STATUS_ROUTE_PATH,
Expand All @@ -29,7 +30,12 @@ import {
LATEST_VULNERABILITIES_INDEX_DEFAULT_NS,
VULN_MGMT_POLICY_TEMPLATE,
} from '../../../common/constants';
import type { CspApiRequestHandlerContext, CspRouter, StatusResponseInfo } from '../../types';
import type {
CspApiRequestHandlerContext,
CspRequestHandlerContext,
CspRouter,
StatusResponseInfo,
} from '../../types';
import type {
CspSetupStatus,
CspStatusCode,
Expand Down Expand Up @@ -328,44 +334,55 @@ export const statusQueryParamsSchema = schema.object({
check: schema.oneOf([schema.literal('all'), schema.literal('init')], { defaultValue: 'all' }),
});

export const defineGetCspStatusRoute = (router: CspRouter): void =>
router.get(
{
export const defineGetCspStatusRoute = (
router: CspRouter
): VersionedRoute<'get', CspRequestHandlerContext> =>
router.versioned
.get({
access: 'internal',
path: STATUS_ROUTE_PATH,
validate: { query: statusQueryParamsSchema },
options: {
tags: ['access:cloud-security-posture-read'],
},
},
async (context, request, response) => {
const cspContext = await context.csp;
try {
if (request.query.check === 'init') {
})
.addVersion(
{
version: '1',
validate: {
request: {
query: statusQueryParamsSchema,
},
},
},
async (context, request, response) => {
const cspContext = await context.csp;
try {
if (request.query.check === 'init') {
return response.ok({
body: {
isPluginInitialized: cspContext.isPluginInitialized(),
},
});
}
const status: CspSetupStatus = await getCspStatus({
...cspContext,
esClient: cspContext.esClient.asCurrentUser,
});
return response.ok({
body: {
isPluginInitialized: cspContext.isPluginInitialized(),
},
body: status,
});
} catch (err) {
cspContext.logger.error(`Error getting csp status`);
cspContext.logger.error(err);

const error = transformError(err);
return response.customError({
body: { message: error.message },
statusCode: error.statusCode,
});
}
const status = await getCspStatus({
...cspContext,
esClient: cspContext.esClient.asCurrentUser,
});
return response.ok({
body: status,
});
} catch (err) {
cspContext.logger.error(`Error getting csp status`);
cspContext.logger.error(err);

const error = transformError(err);
return response.customError({
body: { message: error.message },
statusCode: error.statusCode,
});
}
}
);
);

const getStatusResponse = (statusResponseInfo: StatusResponseInfo) => {
const {
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/cloud_security_posture/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"@kbn/shared-ux-router",
"@kbn/core-saved-objects-server",
"@kbn/share-plugin",
"@kbn/core-http-server",
],
"exclude": [
"target/**/*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export class EndpointMetadataGenerator extends BaseDataGenerator {
},
},
},
last_checkin: new Date().toISOString(),
};
return merge(hostInfo, overrides);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export async function indexEndpointHostDocs({
await client
.index({
index: metadataIndex,
body: hostMetadata,
document: hostMetadata,
op_type: 'create',
refresh: 'wait_for',
})
Expand All @@ -225,7 +225,7 @@ export async function indexEndpointHostDocs({
await client
.index({
index: policyResponseIndex,
body: hostPolicyResponse,
document: hostPolicyResponse,
op_type: 'create',
refresh: 'wait_for',
})
Expand Down Expand Up @@ -281,19 +281,17 @@ export const deleteIndexedEndpointHosts = async (
};

if (indexedData.hosts.length) {
const body = {
query: {
bool: {
filter: [{ terms: { 'agent.id': indexedData.hosts.map((host) => host.agent.id) } }],
},
const query = {
bool: {
filter: [{ terms: { 'agent.id': indexedData.hosts.map((host) => host.agent.id) } }],
},
};

response.hosts = await esClient
.deleteByQuery({
index: indexedData.metadataIndex,
wait_for_completion: true,
body,
query,
})
.catch(wrapErrorAndRejectPromise);

Expand All @@ -302,7 +300,7 @@ export const deleteIndexedEndpointHosts = async (
.deleteByQuery({
index: metadataCurrentIndexPattern,
wait_for_completion: true,
body,
query,
})
.catch(wrapErrorAndRejectPromise);
}
Expand All @@ -312,19 +310,17 @@ export const deleteIndexedEndpointHosts = async (
.deleteByQuery({
index: indexedData.policyResponseIndex,
wait_for_completion: true,
body: {
query: {
bool: {
filter: [
{
terms: {
'agent.id': indexedData.policyResponses.map(
(policyResponse) => policyResponse.agent.id
),
},
query: {
bool: {
filter: [
{
terms: {
'agent.id': indexedData.policyResponses.map(
(policyResponse) => policyResponse.agent.id
),
},
],
},
},
],
},
},
})
Expand Down
14 changes: 11 additions & 3 deletions x-pack/plugins/security_solution/common/endpoint/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ export type PolicyInfo = Immutable<{
}>;

// Host Information as returned by the Host Details API.
// NOTE: `HostInfo` type is the original and defined as Immutable.
// NOTE:The `HostInfo` type is the original and defined as Immutable.
export interface HostInfoInterface {
metadata: HostMetadataInterface;
host_status: HostStatus;
Expand All @@ -485,7 +485,7 @@ export interface HostInfoInterface {
*/
configured: PolicyInfo;
/**
* Last reported running in agent (may lag behind configured)
* Last reported running in agent (might lag behind configured)
*/
applied: PolicyInfo;
};
Expand All @@ -494,14 +494,22 @@ export interface HostInfoInterface {
*/
endpoint: PolicyInfo;
};
/**
* The time when the Elastic Agent associated with this Endpoint host checked in with fleet
* Conceptually the value is the same as Agent['last_checkin'] if present, but we fall back to
* UnitedAgentMetadataPersistedData['united']['endpoint']['metadata']['@timestamp']
* if `Agent.last_checkin` value is `undefined`
*/
last_checkin: string;
}

export type HostInfo = Immutable<HostInfoInterface>;

// Host metadata document streamed up to ES by the Endpoint running on host machines.
// NOTE: `HostMetadata` type is the original and defined as Immutable. If needing to
// NOTE: The `HostMetadata` type is the original and defined as Immutable. If you need to
// work with metadata that is not mutable, use `HostMetadataInterface`
export type HostMetadata = Immutable<HostMetadataInterface>;

export interface HostMetadataInterface {
'@timestamp': number;
event: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ describe('When using processes action from response actions console', () => {
};

const endpointDetailsMock = () => {
const newDate = new Date('2023-04-20T09:37:40.309Z');
const endpointMetadata = new EndpointMetadataGenerator('seed').generateHostInfo({
metadata: {
'@timestamp': new Date('2023-04-20T09:37:40.309Z').getTime(),
'@timestamp': newDate.getTime(),
agent: {
id: agentId,
version: '8.8.0',
Expand All @@ -69,6 +70,7 @@ describe('When using processes action from response actions console', () => {
},
},
},
last_checkin: newDate.toISOString(),
});
useGetEndpointDetailsMock.mockReturnValue({
data: endpointMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import React, { memo, useEffect, useMemo, useCallback } from 'react';
import React, { memo, useCallback, useEffect, useMemo } from 'react';
import { EuiDescriptionList } from '@elastic/eui';
import { v4 as uuidV4 } from 'uuid';
import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -242,7 +242,7 @@ export const EndpointStatusActionResult = memo<
'xpack.securitySolution.endpointResponseActions.status.lastActive',
{ defaultMessage: 'Last active' }
)}
value={endpointDetails.metadata['@timestamp']}
value={endpointDetails.last_checkin}
/>
</ConsoleCodeBlock>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Responder header endpoint info', () => {
);
expect(agentStatus.textContent).toBe(`UnhealthyIsolating`);
});
it('should show last updated time', async () => {
it('should show last checkin time', async () => {
const lastUpdated = await renderResult.findByTestId('responderHeaderLastSeen');
expect(lastUpdated).toBeTruthy();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import React, { memo } from 'react';
import {
EuiFlexGroup,
EuiFlexItem,
EuiText,
EuiSkeletonText,
EuiToolTip,
EuiSpacer,
EuiText,
EuiToolTip,
} from '@elastic/eui';
import { euiStyled } from '@kbn/kibana-react-plugin/common';
import { FormattedMessage, FormattedRelative } from '@kbn/i18n-react';
Expand Down Expand Up @@ -88,7 +88,7 @@ export const HeaderEndpointInfo = memo<HeaderEndpointInfoProps>(({ endpointId })
id="xpack.securitySolution.responder.header.lastSeen"
defaultMessage="Last seen {date}"
values={{
date: <FormattedRelative value={endpointDetails.metadata['@timestamp']} />,
date: <FormattedRelative value={endpointDetails.last_checkin} />,
}}
/>
</EuiText>
Expand Down
Loading

0 comments on commit 3fca220

Please sign in to comment.