Skip to content

Commit

Permalink
fix: correctly fill plugins with arbitrary map fields
Browse files Browse the repository at this point in the history
Kong plugin schemas support different type of fields,
including maps. Moreover, these can either be arbitrary
maps, or maps with their own subschema.

go-kong already supports well the second case, but it
falls short with arbitrary maps. An example of arbitrary
map field is the `custom_fields_by_lua` from the `http-log`
plugin, which is defined as follows:

```
{
  "custom_fields_by_lua": {
    "keys": {
      "len_min": 1,
      "type": "string"
    },
    "type": "map",
    "values": {
      "len_min": 1,
      "type": "string"
    }
  }
}
```

This commit makes sure that go-kong is able to correctly
handle such schemas as well.
  • Loading branch information
GGabriele committed Dec 22, 2022
1 parent 4f1666e commit 3e42cb8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
32 changes: 32 additions & 0 deletions kong/plugin_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,38 @@ func TestFillPluginDefaults(T *testing.T) {
Enabled: Bool(false),
},
},
{
name: "nested config with arbitrary map field",
plugin: &Plugin{
Name: String("http-log"),
Config: Configuration{
"custom_fields_by_lua": map[string]interface{}{
"foo": "bar",
},
},
Enabled: Bool(false),
Protocols: []*string{String("grpc"), String("grpcs")},
},
expected: &Plugin{
Name: String("http-log"),
Config: Configuration{
"content_type": string("application/json"),
"custom_fields_by_lua": map[string]interface{}{
"foo": "bar",
},
"flush_timeout": float64(2),
"headers": nil,
"http_endpoint": nil,
"keepalive": float64(60000),
"method": string("POST"),
"queue_size": float64(1),
"retry_count": float64(10),
"timeout": float64(10000),
},
Enabled: Bool(false),
Protocols: []*string{String("grpc"), String("grpcs")},
},
},
}

for _, tc := range tests {
Expand Down
16 changes: 14 additions & 2 deletions kong/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,24 @@ func fillConfigRecord(schema gjson.Result, config Configuration) Configuration {
}

// check if key is already set in the config
if _, ok := config[fname]; ok {
if v, ok := config[fname]; ok {
// the field is set. If it's not a map, then
// the field is fully set. If it's a map, we
// need to make sure that all fields are properly
// filled.
if _, ok := config[fname].(map[string]interface{}); !ok {
//
// some fields are defined as arbitrary maps,
// containing a 'keys' and 'values' subfields.
// in this case, the map is already fully set.
switch v.(type) {
case map[string]interface{}:
keys := value.Get(fname + ".keys")
values := value.Get(fname + ".values")
if keys.Exists() && values.Exists() {
// an arbitrary map, field is already set.
return true
}
default:
// not a map, field is already set.
return true
}
Expand Down

0 comments on commit 3e42cb8

Please sign in to comment.