Skip to content

Commit

Permalink
[8.7] [Defend Workflows] Osquery fixes (#155020) (#155879)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.7`:
- [[Defend Workflows] Osquery fixes
(#155020)](#155020)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Tomasz
Ciecierski","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-04-26T13:34:06Z","message":"[Defend
Workflows] Osquery fixes
(#155020)","sha":"fda5ee96b37f186378d94a7b6a15b295d9616168","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","Team:Defend
Workflows","Feature:Osquery","v8.8.0","v8.7.2","v8.9.0"],"number":155020,"url":"https://github.com/elastic/kibana/pull/155020","mergeCommit":{"message":"[Defend
Workflows] Osquery fixes
(#155020)","sha":"fda5ee96b37f186378d94a7b6a15b295d9616168"}},"sourceBranch":"main","suggestedTargetBranches":["8.7","8.9"],"targetPullRequestStates":[{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/155020","number":155020,"mergeCommit":{"message":"[Defend
Workflows] Osquery fixes
(#155020)","sha":"fda5ee96b37f186378d94a7b6a15b295d9616168"}},{"branch":"8.7","label":"v8.7.2","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.9","label":"v8.9.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Tomasz Ciecierski <[email protected]>
  • Loading branch information
kibanamachine and tomsonpl authored Apr 26, 2023
1 parent e7fd103 commit 7f69a76
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
20 changes: 6 additions & 14 deletions x-pack/plugins/osquery/public/live_queries/form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,8 @@ const LiveQueryFormComponent: React.FC<LiveQueryFormProps> = ({
);

const hooksForm = useHookForm<LiveQueryFormFields>();
const {
handleSubmit,
watch,
setValue,
resetField,
clearErrors,
getFieldState,
register,
formState: { isSubmitting },
} = hooksForm;
const { handleSubmit, watch, setValue, resetField, clearErrors, getFieldState, register } =
hooksForm;

const canRunSingleQuery = useMemo(
() =>
Expand Down Expand Up @@ -157,15 +149,15 @@ const LiveQueryFormComponent: React.FC<LiveQueryFormProps> = ({
saved_query_id: values.savedQueryId,
query,
alert_ids: values.alertIds,
pack_id: values?.packId?.length ? values?.packId[0] : undefined,
pack_id: queryType === 'pack' && values?.packId?.length ? values?.packId[0] : undefined,
ecs_mapping: values.ecs_mapping,
},
(value) => !isEmpty(value)
) as unknown as LiveQueryFormFields;

await mutateAsync(serializedData);
},
[alertAttachmentContext, mutateAsync]
[alertAttachmentContext, mutateAsync, queryType]
);

const serializedData: SavedQuerySOFormData = useMemo(
Expand Down Expand Up @@ -196,7 +188,7 @@ const LiveQueryFormComponent: React.FC<LiveQueryFormProps> = ({
<EuiButton
id="submit-button"
disabled={!enabled}
isLoading={isSubmitting}
isLoading={isLoading}
onClick={handleSubmit(onSubmit)}
>
<FormattedMessage
Expand All @@ -215,7 +207,7 @@ const LiveQueryFormComponent: React.FC<LiveQueryFormProps> = ({
resultsStatus,
handleShowSaveQueryFlyout,
enabled,
isSubmitting,
isLoading,
handleSubmit,
onSubmit,
]
Expand Down
15 changes: 15 additions & 0 deletions x-pack/plugins/osquery/server/common/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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.
*/

export class CustomHttpRequestError extends Error {
constructor(message: string, public readonly statusCode: number = 500) {
super(message);
// For debugging - capture name of subclasses
this.name = this.constructor.name;
this.message = message;
}
}
4 changes: 4 additions & 0 deletions x-pack/plugins/osquery/server/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ export interface SavedQuerySavedObjectAttributes {
}

export type SavedQuerySavedObject = SavedObject<PackSavedObjectAttributes>;

export interface HTTPError extends Error {
statusCode: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { convertSOQueriesToPack } from '../../routes/pack/utils';
import { ACTIONS_INDEX } from '../../../common/constants';
import { TELEMETRY_EBT_LIVE_QUERY_EVENT } from '../../lib/telemetry/constants';
import type { PackSavedObjectAttributes } from '../../common/types';
import { CustomHttpRequestError } from '../../common/error';

interface Metadata {
currentUser: string | undefined;
Expand Down Expand Up @@ -55,7 +56,7 @@ export const createActionHandler = async (
});

if (!selectedAgents.length) {
throw new Error('No agents found for selection');
throw new CustomHttpRequestError('No agents found for selection', 400);
}

let packSO;
Expand Down
13 changes: 11 additions & 2 deletions x-pack/plugins/osquery/server/lib/fleet_integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,20 @@ export const getPackagePolicyDeleteCallback =
await Promise.all(
map(
foundPacks.saved_objects,
(pack: { id: string; references: SavedObjectReference[] }) =>
(pack: {
id: string;
references: SavedObjectReference[];
attributes: { shards: Array<{ key: string; value: string }> };
}) =>
packsClient.update(
packSavedObjectType,
pack.id,
{},
{
shards: filter(
pack.attributes.shards,
(shard) => shard.key !== deletedOsqueryManagerPolicy.policy_id
),
},
{
references: filter(
pack.references,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ export const createLiveQueryRoute = (router: IRouter, osqueryContext: OsqueryApp
body: { data: osqueryAction },
});
} catch (error) {
// TODO validate for 400 (when agents are not found for selection)
// return response.badRequest({ body: new Error('No agents found for selection') });
if (error.statusCode === 400) {
return response.badRequest({ body: error });
}

return response.customError({
statusCode: 500,
Expand Down

0 comments on commit 7f69a76

Please sign in to comment.