• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.alts;
18 
19 import io.grpc.BindableService;
20 import io.grpc.CompressorRegistry;
21 import io.grpc.DecompressorRegistry;
22 import io.grpc.ExperimentalApi;
23 import io.grpc.ForwardingServerBuilder;
24 import io.grpc.HandlerRegistry;
25 import io.grpc.Server;
26 import io.grpc.ServerBuilder;
27 import io.grpc.ServerInterceptor;
28 import io.grpc.ServerServiceDefinition;
29 import io.grpc.ServerStreamTracer;
30 import io.grpc.ServerTransportFilter;
31 import io.grpc.netty.NettyServerBuilder;
32 import java.io.File;
33 import java.net.InetSocketAddress;
34 import java.util.concurrent.Executor;
35 import java.util.concurrent.TimeUnit;
36 
37 /**
38  * gRPC secure server builder used for ALTS. This class adds on the necessary ALTS support to create
39  * a production server on Google Cloud Platform.
40  */
41 @ExperimentalApi("https://github.com/grpc/grpc-java/issues/4151")
42 public final class AltsServerBuilder extends ForwardingServerBuilder<AltsServerBuilder> {
43   private final NettyServerBuilder delegate;
44   private final AltsServerCredentials.Builder credentialsBuilder =
45       new AltsServerCredentials.Builder();
46 
AltsServerBuilder(NettyServerBuilder nettyDelegate)47   private AltsServerBuilder(NettyServerBuilder nettyDelegate) {
48     this.delegate = nettyDelegate;
49   }
50 
51   /** Creates a gRPC server builder for the given port. */
forPort(int port)52   public static AltsServerBuilder forPort(int port) {
53     NettyServerBuilder nettyDelegate = NettyServerBuilder.forAddress(new InetSocketAddress(port));
54     return new AltsServerBuilder(nettyDelegate);
55   }
56 
57   /**
58    * Enables untrusted ALTS for testing. If this function is called, we will not check whether ALTS
59    * is running on Google Cloud Platform.
60    */
enableUntrustedAltsForTesting()61   public AltsServerBuilder enableUntrustedAltsForTesting() {
62     credentialsBuilder.enableUntrustedAltsForTesting();
63     return this;
64   }
65 
66   /** Sets a new handshaker service address for testing. */
setHandshakerAddressForTesting(String handshakerAddress)67   public AltsServerBuilder setHandshakerAddressForTesting(String handshakerAddress) {
68     credentialsBuilder.setHandshakerAddressForTesting(handshakerAddress);
69     return this;
70   }
71 
72   @Override
delegate()73   protected ServerBuilder<?> delegate() {
74     return delegate;
75   }
76 
77   /** {@inheritDoc} */
78   @Override
handshakeTimeout(long timeout, TimeUnit unit)79   public AltsServerBuilder handshakeTimeout(long timeout, TimeUnit unit) {
80     delegate.handshakeTimeout(timeout, unit);
81     return this;
82   }
83 
84   /** {@inheritDoc} */
85   @Override
directExecutor()86   public AltsServerBuilder directExecutor() {
87     delegate.directExecutor();
88     return this;
89   }
90 
91   /** {@inheritDoc} */
92   @Override
addStreamTracerFactory(ServerStreamTracer.Factory factory)93   public AltsServerBuilder addStreamTracerFactory(ServerStreamTracer.Factory factory) {
94     delegate.addStreamTracerFactory(factory);
95     return this;
96   }
97 
98   /** {@inheritDoc} */
99   @Override
addTransportFilter(ServerTransportFilter filter)100   public AltsServerBuilder addTransportFilter(ServerTransportFilter filter) {
101     delegate.addTransportFilter(filter);
102     return this;
103   }
104 
105   /** {@inheritDoc} */
106   @Override
executor(Executor executor)107   public AltsServerBuilder executor(Executor executor) {
108     delegate.executor(executor);
109     return this;
110   }
111 
112   /** {@inheritDoc} */
113   @Override
addService(ServerServiceDefinition service)114   public AltsServerBuilder addService(ServerServiceDefinition service) {
115     delegate.addService(service);
116     return this;
117   }
118 
119   /** {@inheritDoc} */
120   @Override
addService(BindableService bindableService)121   public AltsServerBuilder addService(BindableService bindableService) {
122     delegate.addService(bindableService);
123     return this;
124   }
125 
126   /** {@inheritDoc} */
127   @Override
fallbackHandlerRegistry(HandlerRegistry fallbackRegistry)128   public AltsServerBuilder fallbackHandlerRegistry(HandlerRegistry fallbackRegistry) {
129     delegate.fallbackHandlerRegistry(fallbackRegistry);
130     return this;
131   }
132 
133   /** {@inheritDoc} */
134   @Override
useTransportSecurity(File certChain, File privateKey)135   public AltsServerBuilder useTransportSecurity(File certChain, File privateKey) {
136     throw new UnsupportedOperationException("Can't set TLS settings for ALTS");
137   }
138 
139   /** {@inheritDoc} */
140   @Override
decompressorRegistry(DecompressorRegistry registry)141   public AltsServerBuilder decompressorRegistry(DecompressorRegistry registry) {
142     delegate.decompressorRegistry(registry);
143     return this;
144   }
145 
146   /** {@inheritDoc} */
147   @Override
compressorRegistry(CompressorRegistry registry)148   public AltsServerBuilder compressorRegistry(CompressorRegistry registry) {
149     delegate.compressorRegistry(registry);
150     return this;
151   }
152 
153   /** {@inheritDoc} */
154   @Override
intercept(ServerInterceptor interceptor)155   public AltsServerBuilder intercept(ServerInterceptor interceptor) {
156     delegate.intercept(interceptor);
157     return this;
158   }
159 
160   /** {@inheritDoc} */
161   @Override
build()162   public Server build() {
163     delegate.protocolNegotiator(credentialsBuilder.buildProtocolNegotiator());
164     return delegate.build();
165   }
166 }
167