How to use check_expr in endpoint

233 views
Skip to first unread message

Руслан Терехов

unread,
Dec 23, 2021, 10:34:50 AM12/23/21
to KrakenD Community
Hi, I think I did everything according to the documentation so that there was a condition in the endpoint. But for some reason he ignores the false and is completely executed.

photo_2021-12-23_18-34-14.jpg

Albert Lombarte

unread,
Dec 23, 2021, 12:00:20 PM12/23/21
to Руслан Терехов, KrakenD Community
Привет Руслан

I would say that in the screenshot you provided you are not checking either the request or the response, so your CEL evaluator will be ignored.

The way to check it is when KrakenD starts (the logging level should be DEBUG):
2021/12/23 17:52:34  DEBUG: [CEL: proxy /over-five/:int preEvaluators [0xc000938540]]
2021/12/23 17:52:34  DEBUG: [CEL: proxy /over-five/:int postEvaluators []]

If you don't see the registered evaluator inside preEvaluators or postEvaluators (in red above) then your expression is ignored. I have left a simple example below that checks that the request has an integer over five and accepts or denies the request. It is using something in the request or the response so it will work.

{
"version": 2,
"endpoints": [
{
"endpoint": "/over-five/{int}",
"backend": [
{
"host": ["http://localhost:8080"],
"url_pattern": "/__health"
}
],
"extra_config": {
{
"check_expr": "int(req_params.Int) > 5"
}
]
}
}
]
}

The output:

❯ curl -i http://localhost:8080/over-five/2
HTTP/1.1 500 Internal Server Error
X-Krakend: Version 1.4.1
X-Krakend-Completed: false
Date: Thu, 23 Dec 2021 16:52:41 GMT
Content-Length: 0

❯ curl -i http://localhost:8080/over-five/6
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
X-Krakend: Version 1.4.1
X-Krakend-Completed: true
Date: Thu, 23 Dec 2021 16:52:44 GMT
Content-Length: 15

{"status":"ok"}


Logo
Albert Lombarte
CEO

LinkedIn icon   Twitter icon  


Missatge de Руслан Терехов <akof...@gmail.com> del dia dj., 23 de des. 2021 a les 16:34:
Hi, I think I did everything according to the documentation so that there was a condition in the endpoint. But for some reason he ignores the false and is completely executed.

photo_2021-12-23_18-34-14.jpg

--
You received this message because you are subscribed to the Google Groups "KrakenD Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to community+...@krakend.io.
To view this discussion on the web visit https://groups.google.com/a/krakend.io/d/msgid/community/22278d6b-9f8e-43a2-96a4-16923a8d9ce0n%40krakend.io.

Руслан Терехов

unread,
Dec 24, 2021, 5:14:58 AM12/24/21
to KrakenD Community, alb...@krakend.io, KrakenD Community, Руслан Терехов
I got it, thank you. I have one more problem. I want take value from headers and compare it with int, example: 1 > int(req_headers['version'])
But i have error:
CEL: error parsing the definitions for pipe {url}: cel: error checking the expression and its param definition
falling back to the next pipe proxy

четверг, 23 декабря 2021 г. в 20:00:20 UTC+3, alb...@krakend.io:

Daniel Lopez

unread,
Dec 24, 2021, 6:58:06 AM12/24/21
to KrakenD Community, akof...@gmail.com, Albert Lombarte, KrakenD Community
The req_headers maps a header to an array of strings. In order to access a value from the header array, you need to define the index.

One final comment, double check your endpoint config in order to pass the "version" header.

Here you have a working example:

{
  "endpoint": "/cel",
  "headers_to_pass":["version"],
  "backend": [ ... ],
  "extra_config":{
    "github.com/devopsfaith/krakend-cel":[{
      "check_expr": "1 > int(req_headers['version'][0])"
    }]
  }
}

$ curl -iH'version: 2' localhost:8080/cel

HTTP/1.1 500 Internal Server Error
X-Krakend: Version 1.4.1
X-Krakend-Completed: false
Date: Fri, 24 Dec 2021 11:56:47 GMT
Content-Length: 0

$ curl -iH'version: 1' localhost:8080/cel

HTTP/1.1 500 Internal Server Error
X-Krakend: Version 1.4.1
X-Krakend-Completed: false
Date: Fri, 24 Dec 2021 11:56:50 GMT
Content-Length: 0

$ curl -iH'version: 0' localhost:8080/cel

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
X-Krakend: Version 1.4.1
X-Krakend-Completed: true
Date: Fri, 24 Dec 2021 11:56:54 GMT
Content-Length: 18

{"message":"pong"}

Cheers!
Reply all
Reply to author
Forward
0 new messages