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 <grpcpp/ext/admin_services.h>
18 #include <grpcpp/ext/proto_server_reflection_plugin.h>
19 #include <grpcpp/grpcpp.h>
20 #include <grpcpp/server.h>
21 #include <grpcpp/server_builder.h>
22 #include <grpcpp/server_context.h>
23 #include <grpcpp/support/string_ref.h>
24 #include <grpcpp/xds_server_builder.h>
25
26 #include <algorithm>
27 #include <atomic>
28 #include <chrono>
29 #include <condition_variable>
30 #include <deque>
31 #include <map>
32 #include <memory>
33 #include <mutex>
34 #include <set>
35 #include <sstream>
36 #include <string>
37 #include <thread>
38 #include <vector>
39
40 #include "absl/algorithm/container.h"
41 #include "absl/flags/flag.h"
42 #include "absl/log/log.h"
43 #include "absl/strings/str_format.h"
44 #include "absl/strings/str_join.h"
45 #include "absl/strings/str_split.h"
46 #include "src/core/lib/channel/status_util.h"
47 #include "src/core/util/env.h"
48 #include "src/core/util/gethostname.h"
49 #include "src/core/util/host_port.h"
50 #include "src/proto/grpc/testing/istio_echo.pb.h"
51 #include "test/core/test_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 LOG(INFO) << "Server listening on " << server_address << " over xds";
118 has_xds_listeners = true;
119 } else if (tls_ports.find(port) != tls_ports.end()) {
120 // Create Credentials for Tls Servers -
121 // 1. Uses FileWatcherCertificateProvider with a refresh interval of 600
122 // seconds. (Number decided based on gRPC defaults.
123 // 2. Do not ask for client certificates. (Not yet sure what is needed
124 // right now.) Add ports to the builders
125 experimental::TlsServerCredentialsOptions options(
126 std::make_shared<experimental::FileWatcherCertificateProvider>(
127 absl::GetFlag(FLAGS_key), absl::GetFlag(FLAGS_crt), 600));
128 options.set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE);
129 options.watch_identity_key_cert_pairs();
130 options.set_check_call_host(false);
131 builder.AddListeningPort(server_address, TlsServerCredentials(options));
132 LOG(INFO) << "Server listening on " << server_address << " over tls";
133 } else {
134 builder.AddListeningPort(server_address, InsecureServerCredentials());
135 LOG(INFO) << "Server listening on " << server_address << " over insecure";
136 }
137 }
138 // Enable the default health check service, probably not needed though.
139 grpc::EnableDefaultHealthCheckService(true);
140 std::unique_ptr<Server> xds_server;
141 if (has_xds_listeners) {
142 xds_server = xds_builder.BuildAndStart();
143 }
144 std::unique_ptr<Server> server(builder.BuildAndStart());
145 server->Wait();
146 }
147
148 } // namespace
149 } // namespace testing
150 } // namespace grpc
151
main(int argc,char ** argv)152 int main(int argc, char** argv) {
153 // Preprocess argv, for two things:
154 // 1. merge duplicate flags. So "--grpc=8080 --grpc=9090" becomes
155 // "--grpc=8080,9090".
156 // 2. replace '-' to '_'. So "--istio-version=123" becomes
157 // "--istio_version=123".
158 // 3. remove --version since that is specially interpreted by absl
159 std::map<std::string, std::vector<std::string>> argv_dict;
160 for (int i = 0; i < argc; i++) {
161 std::string arg(argv[i]);
162 size_t equal = arg.find_first_of('=');
163 if (equal != std::string::npos) {
164 std::string f = arg.substr(0, equal);
165 std::string v = arg.substr(equal + 1, std::string::npos);
166 if (f == "--version") {
167 argv_dict["--service_version"].push_back(v);
168 } else {
169 argv_dict[f].push_back(v);
170 }
171 }
172 }
173 std::vector<char*> new_argv_strs;
174 // Keep the command itself.
175 new_argv_strs.push_back(argv[0]);
176 for (const auto& kv : argv_dict) {
177 std::string values;
178 for (const auto& s : kv.second) {
179 if (!values.empty()) values += ",";
180 values += s;
181 }
182 // replace '-' to '_', excluding the leading "--".
183 std::string f = kv.first;
184 std::replace(f.begin() + 2, f.end(), '-', '_');
185 std::string k_vs = absl::StrCat(f, "=", values);
186 char* writable = new char[k_vs.size() + 1];
187 std::copy(k_vs.begin(), k_vs.end(), writable);
188 writable[k_vs.size()] = '\0';
189 new_argv_strs.push_back(writable);
190 }
191 int new_argc = new_argv_strs.size();
192 char** new_argv = new_argv_strs.data();
193 grpc::testing::TestEnvironment env(&new_argc, new_argv);
194 grpc::testing::InitTest(&new_argc, &new_argv, true);
195 // Turn gRPC ports from a string vector to an int vector.
196 std::set<int> grpc_ports;
197 for (const auto& p : absl::GetFlag(FLAGS_grpc)) {
198 int grpc_port = std::stoi(p);
199 grpc_ports.insert(grpc_port);
200 }
201 // Create a map of which ports are supposed to use xds
202 std::set<int> xds_ports;
203 for (const auto& p : absl::GetFlag(FLAGS_xds_grpc_server)) {
204 int port = 0;
205 if (!absl::SimpleAtoi(p, &port)) {
206 LOG(ERROR) << "SimpleAtoi Failure: " << p;
207 return 1;
208 }
209 xds_ports.insert(port);
210 // If the port does not exist in gRPC ports set, add it.
211 if (grpc_ports.find(port) == grpc_ports.end()) {
212 grpc_ports.insert(port);
213 }
214 }
215 // Create a map of which ports are supposed to use tls
216 std::set<int> tls_ports;
217 for (const auto& p : absl::GetFlag(FLAGS_tls)) {
218 int port = 0;
219 if (!absl::SimpleAtoi(p, &port)) {
220 LOG(ERROR) << "SimpleAtoi Failure: " << p;
221 return 1;
222 }
223 tls_ports.insert(port);
224 }
225 // Start the servers
226 grpc::testing::RunServer(grpc_ports, xds_ports, tls_ports);
227 return 0;
228 }
229