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

fix: redirect http to https but port not change #7065

Merged
merged 13 commits into from
May 18, 2022
Merged

fix: redirect http to https but port not change #7065

merged 13 commits into from
May 18, 2022

Conversation

jwrookie
Copy link
Contributor

@jwrookie jwrookie commented May 17, 2022

Description

When http redirects to htpps, we use a correct port

Fixes #7011

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)

Signed-off-by: Wei Jiang <machowei01@gmail.com>
Signed-off-by: Wei Jiang <machowei01@gmail.com>
Signed-off-by: Wei Jiang <machowei01@gmail.com>
@@ -470,3 +470,4 @@ plugin_attr:
connect: 60s
read: 60s
send: 60s
redirect_https_port: 8443 # the default port for use by HTTP redirects to HTTPS
Copy link
Member

Choose a reason for hiding this comment

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

We should use a structure like this:

redirect:
    https_port: ...

and comment it out as nobody wants to be redirected to 8443 by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -148,7 +149,30 @@ function _M.rewrite(conf, ctx)
core.log.info("plugin rewrite phase, conf: ", core.json.delay_encode(conf))

local ret_code = conf.ret_code
local ret_port = tonumber(ctx.var["var_x_forwarded_port"])
local local_conf = core.config.local_conf()
local ret_port = core.table.try_read_attr(local_conf,
Copy link
Member

Choose a reason for hiding this comment

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

There is a wrapper to get the attribute of a plugin:

local attr = plugin.plugin_attr(plugin_name)

We need to use it instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

--- yaml_config
apisix:
ssl:
enable: null
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 add two more test:

  1. use listen_port
  2. use listen and get port in the array

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

docs/en/latest/plugins/redirect.md Outdated Show resolved Hide resolved
docs/zh/latest/plugins/redirect.md Outdated Show resolved Hide resolved
jwrookie and others added 3 commits May 17, 2022 20:53
Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
Signed-off-by: Wei Jiang <machowei01@gmail.com>
spacewander
spacewander previously approved these changes May 17, 2022
@@ -45,7 +45,10 @@ The `redirect` Plugin can be used to configure redirects.

Only one of `http_to_https`, `uri` and `regex_uri` can be configured.

* When enabling `http_to_https`, the port in the redirect URL will be the value of header `X-Forwarded-Port` or the port of the server.
* When enabling `http_to_https`, the ports in the redirect URL will pick a value in the following order (in descending order of priority)
* Read `plugin_attr.redirect_https_port` from the configuration file (`conf/config.yaml`).
Copy link
Member

Choose a reason for hiding this comment

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

redirect_https_port should be updated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -45,7 +45,10 @@ description: 本文介绍了关于 Apache APISIX `redirect` 插件的基本信

`http_to_https`、`uri` 和 `regex_uri` 只能配置其中一个属性。

* 当开启 `http_to_https` 时,重定向 URL 中的端口将是 `X-Forwarded-Port` 请求头的值或服务器的端口。
* 当开启 `http_to_https` 时,重定向 URL 中的端口将按如下顺序选取一个值(按优先级从高到低排列)
* 从配置文件(`conf/config.yaml`)中读取 `plugin_attr.redirect_https_port`。
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -428,59 +428,83 @@ passed



=== TEST 18: redirect
=== TEST 18: redirect(port using `plugin_attr.redirect_https_port`)
Copy link
Member

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

apisix/plugins/redirect.lua Outdated Show resolved Hide resolved
apisix/plugins/redirect.lua Outdated Show resolved Hide resolved
Co-authored-by: tzssangglass <tzssangglass@gmail.com>
Co-authored-by: Alex Zhang <tokers@apache.org>
jwrookie and others added 3 commits May 18, 2022 09:25
Co-authored-by: tzssangglass <tzssangglass@gmail.com>
Signed-off-by: Wei Jiang <machowei01@gmail.com>
spacewander
spacewander previously approved these changes May 18, 2022
Signed-off-by: Wei Jiang <machowei01@gmail.com>
end
end
end

Copy link
Contributor

@soulbird soulbird May 18, 2022

Choose a reason for hiding this comment

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

Personally, I feel that this code is too inelegant. Suggest:

local function get_port(attr)
    local port
    if attr then
        port = attr.https.port
    end

    if port then
        return port
    end

    local local_conf = core.config.local_conf()
    local ssl = core.table.try_read_attr(local_conf, "apisix", "ssl")
    if not ssl or ssl["enable"] then
        return port
    end

    port = ssl["listen_port"]
    if port then
        return port
    end

    local ports = ssl["listen"]
    if ports and #ports > 0 then
        local idx = math_random(1, #ports)
        port = ports[idx]
        if type(port) == "table" then
            port = port.port
        end
    end

    return port
end

local ret_port = get_port(attr)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -148,7 +150,30 @@ function _M.rewrite(conf, ctx)
core.log.info("plugin rewrite phase, conf: ", core.json.delay_encode(conf))

local ret_code = conf.ret_code
local ret_port = tonumber(ctx.var["var_x_forwarded_port"])
local ret_port = nil
local attr = plugin.plugin_attr(plugin_name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Where did you define the variable plugin_name?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

here:

local plugin_name = "redirect"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Was defined before :)

Signed-off-by: Wei Jiang <machowei01@gmail.com>
@spacewander
Copy link
Member

Let's merge master to make CI pass.

@jwrookie jwrookie requested a review from tokers May 18, 2022 04:38
@spacewander spacewander merged commit 435565d into apache:master May 18, 2022
spacewander added a commit that referenced this pull request May 18, 2022
Signed-off-by: Wei Jiang <machowei01@gmail.com>

Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
Co-authored-by: tzssangglass <tzssangglass@gmail.com>
Co-authored-by: Alex Zhang <tokers@apache.org>
Liu-Junlin pushed a commit to Liu-Junlin/apisix that referenced this pull request May 20, 2022
Signed-off-by: Wei Jiang <machowei01@gmail.com>

Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
Co-authored-by: tzssangglass <tzssangglass@gmail.com>
Co-authored-by: Alex Zhang <tokers@apache.org>
hongbinhsu pushed a commit to fitphp/apix that referenced this pull request May 24, 2022
* upsgrteam/master: (351 commits)
  fix(proxy-cache): bypass when method mismatch cache_method (apache#7111)
  chore(script): support to install dependencies under arm64 (apache#7091)
  chore(ci): use the latest build script for apisix-base (apache#7090)
  fix(batch-requests): ignore "unix:" in the configuration (apache#7106)
  fix: install dependencies issues (apache#7092)
  feat(ops): use lua libs to backup config file insteadof shell command (apache#7048)
  test: reduce CI failure caused by flaky tests (apache#7085)
  chore(ci): move set_dns.sh to ci dir (apache#7089)
  feat: release 2.14.0 (apache#7057)
  docs: update "Tracers" Plugins (apache#7086)
  docs: update "Traffic" Plugin docs 3 (apache#7064)
  docs: update "Serverless" Plugins (apache#7076)
  feat(ops): check process running with posix.signal insteadof lsof (apache#7006)
  docs: modify how-to-build filename (apache#7087)
  docs: fix link of hot-reload in docs (apache#7081)
  chore(ci): apt update before install (apache#7080)
  docs: add pubsub develop example for kafka (apache#7059)
  ci: enable rebase in some situation (apache#7074)
  fix: redirect http to https but port not change (apache#7065)
  ci: make it pass under OpenResty 1.21 (apache#7067)
  ...
spacewander added a commit that referenced this pull request Jun 30, 2022
Signed-off-by: Wei Jiang <machowei01@gmail.com>

Co-authored-by: 罗泽轩 <spacewanderlzx@gmail.com>
Co-authored-by: tzssangglass <tzssangglass@gmail.com>
Co-authored-by: Alex Zhang <tokers@apache.org>
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.

bug: redirect http to https but port not change
5 participants