Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly fill plugins with arbitrary map fields #258

Merged
merged 3 commits into from
Jan 12, 2023
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Table of Contents

- [v0.34.1](#v0341)
- [v0.34.0](#v0340)
- [v0.33.0](#v0330)
- [v0.32.0](#v0320)
Expand Down Expand Up @@ -42,6 +43,13 @@
- [0.2.0](#020)
- [0.1.0](#010)

## [v0.34.1]

> Release date: 2022/12/22

- Fix ingestion of entity defaults with arbitray map values
[#258](https://github.com/Kong/go-kong/pull/258)

## [v0.34.0]

> Release date: 2022/12/19
Expand Down Expand Up @@ -639,6 +647,7 @@ authentication credentials in Kong.
releases of Kong since every release of Kong is introducing breaking changes
to the Admin API.

[v0.34.1]: https://github.com/Kong/go-kong/compare/v0.34.0...v0.34.1
[v0.34.0]: https://github.com/Kong/go-kong/compare/v0.33.0...v0.34.0
[v0.33.0]: https://github.com/Kong/go-kong/compare/v0.32.0...v0.33.0
[v0.32.0]: https://github.com/Kong/go-kong/compare/v0.31.1...v0.32.0
Expand Down
33 changes: 33 additions & 0 deletions kong/plugin_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ func TestFillPluginDefaults(T *testing.T) {
// TODO https://github.com/Kong/go-kong/issues/214 this should only skip Enterprise 3.x (with a separate test)
// not all Enterprise versions.
SkipWhenEnterprise(T)
RunWhenKong(T, ">=2.3.0")
assert := assert.New(T)

client, err := NewTestClient(nil, nil)
Expand Down Expand Up @@ -607,6 +608,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