Skip to content

Commit

Permalink
Implement GcpServiceAccountIdentityCredentials
Browse files Browse the repository at this point in the history
  • Loading branch information
shivaspeaks committed Oct 9, 2024
1 parent 9bb06af commit 6227079
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
3 changes: 2 additions & 1 deletion auth/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ tasks.named("jar").configure {

dependencies {
api project(':grpc-api'),
libraries.google.auth.credentials
libraries.google.auth.credentials,
libraries.google.auth.oauth2Http
implementation libraries.guava
testImplementation project(':grpc-testing'),
project(':grpc-core'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2016 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.auth;

import com.google.auth.oauth2.ComputeEngineCredentials;
import com.google.auth.oauth2.IdTokenCredentials;
import io.grpc.CallCredentials;
import io.grpc.Status;
import java.util.concurrent.Executor;

/**
* {@link CallCredentials} that authenticates using the default service account
* of a Google Compute Engine (GCE) instance.
*
* <p>It obtains an ID token from the GCE metadata server and uses it to create
* {@link IdTokenCredentials} which are then adapted to {@link CallCredentials}
* using {@link MoreCallCredentials}.
*
* <p>This class is intended for use on GCE instances. It will not work
* in other environments.
*/
public class GcpServiceAccountIdentityCredentials extends CallCredentials {
private final String audience;

public GcpServiceAccountIdentityCredentials(String audience) {
this.audience = audience;
}

Check warning on line 41 in auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java

View check run for this annotation

Codecov / codecov/patch

auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java#L39-L41

Added lines #L39 - L41 were not covered by tests

@Override
public void applyRequestMetadata(RequestInfo requestInfo,
Executor appExecutor, MetadataApplier applier) {
appExecutor.execute(() -> {

Check warning on line 46 in auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java

View check run for this annotation

Codecov / codecov/patch

auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java#L46

Added line #L46 was not covered by tests
try {
CallCredentials grpcCredentials = getCallCredentials();
grpcCredentials.applyRequestMetadata(requestInfo, appExecutor, applier);
} catch (Exception e) {
applier.fail(Status.UNAUTHENTICATED.withCause(e));
}
});
}

Check warning on line 54 in auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java

View check run for this annotation

Codecov / codecov/patch

auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java#L48-L54

Added lines #L48 - L54 were not covered by tests

private CallCredentials getCallCredentials() throws Exception {
ComputeEngineCredentials credentials = ComputeEngineCredentials.create();
IdTokenCredentials idTokenCredentials = IdTokenCredentials.newBuilder()
.setIdTokenProvider(credentials)
.setTargetAudience(audience)
.build();
return MoreCallCredentials.from(idTokenCredentials);

Check warning on line 62 in auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java

View check run for this annotation

Codecov / codecov/patch

auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java#L57-L62

Added lines #L57 - L62 were not covered by tests
}

@Override
public String toString() {
return "GcpServiceAccountIdentityCallCredentials{"

Check warning on line 67 in auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java

View check run for this annotation

Codecov / codecov/patch

auth/src/main/java/io/grpc/auth/GcpServiceAccountIdentityCredentials.java#L67

Added line #L67 was not covered by tests
+ "audience='" + audience + '\'' + '}';
}
}

0 comments on commit 6227079

Please sign in to comment.