Reputation: 1
I am trying to build http plugin by using go lang to add custom logic in KrakneD. But Currently I am getting 500 Internal server error from KrakenD and 401 unauthorized error in backend. When I debugged more, then I could see bearer token is not getting passed to backend.
KrakenD Backend Config:
"backend": [
{
"url_pattern": "My downstream Path",
"method": "Http Method",
"host": [
"My Host"
],
"extra_config": {
"github.com/devopsfaith/krakend/transport/http/client/executor": {
"name": "Plugin Register Name"
},
"github.com/devopsfaith/krakend-oauth2-clientcredentials": {
"endpoint_params": {},
"token_url": "My Token URL",
"client_id": "My Client ID",
"client_secret": "My Client Secret"
}
},
"disable_host_sanitize": false
}
]
Go Lang Plugin
func (r registerer) registerClients(ctx context.Context, extra map[string]interface{}) (http.Handler, error) {
name, ok := extra["name"].(string)
if !ok {
return nil, errors.New("wrong config")
}
if name != string(r) {
return nil, fmt.Errorf("unknown register %s", name)
}
// return the actual handler wrapping or your custom logic so it can be used as a replacement for the default http client
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fmt.Println(req.Header.Get("Authorization")) // Bearer token is coming empty. I am expecting bearer token value here, which was configured in KrakenD
client := &http.Client{
Timeout: time.Second * 10,
}
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
w.Write(body)
}), nil
}
Upvotes: 0
Views: 724
Reputation: 89
Your backend doesn't see Bearer token, because krakend by default doesn't forward this header. You must set input_headers
field to your krakend config. Check link: https://www.krakend.io/docs/endpoints/parameter-forwarding/#headers-forwarding
Your config must be:
"input_headers": [
"Authorization"
],
"backend": [
{
"url_pattern": "My downstream Path",
"method": "Http Method",
"host": [
"My Host"
],
"extra_config": {
"github.com/devopsfaith/krakend/transport/http/client/executor": {
"name": "Plugin Register Name"
},
"github.com/devopsfaith/krakend-oauth2-clientcredentials": {
"endpoint_params": {},
"token_url": "My Token URL",
"client_id": "My Client ID",
"client_secret": "My Client Secret"
}
},
"disable_host_sanitize": false
}
]
Upvotes: 0