-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(gax): Implement cert-rotation retries for grpc and http-json. #13246
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
Draft
vverman
wants to merge
2
commits into
googleapis:main
Choose a base branch
from
vverman:agentic-cert-rotation-retry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,3 +86,7 @@ monorepo | |
| *.tfstate.lock.info | ||
|
|
||
| .jqwik-database | ||
|
|
||
| **/Agentic_Identities/** | ||
| **/*.patch | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,11 @@ | |
| import io.grpc.Metadata; | ||
| import io.grpc.MethodDescriptor; | ||
| import io.grpc.Status; | ||
| import java.io.FileInputStream; | ||
| import java.io.IOException; | ||
| import java.security.MessageDigest; | ||
| import java.security.cert.CertificateFactory; | ||
| import java.security.cert.X509Certificate; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CancellationException; | ||
|
|
@@ -81,7 +85,19 @@ class ChannelPool extends ManagedChannel { | |
| private ScheduledFuture<?> refreshFuture = null; | ||
| private ScheduledFuture<?> resizeFuture = null; | ||
|
|
||
| private static class DiskCheckResult { | ||
| final String fingerprint; | ||
| final long timestampNanos; | ||
|
|
||
| DiskCheckResult(String fingerprint, long timestampNanos) { | ||
| this.fingerprint = fingerprint; | ||
| this.timestampNanos = timestampNanos; | ||
| } | ||
| } | ||
|
|
||
| private final AtomicReference<DiskCheckResult> lastDiskCheck = new AtomicReference<>(null); | ||
| private final Object entryWriteLock = new Object(); | ||
| private volatile String activeCertFingerprint = ""; | ||
| @VisibleForTesting final AtomicReference<ImmutableList<Entry>> entries = new AtomicReference<>(); | ||
| private final AtomicInteger indexTicker = new AtomicInteger(); | ||
| private final String authority; | ||
|
|
@@ -134,6 +150,11 @@ static ChannelPool create( | |
| entries.set(initialListBuilder.build()); | ||
| authority = entries.get().get(0).channel.authority(); | ||
|
|
||
| String certPath = getWorkloadCertPath(); | ||
| if (certPath != null) { | ||
| this.activeCertFingerprint = getCertificateFingerprint(certPath); | ||
| } | ||
|
|
||
| if (!settings.isStaticSize()) { | ||
| resizeFuture = | ||
| backgroundExecutorProvider | ||
|
|
@@ -425,6 +446,74 @@ private void refreshSafely() { | |
| } | ||
| } | ||
|
|
||
|
|
||
| private static String getWorkloadCertPath() { | ||
| String configPath = System.getenv("GOOGLE_API_CERTIFICATE_CONFIG"); | ||
| if (configPath != null && !configPath.isEmpty()) { | ||
| java.io.File configFile = new java.io.File(configPath); | ||
| if (configFile.exists() && !configFile.isDirectory()) { | ||
| // If explicit config exists, check it | ||
| } | ||
| } | ||
| java.io.File bundleFile = new java.io.File("/var/run/secrets/workload-spiffe-credentials/credentialbundle.pem"); | ||
| if (bundleFile.exists()) { | ||
| return bundleFile.getAbsolutePath(); | ||
| } | ||
| java.io.File certsFile = new java.io.File("/var/run/secrets/workload-spiffe-credentials/certificates.pem"); | ||
| if (certsFile.exists()) { | ||
| return certsFile.getAbsolutePath(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static String getCertificateFingerprint(String certPath) { | ||
| try (FileInputStream fis = new FileInputStream(certPath)) { | ||
| CertificateFactory cf = CertificateFactory.getInstance("X.509"); | ||
| X509Certificate cert = (X509Certificate) cf.generateCertificate(fis); | ||
| MessageDigest md = MessageDigest.getInstance("SHA-256"); | ||
| byte[] der = cert.getEncoded(); | ||
| byte[] digest = md.digest(der); | ||
| StringBuilder sb = new StringBuilder(); | ||
| for (byte b : digest) { | ||
| sb.append(String.format("%02x", b)); | ||
| } | ||
| return sb.toString(); | ||
| } catch (Exception e) { | ||
| LOG.log(Level.FINE, "Could not read or parse workload certificate at path " + certPath, e); | ||
| return ""; | ||
| } | ||
| } | ||
|
|
||
| private String getOrUpdateDiskFingerprint(String certPath) { | ||
| long now = System.nanoTime(); | ||
| DiskCheckResult cached = lastDiskCheck.get(); | ||
| if (cached != null && (now - cached.timestampNanos < java.util.concurrent.TimeUnit.SECONDS.toNanos(1))) { | ||
| return cached.fingerprint; | ||
| } | ||
|
|
||
| synchronized (lastDiskCheck) { | ||
| cached = lastDiskCheck.get(); | ||
| if (cached != null && (now - cached.timestampNanos < java.util.concurrent.TimeUnit.SECONDS.toNanos(1))) { | ||
| return cached.fingerprint; | ||
| } | ||
| String fingerprint = getCertificateFingerprint(certPath); | ||
| lastDiskCheck.set(new DiskCheckResult(fingerprint, System.nanoTime())); | ||
| return fingerprint; | ||
| } | ||
| } | ||
|
|
||
| boolean shouldRefresh() { | ||
| String certPath = getWorkloadCertPath(); | ||
| if (certPath == null) { | ||
| return false; | ||
| } | ||
| String currentDiskFingerprint = getOrUpdateDiskFingerprint(certPath); | ||
| if (currentDiskFingerprint.isEmpty()) { | ||
| return false; | ||
| } | ||
| return !currentDiskFingerprint.equalsIgnoreCase(activeCertFingerprint); | ||
|
Comment on lines
+506
to
+514
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC, this is the new gated logic right? Basically to ensure that we only check the fingerprint if the cert exists on the system. No need to have the rotation if the file doesn't exist |
||
| } | ||
|
|
||
| /** | ||
| * Replace all of the channels in the channel pool with fresh ones. This is meant to mitigate the | ||
| * hourly GFE disconnects by giving clients the ability to prime the channel on reconnect. | ||
|
|
@@ -441,7 +530,23 @@ void refresh() { | |
| // - then thread2 will shut down channel that thread1 will put back into circulation (after it | ||
| // replaces the list) | ||
| synchronized (entryWriteLock) { | ||
| LOG.fine("Refreshing all channels"); | ||
| String certPath = getWorkloadCertPath(); | ||
| if (certPath == null) { | ||
| return; | ||
| } | ||
| String currentDiskFingerprint = getOrUpdateDiskFingerprint(certPath); | ||
| if (currentDiskFingerprint.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| // Double-check fingerprint inside the lock | ||
| if (currentDiskFingerprint.equals(this.activeCertFingerprint)) { | ||
| LOG.fine("Channel pool was already refreshed by a concurrent thread, skipping duplicate refresh"); | ||
| return; | ||
| } | ||
|
|
||
| this.activeCertFingerprint = currentDiskFingerprint; | ||
| LOG.fine("Refreshing all channels with new certificate fingerprint: " + activeCertFingerprint); | ||
| ArrayList<Entry> newEntries = new ArrayList<>(entries.get()); | ||
|
|
||
| for (int i = 0; i < newEntries.size(); i++) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to split this shouldRefresh logic to two parts