Skip to content

Commit

Permalink
Set default Content-Type to application/json
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
jmasar committed Feb 1, 2024
1 parent 9d18ce6 commit a8fb76b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
7 changes: 7 additions & 0 deletions config/krakend/partials/content_header.tmpl
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion config/krakend/partials/jwt_fake_config.tmpl
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 22 additions & 0 deletions config/krakend/partials/uc_modifiers.tmpl
Original file line number Diff line number Diff line change
@@ -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"
}
}
]
}
}
7 changes: 7 additions & 0 deletions config/krakend/scripts/header_check.lua
Original file line number Diff line number Diff line change
@@ -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
19 changes: 10 additions & 9 deletions config/krakend/templates/uniconfig.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@
{{ 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 }}
},

"backend": [
{
"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": [
Expand Down Expand Up @@ -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 }}
Expand All @@ -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": [
Expand Down

0 comments on commit a8fb76b

Please sign in to comment.