Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fleet] use @kbn/config-schema in Fleet API (Part 1) #192447

Merged
merged 9 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
316 changes: 307 additions & 9 deletions x-pack/plugins/fleet/server/routes/package_policy/index.ts

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions x-pack/plugins/fleet/server/routes/schema/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 { schema } from '@kbn/config-schema';

export const genericErrorResponse = () =>
schema.object(
{
statusCode: schema.number(),
error: schema.string(),
message: schema.string(),
},
{
meta: { description: 'Generic Error' },
}
);

export const notFoundResponse = () =>
schema.object({
message: schema.string(),
});

export const internalErrorResponse = () =>
schema.object(
{
message: schema.string(),
},
{ meta: { description: 'Internal Server Error' } }
);
165 changes: 160 additions & 5 deletions x-pack/plugins/fleet/server/routes/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { schema } from '@kbn/config-schema';

import { parseExperimentalConfigValue } from '../../../common/experimental_features';
import { API_VERSIONS } from '../../../common/constants';
Expand All @@ -18,6 +19,8 @@ import {
} from '../../types';
import type { FleetConfigType } from '../../config';

import { genericErrorResponse, notFoundResponse } from '../schema/errors';

import { getEnrollmentSettingsHandler } from './enrollment_settings_handler';

import {
Expand All @@ -27,6 +30,36 @@ import {
putSpaceSettingsHandler,
} from './settings_handler';

const spaceSettingsResponse = () =>
schema.object({
item: schema.object({
managed_by: schema.maybe(schema.string()),
allowed_namespace_prefixes: schema.arrayOf(schema.string()),
}),
});

const settingsResponse = () =>
schema.object({
item: schema.object({
has_seen_add_data_notice: schema.maybe(schema.boolean()),
fleet_server_hosts: schema.maybe(schema.arrayOf(schema.string())),
prerelease_integrations_enabled: schema.boolean(),
id: schema.string(),
version: schema.maybe(schema.string()),
preconfigured_fields: schema.maybe(schema.arrayOf(schema.literal('fleet_server_hosts'))),
secret_storage_requirements_met: schema.maybe(schema.boolean()),
output_secret_storage_requirements_met: schema.maybe(schema.boolean()),
use_space_awareness_migration_status: schema.maybe(
schema.oneOf([
schema.literal('pending'),
schema.literal('success'),
schema.literal('error'),
])
),
use_space_awareness_migration_started_at: schema.maybe(schema.string()),
}),
});

export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType) => {
const experimentalFeatures = parseExperimentalConfigValue(config.enableExperimental);
if (experimentalFeatures.useSpaceAwareness) {
Expand All @@ -45,7 +78,14 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType
.addVersion(
{
version: API_VERSIONS.public.v1,
validate: { request: GetSpaceSettingsRequestSchema },
validate: {
request: GetSpaceSettingsRequestSchema,
response: {
200: {
body: spaceSettingsResponse,
},
},
},
},
getSpaceSettingsHandler
);
Expand All @@ -61,7 +101,14 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType
.addVersion(
{
version: API_VERSIONS.public.v1,
validate: { request: PutSpaceSettingsRequestSchema },
validate: {
request: PutSpaceSettingsRequestSchema,
response: {
200: {
body: spaceSettingsResponse,
},
},
},
},
putSpaceSettingsHandler
);
Expand All @@ -74,11 +121,27 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType
fleet: { readSettings: true },
},
description: `Get settings`,
options: {
tags: ['oas-tag:Fleet internals'],
},
})
.addVersion(
{
version: API_VERSIONS.public.v1,
validate: { request: GetSettingsRequestSchema },
validate: {
request: GetSettingsRequestSchema,
response: {
200: {
body: settingsResponse,
},
400: {
body: genericErrorResponse,
},
404: {
body: notFoundResponse,
},
},
},
},
getSettingsHandler
);
Expand All @@ -89,11 +152,27 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType
fleet: { allSettings: true },
},
description: `Update settings`,
options: {
tags: ['oas-tag:Fleet internals'],
},
})
.addVersion(
{
version: API_VERSIONS.public.v1,
validate: { request: PutSettingsRequestSchema },
validate: {
request: PutSettingsRequestSchema,
response: {
200: {
body: settingsResponse,
},
400: {
body: genericErrorResponse,
},
404: {
body: notFoundResponse,
},
},
},
},
putSettingsHandler
);
Expand All @@ -104,11 +183,87 @@ export const registerRoutes = (router: FleetAuthzRouter, config: FleetConfigType
return authz.fleet.addAgents || authz.fleet.addFleetServers;
},
description: `Get enrollment settings`,
options: {
tags: ['oas-tag:Fleet internals'],
},
})
.addVersion(
{
version: API_VERSIONS.public.v1,
validate: { request: GetEnrollmentSettingsRequestSchema },
validate: {
request: GetEnrollmentSettingsRequestSchema,
response: {
200: {
body: () =>
schema.object({
fleet_server: schema.object({
policies: schema.arrayOf(
schema.object({
id: schema.string(),
name: schema.string(),
is_managed: schema.boolean(),
is_default_fleet_server: schema.maybe(schema.boolean()),
has_fleet_server: schema.maybe(schema.boolean()),
fleet_server_host_id: schema.nullable(schema.maybe(schema.string())),
download_source_id: schema.nullable(schema.maybe(schema.string())),
space_ids: schema.maybe(schema.arrayOf(schema.string())),
})
),
has_active: schema.boolean(),
host: schema.maybe(
schema.object({
id: schema.string(),
name: schema.string(),
host_urls: schema.arrayOf(schema.string()),
is_default: schema.boolean(),
is_preconfigured: schema.boolean(),
is_internal: schema.maybe(schema.boolean()),
proxy_id: schema.nullable(schema.maybe(schema.string())),
})
),
host_proxy: schema.maybe(
schema.object({
id: schema.string(),
proxy_headers: schema.maybe(
schema.recordOf(
schema.string(),
schema.oneOf([schema.string(), schema.number(), schema.boolean()])
)
),
name: schema.string(),
url: schema.string(),
certificate_authorities: schema.nullable(schema.maybe(schema.string())),
certificate: schema.nullable(schema.maybe(schema.string())),
certificate_key: schema.nullable(schema.maybe(schema.string())),
is_preconfigured: schema.boolean(),
})
),
}),
download_source: schema.maybe(
schema.object({
id: schema.string(),
name: schema.string(),
host: schema.string(),
is_default: schema.boolean(),
proxy_id: schema.nullable(
schema.maybe(
schema.string({
meta: {
description:
'The ID of the proxy to use for this download source. See the proxies API for more information.',
},
})
)
),
})
),
}),
},
400: {
body: genericErrorResponse,
},
},
},
},
getEnrollmentSettingsHandler
);
Expand Down
Loading