Skip to content

Commit

Permalink
[Security Solution][Endpoint] changes to Endpoint metadata API in sup…
Browse files Browse the repository at this point in the history
…port of space awareness (#193490)

## Summary

### Fleet Plugin

- Added some mocks and updates others


### Security Solution

The following refactoring changes were done in preparation for
forthcoming work for supporting kibana spaces:

- A new saved objects client factory service was introduced for use in
`EndpointAppContextServices`
- Deleted older saved objects client utilities
- Simplified the list of options passed to
`EndpointAppContextService#start()` method
- Updated `EndpiontFleetServicesFactory` with:
    - simpler list of constructor arguments
    - changed members of object return by `asInternalUser()`
- Refactored the `EndpintMetadataService` to:
    - take in simplified constructor arguments
- Simplified most methods of the class with removal all ES Client, SO
Client or Fleet services arguments from class method calling signatures.
These are not provided to the service class upon initialization and can
be accessed internally by the methods
- Updates to Mocks and tests to reflect the above changes
  • Loading branch information
paul-tavares authored Sep 27, 2024
1 parent d9ca7c6 commit 0b1e9f4
Show file tree
Hide file tree
Showing 44 changed files with 726 additions and 786 deletions.
35 changes: 3 additions & 32 deletions x-pack/plugins/cloud_defend/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,16 @@ import {
httpServerMock,
savedObjectsClientMock,
} from '@kbn/core/server/mocks';
import {
createPackagePolicyServiceMock,
createArtifactsClientMock,
createMockPackageService,
createMockAgentService,
createMockAgentPolicyService,
} from '@kbn/fleet-plugin/server/mocks';

import { createPackagePolicyMock } from '@kbn/fleet-plugin/common/mocks';
import { dataPluginMock } from '@kbn/data-plugin/server/mocks';
import { CloudDefendPlugin } from './plugin';
import { CloudDefendPluginStartDeps } from './types';
import { createFleetAuthzMock } from '@kbn/fleet-plugin/common/mocks';
import { PackagePolicy, UpdatePackagePolicy } from '@kbn/fleet-plugin/common';
import {
ExternalCallback,
FleetStartContract,
PostPackagePolicyPostCreateCallback,
} from '@kbn/fleet-plugin/server';
import { PostPackagePolicyPostCreateCallback } from '@kbn/fleet-plugin/server';
import { INTEGRATION_PACKAGE_NAME } from '../common/constants';
import Chance from 'chance';
import type { AwaitedProperties } from '@kbn/utility-types';
import type { DeeplyMockedKeys } from '@kbn/utility-types-jest';
import {
ElasticsearchClient,
RequestHandlerContext,
Expand All @@ -42,33 +29,17 @@ import {
import { securityMock } from '@kbn/security-plugin/server/mocks';
import { licensingMock } from '@kbn/licensing-plugin/server/mocks';
import * as onPackagePolicyPostCreateCallback from './lib/fleet_util';
import { createFleetStartContractMock } from '@kbn/fleet-plugin/server/mocks';

const chance = new Chance();

const mockRouteContext = {
core: coreMock.createRequestHandlerContext(),
} as unknown as AwaitedProperties<RequestHandlerContext>;

const createMockFleetStartContract = (): DeeplyMockedKeys<FleetStartContract> => {
return {
authz: {
fromRequest: jest.fn(async (_) => createFleetAuthzMock()),
},
fleetSetupCompleted: jest.fn().mockResolvedValue(undefined),
// @ts-expect-error 2322
agentService: createMockAgentService(),
// @ts-expect-error 2322
packageService: createMockPackageService(),
agentPolicyService: createMockAgentPolicyService(),
registerExternalCallback: jest.fn((..._: ExternalCallback) => {}),
packagePolicyService: createPackagePolicyServiceMock(),
createArtifactsClient: jest.fn().mockReturnValue(createArtifactsClientMock()),
};
};

describe('Cloud Defend Plugin', () => {
describe('start()', () => {
const fleetMock = createMockFleetStartContract();
const fleetMock = createFleetStartContractMock();
const mockPlugins: CloudDefendPluginStartDeps = {
fleet: fleetMock,
data: dataPluginMock.createStartContract(),
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/cloud_defend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"@kbn/es-types",
"@kbn/data-views-plugin",
"@kbn/utility-types",
"@kbn/utility-types-jest",
"@kbn/kubernetes-security-plugin",
"@kbn/core-http-router-server-mocks",
"@kbn/core-elasticsearch-server",
Expand Down
31 changes: 2 additions & 29 deletions x-pack/plugins/cloud_security_posture/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,26 @@ import {
httpServerMock,
savedObjectsClientMock,
} from '@kbn/core/server/mocks';
import {
createPackagePolicyServiceMock,
createArtifactsClientMock,
createMockPackageService,
createMockAgentService,
createMockAgentPolicyService,
} from '@kbn/fleet-plugin/server/mocks';
import { createFleetStartContractMock } from '@kbn/fleet-plugin/server/mocks';
import { taskManagerMock } from '@kbn/task-manager-plugin/server/mocks';

import { createPackagePolicyMock, deletePackagePolicyMock } from '@kbn/fleet-plugin/common/mocks';
import { dataPluginMock } from '@kbn/data-plugin/server/mocks';
import { CspPlugin } from './plugin';
import { CspServerPluginStartDeps } from './types';
import { createFleetAuthzMock } from '@kbn/fleet-plugin/common/mocks';
import {
Installation,
ListResult,
PackagePolicy,
UpdatePackagePolicy,
} from '@kbn/fleet-plugin/common';
import {
FleetStartContract,
PostPackagePolicyPostDeleteCallback,
PostPackagePolicyPostCreateCallback,
ExternalCallback,
} from '@kbn/fleet-plugin/server';
import { CLOUD_SECURITY_POSTURE_PACKAGE_NAME } from '../common/constants';
import Chance from 'chance';
import type { AwaitedProperties } from '@kbn/utility-types';
import type { DeeplyMockedKeys } from '@kbn/utility-types-jest';
import { createIndexPatternsStartMock } from '@kbn/data-views-plugin/server/mocks';
import {
ElasticsearchClient,
Expand All @@ -56,26 +46,9 @@ const mockRouteContext = {
core: coreMock.createRequestHandlerContext(),
} as unknown as AwaitedProperties<RequestHandlerContext>;

const createMockFleetStartContract = (): DeeplyMockedKeys<FleetStartContract> => {
return {
authz: {
fromRequest: jest.fn(async (_) => createFleetAuthzMock()),
},
fleetSetupCompleted: jest.fn().mockResolvedValue(undefined),
// @ts-expect-error 2322
agentService: createMockAgentService(),
// @ts-expect-error 2322
packageService: createMockPackageService(),
agentPolicyService: createMockAgentPolicyService(),
registerExternalCallback: jest.fn((..._: ExternalCallback) => {}),
packagePolicyService: createPackagePolicyServiceMock(),
createArtifactsClient: jest.fn().mockReturnValue(createArtifactsClientMock()),
};
};

describe('Cloud Security Posture Plugin', () => {
describe('start()', () => {
const fleetMock = createMockFleetStartContract();
const fleetMock = createFleetStartContractMock();
const mockPlugins: CspServerPluginStartDeps = {
fleet: fleetMock,
data: dataPluginMock.createStartContract(),
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/cloud_security_posture/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@kbn/monaco",
"@kbn/utility-types",
"@kbn/core-logging-server-mocks",
"@kbn/utility-types-jest",
"@kbn/securitysolution-es-utils",
"@kbn/core-elasticsearch-client-server-mocks",
"@kbn/core-elasticsearch-server",
Expand Down
40 changes: 37 additions & 3 deletions x-pack/plugins/fleet/server/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ import { cloudMock } from '@kbn/cloud-plugin/public/mocks';
import { SPACES_EXTENSION_ID } from '@kbn/core-saved-objects-server';
import type { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server';

import type { DeeplyMockedKeys } from '@kbn/utility-types-jest';

import { createFleetActionsClientMock } from '../services/actions/mocks';

import { createFleetFilesClientFactoryMock } from '../services/files/mocks';

import { createArtifactsClientMock } from '../services/artifacts/mocks';

import type { PackagePolicyClient } from '../services/package_policy_service';
import type { AgentPolicyServiceInterface } from '../services';
import type { FleetAppContext } from '../plugin';
import type { FleetAppContext, FleetStartContract } from '../plugin';
import { createMockTelemetryEventsSender } from '../telemetry/__mocks__';
import type { FleetConfigType } from '../../common/types';
import type { ExperimentalFeatures } from '../../common/experimental_features';
Expand All @@ -35,6 +43,8 @@ import { packageServiceMock } from '../services/epm/package_service.mock';
import type { UninstallTokenServiceInterface } from '../services/security/uninstall_token_service';
import type { MessageSigningServiceInterface } from '../services/security';

import { getPackageSpecTagId } from '../services/epm/kibana/assets/tag_assets';

import { PackagePolicyMocks } from './package_policy.mocks';

// Export all mocks from artifacts
Expand Down Expand Up @@ -238,7 +248,7 @@ export const createMockAgentClient = () => agentServiceMock.createClient();
*/
export const createMockPackageService = () => packageServiceMock.create();

export function createMessageSigningServiceMock(): MessageSigningServiceInterface {
export function createMessageSigningServiceMock(): jest.Mocked<MessageSigningServiceInterface> {
return {
isEncryptionAvailable: true,
generateKeyPair: jest.fn(),
Expand All @@ -253,7 +263,7 @@ export function createMessageSigningServiceMock(): MessageSigningServiceInterfac
};
}

export function createUninstallTokenServiceMock(): UninstallTokenServiceInterface {
export function createUninstallTokenServiceMock(): DeeplyMockedKeys<UninstallTokenServiceInterface> {
return {
getToken: jest.fn(),
getTokenMetadata: jest.fn(),
Expand All @@ -269,3 +279,27 @@ export function createUninstallTokenServiceMock(): UninstallTokenServiceInterfac
scoped: jest.fn().mockImplementation(() => createUninstallTokenServiceMock()),
};
}

export const createFleetStartContractMock = (): DeeplyMockedKeys<FleetStartContract> => {
const fleetAuthzMock = createFleetAuthzMock();
const fleetArtifactsClient = createArtifactsClientMock();
const fleetActionsClient = createFleetActionsClientMock();

const startContract: DeeplyMockedKeys<FleetStartContract> = {
fleetSetupCompleted: jest.fn(async () => {}),
authz: { fromRequest: jest.fn(async (_) => fleetAuthzMock) },
packageService: createMockPackageService(),
agentService: createMockAgentService(),
packagePolicyService: createPackagePolicyServiceMock(),
agentPolicyService: createMockAgentPolicyService(),
registerExternalCallback: jest.fn(),
createArtifactsClient: jest.fn((_) => fleetArtifactsClient),
createFilesClient: createFleetFilesClientFactoryMock(),
messageSigningService: createMessageSigningServiceMock(),
uninstallTokenService: createUninstallTokenServiceMock(),
createFleetActionsClient: jest.fn((_) => fleetActionsClient),
getPackageSpecTagId: jest.fn(getPackageSpecTagId),
};

return startContract;
};
10 changes: 8 additions & 2 deletions x-pack/plugins/fleet/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import {
AGENT_POLICY_SAVED_OBJECT_TYPE,
LEGACY_AGENT_POLICY_SAVED_OBJECT_TYPE,
} from '../common/constants';

import { getFilesClientFactory } from './services/files/get_files_client_factory';

import type { MessageSigningServiceInterface } from './services/security';
Expand All @@ -98,7 +99,12 @@ import { registerEncryptedSavedObjects, registerSavedObjects } from './saved_obj
import { registerRoutes } from './routes';

import type { ExternalCallback, FleetRequestHandlerContext } from './types';
import type { AgentPolicyServiceInterface, AgentService, PackageService } from './services';
import type {
AgentPolicyServiceInterface,
AgentService,
ArtifactsClientInterface,
PackageService,
} from './services';
import {
agentPolicyService,
AgentServiceImpl,
Expand Down Expand Up @@ -236,7 +242,7 @@ export interface FleetStartContract {
* Create a Fleet Artifact Client instance
* @param packageName
*/
createArtifactsClient: (packageName: string) => FleetArtifactsClient;
createArtifactsClient: (packageName: string) => ArtifactsClientInterface;

/**
* Create a Fleet Files client instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import type { DeeplyMockedKeys } from '@kbn/utility-types-jest';

import type { AgentClient, AgentService } from './agent_service';

const createClientMock = (): jest.Mocked<AgentClient> => ({
Expand All @@ -15,7 +17,7 @@ const createClientMock = (): jest.Mocked<AgentClient> => ({
getLatestAgentAvailableVersion: jest.fn(),
});

const createServiceMock = (): jest.Mocked<AgentService> => ({
const createServiceMock = (): DeeplyMockedKeys<AgentService> => ({
asInternalUser: createClientMock(),
asScoped: jest.fn().mockReturnValue(createClientMock()),
});
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/fleet/server/services/artifacts/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { errors } from '@elastic/elasticsearch';
import { elasticsearchServiceMock } from '@kbn/core/server/mocks';
import type { SearchHit, ESSearchResponse } from '@kbn/es-types';

import type { DeeplyMockedKeys } from '@kbn/utility-types-jest';

import type {
Artifact,
ArtifactElasticsearchProperties,
Expand All @@ -20,7 +22,7 @@ import type {
} from './types';
import { newArtifactToElasticsearchProperties } from './mappings';

export const createArtifactsClientMock = (): jest.Mocked<ArtifactsClientInterface> => {
export const createArtifactsClientMock = (): DeeplyMockedKeys<ArtifactsClientInterface> => {
return {
getArtifact: jest.fn().mockResolvedValue(generateArtifactMock()),
createArtifact: jest.fn().mockResolvedValue(generateArtifactMock()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* 2.0.
*/

import type { DeeplyMockedKeys } from '@kbn/utility-types-jest';

import type { PackageClient, PackageService } from './package_service';

const createClientMock = (): jest.Mocked<PackageClient> => ({
Expand All @@ -21,8 +23,8 @@ const createClientMock = (): jest.Mocked<PackageClient> => ({
reinstallEsAssets: jest.fn(),
});

const createServiceMock = (): PackageService => ({
asScoped: jest.fn(createClientMock),
const createServiceMock = (): DeeplyMockedKeys<PackageService> => ({
asScoped: jest.fn((_) => createClientMock()),
asInternalUser: createClientMock(),
});

Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/fleet/server/services/files/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import { Readable } from 'stream';

import type { estypes } from '@elastic/elasticsearch';

import type { DeeplyMockedKeys } from '@kbn/utility-types-jest';

import type {
FilesClientFactory,
FleetFile,
FleetFromHostFileClientInterface,
FleetToHostFileClientInterface,
Expand Down Expand Up @@ -153,3 +156,10 @@ export const createHapiReadableStreamMock = (): HapiReadableStream => {

return readable;
};

export const createFleetFilesClientFactoryMock = (): DeeplyMockedKeys<FilesClientFactory> => {
return {
toHost: jest.fn((_) => createFleetToHostFilesClientMock()),
fromHost: jest.fn((_) => createFleetFromHostFilesClientMock()),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { ElasticsearchClient } from '@kbn/core-elasticsearch-server';
import { FleetStartContract } from '@kbn/fleet-plugin/server';
import { FleetArtifactsClient } from '@kbn/fleet-plugin/server/services';
import { ArtifactsClientInterface } from '@kbn/fleet-plugin/server/services';
import { TaskManagerSetupContract } from '@kbn/task-manager-plugin/server';
import { CoreStart, Logger } from '@kbn/core/server';
import { getApmArtifactClient } from '../fleet/source_maps';
Expand Down Expand Up @@ -141,7 +141,7 @@ async function getArtifactsForPage({
kuery,
}: {
page: number;
apmArtifactClient: FleetArtifactsClient;
apmArtifactClient: ArtifactsClientInterface;
kuery: string;
}) {
return await apmArtifactClient.listArtifacts({
Expand All @@ -163,7 +163,7 @@ async function paginateArtifacts({
}: {
taskState?: TaskState;
page: number;
apmArtifactClient: FleetArtifactsClient;
apmArtifactClient: ArtifactsClientInterface;
kuery: string;
logger: Logger;
internalESClient: ElasticsearchClient;
Expand Down
Loading

0 comments on commit 0b1e9f4

Please sign in to comment.