Skip to content

Commit

Permalink
added api validation for serverless
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Nov 16, 2023
1 parent b3a49c7 commit 5547f55
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions x-pack/plugins/fleet/server/routes/output/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type { TypeOf } from '@kbn/config-schema';

import Boom from '@hapi/boom';

import type { ValueOf } from '@elastic/eui';

import { outputType } from '../../../common/constants';

import type {
Expand All @@ -23,11 +25,12 @@ import type {
GetOneOutputResponse,
GetOutputsResponse,
Output,
OutputType,
PostLogstashApiKeyResponse,
} from '../../../common/types';
import { outputService } from '../../services/output';
import { defaultFleetErrorHandler, FleetUnauthorizedError } from '../../errors';
import { agentPolicyService } from '../../services';
import { agentPolicyService, appContextService } from '../../services';
import { generateLogstashApiKey, canCreateLogstashApiKey } from '../../services/api_keys';

function ensureNoDuplicateSecrets(output: Partial<Output>) {
Expand Down Expand Up @@ -89,8 +92,9 @@ export const putOutputHandler: RequestHandler<
const soClient = coreContext.savedObjects.client;
const esClient = coreContext.elasticsearch.client.asInternalUser;
const outputUpdate = request.body;
ensureNoDuplicateSecrets(outputUpdate);
try {
validateOutputServerless(outputUpdate.type);
ensureNoDuplicateSecrets(outputUpdate);
await outputService.update(soClient, esClient, request.params.outputId, outputUpdate);
const output = await outputService.get(soClient, request.params.outputId);
if (output.is_default || output.is_default_monitoring) {
Expand Down Expand Up @@ -125,6 +129,7 @@ export const postOutputHandler: RequestHandler<
const esClient = coreContext.elasticsearch.client.asInternalUser;
try {
const { id, ...newOutput } = request.body;
validateOutputServerless(newOutput.type);
ensureNoDuplicateSecrets(newOutput);
const output = await outputService.create(soClient, esClient, newOutput, { id });
if (output.is_default || output.is_default_monitoring) {
Expand All @@ -141,6 +146,13 @@ export const postOutputHandler: RequestHandler<
}
};

function validateOutputServerless(type?: ValueOf<OutputType>): void {
const cloudSetup = appContextService.getCloud();
if (cloudSetup?.isServerlessEnabled && type === outputType.RemoteElasticsearch) {
throw Boom.badRequest('Output type remote_elasticsearch not supported in serverless');
}
}

export const deleteOutputHandler: RequestHandler<
TypeOf<typeof DeleteOutputRequestSchema.params>
> = async (context, request, response) => {
Expand Down

0 comments on commit 5547f55

Please sign in to comment.