Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions changes/54690-validate-custom-settings-types
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed `fleetctl apply` silently dropping incorrectly typed paths and labels in fleet custom profile settings. Invalid values now return a validation error.
50 changes: 50 additions & 0 deletions cmd/fleetctl/fleetctl/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4204,6 +4204,56 @@ spec:
`,
wantErr: `400 Bad Request: invalid value type at 'macos_settings.enable_disk_encryption': expected bool but got float64`,
},
{
desc: "team custom_settings invalid path",
spec: `
apiVersion: v1
kind: fleet
spec:
team:
name: team1
mdm:
apple_settings:
custom_settings:
- path: 123
`,
wantOutput: "[!] In fleet: `custom_settings` is deprecated, please use `configuration_profiles` instead.",
wantErr: `400 Bad Request: invalid value type at 'macos_settings.custom_settings.path': expected string but got float64`,
},
{
desc: "team custom_settings invalid labels",
spec: `
apiVersion: v1
kind: fleet
spec:
team:
name: team1
mdm:
apple_settings:
custom_settings:
- path: a
labels: Engineering
`,
wantOutput: "[!] In fleet: `custom_settings` is deprecated, please use `configuration_profiles` instead.",
wantErr: `400 Bad Request: invalid value type at 'macos_settings.custom_settings.labels': expected array of strings but got string`,
},
{
desc: "team custom_settings invalid label element",
spec: `
apiVersion: v1
kind: fleet
spec:
team:
name: team1
mdm:
apple_settings:
custom_settings:
- path: a
labels_include_any: [1, Engineering]
`,
wantOutput: "[!] In fleet: `custom_settings` is deprecated, please use `configuration_profiles` instead.",
wantErr: `400 Bad Request: invalid value type at 'macos_settings.custom_settings.labels_include_any': expected string but got float64`,
},
{
desc: "team config macos_settings.enable_disk_encryption true",
spec: `
Expand Down
53 changes: 37 additions & 16 deletions server/fleet/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,20 +897,34 @@ var _ WithMDMProfileSpecs = MacOSSettings{}
func (s *MacOSSettings) FromMap(m map[string]interface{}) (map[string]bool, error) {
set := make(map[string]bool)

extractLabelField := func(parentMap map[string]interface{}, fieldName string) []string {
extractLabelField := func(parentMap map[string]any, fieldName string) ([]string, error) {
v, ok := parentMap[fieldName]
if !ok || v == nil {
return nil, nil
}
labels, ok := v.([]any)
if !ok {
return nil, &json.UnmarshalTypeError{
Value: fmt.Sprintf("%T", v),
Type: reflect.TypeFor[[]string](),
Field: "macos_settings.custom_settings." + fieldName,
}
}
var ret []string
if labels, ok := parentMap[fieldName].([]interface{}); ok {
for _, label := range labels {
if strLabel, ok := label.(string); ok {
ret = append(ret, strLabel)
for _, label := range labels {
strLabel, ok := label.(string)
if !ok {
return nil, &json.UnmarshalTypeError{
Value: fmt.Sprintf("%T", label),
Type: reflect.TypeFor[string](),
Field: "macos_settings.custom_settings." + fieldName,
}
}
ret = append(ret, strLabel)
}
return ret
return ret, nil
}

// unlike a bad label, a wrong type here is an error: dropping it would
// clear the stored name or description
extractStringField := func(parentMap map[string]any, fieldName string) (string, error) {
v, ok := parentMap[fieldName]
if !ok || v == nil {
Expand All @@ -936,22 +950,29 @@ func (s *MacOSSettings) FromMap(m map[string]interface{}) (map[string]bool, erro
for _, v := range vals {
if m, ok := v.(map[string]interface{}); ok {
var spec MDMProfileSpec
// extract the Path field
if path, ok := m["path"].(string); ok {
spec.Path = path
}
var err error
if spec.Path, err = extractStringField(m, "path"); err != nil {
return nil, err
}
if spec.Name, err = extractStringField(m, "name"); err != nil {
return nil, err
}
if spec.Description, err = extractStringField(m, "description"); err != nil {
return nil, err
}

spec.Labels = extractLabelField(m, "labels")
spec.LabelsIncludeAll = extractLabelField(m, "labels_include_all")
spec.LabelsExcludeAny = extractLabelField(m, "labels_exclude_any")
spec.LabelsIncludeAny = extractLabelField(m, "labels_include_any")
if spec.Labels, err = extractLabelField(m, "labels"); err != nil {
return nil, err
}
if spec.LabelsIncludeAll, err = extractLabelField(m, "labels_include_all"); err != nil {
return nil, err
}
if spec.LabelsExcludeAny, err = extractLabelField(m, "labels_exclude_any"); err != nil {
return nil, err
}
if spec.LabelsIncludeAny, err = extractLabelField(m, "labels_include_any"); err != nil {
return nil, err
}

csSpecs = append(csSpecs, spec)
} else if m, ok := v.(string); ok { // for backwards compatibility with the old way to define profiles
Expand Down
44 changes: 42 additions & 2 deletions server/fleet/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1392,21 +1392,61 @@ func TestMacOSSettingsFromMapCustomSettings(t *testing.T) {

// null is the same as omitted, but any other non-string is rejected
require.NoError(t, json.Unmarshal([]byte(`{"custom_settings": [
{"path": "a", "name": null, "description": null}
{"path": "a", "name": null, "description": null},
{"path": null},
{}
]}`), &raw))
_, err = s.FromMap(raw)
require.NoError(t, err)
require.Equal(t, []MDMProfileSpec{{Path: "a"}}, s.CustomSettings)
require.Equal(t, []MDMProfileSpec{{Path: "a"}, {}, {}}, s.CustomSettings)

for _, tc := range []struct{ field, value string }{
{"description", `123`},
{"description", `{"a": "b"}`},
{"name", `true`},
{"name", `["x"]`},
{"path", `123`},
{"path", `true`},
{"path", `["x"]`},
} {
require.NoError(t, json.Unmarshal([]byte(`{"custom_settings": [{"path": "a", "`+
tc.field+`": `+tc.value+`}]}`), &raw))
_, err = s.FromMap(raw)
require.ErrorContains(t, err, "macos_settings.custom_settings."+tc.field+" of type string", tc.value)
}
}

func TestMacOSSettingsFromMapCustomSettingsLabels(t *testing.T) {
for _, field := range []string{"labels", "labels_include_all", "labels_include_any", "labels_exclude_any"} {
t.Run(field, func(t *testing.T) {
for _, value := range []string{`"Engineering"`, `123`, `{}`, `[1, "Engineering"]`, `["Engineering", false]`, `[null]`} {
t.Run(value, func(t *testing.T) {
var raw map[string]any
require.NoError(t, json.Unmarshal([]byte(`{"custom_settings": [{"path": "a", "`+field+`": `+value+`}]}`), &raw))
s := MacOSSettings{CustomSettings: []MDMProfileSpec{{Path: "existing"}}}
_, err := s.FromMap(raw)
var typeErr *json.UnmarshalTypeError
require.ErrorAs(t, err, &typeErr)
require.Equal(t, "macos_settings.custom_settings."+field, typeErr.Field)
require.Equal(t, []MDMProfileSpec{{Path: "existing"}}, s.CustomSettings)
})
}
for _, value := range []string{`null`, `[]`, `["Engineering", "Sales"]`} {
t.Run(value, func(t *testing.T) {
input := `{"custom_settings": [{"path": "a", "` + field + `": ` + value + `}]}`
var raw map[string]any
require.NoError(t, json.Unmarshal([]byte(input), &raw))
var want, got MacOSSettings
require.NoError(t, json.Unmarshal([]byte(input), &want))
if value == `[]` {
want.CustomSettings = []MDMProfileSpec{{Path: "a"}}
}
_, err := got.FromMap(raw)
require.NoError(t, err)
require.Len(t, got.CustomSettings, 1)
require.Equal(t, want.CustomSettings, got.CustomSettings)
})
}
})
}
}
Loading