diff --git a/x-pack/plugins/fleet/common/types/models/agent.ts b/x-pack/plugins/fleet/common/types/models/agent.ts index 34f68fc34c502..256dd76c41ca4 100644 --- a/x-pack/plugins/fleet/common/types/models/agent.ts +++ b/x-pack/plugins/fleet/common/types/models/agent.ts @@ -79,6 +79,8 @@ export interface AgentAction extends NewAgentAction { export interface AgentMetadata { [x: string]: any; } + +// SO definition for this type is declared in server/types/interfaces interface AgentBase { type: AgentType; active: boolean; @@ -124,10 +126,6 @@ export interface Agent extends AgentBase { metrics?: AgentMetrics; } -export interface AgentSOAttributes extends AgentBase { - packages?: string[]; -} - export interface CurrentUpgrade { actionId: string; complete: boolean; diff --git a/x-pack/plugins/fleet/common/types/models/agent_policy.ts b/x-pack/plugins/fleet/common/types/models/agent_policy.ts index 30737d2b2052f..a701d4653f242 100644 --- a/x-pack/plugins/fleet/common/types/models/agent_policy.ts +++ b/x-pack/plugins/fleet/common/types/models/agent_policy.ts @@ -36,6 +36,7 @@ export interface NewAgentPolicy { is_protected?: boolean; } +// SO definition for this type is declared in server/types/interfaces export interface AgentPolicy extends Omit { id: string; status: ValueOf; @@ -48,8 +49,6 @@ export interface AgentPolicy extends Omit { is_protected: boolean; } -export type AgentPolicySOAttributes = Omit; - export interface FullAgentPolicyInputStream { id: string; data_stream: { diff --git a/x-pack/plugins/fleet/common/types/models/enrollment_api_key.ts b/x-pack/plugins/fleet/common/types/models/enrollment_api_key.ts index 5c5568f7dad42..4398675831b45 100644 --- a/x-pack/plugins/fleet/common/types/models/enrollment_api_key.ts +++ b/x-pack/plugins/fleet/common/types/models/enrollment_api_key.ts @@ -5,6 +5,7 @@ * 2.0. */ +// SO definition for this type is declared in server/types/interfaces export interface EnrollmentAPIKey { id: string; api_key_id: string; @@ -15,8 +16,6 @@ export interface EnrollmentAPIKey { created_at: string; } -export type EnrollmentAPIKeySOAttributes = Omit; - // Generated /** diff --git a/x-pack/plugins/fleet/common/types/models/fleet_proxy.ts b/x-pack/plugins/fleet/common/types/models/fleet_proxy.ts index bad53dacdff93..1c69b4db9fbfa 100644 --- a/x-pack/plugins/fleet/common/types/models/fleet_proxy.ts +++ b/x-pack/plugins/fleet/common/types/models/fleet_proxy.ts @@ -14,6 +14,7 @@ interface BaseFleetProxy { is_preconfigured: boolean; } +// SO definition for this type is declared in server/types/interfaces export interface NewFleetProxy extends BaseFleetProxy { proxy_headers?: Record | null; } @@ -21,7 +22,3 @@ export interface NewFleetProxy extends BaseFleetProxy { export interface FleetProxy extends NewFleetProxy { id: string; } - -export interface FleetProxySOAttributes extends BaseFleetProxy { - proxy_headers?: string | null; -} diff --git a/x-pack/plugins/fleet/common/types/models/fleet_server_policy_config.ts b/x-pack/plugins/fleet/common/types/models/fleet_server_policy_config.ts index 8b7c2ffc0913f..5609853a4ad2e 100644 --- a/x-pack/plugins/fleet/common/types/models/fleet_server_policy_config.ts +++ b/x-pack/plugins/fleet/common/types/models/fleet_server_policy_config.ts @@ -5,6 +5,7 @@ * 2.0. */ +// SO definition for this type is declared in server/types/interfaces export interface NewFleetServerHost { name: string; host_urls: string[]; @@ -16,5 +17,3 @@ export interface NewFleetServerHost { export interface FleetServerHost extends NewFleetServerHost { id: string; } - -export type FleetServerHostSOAttributes = NewFleetServerHost; diff --git a/x-pack/plugins/fleet/common/types/models/output.ts b/x-pack/plugins/fleet/common/types/models/output.ts index f8836dae58470..665498cf8f322 100644 --- a/x-pack/plugins/fleet/common/types/models/output.ts +++ b/x-pack/plugins/fleet/common/types/models/output.ts @@ -30,11 +30,6 @@ export interface NewOutput { allow_edit?: string[]; } -export type OutputSOAttributes = NewOutput & { - output_id?: string; - ssl?: string | null; // encrypted ssl field -}; - export type Output = NewOutput & { id: string; }; diff --git a/x-pack/plugins/fleet/common/types/models/package_policy.ts b/x-pack/plugins/fleet/common/types/models/package_policy.ts index fe4013c522be7..63c55952f6257 100644 --- a/x-pack/plugins/fleet/common/types/models/package_policy.ts +++ b/x-pack/plugins/fleet/common/types/models/package_policy.ts @@ -92,6 +92,7 @@ export interface UpdatePackagePolicy extends NewPackagePolicy { version?: string; } +// SO definition for this type is declared in server/types/interfaces export interface PackagePolicy extends Omit { id: string; inputs: PackagePolicyInput[]; @@ -105,8 +106,6 @@ export interface PackagePolicy extends Omit { created_by: string; } -export type PackagePolicySOAttributes = Omit; - export type DryRunPackagePolicy = NewPackagePolicy & { errors?: Array<{ key: string | undefined; message: string }>; missingVars?: string[]; diff --git a/x-pack/plugins/fleet/common/types/models/settings.ts b/x-pack/plugins/fleet/common/types/models/settings.ts index c70fa944e6c24..01f95146e3621 100644 --- a/x-pack/plugins/fleet/common/types/models/settings.ts +++ b/x-pack/plugins/fleet/common/types/models/settings.ts @@ -5,8 +5,6 @@ * 2.0. */ -import type { SavedObjectAttributes } from '@kbn/core/public'; - export interface BaseSettings { has_seen_add_data_notice?: boolean; fleet_server_hosts?: string[]; @@ -17,5 +15,3 @@ export interface Settings extends BaseSettings { id: string; preconfigured_fields?: Array<'fleet_server_hosts'>; } - -export interface SettingsSOAttributes extends BaseSettings, SavedObjectAttributes {} diff --git a/x-pack/plugins/fleet/server/integration_tests/cloud_preconfiguration.test.ts b/x-pack/plugins/fleet/server/integration_tests/cloud_preconfiguration.test.ts index f021becf6f444..20b9e25e7994a 100644 --- a/x-pack/plugins/fleet/server/integration_tests/cloud_preconfiguration.test.ts +++ b/x-pack/plugins/fleet/server/integration_tests/cloud_preconfiguration.test.ts @@ -15,8 +15,11 @@ import { } from '@kbn/core-test-helpers-kbn-server'; import { AGENT_POLICY_INDEX } from '../../common'; -import type { PackagePolicySOAttributes, OutputSOAttributes } from '../../common/types'; -import type { AgentPolicySOAttributes } from '../types'; +import type { + AgentPolicySOAttributes, + PackagePolicySOAttributes, + OutputSOAttributes, +} from '../types'; import { useDockerRegistry, waitForFleetSetup } from './helpers'; import { diff --git a/x-pack/plugins/fleet/server/services/agent_policy.ts b/x-pack/plugins/fleet/server/services/agent_policy.ts index 96b53da7cbb6c..b69b8ba631423 100644 --- a/x-pack/plugins/fleet/server/services/agent_policy.ts +++ b/x-pack/plugins/fleet/server/services/agent_policy.ts @@ -398,7 +398,10 @@ class AgentPolicyService { const filter = kuery ? normalizeKuery(SAVED_OBJECT_TYPE, kuery) : undefined; let agentPoliciesSO; try { - agentPoliciesSO = await soClient.find({ ...baseFindParams, filter }); + agentPoliciesSO = await soClient.find({ + ...baseFindParams, + filter, + }); } catch (e) { const isBadRequest = e.output?.statusCode === 400; const isKQLSyntaxError = e.message?.startsWith('KQLSyntaxError'); diff --git a/x-pack/plugins/fleet/server/services/agents/saved_objects.ts b/x-pack/plugins/fleet/server/services/agents/saved_objects.ts deleted file mode 100644 index 596c7db5d8472..0000000000000 --- a/x-pack/plugins/fleet/server/services/agents/saved_objects.ts +++ /dev/null @@ -1,26 +0,0 @@ -/* - * 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 type { SavedObject } from '@kbn/core/server'; - -import type { Agent, AgentSOAttributes } from '../../types'; - -export function savedObjectToAgent(so: SavedObject): Agent { - if (so.error) { - throw new Error(so.error.message); - } - - return { - id: so.id, - ...so.attributes, - local_metadata: so.attributes.local_metadata, - user_provided_metadata: so.attributes.user_provided_metadata, - access_api_key: undefined, - status: undefined, - packages: so.attributes.packages ?? [], - }; -} diff --git a/x-pack/plugins/fleet/server/services/epm/packages/get.test.ts b/x-pack/plugins/fleet/server/services/epm/packages/get.test.ts index e4cd54f90973d..14fe84796b9fd 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/get.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/get.test.ts @@ -11,7 +11,8 @@ import { SavedObjectsErrorHelpers } from '@kbn/core/server'; import { savedObjectsClientMock } from '@kbn/core/server/mocks'; import { PACKAGES_SAVED_OBJECT_TYPE, PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '../../../../common'; -import type { PackagePolicySOAttributes, RegistryPackage } from '../../../../common/types'; +import type { RegistryPackage } from '../../../../common/types'; +import type { PackagePolicySOAttributes } from '../../../types'; import { createAppContextStartContractMock } from '../../../mocks'; import { appContextService } from '../../app_context'; diff --git a/x-pack/plugins/fleet/server/services/epm/packages/get.ts b/x-pack/plugins/fleet/server/services/epm/packages/get.ts index 351128af7f4bf..696aa6a272970 100644 --- a/x-pack/plugins/fleet/server/services/epm/packages/get.ts +++ b/x-pack/plugins/fleet/server/services/epm/packages/get.ts @@ -16,11 +16,7 @@ import { SO_SEARCH_LIMIT, } from '../../../../common/constants'; import { isPackageLimited } from '../../../../common/services'; -import type { - PackageUsageStats, - PackagePolicySOAttributes, - Installable, -} from '../../../../common/types'; +import type { PackageUsageStats, Installable } from '../../../../common/types'; import { PACKAGES_SAVED_OBJECT_TYPE } from '../../../constants'; import type { ArchivePackage, @@ -28,7 +24,7 @@ import type { EpmPackageAdditions, GetCategoriesRequest, } from '../../../../common/types'; -import type { Installation, PackageInfo } from '../../../types'; +import type { Installation, PackageInfo, PackagePolicySOAttributes } from '../../../types'; import { FleetError, PackageFailedVerificationError, diff --git a/x-pack/plugins/fleet/server/services/output.ts b/x-pack/plugins/fleet/server/services/output.ts index e6ed4d2d4986f..8666330f6640f 100644 --- a/x-pack/plugins/fleet/server/services/output.ts +++ b/x-pack/plugins/fleet/server/services/output.ts @@ -164,7 +164,7 @@ async function validateTypeChanges( soClient: SavedObjectsClientContract, esClient: ElasticsearchClient, id: string, - data: Partial, + data: Nullable>, originalOutput: Output, defaultDataOutputId: string | null, fromPreconfiguration: boolean @@ -194,18 +194,18 @@ async function validateTypeChanges( async function updateFleetServerPoliciesDataOutputId( soClient: SavedObjectsClientContract, esClient: ElasticsearchClient, - data: Partial, + data: Nullable>, isDefault: boolean, defaultDataOutputId: string | null, fleetServerPolicies: AgentPolicy[], fromPreconfiguration: boolean ) { // if a logstash output is updated to become default - // if fleet server policies are don't have data_output_id or if they are using the new output + // if fleet server policies don't have data_output_id // update them to use the default output if (data?.type === outputType.Logstash && isDefault) { for (const policy of fleetServerPolicies) { - if (!policy.data_output_id || policy.data_output_id === data?.id) { + if (!policy.data_output_id) { await agentPolicyService.update( soClient, esClient, @@ -582,7 +582,7 @@ class OutputService { soClient, esClient, id, - data, + updateData, originalOutput, defaultDataOutputId, fromPreconfiguration diff --git a/x-pack/plugins/fleet/server/services/settings.ts b/x-pack/plugins/fleet/server/services/settings.ts index 5b48ee8177885..2b5d228ffc97e 100644 --- a/x-pack/plugins/fleet/server/services/settings.ts +++ b/x-pack/plugins/fleet/server/services/settings.ts @@ -10,7 +10,8 @@ import type { SavedObjectsClientContract } from '@kbn/core/server'; import { normalizeHostsForAgents } from '../../common/services'; import { GLOBAL_SETTINGS_SAVED_OBJECT_TYPE, GLOBAL_SETTINGS_ID } from '../../common/constants'; -import type { SettingsSOAttributes, Settings, BaseSettings } from '../../common/types'; +import type { Settings, BaseSettings } from '../../common/types'; +import type { SettingsSOAttributes } from '../types'; import { appContextService } from './app_context'; import { listFleetServerHosts } from './fleet_server_host'; diff --git a/x-pack/plugins/fleet/server/types/index.tsx b/x-pack/plugins/fleet/server/types/index.tsx index a89405eb54f05..1f669f07d1433 100644 --- a/x-pack/plugins/fleet/server/types/index.tsx +++ b/x-pack/plugins/fleet/server/types/index.tsx @@ -8,7 +8,6 @@ export type { Agent, AgentMetadata, - AgentSOAttributes, AgentStatus, AgentType, AgentAction, @@ -22,12 +21,10 @@ export type { NewPackagePolicy, UpdatePackagePolicy, DryRunPackagePolicy, - PackagePolicySOAttributes, FullAgentPolicyInput, FullAgentPolicy, FullAgentPolicyOutput, AgentPolicy, - AgentPolicySOAttributes, NewAgentPolicy, PreconfiguredAgentPolicy, AgentPolicyStatus, @@ -35,16 +32,12 @@ export type { ShipperOutput, Output, NewOutput, - OutputSOAttributes, OutputType, EnrollmentAPIKey, - EnrollmentAPIKeySOAttributes, NewFleetServerHost, FleetServerHost, - FleetServerHostSOAttributes, NewFleetProxy, FleetProxy, - FleetProxySOAttributes, Installation, EpmPackageInstallStatus, InstallationStatus, @@ -73,7 +66,6 @@ export type { TemplateMap, TemplateMapEntry, Settings, - SettingsSOAttributes, InstallType, InstallSource, InstallResult, @@ -119,4 +111,5 @@ export interface PackageVerificationResult { export * from './models'; export * from './rest_spec'; export * from './extensions'; +export * from './so_attributes'; export type { FleetRequestHandler, FleetRequestHandlerContext } from './request_context'; diff --git a/x-pack/plugins/fleet/server/types/so_attributes.ts b/x-pack/plugins/fleet/server/types/so_attributes.ts new file mode 100644 index 0000000000000..bdc8cf2886fe5 --- /dev/null +++ b/x-pack/plugins/fleet/server/types/so_attributes.ts @@ -0,0 +1,143 @@ +/* + * 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. + */ + +// Definitions for SO attributes. They mirror the types defined under /common/types/models +import type { agentPolicyStatuses } from '../../common/constants'; +import type { + MonitoringType, + ValueOf, + AgentMetadata, + OutputType, + ShipperOutput, +} from '../../common/types'; +import type { AgentType, FleetServerAgentComponent } from '../../common/types/models'; + +import type { + PackagePolicy, + PackagePolicyInput, + PackagePolicyPackage, + PackagePolicyConfigRecord, +} from '../../common/types/models/package_policy'; +import type { PolicySecretReference } from '../../common/types/models/secret'; + +export type AgentPolicyStatus = typeof agentPolicyStatuses; + +export interface AgentPolicySOAttributes { + name: string; + namespace: string; + is_managed: boolean; // required for created policy + updated_at: string; + updated_by: string; + revision: number; + is_protected: boolean; + description?: string; + is_default?: boolean; + is_default_fleet_server?: boolean; // Optional when creating a policy + has_fleet_server?: boolean; + monitoring_enabled?: MonitoringType; + unenroll_timeout?: number; + inactivity_timeout?: number; + is_preconfigured?: boolean; + // Nullable to allow user to reset to default outputs + data_output_id?: string | null; + monitoring_output_id?: string | null; + download_source_id?: string | null; + fleet_server_host_id?: string | null; + schema_version?: string; + agent_features?: Array<{ name: string; enabled: boolean }>; + status: ValueOf; + package_policies?: PackagePolicy[]; + agents?: number; +} + +export interface AgentSOAttributes { + type: AgentType; + active: boolean; + enrolled_at: string; + user_provided_metadata: AgentMetadata; + local_metadata: AgentMetadata; + unenrolled_at?: string; + unenrollment_started_at?: string; + upgraded_at?: string | null; + upgrade_started_at?: string | null; + access_api_key_id?: string; + default_api_key?: string; + default_api_key_id?: string; + policy_id?: string; + policy_revision?: number | null; + last_checkin?: string; + last_checkin_status?: 'error' | 'online' | 'degraded' | 'updating'; + last_checkin_message?: string; + tags?: string[]; + components?: FleetServerAgentComponent[]; + packages?: string[]; +} + +export interface FleetProxySOAttributes { + name: string; + url: string; + is_preconfigured: boolean; + certificate_authorities?: string | null; + certificate?: string | null; + certificate_key?: string | null; + proxy_headers?: string | null; +} + +export interface FleetServerHostSOAttributes { + name: string; + host_urls: string[]; + is_default: boolean; + is_preconfigured: boolean; + proxy_id?: string | null; +} + +export interface PackagePolicySOAttributes { + name: string; + namespace: string; + enabled: boolean; + revision: number; + created_at: string; + created_by: string; + inputs: PackagePolicyInput[]; + policy_id: string; + updated_at: string; + updated_by: string; + description?: string; + is_managed?: boolean; + secret_references?: PolicySecretReference[]; + package?: PackagePolicyPackage; + vars?: PackagePolicyConfigRecord; + elasticsearch?: { + privileges?: { + cluster?: string[]; + }; + }; + agents?: number; +} + +export interface OutputSOAttributes { + is_default: boolean; + is_default_monitoring: boolean; + name: string; + type: ValueOf; + hosts?: string[]; + ca_sha256?: string | null; + ca_trusted_fingerprint?: string | null; + is_preconfigured?: boolean; + config_yaml?: string | null; + proxy_id?: string | null; + shipper?: ShipperOutput | null; + allow_edit?: string[]; + output_id?: string; + ssl?: string | null; // encrypted ssl field +} + +export interface SettingsSOAttributes { + has_seen_add_data_notice?: boolean; + fleet_server_hosts?: string[]; + prerelease_integrations_enabled: boolean; +}