From a8fb76bc1e31c9799e3eecc0a8159c2237246649 Mon Sep 17 00:00:00 2001 From: jmasar Date: Mon, 29 Jan 2024 13:19:47 +0100 Subject: [PATCH] Set default Content-Type to application/json - add Content-Type via lua script only when it has no value - setting Content-Type in modifier/martian is no longer necesary Type: Bugfix JIRA: DEP-682 Signed-off-by: jmasar --- config/krakend/partials/content_header.tmpl | 7 +++++++ config/krakend/partials/jwt_fake_config.tmpl | 2 +- config/krakend/partials/uc_modifiers.tmpl | 22 ++++++++++++++++++++ config/krakend/scripts/header_check.lua | 7 +++++++ config/krakend/templates/uniconfig.tmpl | 19 +++++++++-------- 5 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 config/krakend/partials/content_header.tmpl create mode 100644 config/krakend/partials/uc_modifiers.tmpl create mode 100644 config/krakend/scripts/header_check.lua diff --git a/config/krakend/partials/content_header.tmpl b/config/krakend/partials/content_header.tmpl new file mode 100644 index 0000000..964ac2f --- /dev/null +++ b/config/krakend/partials/content_header.tmpl @@ -0,0 +1,7 @@ +"modifier/lua-endpoint": { + "@comment": "Add header Content-Type with value application/json if no Content-Type value present", + "sources": ["scripts/header_check.lua"], + "allow_open_libs": true, + "pre": "local r = ctx.load(); check_header(r)", + "live": true +} \ No newline at end of file diff --git a/config/krakend/partials/jwt_fake_config.tmpl b/config/krakend/partials/jwt_fake_config.tmpl index 12b2a1e..eb4aebf 100644 --- a/config/krakend/partials/jwt_fake_config.tmpl +++ b/config/krakend/partials/jwt_fake_config.tmpl @@ -1,6 +1,6 @@ "modifier/lua-endpoint": { "sources": ["scripts/fake_frinx_token.lua"], "allow_open_libs": true, - "pre": "local r = ctx.load(); fake_token_validate(r);", + "pre": "local r = ctx.load(); fake_token_validate(r)", "live": true } diff --git a/config/krakend/partials/uc_modifiers.tmpl b/config/krakend/partials/uc_modifiers.tmpl new file mode 100644 index 0000000..f49f573 --- /dev/null +++ b/config/krakend/partials/uc_modifiers.tmpl @@ -0,0 +1,22 @@ +"modifier/martian": { + "fifo.Group": { + "scope": ["request", "response"], + "aggregateErrors": true, + "modifiers": [ + { + "header.Modifier": { + "scope": ["request"], + "name": "Accept-Encoding", + "value": "identity" + } + }, + { + "header.Modifier": { + "scope": ["response"], + "name": "Content-Type", + "value": "application/json; charset=utf-8" + } + } + ] + } +} \ No newline at end of file diff --git a/config/krakend/scripts/header_check.lua b/config/krakend/scripts/header_check.lua new file mode 100644 index 0000000..fcd3636 --- /dev/null +++ b/config/krakend/scripts/header_check.lua @@ -0,0 +1,7 @@ +function check_header(request) + local header = request:headers("Content-Type") + + if header == "" then + request:headers("Content-Type", "application/json") + end +end \ No newline at end of file diff --git a/config/krakend/templates/uniconfig.tmpl b/config/krakend/templates/uniconfig.tmpl index 1191570..9196dab 100644 --- a/config/krakend/templates/uniconfig.tmpl +++ b/config/krakend/templates/uniconfig.tmpl @@ -16,13 +16,14 @@ {{ if eq $jwt_production "true" }} {{ include "jwt_config.tmpl" }}, "modifier/lua-endpoint": { - "sources": ["scripts/uniconfig_rbac.lua"], + "sources": ["scripts/uniconfig_rbac.lua", "scripts/header_check.lua"], "allow_open_libs": true, - "pre": "local r = ctx.load(); user_group_auth(r, os.getenv('UNICONFIG_CONTROLLER_ADMIN_GROUP'));", + "pre": "local r = ctx.load(); user_group_auth(r, os.getenv('UNICONFIG_CONTROLLER_ADMIN_GROUP')); check_header(r)", "live": true } {{ else if eq $jwt_production "false" }} - {{ include "jwt_fake_config.tmpl" }} + {{ include "jwt_fake_config.tmpl" }}, + {{ include "content_header.tmpl" }} {{ end }} }, @@ -30,7 +31,7 @@ { "url_pattern": "/rests/operations{{range $index_for_uri, $not_used_element2 := $.range -}} {{- if le $index_for_uri $index_in_range -}} /{{"{"}}n_{{$index_for_uri}}{{"}" -}} {{end}}{{end}}", "extra_config": { - {{ include "modifiers.tmpl" }} + {{ include "uc_modifiers.tmpl" }} }, "encoding": "no-op", "host": [ @@ -58,17 +59,17 @@ {{ if eq $jwt_production "true" }} {{ include "jwt_config.tmpl" }}, "modifier/lua-endpoint": { - "sources": ["scripts/uniconfig_query_escape.lua", "scripts/uniconfig_rbac.lua"], + "sources": ["scripts/uniconfig_query_escape.lua", "scripts/uniconfig_rbac.lua", "scripts/header_check.lua"], "allow_open_libs": true, - "pre": "local r = ctx.load(); r:url(urlencode(r)); user_group_auth(r, os.getenv('UNICONFIG_CONTROLLER_ADMIN_GROUP'))", + "pre": "local r = ctx.load(); r:url(urlencode(r)); user_group_auth(r, os.getenv('UNICONFIG_CONTROLLER_ADMIN_GROUP')); check_header(r)", "live": true } {{ else if eq $jwt_production "false" }} "modifier/lua-endpoint": { "@comment": "Fixing escape queries with multiple fields separated by semicolon", - "sources": ["scripts/uniconfig_query_escape.lua", "scripts/fake_frinx_token.lua"], + "sources": ["scripts/uniconfig_query_escape.lua", "scripts/fake_frinx_token.lua", "scripts/header_check.lua"], "allow_open_libs": true, - "pre": "local r = ctx.load(); r:url(urlencode(r)); fake_token_validate(r)", + "pre": "local r = ctx.load(); r:url(urlencode(r)); fake_token_validate(r); check_header(r)", "live": true } {{ end }} @@ -78,7 +79,7 @@ { "url_pattern": "/rests/data{{range $index_for_uri, $not_used_element2 := $.range -}} {{- if le $index_for_uri $index_in_range -}} /{{"{"}}n_{{$index_for_uri}}{{"}" -}} {{end}}{{end}}", "extra_config": { - {{ include "modifiers.tmpl" }} + {{ include "uc_modifiers.tmpl" }} }, "encoding": "no-op", "host": [