mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 08:58:41 +00:00
Make fleetctl apply -f fail with unknown kind: config fields (#3026)
* Make fleetctl apply fail with unknown fields * Add unit test
This commit is contained in:
parent
cca1e2e043
commit
59e01fbe08
3 changed files with 45 additions and 4 deletions
1
changes/fleetctl-apply-to-fail-unknown-fields
Normal file
1
changes/fleetctl-apply-to-fail-unknown-fields
Normal file
|
|
@ -0,0 +1 @@
|
|||
* Make `fleetctl apply` fail if there's an unknown field.
|
||||
|
|
@ -234,3 +234,42 @@ spec:
|
|||
assert.True(t, savedAppConfig.HostSettings.EnableHostUsers)
|
||||
assert.True(t, savedAppConfig.HostSettings.EnableSoftwareInventory)
|
||||
}
|
||||
|
||||
func TestApplyAppConfigUnknownFields(t *testing.T) {
|
||||
_, ds := runServerWithMockedDS(t)
|
||||
|
||||
ds.ListUsersFunc = func(ctx context.Context, opt fleet.UserListOptions) ([]*fleet.User, error) {
|
||||
return userRoleSpecList, nil
|
||||
}
|
||||
|
||||
ds.UserByEmailFunc = func(ctx context.Context, email string) (*fleet.User, error) {
|
||||
if email == "admin1@example.com" {
|
||||
return userRoleSpecList[0], nil
|
||||
}
|
||||
return userRoleSpecList[1], nil
|
||||
}
|
||||
|
||||
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
||||
return &fleet.AppConfig{}, nil
|
||||
}
|
||||
|
||||
var savedAppConfig *fleet.AppConfig
|
||||
ds.SaveAppConfigFunc = func(ctx context.Context, config *fleet.AppConfig) error {
|
||||
savedAppConfig = config
|
||||
return nil
|
||||
}
|
||||
|
||||
name := writeTmpYml(t, `---
|
||||
apiVersion: v1
|
||||
kind: config
|
||||
spec:
|
||||
host_settings:
|
||||
enabled_software_inventory: false # typo, correct config is enable_software_inventory
|
||||
`)
|
||||
|
||||
runAppCheckErr(t, []string{"apply", "-f", name},
|
||||
"applying fleet config: apply config received status 400 Bad request: "+
|
||||
"json: unknown field \"enabled_software_inventory\"",
|
||||
)
|
||||
require.Nil(t, savedAppConfig)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
|
@ -88,7 +89,6 @@ func (svc *Service) sendTestEmail(ctx context.Context, config *fleet.AppConfig)
|
|||
return mailError{message: err.Error()}
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (svc *Service) ModifyAppConfig(ctx context.Context, p []byte) (*fleet.AppConfig, error) {
|
||||
|
|
@ -102,9 +102,10 @@ func (svc *Service) ModifyAppConfig(ctx context.Context, p []byte) (*fleet.AppCo
|
|||
}
|
||||
|
||||
// We apply the config that is incoming to the old one
|
||||
err = json.Unmarshal(p, &appConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
decoder := json.NewDecoder(bytes.NewReader(p))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(&appConfig); err != nil {
|
||||
return nil, &badRequestError{message: err.Error()}
|
||||
}
|
||||
|
||||
if appConfig.SMTPSettings.SMTPEnabled || appConfig.SMTPSettings.SMTPConfigured {
|
||||
|
|
|
|||
Loading…
Reference in a new issue