Commit 2a853d2
authored
Support TLS for ingress (#27)
* Swap to caddy and support TLS
* Add logging
* test pass
* check if timing was issue
* Raf style AI review
- Added `os.Remove(d.paths.CaddyPIDFile())` when `waitForAdmin` times out to prevent stale PID files
- Changed `append(redirectRoutes, routes...)` to `slices.Concat(redirectRoutes, routes)` to avoid mutating original slices
- Created `DNSProvider` type with constants `DNSProviderNone`, `DNSProviderCloudflare`, `DNSProviderRoute53`
- Added `ParseDNSProvider()` function that returns an error for unknown providers
- Server now fails to start if an invalid DNS provider is configured
- Updated `tls` field description: "Use with match.port=443 for standard HTTPS"
- Replaced Envoy references with Caddy
- Added `CADDY_STOP_ON_SHUTDOWN` with note to set `false` for production
- Added TLS Ingress section documenting ACME configuration for Cloudflare/Route53
- Added all new environment variables to the table
- Created `waitForProcessExit()` helper that polls every 100ms
- `Stop()` now uses polling instead of `time.Sleep(2 * time.Second)`
- Added debug logging when `Process.Kill()` fails in `Stop()`
- `TestCreateIngress_TLSWithoutACME` verifies error when TLS requested but ACME not configured
- Removed separate `logging` section from Caddy JSON config
- Caddy now writes JSON logs to stderr, captured to `caddy.log` by `daemon.go`
- Log forwarder now tails `CaddyLogFile()` instead of separate `CaddySystemLog()`
- Removed unused `CaddySystemLog()` path method
- `TestGenerateConfig_MixedTLSAndNonTLS` verifies correct behavior with both TLS and non-TLS rules in the same ingress
- Removed unused `ErrCaddyNotRunning`
* More review
Summary of Changes
1. Fixed non-deterministic listenAddrs order (`lib/ingress/config.go`)
- Ports are now collected and sorted before building listen addresses
- Added `TestGenerateConfig_DeterministicOrder` test to verify consistent output
2. Fixed DNS challenge config for Caddy modules (`lib/ingress/config.go`)
- Updated `buildDNSChallengeConfig()` to use correct Caddy DNS module format
- Consolidated propagation settings to apply to all providers:
- `DNS_PROPAGATION_TIMEOUT` - works for both Cloudflare and Route53
- `DNS_RESOLVERS` - custom resolvers for propagation checking
3. Updated Makefile for xcaddy builds
- Changed from downloading pre-built Caddy to building with xcaddy
- Added `build-caddy` and `build-caddy-binaries` targets
- Includes `github.com/caddy-dns/cloudflare` and `github.com/caddy-dns/route53` modules
4. Added test for XDG_DATA_HOME / storage paths (`lib/ingress/config_test.go`)
- `TestGenerateConfig_StoragePath` verifies storage configuration is correct
- Updated `TestGenerateConfig_EmptyIngresses` to check storage section
5. Used slices.Concat for safer append (`lib/ingress/manager.go`)
- Changed `append(existingIngresses, ingress)` to `slices.Concat(existingIngresses, []Ingress{ingress})`
6. Match config path in findCaddyPID (`lib/ingress/daemon.go`)
- Now matches both "caddy run" and the specific config path
- Prevents collision with other Caddy/hypeman instances
7. Reduced admin API timeout to 10s (`lib/ingress/daemon.go`)
- Changed from 30s to 10s for faster failure detection
8. Fixed CaddyStopOnShutdown default (`cmd/api/config/config.go`)
- Default is now `false` (Caddy persists through hypeman restarts)
- Removed "(default: X)" comments from struct fields
9. Updated .env.example
- Added all CADDY_* and ACME_* variables
- Documented all three Route53 auth methods
10. AWS Route53 authentication methods (`lib/ingress/config.go`)
- **Method 1**: Explicit credentials (`AWS_ACCESS_KEY_ID` + `AWS_SECRET_ACCESS_KEY`)
- **Method 2**: Named profile (`AWS_PROFILE`)
- **Method 3**: IAM role/instance profile (leave credentials empty)
* fix build
* error handle port taken
* 404 hint: didn't match hostname
* version check
* Fix caddy shutdown
* Configurable allowed domains
* Domain allow list
* Match ingress by partial id and name
* Use SNI in test
* Tls integration tests passes locally
* Add a few logs
* Use DNS for discovery and support instance name matching
* Accept using sudo for tests, get caching and parallelism
* Remove route53 for now
* Docker login work when run tests with root
* Fix one more spot with unauth'd docker pull
* Review
Summary of Changes
1. Wildcard & Pattern Matching Logic (`lib/ingress/config.go`)
- Added handling for global wildcard `*` pattern that matches any domain
- Added comprehensive documentation explaining wildcard behavior
- Restructured code for clarity with detailed comments
2. Edge Case Tests (`lib/ingress/config_test.go`)
- Added 12 new test cases covering:
- Global `*` wildcard matching
- Single-char subdomains
- Hyphenated and numeric subdomains
- Empty prefix handling
- Apex domain + wildcard combination
- Empty patterns in lists
3. DNS Provider Constants (`lib/ingress/config.go`)
- Added `caddyProviderCloudflare` constant for Caddy module name
- Added `SupportedDNSProviders()` helper function for future-proof error messages
- Updated `ParseDNSProvider()` to use dynamic error message
- Updated `buildDNSChallengeConfig()` to use constant and log warnings for unknown providers
4. Context Propagation (`lib/dns/server.go`)
- Added `ctx` field to Server struct to store base context
- Store context from `Start()` for use in resolver calls
- Updated `handleAQuery()` to use stored context instead of `context.Background()`
- Improved AAAA comment explaining intentional empty response behavior
5. Duration Validation (`lib/providers/providers.go`)
- Added validation for `DNS_PROPAGATION_TIMEOUT` at startup
- Validates Go duration format (e.g., `2m`, `120s`, `1h`)
- Provides helpful error message with expected format examples
6. Consistency Improvements
- **`lib/ingress/manager.go`**: Replaced slice comparison with `strings.HasPrefix()` for ID prefix matching
- **`lib/ingress/daemon.go`**: Added documented constants for polling intervals (`adminPollInterval`, `processExitPollInterval`)
7. TLS Documentation (`lib/ingress/README.md`)
- Expanded TLS/HTTPS section with:
- TLS requirements section
- Detailed `TLS_ALLOWED_DOMAINS` documentation with pattern matching table
- Wildcard behavior explanation
- Example configurations
- Warning scenarios documentation
- Updated ACME/TLS Settings table with new variables
* Address review comments
1. Removed Route53 Documentation (.env.example)
- Removed the AWS Route53 DNS provider documentation section since it's not actually implemented
- Updated ACME_DNS_PROVIDER comment to only mention cloudflare
2. Changed Default Ports to 0 for Random Assignment
- lib/dns/server.go: Changed DefaultPort from 5353 to 0 with a comment explaining this prevents conflicts on shared dev machines
- cmd/api/config/config.go: Changed CADDY_ADMIN_PORT default from 2019 to 0 with an explanatory comment
- Updated .env.example to reflect the new default
3. Fixed DNS Server Context Handling (lib/dns/server.go)
- Removed the stored ctx field from the Server struct
- Added a new resolverTimeout constant (5 seconds)
- Updated handleAQuery to create a fresh context.Background() with timeout for each DNS query
- This ensures DNS queries don't fail if the parent context is cancelled during shutdown
4. Added Comment in lib/ingress/daemon.go
- Added a comment explaining why context.Background() is intentionally used for the startup wait (to ensure it isn't cancelled if the parent context times out)
5. Simplified Test Code (lib/instances/manager_test.go)
- Removed the confusing if envPath := ...; true construct
- Simplified to just the directory walk loop for loading .env files
6. Added Startup Warning (lib/ingress/manager.go)
- Added a check in Initialize() that warns if any existing TLS ingress has a hostname not in the allowed domains list
- Logs the ingress name, hostname, and allowed domains for easier debugging1 parent 71e4964 commit 2a853d2
File tree
35 files changed
+3541
-1422
lines changed- .github/workflows
- cmd/api
- api
- config
- lib
- dns
- ingress
- instances
- logger
- oapi
- paths
- providers
35 files changed
+3541
-1422
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
38 | 48 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
33 | 38 | | |
34 | 39 | | |
35 | 40 | | |
| |||
49 | 54 | | |
50 | 55 | | |
51 | 56 | | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
64 | 97 | | |
65 | 98 | | |
66 | 99 | | |
| |||
107 | 140 | | |
108 | 141 | | |
109 | 142 | | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
116 | 157 | | |
117 | 158 | | |
118 | 159 | | |
| |||
121 | 162 | | |
122 | 163 | | |
123 | 164 | | |
124 | | - | |
| 165 | + | |
125 | 166 | | |
126 | 167 | | |
127 | 168 | | |
| |||
132 | 173 | | |
133 | 174 | | |
134 | 175 | | |
135 | | - | |
| 176 | + | |
136 | 177 | | |
137 | 178 | | |
138 | | - | |
139 | | - | |
| 179 | + | |
140 | 180 | | |
141 | 181 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
| 182 | + | |
156 | 183 | | |
157 | 184 | | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
| 185 | + | |
166 | 186 | | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
| 187 | + | |
174 | 188 | | |
175 | 189 | | |
176 | 190 | | |
| |||
189 | 203 | | |
190 | 204 | | |
191 | 205 | | |
192 | | - | |
| 206 | + | |
193 | 207 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
106 | 113 | | |
107 | 114 | | |
108 | 115 | | |
| |||
111 | 118 | | |
112 | 119 | | |
113 | 120 | | |
114 | | - | |
| 121 | + | |
115 | 122 | | |
116 | 123 | | |
117 | 124 | | |
| |||
144 | 151 | | |
145 | 152 | | |
146 | 153 | | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
147 | 187 | | |
148 | 188 | | |
149 | 189 | | |
| |||
0 commit comments