-
Couldn't load subscription status.
- Fork 41
Description
When we try to execute a plugin schema validation using kongClient.Plugins.Validate(ctx, plugin) but Kong, for some reason, is unreachable (incorrect host, port...) go-kong is getting a nil pointer deference panic the application.
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x64144e]
The equivalent curl is this:
curl --location --request POST 'http://localhost:8001/schemas/plugins/validate' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "correlation-id",
"config": {
}
}'
I created a sample project to reproduce the problem here. I used go1.17 and go-kong version v0.28.1.
Doing some research, I think I found where go-kong is getting this nil deference error. In plugin_service.go, we have this snippet:
...
resp, err: = s.client.Do(ctx, req, nil)
if err != nil {
// Arguably Kong should return a 422 Unprocessable Entity for a well-formed
// HTTP request with a mangled plugin, but it doesn't, it returns a 400.
// Hopefully (usually) we get a 400 because of a mangled plugin rather than
// a mangled request, but we can't easily tell as messageFromBody masks errors
if resp.StatusCode == http.StatusBadRequest {
var apiError * APIError
ok: = errors.As(err, & apiError)
if !ok {
return false, "", err
}
return false, apiError.message, nil
}
return false, "", err
...If kong is not reachable, the resp var in resp, err: = s.client.Do(ctx, req, nil) is nil and the err is not nil. However, even with resp == nil, go-kong is checking the status code here: if resp.StatusCode == http.StatusBadRequest { causing the nil deference error. I don't know if there's more parts that could have this kind of error in go-kong. I only have used the plugin validation until now.