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

nginx_proxy: Adding support for TCP Proxy Protocol #3274

Merged
merged 9 commits into from
Aug 14, 2024

Conversation

miguelrjim
Copy link
Contributor

@miguelrjim miguelrjim commented Oct 21, 2023

This allows Nginx to support the TCP proxy protocol, this is useful if you still want to TLS terminate on the home-assistant host but you have another entrypoint for all your TLS connections (i.e. traefik is your entrypoint and you redirect from it to home-assistant based on the HTTPS domain being requested, this makes sure that any kind of network request to home-assistant remains encrypted).

What this allows is that home-assistant will know the true ip of the user making requests (making this change because I saw in the logs that there was a failed log-in but the ip pointed to my traefik instance).

Tested this by installing these changes as a local add-on on home-assistant.

Summary by CodeRabbit

  • New Features

    • Introduced support for TCP Proxy Protocol, improving handling of TCP traffic.
    • Added real_ip_from configuration option, allowing specification of trusted IP addresses for real IP forwarding.
  • Bug Fixes

    • Enhanced accuracy in logging client IPs through updated proxy settings.
  • Documentation

    • Updated documentation to include new configuration options and their usage.
  • Chores

    • Incremented version number to 3.10.0 and improved configuration management structure for maintainability.

@bvli

This comment was marked as off-topic.

@miguelrjim
Copy link
Contributor Author

Hi @agners, just wondering what would be the next step to get the PR merged?

Copy link
Member

@agners agners left a comment

Choose a reason for hiding this comment

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

I am not very familiar with the various proxy options/configurations, but wouldn't the first option documented in https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol/ to get the originating IP address more generic? (E.g. not using the RealIP module? 🤔 ).

nginx_proxy/config.yaml Outdated Show resolved Hide resolved
nginx_proxy/config.yaml Outdated Show resolved Hide resolved
nginx_proxy/config.yaml Outdated Show resolved Hide resolved
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft March 11, 2024 09:24
@miguelrjim
Copy link
Contributor Author

I am not very familiar with the various proxy options/configurations, but wouldn't the first option documented in https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol/ to get the originating IP address more generic? (E.g. not using the RealIP module? 🤔 ).

Hmm it could be? Tbh i've been using $proxy_protocol_addr as that was being used in a couple of other examples with proxy protocol.

@agners
Copy link
Member

agners commented Mar 12, 2024

Hmm it could be? Tbh i've been using $proxy_protocol_addr as that was being used in a couple of other examples with proxy protocol.

There is also this note:

The Real‑IP modules for HTTP and Stream TCP are not included in NGINX Open Source by default; see Installing NGINX Open Source for details. No extra steps are required for NGINX Plus.

🤔

I am really not an expert in NGINX/Proxy setups. But I think if things can work without the RealIP module, it seems the better approach to me.

nginx_proxy/rootfs/etc/s6-overlay/s6-rc.d/nginx/run Outdated Show resolved Hide resolved
nginx_proxy/config.yaml Outdated Show resolved Hide resolved
nginx_proxy/rootfs/etc/s6-overlay/s6-rc.d/nginx/run Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft April 25, 2024 10:03
@miguelrjim miguelrjim marked this pull request as ready for review April 26, 2024 03:30
@home-assistant home-assistant bot requested a review from agners April 26, 2024 03:30
@agners agners changed the title Adding support for TCP Proxy Protocol nginx_proxy: Adding support for TCP Proxy Protocol Apr 26, 2024
nginx_proxy/config.yaml Outdated Show resolved Hide resolved
@miguelrjim miguelrjim requested a review from agners May 6, 2024 05:22
@miguelrjim
Copy link
Contributor Author

@agners just wondering if we're good to merge the pr after your comments had been taken care of

Copy link
Member

@agners agners left a comment

Choose a reason for hiding this comment

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

Sorry for the late review 😢

I actually wanted to test this, and I finally came around. During testing, I realized one thing: Instead of using the sub config we could just promote the real_ip_from as top level config. The sub-config makes sense if there are multiple configs underneath. The enable property made sense in the past when there were no optional configs.

But today, we have optional configs, so promoting real_ip_from to top level, we can just check if it is set, and if so enable the proxy protocol. Use bashio::config.exists to check if the user has set a value:

if bashio::config.exists 'real_ip_from'; then

The current approach if the user sets proxy protocol to enable, the add-on will only tell at runtime through logs that real_ip_from is missing. With the single config approach, all requirements are checked at config time. So I'd prefer this approach for that reason.

If you prefer the current approach, that is fine too, but we have to remove the default value of real_ip_from, otherwise users which don't use that feature are forced to set a dummy value there.

Also, in any case the top-level config needs translation strings. So please also add name/description strings for the top-level config in translations/en.yaml.

nginx_proxy/config.yaml Outdated Show resolved Hide resolved
nginx_proxy/config.yaml Outdated Show resolved Hide resolved
@miguelrjim
Copy link
Contributor Author

@agners No worries, thanks for taking your time reviewing it!

I've included your suggested changes, I agree this makes it easier and makes sense! I've tested the latest changes locally as well and it's working as intended.

@miguelrjim miguelrjim marked this pull request as ready for review August 4, 2024 19:43
@home-assistant home-assistant bot requested a review from agners August 4, 2024 19:43
Copy link
Contributor

coderabbitai bot commented Aug 4, 2024

Walkthrough

Walkthrough

The changes in the nginx_proxy project introduce version 3.10.0, significantly enhancing proxy server capabilities by adding support for the TCP Proxy Protocol. A key update is the new real_ip_from option for specifying trusted IP addresses, which improves the handling of real client IPs. Additional improvements also include modular configuration options and enhanced logging, collectively increasing the server's efficiency, security, and flexibility.

Changes

Files Change Summary
nginx_proxy/CHANGELOG.md Updated for version 3.10.0, adding TCP Proxy Protocol support while retaining previous version updates.
nginx_proxy/DOCS.md, nginx_proxy/translations/en.yaml Documented the real_ip_from option for specifying trusted IP addresses in NGINX configurations and translations.
nginx_proxy/config.yaml Upgraded version to 3.10.0; added real_ip_from field for trusted IP addresses.
nginx_proxy/rootfs/etc/nginx/nginx.conf.gtpl Enhanced real IP handling with conditional logic based on real_ip_from, allowing for flexible server configurations.
nginx_proxy/rootfs/etc/s6-overlay/s6-rc.d/nginx/run Updated script to manage Nginx configuration dynamically based on the presence of the real_ip_from parameter, improving maintainability and logging.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ac978b5 and 762aa03.

Files selected for processing (6)
  • nginx_proxy/CHANGELOG.md (2 hunks)
  • nginx_proxy/DOCS.md (2 hunks)
  • nginx_proxy/config.yaml (2 hunks)
  • nginx_proxy/rootfs/etc/nginx/nginx.conf.gtpl (3 hunks)
  • nginx_proxy/rootfs/etc/s6-overlay/s6-rc.d/nginx/run (2 hunks)
  • nginx_proxy/translations/en.yaml (1 hunks)
Files skipped from review due to trivial changes (1)
  • nginx_proxy/translations/en.yaml
Files skipped from review as they are similar to previous changes (5)
  • nginx_proxy/CHANGELOG.md
  • nginx_proxy/DOCS.md
  • nginx_proxy/config.yaml
  • nginx_proxy/rootfs/etc/nginx/nginx.conf.gtpl
  • nginx_proxy/rootfs/etc/s6-overlay/s6-rc.d/nginx/run

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (2)
nginx_proxy/DOCS.md (2)

46-46: Consider providing example values or a default value for real_ip_from.

Including example values or a default value can help users understand how to configure this option correctly.

real_ip_from:
  - 192.168.1.0/24
  - 10.0.0.0/8

82-85: Improve the explanation of the real_ip_from option.

To align with the documentation style guide, front the goal in the instructional sentence and use bold for the link text.

- If specified, configures Nginx to use Proxy Protocol to get the Real Ip from an upstream load balancer; [for more information](https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol/).
+ Configure Nginx to use Proxy Protocol to get the Real IP from an upstream load balancer if specified; see **for more information**(https://docs.nginx.com/nginx/admin-guide/load-balancer/using-proxy-protocol/).

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

nginx_proxy/translations/en.yaml Outdated Show resolved Hide resolved
Copy link
Member

@agners agners left a comment

Choose a reason for hiding this comment

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

Thanks for the update. This looks quite good, however I have yet another review/request 🙈

nginx_proxy/config.yaml Outdated Show resolved Hide resolved
nginx_proxy/translations/en.yaml Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft August 8, 2024 16:59
@miguelrjim miguelrjim marked this pull request as ready for review August 11, 2024 18:36
@home-assistant home-assistant bot requested a review from agners August 11, 2024 18:36
@miguelrjim
Copy link
Contributor Author

@agners Made more substantial changes to move replacement logic by taking advantage of templating; tested the changes locally by specifying different values in the ui options (changing the boolean values & text inputs) and the generated nginx.conf was as expected; nginx started successfully and was processing requests as intended, currently using this latest version directly in my home assistant instance.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
nginx_proxy/rootfs/etc/s6-overlay/s6-rc.d/nginx/run (1)

13-16: Template file missing:

The template file /etc/nginx/nginx.conf.gtpl does not exist in the expected location. This will prevent the successful generation of nginx.conf using tempio. Please ensure that the file is present and correctly located in the repository.

Analysis chain

Verify the existence of the template file.

Ensure that the template file /etc/nginx/nginx.conf.gtpl exists and is correctly formatted for use with tempio.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence and format of the template file.

# Test: Check if the template file exists.
if [ -f /etc/nginx/nginx.conf.gtpl ]; then
  echo "Template file exists."
else
  echo "Template file does not exist."
fi

Length of output: 108

Copy link
Member

@agners agners left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the contribution and your patience!

What would be nice if this add-on makes use of the new add-on config directory instead of share for the configuration. But definitely something for another day 😄

@agners agners merged commit cb37a2e into home-assistant:master Aug 14, 2024
10 checks passed
@kelvan
Copy link

kelvan commented Aug 14, 2024

hi, just installed the update and now my nginx is unable to start, it seems this option is not optional?

Missing option 'real_ip_from' in root in NGINX Home Assistant SSL proxy (core_nginx_proxy).

Edit: after adding a random IP, saving and removing it again, it starts up again

@mpreitz
Copy link

mpreitz commented Aug 14, 2024

This feature breaks the funcionality of the plugin. Setting a "real_ip_from" is mandantory, otherwise the plugin won't start. If setting the local network as allowed the plugin starts, but no connection is possible.

Would be nice to read a word in the changelog if breaking changes are part of the release.

@agners
Copy link
Member

agners commented Aug 14, 2024

Making real_ip_from mandatory was not really intended 😢

This should address the issue: #3725.

@mad-tunes
Copy link

Might this be related to some having trouble getting to HA after updating to 3.10.0?
https://discord.com/channels/330944238910963714/672220642099200000/1273278057511387169
Thanks

@mad-tunes
Copy link

mad-tunes commented Aug 14, 2024

It seems that adding real_ip_from: [] to settings yaml, or setting anything in the GUI and then removing it, is letting people at HA thorugh nginx with 3.10.0

@miguelrjim
Copy link
Contributor Author

Sorry! This was definitely not intended to be a breaking change, thanks for the quick fix @agners

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants