Aleks,
On 4/14/21 1:19 PM, Aleksandar Lazic wrote:
From 46ddac8379324b645c662e19de39d5de4ac74a77 Mon Sep 17 00:00:00 2001
From: Aleksandar Lazic <al-hapr...@none.at>
Date: Wed, 14 Apr 2021 13:11:26 +0200
Subject: [PATCH 2/2] MINOR: sample: converter: Add json_query converter
With the json_query can a JSON value be extacted from a Header
or body of the request and saved to a variable.
This converter makes it possible to handle some JSON Workload
to route requests to differnt backends.
Typo: different.
---
doc/configuration.txt | 32 ++++++++
reg-tests/converter/json_query.vtc | 116 +++++++++++++++++++++++++++++
src/sample.c | 95 +++++++++++++++++++++++
3 files changed, 243 insertions(+)
create mode 100644 reg-tests/converter/json_query.vtc
diff --git a/doc/configuration.txt b/doc/configuration.txt
index f242300e7..374e7939b 100644
--- a/doc/configuration.txt
+++ b/doc/configuration.txt
@@ -15961,6 +15961,38 @@ json([<input-code>])
Output log:
{"ip":"127.0.0.1","user-agent":"Very \"Ugly\" UA 1\/2"}
+
This empty line should not be there.
+json_query(<json_path>,[<output_type>])
+ This converter searches for the key given by <json_path> and returns
+ the value.
+ <json_path> must be a valid JSONPath String as defined in
I'd use string in lowercase.
+ https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/
+
+ A floating point value will always be returned as String.
+
+ The follwing JSON types are recognized.
Typo: following.
I'd also use a ':' instead of '.'.
+ - string : This is the default search type and returns a String;
+ - boolean : If the JSON value is not a String or a Number
+ - number : When the JSON value is a Number then will the value be
+ converted to a String. If its known that the value is a
+ integer then add 'int' to the <output_type> which helps
+ haproxy to convert the value to a integer for further usage;
I'd probably completely rephrase this as:
The json_query converter supports the JSON types string, boolean and
number. Floating point numbers will be returned as a string. By
specifying the output_type 'int' the value will be converted to an
Integer. If conversion is not possible the json_query converter fails.
+ Example:
+ # get the value of the key 'iss' from a JWT Bearer token
+ http-request set-var(txn.token_payload)
req.hdr(Authorization),word(2,.),ub64dec,json_query('$.iss')
+
+ # get a integer value from the request body
+ # "{"integer":4}" => 5
+ http-request set-var(txn.pay_int)
req.body,json_query('$.integer','int'),add(1)
+
+ # get a key with '.' in the name
+ # {"my.key":"myvalue"} => myvalue
+ http-request set-var(txn.pay_mykey) req.body,json_query('$.my\\.key')
+
+ # {"boolean-false":false} => 0
+ http-request set-var(txn.pay_boolean_false)
req.body,json_query('$.boolean-false')
These examples look good to me. I'd just move the JWT example to the
bottom, so that the simple examples come first.
language(<value>[,<default>])
Returns the value with the highest q-factor from a list as extracted from the
"accept-language" header using "req.fhdr". Values with no q-factor have a
diff --git a/reg-tests/converter/json_query.vtc
b/reg-tests/converter/json_query.vtc
new file mode 100644
index 000000000..88ef58a0c
--- /dev/null
+++ b/reg-tests/converter/json_query.vtc
@@ -0,0 +1,116 @@
+varnishtest "JSON Query converters Test"
+#REQUIRE_VERSION=2.4
+
+feature ignore_unknown_macro
+
+server s1 {
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+
+ rxreq
+ txresp
+} -start
You can use '-repeat 8' to simplify the server definition.
+haproxy h1 -conf {
+ defaults
+ mode http
+ timeout connect 1s
+ timeout client 1s
+ timeout server 1s
+ option http-buffer-request
+
+ frontend fe
+ bind "fd@${fe}"
+ tcp-request inspect-delay 1s
+
+ http-request set-var(sess.header_json)
req.hdr(Authorization),json_query('$.iss')
+ http-request set-var(sess.pay_json) req.body,json_query('$.iss')
+ http-request set-var(sess.pay_int)
req.body,json_query('$.integer',"int"),add(1)
+ http-request set-var(sess.pay_neg_int)
req.body,json_query('$.negativ-integer',"int"),add(1)
Inconsistent indentation here.
+ http-request set-var(sess.pay_double) req.body,json_query('$.double')
+ http-request set-var(sess.pay_boolean_true)
req.body,json_query('$.boolean-true')
+ http-request set-var(sess.pay_boolean_false)
req.body,json_query('$.boolean-false')
+ http-request set-var(sess.pay_mykey) req.body,json_query('$.my\\.key')
+
+ http-response set-header x-var_header %[var(sess.header_json)]
+ http-response set-header x-var_body %[var(sess.pay_json)]
+ http-response set-header x-var_body_int %[var(sess.pay_int)]
+ http-response set-header x-var_body_neg_int %[var(sess.pay_neg_int)]
+ http-response set-header x-var_body_double %[var(sess.pay_double)]
+ http-response set-header x-var_body_boolean_true
%[var(sess.pay_boolean_true)]
+ http-response set-header x-var_body_boolean_false
%[var(sess.pay_boolean_false)]
+ http-response set-header x-var_body_mykey %[var(sess.pay_mykey)]
+
+ default_backend be
+
+ backend be
+ server s1 ${s1_addr}:${s1_port}
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+ txreq -url "/" \
+ -hdr "Authorization: {\"iss\":\"kubernetes.io/serviceaccount\"}"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-var_header ~ "kubernetes.io/serviceaccount"
+
+ txreq -url "/" \
+ -body "{\"iss\":\"kubernetes.io/serviceaccount\"}"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-var_body ~ "kubernetes.io/serviceaccount"
+
+ txreq -url "/" \
+ -body "{\"integer\":4}"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-var_body_int ~ "5"
+
+ txreq -url "/" \
+ -body "{\"integer\":-4}"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-var_body_int ~ "-3"
+
+ txreq -url "/" \
+ -body "{\"double\":4.5}"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-var_body_double ~ "4.5"
+
+ txreq -url "/" \
+ -body "{\"boolean-true\":true}"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-var_body_boolean_true == 1
+
+ txreq -url "/" \
+ -body "{\"boolean-false\":false}"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-var_body_boolean_false == 0
+
+ txreq -url "/" \
+ -body "{\"my.key\":\"myvalue\"}"
+ rxresp
+ expect resp.status == 200
+ expect resp.http.x-var_body_mykey ~ "myvalue"
+} -run
\ No newline at end of file
diff --git a/src/sample.c b/src/sample.c
index 7337ba06a..a49d4a89e 100644
--- a/src/sample.c
+++ b/src/sample.c
@@ -16,6 +16,7 @@
#include <arpa/inet.h>
#include <stdio.h>
+#include <import/mjson.h>
#include <import/sha1.h>
#include <import/xxhash.h>
@@ -3689,6 +3690,99 @@ static int sample_conv_rtrim(const struct arg *arg_p, struct sample *smp, void *
return 1;
}
+/* This function checks the "json_query" converter's arguments. */
+static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
+ const char *file, int line, char **err)
+{
+ if (arg[0].data.str.data == 0) {
+ memprintf(err, "json_path must not be empty");
+ return 0;
+ }
+
+ if (arg[1].data.str.data != 0) {
+ if (strncmp(arg[1].data.str.area, "int", sizeof("int"))
!= 0) {
Use:
if (strcmp(arg[1].data.str.area, "int") != 0) {
+ memprintf(err, "output_type only supports \"int\" as
argument");
+ return 0;
+ } else {
+ arg[1].type = ARGT_SINT;
+ arg[1].data.sint = 0;
+ }
+ }
+ return 1;
+}
+
+/* Limit JSON integer values to the range [-(2**53)+1, (2**53)-1] as per
+ * the recommendation for interoperable integers in section 6 of RFC 7159.
+ */
+#define JSON_INT_MAX ((1ULL << 53) - 1)
+#define JSON_INT_MIN (0 - JSON_INT_MAX)
+
+/* This sample function get the value from a given json string.
+ * The mjson library is used to parse the json struct
+ */
json -> JSON.
+static int sample_conv_json_query(const struct arg *args, struct sample *smp,
void *private)
+{
+ struct buffer *trash = get_trash_chunk();
+ const char *p; /* holds the temporay string from mjson_find */
Typo: temporary.
+ int tok, n; /* holds the token enum and the length of the value */
+ int rc; /* holds the return code from mjson_get_string */
+
+ tok = mjson_find(smp->data.u.str.area, smp->data.u.str.data,
args[0].data.str.area, &p, &n);
+
+ switch(tok) {
+ case MJSON_TOK_NUMBER:
+ if (args[1].type == ARGT_SINT) {
+ smp->data.u.sint = atoll(p);
+
+ if (smp->data.u.sint < 0 && smp->data.u.sint <
JSON_INT_MIN) {
+ /* JSON integer too big negativ value */
This comment appears to be useless. It is implied by the 'if'. I also
believe that the 'sint < 0' check is not needed.
+ return 0;
+ } else if (smp->data.u.sint > 0 &&
smp->data.u.sint > JSON_INT_MAX) {
+ /* JSON integer too big positiv value */
Same here.
+ return 0;
+ }
+
+ smp->data.type = SMP_T_SINT;
+ } else {
+ double double_val;
+ if(mjson_get_number(smp->data.u.str.area,
smp->data.u.str.data, args[0].data.str.area, &double_val) == 0) {
+ return 0;
+ } else {
+ trash->data = snprintf(trash->area,
+
trash->size,
+
"%g",double_val);
This indentation looks a bit strange.
+ smp->data.u.str = *trash;
+ smp->data.type = SMP_T_STR;
Incorrect identation here.
+ }
+ }
+ break;
+ case MJSON_TOK_TRUE:
+ smp->data.type = SMP_T_BOOL;
+ smp->data.u.sint = 1;
+ break;
+ case MJSON_TOK_FALSE:
+ smp->data.type = SMP_T_BOOL;
+ smp->data.u.sint = 0;
+ break;
+ case MJSON_TOK_STRING:
+ rc = mjson_get_string(smp->data.u.str.area,
smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
+ if (rc == -1 ) {
Additional space after -1.
+ /* invalid string */
+ return 0;
+ } else {
+ trash->data = rc;
+ smp->data.u.str = *trash;
+ smp->data.type = SMP_T_STR;
+ }
+ break;
+ default:
+ /* no valid token found */
+ return 0;
+ }
+ return 1;
+}
+
+
/************************************************************************/
/* All supported sample fetch functions must be declared here */
/************************************************************************/
@@ -4203,6 +4297,7 @@ static struct sample_conv_kw_list sample_conv_kws = {ILH,
{
{ "cut_crlf", sample_conv_cut_crlf, 0, NULL, SMP_T_STR,
SMP_T_STR },
{ "ltrim", sample_conv_ltrim, ARG1(1,STR), NULL, SMP_T_STR,
SMP_T_STR },
{ "rtrim", sample_conv_rtrim, ARG1(1,STR), NULL, SMP_T_STR,
SMP_T_STR },
+ { "json_query", sample_conv_json_query, ARG2(1,STR,STR),
sample_check_json_query , SMP_T_STR, SMP_T_ANY },
{ NULL, NULL, 0, 0, 0 },
}};
--
2.25.1