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

vars: Allow overriding http.auth.user.id in replacer as a special case #6108

Merged
merged 2 commits into from
Mar 5, 2024

Conversation

francislavoie
Copy link
Member

@francislavoie francislavoie commented Feb 15, 2024

Context: https://caddy.community/t/logging-a-response-header-field-in-caddy-reverse-proxy-without-returning-to-client/22758

I was thinking, it would probably be useful to allow users to fill http.auth.user.id for themselves using vars, because then it would show up in the access logs in the dedicated user_id field we already have.

Example config:

:8881 {
    log
    header -X-User-Id
    reverse_proxy localhost:8883 {
        handle_response {
            vars http.auth.user.id {rp.header.X-User-Id}
            copy_response
        }
    }
}

:8883 {
    header X-User-Id foobar
    respond "yo"
}

Example log, notice user_id is filled:

2024/02/15 11:30:00.866	INFO	http.log.access	handled request	{
	"request": {"remote_ip": "127.0.0.1", "remote_port": "46870", "client_ip": "127.0.0.1", "proto": "HTTP/1.1", "method": "GET", "host": "localhost:8881", "uri": "/", "headers": {"User-Agent": ["curl/7.81.0"], "Accept": ["*/*"]}},
	"bytes_read": 0,
	"user_id": "foobar",
	"duration": 0.001003354,
	"size": 2,
	"status": 200,
	"resp_headers": {"Content-Length": ["2"], "Content-Type": ["text/plain; charset=utf-8"], "Date": ["Thu, 15 Feb 2024 11:30:00 GMT"], "Server": ["Caddy", "Caddy"]}
}

@francislavoie francislavoie added the feature ⚙️ New feature or request label Feb 15, 2024
@francislavoie francislavoie added this to the v2.8.0 milestone Feb 15, 2024
@francislavoie francislavoie requested a review from mholt February 15, 2024 11:35
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

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

I don't love special cases, but the benefit is compelling... let's try it :)

@mholt mholt enabled auto-merge (squash) March 5, 2024 22:20
@mholt mholt merged commit 5ed8689 into master Mar 5, 2024
24 of 25 checks passed
@mholt mholt deleted the vars-user-id branch March 5, 2024 22:25
@mholt
Copy link
Member

mholt commented Mar 5, 2024

Huh, I'm having trouble getting the s390x tests to pass here. @francislavoie --- must be flaky, because they passed for the auto-merge.

@mohammed90
Copy link
Member

Huh, I'm having trouble getting the s390x tests to pass here. @francislavoie --- must be flaky, because they passed for the auto-merge.

My fault. Will fix it in a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants