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 5107cbf8121d7..67df65b2f12bf 100644 --- a/x-pack/plugins/fleet/common/services/validate_package_policy.ts +++ b/x-pack/plugins/fleet/common/services/validate_package_policy.ts @@ -75,7 +75,7 @@ export const validatePackagePolicy = ( const packageVars = Object.entries(packagePolicy.vars || {}); if (packageVars.length) { validationResults.vars = packageVars.reduce((results, [name, varEntry]) => { - results[name] = validatePackagePolicyConfig(varEntry, packageVarsByName[name]); + results[name] = validatePackagePolicyConfig(varEntry, packageVarsByName[name], name); return results; }, {} as ValidationEntry); } @@ -138,7 +138,8 @@ export const validatePackagePolicy = ( results[name] = input.enabled ? validatePackagePolicyConfig( configEntry, - inputVarDefsByPolicyTemplateAndType[inputKey][name] + inputVarDefsByPolicyTemplateAndType[inputKey][name], + name ) : null; return results; @@ -161,7 +162,7 @@ export const validatePackagePolicy = ( (results, [name, configEntry]) => { results[name] = streamVarDefs && streamVarDefs[name] && input.enabled && stream.enabled - ? validatePackagePolicyConfig(configEntry, streamVarDefs[name]) + ? validatePackagePolicyConfig(configEntry, streamVarDefs[name], name) : null; return results; }, @@ -183,12 +184,14 @@ export const validatePackagePolicy = ( if (Object.entries(validationResults.inputs!).length === 0) { validationResults.inputs = null; } + return validationResults; }; export const validatePackagePolicyConfig = ( configEntry: PackagePolicyConfigRecordEntry, - varDef: RegistryVarsEntry + varDef: RegistryVarsEntry, + varName: string ): string[] | null => { const errors = []; const { value } = configEntry; @@ -198,6 +201,13 @@ export const validatePackagePolicyConfig = ( parsedValue = value.trim(); } + if (varDef === undefined) { + // eslint-disable-next-line no-console + console.debug(`No variable definition for ${varName} found`); + + return null; + } + if (varDef.required) { if (parsedValue === undefined || (typeof parsedValue === 'string' && !parsedValue)) { errors.push( diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/services/has_invalid_but_required_var.ts b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/services/has_invalid_but_required_var.ts index e204d86b51511..bf75b05f41b8d 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/services/has_invalid_but_required_var.ts +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/create_package_policy_page/services/has_invalid_but_required_var.ts @@ -22,7 +22,11 @@ export const hasInvalidButRequiredVar = ( registryVar.required && (!packagePolicyVars || !packagePolicyVars[registryVar.name] || - validatePackagePolicyConfig(packagePolicyVars[registryVar.name], registryVar)?.length) + validatePackagePolicyConfig( + packagePolicyVars[registryVar.name], + registryVar, + registryVar.name + )?.length) ) ) ); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx index 67cff3e2d0304..f083c50a71d16 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/agent_policy/edit_package_policy_page/index.tsx @@ -658,7 +658,7 @@ const UpgradeStatusCallout: React.FunctionComponent<{ )} - {isReadyForUpgrade ? ( + {isReadyForUpgrade && currentPackagePolicy ? (