diff --git a/x-pack/plugins/fleet/common/services/validate_package_policy.ts b/x-pack/plugins/fleet/common/services/validate_package_policy.ts index 84abdce15c645..5107cbf8121d7 100644 --- a/x-pack/plugins/fleet/common/services/validate_package_policy.ts +++ b/x-pack/plugins/fleet/common/services/validate_package_policy.ts @@ -97,12 +97,14 @@ export const validatePackagePolicy = ( >((varDefs, policyTemplate) => { (policyTemplate.inputs || []).forEach((input) => { const varDefKey = hasIntegrations ? `${policyTemplate.name}-${input.type}` : input.type; + if ((input.vars || []).length) { varDefs[varDefKey] = keyBy(input.vars || [], 'name'); } }); return varDefs; }, {}); + const streamsByDatasetAndInput = (packageInfo.data_streams || []).reduce< Record >((streams, dataStream) => { @@ -149,6 +151,7 @@ export const validatePackagePolicy = ( if (input.streams.length) { input.streams.forEach((stream) => { const streamValidationResults: PackagePolicyConfigValidationResults = {}; + const streamVarDefs = streamVarDefsByDatasetAndInput[`${stream.data_stream.dataset}-${input.type}`]; @@ -157,7 +160,7 @@ export const validatePackagePolicy = ( streamValidationResults.vars = Object.entries(stream.vars).reduce( (results, [name, configEntry]) => { results[name] = - streamVarDefs[name] && input.enabled && stream.enabled + streamVarDefs && streamVarDefs[name] && input.enabled && stream.enabled ? validatePackagePolicyConfig(configEntry, streamVarDefs[name]) : null; return results; diff --git a/x-pack/plugins/fleet/server/services/package_policy.ts b/x-pack/plugins/fleet/server/services/package_policy.ts index 23dffe78acd28..966fb3a08cb8d 100644 --- a/x-pack/plugins/fleet/server/services/package_policy.ts +++ b/x-pack/plugins/fleet/server/services/package_policy.ts @@ -890,41 +890,51 @@ export function overridePackageInputs( ): DryRunPackagePolicy { if (!inputsOverride) return basePackagePolicy; - const inputs = [...basePackagePolicy.inputs]; - const packageName = basePackagePolicy.package!.name; - let errors = []; + const availablePolicyTemplates = packageInfo.policy_templates ?? []; + + const inputs = [ + ...basePackagePolicy.inputs.filter((input) => { + if (!input.policy_template) { + return true; + } + + const policyTemplate = availablePolicyTemplates.find( + ({ name }) => name === input.policy_template + ); + + // Ignore any policy template removes in the new package version + if (!policyTemplate) { + return false; + } + + // Ignore any inputs removed from this policy template in the new package version + const policyTemplateStillIncludesInput = + policyTemplate.inputs?.some( + (policyTemplateInput) => policyTemplateInput.type === input.type + ) ?? false; + + return policyTemplateStillIncludesInput; + }), + ]; for (const override of inputsOverride) { let originalInput = inputs.find((i) => i.type === override.type); + // If there's no corresponding input on the original package policy, just + // take the override value from the new package as-is. This case typically + // occurs when inputs or package policies are added/removed between versions. if (!originalInput) { - const e = { - error: new IngestManagerError( - i18n.translate('xpack.fleet.packagePolicyInputOverrideError', { - defaultMessage: 'Input type {inputType} does not exist on package {packageName}', - values: { - inputType: override.type, - packageName, - }, - }) - ), - package: { name: packageName, version: basePackagePolicy.package!.version }, - }; + inputs.push(override as NewPackagePolicyInput); + continue; + } - if (dryRun) { - errors.push({ - key: override.type, - message: String(e.error), - }); - continue; - } else { - throw e; - } + if (typeof override.enabled !== 'undefined') { + originalInput.enabled = override.enabled; } - if (typeof override.enabled !== 'undefined') originalInput.enabled = override.enabled; - if (typeof override.keep_enabled !== 'undefined') + if (typeof override.keep_enabled !== 'undefined') { originalInput.keep_enabled = override.keep_enabled; + } if (override.vars) { originalInput = deepMergeVars(originalInput, override); @@ -936,36 +946,7 @@ export function overridePackageInputs( (s) => s.data_stream.dataset === stream.data_stream.dataset ); - if (!originalStream) { - const streamSet = stream.data_stream.dataset; - const e = { - error: new IngestManagerError( - i18n.translate('xpack.fleet.packagePolicyStreamOverrideError', { - defaultMessage: - 'Data stream {streamSet} does not exist on {inputType} of package {packageName}', - values: { - streamSet, - inputType: override.type, - packageName, - }, - }) - ), - package: { name: packageName, version: basePackagePolicy.package!.version }, - }; - - if (dryRun) { - errors.push({ - key: `${override.type}.streams.${streamSet}`, - message: String(e.error), - }); - - continue; - } else { - throw e; - } - } - - if (typeof stream.enabled !== 'undefined') { + if (typeof stream.enabled !== 'undefined' && originalStream) { originalStream.enabled = stream.enabled; } @@ -991,20 +972,22 @@ export function overridePackageInputs( })) .filter(({ message }) => !!message); - errors = [...errors, ...responseFormattedValidationErrors]; - } + if (responseFormattedValidationErrors.length) { + if (dryRun) { + return { ...resultingPackagePolicy, errors: responseFormattedValidationErrors }; + } - if (errors.length) { - if (dryRun) { - return { ...resultingPackagePolicy, errors }; + throw new IngestManagerError( + i18n.translate('xpack.fleet.packagePolicyInvalidError', { + defaultMessage: 'Package policy is invalid: {errors}', + values: { + errors: responseFormattedValidationErrors + .map(({ key, message }) => `${key}: ${message}`) + .join('\n'), + }, + }) + ); } - - throw new IngestManagerError( - i18n.translate('xpack.fleet.packagePolicyInvalidError', { - defaultMessage: 'Package policy is invalid: {errors}', - values: { errors: errors.map(({ key, message }) => `${key}: ${message}`).join('\n') }, - }) - ); } return resultingPackagePolicy; diff --git a/x-pack/plugins/translations/translations/ja-JP.json b/x-pack/plugins/translations/translations/ja-JP.json index d4bac4baaa498..f106bdcae5550 100644 --- a/x-pack/plugins/translations/translations/ja-JP.json +++ b/x-pack/plugins/translations/translations/ja-JP.json @@ -9980,8 +9980,6 @@ "xpack.fleet.oldAppTitle": "Ingest Manager", "xpack.fleet.overviewPageSubtitle": "ElasticElasticエージェントの集中管理", "xpack.fleet.overviewPageTitle": "Fleet", - "xpack.fleet.packagePolicyInputOverrideError": "パッケージ{packageName}には入力タイプ{inputType}が存在しません。", - "xpack.fleet.packagePolicyStreamOverrideError": "パッケージ{packageName}の{inputType}にはデータストリーム{streamSet}が存在しません", "xpack.fleet.packagePolicyValidation.invalidArrayErrorMessage": "無効なフォーマット", "xpack.fleet.packagePolicyValidation.invalidYamlFormatErrorMessage": "YAML形式が無効です", "xpack.fleet.packagePolicyValidation.nameRequiredErrorMessage": "名前が必要です", diff --git a/x-pack/plugins/translations/translations/zh-CN.json b/x-pack/plugins/translations/translations/zh-CN.json index 00488cc0516a5..70d7ae71690fc 100644 --- a/x-pack/plugins/translations/translations/zh-CN.json +++ b/x-pack/plugins/translations/translations/zh-CN.json @@ -10068,8 +10068,6 @@ "xpack.fleet.oldAppTitle": "采集管理器", "xpack.fleet.overviewPageSubtitle": "Elastic 代理的集中管理", "xpack.fleet.overviewPageTitle": "Fleet", - "xpack.fleet.packagePolicyInputOverrideError": "输入类型 {inputType} 在软件包 {packageName} 上不存在", - "xpack.fleet.packagePolicyStreamOverrideError": "数据流 {streamSet} 在软件包 {packageName} 的 {inputType} 上不存在", "xpack.fleet.packagePolicyValidation.invalidArrayErrorMessage": "格式无效", "xpack.fleet.packagePolicyValidation.invalidYamlFormatErrorMessage": "YAML 格式无效", "xpack.fleet.packagePolicyValidation.nameRequiredErrorMessage": "“名称”必填", diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.1.0/data_stream/test_stream/agent/stream/test_stream.yml.hbs b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.1.0/data_stream/test_stream/agent/stream/stream.yml.hbs similarity index 100% rename from x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.1.0/data_stream/test_stream/agent/stream/test_stream.yml.hbs rename to x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.1.0/data_stream/test_stream/agent/stream/stream.yml.hbs diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.2.0-add-non-required-test-var/data_stream/test_stream/agent/stream/test_stream.yml.hbs b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.2.0-add-non-required-test-var/data_stream/test_stream/agent/stream/stream.yml.hbs similarity index 100% rename from x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.2.0-add-non-required-test-var/data_stream/test_stream/agent/stream/test_stream.yml.hbs rename to x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.2.0-add-non-required-test-var/data_stream/test_stream/agent/stream/stream.yml.hbs diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.3.0-remove-test-var/data_stream/test_stream/agent/stream/test_stream.yml.hbs b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.3.0-remove-test-var/data_stream/test_stream/agent/stream/stream.yml.hbs similarity index 100% rename from x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.3.0-remove-test-var/data_stream/test_stream/agent/stream/test_stream.yml.hbs rename to x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.3.0-remove-test-var/data_stream/test_stream/agent/stream/stream.yml.hbs diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.4.0-add-test-var-as-bool/data_stream/test_stream/agent/stream/test_stream.yml.hbs b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.4.0-add-test-var-as-bool/data_stream/test_stream/agent/stream/stream.yml.hbs similarity index 100% rename from x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.4.0-add-test-var-as-bool/data_stream/test_stream/agent/stream/test_stream.yml.hbs rename to x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.4.0-add-test-var-as-bool/data_stream/test_stream/agent/stream/stream.yml.hbs diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new/agent/stream/stream.yml.hbs b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new/agent/stream/stream.yml.hbs new file mode 100644 index 0000000000000..2870385f21f95 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new/agent/stream/stream.yml.hbs @@ -0,0 +1 @@ +config.version: "2" diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new/fields/fields.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new/fields/fields.yml new file mode 100644 index 0000000000000..6e003ed0ad147 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new/fields/fields.yml @@ -0,0 +1,16 @@ +- name: data_stream.type + type: constant_keyword + description: > + Data stream type. +- name: data_stream.dataset + type: constant_keyword + description: > + Data stream dataset. +- name: data_stream.namespace + type: constant_keyword + description: > + Data stream namespace. +- name: '@timestamp' + type: date + description: > + Event timestamp. diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new/manifest.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new/manifest.yml new file mode 100644 index 0000000000000..42b5d8641c6cd --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new/manifest.yml @@ -0,0 +1,17 @@ +title: Test stream +type: logs +streams: + - input: test_input_new + vars: + - name: test_var_new + type: text + title: Test Var New + default: Test Var New + required: true + show_user: true + - name: test_var_new_2 + type: text + title: Test Var New 2 + default: Test Var New 2 + required: true + show_user: true diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new_2/agent/stream/stream.yml.hbs b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new_2/agent/stream/stream.yml.hbs new file mode 100644 index 0000000000000..2870385f21f95 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new_2/agent/stream/stream.yml.hbs @@ -0,0 +1 @@ +config.version: "2" diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new_2/fields/fields.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new_2/fields/fields.yml new file mode 100644 index 0000000000000..6e003ed0ad147 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new_2/fields/fields.yml @@ -0,0 +1,16 @@ +- name: data_stream.type + type: constant_keyword + description: > + Data stream type. +- name: data_stream.dataset + type: constant_keyword + description: > + Data stream dataset. +- name: data_stream.namespace + type: constant_keyword + description: > + Data stream namespace. +- name: '@timestamp' + type: date + description: > + Event timestamp. diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new_2/manifest.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new_2/manifest.yml new file mode 100644 index 0000000000000..af942e7b6413a --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/data_stream/test_stream_new_2/manifest.yml @@ -0,0 +1,17 @@ +title: Test stream +type: logs +streams: + - input: test_input_new_2 + vars: + - name: test_input_new_2_var_1 + type: text + title: Test Input New 2 Var 1 + default: Test Input New 2 Var 1 + required: true + show_user: true + - name: test_input_new_2_var_2 + type: text + title: Test Input New 2 Var 2 + default: Test Input New 2 Var 2 + required: true + show_user: true diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/docs/README.md b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/docs/README.md new file mode 100644 index 0000000000000..0b9b18421c9dc --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/docs/README.md @@ -0,0 +1,3 @@ +# Test package + +This is a test package for testing automated upgrades for package policies diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/manifest.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/manifest.yml new file mode 100644 index 0000000000000..6c26bb545424d --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.5.0-restructure-inputs/manifest.yml @@ -0,0 +1,27 @@ +format_version: 1.0.0 +name: package_policy_upgrade +title: Tests package policy upgrades +description: This is a test package for upgrading package policies +version: 0.5.0-restructure-inputs +categories: [] +release: beta +type: integration +license: basic +requirement: + elasticsearch: + versions: '>7.7.0' + kibana: + versions: '>7.7.0' +policy_templates: + - name: package_policy_upgrade + title: Package Policy Upgrade + description: Test Package for Upgrading Package Policies + inputs: + - type: test_input_new + title: Test Input New + description: Test Input New + enabled: true + - type: test_input_new_2 + title: Test Input New 2 + description: Test Input New 2 + enabled: true diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new/agent/stream/stream.yml.hbs b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new/agent/stream/stream.yml.hbs new file mode 100644 index 0000000000000..2870385f21f95 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new/agent/stream/stream.yml.hbs @@ -0,0 +1 @@ +config.version: "2" diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new/fields/fields.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new/fields/fields.yml new file mode 100644 index 0000000000000..6e003ed0ad147 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new/fields/fields.yml @@ -0,0 +1,16 @@ +- name: data_stream.type + type: constant_keyword + description: > + Data stream type. +- name: data_stream.dataset + type: constant_keyword + description: > + Data stream dataset. +- name: data_stream.namespace + type: constant_keyword + description: > + Data stream namespace. +- name: '@timestamp' + type: date + description: > + Event timestamp. diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new/manifest.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new/manifest.yml new file mode 100644 index 0000000000000..42b5d8641c6cd --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new/manifest.yml @@ -0,0 +1,17 @@ +title: Test stream +type: logs +streams: + - input: test_input_new + vars: + - name: test_var_new + type: text + title: Test Var New + default: Test Var New + required: true + show_user: true + - name: test_var_new_2 + type: text + title: Test Var New 2 + default: Test Var New 2 + required: true + show_user: true diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new_2/agent/stream/stream.yml.hbs b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new_2/agent/stream/stream.yml.hbs new file mode 100644 index 0000000000000..2870385f21f95 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new_2/agent/stream/stream.yml.hbs @@ -0,0 +1 @@ +config.version: "2" diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new_2/fields/fields.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new_2/fields/fields.yml new file mode 100644 index 0000000000000..6e003ed0ad147 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new_2/fields/fields.yml @@ -0,0 +1,16 @@ +- name: data_stream.type + type: constant_keyword + description: > + Data stream type. +- name: data_stream.dataset + type: constant_keyword + description: > + Data stream dataset. +- name: data_stream.namespace + type: constant_keyword + description: > + Data stream namespace. +- name: '@timestamp' + type: date + description: > + Event timestamp. diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new_2/manifest.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new_2/manifest.yml new file mode 100644 index 0000000000000..af942e7b6413a --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/data_stream/test_stream_new_2/manifest.yml @@ -0,0 +1,17 @@ +title: Test stream +type: logs +streams: + - input: test_input_new_2 + vars: + - name: test_input_new_2_var_1 + type: text + title: Test Input New 2 Var 1 + default: Test Input New 2 Var 1 + required: true + show_user: true + - name: test_input_new_2_var_2 + type: text + title: Test Input New 2 Var 2 + default: Test Input New 2 Var 2 + required: true + show_user: true diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/docs/README.md b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/docs/README.md new file mode 100644 index 0000000000000..0b9b18421c9dc --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/docs/README.md @@ -0,0 +1,3 @@ +# Test package + +This is a test package for testing automated upgrades for package policies diff --git a/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/manifest.yml b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/manifest.yml new file mode 100644 index 0000000000000..82c9d7059e2a6 --- /dev/null +++ b/x-pack/test/fleet_api_integration/apis/fixtures/test_packages/package_policy_upgrade/0.6.0-restructure-policy-templates/manifest.yml @@ -0,0 +1,27 @@ +format_version: 1.0.0 +name: package_policy_upgrade +title: Tests package policy upgrades +description: This is a test package for upgrading package policies +version: 0.6.0-restructure-policy-templates +categories: [] +release: beta +type: integration +license: basic +requirement: + elasticsearch: + versions: '>7.7.0' + kibana: + versions: '>7.7.0' +policy_templates: + - name: package_policy_upgrade_new + title: Package Policy Upgrade New + description: Test Package for Upgrading Package Policies + inputs: + - type: test_input_new + title: Test Input New + description: Test Input New + enabled: true + - type: test_input_new_2 + title: Test Input New 2 + description: Test Input New 2 + enabled: true diff --git a/x-pack/test/fleet_api_integration/apis/package_policy/upgrade.ts b/x-pack/test/fleet_api_integration/apis/package_policy/upgrade.ts index 3470da940f3c5..e75bcfaf75142 100644 --- a/x-pack/test/fleet_api_integration/apis/package_policy/upgrade.ts +++ b/x-pack/test/fleet_api_integration/apis/package_policy/upgrade.ts @@ -357,7 +357,7 @@ export default function (providerContext: FtrProviderContext) { }); describe('upgrade', function () { - it('successfully upgrade package policy', async function () { + it('successfully upgrades package policy', async function () { const { body }: { body: UpgradePackagePolicyResponse } = await supertest .post(`/api/fleet/package_policies/upgrade`) .set('kbn-xsrf', 'xxxx') @@ -573,6 +573,236 @@ export default function (providerContext: FtrProviderContext) { }); }); + describe('when upgrading to a version where inputs have been restructured', function () { + withTestPackageVersion('0.5.0-restructure-inputs'); + + beforeEach(async function () { + const { body: agentPolicyResponse } = await supertest + .post(`/api/fleet/agent_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'Test policy', + namespace: 'default', + }) + .expect(200); + + agentPolicyId = agentPolicyResponse.item.id; + + const { body: packagePolicyResponse } = await supertest + .post(`/api/fleet/package_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'package_policy_upgrade_1', + description: '', + namespace: 'default', + policy_id: agentPolicyId, + enabled: true, + output_id: '', + inputs: [ + { + policy_template: 'package_policy_upgrade', + type: 'test_input', + enabled: true, + streams: [ + { + id: 'test-package_policy_upgrade-xxxx', + enabled: true, + data_stream: { + type: 'test_stream', + dataset: 'package_policy_upgrade.test_stream', + }, + vars: { + test_var: { + value: 'Test value', + }, + }, + }, + ], + }, + ], + package: { + name: 'package_policy_upgrade', + title: 'This is a test package for upgrading package policies', + version: '0.2.0-add-non-required-test-var', + }, + }); + + packagePolicyId = packagePolicyResponse.item.id; + }); + + afterEach(async function () { + await supertest + .post(`/api/fleet/package_policies/delete`) + .set('kbn-xsrf', 'xxxx') + .send({ packagePolicyIds: [packagePolicyId] }) + .expect(200); + + await supertest + .post('/api/fleet/agent_policies/delete') + .set('kbn-xsrf', 'xxxx') + .send({ agentPolicyId }) + .expect(200); + }); + + describe('dry run', function () { + it('returns a valid diff', async function () { + const { body }: { body: UpgradePackagePolicyDryRunResponse } = await supertest + .post(`/api/fleet/package_policies/upgrade`) + .set('kbn-xsrf', 'xxxx') + .send({ + packagePolicyIds: [packagePolicyId], + dryRun: true, + }) + .expect(200); + + expect(body[0].hasErrors).to.be(false); + }); + }); + + describe('upgrade', function () { + it('successfully upgrades package policy', async function () { + const { body }: { body: UpgradePackagePolicyResponse } = await supertest + .post(`/api/fleet/package_policies/upgrade`) + .set('kbn-xsrf', 'xxxx') + .send({ + packagePolicyIds: [packagePolicyId], + dryRun: false, + }) + .expect(200); + + expect(body[0].success).to.be(true); + }); + }); + }); + + describe('when upgrading to a version where policy templates have been restructured', function () { + withTestPackageVersion('0.6.0-restructure-policy-templates'); + + beforeEach(async function () { + const { body: agentPolicyResponse } = await supertest + .post(`/api/fleet/agent_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'Test policy', + namespace: 'default', + }) + .expect(200); + + agentPolicyId = agentPolicyResponse.item.id; + + const { body: packagePolicyResponse } = await supertest + .post(`/api/fleet/package_policies`) + .set('kbn-xsrf', 'xxxx') + .send({ + name: 'package_policy_upgrade_1', + description: '', + namespace: 'default', + policy_id: agentPolicyId, + enabled: true, + output_id: '', + inputs: [ + { + policy_template: 'package_policy_upgrade', + type: 'test_input_new', + enabled: true, + streams: [ + { + id: 'test-package_policy_upgrade-xxxx', + enabled: true, + data_stream: { + type: 'test_stream_new', + dataset: 'package_policy_upgrade.test_stream_new', + }, + vars: { + test_var_new: { + value: 'Test value 1', + }, + test_var_new_2: { + value: 'Test value 2', + }, + }, + }, + ], + }, + { + policy_template: 'package_policy_upgrade', + type: 'test_input_new_2', + enabled: true, + streams: [ + { + id: 'test-package_policy_upgrade-xxxx', + enabled: true, + data_stream: { + type: 'test_stream_new_2', + dataset: 'package_policy_upgrade.test_stream_new_2', + }, + vars: { + test_var_new_2_var_1: { + value: 'Test value 1', + }, + test_var_new_2_var_2: { + value: 'Test value 2', + }, + }, + }, + ], + }, + ], + package: { + name: 'package_policy_upgrade', + title: 'This is a test package for upgrading package policies', + version: '0.5.0-restructure-inputs', + }, + }); + + packagePolicyId = packagePolicyResponse.item.id; + }); + + afterEach(async function () { + await supertest + .post(`/api/fleet/package_policies/delete`) + .set('kbn-xsrf', 'xxxx') + .send({ packagePolicyIds: [packagePolicyId] }) + .expect(200); + + await supertest + .post('/api/fleet/agent_policies/delete') + .set('kbn-xsrf', 'xxxx') + .send({ agentPolicyId }) + .expect(200); + }); + + describe('dry run', function () { + it('returns a valid diff', async function () { + const { body }: { body: UpgradePackagePolicyDryRunResponse } = await supertest + .post(`/api/fleet/package_policies/upgrade`) + .set('kbn-xsrf', 'xxxx') + .send({ + packagePolicyIds: [packagePolicyId], + dryRun: true, + }) + .expect(200); + + expect(body[0].hasErrors).to.be(false); + }); + }); + + describe('upgrade', function () { + it('successfully upgrades package policy', async function () { + const { body }: { body: UpgradePackagePolicyResponse } = await supertest + .post(`/api/fleet/package_policies/upgrade`) + .set('kbn-xsrf', 'xxxx') + .send({ + packagePolicyIds: [packagePolicyId], + dryRun: false, + }) + .expect(200); + + expect(body[0].success).to.be(true); + }); + }); + }); + describe('when package policy is not found', function () { it('should return an 200 with errors when "dryRun:true" is provided', async function () { const { body }: { body: UpgradePackagePolicyDryRunResponse } = await supertest