Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
89 changes: 89 additions & 0 deletions x-pack/plugins/fleet/common/settings/agent_policy_settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,93 @@ export const AGENT_POLICY_ADVANCED_SETTINGS: SettingsConfig[] = [
},
schema: z.number().int().min(0).default(0),
},
{
name: 'agent.download.timeout',
hidden: true,
title: i18n.translate('xpack.fleet.settings.agentPolicyAdvanced.downloadTimeoutTitle', {
defaultMessage: 'Agent binary download timeout',
}),
description: i18n.translate(
'xpack.fleet.settings.agentPolicyAdvanced.downloadTimeoutDescription',
{
defaultMessage: 'Timeout for downloading the agent binary',
}
),
learnMoreLink:
'https://www.elastic.co/guide/en/fleet/current/enable-custom-policy-settings.html#configure-agent-download-timeout',
api_field: {
name: 'agent_download_timeout',
},
schema: zodStringWithDurationValidation.default('2h'),
},
{
name: 'agent.download.target_directory',
hidden: true,
api_field: {
name: 'agent_download_target_directory',
},
title: i18n.translate(
'xpack.fleet.settings.agentPolicyAdvanced.agentDownloadTargetDirectoryTitle',
{
defaultMessage: 'Agent binary target directory',
}
),
description: i18n.translate(
'xpack.fleet.settings.agentPolicyAdvanced.agentDownloadTargetDirectoryDescription',
{
defaultMessage: 'The disk path to which the agent binary will be downloaded',
}
),
learnMoreLink:
'https://www.elastic.co/guide/en/fleet/current/elastic-agent-standalone-download.html',
schema: z.string(),
},
{
name: 'agent.logging.metrics.period',
hidden: true,
api_field: {
name: 'agent_logging_metrics_period',
},
title: i18n.translate(
'xpack.fleet.settings.agentPolicyAdvanced.agentLoggingMetricsPeriodTitle',
{
defaultMessage: 'Agent logging metrics period',
}
),
description: i18n.translate(
'xpack.fleet.settings.agentPolicyAdvanced.agentLoggingMetricsPeriodDescription',
{
defaultMessage: 'The frequency of agent metrics logging',
}
),
learnMoreLink:
'https://www.elastic.co/guide/en/fleet/current/elastic-agent-standalone-logging-config.html#elastic-agent-standalone-logging-settings',
schema: zodStringWithDurationValidation.default('30s'),
},
{
name: 'agent.monitoring.http',
hidden: true,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a hidden flag to be able to hide a config without deleting from this file. This acts like a feature flag, so when a setting is ready to be rendered, just update hidden: false.

api_field: {
name: 'agent_monitoring_http',
},
title: i18n.translate('xpack.fleet.settings.agentPolicyAdvanced.agentMonitoringHttpTitle', {
defaultMessage: 'Agent HTTP monitoring',
}),
description: i18n.translate(
'xpack.fleet.settings.agentPolicyAdvanced.agentMonitoringHttpDescription',
{
defaultMessage: 'Agent HTTP monitoring settings',
}
),
learnMoreLink:
'https://www.elastic.co/guide/en/fleet/current/enable-custom-policy-settings.html#override-default-monitoring-port',
schema: z
.object({
enabled: z.boolean().describe('Enabled').default(false),
host: z.string().describe('Host').default('localhost'),
port: z.number().describe('Port').min(0).max(65353).default(6791),
'buffer.enabled': z.boolean().describe('Buffer Enabled').default(false),
})
.default({}),
},
];
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/settings/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ export interface SettingsConfig {
api_field: {
name: string;
};
hidden?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,86 @@ describe('ConfiguredSettings', () => {
).not.toBeNull();
expect(mockUpdateAdvancedSettingsHasErrors).toHaveBeenCalledWith(true);
});

it('should render field group', () => {
const result = render([
{
name: 'agent.monitoring.http',
api_field: {
name: 'agent_monitoring_http',
},
title: 'Agent HTTP monitoring',
description: 'Agent HTTP monitoring settings',
learnMoreLink:
'https://www.elastic.co/guide/en/fleet/current/enable-custom-policy-settings.html#override-default-monitoring-port',
schema: z
.object({
enabled: z.boolean().describe('Enabled').default(false),
host: z.string().describe('Host').default('localhost'),
port: z.number().describe('Port').min(0).max(65353).default(6791),
'buffer.enabled': z.boolean().describe('Buffer Enabled').default(false),
})
.default({}),
},
]);

expect(result.getByText('Agent HTTP monitoring')).not.toBeNull();
expect(result.getByText('Buffer Enabled')).not.toBeNull();
const switches = result.getAllByRole('switch');
expect(switches).toHaveLength(2);
expect(switches[0]).not.toBeChecked();
expect(switches[1]).not.toBeChecked();
const port = result.getByTestId('configuredSetting-agent.monitoring.http-port');
expect(port).toHaveValue(6791);
const host = result.getByTestId('configuredSetting-agent.monitoring.http-host');
expect(host).toHaveValue('localhost');

act(() => {
fireEvent.click(switches[0]);
});

expect(mockUpdateAgentPolicy).toHaveBeenCalledWith(
expect.objectContaining({
advanced_settings: expect.objectContaining({ agent_monitoring_http: { enabled: true } }),
})
);

act(() => {
fireEvent.change(port, { target: { value: '6792' } });
});

expect(mockUpdateAgentPolicy).toHaveBeenCalledWith(
expect.objectContaining({
advanced_settings: expect.objectContaining({ agent_monitoring_http: { port: 6792 } }),
})
);

act(() => {
fireEvent.change(host, { target: { value: '1.2.3.4' } });
});

expect(mockUpdateAgentPolicy).toHaveBeenCalledWith(
expect.objectContaining({
advanced_settings: expect.objectContaining({ agent_monitoring_http: { host: '1.2.3.4' } }),
})
);
});

it('should not render field if hidden', () => {
const result = render([
{
name: 'agent.limits.go_max_procs',
hidden: true,
title: 'GO_MAX_PROCS',
description: 'Description',
learnMoreLink: '',
api_field: {
name: 'agent_limits_go_max_procs',
},
schema: z.number().int().min(0).default(0),
},
]);

expect(result.queryByText('GO_MAX_PROCS')).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
* 2.0.
*/

import { z, ZodFirstPartyTypeKind } from 'zod';
import React, { useState } from 'react';
import {
EuiDescribedFormGroup,
EuiFieldNumber,
EuiFieldText,
EuiFormRow,
EuiLink,
} from '@elastic/eui';
import { ZodFirstPartyTypeKind } from 'zod';
import React from 'react';
import { EuiFieldNumber, EuiFieldText } from '@elastic/eui';

import type { SettingsConfig } from '../../../../../common/settings/types';
import { useAgentPolicyFormContext } from '../../sections/agent_policy/components/agent_policy_form';

import { SettingsFieldGroup } from './settings_field_group';
import { getInnerType, SettingsFieldWrapper } from './settings_field_wrapper';

export const settingComponentRegistry = new Map<
string,
(settingsconfig: SettingsConfig) => React.ReactElement
>();

settingComponentRegistry.set(ZodFirstPartyTypeKind.ZodObject, (settingsConfig) => (
<SettingsFieldGroup settingsConfig={settingsConfig} />
));

settingComponentRegistry.set(ZodFirstPartyTypeKind.ZodNumber, (settingsConfig) => {
return (
<SettingsFieldWrapper
Expand Down Expand Up @@ -61,97 +61,24 @@ settingComponentRegistry.set(ZodFirstPartyTypeKind.ZodString, (settingsConfig) =
);
});

const SettingsFieldWrapper: React.FC<{
settingsConfig: SettingsConfig;
typeName: keyof typeof ZodFirstPartyTypeKind;
renderItem: Function;
}> = ({ settingsConfig, typeName, renderItem }) => {
const [error, setError] = useState('');
const agentPolicyFormContext = useAgentPolicyFormContext();

const fieldKey = `configuredSetting-${settingsConfig.name}`;
const defaultValue: number =
settingsConfig.schema instanceof z.ZodDefault
? settingsConfig.schema._def.defaultValue()
: undefined;
const coercedSchema = settingsConfig.schema as z.ZodString;

const convertValue = (value: string, type: keyof typeof ZodFirstPartyTypeKind): any => {
if (type === ZodFirstPartyTypeKind.ZodNumber) {
if (value === '') {
return 0;
}
return parseInt(value, 10);
}
return value;
};

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const newValue = convertValue(e.target.value, typeName);
const validationResults = coercedSchema.safeParse(newValue);

if (!validationResults.success) {
setError(validationResults.error.issues[0].message);
agentPolicyFormContext?.updateAdvancedSettingsHasErrors(true);
} else {
setError('');
agentPolicyFormContext?.updateAdvancedSettingsHasErrors(false);
}

const newAdvancedSettings = {
...(agentPolicyFormContext?.agentPolicy.advanced_settings ?? {}),
[settingsConfig.api_field.name]: newValue,
};

agentPolicyFormContext?.updateAgentPolicy({ advanced_settings: newAdvancedSettings });
};

const fieldValue =
agentPolicyFormContext?.agentPolicy.advanced_settings?.[settingsConfig.api_field.name] ??
defaultValue;

return (
<EuiDescribedFormGroup
fullWidth
title={<h4>{settingsConfig.title}</h4>}
description={
<>
{settingsConfig.description}.{' '}
<EuiLink href={settingsConfig.learnMoreLink} external>
Learn more.
</EuiLink>
</>
}
>
<EuiFormRow fullWidth key={fieldKey} error={error} isInvalid={!!error}>
{renderItem({ fieldValue, handleChange, isInvalid: !!error, fieldKey, coercedSchema })}
</EuiFormRow>
</EuiDescribedFormGroup>
);
};

export function ConfiguredSettings({
configuredSettings,
}: {
configuredSettings: SettingsConfig[];
}) {
return (
<>
{configuredSettings.map((configuredSetting) => {
const Component = settingComponentRegistry.get(
configuredSetting.schema instanceof z.ZodDefault
? configuredSetting.schema._def.innerType._def.typeName === 'ZodEffects'
? configuredSetting.schema._def.innerType._def.schema._def.typeName
: configuredSetting.schema._def.innerType._def.typeName
: configuredSetting.schema._def.typeName
);
{configuredSettings
.filter((configuredSetting) => !configuredSetting.hidden)
.map((configuredSetting) => {
const Component = settingComponentRegistry.get(getInnerType(configuredSetting.schema));

if (!Component) {
throw new Error(`Unknown setting type: ${configuredSetting.schema._type}}`);
}
if (!Component) {
throw new Error(`Unknown setting type: ${configuredSetting.schema._type}}`);
}

return <Component key={configuredSetting.name} {...configuredSetting} />;
})}
return <Component key={configuredSetting.name} {...configuredSetting} />;
})}
</>
);
}
Loading