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

[8.x] [Fleet] delete unenrolled agents task #196072

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions packages/kbn-check-mappings-update-cli/current_fields.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,7 @@
"payload.connector.type",
"type"
],
"cloud-security-posture-settings": [
"rules"
],
"cloud-security-posture-settings": [],
"config": [
"buildNum"
],
Expand Down Expand Up @@ -718,6 +716,9 @@
"vars"
],
"ingest_manager_settings": [
"delete_unenrolled_agents",
"delete_unenrolled_agents.enabled",
"delete_unenrolled_agents.is_preconfigured",
"fleet_server_hosts",
"has_seen_add_data_notice",
"output_secret_storage_requirements_met",
Expand Down
12 changes: 12 additions & 0 deletions packages/kbn-check-mappings-update-cli/current_mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2373,6 +2373,18 @@
},
"ingest_manager_settings": {
"properties": {
"delete_unenrolled_agents": {
"properties": {
"enabled": {
"index": false,
"type": "boolean"
},
"is_preconfigured": {
"index": false,
"type": "boolean"
}
}
},
"fleet_server_hosts": {
"type": "keyword"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,7 @@ export async function checkIncompatibleMappings({
throw createFailError(
`Only mappings changes that are compatible with current mappings are allowed. Consider reaching out to the Kibana core team if you are stuck.`
);
} finally {
await esClient.indices.delete({ index: TEST_INDEX_NAME }).catch(() => {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"ingest-download-sources": "279a68147e62e4d8858c09ad1cf03bd5551ce58d",
"ingest-outputs": "daafff49255ab700e07491376fe89f04fc998b91",
"ingest-package-policies": "53a94064674835fdb35e5186233bcd7052eabd22",
"ingest_manager_settings": "e794576a05d19dd5306a1e23cbb82c09bffabd65",
"ingest_manager_settings": "111a616eb72627c002029c19feb9e6c439a10505",
"inventory-view": "b8683c8e352a286b4aca1ab21003115a4800af83",
"kql-telemetry": "93c1d16c1a0dfca9c8842062cf5ef8f62ae401ad",
"legacy-url-alias": "9b8cca3fbb2da46fd12823d3cd38fdf1c9f24bc8",
Expand Down
22 changes: 22 additions & 0 deletions x-pack/plugins/fleet/common/openapi/bundled.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@
},
"additional_yaml_config": {
"type": "string"
},
"delete_unenrolled_agents": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
},
"is_preconfigured": {
"type": "boolean"
}
}
}
}
}
Expand Down Expand Up @@ -6187,6 +6198,17 @@
},
"prerelease_integrations_enabled": {
"type": "boolean"
},
"delete_unenrolled_agents": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
},
"is_preconfigured": {
"type": "boolean"
}
}
}
},
"required": [
Expand Down
14 changes: 14 additions & 0 deletions x-pack/plugins/fleet/common/openapi/bundled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ paths:
type: boolean
additional_yaml_config:
type: string
delete_unenrolled_agents:
type: object
properties:
enabled:
type: boolean
is_preconfigured:
type: boolean
responses:
'200':
description: OK
Expand Down Expand Up @@ -3866,6 +3873,13 @@ components:
type: string
prerelease_integrations_enabled:
type: boolean
delete_unenrolled_agents:
type: object
properties:
enabled:
type: boolean
is_preconfigured:
type: boolean
required:
- fleet_server_hosts
- id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ properties:
type: string
prerelease_integrations_enabled:
type: boolean
delete_unenrolled_agents:
type: object
properties:
enabled:
type: boolean
is_preconfigured:
type: boolean

required:
- fleet_server_hosts
- id
7 changes: 7 additions & 0 deletions x-pack/plugins/fleet/common/openapi/paths/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ put:
type: boolean
additional_yaml_config:
type: string
delete_unenrolled_agents:
type: object
properties:
enabled:
type: boolean
is_preconfigured:
type: boolean
responses:
'200':
description: OK
Expand Down
6 changes: 5 additions & 1 deletion x-pack/plugins/fleet/common/types/models/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
export interface BaseSettings {
has_seen_add_data_notice?: boolean;
fleet_server_hosts?: string[];
prerelease_integrations_enabled: boolean;
prerelease_integrations_enabled?: boolean;
}

export interface Settings extends BaseSettings {
Expand All @@ -19,4 +19,8 @@ export interface Settings extends BaseSettings {
output_secret_storage_requirements_met?: boolean;
use_space_awareness_migration_status?: 'pending' | 'success' | 'error';
use_space_awareness_migration_started_at?: string | null;
delete_unenrolled_agents?: {
enabled: boolean;
is_preconfigured: boolean;
};
}
Original file line number Diff line number Diff line change
@@ -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.
*/

import React, { useCallback, useEffect } from 'react';

import {
EuiTitle,
EuiLink,
EuiSpacer,
EuiDescribedFormGroup,
EuiSwitch,
EuiForm,
EuiFormRow,
EuiToolTip,
} from '@elastic/eui';
import { FormattedMessage } from '@kbn/i18n-react';

import { i18n } from '@kbn/i18n';

import {
useAuthz,
useGetSettings,
usePutSettingsMutation,
useStartServices,
} from '../../../../hooks';

export const AdvancedSection: React.FunctionComponent<{}> = ({}) => {
const authz = useAuthz();
const { docLinks, notifications } = useStartServices();
const deleteUnenrolledAgents =
useGetSettings().data?.item?.delete_unenrolled_agents?.enabled ?? false;
const isPreconfigured =
useGetSettings().data?.item?.delete_unenrolled_agents?.is_preconfigured ?? false;
const [deleteUnenrolledAgentsChecked, setDeleteUnenrolledAgentsChecked] =
React.useState<boolean>(deleteUnenrolledAgents);
const { mutateAsync: mutateSettingsAsync } = usePutSettingsMutation();

useEffect(() => {
if (deleteUnenrolledAgents) {
setDeleteUnenrolledAgentsChecked(deleteUnenrolledAgents);
}
}, [deleteUnenrolledAgents]);

const updateSettings = useCallback(
async (deleteFlag: boolean) => {
try {
setDeleteUnenrolledAgentsChecked(deleteFlag);
const res = await mutateSettingsAsync({
delete_unenrolled_agents: {
enabled: deleteFlag,
is_preconfigured: false,
},
});

if (res.error) {
throw res.error;
}
} catch (error) {
setDeleteUnenrolledAgentsChecked(!deleteFlag);
notifications.toasts.addError(error, {
title: i18n.translate('xpack.fleet.errorUpdatingSettings', {
defaultMessage: 'Error updating settings',
}),
});
}
},
[mutateSettingsAsync, notifications.toasts]
);

return (
<>
<EuiTitle size="s">
<h4 data-test-subj="advancedHeader">
<FormattedMessage
id="xpack.fleet.settings.advancedSectionTitle"
defaultMessage="Advanced Settings"
/>
</h4>
</EuiTitle>
<EuiSpacer size="m" />
<EuiForm component="form">
<EuiDescribedFormGroup
title={
<h3>
<FormattedMessage
id="xpack.fleet.settings.deleteUnenrolledAgentsLabel"
defaultMessage="Delete unenrolled agents"
/>
</h3>
}
description={
<p>
<FormattedMessage
id="xpack.fleet.settings.advancedSection.switchLabel"
defaultMessage="Switching on this setting will enable auto deletion of unenrolled agents. For more information see our {docLink}."
values={{
docLink: (
<EuiLink target="_blank" external href={docLinks.links.fleet.settings}>
<FormattedMessage
id="xpack.fleet.settings.advancedSection.link"
defaultMessage="docs"
/>
</EuiLink>
),
}}
/>
</p>
}
>
<EuiFormRow label="">
<EuiToolTip
content={
isPreconfigured
? i18n.translate('xpack.fleet.settings.advancedSection.preconfiguredTitle', {
defaultMessage: 'This setting is preconfigured and cannot be updated.',
})
: undefined
}
>
<EuiSwitch
label={
<FormattedMessage
id="xpack.fleet.settings.deleteUnenrolledAgentsLabel"
defaultMessage="Delete unenrolled agents"
/>
}
checked={deleteUnenrolledAgentsChecked}
onChange={(e) => updateSettings(e.target.checked)}
disabled={!authz.fleet.allSettings || isPreconfigured}
/>
</EuiToolTip>
</EuiFormRow>
</EuiDescribedFormGroup>
</EuiForm>

<EuiSpacer size="m" />
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { FleetServerHostsSection } from './fleet_server_hosts_section';
import { OutputSection } from './output_section';
import { AgentBinarySection } from './agent_binary_section';
import { FleetProxiesSection } from './fleet_proxies_section';
import { AdvancedSection } from './advanced_section';

export interface SettingsPageProps {
outputs: Output[];
Expand Down Expand Up @@ -52,6 +53,8 @@ export const SettingsPage: React.FunctionComponent<SettingsPageProps> = ({
/>
<EuiSpacer size="m" />
<FleetProxiesSection proxies={proxies} deleteFleetProxy={deleteFleetProxy} />
<EuiSpacer size="m" />
<AdvancedSection />
</>
);
};
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const config: PluginConfigDescriptor = {
schema: schema.object(
{
isAirGapped: schema.maybe(schema.boolean({ defaultValue: false })),
enableDeleteUnenrolledAgents: schema.maybe(schema.boolean({ defaultValue: false })),
registryUrl: schema.maybe(schema.uri({ scheme: ['http', 'https'] })),
registryProxyUrl: schema.maybe(schema.uri({ scheme: ['http', 'https'] })),
agents: schema.object({
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export class OutputUnauthorizedError extends FleetError {}
export class OutputInvalidError extends FleetError {}
export class OutputLicenceError extends FleetError {}
export class DownloadSourceError extends FleetError {}
export class DeleteUnenrolledAgentsPreconfiguredError extends FleetError {}

// Not found errors
export class AgentNotFoundError extends FleetNotFoundError {}
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export const createAppContextStartContractMock = (
}
: {}),
unenrollInactiveAgentsTask: {} as any,
deleteUnenrolledAgentsTask: {} as any,
};
};

Expand Down
Loading