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

Overwrite source address with logging #272

Merged
merged 3 commits into from
May 29, 2024
Merged

Conversation

devbugging
Copy link
Contributor

@devbugging devbugging commented May 29, 2024

Description

Allow defining header value used for determining the client source IP. This is useful when the EVM GW is behind a proxy that sets the source IP in a header.
Also add this source IP to logs so we can validate how this behaves on a deployment.


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@devbugging devbugging self-assigned this May 29, 2024
Copy link
Contributor

coderabbitai bot commented May 29, 2024

Walkthrough

The changes involve a significant update to the HTTP server configuration handling. The httpServer struct now includes a config field, and the NewHTTPServer function has been updated to accept a configuration object. Additionally, command-line flags for new configuration options, such as rate limiting and address headers, have been added. These updates streamline configuration management and enhance the server's flexibility and functionality.

Changes

Files Change Summary
api/server.go Added config import, updated httpServer struct to include config field, modified NewHTTPServer function.
bootstrap/bootstrap.go Replaced rpc.DefaultHTTPTimeouts with cfg in startServer function, impacting server initialization.
config/config.go Updated Config struct with RateLimit and AddressHeader fields, added corresponding flags in FromFlags function.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant User
    participant Proxy
    participant HTTPServer
    participant Config

    User->>Proxy: HTTP Request
    Proxy->>HTTPServer: Forward Request with Address Header
    HTTPServer->>Config: Load Configuration
    Config-->>HTTPServer: Provide Config Object
    HTTPServer->>HTTPServer: Overwrite Remote Address
    HTTPServer->>User: Send Response
Loading

Poem

In the code where servers hum,
Configurations now become,
A streamlined dance, a graceful flow,
With headers set and limits low.
Behind the proxy, changes gleam,
A rabbit's dream, a coder's theme. 🐇✨


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 Configration 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.

@devbugging devbugging changed the title Log source IP address Configurable source IP header May 29, 2024
@coderabbitai coderabbitai bot changed the title Configurable source IP header Log source IP address May 29, 2024
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: 2

Outside diff range and nitpick comments (1)
config/config.go (1)

107-108: The new command-line flags for RateLimit and AddressHeader are correctly implemented. Ensure they are documented in the user guide.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 76edbd8 and 7b8bf4c.
Files selected for processing (3)
  • api/server.go (4 hunks)
  • bootstrap/bootstrap.go (2 hunks)
  • config/config.go (2 hunks)
Additional comments not posted (2)
bootstrap/bootstrap.go (1)

203-203: Ensure all calls to NewHTTPServer have been updated to pass the new cfg parameter.

Verification successful

The search results indicate that the NewHTTPServer function is defined in api/server.go and called in bootstrap/bootstrap.go. Let's verify the function definition in api/server.go to ensure it matches the new signature and confirm there are no other calls to NewHTTPServer that might have been missed.


The verification confirms that the NewHTTPServer function in api/server.go has been updated to include the cfg parameter and that there are no other calls to NewHTTPServer that might have been missed.

  • api/server.go:59: The function definition matches the new signature with cfg as a parameter.
  • bootstrap/bootstrap.go:203: The call to NewHTTPServer correctly passes the cfg parameter.
Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `NewHTTPServer` match the new signature.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type go $'NewHTTPServer'

Length of output: 175



Script:

#!/bin/bash
# Verify the function definition of `NewHTTPServer` in `api/server.go`
ast-grep --lang go --pattern $'func NewHTTPServer(logger $_, cfg $_) $_' api/server.go

# Confirm there are no other calls to `NewHTTPServer` that might have been missed
rg --type go $'NewHTTPServer'

Length of output: 1075

api/server.go (1)

52-52: The addition of the config field to the httpServer struct is a crucial change for enhancing configurability.

Comment on lines +67 to +70
// RateLimit requests made by the client identified by IP over any protocol (ws/http).
RateLimit uint64
// Address header used to identified clients, usually set by the proxy
AddressHeader string
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure that the new configuration fields RateLimit and AddressHeader are validated to prevent misconfigurations.

// Add validation for RateLimit and AddressHeader
if cfg.RateLimit < 0 {
    return nil, fmt.Errorf("RateLimit cannot be negative")
}
if cfg.AddressHeader != "" && !strings.Contains(cfg.AddressHeader, "X-") {
    return nil, fmt.Errorf("AddressHeader should start with 'X-'")
}

Comment on lines +234 to +238
// this overwrites the remote address with the header value, this is used when the server is
// behind a proxy, and the true source address is overwritten by proxy, but retained in a header.
if h.config.AddressHeader != "" {
r.RemoteAddr = r.Header.Get(h.config.AddressHeader)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The implementation for overwriting the remote address using the AddressHeader is correct. Consider adding a unit test to ensure this behavior works as expected when the server is behind a proxy.

Would you like me to help by writing a unit test for this functionality?

@devbugging devbugging changed the title Log source IP address Overwrite source address with logging May 29, 2024
Comment on lines +67 to +68
// RateLimit requests made by the client identified by IP over any protocol (ws/http).
RateLimit uint64
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this supposed to be in this PR?
nothing else mentions it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not really, it's needed in the follow up PR #267
Which I will get in as soon as I validate this one.

@devbugging devbugging merged commit 0d5c333 into main May 29, 2024
2 checks passed
@franklywatson franklywatson deleted the gregor/log-source-address branch May 29, 2024 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

2 participants