From f0915e7619d71411b007f193a4bf9e1ee97f34d7 Mon Sep 17 00:00:00 2001 From: sanjaypujare Date: Tue, 24 Nov 2020 09:08:27 -0800 Subject: [PATCH] xds: fix xds example to be consistent with hello world example (#7659) --- examples/example-xds/README.md | 26 ++++++++--------- examples/example-xds/build.gradle | 7 ++--- .../helloworldxds/HelloWorldClientXds.java | 28 ++++++++----------- .../helloworldxds/HelloWorldServerXds.java | 6 +++- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/examples/example-xds/README.md b/examples/example-xds/README.md index b80df219ca..6f0d499f6f 100644 --- a/examples/example-xds/README.md +++ b/examples/example-xds/README.md @@ -18,8 +18,8 @@ directory: $ ../gradlew installDist ``` -This creates the scripts `build/install/example-xds/bin/hello-world-client-xds` and -`build/install/example-xds/bin/hello-world-server-xds`. +This creates the scripts `build/install/example-xds/bin/xds-hello-world-client` and +`build/install/example-xds/bin/xds-hello-world-server`. ### Run the example without using XDS Credentials @@ -27,13 +27,13 @@ To use XDS, you should first deploy the XDS management server in your deployment and know its name. You need to set the `GRPC_XDS_BOOTSTRAP` environment variable (preferred) or if that is not set then the `io.grpc.xds.bootstrap` java system property to point to the gRPC XDS bootstrap file (see [gRFC A27](https://github.com/grpc/proposal/blob/master/A27-xds-global-load-balancing.md#xdsclient-and-bootstrap-file) for the -bootstrap format). This is needed by both `build/install/example-xds/bin/hello-world-client-xds` -and `build/install/example-xds/bin/hello-world-server-xds`. +bootstrap format). This is needed by both `build/install/example-xds/bin/xds-hello-world-client` +and `build/install/example-xds/bin/xds-hello-world-server`. 1. To start the XDS-enabled example server, run: ``` $ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json -$ ./build/install/example-xds/bin/hello-world-server-xds 8000 my-test-xds-server +$ ./build/install/example-xds/bin/xds-hello-world-server 8000 my-test-xds-server ``` The first command line argument is the port to listen on (`8000`) and the second argument is a string @@ -42,29 +42,29 @@ id (`my-test-xds-server`) to be included in the greeting response to the client. 2. In a different terminal window, run the XDS-enabled example client: ``` $ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json -$ ./build/install/example-xds/bin/xds-hello-world-client xds:///yourServersName:8000 my-test-xds-client +$ ./build/install/example-xds/bin/xds-hello-world-client my-test-xds-client xds:///yourServersName:8000 ``` -The first command line argument (`xds:///yourServersName:8000`) is the target to connect to using the -`xds:` target scheme and the second argument (`my-test-xds-client`) is the name you wish to include in -the greeting request to the server. +The first command line argument (`my-test-xds-client`) is the name you wish to include in the greeting request +to the server and the second argument (`xds:///yourServersName:8000`) is the target to connect to using the +`xds:` target scheme. ### Run the example with xDS Credentials The above example used plaintext (insecure) credentials as explicitly provided by the client and server code. We will now demonstrate how the code can authorize use of xDS provided credentials by using -`XdsChannelCredentials` on the client side and using `XdsServerBuilder.useXdsSecurityWithPlaintextFallback()` -on the server side. This code is enabled by providing an additional command line argument. +`XdsChannelCredentials` on the client side and using `XdsServerCredentials` on the server side. +This code is enabled by providing an additional command line argument. 1. On the server side, add `--secure` on the command line to authorize use of xDS security: ``` $ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json -$ ./build/install/example-xds/bin/hello-world-server-xds 8000 my-test-xds-server --secure +$ ./build/install/example-xds/bin/xds-hello-world-server 8000 my-test-xds-server --secure ``` 2. Similarly, add `--secure` on the command line when you run the xDS client: ``` $ export GRPC_XDS_BOOTSTRAP=/path/to/bootstrap.json -$ ./build/install/example-xds/bin/hello-world-client-xds xds:///yourServersName:8000 my-test-xds-client --secure +$ ./build/install/example-xds/bin/xds-hello-world-client my-test-xds-client xds:///yourServersName:8000 --secure ``` In this case, if the xDS management server is configured to provide mTLS credentials (for example) to the client and diff --git a/examples/example-xds/build.gradle b/examples/example-xds/build.gradle index 453c4be13c..186aed5839 100644 --- a/examples/example-xds/build.gradle +++ b/examples/example-xds/build.gradle @@ -27,12 +27,11 @@ def nettyTcNativeVersion = '2.0.31.Final' def protocVersion = '3.12.0' dependencies { - implementation "io.grpc:grpc-netty:${grpcVersion}" + implementation "io.grpc:grpc-netty-shaded:${grpcVersion}" implementation "io.grpc:grpc-protobuf:${grpcVersion}" implementation "io.grpc:grpc-stub:${grpcVersion}" implementation "io.grpc:grpc-xds:${grpcVersion}" compileOnly "org.apache.tomcat:annotations-api:6.0.53" - runtimeOnly "io.netty:netty-tcnative-boringssl-static:${nettyTcNativeVersion}" } protobuf { @@ -49,14 +48,14 @@ startScripts.enabled = false task helloWorldClientXds(type: CreateStartScripts) { mainClassName = 'io.grpc.examples.helloworldxds.HelloWorldClientXds' - applicationName = 'hello-world-client-xds' + applicationName = 'xds-hello-world-client' outputDir = new File(project.buildDir, 'tmp') classpath = startScripts.classpath } task helloWorldServerXds(type: CreateStartScripts) { mainClassName = 'io.grpc.examples.helloworldxds.HelloWorldServerXds' - applicationName = 'hello-world-server-xds' + applicationName = 'xds-hello-world-server' outputDir = new File(project.buildDir, 'tmp') classpath = startScripts.classpath } diff --git a/examples/example-xds/src/main/java/io/grpc/examples/helloworldxds/HelloWorldClientXds.java b/examples/example-xds/src/main/java/io/grpc/examples/helloworldxds/HelloWorldClientXds.java index d4d560e83a..956a9f93f5 100644 --- a/examples/example-xds/src/main/java/io/grpc/examples/helloworldxds/HelloWorldClientXds.java +++ b/examples/example-xds/src/main/java/io/grpc/examples/helloworldxds/HelloWorldClientXds.java @@ -16,21 +16,14 @@ package io.grpc.examples.helloworldxds; -import io.grpc.ChannelCredentials; import io.grpc.Grpc; import io.grpc.InsecureChannelCredentials; import io.grpc.ManagedChannel; -import io.grpc.ManagedChannelBuilder; import io.grpc.StatusRuntimeException; import io.grpc.examples.helloworld.GreeterGrpc; import io.grpc.examples.helloworld.HelloReply; import io.grpc.examples.helloworld.HelloRequest; -import io.grpc.netty.GrpcSslContexts; -import io.grpc.netty.NettyChannelBuilder; import io.grpc.xds.XdsChannelCredentials; -import io.netty.handler.ssl.SslContext; -import io.netty.handler.ssl.SslContextBuilder; -import java.io.File; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; @@ -79,23 +72,26 @@ public class HelloWorldClientXds { * greeting. */ public static void main(String[] args) throws Exception { - String user = "xds-client"; + String user; boolean useXdsCreds = false; - if (args.length < 1 || args.length > 3) { - System.out.println("USAGE: HelloWorldClientXds target [name [--secure]]\n"); + if (args.length < 2 || args.length > 3) { + System.out.println("USAGE: HelloWorldClientXds name target [--secure]\n"); + System.err.println(" name The name you wish to include in the greeting request."); System.err.println(" target The xds target to connect to using the 'xds:' target scheme."); - System.err.println(" name The name you wish to include in the greeting request. Defaults to " + user); System.err.println( " '--secure' Indicates using xDS credentials otherwise defaults to insecure."); System.exit(1); } - if (args.length > 1) { - user = args[1]; - if (args.length == 3) { - useXdsCreds = args[2].toLowerCase().startsWith("--s"); + user = args[0]; + + if (args.length == 3) { + if ("--secure".startsWith(args[2])) { + useXdsCreds = true; + } else { + System.out.println("Ignored: " + args[2]); } } - HelloWorldClientXds client = new HelloWorldClientXds(args[0], useXdsCreds); + HelloWorldClientXds client = new HelloWorldClientXds(args[1], useXdsCreds); try { client.greet(user); } finally { diff --git a/examples/example-xds/src/main/java/io/grpc/examples/helloworldxds/HelloWorldServerXds.java b/examples/example-xds/src/main/java/io/grpc/examples/helloworldxds/HelloWorldServerXds.java index 940c93d95d..5598f534be 100644 --- a/examples/example-xds/src/main/java/io/grpc/examples/helloworldxds/HelloWorldServerXds.java +++ b/examples/example-xds/src/main/java/io/grpc/examples/helloworldxds/HelloWorldServerXds.java @@ -101,7 +101,11 @@ public class HelloWorldServerXds { if (args.length > 1) { hostName = args[1]; if (args.length == 3) { - useXdsCreds = args[2].toLowerCase().startsWith("--s"); + if ("--secure".startsWith(args[2])) { + useXdsCreds = true; + } else { + System.out.println("Ignored: " + args[2]); + } } } final HelloWorldServerXds server =