Skip to content

feat: allow sending headers upstream returned by OPA server#9710

Merged
juststillthinking merged 11 commits into
apache:masterfrom
Revolyssup:revolyssup/9704
Jul 19, 2023
Merged

feat: allow sending headers upstream returned by OPA server#9710
juststillthinking merged 11 commits into
apache:masterfrom
Revolyssup:revolyssup/9704

Conversation

@Revolyssup

@Revolyssup Revolyssup commented Jun 21, 2023

Copy link
Copy Markdown
Contributor

Description

Fixes #9704

Documentation to be updated

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

@juststillthinking

juststillthinking commented Jun 26, 2023

Copy link
Copy Markdown
Contributor

Please make the ci pass and no test cases?

@juststillthinking juststillthinking added the wait for update wait for the author's response in this issue/PR label Jun 26, 2023
Signed-off-by: revolyssup <[email protected]>
@Revolyssup

Copy link
Copy Markdown
Contributor Author

Please make the ci pass and no test cases?

Added test case @monkeyDluffy6017

Signed-off-by: revolyssup <[email protected]>
@lingsamuel lingsamuel removed the wait for update wait for the author's response in this issue/PR label Jun 26, 2023
lingsamuel
lingsamuel previously approved these changes Jun 26, 2023
@Revolyssup

Copy link
Copy Markdown
Contributor Author

@monkeyDluffy6017 PTAL

Comment thread apisix/plugins/opa.lua Outdated
Comment on lines +130 to +134
else if conf.send_header_upstream and result.headers then
for key,value in pairs(result.headers) do
core.request.set_header(ctx,key,value)
end
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to proxy all of opa's request headers?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What else do you propose? How to select which ones to proxy?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what HTTP header should pass upstream? instead of forwarding all HTTP header

@Sn0rt

Sn0rt commented Jul 7, 2023

Copy link
Copy Markdown
Contributor

@chusov pls help us take a look

@chusov

chusov commented Jul 10, 2023

Copy link
Copy Markdown

Maybe add a parameter with a list of headers?
send_header_upstream_list

@Revolyssup

Copy link
Copy Markdown
Contributor Author

Maybe add a parameter with a list of headers? send_header_upstream_list

@monkeyDluffy6017 What do you think about this?

@juststillthinking

Copy link
Copy Markdown
Contributor

@chusov i think the headers that need to be passed to upstream from OPA server should be fixed, what headers do you exactly need?

@chusov

chusov commented Jul 11, 2023

Copy link
Copy Markdown

@monkeyDluffy6017
at the moment it is enough X-Username, in the future there were plans to add more depending on the service

@juststillthinking

Copy link
Copy Markdown
Contributor

Maybe add a parameter with a list of headers? send_header_upstream_list

@monkeyDluffy6017 What do you think about this?

LGTM

@Revolyssup

Copy link
Copy Markdown
Contributor Author

Maybe add a parameter with a list of headers? send_header_upstream_list

@monkeyDluffy6017 What do you think about this?

LGTM

Okay I'm making the change. @Sn0rt This one change remaining as suggested above by user

Comment thread apisix/plugins/opa.lua Outdated
Comment thread apisix/plugins/opa.lua
Comment on lines +40 to +47
send_headers_upstream = {
type = "array",
minItems = 0,
items = {
type = "string"
},
description = "list of headers to pass to upstream in request"
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
send_headers_upstream = {
type = "array",
minItems = 0,
items = {
type = "string"
},
description = "list of headers to pass to upstream in request"
},
send_headers_upstream = {
type = "array",
minItems = 1,
items = {
type = "string"
},
description = "list of headers to pass to upstream in request"
},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread apisix/plugins/opa.lua Outdated
Comment on lines +136 to +147
else if result.headers and #conf.send_headers_upstream > 0 then
local headersToSend = {}
for key in pairs(conf.send_headers_upstream) do
headersToSend[key] = true
end
for key,value in pairs(result.headers) do
if headersToSend[key] then
core.request.set_header(ctx,key,value)
end

end
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else if result.headers and #conf.send_headers_upstream > 0 then
local headersToSend = {}
for key in pairs(conf.send_headers_upstream) do
headersToSend[key] = true
end
for key,value in pairs(result.headers) do
if headersToSend[key] then
core.request.set_header(ctx,key,value)
end
end
end
else if result.headers and conf.send_headers_upstream then
local headersToSend = {}
for key in pairs(conf.send_headers_upstream) do
headersToSend[key] = true
end
for key,value in pairs(result.headers) do
if headersToSend[key] then
core.request.set_header(ctx,key,value)
end
end
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indent is not right, only four spaces are needed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@juststillthinking juststillthinking added wait for update wait for the author's response in this issue/PR and removed discuss labels Jul 18, 2023
Signed-off-by: revolyssup <[email protected]>
Comment thread apisix/plugins/opa.lua Outdated
for key in pairs(conf.send_headers_upstream) do
headersToSend[key] = true
end
for key,value in pairs(result.headers) do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for key,value in pairs(result.headers) do
for key, value in pairs(result.headers) do

Comment thread apisix/plugins/opa.lua Outdated
if headersToSend[key] then
core.request.set_header(ctx,key,value)
end

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blank is needed

Comment thread apisix/plugins/opa.lua Outdated
end
for key,value in pairs(result.headers) do
if headersToSend[key] then
core.request.set_header(ctx,key,value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
core.request.set_header(ctx,key,value)
core.request.set_header(ctx, key, value)

@juststillthinking juststillthinking Jul 18, 2023

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could refactor this block like this:

        for _, name in ipairs(conf.send_headers_upstream) do
            if result.headers[name] then
                core.request.set_header(ctx, name, value)
            end
        end

@Revolyssup Revolyssup Jul 18, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes more sense. Done

Signed-off-by: revolyssup <[email protected]>
Signed-off-by: revolyssup <[email protected]>
@juststillthinking juststillthinking added approved and removed wait for update wait for the author's response in this issue/PR labels Jul 18, 2023
@juststillthinking
juststillthinking merged commit 79b24cd into apache:master Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: opa plugins, add the ability to set headers

6 participants