Skip to content

Commit

Permalink
[Asset management] Text updates (elastic#98192) (elastic#98703)
Browse files Browse the repository at this point in the history
* updated scheduled query activation toggle text and interval header in query group

* added id validation for schedule queries

* fixed up agent resolution to ignore inactive agents, and properly pull all agents

* nixed unused file

* more validation for query fields

* added status table to the results data tab, added more validation

* updated wording

* added error notifications for failed queries

* pr feedback and cleanup

* fix up last hook

* use the pluralize macro, removed rbac tags

Co-authored-by: Kibana Machine <[email protected]>

Co-authored-by: Bryan Clement <[email protected]>
  • Loading branch information
kibanamachine and lykkin authored Apr 29, 2021
1 parent c571387 commit 3ac08ba
Show file tree
Hide file tree
Showing 32 changed files with 532 additions and 163 deletions.
10 changes: 10 additions & 0 deletions x-pack/plugins/osquery/common/schemas/common/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export type Name = t.TypeOf<typeof name>;
export const nameOrUndefined = t.union([name, t.undefined]);
export type NameOrUndefined = t.TypeOf<typeof nameOrUndefined>;

export const agentSelection = t.type({
agents: t.array(t.string),
allAgentsSelected: t.boolean,
platformsSelected: t.array(t.string),
policiesSelected: t.array(t.string),
});
export type AgentSelection = t.TypeOf<typeof agentSelection>;
export const agentSelectionOrUndefined = t.union([agentSelection, t.undefined]);
export type AgentSelectionOrUndefined = t.TypeOf<typeof agentSelectionOrUndefined>;

export const description = t.string;
export type Description = t.TypeOf<typeof description>;
export const descriptionOrUndefined = t.union([description, t.undefined]);
Expand Down
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 * as t from 'io-ts';

import { query, agentSelection } from '../../common/schemas';

export const createActionRequestBodySchema = t.type({
agentSelection,
query,
});

export type CreateActionRequestBodySchema = t.OutputOf<typeof createActionRequestBodySchema>;
8 changes: 8 additions & 0 deletions x-pack/plugins/osquery/common/schemas/routes/action/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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 * from './create_action_request_body_schema';
14 changes: 12 additions & 2 deletions x-pack/plugins/osquery/public/action_results/use_action_results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { flatten, reverse, uniqBy } from 'lodash/fp';
import { useQuery } from 'react-query';

import { i18n } from '@kbn/i18n';
import { createFilter } from '../common/helpers';
import { useKibana } from '../common/lib/kibana';
import {
Expand All @@ -32,7 +33,7 @@ export interface ResultsArgs {
totalCount: number;
}

interface UseActionResults {
export interface UseActionResults {
actionId: string;
activePage: number;
agentIds?: string[];
Expand All @@ -55,7 +56,10 @@ export const useActionResults = ({
skip = false,
isLive = false,
}: UseActionResults) => {
const { data } = useKibana().services;
const {
data,
notifications: { toasts },
} = useKibana().services;

return useQuery(
['actionResults', { actionId }],
Expand Down Expand Up @@ -120,6 +124,12 @@ export const useActionResults = ({
refetchInterval: isLive ? 1000 : false,
keepPreviousData: true,
enabled: !skip && !!agentIds?.length,
onError: (error: Error) =>
toasts.addError(error, {
title: i18n.translate('xpack.osquery.action_results.fetchError', {
defaultMessage: 'Error while fetching action results',
}),
}),
}
);
};
12 changes: 11 additions & 1 deletion x-pack/plugins/osquery/public/actions/use_action_details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useQuery } from 'react-query';

import { i18n } from '@kbn/i18n';
import { createFilter } from '../common/helpers';
import { useKibana } from '../common/lib/kibana';
import {
Expand All @@ -32,7 +33,10 @@ interface UseActionDetails {
}

export const useActionDetails = ({ actionId, filterQuery, skip = false }: UseActionDetails) => {
const { data } = useKibana().services;
const {
data,
notifications: { toasts },
} = useKibana().services;

return useQuery(
['actionDetails', { actionId, filterQuery }],
Expand All @@ -57,6 +61,12 @@ export const useActionDetails = ({ actionId, filterQuery, skip = false }: UseAct
},
{
enabled: !skip,
onError: (error: Error) =>
toasts.addError(error, {
title: i18n.translate('xpack.osquery.action_details.fetchError', {
defaultMessage: 'Error while fetching action details',
}),
}),
}
);
};
12 changes: 11 additions & 1 deletion x-pack/plugins/osquery/public/actions/use_all_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useQuery } from 'react-query';

import { i18n } from '@kbn/i18n';
import { createFilter } from '../common/helpers';
import { useKibana } from '../common/lib/kibana';
import {
Expand Down Expand Up @@ -47,7 +48,10 @@ export const useAllActions = ({
filterQuery,
skip = false,
}: UseAllActions) => {
const { data } = useKibana().services;
const {
data,
notifications: { toasts },
} = useKibana().services;

return useQuery(
['actions', { activePage, direction, limit, sortField }],
Expand Down Expand Up @@ -78,6 +82,12 @@ export const useAllActions = ({
{
keepPreviousData: true,
enabled: !skip,
onError: (error: Error) =>
toasts.addError(error, {
title: i18n.translate('xpack.osquery.all_actions.fetchError', {
defaultMessage: 'Error while fetching actions',
}),
}),
}
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useQuery } from 'react-query';

import { i18n } from '@kbn/i18n';
import { useKibana } from '../common/lib/kibana';
import {
agentPolicyRouteService,
Expand All @@ -15,7 +16,10 @@ import {
} from '../../../fleet/common';

export const useAgentPolicies = () => {
const { http } = useKibana().services;
const {
http,
notifications: { toasts },
} = useKibana().services;

return useQuery<GetAgentPoliciesResponse, unknown, GetAgentPoliciesResponseItem[]>(
['agentPolicies'],
Expand All @@ -30,6 +34,12 @@ export const useAgentPolicies = () => {
placeholderData: [],
keepPreviousData: true,
select: (response) => response.items,
onError: (error) =>
toasts.addError(error as Error, {
title: i18n.translate('xpack.osquery.agent_policies.fetchError', {
defaultMessage: 'Error while fetching agent policies',
}),
}),
}
);
};
12 changes: 11 additions & 1 deletion x-pack/plugins/osquery/public/agent_policies/use_agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { useQuery } from 'react-query';

import { i18n } from '@kbn/i18n';
import { useKibana } from '../common/lib/kibana';
import { agentPolicyRouteService } from '../../../fleet/common';

Expand All @@ -16,7 +17,10 @@ interface UseAgentPolicy {
}

export const useAgentPolicy = ({ policyId, skip }: UseAgentPolicy) => {
const { http } = useKibana().services;
const {
http,
notifications: { toasts },
} = useKibana().services;

return useQuery(
['agentPolicy', { policyId }],
Expand All @@ -25,6 +29,12 @@ export const useAgentPolicy = ({ policyId, skip }: UseAgentPolicy) => {
enabled: !skip,
keepPreviousData: true,
select: (response) => response.item,
onError: (error: Error) =>
toasts.addError(error, {
title: i18n.translate('xpack.osquery.agent_policy_details.fetchError', {
defaultMessage: 'Error while fetching agent policy details',
}),
}),
}
);
};
12 changes: 11 additions & 1 deletion x-pack/plugins/osquery/public/agents/use_agent_groups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
import { useState } from 'react';
import { useQuery } from 'react-query';
import { i18n } from '@kbn/i18n';
import { useKibana } from '../common/lib/kibana';
import { useAgentPolicies } from './use_agent_policies';

Expand All @@ -24,7 +25,10 @@ interface UseAgentGroups {
}

export const useAgentGroups = ({ osqueryPolicies, osqueryPoliciesLoading }: UseAgentGroups) => {
const { data } = useKibana().services;
const {
data,
notifications: { toasts },
} = useKibana().services;

const { agentPoliciesLoading, agentPolicyById } = useAgentPolicies(osqueryPolicies);
const [platforms, setPlatforms] = useState<Group[]>([]);
Expand Down Expand Up @@ -96,6 +100,12 @@ export const useAgentGroups = ({ osqueryPolicies, osqueryPoliciesLoading }: UseA
},
{
enabled: !osqueryPoliciesLoading && !agentPoliciesLoading,
onError: (error) =>
toasts.addError(error as Error, {
title: i18n.translate('xpack.osquery.agent_groups.fetchError', {
defaultMessage: 'Error while fetching agent groups',
}),
}),
}
);

Expand Down
12 changes: 11 additions & 1 deletion x-pack/plugins/osquery/public/agents/use_agent_policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,27 @@

import { mapKeys } from 'lodash';
import { useQueries, UseQueryResult } from 'react-query';
import { i18n } from '@kbn/i18n';
import { useKibana } from '../common/lib/kibana';
import { agentPolicyRouteService, GetOneAgentPolicyResponse } from '../../../fleet/common';

export const useAgentPolicies = (policyIds: string[] = []) => {
const { http } = useKibana().services;
const {
http,
notifications: { toasts },
} = useKibana().services;

const agentResponse = useQueries(
policyIds.map((policyId) => ({
queryKey: ['agentPolicy', policyId],
queryFn: () => http.get(agentPolicyRouteService.getInfoPath(policyId)),
enabled: policyIds.length > 0,
onError: (error) =>
toasts.addError(error as Error, {
title: i18n.translate('xpack.osquery.action_policy_details.fetchError', {
defaultMessage: 'Error while fetching policy details',
}),
}),
}))
) as Array<UseQueryResult<GetOneAgentPolicyResponse>>;

Expand Down
12 changes: 11 additions & 1 deletion x-pack/plugins/osquery/public/agents/use_agent_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { useQuery } from 'react-query';

import { GetAgentStatusResponse, agentRouteService } from '../../../fleet/common';
Expand All @@ -16,7 +17,10 @@ interface UseAgentStatus {
}

export const useAgentStatus = ({ policyId, skip }: UseAgentStatus) => {
const { http } = useKibana().services;
const {
http,
notifications: { toasts },
} = useKibana().services;

return useQuery<GetAgentStatusResponse, unknown, GetAgentStatusResponse['results']>(
['agentStatus', policyId],
Expand All @@ -34,6 +38,12 @@ export const useAgentStatus = ({ policyId, skip }: UseAgentStatus) => {
{
enabled: !skip,
select: (response) => response.results,
onError: (error) =>
toasts.addError(error as Error, {
title: i18n.translate('xpack.osquery.agent_status.fetchError', {
defaultMessage: 'Error while fetching agent status',
}),
}),
}
);
};
12 changes: 11 additions & 1 deletion x-pack/plugins/osquery/public/agents/use_all_agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { i18n } from '@kbn/i18n';
import { useQuery } from 'react-query';

import { GetAgentsResponse, agentRouteService } from '../../../fleet/common';
Expand All @@ -27,7 +28,10 @@ export const useAllAgents = (
opts: RequestOptions = { perPage: 9000 }
) => {
const { perPage } = opts;
const { http } = useKibana().services;
const {
http,
notifications: { toasts },
} = useKibana().services;
const { isLoading: agentsLoading, data: agentData } = useQuery<GetAgentsResponse>(
['agents', osqueryPolicies, searchValue, perPage],
() => {
Expand All @@ -52,6 +56,12 @@ export const useAllAgents = (
},
{
enabled: !osqueryPoliciesLoading,
onError: (error) =>
toasts.addError(error as Error, {
title: i18n.translate('xpack.osquery.agents.fetchError', {
defaultMessage: 'Error while fetching agents',
}),
}),
}
);

Expand Down
27 changes: 22 additions & 5 deletions x-pack/plugins/osquery/public/agents/use_osquery_policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,41 @@
* 2.0.
*/

import { uniq } from 'lodash';
import { useQuery } from 'react-query';
import { useMemo } from 'react';
import { i18n } from '@kbn/i18n';
import { useKibana } from '../common/lib/kibana';
import { packagePolicyRouteService, PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../../../fleet/common';
import { OSQUERY_INTEGRATION_NAME } from '../../common';

export const useOsqueryPolicies = () => {
const { http } = useKibana().services;
const {
http,
notifications: { toasts },
} = useKibana().services;

const { isLoading: osqueryPoliciesLoading, data: osqueryPolicies } = useQuery(
const { isLoading: osqueryPoliciesLoading, data: osqueryPolicies = [] } = useQuery(
['osqueryPolicies'],
() =>
http.get(packagePolicyRouteService.getListPath(), {
query: {
kuery: `${PACKAGE_POLICY_SAVED_OBJECT_TYPE}.package.name:${OSQUERY_INTEGRATION_NAME}`,
},
}),
{ select: (data) => data.items.map((p: { policy_id: string }) => p.policy_id) }
{
select: (response) =>
uniq<string>(response.items.map((p: { policy_id: string }) => p.policy_id)),
onError: (error: Error) =>
toasts.addError(error, {
title: i18n.translate('xpack.osquery.osquery_policies.fetchError', {
defaultMessage: 'Error while fetching osquery policies',
}),
}),
}
);

return { osqueryPoliciesLoading, osqueryPolicies };
return useMemo(() => ({ osqueryPoliciesLoading, osqueryPolicies }), [
osqueryPoliciesLoading,
osqueryPolicies,
]);
};
Loading

0 comments on commit 3ac08ba

Please sign in to comment.