Skip to content

Commit

Permalink
Merge branch 'master' into transpile-kbn-alerts-with-babel
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Aug 19, 2021
2 parents ddeb770 + 6317202 commit 1726a1f
Show file tree
Hide file tree
Showing 41 changed files with 6,225 additions and 955 deletions.
4 changes: 4 additions & 0 deletions docs/settings/monitoring-settings.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ For more information, see
monitoring back-end does not run and {kib} stats are not sent to the monitoring
cluster.

| `monitoring.ui.ccs.enabled`
| Set to `true` (default) to enable {ref}/modules-cross-cluster-search.html[cross-cluster search] of your monitoring data. The {ref}/modules-remote-clusters.html#remote-cluster-settings[`remote_cluster_client`] role must exist on each node.


| `monitoring.ui.elasticsearch.hosts`
| Specifies the location of the {es} cluster where your monitoring data is stored.
By default, this is the same as <<elasticsearch-hosts, `elasticsearch.hosts`>>. This setting enables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ const apmRoutes = route([
}),
}),
]),
defaults: {
query: {
rangeFrom: 'now-15m',
rangeTo: 'now',
},
},
},
{
path: '/',
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/apm/public/components/routing/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { i18n } from '@kbn/i18n';
import { Outlet } from '@kbn/typed-react-router-config';
import * as t from 'io-ts';
import React from 'react';
import { Redirect } from 'react-router-dom';
import { RedirectTo } from '../redirect_to';
import { ENVIRONMENT_ALL } from '../../../../common/environment_filter_values';
import { environmentRt } from '../../../../common/environment_rt';
import { BackendDetailOverview } from '../../app/backend_detail_overview';
Expand Down Expand Up @@ -121,7 +121,7 @@ export const home = {
},
{
path: '/',
element: <Redirect to="/services" />,
element: <RedirectTo pathname="/services" />,
},
],
} as const;
63 changes: 40 additions & 23 deletions x-pack/plugins/apm/public/components/routing/redirect_to.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,50 @@
*/

import React from 'react';
import { Redirect, RouteComponentProps } from 'react-router-dom';
import { Location } from 'history';
import { Redirect, useLocation, RouteComponentProps } from 'react-router-dom';

/**
* Given a path, redirect to that location, preserving the search and maintaining
* backward-compatibilty with legacy (pre-7.9) hash-based URLs.
* Function that returns a react component to redirect to a given pathname removing hash-based URLs
* @param pathname
*/
export function redirectTo(to: string) {
export function redirectTo(pathname: string) {
return ({ location }: RouteComponentProps<{}>) => {
let resolvedUrl: URL | undefined;
return <RenderRedirectTo location={location} pathname={pathname} />;
};
}

// Redirect root URLs with a hash to support backward compatibility with URLs
// from before we switched to the non-hash platform history.
if (location.pathname === '' && location.hash.length > 0) {
// We just want the search and pathname so the host doesn't matter
resolvedUrl = new URL(location.hash.slice(1), 'http://localhost');
to = resolvedUrl.pathname;
}
/**
* React component to redirect to a given pathname removing hash-based URLs
* @param param0
*/
export function RedirectTo({ pathname }: { pathname: string }) {
const location = useLocation();
return <RenderRedirectTo location={location} pathname={pathname} />;
}

return (
<Redirect
to={{
...location,
hash: '',
pathname: to,
search: resolvedUrl ? resolvedUrl.search : location.search,
}}
/>
);
};
interface Props {
location: Location;
pathname: string;
}

/**
* Given a pathname, redirect to that location, preserving the search and maintaining
* backward-compatibilty with legacy (pre-7.9) hash-based URLs.
*/
function RenderRedirectTo(props: Props) {
const { location } = props;
let search = location.search;
let pathname = props.pathname;

// Redirect root URLs with a hash to support backward compatibility with URLs
// from before we switched to the non-hash platform history.
if (location.pathname === '' && location.hash.length > 0) {
// We just want the search and pathname so the host doesn't matter
const resolvedUrl = new URL(location.hash.slice(1), 'http://localhost');
search = resolvedUrl.search;
pathname = resolvedUrl.pathname;
}

return <Redirect to={{ ...location, hash: '', pathname, search }} />;
}
1 change: 1 addition & 0 deletions x-pack/plugins/cases/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"id":"cases",
"kibanaVersion":"kibana",
"optionalPlugins":[
"ruleRegistry",
"security",
"spaces"
],
Expand Down
14 changes: 3 additions & 11 deletions x-pack/plugins/cases/server/client/alerts/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,11 @@ export const get = async (
{ alertsInfo }: AlertGet,
clientArgs: CasesClientArgs
): Promise<CasesClientGetAlertsResponse> => {
const { alertsService, scopedClusterClient, logger } = clientArgs;
const { alertsService, logger } = clientArgs;
if (alertsInfo.length === 0) {
return [];
}

const alerts = await alertsService.getAlerts({ alertsInfo, scopedClusterClient, logger });
if (!alerts) {
return [];
}

return alerts.docs.map((alert) => ({
id: alert._id,
index: alert._index,
...alert._source,
}));
const alerts = await alertsService.getAlerts({ alertsInfo, logger });
return alerts ?? [];
};
12 changes: 1 addition & 11 deletions x-pack/plugins/cases/server/client/alerts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@

import { CaseStatuses } from '../../../common/api';
import { AlertInfo } from '../../common';

interface Alert {
id: string;
index: string;
destination?: {
ip: string;
};
source?: {
ip: string;
};
}
import { Alert } from '../../services/alerts/types';

export type CasesClientGetAlertsResponse = Alert[];

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/server/client/alerts/update_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export const updateStatus = async (
{ alerts }: UpdateAlertsStatusArgs,
clientArgs: CasesClientArgs
): Promise<void> => {
const { alertsService, scopedClusterClient, logger } = clientArgs;
await alertsService.updateAlertsStatus({ alerts, scopedClusterClient, logger });
const { alertsService, logger } = clientArgs;
await alertsService.updateAlertsStatus({ alerts, logger });
};
34 changes: 3 additions & 31 deletions x-pack/plugins/cases/server/client/attachments/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ import {
} from '../../services/user_actions/helpers';

import { AttachmentService, CasesService, CaseUserActionService } from '../../services';
import {
createCaseError,
CommentableCase,
createAlertUpdateRequest,
isCommentRequestTypeGenAlert,
} from '../../common';
import { createCaseError, CommentableCase, isCommentRequestTypeGenAlert } from '../../common';
import { CasesClientArgs, CasesClientInternal } from '..';

import { decodeCommentRequest } from '../utils';
Expand Down Expand Up @@ -195,22 +190,9 @@ const addGeneratedAlerts = async (
user: userDetails,
commentReq: query,
id: savedObjectID,
casesClientInternal,
});

if (
(newComment.attributes.type === CommentType.alert ||
newComment.attributes.type === CommentType.generatedAlert) &&
caseInfo.attributes.settings.syncAlerts
) {
const alertsToUpdate = createAlertUpdateRequest({
comment: query,
status: subCase.attributes.status,
});
await casesClientInternal.alerts.updateStatus({
alerts: alertsToUpdate,
});
}

await userActionService.bulkCreate({
unsecuredSavedObjectsClient,
actions: [
Expand Down Expand Up @@ -386,19 +368,9 @@ export const addComment = async (
user: userInfo,
commentReq: query,
id: savedObjectID,
casesClientInternal,
});

if (newComment.attributes.type === CommentType.alert && updatedCase.settings.syncAlerts) {
const alertsToUpdate = createAlertUpdateRequest({
comment: query,
status: updatedCase.status,
});

await casesClientInternal.alerts.updateStatus({
alerts: alertsToUpdate,
});
}

await userActionService.bulkCreate({
unsecuredSavedObjectsClient,
actions: [
Expand Down
65 changes: 44 additions & 21 deletions x-pack/plugins/cases/server/client/cases/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import Boom from '@hapi/boom';
import { SavedObjectsFindResponse, SavedObject } from 'kibana/server';
import { SavedObjectsFindResponse, SavedObject, Logger } from 'kibana/server';

import {
ActionConnector,
Expand All @@ -22,26 +22,16 @@ import {
import { buildCaseUserActionItem } from '../../services/user_actions/helpers';

import { createIncident, getCommentContextFromAttributes } from './utils';
import { createCaseError, flattenCaseSavedObject, getAlertInfoFromComments } from '../../common';
import {
AlertInfo,
createCaseError,
flattenCaseSavedObject,
getAlertInfoFromComments,
} from '../../common';
import { CasesClient, CasesClientArgs, CasesClientInternal } from '..';
import { Operations } from '../../authorization';
import { casesConnectors } from '../../connectors';

/**
* Returns true if the case should be closed based on the configuration settings and whether the case
* is a collection. Collections are not closable because we aren't allowing their status to be changed.
* In the future we could allow push to close all the sub cases of a collection but that's not currently supported.
*/
function shouldCloseByPush(
configureSettings: SavedObjectsFindResponse<CasesConfigureAttributes>,
caseInfo: SavedObject<CaseAttributes>
): boolean {
return (
configureSettings.total > 0 &&
configureSettings.saved_objects[0].attributes.closure_type === 'close-by-pushing' &&
caseInfo.attributes.type !== CaseType.collection
);
}
import { CasesClientGetAlertsResponse } from '../alerts/types';

/**
* Parameters for pushing a case to an external system
Expand Down Expand Up @@ -106,9 +96,7 @@ export const push = async (

const alertsInfo = getAlertInfoFromComments(theCase?.comments);

const alerts = await casesClientInternal.alerts.get({
alertsInfo,
});
const alerts = await getAlertsCatchErrors({ casesClientInternal, alertsInfo, logger });

const getMappingsResponse = await casesClientInternal.configuration.getMappings({
connector: theCase.connector,
Expand Down Expand Up @@ -278,3 +266,38 @@ export const push = async (
throw createCaseError({ message: `Failed to push case: ${error}`, error, logger });
}
};

async function getAlertsCatchErrors({
casesClientInternal,
alertsInfo,
logger,
}: {
casesClientInternal: CasesClientInternal;
alertsInfo: AlertInfo[];
logger: Logger;
}): Promise<CasesClientGetAlertsResponse> {
try {
return await casesClientInternal.alerts.get({
alertsInfo,
});
} catch (error) {
logger.error(`Failed to retrieve alerts during push: ${error}`);
return [];
}
}

/**
* Returns true if the case should be closed based on the configuration settings and whether the case
* is a collection. Collections are not closable because we aren't allowing their status to be changed.
* In the future we could allow push to close all the sub cases of a collection but that's not currently supported.
*/
function shouldCloseByPush(
configureSettings: SavedObjectsFindResponse<CasesConfigureAttributes>,
caseInfo: SavedObject<CaseAttributes>
): boolean {
return (
configureSettings.total > 0 &&
configureSettings.saved_objects[0].attributes.closure_type === 'close-by-pushing' &&
caseInfo.attributes.type !== CaseType.collection
);
}
27 changes: 17 additions & 10 deletions x-pack/plugins/cases/server/client/cases/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { fold } from 'fp-ts/lib/Either';
import { identity } from 'fp-ts/lib/function';

import {
Logger,
SavedObject,
SavedObjectsClientContract,
SavedObjectsFindResponse,
Expand Down Expand Up @@ -307,12 +308,14 @@ async function updateAlerts({
caseService,
unsecuredSavedObjectsClient,
casesClientInternal,
logger,
}: {
casesWithSyncSettingChangedToOn: UpdateRequestWithOriginalCase[];
casesWithStatusChangedAndSynced: UpdateRequestWithOriginalCase[];
caseService: CasesService;
unsecuredSavedObjectsClient: SavedObjectsClientContract;
casesClientInternal: CasesClientInternal;
logger: Logger;
}) {
/**
* It's possible that a case ID can appear multiple times in each array. I'm intentionally placing the status changes
Expand Down Expand Up @@ -361,7 +364,9 @@ async function updateAlerts({
[]
);

await casesClientInternal.alerts.updateStatus({ alerts: alertsToUpdate });
await casesClientInternal.alerts.updateStatus({
alerts: alertsToUpdate,
});
}

function partitionPatchRequest(
Expand Down Expand Up @@ -562,15 +567,6 @@ export const update = async (
);
});

// Update the alert's status to match any case status or sync settings changes
await updateAlerts({
casesWithStatusChangedAndSynced,
casesWithSyncSettingChangedToOn,
caseService,
unsecuredSavedObjectsClient,
casesClientInternal,
});

const returnUpdatedCase = myCases.saved_objects
.filter((myCase) =>
updatedCases.saved_objects.some((updatedCase) => updatedCase.id === myCase.id)
Expand Down Expand Up @@ -598,6 +594,17 @@ export const update = async (
}),
});

// Update the alert's status to match any case status or sync settings changes
// Attempt to do this after creating/changing the other entities just in case it fails
await updateAlerts({
casesWithStatusChangedAndSynced,
casesWithSyncSettingChangedToOn,
caseService,
unsecuredSavedObjectsClient,
casesClientInternal,
logger,
});

return CasesResponseRt.encode(returnUpdatedCase);
} catch (error) {
const idVersions = cases.cases.map((caseInfo) => ({
Expand Down
Loading

0 comments on commit 1726a1f

Please sign in to comment.