Skip to content

Commit b17eca9

Browse files
committed
fix: omit unset variables without defaults from Fleet API requests
Previously, elastic-package would send null values for variables without defaults, causing Fleet API validation errors for non-required URL-type variables. This change modifies the variable handling to skip unset variables that have no default values instead of including them as nulls. Fixes #2979
1 parent f5ad3db commit b17eca9

File tree

4 files changed

+32
-17
lines changed

4 files changed

+32
-17
lines changed

internal/packages/archetype/data_stream_inputs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func unpackVars(output *[]packages.Variable, input []InputVariable) {
8484
newVar.MaxDuration = inputVar.MaxDuration
8585
newVar.Description = inputVar.Description
8686
if inputVar.Default != nil {
87+
newVar.Default = &packages.VarValue{}
8788
newVar.Default.Unpack(inputVar.Default)
8889
}
8990
*output = append(*output, newVar)

internal/packages/packages.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,19 @@ func VarValueYamlString(vv VarValue, field string, numSpaces ...int) string {
102102

103103
// Variable is an instance of configuration variable (named, typed).
104104
type Variable struct {
105-
Name string `config:"name" json:"name" yaml:"name"`
106-
Type string `config:"type" json:"type" yaml:"type"`
107-
Title string `config:"title" json:"title" yaml:"title"`
108-
Description string `config:"description" json:"description" yaml:"description"`
109-
Multi bool `config:"multi" json:"multi" yaml:"multi"`
110-
Required bool `config:"required" json:"required" yaml:"required"`
111-
Secret bool `config:"secret" json:"secret" yaml:"secret"`
112-
ShowUser bool `config:"show_user" json:"show_user" yaml:"show_user"`
113-
HideInDeploymentModes []string `config:"hide_in_deployment_modes" json:"hide_in_deployment_modes" yaml:"hide_in_deployment_modes"`
114-
UrlAllowedSchemes []string `config:"url_allowed_schemes" json:"url_allowed_schemes" yaml:"url_allowed_schemes"`
115-
MinDuration string `config:"min_duration" json:"min_duration" yaml:"min_duration"`
116-
MaxDuration string `config:"max_duration" json:"max_duration" yaml:"max_duration"`
117-
Default VarValue `config:"default" json:"default" yaml:"default"`
105+
Name string `config:"name" json:"name" yaml:"name"`
106+
Type string `config:"type" json:"type" yaml:"type"`
107+
Title string `config:"title" json:"title" yaml:"title"`
108+
Description string `config:"description" json:"description" yaml:"description"`
109+
Multi bool `config:"multi" json:"multi" yaml:"multi"`
110+
Required bool `config:"required" json:"required" yaml:"required"`
111+
Secret bool `config:"secret" json:"secret" yaml:"secret"`
112+
ShowUser bool `config:"show_user" json:"show_user" yaml:"show_user"`
113+
HideInDeploymentModes []string `config:"hide_in_deployment_modes" json:"hide_in_deployment_modes" yaml:"hide_in_deployment_modes"`
114+
UrlAllowedSchemes []string `config:"url_allowed_schemes" json:"url_allowed_schemes" yaml:"url_allowed_schemes"`
115+
MinDuration string `config:"min_duration" json:"min_duration" yaml:"min_duration"`
116+
MaxDuration string `config:"max_duration" json:"max_duration" yaml:"max_duration"`
117+
Default *VarValue `config:"default" json:"default" yaml:"default"`
118118
}
119119

120120
// Input is a single input configuration.

internal/resources/fleetpolicy.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,24 @@ func createInputPackagePolicy(policy FleetAgentPolicy, manifest packages.Package
312312
func setKibanaVariables(definitions []packages.Variable, values common.MapStr) kibana.Vars {
313313
vars := kibana.Vars{}
314314
for _, definition := range definitions {
315+
// Elastic Package uses the deprecated 'inputs' array in its /api/fleet/package_policies request.
316+
// When using this API parameter, default values are not automatically incorporated into
317+
// the policy, whereas with the 'inputs' object, defaults are incorporated by the API service.
318+
// This means that our client must include the default values in its request to ensure correct behavior.
315319
val := definition.Default
316320

317321
value, err := values.GetValue(definition.Name)
318322
if err == nil {
319-
val = packages.VarValue{}
323+
val = &packages.VarValue{}
320324
val.Unpack(value)
325+
} else if errors.Is(err, common.ErrKeyNotFound) && definition.Default == nil {
326+
// Do not include nulls for unset variables.
327+
continue
321328
}
322329

323330
vars[definition.Name] = kibana.Var{
324331
Type: definition.Type,
325-
Value: val,
332+
Value: *val,
326333
}
327334
}
328335
return vars

internal/testrunner/runners/system/tester.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,17 +2042,24 @@ func createInputPackageDatastream(
20422042
func setKibanaVariables(definitions []packages.Variable, values common.MapStr) kibana.Vars {
20432043
vars := kibana.Vars{}
20442044
for _, definition := range definitions {
2045+
// Elastic Package uses the deprecated 'inputs' array in its /api/fleet/package_policies request.
2046+
// When using this API parameter, default values are not automatically incorporated into
2047+
// the policy, whereas with the 'inputs' object, defaults are incorporated by the API service.
2048+
// This means that our client must include the default values in its request to ensure correct behavior.
20452049
val := definition.Default
20462050

20472051
value, err := values.GetValue(definition.Name)
20482052
if err == nil {
2049-
val = packages.VarValue{}
2053+
val = &packages.VarValue{}
20502054
val.Unpack(value)
2055+
} else if errors.Is(err, common.ErrKeyNotFound) && definition.Default == nil {
2056+
// Do not include nulls for unset variables.
2057+
continue
20512058
}
20522059

20532060
vars[definition.Name] = kibana.Var{
20542061
Type: definition.Type,
2055-
Value: val,
2062+
Value: *val,
20562063
}
20572064
}
20582065
return vars

0 commit comments

Comments
 (0)