Skip to content

Commit

Permalink
Use settings adapter to create plugin config
Browse files Browse the repository at this point in the history
  • Loading branch information
Machi3mfl committed Dec 18, 2024
1 parent bced596 commit d6ae59d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 450 deletions.
76 changes: 0 additions & 76 deletions plugins/wazuh-core/common/configuration-store.ts

This file was deleted.

120 changes: 36 additions & 84 deletions plugins/wazuh-core/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,23 +405,23 @@ export enum SettingCategory {
API_CONNECTION,
}

type TPluginSettingOptionsTextArea = {
export type TPluginSettingOptionsTextArea = {
maxRows?: number;
minRows?: number;
maxLength?: number;
};

type TPluginSettingOptionsSelect = {
export type TPluginSettingOptionsSelect = {
select: { text: string; value: any }[];
};

type TPluginSettingOptionsEditor = {
export type TPluginSettingOptionsEditor = {
editor: {
language: string;
};
};

type TPluginSettingOptionsFile = {
export type TPluginSettingOptionsFile = {
file: {
type: 'image';
extensions?: string[];
Expand All @@ -444,15 +444,15 @@ type TPluginSettingOptionsFile = {
};
};

type TPluginSettingOptionsNumber = {
export type TPluginSettingOptionsNumber = {
number: {
min?: number;
max?: number;
integer?: boolean;
};
};

type TPluginSettingOptionsSwitch = {
export type TPluginSettingOptionsSwitch = {
switch: {
values: {
disabled: { label?: string; value: any };
Expand All @@ -461,6 +461,16 @@ type TPluginSettingOptionsSwitch = {
};
};

export type TPlugginSettingOptionsObjectOf = {
objectOf: {
[key: string]: TPluginSetting;
};
}

type TPluginSettingOptionsArrayOf = {
arrayOf: TPluginSetting;
}

export enum EpluginSettingType {
text = 'text',
textarea = 'textarea',
Expand All @@ -472,6 +482,7 @@ export enum EpluginSettingType {
password = 'password',
arrayOf = 'arrayOf',
custom = 'custom',
objectOf = 'objectOf',
}

export type TPluginSetting = {
Expand All @@ -484,7 +495,14 @@ export type TPluginSetting = {
// Type.
type: EpluginSettingType;
source: EConfigurationProviders;
options?: TPluginSettingOptionsTextArea | TPluginSettingOptionsSelect | TPluginSettingOptionsEditor | TPluginSettingOptionsFile | TPluginSettingOptionsNumber | TPluginSettingOptionsSwitch;
options?: TPluginSettingOptionsTextArea
| TPluginSettingOptionsSelect
| TPluginSettingOptionsEditor
| TPluginSettingOptionsFile
| TPluginSettingOptionsNumber
| TPluginSettingOptionsSwitch
| TPlugginSettingOptionsObjectOf
| TPluginSettingOptionsArrayOf;
// Default value.
defaultValue: any;
validate?: (value: any) => string | undefined;
Expand Down Expand Up @@ -639,17 +657,7 @@ export const PLUGIN_SETTINGS: { [key: string]: TPluginSetting } = {
defaultValue: false,
validate: SettingsValidator.isBoolean,
},
hosts: {
title: 'Server hosts',
description: 'Configure the API connections.',
source: EConfigurationProviders.INITIALIZER_CONTEXT,
category: SettingCategory.API_CONNECTION,
type: EpluginSettingType.arrayOf,
defaultValue: [],
store: {
file: {
configurableManaged: false,
defaultBlock: `# The following configuration is the default structure to define a host.
/*`# The following configuration is the default structure to define a host.
#
# hosts:
# # Host ID / name,
Expand Down Expand Up @@ -678,39 +686,21 @@ hosts:
username: wazuh-wui
password: wazuh-wui
run_as: false`,
transformFrom: value => {
return value.map(hostData => {
const key = Object.keys(hostData)?.[0];
return { ...hostData[key], id: key };
});
},
},
},
*/
hosts: {
title: 'Server hosts',
description: 'Configure the API connections.',
source: EConfigurationProviders.INITIALIZER_CONTEXT,
category: SettingCategory.API_CONNECTION,
type: EpluginSettingType.objectOf,
defaultValue: [],
options: {
arrayOf: {
id: {
title: 'Identifier',
description: 'Identifier of the API connection. This must be unique.',
type: EpluginSettingType.text,
defaultValue: 'default',
isConfigurableFromSettings: true,
validateUIForm: function (value) {
return this.validate(value);
},
validate: SettingsValidator.compose(
SettingsValidator.isString,
SettingsValidator.isNotEmptyString,
),
},
objectOf: {
url: {
title: 'URL',
description: 'Server URL address',
type: EpluginSettingType.text,
defaultValue: 'https://localhost',
isConfigurableFromSettings: true,
validateUIForm: function (value) {
return this.validate(value);
},
validate: SettingsValidator.compose(
SettingsValidator.isString,
SettingsValidator.isNotEmptyString,
Expand All @@ -721,42 +711,22 @@ hosts:
description: 'Port',
type: EpluginSettingType.number,
defaultValue: 55000,
isConfigurableFromSettings: true,
options: {
number: {
min: 0,
max: 65535,
integer: true,
},
},
uiFormTransformConfigurationValueToInputValue: function (
value: number,
) {
return String(value);
},
uiFormTransformInputValueToConfigurationValue: function (
value: string,
): number {
return Number(value);
},
validateUIForm: function (value) {
return this.validate(
this.uiFormTransformInputValueToConfigurationValue(value),
);
},
validate: function (value) {
return SettingsValidator.number(this.options.number)(value);
return SettingsValidator.number(this.options?.number)(value);
},
},
username: {
title: 'Username',
description: 'Server API username',
type: EpluginSettingType.text,
defaultValue: 'wazuh-wui',
isConfigurableFromSettings: true,
validateUIForm: function (value) {
return this.validate(value);
},
validate: SettingsValidator.compose(
SettingsValidator.isString,
SettingsValidator.isNotEmptyString,
Expand All @@ -767,10 +737,6 @@ hosts:
description: "User's Password",
type: EpluginSettingType.password,
defaultValue: 'wazuh-wui',
isConfigurableFromSettings: true,
validateUIForm: function (value) {
return this.validate(value);
},
validate: SettingsValidator.compose(
SettingsValidator.isString,
SettingsValidator.isNotEmptyString,
Expand All @@ -781,7 +747,6 @@ hosts:
description: 'Use the authentication context.',
type: EpluginSettingType.switch,
defaultValue: false,
isConfigurableFromSettings: true,
options: {
switch: {
values: {
Expand All @@ -790,24 +755,11 @@ hosts:
},
},
},
uiFormTransformChangedInputValue: function (
value: boolean | string,
): boolean {
return Boolean(value);
},
validateUIForm: function (value) {
return this.validate(value);
},
validate: SettingsValidator.isBoolean,
},
},
},
isConfigurableFromSettings: false,
uiFormTransformChangedInputValue: function (
value: boolean | string,
): boolean {
return Boolean(value);
},
// TODO: add validation
// validate: SettingsValidator.isBoolean,
// validate: function (schema) {
Expand Down
Loading

0 comments on commit d6ae59d

Please sign in to comment.