Skip to content

Commit

Permalink
Merge branch '8.11' into backport/8.11/pr-170974
Browse files Browse the repository at this point in the history
  • Loading branch information
kibanamachine authored Nov 10, 2023
2 parents 3959e79 + 8c233cd commit edd4e96
Show file tree
Hide file tree
Showing 15 changed files with 219 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { useGetOneAgentPolicyFull, useGetOneAgentPolicy, useStartServices } from '../../../hooks';
import { Loading } from '../../../components';
import { fullAgentPolicyToYaml, agentPolicyRouteService } from '../../../services';
import { API_VERSIONS } from '../../../../../../common/constants';

const FlyoutBody = styled(EuiFlyoutBody)`
.euiFlyoutBody__overflowContent {
Expand Down Expand Up @@ -65,9 +66,9 @@ export const AgentPolicyYamlFlyout = memo<{ policyId: string; onClose: () => voi
</>
);

const downloadLink = core.http.basePath.prepend(
agentPolicyRouteService.getInfoFullDownloadPath(policyId)
);
const downloadLink =
core.http.basePath.prepend(agentPolicyRouteService.getInfoFullDownloadPath(policyId)) +
`?apiVersion=${API_VERSIONS.public.v1}`;

return (
<EuiFlyout onClose={onClose} size="l" maxWidth={640}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EuiSteps, EuiSpacer } from '@elastic/eui';
import { safeDump } from 'js-yaml';

import type { FullAgentPolicy } from '../../../../../../../../../../common/types/models/agent_policy';

import { API_VERSIONS } from '../../../../../../../../../../common/constants';
import {
AgentStandaloneBottomBar,
StandaloneModeWarningCallout,
Expand Down Expand Up @@ -95,7 +95,9 @@ export const InstallElasticAgentStandalonePageStep: React.FC<InstallAgentPagePro
const installManagedCommands = StandaloneInstructions(kibanaVersion);

const downloadLink = core.http.basePath.prepend(
`${agentPolicyRouteService.getInfoFullDownloadPath(agentPolicy?.id)}?standalone=true`
`${agentPolicyRouteService.getInfoFullDownloadPath(
agentPolicy?.id
)}?standalone=true&apiVersion=${API_VERSIONS.public.v1}`
);
const steps = [
ConfigureStandaloneAgentStep({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { getManifestDownloadLink } from './kubernetes_instructions';
describe('getManifestDownloadLink', () => {
it('should return the correct link', () => {
expect(getManifestDownloadLink('https://fleet.host', 'enrollmentToken')).toEqual(
'/api/fleet/kubernetes/download?fleetServer=https%3A%2F%2Ffleet.host&enrolToken=enrollmentToken'
'/api/fleet/kubernetes/download?apiVersion=2023-10-31&fleetServer=https%3A%2F%2Ffleet.host&enrolToken=enrollmentToken'
);
expect(getManifestDownloadLink('https://fleet.host')).toEqual(
'/api/fleet/kubernetes/download?fleetServer=https%3A%2F%2Ffleet.host'
'/api/fleet/kubernetes/download?apiVersion=2023-10-31&fleetServer=https%3A%2F%2Ffleet.host'
);
expect(getManifestDownloadLink(undefined, 'enrollmentToken')).toEqual(
'/api/fleet/kubernetes/download?enrolToken=enrollmentToken'
'/api/fleet/kubernetes/download?apiVersion=2023-10-31&enrolToken=enrollmentToken'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { i18n } from '@kbn/i18n';

import { useStartServices } from '../../hooks';

import { agentPolicyRouteService } from '../../../common';
import { agentPolicyRouteService, API_VERSIONS } from '../../../common';

import { sendGetK8sManifest } from '../../hooks/use_request/k8s';

Expand All @@ -33,6 +33,7 @@ interface Props {

export const getManifestDownloadLink = (fleetServerHost?: string, enrollmentAPIKey?: string) => {
const searchParams = new URLSearchParams({
apiVersion: API_VERSIONS.public.v1,
...(fleetServerHost && { fleetServer: fleetServerHost }),
...(enrollmentAPIKey && { enrolToken: enrollmentAPIKey }),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { safeDump } from 'js-yaml';
import type { EuiContainedStepProps } from '@elastic/eui/src/components/steps/steps';

import type { FullAgentPolicy } from '../../../../common/types/models/agent_policy';

import { API_VERSIONS } from '../../../../common/constants';
import {
fullAgentPolicyToYaml,
agentPolicyRouteService,
Expand Down Expand Up @@ -73,10 +73,12 @@ export const StandaloneSteps: React.FunctionComponent<InstructionProps> = ({
? core.http.basePath.prepend(
`${agentPolicyRouteService.getInfoFullDownloadPath(
selectedPolicy?.id
)}?kubernetes=true&standalone=true`
)}?kubernetes=true&standalone=true&apiVersion=${API_VERSIONS.public.v1}`
)
: core.http.basePath.prepend(
`${agentPolicyRouteService.getInfoFullDownloadPath(selectedPolicy?.id)}?standalone=true`
`${agentPolicyRouteService.getInfoFullDownloadPath(
selectedPolicy?.id
)}?standalone=true&apiVersion=${API_VERSIONS.public.v1}`
);
}

Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/server/routes/agent_policy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const registerRoutes = (router: FleetAuthzRouter) => {
fleetAuthz: {
fleet: { all: true },
},
enableQueryVersion: true,
})
.addVersion(
{
Expand Down Expand Up @@ -206,6 +207,7 @@ export const registerRoutes = (router: FleetAuthzRouter) => {
fleetAuthz: {
fleet: { all: true },
},
enableQueryVersion: true,
})
.addVersion(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,55 +688,108 @@ describe('tagKibanaAssets', () => {
);
});

it('should respect SecuritySolution tags', async () => {
savedObjectTagClient.get.mockRejectedValue(new Error('not found'));
savedObjectTagClient.create.mockImplementation(({ name }: { name: string }) =>
Promise.resolve({ id: name.toLowerCase(), name })
);
const kibanaAssets = {
dashboard: [
{ id: 'dashboard1', type: 'dashboard' },
{ id: 'dashboard2', type: 'dashboard' },
{ id: 'search_id1', type: 'search' },
{ id: 'search_id2', type: 'search' },
],
} as any;
const assetTags = [
{
text: 'Security Solution',
asset_types: ['dashboard'],
},
];
await tagKibanaAssets({
savedObjectTagAssignmentService,
savedObjectTagClient,
kibanaAssets,
pkgTitle: 'TestPackage',
pkgName: 'test-pkg',
spaceId: 'default',
importedAssets: [],
assetTags,
describe('Security Solution tag', () => {
it('creates tag in default space', async () => {
savedObjectTagClient.get.mockRejectedValue(new Error('not found'));
savedObjectTagClient.create.mockImplementation(({ name }: { name: string }) =>
Promise.resolve({ id: name.toLowerCase(), name })
);
const kibanaAssets = {
dashboard: [
{ id: 'dashboard1', type: 'dashboard' },
{ id: 'dashboard2', type: 'dashboard' },
{ id: 'search_id1', type: 'search' },
{ id: 'search_id2', type: 'search' },
],
} as any;
const assetTags = [
{
text: 'Security Solution',
asset_types: ['dashboard'],
},
];
await tagKibanaAssets({
savedObjectTagAssignmentService,
savedObjectTagClient,
kibanaAssets,
pkgTitle: 'TestPackage',
pkgName: 'test-pkg',
spaceId: 'default',
importedAssets: [],
assetTags,
});
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
managedTagPayloadArg1,
managedTagPayloadArg2
);
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
{
color: '#4DD2CA',
description: '',
name: 'TestPackage',
},
{ id: 'fleet-pkg-test-pkg-default', overwrite: true, refresh: false }
);
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
{
color: expect.any(String),
description: 'Tag defined in package-spec',
name: 'Security Solution',
},
{ id: 'security-solution-default', overwrite: true, refresh: false }
);
});

it('creates tag in non-default space', async () => {
savedObjectTagClient.get.mockRejectedValue(new Error('not found'));
savedObjectTagClient.create.mockImplementation(({ name }: { name: string }) =>
Promise.resolve({ id: name.toLowerCase(), name })
);
const kibanaAssets = {
dashboard: [
{ id: 'dashboard1', type: 'dashboard' },
{ id: 'dashboard2', type: 'dashboard' },
{ id: 'search_id1', type: 'search' },
{ id: 'search_id2', type: 'search' },
],
} as any;
const assetTags = [
{
text: 'Security Solution',
asset_types: ['dashboard'],
},
];
await tagKibanaAssets({
savedObjectTagAssignmentService,
savedObjectTagClient,
kibanaAssets,
pkgTitle: 'TestPackage',
pkgName: 'test-pkg',
spaceId: 'my-secondary-space',
importedAssets: [],
assetTags,
});
expect(savedObjectTagClient.create).toHaveBeenCalledWith(managedTagPayloadArg1, {
...managedTagPayloadArg2,
id: 'fleet-managed-my-secondary-space',
});
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
{
color: expect.any(String),
description: '',
name: 'TestPackage',
},
{ id: 'fleet-pkg-test-pkg-my-secondary-space', overwrite: true, refresh: false }
);
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
{
color: expect.anything(),
description: 'Tag defined in package-spec',
name: 'Security Solution',
},
{ id: 'security-solution-my-secondary-space', overwrite: true, refresh: false }
);
});
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
managedTagPayloadArg1,
managedTagPayloadArg2
);
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
{
color: '#4DD2CA',
description: '',
name: 'TestPackage',
},
{ id: 'fleet-pkg-test-pkg-default', overwrite: true, refresh: false }
);
expect(savedObjectTagClient.create).toHaveBeenCalledWith(
{
color: expect.any(String),
description: 'Tag defined in package-spec',
name: 'Security Solution',
},
{ id: 'security-solution-default', overwrite: true, refresh: false }
);
});

it('should only call savedObjectTagClient.create for basic tags if there are no assetTags to assign', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const PACKAGE_TAG_COLOR = '#4DD2CA';
const MANAGED_TAG_NAME = 'Managed';
const LEGACY_MANAGED_TAG_ID = 'managed';
const SECURITY_SOLUTION_TAG_NAME = 'Security Solution';
const SECURITY_SOLUTION_TAG_ID = 'security-solution-default';
const SECURITY_SOLUTION_TAG_ID_BASE = 'security-solution';

// the tag service only accepts 6-digits hex colors
const TAG_COLORS = [
Expand Down Expand Up @@ -65,8 +65,10 @@ const getLegacyPackageTagId = (pkgName: string) => pkgName;
In that case return id `security-solution-default`
*/
export const getPackageSpecTagId = (spaceId: string, pkgName: string, tagName: string) => {
if (tagName.toLowerCase() === SECURITY_SOLUTION_TAG_NAME.toLowerCase())
return SECURITY_SOLUTION_TAG_ID;
if (tagName.toLowerCase() === SECURITY_SOLUTION_TAG_NAME.toLowerCase()) {
return `${SECURITY_SOLUTION_TAG_ID_BASE}-${spaceId}`;
}

// UUID v5 needs a namespace (uuid.DNS) to generate a predictable uuid
const uniqueId = uuidv5(`${tagName.toLowerCase()}`, uuidv5.DNS);
return `fleet-shared-tag-${pkgName}-${uniqueId}-${spaceId}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

import * as t from 'io-ts';
import { toNumberRt } from '@kbn/io-ts-utils';
import { Direction } from '../../search_strategy';

export const getLiveQueryResultsRequestQuerySchema = t.type({
kuery: t.union([t.string, t.undefined]),
page: t.union([toNumberRt, t.undefined]),
pageSize: t.union([toNumberRt, t.undefined]),
sort: t.union([t.string, t.undefined]),
sortOrder: t.union([t.union([t.literal('asc'), t.literal('desc')]), t.undefined]),
sortOrder: t.union([t.literal(Direction.asc), t.literal(Direction.desc), t.undefined]),
});

export type GetLiveQueryResultsRequestQuerySchema = t.OutputOf<
Expand Down
49 changes: 49 additions & 0 deletions x-pack/plugins/osquery/cypress/e2e/api/live_query_results.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 { request } from '../../tasks/common';
import { loadLiveQuery } from '../../tasks/api_fixtures';
import { API_VERSIONS } from '../../../common/constants';
import { ServerlessRoleName } from '../../support/roles';

describe('Live query', { tags: ['@ess', '@serverless'] }, () => {
let liveQueryId: string;
let queriesQueryActionId: string;

beforeEach(() => {
cy.login(ServerlessRoleName.SOC_MANAGER);
loadLiveQuery().then((liveQuery) => {
liveQueryId = liveQuery.action_id;
queriesQueryActionId = liveQuery.queries?.[0].action_id;
});
});

context('GET getLiveQueryDetailsRoute', () => {
it('validates we get successful response', () => {
request({
url: `/api/osquery/live_queries/${liveQueryId}`,
headers: {
'Elastic-Api-Version': API_VERSIONS.public.v1,
},
}).then((response) => {
expect(response.status).to.eq(200);
});
});
});
context('GET getLiveQueryResultsRoute', () => {
it('validates we get successful response', () => {
request({
url: `/api/osquery/live_queries/${liveQueryId}/results/${queriesQueryActionId}`,
headers: {
'Elastic-Api-Version': API_VERSIONS.public.v1,
},
}).then((response) => {
expect(response.status).to.eq(200);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import { PLUGIN_ID } from '../../../common';
import type {
ActionDetailsRequestOptions,
ActionDetailsStrategyResponse,
ResultsRequestOptions,
ResultsStrategyResponse,
} from '../../../common/search_strategy';
import { OsqueryQueries } from '../../../common/search_strategy';
import { Direction, OsqueryQueries } from '../../../common/search_strategy';
import { generateTablePaginationOptions } from '../../../common/utils/build_query';
import { getActionResponses } from './utils';
import {
Expand Down Expand Up @@ -79,7 +81,7 @@ export const getLiveQueryResultsRoute = (router: IRouter<DataRequestHandlerConte
);

const res = await lastValueFrom(
search.search<{}>(
search.search<ResultsRequestOptions, ResultsStrategyResponse>(
{
actionId: request.params.actionId,
factoryQueryType: OsqueryQueries.results,
Expand All @@ -88,10 +90,12 @@ export const getLiveQueryResultsRoute = (router: IRouter<DataRequestHandlerConte
request.query.page ?? 0,
request.query.pageSize ?? 100
),
sort: {
direction: request.query.sortOrder ?? 'desc',
field: request.query.sort ?? '@timestamp',
},
sort: [
{
direction: request.query.sortOrder ?? Direction.desc,
field: request.query.sort ?? '@timestamp',
},
],
},
{ abortSignal, strategy: 'osquerySearchStrategy' }
)
Expand Down
Loading

0 comments on commit edd4e96

Please sign in to comment.