From 15d92e035cc1aabd4730e217b881a8049d700e31 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 | 4 ++-- config/krakend/partials/modifiers.tmpl | 7 ------- config/krakend/scripts/header_check.lua | 7 +++++++ config/krakend/templates/inventory.tmpl | 1 + config/krakend/templates/resource.tmpl | 1 + config/krakend/templates/uniconfig.tmpl | 15 +++++++++------ config/krakend/templates/unistore.tmpl | 12 ++++++------ config/krakend/templates/workflow_proxy.tmpl | 3 ++- 9 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 config/krakend/partials/content_header.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..9c87234 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"], + "sources": ["scripts/fake_frinx_token.lua", "scripts/header_check.lua"], "allow_open_libs": true, - "pre": "local r = ctx.load(); fake_token_validate(r);", + "pre": "local r = ctx.load(); fake_token_validate(r); check_header(r)", "live": true } diff --git a/config/krakend/partials/modifiers.tmpl b/config/krakend/partials/modifiers.tmpl index a4653f2..545cb36 100644 --- a/config/krakend/partials/modifiers.tmpl +++ b/config/krakend/partials/modifiers.tmpl @@ -10,13 +10,6 @@ "value": "identity" } }, - { - "header.Modifier": { - "scope": ["request"], - "name": "Content-Type", - "value": "application/json" - } - }, { "header.Modifier": { "scope": ["response"], 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/inventory.tmpl b/config/krakend/templates/inventory.tmpl index 05bc103..d71e819 100644 --- a/config/krakend/templates/inventory.tmpl +++ b/config/krakend/templates/inventory.tmpl @@ -8,6 +8,7 @@ {{ $jwt_production := env "AUTH_ENABLED" }} {{ if eq $jwt_production "true" }} {{ include "jwt_config.tmpl" }} + {{ include "content_header.tmpl" }} {{ else if eq $jwt_production "false" }} {{ include "jwt_fake_config.tmpl" }} {{ end }} diff --git a/config/krakend/templates/resource.tmpl b/config/krakend/templates/resource.tmpl index 4b73596..c7f6f60 100644 --- a/config/krakend/templates/resource.tmpl +++ b/config/krakend/templates/resource.tmpl @@ -16,6 +16,7 @@ {{ $jwt_production := env "AUTH_ENABLED" }} {{ if eq $jwt_production "true" }} {{ include "jwt_config.tmpl" }} + {{ include "content_header.tmpl" }} {{ else if eq $jwt_production "false" }} {{ include "jwt_fake_config.tmpl" }} {{ end }} diff --git a/config/krakend/templates/uniconfig.tmpl b/config/krakend/templates/uniconfig.tmpl index 1191570..0191e19 100644 --- a/config/krakend/templates/uniconfig.tmpl +++ b/config/krakend/templates/uniconfig.tmpl @@ -16,9 +16,9 @@ {{ 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" }} @@ -58,17 +58,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 }} @@ -97,6 +97,9 @@ "endpoint": "/static/list/uniconfig", "method": "GET", + "extra_config": { + {{ include "content_header.tmpl" }} + }, "backend": [ { "url_pattern": "/__health", diff --git a/config/krakend/templates/unistore.tmpl b/config/krakend/templates/unistore.tmpl index dfbea77..3973c2f 100644 --- a/config/krakend/templates/unistore.tmpl +++ b/config/krakend/templates/unistore.tmpl @@ -15,9 +15,9 @@ {{ if eq $jwt_production "true" }} {{ include "jwt_config.tmpl" }}, "modifier/lua-endpoint": { - "sources": ["scripts/unistore_rbac.lua"], + "sources": ["scripts/unistore_rbac.lua", "scripts/header_check.lua"], "allow_open_libs": true, - "pre": "local r = ctx.load(); user_group_auth(r)", + "pre": "local r = ctx.load(); user_group_auth(r); check_header(r)", "live": true } {{ else if eq $jwt_production "false" }} @@ -62,17 +62,17 @@ {{ if eq $jwt_production "true" }} {{ include "jwt_config.tmpl" }}, "modifier/lua-endpoint": { - "sources": ["scripts/uniconfig_query_escape.lua", "scripts/unistore_rbac.lua"], + "sources": ["scripts/uniconfig_query_escape.lua", "scripts/unistore_rbac.lua", "scripts/header_check.lua"], "allow_open_libs": true, - "pre": "local r = ctx.load(); r:url(urlencode(r)); user_group_auth(r)", + "pre": "local r = ctx.load(); r:url(urlencode(r)); user_group_auth(r); 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 }} diff --git a/config/krakend/templates/workflow_proxy.tmpl b/config/krakend/templates/workflow_proxy.tmpl index 5691744..1a9bb48 100644 --- a/config/krakend/templates/workflow_proxy.tmpl +++ b/config/krakend/templates/workflow_proxy.tmpl @@ -15,7 +15,8 @@ "extra_config": { {{ $jwt_production := env "AUTH_ENABLED" }} {{ if eq $jwt_production "true" }} - {{ include "jwt_config.tmpl" }} + {{ include "jwt_config.tmpl" }}, + {{ include "content_header.tmpl" }} {{ else if eq $jwt_production "false" }} {{ include "jwt_fake_config.tmpl" }} {{ end }}