Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Mar 18, 2022
1 parent acd3ce3 commit 156f7f9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/server/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export class GenerateServiceTokenError extends IngestManagerError {}
export class FleetUnauthorizedError extends IngestManagerError {}

export class OutputUnauthorizedError extends IngestManagerError {}
export class OutputInvalidError extends IngestManagerError {}
export class OutputLicenceError extends IngestManagerError {}

export class ArtifactsClientError extends IngestManagerError {}
export class ArtifactsClientAccessDeniedError extends IngestManagerError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import { savedObjectsClientMock } from 'src/core/server/mocks';

import { appContextService } from '..';
import { outputService } from '../output';

import { validateOutputForPolicy } from '.';

jest.mock('../app_context');
jest.mock('../output');

const mockedAppContextService = appContextService as jest.Mocked<typeof appContextService>;
const mockedOutputService = outputService as jest.Mocked<typeof outputService>;

function mockHasLicence(res: boolean) {
mockedAppContextService.getSecurityLicense.mockReturnValue({
Expand Down Expand Up @@ -186,5 +189,57 @@ describe('validateOutputForPolicy', () => {
{ data_output_id: 'test1', monitoring_output_id: 'test1' }
);
});

it('should not allow APM for a logstash output', async () => {
mockHasLicence(true);
mockedOutputService.get.mockResolvedValue({
type: 'logstash',
} as any);
await expect(
validateOutputForPolicy(
savedObjectsClientMock.create(),
{
data_output_id: 'test1',
monitoring_output_id: 'test1',
},
{ data_output_id: 'newdataoutput', monitoring_output_id: 'test1' },
true // hasAPM
)
).rejects.toThrow(/Logstash output is not usable with policy using the APM integration./);
});

it('should allow APM for an elasticsearch output', async () => {
mockHasLicence(true);
mockedOutputService.get.mockResolvedValue({
type: 'elasticsearch',
} as any);

await validateOutputForPolicy(
savedObjectsClientMock.create(),
{
data_output_id: 'test1',
monitoring_output_id: 'test1',
},
{ data_output_id: 'newdataoutput', monitoring_output_id: 'test1' },
true // hasAPM
);
});

it('should allow logstash output for a policy not using APM', async () => {
mockHasLicence(true);
mockedOutputService.get.mockResolvedValue({
type: 'logstash',
} as any);

await validateOutputForPolicy(
savedObjectsClientMock.create(),
{
data_output_id: 'test1',
monitoring_output_id: 'test1',
},
{ data_output_id: 'newdataoutput', monitoring_output_id: 'test1' },
false // do not have APM
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { AgentPolicySOAttributes } from '../../types';
import { LICENCE_FOR_PER_POLICY_OUTPUT, outputType } from '../../../common';
import { appContextService } from '..';
import { outputService } from '../output';
import { OutputInvalidError, OutputLicenceError } from '../../errors';

/**
* Get the data output for a given agent policy
Expand Down Expand Up @@ -52,12 +53,13 @@ export async function validateOutputForPolicy(

const data = { ...existingData, ...newData };

// TODO test
if (isPolicyUsingAPM) {
const dataOutput = await getDataOutputForAgentPolicy(soClient, data);

if (dataOutput.type === outputType.Logstash) {
throw new Error('Logstash output is not usable with policy using the APM integration.');
throw new OutputInvalidError(
'Logstash output is not usable with policy using the APM integration.'
);
}
}

Expand All @@ -75,7 +77,7 @@ export async function validateOutputForPolicy(
.hasAtLeast(LICENCE_FOR_PER_POLICY_OUTPUT);

if (!hasLicence) {
throw new Error(
throw new OutputLicenceError(
`Invalid licence to set per policy output, you need ${LICENCE_FOR_PER_POLICY_OUTPUT} licence`
);
}
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/services/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
AGENT_POLICY_SAVED_OBJECT_TYPE,
} from '../constants';
import { decodeCloudId, normalizeHostsForAgents, SO_SEARCH_LIMIT, outputType } from '../../common';
import { OutputUnauthorizedError } from '../errors';
import { OutputUnauthorizedError, OutputInvalidError } from '../errors';

import { agentPolicyService } from './agent_policy';
import { appContextService } from './app_context';
Expand Down Expand Up @@ -78,7 +78,7 @@ async function validateLogstashOutputNotUsedInAPMPolicy(
});
for (const agentPolicy of agentPolicySO.items) {
if (agentPolicyService.hasAPMIntegration(agentPolicy)) {
throw new Error('Logstash output cannot be used with APM integration.');
throw new OutputInvalidError('Logstash output cannot be used with APM integration.');
}
}
}
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ class PackagePolicyService implements PackagePolicyServiceInterface {
): Promise<PackagePolicy> {
const agentPolicy = await agentPolicyService.get(soClient, packagePolicy.policy_id, true);

// TODO throw if no agent policy
if (agentPolicy && packagePolicy.package?.name === FLEET_APM_PACKAGE) {
const dataOutput = await getDataOutputForAgentPolicy(soClient, agentPolicy);
if (dataOutput.type === outputType.Logstash) {
Expand Down

0 comments on commit 156f7f9

Please sign in to comment.