Skip to content

Support certificate auto-rotation in Kestrel #32351

Description

@aelij

Is your feature request related to a problem? Please describe.

In Kubernetes, certificates are mounted as secret volumes, which can be configured to update automatically when the cert is rotated (e.g. from Key Vault). To achieve auto-rotation in Kestrel today, we need to hook up ServerCertificateSelector and listen to file changes (e.g. using IFileProvider.Watch).

Describe the solution you'd like

Add a HttpsConnectionAdapterOptions.ServerCertificatePath property that would watch for file changes.

Activity

  1. blowdart commented on May 3, 2021

    @blowdart
    Contributor

    This is a feature we're considering, above and beyond kubernetes. We're not at the design stage yet, but it's on the radar

  2. added this to the Backlog milestone on May 3, 2021
  3. davidfowl commented on May 5, 2021

    @davidfowl
    Member

    @blowdart this is something I think we should support natively. We did work in .NET 5 to make Kestrel respect configuration reload but that doesn't work well for things in configuration that change without configuration itself changing.

    This is going to affect YARP as well @Tratcher.

    @aelij Are you using certmgr?

  4. removed this from the Backlog milestone on May 5, 2021
  5. aelij commented on May 5, 2021

    @aelij
    ContributorAuthor

    @davidfowl No, we're planning on using the new Secrets Store CSI Driver once it's out of preview, which natively supports auto-rotation.

  6. self-assigned this
    on May 5, 2021
  7. adityamandaleeka commented on Sep 1, 2021

    @adityamandaleeka
    Member

    @blowdart Removing this from 6. Please move it back if you think it should get done.

  8. 78 remaining items

  9. amcasey commented on Aug 21, 2023

    @amcasey
    Member

    There's little advantage to dropping the mtime check without also forcing polling since FileSystemWatcher won't resolve symlinks. I guess the chief advantage would be being able to respect the polling environment variables.

    While we're talking about runtime improvements, it would be nice if the non-polling watcher just didn't send duplicate events. 😉

  10. jozkee commented on Aug 21, 2023

    @jozkee
    Member

    it would be nice if the non-polling watcher just didn't send duplicate events. 😉

    dotnet/runtime#24079

  11. amcasey commented on Aug 21, 2023

    @amcasey
    Member

    Regardless of which path we take, I'll need to update the code to stop instantiating PhysicalFileWatcher directly

    I made this change here. Unfortunately, I don't think we'll be able to take it for 8.0 because it will prevent us from using polling for specific files (the file watcher is shared by several consumers). Not having it is an abstraction violation, but fixing it doesn't fix the abstraction violation because we still pass the path directly to X509Certificate, rather than retrieving a stream from the file provider.

  12. added 2 commits that reference this issue on Aug 22, 2023
    c3aa792
    76bd8bb
  13. amcasey commented on Aug 22, 2023

    @amcasey
    Member

    PR for polling and ignoring mtime: #50251

  14. amcasey commented on Aug 22, 2023

    @amcasey
    Member

    It took a bit of massaging, but I got @aelij's repro script working (he was naively taking for granted that I would know when and how to log in to azure 😆). With #50251, I'm seeing

    trce: Microsoft.AspNetCore.Server.Kestrel.Core.Internal.CertificatePathWatcher[15]
          Flagged 1 observers of '/certs/cert1.crt' as changed.
    ...
    info: Microsoft.AspNetCore.Server.Kestrel[0]
          Config changed. Stopping the following endpoints: 'https://*:5001'
    info: Microsoft.AspNetCore.Server.Kestrel[0]
          Config changed. Starting the following endpoints: 'https://*:5001'
    

    🥳

  15. amcasey commented on Aug 22, 2023

    @amcasey
    Member

    @aelij I notice that your repro has the certs in /certs, which isn't under the /app, the content root. Is that the way things are normally laid out or was that just simpler for a toy repro? I don't believe it will cause a problem, but it suggests that a "fix" like #50246 would break people.

  16. pinkfloydx33 commented on Aug 23, 2023

    @pinkfloydx33

    I don't think there's "a usual". For example at my job by convention we put things into /app/config, /app/secrets and /app/certs. But it could've been anywhere. I know some folks who just mount to /config while the program still runs from /app.

    IOW mounting k8s certs within the content root is certainly possible but is no way guaranteed. The mount point may be dictated by devops, and/or those responsible for managing configuration and might not even be a developer concern.

  17. aelij commented on Aug 23, 2023

    @aelij
    ContributorAuthor

    Yes, there should be no relation to the app root.

  18. amcasey commented on Aug 24, 2023

    @amcasey
    Member

    It's in! @aelij I've already validated using your sample project (thanks again!), but it would be great if you could confirm that an upcoming nightly works for you.

  19. amcasey commented on Aug 25, 2023

    @amcasey
    Member

    Not sure why merging #50251 didn't close this...

  20. aelij commented on Aug 27, 2023

    @aelij
    ContributorAuthor

    I can confirm it's working. Thanks @amcasey!

  21. ghost locked as resolved and limited conversation to collaborators on Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsfeature-kestrelpartner-impact

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions