Skip to content
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

alts: deprecate ComputeEngineChannelBuilder and add GoogleComputeEngineChannelBuilder #6368

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -37,7 +37,11 @@
/**
* {@code ManagedChannelBuilder} for Google Compute Engine. This class sets up a secure channel
* using ALTS if applicable and using TLS as fallback.
*
* @deprecated Please use {@link GoogleComputeEngineChannelBuilder} instead. See the bug
* https://github.com/grpc/grpc-java/issues/6367 for more information.
*/
@Deprecated
public final class ComputeEngineChannelBuilder
extends ForwardingChannelBuilder<GoogleDefaultChannelBuilder> {

Expand Down
101 changes: 101 additions & 0 deletions alts/src/main/java/io/grpc/alts/GoogleComputeEngineChannelBuilder.java
@@ -0,0 +1,101 @@
/*
* Copyright 2019 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.alts;

import com.google.auth.oauth2.ComputeEngineCredentials;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import io.grpc.CallCredentials;
import io.grpc.ForwardingChannelBuilder;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Status;
import io.grpc.alts.internal.AltsProtocolNegotiator.GoogleDefaultProtocolNegotiatorFactory;
import io.grpc.auth.MoreCallCredentials;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.SharedResourcePool;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.InternalNettyChannelBuilder;
import io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator;
import io.grpc.netty.NettyChannelBuilder;
import io.netty.handler.ssl.SslContext;
import javax.net.ssl.SSLException;

/**
* {@code ManagedChannelBuilder} for Google Compute Engine. This class sets up a secure channel
* using ALTS if applicable and using TLS as fallback.
*
* @since 1.25.0
*/
public final class GoogleComputeEngineChannelBuilder
extends ForwardingChannelBuilder<GoogleComputeEngineChannelBuilder> {

private final NettyChannelBuilder delegate;

private GoogleComputeEngineChannelBuilder(String target) {
delegate = NettyChannelBuilder.forTarget(target);
SslContext sslContext;
try {
sslContext = GrpcSslContexts.forClient().build();
} catch (SSLException e) {
throw new RuntimeException(e);
}
InternalNettyChannelBuilder.setProtocolNegotiatorFactory(
delegate(),
new GoogleDefaultProtocolNegotiatorFactory(
/* targetServiceAccounts= */ ImmutableList.<String>of(),
SharedResourcePool.forResource(HandshakerServiceChannel.SHARED_HANDSHAKER_CHANNEL),
sslContext));
CallCredentials credentials = MoreCallCredentials.from(ComputeEngineCredentials.create());
Status status = Status.OK;
if (!CheckGcpEnvironment.isOnGcp()) {
status =
Status.INTERNAL.withDescription(
"Compute Engine Credentials can only be used on Google Cloud Platform");
}
delegate().intercept(new CallCredentialsInterceptor(credentials, status));
}

/** "Overrides" the static method in {@link ManagedChannelBuilder}. */
public static final GoogleComputeEngineChannelBuilder forTarget(String target) {
return new GoogleComputeEngineChannelBuilder(target);
}

/** "Overrides" the static method in {@link ManagedChannelBuilder}. */
public static GoogleComputeEngineChannelBuilder forAddress(String name, int port) {
return forTarget(GrpcUtil.authorityFromHostAndPort(name, port));
}

@Override
protected NettyChannelBuilder delegate() {
return delegate;
}

@VisibleForTesting
ProtocolNegotiator getProtocolNegotiatorForTest() {
SslContext sslContext;
try {
sslContext = GrpcSslContexts.forClient().build();
} catch (SSLException e) {
throw new RuntimeException(e);
}
return new GoogleDefaultProtocolNegotiatorFactory(
/* targetServiceAccounts= */ ImmutableList.<String>of(),
SharedResourcePool.forResource(HandshakerServiceChannel.SHARED_HANDSHAKER_CHANNEL),
sslContext)
.buildProtocolNegotiator();
}
}
Expand Up @@ -24,11 +24,12 @@
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class ComputeEngineChannelBuilderTest {
public final class GoogleComputeEngineChannelBuilderTest {

@Test
public void buildsNettyChannel() throws Exception {
ComputeEngineChannelBuilder builder = ComputeEngineChannelBuilder.forTarget("localhost:8080");
GoogleComputeEngineChannelBuilder builder =
GoogleComputeEngineChannelBuilder.forTarget("localhost:8080");
builder.build();

ProtocolNegotiator protocolNegotiator = builder.getProtocolNegotiatorForTest();
Expand Down
Expand Up @@ -1719,7 +1719,7 @@ public void computeEngineCreds(String serviceAccount, String oauthScope) throws
assertResponse(goldenResponse, response);
}

/** Sends an unary rpc with ComputeEngineChannelBuilder. */
/** Sends an unary rpc with GoogleComputeEngineChannelBuilder. */
public void computeEngineChannelCredentials(
String defaultServiceAccount,
TestServiceGrpc.TestServiceBlockingStub computeEngineStub) throws Exception {
Expand Down
Expand Up @@ -21,7 +21,7 @@
import com.google.protobuf.ByteString;
import io.grpc.ManagedChannel;
import io.grpc.StatusRuntimeException;
import io.grpc.alts.ComputeEngineChannelBuilder;
import io.grpc.alts.GoogleComputeEngineChannelBuilder;
import io.grpc.testing.integration.Messages.Payload;
import io.grpc.testing.integration.Messages.SimpleRequest;
import io.grpc.testing.integration.Messages.SimpleResponse;
Expand Down Expand Up @@ -214,7 +214,7 @@ private void run() throws Exception {
}

private ManagedChannel createChannel() {
return ComputeEngineChannelBuilder.forTarget(target).build();
return GoogleComputeEngineChannelBuilder.forTarget(target).build();
}
}

Expand Up @@ -20,7 +20,7 @@
import com.google.common.io.Files;
import io.grpc.ManagedChannel;
import io.grpc.alts.AltsChannelBuilder;
import io.grpc.alts.ComputeEngineChannelBuilder;
import io.grpc.alts.GoogleComputeEngineChannelBuilder;
import io.grpc.alts.GoogleDefaultChannelBuilder;
import io.grpc.internal.AbstractManagedChannelImplBuilder;
import io.grpc.internal.GrpcUtil;
Expand Down Expand Up @@ -280,7 +280,7 @@ private void runTest(TestCases testCase) throws Exception {
break;

case COMPUTE_ENGINE_CHANNEL_CREDENTIALS: {
ManagedChannel channel = ComputeEngineChannelBuilder
ManagedChannel channel = GoogleComputeEngineChannelBuilder
.forAddress(serverHost, serverPort).build();
try {
TestServiceGrpc.TestServiceBlockingStub computeEngineStub =
Expand Down Expand Up @@ -396,7 +396,7 @@ protected ManagedChannel createChannel() {
}
if (customCredentialsType != null
&& customCredentialsType.equals("compute_engine_channel_creds")) {
return ComputeEngineChannelBuilder.forAddress(serverHost, serverPort).build();
return GoogleComputeEngineChannelBuilder.forAddress(serverHost, serverPort).build();
}
if (useAlts) {
return AltsChannelBuilder.forAddress(serverHost, serverPort).build();
Expand Down