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 15d92e0
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 22 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
}
4 changes: 2 additions & 2 deletions 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"],
"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
}
7 changes: 0 additions & 7 deletions config/krakend/partials/modifiers.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,6 @@
"value": "identity"
}
},
{
"header.Modifier": {
"scope": ["request"],
"name": "Content-Type",
"value": "application/json"
}
},
{
"header.Modifier": {
"scope": ["response"],
Expand Down
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
1 change: 1 addition & 0 deletions config/krakend/templates/inventory.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
1 change: 1 addition & 0 deletions config/krakend/templates/resource.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
15 changes: 9 additions & 6 deletions config/krakend/templates/uniconfig.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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" }}
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -97,6 +97,9 @@

"endpoint": "/static/list/uniconfig",
"method": "GET",
"extra_config": {
{{ include "content_header.tmpl" }}
},
"backend": [
{
"url_pattern": "/__health",
Expand Down
12 changes: 6 additions & 6 deletions config/krakend/templates/unistore.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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" }}
Expand Down Expand Up @@ -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 }}
Expand Down
3 changes: 2 additions & 1 deletion config/krakend/templates/workflow_proxy.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down

0 comments on commit 15d92e0

Please sign in to comment.