Skip to content

feat: set cors allow origins by plugin metadata#6546

Merged
spacewander merged 8 commits into
apache:masterfrom
Gary-Airwallex:feature/set_cors_allow_origins_by_plugin_metadata
Mar 13, 2022
Merged

feat: set cors allow origins by plugin metadata#6546
spacewander merged 8 commits into
apache:masterfrom
Gary-Airwallex:feature/set_cors_allow_origins_by_plugin_metadata

Conversation

@Gary-Airwallex

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

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor Author

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

Comment thread apisix/plugins/cors.lua Outdated
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
Copy Markdown
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

Comment thread t/plugin/cors3.t Outdated
}
}
--- request
GET /t

Copy link
Copy Markdown
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
Comment thread apisix/plugins/cors.lua Outdated
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
Copy Markdown
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
Copy Markdown
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
Comment thread apisix/plugins/cors.lua Outdated
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
Copy Markdown
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
Copy Markdown
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

Comment thread docs/zh/latest/plugins/cors.md Outdated
Comment thread docs/zh/latest/plugins/cors.md Outdated
@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