Skip to content

Commit 9383906

Browse files
iykonGuaris
andauthored
Feat(Datakit): Use a dynamic internal auth endpoint to inject request headers before proxying a request (#3656)
* Add datakit dynamic url exampel * Add docs for datakit call node proxy options * small changes * Add docs for datakit call node url-encoded support * fixes --------- Co-authored-by: Angel <angel.guarisma@konghq.com> Co-authored-by: Angel <Guaris@users.noreply.github.com>
1 parent f8d78dd commit 9383906

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
description: |
2+
Use a dynamic internal auth endpoint to inject request headers before proxying a request.
3+
4+
extended_description: |
5+
Use a dynamic internal auth endpoint to inject request headers before proxying a request.
6+
7+
This example contains the following nodes:
8+
1. The node `STATIC_INPUTS` sets some static values that will be used as inputs to other nodes.
9+
1. The node `BUILD_HEADERS` fetches an API key from the client query and injects it into the request headers that will be sent to the auth service.
10+
1. The node `BUILD_URL` constructs the auth service URL dynamically based on the request path parameter from request headers.
11+
1. The node `AUTH_REQUEST` makes a POST request to the auth service.
12+
1. The node `UPSTREAM_AUTH_HEADER` composes an Authorization header from the access token received from the auth service and
13+
adds it to the service request headers before proxying the request.
14+
15+
title: Authenticate Kong to a third-party service resolved at runtime
16+
weight: 900
17+
18+
config:
19+
nodes:
20+
- name: STATIC_INPUTS
21+
type: static
22+
values:
23+
headers:
24+
Content-Type: application/x-www-form-urlencoded
25+
body: grant_type=client_credentials
26+
27+
- name: BUILD_HEADERS
28+
type: jq
29+
inputs:
30+
headers: STATIC_INPUTS.headers
31+
query: request.query
32+
jq: |
33+
.headers * {
34+
"X-Api-Key": (.query.api_key // "none")
35+
}
36+
37+
- name: BUILD_URL
38+
type: jq
39+
input: request.headers
40+
jq: |
41+
"https://my-token-service/" + .path + "/auth-token"
42+
43+
- name: AUTH_REQUEST
44+
type: call
45+
inputs:
46+
headers: BUILD_HEADERS
47+
body: STATIC_INPUTS.body
48+
url: BUILD_URL
49+
url: "https://my-token-service/auth-token"
50+
method: POST
51+
52+
- name: UPSTREAM_AUTH_HEADER
53+
type: jq
54+
input: AUTH_REQUEST.body
55+
output: service_request.headers
56+
jq: |
57+
{
58+
Authorization: (.token_type + " " + .access_token)
59+
}
60+
61+
tools:
62+
- deck
63+
- admin-api
64+
- konnect-api
65+
- kic
66+
- terraform
67+
68+
min_version:
69+
gateway: '3.13'

app/_kong_plugins/datakit/examples/authenticate-third-party.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ config:
2121
values:
2222
headers:
2323
Content-Type: application/x-www-form-urlencoded
24-
body: grant_type=client_credentials
24+
body:
25+
grant_type: client_credentials
26+
client_id: my_client_id
2527

2628
- name: BUILD_HEADERS
2729
type: jq

app/_kong_plugins/datakit/index.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ rows:
7777
description: "Leverage the `cache` and `branch` nodes to conditionally store or retrieve cache items."
7878
- usecase: "[Transform XML into JSON, or JSON into XML](/plugins/datakit/examples/convert-json-to-xml-and-back/)"
7979
description: "Transform JSON requests into XML so you can send the data to a SOAP service, then transform the resulting XML back into JSON."
80+
- usecase: "[Third-party auth with dynamic url](/plugins/datakit/examples/authenticate-third-party-with-dynamic-url/)"
81+
description: Dynamically resolve an internal authentication endpoint and inject the necessary request headers prior to proxying the request.
8082
{% endtable %}
8183
<!--vale on-->
8284

@@ -520,6 +522,11 @@ rows:
520522
* `body`: Request body
521523
* `headers`: Request headers
522524
* `query`: Key-value pairs to encode as the request query string
525+
* `url`: The request URL resolved at runtime
526+
* `https_proxy`: The HTTPS proxy URL to use for the request
527+
* `http_proxy`: The HTTP proxy URL to use for the request
528+
* `proxy_auth_username`: The username to authenticate with the proxy
529+
* `proxy_auth_password`: The password to authenticate with the proxy
523530
outputs: |
524531
* `body`: The response body
525532
* `headers`: The response headers
@@ -655,13 +662,26 @@ Send a POST request with a JSON body:
655662
name: Datakit
656663
```
657664

665+
Perform a request through a proxy server:
666+
667+
```yaml
668+
- name: CALL
669+
type: call
670+
url: https://example.com/foo
671+
inputs:
672+
https_proxy: http://my-proxy.example.com:8080
673+
proxy_auth_username: my-username
674+
proxy_auth_password: my-password
675+
```
676+
658677
Call nodes are used in most datakit workflows. For complete examples, see:
659678
* [Third-party auth](/plugins/datakit/examples/authenticate-third-party/)
660679
* [Request multiplexing](/plugins/datakit/examples/combine-two-apis-into-one-response/)
661680
* [Manipulate request headers](/plugins/datakit/examples/manipulate-request-headers/)
662681
* [Authentication with Vault secrets](/plugins/datakit/examples/authenticate-with-vault-secret/)
663682
* [Conditionally fetch or store cache data](/plugins/datakit/examples/conditionally-store-cached-items/)
664683
* [Transform XML into JSON, or JSON into XML](/plugins/datakit/examples/convert-json-to-xml-and-back/)
684+
* [Third-party auth with dynamic url](/plugins/datakit/examples/authenticate-third-party-with-dynamic-url/)
665685

666686
#### Automatic JSON body handling
667687

@@ -707,6 +727,32 @@ the endpoint returns a non-2xx status code. It will also fail if the endpoint
707727
returns a JSON mime-type in the `Content-Type` header if the response body is
708728
not valid JSON.
709729

730+
#### Resolve URL at runtime
731+
732+
A `call` node defines its `url` statically during configuration. To substitute a different endpoint at runtime, pass a value via the `url` input. If the input is `nil`, Datakit automatically reverts to the configured static URL.
733+
734+
For example:
735+
736+
```yaml
737+
- name: DYNAMIC_URL
738+
type: call
739+
url: https://example.com/default
740+
inputs:
741+
url: request.body
742+
```
743+
744+
#### Proxy options
745+
The `call` node supports performing requests via a proxy server. This is controlled by proxy options. See above example for more details.
746+
747+
#### Request body encoding
748+
Call node supports following content types for request body encoding:
749+
* `application/json`
750+
* `application/x-www-form-urlencoded`
751+
752+
By default, if the body input is an object, it will be encoded as JSON. To override this behavior and use `application/x-www-form-urlencoded`, set the `Content-Type` header accordingly in the `headers` input for the call node.
753+
754+
See [Third-party auth](/plugins/datakit/examples/authenticate-third-party/) for an example of using `application/x-www-form-urlencoded` request body encoding.
755+
710756
#### Limitations
711757

712758
Due to platform limitations, the `call` node can't be executed after proxying a
@@ -1019,6 +1065,7 @@ For more detailed examples, see:
10191065
* [Request multiplexing](/plugins/datakit/examples/combine-two-apis-into-one-response/)
10201066
* [Manipulate request headers](/plugins/datakit/examples/manipulate-request-headers/)
10211067
* [Authentication with Vault secrets](/plugins/datakit/examples/authenticate-with-vault-secret/)
1068+
* [Third-party auth with dynamic url](/plugins/datakit/examples/authenticate-third-party-with-dynamic-url/)
10221069
10231070
### Exit node
10241071
@@ -1374,6 +1421,7 @@ For more detailed examples, see:
13741421
* [Third-party auth](/plugins/datakit/examples/authenticate-third-party/)
13751422
* [Authentication with Vault secrets](/plugins/datakit/examples/authenticate-with-vault-secret/)
13761423
* [Conditionally fetch or store cache data](/plugins/datakit/examples/conditionally-store-cached-items/)
1424+
* [Third-party auth with dynamic url](/plugins/datakit/examples/authenticate-third-party-with-dynamic-url/)
13771425

13781426
### XML to JSON node {% new_in 3.13 %}
13791427

0 commit comments

Comments
 (0)