Snippet Issue, works outside snippet

1. Caddy version (caddy version):

v2.4.0-beta.2

2. How I run Caddy:

a. System environment:

Docker

b. Command:

N/A - Docker

c. Service/unit/compose file:

See above, use docker.

d. My complete Caddyfile or JSON config:

	default_sni dulanic.com
	acme_ca https://acme-v02.api.letsencrypt.org/directory
	email {$ACMEEMAIL}
	# debug
}
(proxyheaders) {
	flush_interval -1
	header_up X-Real-IP {http.request.header.CF-Connecting-IP}
	header_up X-Forwarded-For {http.request.header.CF-Connecting-IP}
	header_up X-Forwarded-Host {hostport}
}
(main) {
	tls {
		on_demand
		dns cloudflare {env.CLOUDFLARE_API_TOKEN}
	}
	header {
		Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
		Content-Security-Policy "upgrade-insecure-requests"
		X-Frame-Options "SAMEORIGIN"
		X-Content-Type-Options "nosniff"
		X-XSS-Protection "1; mode=block"
		Referrer-Policy "strict-origin-when-cross-origin"
	}
	log {
		output file /logs/caddy.json {
			roll_size 100MiB
			roll_keep 20
			roll_keep_for 365d
		}
		level INFO
		format filter {
			wrap json
			fields {
				size delete
				duration delete
				request>headers>Sec-Fetch-Site delete
				request>headers>Sec-Fetch-Mode delete
				request>headers>Connection delete
				request>headers>Authorization delete
				request>headers>Cf-Ray delete
				request>headers>Accept delete
				request>headers>Accept-Encoding delete
				request>headers>Cookie delete
				request>headers>Cf-Request-Id delete
				request>headers>Sec-Fetch-Site delete
				request>headers>Sec-Fetch-User delete
				request>headers>Content-Length delete
				request>headers>Sec-Fetch-Dest delete
				request>headers>Cdn-Loop delete
				request>headers>Cf-Visitor delete
				request>headers>Accept-Language delete
				request>headers>X-Forwarded-Proto delete
				request>headers>Sec-Ch-Ua delete
				request>headers>Dnt delete
				request>tls delete
				resp_headers delete
			}
		}
	}
}
(jwt_protected) {
	route {
		jwt {
			trusted_tokens {
				static_secret {
					token_secret {env.token_shared_secret}
				}
			}
			allow roles verified
			auth_url https://auth.dulanic.com/auth/
		}
	}
}
https://subdomain.dulanic.com {
	import main
	import jwt_protected
	reverse_proxy test:4000 {
		import proxyheaders
	}
}

3. The problem I’m having:

This code works placed directly before the reverse_proxy, but not as import jwt_protected. I’d prefer it to be a import as I want to reuuse the snippet in multiple subdomains.

	route {
		jwt {
			trusted_tokens {
				static_secret {
					token_secret {env.token_shared_secret}
				}
			}
			allow roles verified
			auth_url https://auth.dulanic.com/auth/
		}
	}

4. Error messages and/or full log output:

{"level":"error","ts":1619101882.4429889,"logger":"http.handlers.authentication","msg":"auth provider returned error","provider":"jwt","error":"jwt-87: token keys and secrets must be defined either via environment variables or via token_ configuration element"}

5. What I already tried:

It works if I put the snippet directly under the path, but not as a snippet. I worked /w @greenpau here. https://github.com/greenpau/caddy-auth-portal/issues/122#issuecomment-824913704

6. Links to relevant resources:

I’m confused. Could you compare the adapted JSON to see where it differs?

Run caddy adapt --pretty on your Caddyfile and compare.

Looks like it isn’t pulling in the token when it’s in the snippet.

This is the diff between the 2, just moved to the snippet.

1 Like

Interesting, I can replicate the issue.

I think this was a side effect of this feature:

I think {env.*} is being replaced here when it shouldn’t be, so if you don’t have the environment variable set when you’re adapting/running Caddy, import replaces it with an empty string. I’ll take a look at fixing it.

1 Like

Easy enough:

1 Like

That was a fast fix :grinning:

1 Like

So I’m not sure that fixed it. It is passing the env var name instyead of the value of the env value. This is from the autosave.json, all other env variables show the value except this one.

As I wrote on the PR, that’s working as intended:

This topic was automatically closed after 30 days. New replies are no longer allowed.