• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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.examples.helloworldtls;
18 
19 import io.grpc.Channel;
20 import io.grpc.Grpc;
21 import io.grpc.ManagedChannel;
22 import io.grpc.StatusRuntimeException;
23 import io.grpc.TlsChannelCredentials;
24 import io.grpc.examples.helloworld.GreeterGrpc;
25 import io.grpc.examples.helloworld.HelloReply;
26 import io.grpc.examples.helloworld.HelloRequest;
27 import java.io.File;
28 import java.util.concurrent.TimeUnit;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 
32 /**
33  * A simple client that requests a greeting from the {@link HelloWorldServerTls} with TLS.
34  */
35 public class HelloWorldClientTls {
36     private static final Logger logger = Logger.getLogger(HelloWorldClientTls.class.getName());
37 
38     private final GreeterGrpc.GreeterBlockingStub blockingStub;
39 
40     /**
41      * Construct client for accessing RouteGuide server using the existing channel.
42      */
HelloWorldClientTls(Channel channel)43     public HelloWorldClientTls(Channel channel) {
44         blockingStub = GreeterGrpc.newBlockingStub(channel);
45     }
46 
47     /**
48      * Say hello to server.
49      */
greet(String name)50     public void greet(String name) {
51         logger.info("Will try to greet " + name + " ...");
52         HelloRequest request = HelloRequest.newBuilder().setName(name).build();
53         HelloReply response;
54         try {
55             response = blockingStub.sayHello(request);
56         } catch (StatusRuntimeException e) {
57             logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
58             return;
59         }
60         logger.info("Greeting: " + response.getMessage());
61     }
62 
63     /**
64      * Greet server. If provided, the first element of {@code args} is the name to use in the
65      * greeting.
66      */
main(String[] args)67     public static void main(String[] args) throws Exception {
68 
69         if (args.length < 2 || args.length == 4 || args.length > 5) {
70             System.out.println("USAGE: HelloWorldClientTls host port [trustCertCollectionFilePath " +
71                     "[clientCertChainFilePath clientPrivateKeyFilePath]]\n  Note: clientCertChainFilePath and " +
72                     "clientPrivateKeyFilePath are only needed if mutual auth is desired.");
73             System.exit(0);
74         }
75 
76         // If only defaults are necessary, you can use TlsChannelCredentials.create() instead of
77         // interacting with the Builder.
78         TlsChannelCredentials.Builder tlsBuilder = TlsChannelCredentials.newBuilder();
79         switch (args.length) {
80             case 5:
81                 tlsBuilder.keyManager(new File(args[3]), new File(args[4]));
82                 // fallthrough
83             case 3:
84                 tlsBuilder.trustManager(new File(args[2]));
85                 // fallthrough
86             default:
87         }
88         String host = args[0];
89         int port = Integer.parseInt(args[1]);
90         ManagedChannel channel = Grpc.newChannelBuilderForAddress(host, port, tlsBuilder.build())
91                 /* Only for using provided test certs. */
92                 .overrideAuthority("foo.test.google.fr")
93                 .build();
94         try {
95             HelloWorldClientTls client = new HelloWorldClientTls(channel);
96             client.greet(host);
97         } finally {
98             channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
99         }
100     }
101 }
102