Skip to content

fix(traefik): copy all traefik certs from project to router, fixes #8100 - #8108

Closed
rfay wants to merge 7 commits into
ddev:mainfrom
rfay:20260205_multiple_certs
Closed

rfay wants to merge 7 commits into
ddev:mainfrom
rfay:20260205_multiple_certs

Conversation

@rfay

@rfay rfay commented Feb 5, 2026 •

Copy link
Copy Markdown
Member

The Issue

Since DDEV v1.25.0, only certificates named exactly <projectname>.crt and <projectname>.key are copied from .ddev/traefik/certs/ to the router. This prevents users from placing additional certificates (e.g., mTLS CA certs) in the project's traefik certs directory and having them available for use in custom Traefik configurations.

Related future issue: #8047

NOTE

This adds TestTraefikCustomProjectConfig which adds coverage that we didn't have before, and may have timing problems. There's a bit of work trying to make sure that traefik does the right thing, that we wait for it, and that the test has explicit check for when traefik is ready. I think that the test has uncovered an area of concern that was not previously shown, but it has nothing to do with copying the certs. It's worth being skeptical and chasing this down.

How This PR Solves The Issue

Modified PushGlobalTraefikConfig() in pkg/ddevapp/traefik.go to:

  • Copy ALL .crt and .key files from .ddev/traefik/certs/ instead of only the project-named files
  • Track all copied certificates in expectedCerts map so they won't be removed during cleanup
  • Add debug logging for each copied certificate

This allows users to place any additional certificates (e.g., ca.crt, client.crt, etc.) in .ddev/traefik/certs/ and reference them in custom Traefik configuration files.

Manual Testing Instructions

  1. Create a test project: ddev config --project-type=php
  2. Create a custom certificate in .ddev/traefik/certs/, e.g., .ddev/traefik/certs/custom-ca.crt
  3. Start the project: ddev start
  4. Verify the cert is copied to the router: docker exec ddev-router ls /mnt/ddev-global-cache/traefik/certs/
  5. Confirm custom-ca.crt appears in the list
  6. Create a custom Traefik config that references the cert
  7. Verify it's accessible in the router container

Automated Testing Overview

  • Existing TestTraefikSimple test passes - verifies basic cert handling
  • Existing TestCustomGlobalConfig test passes - verifies custom cert usage
  • Adds TestTraefikCustomProjectConfig covering this issue and project traefik config changes

Release/Deployment Notes

This is a bug fix that restores functionality from pre-v1.25.0. Users who need to use additional certificates (e.g., for mTLS) in custom Traefik configurations can now place them in .ddev/traefik/certs/ and they will be mounted into the router container.

@github-actions github-actions Bot added the bugfix label Feb 5, 2026
@github-actions

github-actions Bot commented Feb 5, 2026 •

Copy link
Copy Markdown

@rfay
rfay marked this pull request as ready for review February 5, 2026 16:03
@rfay
rfay requested a review from a team as a code owner February 5, 2026 16:03
@rfay
rfay requested a review from stasadev February 5, 2026 16:03
@raeven-embloom

Copy link
Copy Markdown

As stated in #8100 (comment): I can vouch it works for me without breaking anything else 👍

rfay added a commit to rfay/ddev that referenced this pull request Feb 5, 2026
## The Issue

- Fixes race condition in TestTraefikCustomProjectConfig
- Related to ddev#8100, ddev#8108

When the router is created, `PushGlobalTraefikConfig()` copies configuration
files to the Docker volume but doesn't verify that Traefik has actually
detected and loaded these changes. This causes intermittent test failures
where test code expects routing to work immediately, but Traefik's file
watcher hasn't processed the changes yet (typically 0.5-2 seconds delay).

The "router already running" path called `ClearRouterHealthcheck()` after
pushing config, but the "router creation" path did not verify config loading.

## How This PR Solves The Issue

Consolidated config verification into a single `ClearRouterHealthcheck()` call
after `ContainerWait()` that applies to both code paths:

- Router creation: Push config → start container → wait for healthy → verify config loaded
- Router already running: Push config → wait (returns immediately) → verify config loaded

The healthcheck works even when file counts don't change (e.g., updating
existing certificates) because it queries Traefik's live API state
(`/api/http/routers`) rather than counting files.

## Manual Testing Instructions

1. Build DDEV: `make`
2. Run the test multiple times:
   ```bash
   for i in {1..5}; do
     go test -v -run TestTraefikCustomProjectConfig ./pkg/ddevapp || break
   done
   ```
3. Test should pass consistently

## Automated Testing Overview

- `TestTraefikCustomProjectConfig` now passes consistently
- Verifies custom Traefik configuration is loaded before HTTP redirect test
- No new tests needed - fixes race condition in existing test

## Release/Deployment Notes

- Minimal user impact: Adds ~1-2 seconds to router operations for verification
- Prevents silent failures where router appears healthy but configs aren't loaded
- Better error messages if configuration fails to load

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
rfay and others added 5 commits February 5, 2026 13:05
## The Issue

- Fixes ddev#8100

Since DDEV v1.25.0, only certificates named exactly `<projectname>.crt` and
`<projectname>.key` are copied from `.ddev/traefik/certs/` to the router.
This prevents users from placing additional certificates (e.g., mTLS CA certs)
in the project's traefik certs directory and having them available for use in
custom Traefik configurations.

## How This PR Solves The Issue

Modified `PushGlobalTraefikConfig()` in `pkg/ddevapp/traefik.go` to:
- Copy ALL `.crt` and `.key` files from `.ddev/traefik/certs/` instead of
  only the project-named files
- Track all copied certificates in `expectedCerts` map so they won't be
  removed during cleanup
- Add debug logging for each copied certificate

This allows users to place any additional certificates (e.g., `ca.crt`,
`client.crt`, etc.) in `.ddev/traefik/certs/` and reference them in custom
Traefik configuration files.

## Manual Testing Instructions

1. Create a test project: `ddev config --project-type=php`
2. Create a custom certificate in `.ddev/traefik/certs/`, e.g.,
   `.ddev/traefik/certs/custom-ca.crt`
3. Start the project: `ddev start`
4. Verify the cert is copied to the router:
   `docker exec ddev-router ls /mnt/ddev-global-cache/traefik/certs/`
5. Confirm `custom-ca.crt` appears in the list
6. Create a custom Traefik config that references the cert
7. Verify it's accessible in the router container

## Automated Testing Overview

- Existing `TestTraefikSimple` test passes - verifies basic cert handling
- Existing `TestCustomGlobalConfig` test passes - verifies custom cert usage
- No new tests added since existing integration tests cover the cert copying
  functionality

## Release/Deployment Notes

This is a bug fix that restores functionality from pre-v1.25.0. Users who need
to use additional certificates (e.g., for mTLS) in custom Traefik
configurations can now place them in `.ddev/traefik/certs/` and they will be
mounted into the router container.

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
…efik config and certs

Adds test coverage for:
- Custom traefik config files without #ddev-generated signature
- Multiple certificate files in .ddev/traefik/certs/
- Verification that custom config is preserved across restarts
- Verification that all cert files are copied to router volume
- Functional test of HTTP->HTTPS redirect middleware to ensure custom config works

The test modifies the generated config to enable the redirectHttps middleware
and verifies that HTTP requests are properly redirected to HTTPS.

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
## The Issue

- Fixes race condition in TestTraefikCustomProjectConfig
- Related to ddev#8100, ddev#8108

When the router is created, `PushGlobalTraefikConfig()` copies configuration
files to the Docker volume but doesn't verify that Traefik has actually
detected and loaded these changes. This causes intermittent test failures
where test code expects routing to work immediately, but Traefik's file
watcher hasn't processed the changes yet (typically 0.5-2 seconds delay).

The "router already running" path called `ClearRouterHealthcheck()` after
pushing config, but the "router creation" path did not verify config loading.

## How This PR Solves The Issue

Consolidated config verification into a single `ClearRouterHealthcheck()` call
after `ContainerWait()` that applies to both code paths:

- Router creation: Push config → start container → wait for healthy → verify config loaded
- Router already running: Push config → wait (returns immediately) → verify config loaded

The healthcheck works even when file counts don't change (e.g., updating
existing certificates) because it queries Traefik's live API state
(`/api/http/routers`) rather than counting files.

## Manual Testing Instructions

1. Build DDEV: `make`
2. Run the test multiple times:
   ```bash
   for i in {1..5}; do
     go test -v -run TestTraefikCustomProjectConfig ./pkg/ddevapp || break
   done
   ```
3. Test should pass consistently

## Automated Testing Overview

- `TestTraefikCustomProjectConfig` now passes consistently
- Verifies custom Traefik configuration is loaded before HTTP redirect test
- No new tests needed - fixes race condition in existing test

## Release/Deployment Notes

- Minimal user impact: Adds ~1-2 seconds to router operations for verification
- Prevents silent failures where router appears healthy but configs aren't loaded
- Better error messages if configuration fails to load

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
…ix intermittent test failures

The issue: Traefik's fsnotify may not reliably detect configuration changes when
files are copied into a Docker volume by a temporary container. This caused
intermittent failures in TestTraefikCustomProjectConfig where the HTTP redirect
middleware wasn't being applied because Traefik hadn't reloaded the config.

Root cause: When CopyIntoVolume copies files, it:
1. Creates a temporary container
2. Tars files and copies them via docker cp
3. Extracts them in the temp container's volume mount
4. Router container watches the same volume

Docker volume fsnotify events may not propagate reliably between different
containers accessing the same volume, even though the files are physically
present.

The fix: After ClearRouterHealthcheck(), explicitly touch a marker file INSIDE
the router container itself in the directory Traefik is watching. This guarantees
Traefik's fsnotify sees a filesystem change and triggers a reload, even when the
actual config files didn't trigger fsnotify.

Key details:
- Marker file must be in /mnt/ddev-global-cache/traefik/config/ (the watched dir)
- Touch happens inside router container (same container that's watching)
- Reduced wait time from 2.5s to 1s since we're actively triggering reload
- Works around fsnotify limitations in Docker volume scenarios

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@rfay
rfay force-pushed the 20260205_multiple_certs branch from cdcf494 to 2df5823 Compare February 5, 2026 20:32
…ustomProjectConfig

The marker file approach triggers Traefik's fsnotify, but doesn't wait long enough for Traefik to actually process the configuration (providersThrottleDuration default is 2s, we only wait 1s).

Rather than increasing the general wait time (which affects all users), add test-specific verification that polls Traefik's API to confirm the expected middleware is actually loaded.

Changes:
- Add waitForTraefikRouterMiddleware() helper that polls /api/http/routers/{name}@file
- Query for specific router and check its middleware list
- Poll for up to 10 seconds with 200ms interval
- Handle Traefik's provider suffix (@file) in middleware names

This approach:
- Keeps marker file + 1s wait for normal users (fast, good enough)
- Gives test reliable verification by checking actual Traefik state
- Doesn't slow down production code paths

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@rfay rfay changed the title fix: copy all traefik certs from project to router, fixes #8100 fix(traefik): copy all traefik certs from project to router, fixes #8100 Feb 5, 2026
@rfay

rfay commented Feb 6, 2026 •

Copy link
Copy Markdown
Member Author

Add instrumentation to diagnose why the test fails in CI environments:

1. Show config file content on host before restart
2. Show config file content in router volume after restart
3. Compare to detect if files are being copied correctly
4. Show Traefik configuration logs
5. On failure, dump all routers and overview from Traefik API

This will help us understand:
- Is the modified config being copied to the volume?
- Is Traefik seeing the file changes?
- Is Traefik rejecting the config for some reason?
- What does Traefik think is in its configuration?

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@rfay

rfay commented Feb 7, 2026

Copy link
Copy Markdown
Member Author

Closing in favor of #8116 which takes a simpler approach by copying directly to the router container, eliminating the cross-container fsnotify
propagation issue rather than trying to work around it.

@raeven-embloom Your situation should already work in HEAD, but there was some intermittent behavior discovered in testing on some docker providers that would likely never have affected real users, but we sure want them to be gone.

The commit that did what you actually wanted was the first commit in #8113 , which is already in HEAD

@rfay rfay closed this Feb 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Additional certificate (for mTLS) is no longer mounted in ddev-router since v1.25.0

2 participants