Repository navigation
feat(storage): wire MetricsRecordingCredentials into HTTP and gRPC StorageOptions #15234
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
Open
nidhiii-27
wants to merge
9
commits into
feat/storage-credential-refresh-wrapper
Choose a base branch
from
feat/storage-credential-refresh-wiring
base: feat/storage-credential-refresh-wrapper
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.
+225
−20
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8e0fe46
feat(storage): wire MetricsRecordingCredentials into HTTP and gRPC St…
nidhiii-27 483c260
Merge branch 'feat/storage-credential-refresh-wrapper' into feat/stor…
nidhiii-27 e27cb44
test(storage): avoid Mockito mocks of @NullMarked credentials on Java 8
nidhiii-27 869ab6c
Merge branch 'feat/storage-credential-refresh-wrapper' into feat/stor…
nidhiii-27 3df6ae1
Merge branch 'feat/storage-credential-refresh-wrapper' into feat/stor…
nidhiii-27 1f20c6a
Merge branch 'feat/storage-credential-refresh-wrapper' into feat/stor…
nidhiii-27 cdca421
test(storage): use TestUtils.nullStreamController in GrpcStorageImplT…
nidhiii-27 4dac5a1
Merge feat/storage-credential-refresh-wrapper and address wiring test…
nidhiii-27 f986481
test(storage): merge Java 8 GrpcStorageImplTest StreamController fix
nidhiii-27 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
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
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 |
|---|---|---|
|
|
@@ -31,19 +31,25 @@ | |
| import static org.junit.Assert.assertThrows; | ||
|
|
||
| import com.google.api.core.ApiClock; | ||
| import com.google.api.gax.rpc.internal.QuotaProjectIdHidingCredentials; | ||
| import com.google.auth.CredentialTypeForMetrics; | ||
| import com.google.auth.Credentials; | ||
| import com.google.auth.RequestMetadataCallback; | ||
| import com.google.auth.ServiceAccountSigner; | ||
| import com.google.auth.http.HttpTransportFactory; | ||
| import com.google.auth.oauth2.AccessToken; | ||
| import com.google.auth.oauth2.ComputeEngineCredentials; | ||
| import com.google.auth.oauth2.GdchCredentials; | ||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.google.auth.oauth2.OAuth2Credentials; | ||
| import com.google.auth.oauth2.QuotaProjectIdProvider; | ||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import com.google.cloud.NoCredentials; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.util.concurrent.MoreExecutors; | ||
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.metrics.MeterProvider; | ||
| import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
| import io.opentelemetry.sdk.metrics.data.HistogramPointData; | ||
| import io.opentelemetry.sdk.metrics.data.MetricData; | ||
|
|
@@ -54,6 +60,7 @@ | |
| import java.io.InterruptedIOException; | ||
| import java.io.ObjectInputStream; | ||
| import java.io.ObjectOutputStream; | ||
| import java.io.Serializable; | ||
| import java.net.SocketTimeoutException; | ||
| import java.net.URI; | ||
| import java.security.PrivateKey; | ||
|
|
@@ -92,18 +99,7 @@ public void setUp() { | |
| reader = InMemoryMetricReader.create(); | ||
| provider = SdkMeterProvider.builder().registerMetricReader(reader).build(); | ||
| clockNanos = new AtomicLong(1_000_000_000L); | ||
| fakeClock = | ||
| new ApiClock() { | ||
| @Override | ||
| public long nanoTime() { | ||
| return clockNanos.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public long millisTime() { | ||
| return clockNanos.get() / 1_000_000L; | ||
| } | ||
| }; | ||
| fakeClock = new SerializableFakeClock(clockNanos); | ||
| debugMetrics = createMetrics(/* debug= */ true); | ||
| } | ||
|
|
||
|
|
@@ -480,6 +476,157 @@ public void onFailure(Throwable exception) { | |
| assertThat(findPointByError(STATUS_AUTHENTICATION_ERROR).getCount()).isEqualTo(1L); | ||
| } | ||
|
|
||
| @Test | ||
| public void httpStorageOptions_wiresScopedCredentialsWithoutMutating() throws Exception { | ||
| SigningTestCredentials signingCreds = new SigningTestCredentials(); | ||
| signingCreds.onRefresh = () -> clockNanos.addAndGet(150_000_000L); | ||
| HttpStorageOptions httpDebugOptions = | ||
| StorageOptions.http() | ||
| .setProjectId("test-project") | ||
| .setCredentials(signingCreds) | ||
| .setClock(fakeClock) | ||
| .setEnableOtelMetrics(true) | ||
| .setEnableOtelDebugMetrics(true) | ||
| .setMeterProvider(provider) | ||
| .build(); | ||
| HttpStorageOptions httpDebugOptionsCopy = httpDebugOptions.toBuilder().build(); | ||
|
|
||
| // getCredentials() stays untouched (ServiceAccountSigner, equals, hashCode, Serializable). | ||
| assertThat(httpDebugOptions.getCredentials()).isSameInstanceAs(signingCreds); | ||
| assertThat(httpDebugOptions.getCredentials()).isInstanceOf(ServiceAccountSigner.class); | ||
| assertThat(httpDebugOptions).isEqualTo(httpDebugOptionsCopy); | ||
| assertThat(httpDebugOptions.hashCode()).isEqualTo(httpDebugOptionsCopy.hashCode()); | ||
| Credentials scopedDebugCreds = httpDebugOptions.getScopedCredentials(); | ||
| assertThat(scopedDebugCreds).isInstanceOf(MetricsRecordingCredentials.class); | ||
| scopedDebugCreds.refresh(); | ||
| HistogramPointData okPoint = findPointByError(STATUS_OK); | ||
| assertThat(okPoint.getCount()).isEqualTo(1L); | ||
| assertThat(okPoint.getSum()).isWithin(0.0001).of(0.15); | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { | ||
| oos.writeObject(httpDebugOptions); | ||
| } | ||
| try (ObjectInputStream ois = | ||
| new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) { | ||
| HttpStorageOptions deserializedOptions = (HttpStorageOptions) ois.readObject(); | ||
| assertThat(deserializedOptions.getCredentials()).isInstanceOf(ServiceAccountSigner.class); | ||
| } | ||
|
|
||
| // HTTP safely wraps ComputeEngineCredentials when debug metrics are enabled. | ||
| ComputeEngineCredentials computeCreds = ComputeEngineCredentials.create(); | ||
| HttpStorageOptions httpComputeOptions = | ||
| StorageOptions.http() | ||
| .setProjectId("test-project") | ||
| .setCredentials(computeCreds) | ||
| .setEnableOtelMetrics(true) | ||
| .setEnableOtelDebugMetrics(true) | ||
| .setMeterProvider(provider) | ||
| .build(); | ||
| assertThat(httpComputeOptions.getScopedCredentials()) | ||
| .isInstanceOf(MetricsRecordingCredentials.class); | ||
|
|
||
| // Toggling debug off via toBuilder() yields unwrapped credentials without initializing metrics. | ||
| MeterProvider throwingMeterProvider = | ||
| name -> { | ||
| throw new AssertionError("metrics should not be initialized"); | ||
| }; | ||
| HttpStorageOptions httpStdOptions = | ||
|
nidhiii-27 marked this conversation as resolved.
Resolved
|
||
| httpDebugOptions.toBuilder() | ||
| .setEnableOtelDebugMetrics(false) | ||
| .setMeterProvider(throwingMeterProvider) | ||
| .build(); | ||
| assertThat(httpStdOptions.getScopedCredentials()).isSameInstanceAs(signingCreds); | ||
| } | ||
|
|
||
| @Test | ||
| public void grpcStorageOptions_wiresCredentialsWithoutMutating() throws Exception { | ||
| // 1) GoogleCredentials.create(null) is wrapped by MetricsRecordingCredentials and throws | ||
| // IllegalStateException during the quota-project probe; must not emit a spurious error. | ||
| GrpcStorageOptions nullTokenOptions = | ||
| StorageOptions.grpc() | ||
| .setProjectId("test-project") | ||
| .setCredentials(GoogleCredentials.create(/* accessToken= */ null)) | ||
| .setClock(fakeClock) | ||
| .setEnableOtelMetrics(true) | ||
| .setEnableOtelDebugMetrics(true) | ||
| .setMeterProvider(provider) | ||
| .build(); | ||
| assertThat(nullTokenOptions.getStorageSettings().getCredentialsProvider().getCredentials()) | ||
| .isInstanceOf(MetricsRecordingCredentials.class); | ||
| assertThat(findRefreshMetric()).isNull(); | ||
|
|
||
| // 2) With x-goog-user-project: probe records refresh, preserves getCredentials(), and wraps | ||
| // QuotaProjectIdHidingCredentials around MetricsRecordingCredentials. | ||
| TestGoogleCredentials grpcCreds = new TestGoogleCredentials(); | ||
| grpcCreds.extraHeaders = ImmutableMap.of("x-goog-user-project", ImmutableList.of("quota-proj")); | ||
| grpcCreds.onRefresh = () -> clockNanos.addAndGet(150_000_000L); | ||
| GrpcStorageOptions grpcDebugOptions = | ||
| StorageOptions.grpc() | ||
| .setProjectId("test-project") | ||
| .setCredentials(grpcCreds) | ||
| .setClock(fakeClock) | ||
| .setEnableOtelMetrics(true) | ||
| .setEnableOtelDebugMetrics(true) | ||
| .setMeterProvider(provider) | ||
| .build(); | ||
| assertThat(grpcDebugOptions.getCredentials()).isSameInstanceAs(grpcCreds); | ||
| Credentials providerCreds = | ||
| grpcDebugOptions.getStorageSettings().getCredentialsProvider().getCredentials(); | ||
| assertThat(providerCreds).isInstanceOf(QuotaProjectIdHidingCredentials.class); | ||
| assertThat(providerCreds.getRequestMetadata(STORAGE_URI)) | ||
| .doesNotContainKey("x-goog-user-project"); | ||
| HistogramPointData okPoint = findPointByError(STATUS_OK); | ||
| assertThat(okPoint.getCount()).isEqualTo(1L); | ||
| assertThat(okPoint.getSum()).isWithin(0.0001).of(0.15); | ||
| providerCreds.refresh(); | ||
| assertThat(findPointByError(STATUS_OK).getCount()).isEqualTo(2L); | ||
|
|
||
| // 3) Without x-goog-user-project: direct MetricsRecordingCredentials when debug enabled, and | ||
| // unwrapped passthrough when debug toggled off via toBuilder(). | ||
| TestGoogleCredentials noQuotaCreds = new TestGoogleCredentials(); | ||
| GrpcStorageOptions grpcNoQuotaDebug = | ||
| StorageOptions.grpc() | ||
| .setProjectId("test-project") | ||
| .setCredentials(noQuotaCreds) | ||
| .setClock(fakeClock) | ||
| .setEnableOtelMetrics(true) | ||
| .setEnableOtelDebugMetrics(true) | ||
| .setMeterProvider(provider) | ||
| .build(); | ||
| assertThat(grpcNoQuotaDebug.getStorageSettings().getCredentialsProvider().getCredentials()) | ||
| .isInstanceOf(MetricsRecordingCredentials.class); | ||
| GrpcStorageOptions grpcNoQuotaStd = | ||
| grpcNoQuotaDebug.toBuilder().setEnableOtelDebugMetrics(false).build(); | ||
| assertThat(grpcNoQuotaStd.getStorageSettings().getCredentialsProvider().getCredentials()) | ||
| .isSameInstanceAs(noQuotaCreds); | ||
|
|
||
| // 4) gRPC keeps ComputeEngineCredentials unwrapped for DirectPath ALTS hard-bound tokens. | ||
| // Construct a real instance with a fresh AccessToken instead of a Mockito mock because | ||
| // JSpecify @NullMarked on google-auth-library classes triggers ArrayStoreException under | ||
| // ByteBuddy on JDK 8. | ||
| ComputeEngineCredentials.Builder computeBuilder = | ||
| ComputeEngineCredentials.newBuilder() | ||
| .setUniverseDomain("googleapis.com") | ||
| .setHttpTransportFactory( | ||
| () -> { | ||
| throw new UnsupportedOperationException("unused"); | ||
| }); | ||
| computeBuilder.setAccessToken( | ||
| new AccessToken( | ||
| "compute-token", new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1)))); | ||
| ComputeEngineCredentials computeCreds = computeBuilder.build(); | ||
| GrpcStorageOptions grpcComputeOptions = | ||
| StorageOptions.grpc() | ||
| .setProjectId("test-project") | ||
| .setCredentials(computeCreds) | ||
| .setEnableOtelMetrics(true) | ||
| .setEnableOtelDebugMetrics(true) | ||
| .setMeterProvider(provider) | ||
| .build(); | ||
| assertThat(grpcComputeOptions.getStorageSettings().getCredentialsProvider().getCredentials()) | ||
| .isSameInstanceAs(computeCreds); | ||
| } | ||
|
|
||
| private StorageClientMetrics createMetrics(boolean debug) { | ||
| return StorageOptions.http() | ||
| .setProjectId("test-project") | ||
|
|
@@ -508,7 +655,7 @@ private HistogramPointData findPointByError(String errorType) { | |
| .orElseThrow(() -> new AssertionError("Missing point for error.type=" + errorType)); | ||
| } | ||
|
|
||
| private static final class TestGoogleCredentials extends GoogleCredentials { | ||
| private static class TestGoogleCredentials extends GoogleCredentials { | ||
| private static final long serialVersionUID = 1L; | ||
| private final AtomicInteger counter = new AtomicInteger(); | ||
| String credentialSource = "test-source"; | ||
|
|
@@ -517,6 +664,7 @@ private static final class TestGoogleCredentials extends GoogleCredentials { | |
| transient boolean returnNewCopyOnFactory; | ||
| transient @Nullable Runnable onRefresh; | ||
| transient @Nullable IOException failureToThrow; | ||
| transient @Nullable Map<String, List<String>> extraHeaders; | ||
| transient @Nullable RuntimeException runtimeFailureToThrow; | ||
| transient @Nullable RuntimeException throwRuntimeOnAsync; | ||
|
|
||
|
|
@@ -536,6 +684,11 @@ public AccessToken refreshAccessToken() throws IOException { | |
| new Date(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1))); | ||
| } | ||
|
|
||
| @Override | ||
| protected Map<String, List<String>> getAdditionalHeaders() { | ||
| return extraHeaders != null ? extraHeaders : ImmutableMap.of(); | ||
| } | ||
|
|
||
| @Override | ||
| public void getRequestMetadata( | ||
| @Nullable URI uri, Executor executor, RequestMetadataCallback callback) { | ||
|
|
@@ -656,4 +809,38 @@ public byte[] getEncoded() { | |
| return new byte[0]; | ||
| } | ||
| } | ||
|
|
||
| private static final class SerializableFakeClock implements ApiClock, Serializable { | ||
| private static final long serialVersionUID = 1L; | ||
| private final AtomicLong nanos; | ||
|
|
||
| SerializableFakeClock(AtomicLong nanos) { | ||
| this.nanos = nanos; | ||
| } | ||
|
|
||
| @Override | ||
| public long nanoTime() { | ||
| return nanos.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public long millisTime() { | ||
| return nanos.get() / 1_000_000L; | ||
| } | ||
| } | ||
|
|
||
| private static final class SigningTestCredentials extends TestGoogleCredentials | ||
| implements ServiceAccountSigner { | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| @Override | ||
| public String getAccount() { | ||
| return "[email protected]"; | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] sign(byte[] toSign) { | ||
| return toSign; | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.