Browse Source

api: return 400 in case of non-existent config fields (#2425)

pull/2426/head
Alessandro Ros 3 years ago committed by GitHub
parent
commit
61b77a3ff9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      internal/conf/conf.go
  2. 4
      internal/conf/path.go
  3. 8
      internal/core/api.go
  4. 46
      internal/core/api_test.go

4
internal/conf/conf.go

@ -335,7 +335,9 @@ func (conf *Conf) Check() error {
return nil return nil
} }
// UnmarshalJSON implements json.Unmarshaler. It is used to set default values. // UnmarshalJSON implements json.Unmarshaler. It is used to:
// - force DisallowUnknownFields
// - set default values
func (conf *Conf) UnmarshalJSON(b []byte) error { func (conf *Conf) UnmarshalJSON(b []byte) error {
// general // general
conf.LogLevel = LogLevel(logger.Info) conf.LogLevel = LogLevel(logger.Info)

4
internal/conf/path.go

@ -447,7 +447,9 @@ func (pconf PathConf) HasOnDemandPublisher() bool {
return pconf.RunOnDemand != "" return pconf.RunOnDemand != ""
} }
// UnmarshalJSON implements json.Unmarshaler. It is used to set default values. // UnmarshalJSON implements json.Unmarshaler. It is used to:
// - force DisallowUnknownFields
// - set default values
func (pconf *PathConf) UnmarshalJSON(b []byte) error { func (pconf *PathConf) UnmarshalJSON(b []byte) error {
// General // General
pconf.Source = "publisher" pconf.Source = "publisher"

8
internal/core/api.go

@ -63,7 +63,9 @@ func generateStructWithOptionalFields(model interface{}) interface{} {
func loadConfData(ctx *gin.Context) (interface{}, error) { func loadConfData(ctx *gin.Context) (interface{}, error) {
in := generateStructWithOptionalFields(conf.Conf{}) in := generateStructWithOptionalFields(conf.Conf{})
err := json.NewDecoder(ctx.Request.Body).Decode(in) d := json.NewDecoder(ctx.Request.Body)
d.DisallowUnknownFields()
err := d.Decode(in)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -73,7 +75,9 @@ func loadConfData(ctx *gin.Context) (interface{}, error) {
func loadConfPathData(ctx *gin.Context) (interface{}, error) { func loadConfPathData(ctx *gin.Context) (interface{}, error) {
in := generateStructWithOptionalFields(conf.PathConf{}) in := generateStructWithOptionalFields(conf.PathConf{})
err := json.NewDecoder(ctx.Request.Body).Decode(in) d := json.NewDecoder(ctx.Request.Body)
d.DisallowUnknownFields()
err := d.Decode(in)
if err != nil { if err != nil {
return nil, err return nil, err
} }

46
internal/core/api_test.go

@ -155,6 +155,29 @@ func TestAPIConfigSet(t *testing.T) {
require.Equal(t, []interface{}{"tcp"}, out["protocols"]) require.Equal(t, []interface{}{"tcp"}, out["protocols"])
} }
func TestAPIConfigSetUnknownField(t *testing.T) {
p, ok := newInstance("api: yes\n")
require.Equal(t, true, ok)
defer p.Close()
b := map[string]interface{}{
"test": "asd",
}
byts, err := json.Marshal(b)
require.NoError(t, err)
hc := &http.Client{Transport: &http.Transport{}}
req, err := http.NewRequest("POST", "http://localhost:9997/v2/config/set", bytes.NewReader(byts))
require.NoError(t, err)
res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, http.StatusBadRequest, res.StatusCode)
}
func TestAPIConfigPathsAdd(t *testing.T) { func TestAPIConfigPathsAdd(t *testing.T) {
p, ok := newInstance("api: yes\n") p, ok := newInstance("api: yes\n")
require.Equal(t, true, ok) require.Equal(t, true, ok)
@ -179,6 +202,29 @@ func TestAPIConfigPathsAdd(t *testing.T) {
require.Equal(t, "10s", out.Paths["my/path"].SourceOnDemandStartTimeout) require.Equal(t, "10s", out.Paths["my/path"].SourceOnDemandStartTimeout)
} }
func TestAPIConfigPathsAddUnknownField(t *testing.T) {
p, ok := newInstance("api: yes\n")
require.Equal(t, true, ok)
defer p.Close()
b := map[string]interface{}{
"test": "asd",
}
byts, err := json.Marshal(b)
require.NoError(t, err)
hc := &http.Client{Transport: &http.Transport{}}
req, err := http.NewRequest("POST", "http://localhost:9997/v2/config/paths/add/my/path", bytes.NewReader(byts))
require.NoError(t, err)
res, err := hc.Do(req)
require.NoError(t, err)
defer res.Body.Close()
require.Equal(t, http.StatusBadRequest, res.StatusCode)
}
func TestAPIConfigPathsEdit(t *testing.T) { func TestAPIConfigPathsEdit(t *testing.T) {
p, ok := newInstance("api: yes\n") p, ok := newInstance("api: yes\n")
require.Equal(t, true, ok) require.Equal(t, true, ok)

Loading…
Cancel
Save