Skip to content

New field 'body' to HTTP GET/GET-by-POST metadata#293

Merged
guicassolato merged 1 commit intomainfrom
http-metadata-raw-body
Jun 3, 2022
Merged

New field 'body' to HTTP GET/GET-by-POST metadata#293
guicassolato merged 1 commit intomainfrom
http-metadata-raw-body

Conversation

@guicassolato
Copy link
Collaborator

@guicassolato guicassolato commented Jun 1, 2022

It adds a new field spec.metadata.body.

The new field is an alternative to spec.metadata.bodyParameters for defining raw HTTP bodies and ensure proper content encoding of JSON with multiple levels.

E.g., the following config:

apiVersion: authorino.kuadrant.io/v1beta1
kind: AuthConfig
metadata:
  name: talker-api-protection
spec:
  hosts: ["talker-api-authorino.127.0.0.1.nip.io"]
  metadata:
  - name: echo-api-post-json
    http:
      endpoint: http://talker-api.default.svc.cluster.local:3000/metadata?encoding=json
      method: POST
      contentType: application/json
      body:
        valueFrom:
          authJSON: |
            \{"original_path":"{context.request.http.headers.x-forwarded-for}","request":\{"key1":\{"key2":\{"key3":"{context.request.http.headers.x-forwarded-for}"\}\}\}\}

will send a request:

POST /metadata?encoding=json HTTP/1.1
Content-Length: 81
Content-Type: application/json
Host: talker-api.default.svc.cluster.local:3000
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip
Version: HTTP/1.1
(…request headers omitted)

{"original_path":"10.244.0.8","request":{"key1":{"key2":{"key3":"10.244.0.8"}}}}

A similar config to the above using bodyParameters instead would be:

kubectl apply -f -<<EOF
apiVersion: authorino.kuadrant.io/v1beta1
kind: AuthConfig
metadata:
  name: talker-api-protection
spec:
  hosts: ["talker-api-authorino.127.0.0.1.nip.io"]
  metadata:
  - name: echo-api-post-json
    http:
      endpoint: http://talker-api.default.svc.cluster.local:3000/metadata?encoding=json
      method: POST
      contentType: application/json
      bodyParameters:
      - name: original_path
        valueFrom: { authJSON: context.request.http.headers.x-forwarded-for }
      - name: request
        valueFrom:
          authJSON: |
            \{"key1":\{"key2":\{"key3":"{context.request.http.headers.x-forwarded-for}"\}\}\}

In the latter however, 'request' is encoded as a JSON string, thus resulting in the following request sent to the metadata service:

POST /metadata?encoding=json HTTP/1.1
Content-Length: 92
Content-Type: application/json
Host: talker-api.default.svc.cluster.local:3000
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip
Version: HTTP/1.1
(…request headers omitted)

{"original_path":"10.244.0.8","request":"{\"key1\":{\"key2\":{\"key3\":\"10.244.0.8\"}}}\n"}

Closes #271

The new field is an alternative to 'bodyParameters' for defining raw HTTP bodies and ensure proper content encoding of JSON with multiple levels.

E.g., the following config:

```yaml
apiVersion: authorino.kuadrant.io/v1beta1
kind: AuthConfig
metadata:
  name: talker-api-protection
spec:
  hosts: ["talker-api-authorino.127.0.0.1.nip.io"]
  metadata:
  - name: echo-api-post-json
    http:
      endpoint: http://talker-api.default.svc.cluster.local:3000/metadata?encoding=json
      method: POST
      contentType: application/json
      body:
        valueFrom:
          authJSON: |
            \{"original_path":"{context.request.http.headers.x-forwarded-for}","request":\{"key1":\{"key2":\{"key3":"{context.request.http.headers.x-forwarded-for}"\}\}\}\}
```

will send a request:

```
POST /metadata?encoding=json HTTP/1.1
Content-Length: 81
Content-Type: application/json
Host: talker-api.default.svc.cluster.local:3000
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip
Version: HTTP/1.1
(…request headers omitted)

{"original_path":"10.244.0.8","request":{"key1":{"key2":{"key3":"10.244.0.8"}}}}
```

A similar config to the above using 'bodyParameters' instead would be:

```yaml
kubectl apply -f -<<EOF
apiVersion: authorino.kuadrant.io/v1beta1
kind: AuthConfig
metadata:
  name: talker-api-protection
spec:
  hosts: ["talker-api-authorino.127.0.0.1.nip.io"]
  metadata:
  - name: echo-api-post-json
    http:
      endpoint: http://talker-api.default.svc.cluster.local:3000/metadata?encoding=json
      method: POST
      contentType: application/json
      bodyParameters:
      - name: original_path
        valueFrom: { authJSON: context.request.http.headers.x-forwarded-for }
      - name: request
        valueFrom:
          authJSON: |
            \{"key1":\{"key2":\{"key3":"{context.request.http.headers.x-forwarded-for}"\}\}\}
```

In the latter however, 'request' is encoded as a JSON string, thus resulting in the following request sent to the metadata service:

```
POST /metadata?encoding=json HTTP/1.1
Content-Length: 92
Content-Type: application/json
Host: talker-api.default.svc.cluster.local:3000
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip
Version: HTTP/1.1
(…request headers omitted)

{"original_path":"10.244.0.8","request":"{\"key1\":{\"key2\":{\"key3\":\"10.244.0.8\"}}}\n"}
```
@guicassolato guicassolato force-pushed the http-metadata-raw-body branch from efcc8be to 5c1d98c Compare June 2, 2022 12:32
@guicassolato guicassolato requested a review from thomasmaas June 2, 2022 12:57
@guicassolato guicassolato merged commit b8a6af7 into main Jun 3, 2022
@guicassolato guicassolato deleted the http-metadata-raw-body branch June 3, 2022 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested dynamic parameters in the body of HTTP external metadata POST requests

3 participants