Skip to content

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

Merged
spacewander merged 13 commits into
apache:masterfrom
jwrookie:fix/http_to_https
May 18, 2022
Merged

fix: redirect http to https but port not change#7065
spacewander merged 13 commits into
apache:masterfrom
jwrookie:fix/http_to_https

Conversation

@jwrookie

@jwrookie jwrookie commented May 17, 2022

Copy link
Copy Markdown
Contributor

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)

Comment thread conf/config-default.yaml Outdated
connect: 60s
read: 60s
send: 60s
redirect_https_port: 8443 # the default port for use by HTTP redirects to HTTPS

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 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
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/redirect.lua Outdated
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
Copy Markdown
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
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 t/plugin/redirect.t
--- yaml_config
apisix:
ssl:
enable: null

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 add two more test:

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

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 docs/en/latest/plugins/redirect.md Outdated
Comment thread docs/zh/latest/plugins/redirect.md Outdated
spacewander
spacewander previously approved these changes May 17, 2022
Comment thread docs/en/latest/plugins/redirect.md Outdated

* 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
Copy Markdown
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
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 docs/zh/latest/plugins/redirect.md Outdated

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

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.

ditto

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 t/plugin/redirect.t Outdated


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

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.

ditto

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/redirect.lua Outdated
Comment thread apisix/plugins/redirect.lua Outdated
Comment thread docs/en/latest/plugins/redirect.md Outdated
Co-authored-by: tzssangglass <[email protected]>
Co-authored-by: Alex Zhang <[email protected]>
spacewander
spacewander previously approved these changes May 18, 2022
Signed-off-by: Wei Jiang <[email protected]>
end
end
end

@soulbird soulbird May 18, 2022

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.

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
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/redirect.lua Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 <[email protected]>
@spacewander

Copy link
Copy Markdown
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 <[email protected]>

Co-authored-by: 罗泽轩 <[email protected]>
Co-authored-by: tzssangglass <[email protected]>
Co-authored-by: Alex Zhang <[email protected]>
Liu-Junlin pushed a commit to Liu-Junlin/apisix that referenced this pull request May 20, 2022
Signed-off-by: Wei Jiang <[email protected]>

Co-authored-by: 罗泽轩 <[email protected]>
Co-authored-by: tzssangglass <[email protected]>
Co-authored-by: Alex Zhang <[email protected]>
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 <[email protected]>

Co-authored-by: 罗泽轩 <[email protected]>
Co-authored-by: tzssangglass <[email protected]>
Co-authored-by: Alex Zhang <[email protected]>
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