xref: /aosp_15_r20/external/grpc-grpc/test/cpp/interop/istio_echo_server.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 // Copyright 2022 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 #include <algorithm>
18 #include <atomic>
19 #include <chrono>
20 #include <condition_variable>
21 #include <deque>
22 #include <map>
23 #include <memory>
24 #include <mutex>
25 #include <set>
26 #include <sstream>
27 #include <string>
28 #include <thread>
29 #include <vector>
30 
31 #include "absl/algorithm/container.h"
32 #include "absl/flags/flag.h"
33 #include "absl/strings/str_format.h"
34 #include "absl/strings/str_join.h"
35 #include "absl/strings/str_split.h"
36 
37 #include <grpcpp/ext/admin_services.h>
38 #include <grpcpp/ext/proto_server_reflection_plugin.h>
39 #include <grpcpp/grpcpp.h>
40 #include <grpcpp/server.h>
41 #include <grpcpp/server_builder.h>
42 #include <grpcpp/server_context.h>
43 #include <grpcpp/support/string_ref.h>
44 #include <grpcpp/xds_server_builder.h>
45 
46 #include "src/core/lib/channel/status_util.h"
47 #include "src/core/lib/gprpp/env.h"
48 #include "src/core/lib/gprpp/host_port.h"
49 #include "src/core/lib/iomgr/gethostname.h"
50 #include "src/proto/grpc/testing/istio_echo.pb.h"
51 #include "test/core/util/test_config.h"
52 #include "test/cpp/interop/istio_echo_server_lib.h"
53 #include "test/cpp/util/test_config.h"
54 
55 // A list of ports to listen on, for gRPC traffic.
56 ABSL_FLAG(std::vector<std::string>, grpc, std::vector<std::string>({"7070"}),
57           "GRPC ports");
58 ABSL_FLAG(std::vector<std::string>, tls, std::vector<std::string>({}),
59           "Ports that are using TLS. These must be defined as http/grpc/tcp.");
60 ABSL_FLAG(std::vector<std::string>, xds_grpc_server,
61           std::vector<std::string>({}),
62           "Ports that should rely on XDS configuration to serve");
63 ABSL_FLAG(std::string, crt, "", "gRPC TLS server-side certificate");
64 ABSL_FLAG(std::string, key, "", "gRPC TLS server-side key");
65 ABSL_FLAG(std::string, forwarding_address, "0.0.0.0:7072",
66           "Forwarding address for unhandled protocols");
67 ABSL_FLAG(std::string, service_version, "", "Version string for the service");
68 
69 // The following flags must be defined, but are not used for now. Some may be
70 // necessary for certain tests.
71 ABSL_FLAG(std::vector<std::string>, port, std::vector<std::string>({"8080"}),
72           "HTTP/1.1 ports");
73 ABSL_FLAG(std::vector<std::string>, tcp, std::vector<std::string>({"9090"}),
74           "TCP ports");
75 ABSL_FLAG(std::vector<std::string>, bind_ip, std::vector<std::string>({}),
76           "Ports that are bound to INSTANCE_IP rather than wildcard IP.");
77 ABSL_FLAG(std::vector<std::string>, bind_localhost,
78           std::vector<std::string>({}),
79           "Ports that are bound to localhost rather than wildcard IP.");
80 ABSL_FLAG(std::vector<std::string>, server_first, std::vector<std::string>({}),
81           "Ports that are server first. These must be defined as tcp.");
82 ABSL_FLAG(std::string, metrics, "", "Metrics port");
83 ABSL_FLAG(std::string, uds, "", "HTTP server on unix domain socket");
84 ABSL_FLAG(std::string, cluster, "", "Cluster where this server is deployed");
85 ABSL_FLAG(std::string, istio_version, "", "Istio sidecar version");
86 ABSL_FLAG(std::string, disable_alpn, "", "disable ALPN negotiation");
87 
88 namespace grpc {
89 namespace testing {
90 namespace {
91 
RunServer(const std::set<int> & grpc_ports,const std::set<int> & xds_ports,const std::set<int> & tls_ports)92 void RunServer(const std::set<int>& grpc_ports, const std::set<int>& xds_ports,
93                const std::set<int>& tls_ports) {
94   // Get hostname
95   std::string hostname;
96   char* hostname_p = grpc_gethostname();
97   if (hostname_p == nullptr) {
98     hostname = absl::StrFormat("generated-%d", rand() % 1000);
99   } else {
100     hostname = hostname_p;
101     free(hostname_p);
102   }
103   EchoTestServiceImpl echo_test_service(
104       std::move(hostname), absl::GetFlag(FLAGS_service_version),
105       absl::GetFlag(FLAGS_forwarding_address));
106   grpc::reflection::InitProtoReflectionServerBuilderPlugin();
107   ServerBuilder builder;
108   XdsServerBuilder xds_builder;
109   bool has_xds_listeners = false;
110   builder.RegisterService(&echo_test_service);
111   xds_builder.RegisterService(&echo_test_service);
112   for (int port : grpc_ports) {
113     auto server_address = grpc_core::JoinHostPort("0.0.0.0", port);
114     if (xds_ports.find(port) != xds_ports.end()) {
115       xds_builder.AddListeningPort(
116           server_address, XdsServerCredentials(InsecureServerCredentials()));
117       gpr_log(GPR_INFO, "Server listening on %s over xds",
118               server_address.c_str());
119       has_xds_listeners = true;
120     } else if (tls_ports.find(port) != tls_ports.end()) {
121       // Create Credentials for Tls Servers -
122       // 1. Uses FileWatcherCertificateProvider with a refresh interval of 600
123       // seconds. (Number decided based on gRPC defaults.
124       // 2. Do not ask for client certificates. (Not yet sure what is needed
125       // right now.) Add ports to the builders
126       experimental::TlsServerCredentialsOptions options(
127           std::make_shared<experimental::FileWatcherCertificateProvider>(
128               absl::GetFlag(FLAGS_key), absl::GetFlag(FLAGS_crt), 600));
129       options.set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE);
130       options.watch_identity_key_cert_pairs();
131       options.set_check_call_host(false);
132       builder.AddListeningPort(server_address, TlsServerCredentials(options));
133       gpr_log(GPR_INFO, "Server listening on %s over tls",
134               server_address.c_str());
135     } else {
136       builder.AddListeningPort(server_address, InsecureServerCredentials());
137       gpr_log(GPR_INFO, "Server listening on %s over insecure",
138               server_address.c_str());
139     }
140   }
141   // Enable the default health check service, probably not needed though.
142   grpc::EnableDefaultHealthCheckService(true);
143   std::unique_ptr<Server> xds_server;
144   if (has_xds_listeners) {
145     xds_server = xds_builder.BuildAndStart();
146   }
147   std::unique_ptr<Server> server(builder.BuildAndStart());
148   server->Wait();
149 }
150 
151 }  // namespace
152 }  // namespace testing
153 }  // namespace grpc
154 
main(int argc,char ** argv)155 int main(int argc, char** argv) {
156   //  Preprocess argv, for two things:
157   //  1. merge duplciate flags. So "--grpc=8080 --grpc=9090" becomes
158   //  "--grpc=8080,9090".
159   //  2. replace '-' to '_'. So "--istio-version=123" becomes
160   //  "--istio_version=123".
161   //  3. remove --version since that is specially interpretted by absl
162   std::map<std::string, std::vector<std::string>> argv_dict;
163   for (int i = 0; i < argc; i++) {
164     std::string arg(argv[i]);
165     size_t equal = arg.find_first_of('=');
166     if (equal != std::string::npos) {
167       std::string f = arg.substr(0, equal);
168       std::string v = arg.substr(equal + 1, std::string::npos);
169       if (f == "--version") {
170         argv_dict["--service_version"].push_back(v);
171       } else {
172         argv_dict[f].push_back(v);
173       }
174     }
175   }
176   std::vector<char*> new_argv_strs;
177   // Keep the command itself.
178   new_argv_strs.push_back(argv[0]);
179   for (const auto& kv : argv_dict) {
180     std::string values;
181     for (const auto& s : kv.second) {
182       if (!values.empty()) values += ",";
183       values += s;
184     }
185     // replace '-' to '_', excluding the leading "--".
186     std::string f = kv.first;
187     std::replace(f.begin() + 2, f.end(), '-', '_');
188     std::string k_vs = absl::StrCat(f, "=", values);
189     char* writable = new char[k_vs.size() + 1];
190     std::copy(k_vs.begin(), k_vs.end(), writable);
191     writable[k_vs.size()] = '\0';
192     new_argv_strs.push_back(writable);
193   }
194   int new_argc = new_argv_strs.size();
195   char** new_argv = new_argv_strs.data();
196   grpc::testing::TestEnvironment env(&new_argc, new_argv);
197   grpc::testing::InitTest(&new_argc, &new_argv, true);
198   // Turn gRPC ports from a string vector to an int vector.
199   std::set<int> grpc_ports;
200   for (const auto& p : absl::GetFlag(FLAGS_grpc)) {
201     int grpc_port = std::stoi(p);
202     grpc_ports.insert(grpc_port);
203   }
204   // Create a map of which ports are supposed to use xds
205   std::set<int> xds_ports;
206   for (const auto& p : absl::GetFlag(FLAGS_xds_grpc_server)) {
207     int port = 0;
208     if (!absl::SimpleAtoi(p, &port)) {
209       gpr_log(GPR_ERROR, "SimpleAtoi Failure: %s", p.c_str());
210       return 1;
211     }
212     xds_ports.insert(port);
213     // If the port does not exist in gRPC ports set, add it.
214     if (grpc_ports.find(port) == grpc_ports.end()) {
215       grpc_ports.insert(port);
216     }
217   }
218   // Create a map of which ports are supposed to use tls
219   std::set<int> tls_ports;
220   for (const auto& p : absl::GetFlag(FLAGS_tls)) {
221     int port = 0;
222     if (!absl::SimpleAtoi(p, &port)) {
223       gpr_log(GPR_ERROR, "SimpleAtoi Failure: %s", p.c_str());
224       return 1;
225     }
226     tls_ports.insert(port);
227   }
228   // Start the servers
229   grpc::testing::RunServer(grpc_ports, xds_ports, tls_ports);
230   return 0;
231 }
232