Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set cors allow origins by plugin metadata #6546

Conversation

Gary-Airwallex
Copy link
Contributor

@Gary-Airwallex Gary-Airwallex commented Mar 8, 2022

What this PR does / why we need it:

Supports setting allow_origins by plugin metadata in the cors plugin. We usually have many routes sharing the same set of allowed origins, and it's really inconvenient when trying to update the allowed_origins sets (have to do it for all routes). Existing admin objects like services and plugin configs don't exactly solve this issue since the routes may have different configurations for other plugins but share the same allowe_origins.

Pre-submission checklist:

  • Did you explain what problem does this PR solve? Or what new features have been added?
  • Have you added corresponding test cases?
  • Have you modified the corresponding document?
  • Is this PR backward compatible? If it is not backward compatible, please discuss on the mailing list first

@Gary-Airwallex
Copy link
Contributor Author

I haven't edited any docs yet, would like some opinions on the feature and the implementation/tests before moving on

if not match_origins(req_origin, allow_origins) then
allow_origins = process_with_allow_origins_by_regex(conf, ctx, req_origin)
end
if not match_origins(req_origin, allow_origins) then
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if not match_origins(req_origin, allow_origins) then
if not allow_origins then

t/plugin/cors3.t Outdated
}
}
--- request
GET /t
Copy link
Member

Choose a reason for hiding this comment

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

Let's use this script to simplify the duplicate fields:

#!/usr/bin/env python
# coding: utf-8
# Usage: ./simpify.py $test_filename
import sys
with open(sys.argv[1]) as f:
    lines = [l.rstrip() for l in f.readlines()]
newlines = []
for i, l in enumerate(lines):
    if ((l == "GET /t" and lines[i-1] == "--- request") or
        (l == "[error]" and lines[i-1] == "--- no_error_log")):
        newlines = newlines[:-1]
        continue
    newlines.append(l)
res = "\n".join(newlines)
# print(res)
with open(sys.argv[1], "w") as f:
   f.write(res + '\n') 

…etadata

# Conflicts:
#	docs/en/latest/plugins/cors.md
#	docs/zh/latest/plugins/cors.md
create_multiple_origin_cache, conf)

if not (cache_key and cache_version) then
cache_key, cache_version = core.lrucache.plugin_ctx_id(ctx)
Copy link
Member

Choose a reason for hiding this comment

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

plugin_ctx_id only returns an ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

😂fixed

spacewander
spacewander previously approved these changes Mar 10, 2022
local allow_origins = conf.allow_origins
local function process_with_allow_origins(allow_origins_conf, ctx, req_origin,
cache_key, cache_version)
local allow_origins = allow_origins_conf
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to declare allow_origins_conf as local allow_origins here, just change the allow_origins_conf -> allow_origins in parameter list is ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, changed that

docs/zh/latest/plugins/cors.md Outdated Show resolved Hide resolved
docs/zh/latest/plugins/cors.md Outdated Show resolved Hide resolved
Gary-Airwallex and others added 4 commits March 11, 2022 11:45
Co-authored-by: tzssangglass <tzssangglass@gmail.com>
…y_plugin_metadata' into feature/set_cors_allow_origins_by_plugin_metadata
@spacewander spacewander merged commit 09e36cd into apache:master Mar 13, 2022
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.

3 participants