Is there a way to send metadata in krakend endpoint configuration?
Asked Answered
D

2

7

I'm using Krakend as API-Gateway, and my configuration looks like this :

{
  "plugin": {
    "folder": "/etc/krakend/plugins/authenticator/",
    "pattern":".so"
  },
  "port": 8080,
  "extra_config": {
    "github_com/devopsfaith/krakend/transport/http/server/handler": {
      "name": "authenticator"
    }
  },

  "endpoints": [
    {
      "output_encoding": "no-op",
      "backend": [
        {
          "encoding": "no-op",
          "host": [
            "127.0.0.1:8080"
          ],
          "url_pattern": "/api/v1/address/{id}",
          "method": "GET"
        }
      ],
      "endpoint": "/api/v1/addresses/{id}",
      "method": "GET"
    }
  ],

  "name": "gateway",
  "timeout": "30s",
  "version": 2
}

I want to pass some metadata per end point and access it in my predefined plugin . In this case authenticator plugin.

Decomposed answered 4/1, 2022 at 7:9 Comment(0)
A
0

You're trying to use a http-server plugin, which is supposed to be in the router layer, aka before it hits the krakend enpoints. So it is defined globally.

Since you're trying to have difference configurations per endpoint, consider using http-client plugins.

In this way you can get your metadata from the extra_config of each endpoint.

endpoints: [
{
  "output_encoding": "no-op",
  "backend": [
    {
      "encoding": "no-op",
      "host": [
        "127.0.0.1:8080"
      ],
      "url_pattern": "/api/v1/address/{id}",
      "method": "GET",
      "extra_config": {
        "plugin/http-client": {
          "name": "krakend-client-example",
          "krakend-client-example": {
            "metadata_example_1": "example"
          }
        }
      }
    }
  ],
  "endpoint": "/api/v1/addresses/{id}",
  "method": "GET"
},
]
Agonize answered 13/9, 2023 at 5:42 Comment(0)
F
-1

What you are trying to achieve is perfectly possible, and is the way all components work in KrakenD. Your plugin can access the KrakenD configuration using the namespace you define. For instance, you could set your metadata like this (I am assuming you have in your Go code a pluginName = "slifer2015-authenticator" ):

{
    "endpoints": [
        {
            "output_encoding": "no-op",
            "backend": [
                {
                    "encoding": "no-op",
                    "host": [
                        "127.0.0.1:8080"
                    ],
                    "url_pattern": "/api/v1/address/{id}"
                }
            ],
            "endpoint": "/api/v1/addresses/{id}",
            "extra_config": {
                "github_com/devopsfaith/krakend/transport/http/server/handler": {
                    "name": [
                        "slifer2015-authenticator",
                        "some-other-plugin-here"
                    ],
                    "slifer2015-authenticator": {
                        "Metadata1": "value1",
                        "Metadata2": {
                            "Some": 10,
                            "Thing": 100,
                            "Here": "60s"
                        }
                    }
                }
            }
        }
    ]
}

Then your metada is available in the extra parameter when the registerer kicks in, inside the key you have chosen.

func (r registerer) registerHandlers(ctx context.Context, extra map[string]interface{}, h http.Handler) (http.Handler, error) {
``
Faugh answered 8/1, 2022 at 13:41 Comment(1)
I tried this , unfortunately It doesn't work . It only apply the plugin when I put extra_config in the root of configuration not per endpoint :(Decomposed

© 2022 - 2024 — McMap. All rights reserved.