Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SSL cache part 2 #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 17, 2025
Merged

SSL cache part 2 #287

merged 6 commits into from
Jan 17, 2025

Conversation

pluknet
Copy link
Contributor

@pluknet pluknet commented Oct 30, 2024

See commits.

@pluknet pluknet requested review from bavshin-f5 and arut October 30, 2024 16:35
Copy link
Member

@bavshin-f5 bavshin-f5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SSL: error message default in object caching API.

LGTM.

SSL: encrypted certificate keys are exempt from object caching.

The change looks fine, but I have some concerns:

  • I wonder if it is possible to restrict the effect to the keys that actually require passphrases. Is it even worth trying?
  • Do we know if the configurations that prompted the SSL cache work use ssl_password_file? Would that regress the configuration load time for them?

SSL: caching certificates and certificate keys with variables.
Upstream: caching certificates and certificate keys with variables.

Not done looking through these, some preliminary comments:

I'm not sure if it's worth introducing both valid and inactivity with a proactive expiration logic. If I understand the intention, open file cache has both to avoid wasting a limited resource — file descriptors. Here we can allow keeping a stale cert in memory to simplify the implementation.

@pluknet
Copy link
Contributor Author

pluknet commented Nov 19, 2024

SSL: error message default in object caching API.

LGTM.

Actually, it doesn't belong to this series and should be fixed separately, preferably prior to the next release.
Currently this is the reason for potential segfaults, see bad malloc reports.
Moved to #333.

@Maryna-f5 Maryna-f5 added this to the nginx-1.27.4 milestone Nov 19, 2024
@pluknet
Copy link
Contributor Author

pluknet commented Dec 18, 2024

I wonder if it is possible to restrict the effect to the keys that actually require passphrases. Is it even worth trying?

That's a good point.
I rewrote the patch to actually restrict only encrypted keys, based on callback invocation.
In the previous approach, it was possible to disable caching at all e.g. with ssl_password_file at the top level.

Do we know if the configurations that prompted the SSL cache work use ssl_password_file? Would that regress the configuration load time for them?

I'm not aware of how encrypted keys are widely used.
Disabling caching of encrypted keys still makes a noticeable speedup in my basic tests, because certificates itself are cached.

We can think about it later, if such requested arise.

@pluknet
Copy link
Contributor Author

pluknet commented Dec 18, 2024

I'm not sure if it's worth introducing both valid and inactivity with a proactive expiration logic.

I'd rather compare this to shared SSL sessions that have a similar expiration logic.
Here it is used to mitigate the problem of keeping cached items at the maximum configured size once fully populated.

Though, I don't see a strong effect from using it here, and it somewhat duplicates the "valid=" time, so it may have sense to rethink this parameter.

@pluknet
Copy link
Contributor Author

pluknet commented Dec 18, 2024

Changes:

  • logic to prevent caching encrypted keys is rewritten
  • cache nodes are now created after SSL object, because this is not needed for non-cacheable SSL objects (read: encrypted keys)
  • the first two patches are swapped for more clear diff

@pluknet
Copy link
Contributor Author

pluknet commented Dec 26, 2024

Changes:

  • partially reverted 7a26242 to allow caching encrypted keys with passphrase prompt, and related code simplifications.
  • made "inactive=" actually gradually expire old nodes

@pluknet
Copy link
Contributor Author

pluknet commented Jan 8, 2025

Most notably, this change reuses ngx_file_info() and improves its error handling; improves cache policy for runtime objects.
To be re-based/merged into appropriate existing commits.

@bavshin-f5
Copy link
Member

First, a general note: I feel like the logic in the dynamic cache introduction is getting too complex for a single commit.
Would it make sense to split it in 2 parts: basic dynamic cache implementation with reload from disk every time cn->valid expires, and another commit with the file info checks?


I've read the latest changes, and I disagree with equating the ability to revalidate an already cached entry with the ability to cache.
My rationale here:

  • The connection cache is already opt-in, with an expectation that the user is aware of the configuration. It's unlikely that someone accidentally enables runtime cache for engine: keys, but doesn't want that to happen. I'd say, the expectation is exactly the opposite.
  • There's not a lot of risk in keeping an object cached for valid even if we are unable to confirm that it wasn't updated and extend the life time past that. An ability to tune valid= or disable the cache for current context and your ngx_ssl_cache_purge patch cover all the negative cases I could imagine.
  • Disabling caching for one of the two related objects should increase the likeliness of getting X509_R_KEY_VALUES_MISMATCH. We already have to do that for encrypted keys, no reason to add more special cases :(

I wonder if you have any other potential uses for ngx_ssl_cache_purge. Otherwise, I'd like to suggest an alternative: passing a flag like NGX_SSL_CACHE_FORCE_UPDATE to ngx_ssl_cache_connection_fetch. This way we can avoid reallocation of the cache node.
If you think the function already has too many arguments, we can make it a mask on index. There`s no less than 30 unused bits, might as well use a few.

@pluknet
Copy link
Contributor Author

pluknet commented Jan 9, 2025

Merged fixup commits to use as a new baseline.

@pluknet
Copy link
Contributor Author

pluknet commented Jan 9, 2025

Rebase.

Memory based objects are always inherited, engine based objects are
never inherited to adhere the volatile nature of engines, file based
objects are inherited subject to modification time and file index.

The previous behaviour to bypass cache from the old configuration cycle
is preserved with a new directive "ssl_object_cache_inheritable off;".
@pluknet
Copy link
Contributor Author

pluknet commented Jan 13, 2025

Addressed @bavshin-f5 comments.

@pluknet
Copy link
Contributor Author

pluknet commented Jan 14, 2025

Addressed @bavshin-f5 comments.

  • backed out change in ngx_ssl_cache_lookup
  • adjusted comments

SSL object cache, as previously introduced in 1.27.2, did not take
into account encrypted certificate keys that might be unexpectedly
fetched from the cache regardless of the matching passphrase.  To
avoid this, caching of encrypted certificate keys is now disabled
based on the passphrase callback invocation.

A notable exception is encrypted certificate keys configured without
ssl_password_file.  They are loaded once resulting in the passphrase
prompt on startup and reused in other contexts as applicable.
pluknet and others added 4 commits January 15, 2025 03:16
A new directive "ssl_certificate_cache max=N [valid=time] [inactive=time]"
enables caching of SSL certificate chain and secret key objects specified
by "ssl_certificate" and "ssl_certificate_key" directives with variables.

Co-authored-by: Aleksei Bavshin <[email protected]>
Revalidation is based on file modification time and uniq file index,
and happens after the cache object validity time is expired.
Caching is enabled with proxy_ssl_certificate_cache and friends.

Co-authored-by: Aleksei Bavshin <[email protected]>
This can happen with certificates and certificate keys specified
with variables due to partial cache update in various scenarios:
- cache expiration with only one element of pair evicted
- on-disk update with non-cacheable encrypted keys
- non-atomic on-disk update

The fix is to retry with fresh data on X509_R_KEY_VALUES_MISMATCH.
@pluknet
Copy link
Contributor Author

pluknet commented Jan 15, 2025

password callback fixes:

  • MSVC C4701 bogus warning
  • initialization order restored

Copy link
Member

@bavshin-f5 bavshin-f5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@pluknet pluknet merged commit 5d5d9ad into nginx:master Jan 17, 2025
1 check passed
@pluknet pluknet deleted the ssl-cache-part2 branch January 17, 2025 00:37
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.

5 participants