Skip to content

Commit

Permalink
[Osquery] Add support for platform and version fields (#101835)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrykkopycinski committed Jun 15, 2021
1 parent 7683d9e commit 35cc59b
Show file tree
Hide file tree
Showing 32 changed files with 1,115 additions and 387 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const DEFAULT_OPTIONS = {
stripEmptyFields: true,
};

interface UseFormReturn<T extends FormData, I extends FormData> {
export interface UseFormReturn<T extends FormData, I extends FormData> {
form: FormHook<T, I>;
}

Expand Down
123 changes: 123 additions & 0 deletions x-pack/plugins/fleet/server/services/package_policy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,129 @@ describe('Package policy service', () => {
expect(modifiedStream.vars!.paths.value).toEqual(expect.arrayContaining(['north', 'south']));
expect(modifiedStream.vars!.period.value).toEqual('12mo');
});

it('should add new input vars when updating', async () => {
const savedObjectsClient = savedObjectsClientMock.create();
const mockPackagePolicy = createPackagePolicyMock();
const mockInputs = [
{
config: {},
enabled: true,
keep_enabled: true,
type: 'endpoint',
vars: {
dog: {
type: 'text',
value: 'dalmatian',
},
cat: {
type: 'text',
value: 'siamese',
frozen: true,
},
},
streams: [
{
data_stream: {
type: 'birds',
dataset: 'migratory.patterns',
},
enabled: false,
id: `endpoint-migratory.patterns-${mockPackagePolicy.id}`,
vars: {
paths: {
value: ['north', 'south'],
type: 'text',
frozen: true,
},
},
},
],
},
];
const inputsUpdate = [
{
config: {},
enabled: false,
type: 'endpoint',
vars: {
dog: {
type: 'text',
value: 'labrador',
},
cat: {
type: 'text',
value: 'tabby',
},
},
streams: [
{
data_stream: {
type: 'birds',
dataset: 'migratory.patterns',
},
enabled: false,
id: `endpoint-migratory.patterns-${mockPackagePolicy.id}`,
vars: {
paths: {
value: ['east', 'west'],
type: 'text',
},
period: {
value: '12mo',
type: 'text',
},
},
},
],
},
];
const attributes = {
...mockPackagePolicy,
inputs: mockInputs,
};

savedObjectsClient.get.mockResolvedValue({
id: 'test',
type: 'abcd',
references: [],
version: 'test',
attributes,
});

savedObjectsClient.update.mockImplementation(
async (
type: string,
id: string,
attrs: any
): Promise<SavedObjectsUpdateResponse<PackagePolicySOAttributes>> => {
savedObjectsClient.get.mockResolvedValue({
id: 'test',
type: 'abcd',
references: [],
version: 'test',
attributes: attrs,
});
return attrs;
}
);
const elasticsearchClient = elasticsearchServiceMock.createClusterClient().asInternalUser;

const result = await packagePolicyService.update(
savedObjectsClient,
elasticsearchClient,
'the-package-policy-id',
{ ...mockPackagePolicy, inputs: inputsUpdate }
);

const [modifiedInput] = result.inputs;
expect(modifiedInput.enabled).toEqual(true);
expect(modifiedInput.vars!.dog.value).toEqual('labrador');
expect(modifiedInput.vars!.cat.value).toEqual('siamese');
const [modifiedStream] = modifiedInput.streams;
expect(modifiedStream.vars!.paths.value).toEqual(expect.arrayContaining(['north', 'south']));
expect(modifiedStream.vars!.period.value).toEqual('12mo');
});
});

describe('runExternalCallbacks', () => {
Expand Down
11 changes: 8 additions & 3 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,16 @@ function _enforceFrozenVars(
newVars: Record<string, PackagePolicyConfigRecordEntry>
) {
const resultVars: Record<string, PackagePolicyConfigRecordEntry> = {};
for (const [key, val] of Object.entries(newVars)) {
if (oldVars[key]?.frozen) {
resultVars[key] = oldVars[key];
} else {
resultVars[key] = val;
}
}
for (const [key, val] of Object.entries(oldVars)) {
if (val.frozen) {
if (!newVars[key] && val.frozen) {
resultVars[key] = val;
} else {
resultVars[key] = newVars[key];
}
}
return resultVars;
Expand Down
30 changes: 30 additions & 0 deletions x-pack/plugins/osquery/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import { PackagePolicy, PackagePolicyInput, PackagePolicyInputStream } from '../../fleet/common';

export const savedQuerySavedObjectType = 'osquery-saved-query';
export const packSavedObjectType = 'osquery-pack';
export type SavedObjectType = 'osquery-saved-query' | 'osquery-pack';
Expand All @@ -25,3 +27,31 @@ export type RequiredKeepUndefined<T> = { [K in keyof T]-?: [T[K]] } extends infe
? { [K in keyof U]: U[K][0] }
: never
: never;

export interface OsqueryManagerPackagePolicyConfigRecordEntry {
type: string;
value: string;
frozen?: boolean;
}

export interface OsqueryManagerPackagePolicyConfigRecord {
id: OsqueryManagerPackagePolicyConfigRecordEntry;
query: OsqueryManagerPackagePolicyConfigRecordEntry;
interval: OsqueryManagerPackagePolicyConfigRecordEntry;
platform?: OsqueryManagerPackagePolicyConfigRecordEntry;
version?: OsqueryManagerPackagePolicyConfigRecordEntry;
}

export interface OsqueryManagerPackagePolicyInputStream
extends Omit<PackagePolicyInputStream, 'config' | 'vars'> {
config?: OsqueryManagerPackagePolicyConfigRecord;
vars?: OsqueryManagerPackagePolicyConfigRecord;
}

export interface OsqueryManagerPackagePolicyInput extends Omit<PackagePolicyInput, 'streams'> {
streams: OsqueryManagerPackagePolicyInputStream[];
}

export interface OsqueryManagerPackagePolicy extends Omit<PackagePolicy, 'inputs'> {
inputs: OsqueryManagerPackagePolicyInput[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import React, { useCallback, useMemo, useState } from 'react';
import styled from 'styled-components';

import { PLUGIN_ID } from '../../../fleet/common';
import { pagePathGetters } from '../../../fleet/public';
import { useActionResults } from './use_action_results';
import { useAllResults } from '../results/use_all_results';
Expand Down Expand Up @@ -130,7 +131,7 @@ const ActionResultsSummaryComponent: React.FC<ActionResultsSummaryProps> = ({
(agentId) => (
<EuiLink
className="eui-textTruncate"
href={getUrlForApp('fleet', {
href={getUrlForApp(PLUGIN_ID, {
path: `#` + pagePathGetters.fleet_agent_details({ agentId }),
})}
target="_blank"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { EuiLink } from '@elastic/eui';
import React, { useCallback, useMemo } from 'react';

import { PLUGIN_ID } from '../../../fleet/common';
import { pagePathGetters } from '../../../fleet/public';
import { useKibana, isModifiedEvent, isLeftClickEvent } from '../common/lib/kibana';
import { useAgentPolicy } from './use_agent_policy';
Expand All @@ -25,7 +26,7 @@ const AgentsPolicyLinkComponent: React.FC<AgentsPolicyLinkProps> = ({ policyId }

const href = useMemo(
() =>
getUrlForApp('fleet', {
getUrlForApp(PLUGIN_ID, {
path: `#` + pagePathGetters.policy_details({ policyId }),
}),
[getUrlForApp, policyId]
Expand All @@ -36,7 +37,7 @@ const AgentsPolicyLinkComponent: React.FC<AgentsPolicyLinkProps> = ({ policyId }
if (!isModifiedEvent(event) && isLeftClickEvent(event)) {
event.preventDefault();

return navigateToApp('fleet', {
return navigateToApp(PLUGIN_ID, {
path: `#` + pagePathGetters.policy_details({ policyId }),
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React, { useCallback, useMemo } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiButtonEmpty, EuiFlexItem } from '@elastic/eui';

import { INTEGRATIONS_PLUGIN_ID } from '../../../fleet/common';
import { pagePathGetters } from '../../../fleet/public';

import { useKibana, isModifiedEvent, isLeftClickEvent } from '../common/lib/kibana';
Expand All @@ -22,12 +23,12 @@ const ManageIntegrationLinkComponent = () => {

const integrationHref = useMemo(() => {
if (osqueryIntegration) {
return getUrlForApp('fleet', {
return getUrlForApp(INTEGRATIONS_PLUGIN_ID, {
path:
'#' +
pagePathGetters.integration_details_policies({
pkgkey: `${osqueryIntegration.name}-${osqueryIntegration.version}`,
}),
})[1],
});
}
}, [getUrlForApp, osqueryIntegration]);
Expand All @@ -37,12 +38,12 @@ const ManageIntegrationLinkComponent = () => {
if (!isModifiedEvent(event) && isLeftClickEvent(event)) {
event.preventDefault();
if (osqueryIntegration) {
return navigateToApp('fleet', {
return navigateToApp(INTEGRATIONS_PLUGIN_ID, {
path:
'#' +
pagePathGetters.integration_details_policies({
pkgkey: `${osqueryIntegration.name}-${osqueryIntegration.version}`,
}),
})[1],
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ import { EuiFlexGroup, EuiFlexItem, EuiCard, EuiIcon } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import React, { useCallback, useMemo } from 'react';

import { PLUGIN_ID } from '../../common';
import { useKibana, isModifiedEvent, isLeftClickEvent } from '../common/lib/kibana';

interface NavigationButtonsProps {
isDisabled?: boolean;
integrationPolicyId?: string;
agentPolicyId?: string;
integrationPolicyId?: string | undefined;
agentPolicyId?: string | undefined;
}

const NavigationButtonsComponent: React.FC<NavigationButtonsProps> = ({
isDisabled,
isDisabled = false,
integrationPolicyId,
agentPolicyId,
}) => {
Expand All @@ -28,7 +29,7 @@ const NavigationButtonsComponent: React.FC<NavigationButtonsProps> = ({

const liveQueryHref = useMemo(
() =>
getUrlForApp('osquery', {
getUrlForApp(PLUGIN_ID, {
path: agentPolicyId
? `/live_queries/new?agentPolicyId=${agentPolicyId}`
: ' `/live_queries/new',
Expand All @@ -40,7 +41,7 @@ const NavigationButtonsComponent: React.FC<NavigationButtonsProps> = ({
(event) => {
if (!isModifiedEvent(event) && isLeftClickEvent(event)) {
event.preventDefault();
navigateToApp('osquery', {
navigateToApp(PLUGIN_ID, {
path: agentPolicyId
? `/live_queries/new?agentPolicyId=${agentPolicyId}`
: ' `/live_queries/new',
Expand All @@ -50,7 +51,7 @@ const NavigationButtonsComponent: React.FC<NavigationButtonsProps> = ({
[agentPolicyId, navigateToApp]
);

const scheduleQueryGroupsHref = getUrlForApp('osquery', {
const scheduleQueryGroupsHref = getUrlForApp(PLUGIN_ID, {
path: integrationPolicyId
? `/scheduled_query_groups/${integrationPolicyId}/edit`
: `/scheduled_query_groups`,
Expand All @@ -60,7 +61,7 @@ const NavigationButtonsComponent: React.FC<NavigationButtonsProps> = ({
(event) => {
if (!isModifiedEvent(event) && isLeftClickEvent(event)) {
event.preventDefault();
navigateToApp('osquery', {
navigateToApp(PLUGIN_ID, {
path: integrationPolicyId
? `/scheduled_query_groups/${integrationPolicyId}/edit`
: `/scheduled_query_groups`,
Expand Down
Loading

0 comments on commit 35cc59b

Please sign in to comment.