Use DateTimeOffset.Now for checking certificate validity#12680
Use DateTimeOffset.Now for checking certificate validity#12680danegsta merged 4 commits intodotnet:mainfrom
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 12680Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 12680" |
There was a problem hiding this comment.
Pull Request Overview
This PR changes the time value used for certificate validation from DateTime.UtcNow to DateTimeOffset.Now. The purpose is to align with the proper type for comparing against X509Certificate2's NotBefore and NotAfter properties.
Key Changes
- Updated the
nowvariable initialization inDeveloperCertificateServicefromDateTime.UtcNowtoDateTimeOffset.Now
|
@mitchdenny @JamesNK can one or both of you try clearing your dev certs ( We ran into a report of what seems to be a timezone related issue determining if the certificate is valid; this PR ensures we use the same DateTimeOffset.Now value that ASP.NET uses to generate and check the not before/not after times for the dev cert. |
| using var store = new X509Store(StoreName.My, StoreLocation.CurrentUser); | ||
| store.Open(OpenFlags.ReadOnly); | ||
| var now = DateTime.UtcNow; | ||
| var now = DateTimeOffset.Now; |
There was a problem hiding this comment.
Add a comment saying why DateTimeOffset.Now is used. Link to eqivilent code in ASP.NET Core that's being followed.
Are there unit tests for this type? You'd probably need to pass in a TimeProvider to avoid changing time from interfering.
There was a problem hiding this comment.
We don't currently have unit tests that validate the certificate not before/not after logic; ideally we'd be able to borrow some of the certificate creation logic from ASP.NET to validate properly. I've move the comment below this line up and added a reference to why we're using DateTimeOffset.Now.
| // Configure required environment variables for custom certificate trust when running as an executable. | ||
| // TODO: Make CertificateTrustScope.System the default once we're able to validate that certificates are valid for OpenSSL. Otherwise we potentially add invalid certificates to the bundle which causes OpenSSL to error. | ||
| resourceBuilder | ||
| .WithCertificateTrustScope(CertificateTrustScope.System) |
There was a problem hiding this comment.
We ran into an issue with certificates that the certificate store and X509Certificate2 consider valid on Windows, but OpenSSL doesn't. This caused OpenSSL errors trying to validate HTTPS connections if they were present in the cert bundle for Python. DCP has the ability to do the necessary validation of a certificate to ensure it won't cause errors, but we'll need to add a new property for executables to be able to take advantage of it.
Until then, I'm switching Python back to the default trust scope of Append. That'll let us trust the OTEL connection, it just won't enable trusting the dev cert over arbitrary http clients.
|
There'll need to be follow up work to resolve the invalid certificates issue: #12693 |
|
/backport to release/13.0 |
|
Started backporting to release/13.0: https://github.com/dotnet/aspire/actions/runs/19114831650 |
|
@danegsta backporting to "release/13.0" failed, the patch most likely resulted in conflicts: $ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch
Applying: Use DateTimeOffset.Now for checking certificate validity
Applying: Avoid loading all system certificates until we can validate them via DCP
Using index info to reconstruct a base tree...
M src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs
CONFLICT (content): Merge conflict in src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0002 Avoid loading all system certificates until we can validate them via DCP
Error: The process '/usr/bin/git' failed with exit code 128Please backport manually! |
|
/backport to release/13.0 |
|
Started backporting to release/13.0: https://github.com/dotnet/aspire/actions/runs/19116888141 |
|
@danegsta backporting to "release/13.0" failed, the patch most likely resulted in conflicts: $ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch
Applying: Use DateTimeOffset.Now for checking certificate validity
Applying: Avoid loading all system certificates until we can validate them via DCP
Using index info to reconstruct a base tree...
M src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs
CONFLICT (content): Merge conflict in src/Aspire.Hosting.Python/PythonAppResourceBuilderExtensions.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0002 Avoid loading all system certificates until we can validate them via DCP
Error: The process '/usr/bin/git' failed with exit code 128Please backport manually! |
* Use DateTimeOffset.Now for checking certificate validity * Avoid loading all system certificates until we can validate them via DCP * Ensure comment covers usage of DateTimeOffset.Now as well
Description
ASP.NET uses
DateTimeOffset.Nowto compare the current time against certificate not before/not after fields. Update our check to do the same. Otherwise we're comparing aDateTimevalue (in UTC) against aDateTimeOffsetvalue is resulting in theDateTimevalue being incorrectly evaluated as a local time zone value in comparisons.Additionally, the Windows certificate store considers some certificates as valid that OpenSSL doesn't, resulting in possible issues when loading certificates from the store into a language that uses OpenSSL, such as Python or Node. We were previously including all system certificates by default for Python, but this can result in errors on some systems. For now we'll revert to only trusting the developer certificate for OTEL purposes, with follow up work to use DCP to validate certificates for executables as well as containers.
Fixes #12618