| 1 |
// |
| 2 |
// Copyright 2019 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 "src/core/ext/xds/xds_bootstrap.h" |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
|
| 21 |
#include <map> |
| 22 |
#include <memory> |
| 23 |
#include <string> |
| 24 |
#include <utility> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include "absl/status/status.h" |
| 28 |
#include "absl/status/statusor.h" |
| 29 |
#include "absl/strings/numbers.h" |
| 30 |
#include "absl/strings/str_format.h" |
| 31 |
#include "absl/strings/string_view.h" |
| 32 |
#include "gmock/gmock.h" |
| 33 |
#include "gtest/gtest.h" |
| 34 |
|
| 35 |
#include <grpc/grpc.h> |
| 36 |
#include <grpc/grpc_security.h> |
| 37 |
#include <grpc/grpc_security_constants.h> |
| 38 |
#include <grpc/support/alloc.h> |
| 39 |
|
| 40 |
#include "src/core/ext/xds/certificate_provider_store.h" |
| 41 |
#include "src/core/ext/xds/xds_bootstrap_grpc.h" |
| 42 |
#include "src/core/lib/config/core_configuration.h" |
| 43 |
#include "src/core/lib/gpr/tmpfile.h" |
| 44 |
#include "src/core/lib/gprpp/env.h" |
| 45 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 46 |
#include "src/core/lib/iomgr/error.h" |
| 47 |
#include "src/core/lib/json/json.h" |
| 48 |
#include "src/core/lib/json/json_object_loader.h" |
| 49 |
#include "src/core/lib/security/certificate_provider/certificate_provider_factory.h" |
| 50 |
#include "src/core/lib/security/credentials/tls/grpc_tls_certificate_provider.h" |
| 51 |
#include "test/core/util/test_config.h" |
| 52 |
|
| 53 |
namespace grpc_core { |
| 54 |
namespace testing { |
| 55 |
namespace { |
| 56 |
|
| 57 |
TEST(XdsBootstrapTest, Basic) { |
| 58 |
SetEnv("GRPC_EXPERIMENTAL_XDS_FEDERATION", "true"); |
| 59 |
const char* json_str = |
| 60 |
"{" |
| 61 |
" \"xds_servers\": [" |
| 62 |
" {" |
| 63 |
" \"server_uri\": \"fake:///lb\"," |
| 64 |
" \"channel_creds\": [" |
| 65 |
" {" |
| 66 |
" \"type\": \"fake\"," |
| 67 |
" \"ignore\": 0" |
| 68 |
" }" |
| 69 |
" ]," |
| 70 |
" \"ignore\": 0" |
| 71 |
" }," |
| 72 |
" {" |
| 73 |
" \"server_uri\": \"ignored\"," |
| 74 |
" \"channel_creds\": [" |
| 75 |
" {" |
| 76 |
" \"type\": \"ignored\"," |
| 77 |
" \"ignore\": 0" |
| 78 |
" }," |
| 79 |
" {" |
| 80 |
" \"type\": \"fake\"" |
| 81 |
" }" |
| 82 |
" ]," |
| 83 |
" \"ignore\": 0" |
| 84 |
" }" |
| 85 |
" ]," |
| 86 |
" \"authorities\": {" |
| 87 |
" \"xds.example.com\": {" |
| 88 |
" \"client_listener_resource_name_template\": " |
| 89 |
"\"xdstp://xds.example.com/envoy.config.listener.v3.Listener/grpc/server/" |
| 90 |
"%s\"," |
| 91 |
" \"xds_servers\": [" |
| 92 |
" {" |
| 93 |
" \"server_uri\": \"fake:///xds_server\"," |
| 94 |
" \"channel_creds\": [" |
| 95 |
" {" |
| 96 |
" \"type\": \"fake\"" |
| 97 |
" }" |
| 98 |
" ]," |
| 99 |
" \"server_features\": [\"xds_v3\"]" |
| 100 |
" }" |
| 101 |
" ]" |
| 102 |
" }," |
| 103 |
" \"xds.example2.com\": {" |
| 104 |
" \"client_listener_resource_name_template\": " |
| 105 |
"\"xdstp://xds.example2.com/envoy.config.listener.v3.Listener/grpc/" |
| 106 |
"server/%s\"," |
| 107 |
" \"xds_servers\": [" |
| 108 |
" {" |
| 109 |
" \"server_uri\": \"fake:///xds_server2\"," |
| 110 |
" \"channel_creds\": [" |
| 111 |
" {" |
| 112 |
" \"type\": \"fake\"" |
| 113 |
" }" |
| 114 |
" ]," |
| 115 |
" \"server_features\": [\"xds_v3\"]" |
| 116 |
" }" |
| 117 |
" ]" |
| 118 |
" }" |
| 119 |
" }," |
| 120 |
" \"node\": {" |
| 121 |
" \"id\": \"foo\"," |
| 122 |
" \"cluster\": \"bar\"," |
| 123 |
" \"locality\": {" |
| 124 |
" \"region\": \"milky_way\"," |
| 125 |
" \"zone\": \"sol_system\"," |
| 126 |
" \"sub_zone\": \"earth\"," |
| 127 |
" \"ignore\": {}" |
| 128 |
" }," |
| 129 |
" \"metadata\": {" |
| 130 |
" \"foo\": 1," |
| 131 |
" \"bar\": 2" |
| 132 |
" }," |
| 133 |
" \"ignore\": \"whee\"" |
| 134 |
" }," |
| 135 |
" \"server_listener_resource_name_template\": \"example/resource\"," |
| 136 |
" \"ignore\": {}" |
| 137 |
"}"; |
| 138 |
auto bootstrap_or = GrpcXdsBootstrap::Create(json_str); |
| 139 |
ASSERT_TRUE(bootstrap_or.ok()) << bootstrap_or.status(); |
| 140 |
auto bootstrap = std::move(*bootstrap_or); |
| 141 |
auto* server = |
| 142 |
&static_cast<const GrpcXdsBootstrap::GrpcXdsServer&>(bootstrap->server()); |
| 143 |
EXPECT_EQ(server->server_uri(), "fake:///lb"); |
| 144 |
EXPECT_EQ(server->channel_creds_type(), "fake"); |
| 145 |
EXPECT_TRUE(server->channel_creds_config().empty()) |
| 146 |
<< Json{server->channel_creds_config()}.Dump(); |
| 147 |
EXPECT_EQ(bootstrap->authorities().size(), 2); |
| 148 |
auto* authority = static_cast<const GrpcXdsBootstrap::GrpcAuthority*>( |
| 149 |
bootstrap->LookupAuthority("xds.example.com")); |
| 150 |
ASSERT_NE(authority, nullptr); |
| 151 |
EXPECT_EQ(authority->client_listener_resource_name_template(), |
| 152 |
"xdstp://xds.example.com/envoy.config.listener.v3.Listener/grpc/" |
| 153 |
"server/%s"); |
| 154 |
server = |
| 155 |
static_cast<const GrpcXdsBootstrap::GrpcXdsServer*>(authority->server()); |
| 156 |
ASSERT_NE(server, nullptr); |
| 157 |
EXPECT_EQ(server->server_uri(), "fake:///xds_server"); |
| 158 |
EXPECT_EQ(server->channel_creds_type(), "fake"); |
| 159 |
EXPECT_TRUE(server->channel_creds_config().empty()) |
| 160 |
<< Json{server->channel_creds_config()}.Dump(); |
| 161 |
authority = static_cast<const GrpcXdsBootstrap::GrpcAuthority*>( |
| 162 |
bootstrap->LookupAuthority("xds.example2.com")); |
| 163 |
ASSERT_NE(authority, nullptr); |
| 164 |
EXPECT_EQ(authority->client_listener_resource_name_template(), |
| 165 |
"xdstp://xds.example2.com/envoy.config.listener.v3.Listener/grpc/" |
| 166 |
"server/%s"); |
| 167 |
server = |
| 168 |
static_cast<const GrpcXdsBootstrap::GrpcXdsServer*>(authority->server()); |
| 169 |
ASSERT_NE(server, nullptr); |
| 170 |
EXPECT_EQ(server->server_uri(), "fake:///xds_server2"); |
| 171 |
EXPECT_EQ(server->channel_creds_type(), "fake"); |
| 172 |
EXPECT_TRUE(server->channel_creds_config().empty()) |
| 173 |
<< Json{server->channel_creds_config()}.Dump(); |
| 174 |
ASSERT_NE(bootstrap->node(), nullptr); |
| 175 |
EXPECT_EQ(bootstrap->node()->id(), "foo"); |
| 176 |
EXPECT_EQ(bootstrap->node()->cluster(), "bar"); |
| 177 |
EXPECT_EQ(bootstrap->node()->locality_region(), "milky_way"); |
| 178 |
EXPECT_EQ(bootstrap->node()->locality_zone(), "sol_system"); |
| 179 |
EXPECT_EQ(bootstrap->node()->locality_sub_zone(), "earth"); |
| 180 |
EXPECT_THAT(bootstrap->node()->metadata(), |
| 181 |
::testing::ElementsAre( |
| 182 |
::testing::Pair( |
| 183 |
::testing::Eq("bar"), |
| 184 |
::testing::AllOf( |
| 185 |
::testing::Property(&Json::type, Json::Type::NUMBER), |
| 186 |
::testing::Property(&Json::string_value, "2"))), |
| 187 |
::testing::Pair( |
| 188 |
::testing::Eq("foo"), |
| 189 |
::testing::AllOf( |
| 190 |
::testing::Property(&Json::type, Json::Type::NUMBER), |
| 191 |
::testing::Property(&Json::string_value, "1"))))); |
| 192 |
EXPECT_EQ(bootstrap->server_listener_resource_name_template(), |
| 193 |
"example/resource"); |
| 194 |
UnsetEnv("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 195 |
} |
| 196 |
|
| 197 |
TEST(XdsBootstrapTest, ValidWithoutNode) { |
| 198 |
const char* json_str = |
| 199 |
"{" |
| 200 |
" \"xds_servers\": [" |
| 201 |
" {" |
| 202 |
" \"server_uri\": \"fake:///lb\"," |
| 203 |
" \"channel_creds\": [{\"type\": \"fake\"}]" |
| 204 |
" }" |
| 205 |
" ]" |
| 206 |
"}"; |
| 207 |
auto bootstrap_or = GrpcXdsBootstrap::Create(json_str); |
| 208 |
ASSERT_TRUE(bootstrap_or.ok()) << bootstrap_or.status(); |
| 209 |
auto bootstrap = std::move(*bootstrap_or); |
| 210 |
auto* server = |
| 211 |
&static_cast<const GrpcXdsBootstrap::GrpcXdsServer&>(bootstrap->server()); |
| 212 |
EXPECT_EQ(server->server_uri(), "fake:///lb"); |
| 213 |
EXPECT_EQ(server->channel_creds_type(), "fake"); |
| 214 |
EXPECT_EQ(bootstrap->node(), nullptr); |
| 215 |
} |
| 216 |
|
| 217 |
TEST(XdsBootstrapTest, InsecureCreds) { |
| 218 |
const char* json_str = |
| 219 |
"{" |
| 220 |
" \"xds_servers\": [" |
| 221 |
" {" |
| 222 |
" \"server_uri\": \"fake:///lb\"," |
| 223 |
" \"channel_creds\": [{\"type\": \"insecure\"}]" |
| 224 |
" }" |
| 225 |
" ]" |
| 226 |
"}"; |
| 227 |
auto bootstrap_or = GrpcXdsBootstrap::Create(json_str); |
| 228 |
ASSERT_TRUE(bootstrap_or.ok()) << bootstrap_or.status(); |
| 229 |
auto bootstrap = std::move(*bootstrap_or); |
| 230 |
auto* server = |
| 231 |
&static_cast<const GrpcXdsBootstrap::GrpcXdsServer&>(bootstrap->server()); |
| 232 |
EXPECT_EQ(server->server_uri(), "fake:///lb"); |
| 233 |
EXPECT_EQ(server->channel_creds_type(), "insecure"); |
| 234 |
EXPECT_EQ(bootstrap->node(), nullptr); |
| 235 |
} |
| 236 |
|
| 237 |
TEST(XdsBootstrapTest, GoogleDefaultCreds) { |
| 238 |
// Generate call creds file needed by GoogleDefaultCreds. |
| 239 |
const char token_str[] = |
| 240 |
"{ \"client_id\": \"32555999999.apps.googleusercontent.com\"," |
| 241 |
" \"client_secret\": \"EmssLNjJy1332hD4KFsecret\"," |
| 242 |
" \"refresh_token\": \"1/Blahblasj424jladJDSGNf-u4Sua3HDA2ngjd42\"," |
| 243 |
" \"type\": \"authorized_user\"}"; |
| 244 |
char* creds_file_name; |
| 245 |
FILE* creds_file = gpr_tmpfile("xds_bootstrap_test", &creds_file_name); |
| 246 |
ASSERT_NE(creds_file_name, nullptr); |
| 247 |
ASSERT_NE(creds_file, nullptr); |
| 248 |
ASSERT_EQ(fwrite(token_str, 1, sizeof(token_str), creds_file), |
| 249 |
sizeof(token_str)); |
| 250 |
fclose(creds_file); |
| 251 |
SetEnv(GRPC_GOOGLE_CREDENTIALS_ENV_VAR, creds_file_name); |
| 252 |
gpr_free(creds_file_name); |
| 253 |
// Now run test. |
| 254 |
const char* json_str = |
| 255 |
"{" |
| 256 |
" \"xds_servers\": [" |
| 257 |
" {" |
| 258 |
" \"server_uri\": \"fake:///lb\"," |
| 259 |
" \"channel_creds\": [{\"type\": \"google_default\"}]" |
| 260 |
" }" |
| 261 |
" ]" |
| 262 |
"}"; |
| 263 |
auto bootstrap_or = GrpcXdsBootstrap::Create(json_str); |
| 264 |
ASSERT_TRUE(bootstrap_or.ok()) << bootstrap_or.status(); |
| 265 |
auto bootstrap = std::move(*bootstrap_or); |
| 266 |
auto* server = |
| 267 |
&static_cast<const GrpcXdsBootstrap::GrpcXdsServer&>(bootstrap->server()); |
| 268 |
EXPECT_EQ(server->server_uri(), "fake:///lb"); |
| 269 |
EXPECT_EQ(server->channel_creds_type(), "google_default"); |
| 270 |
EXPECT_EQ(bootstrap->node(), nullptr); |
| 271 |
} |
| 272 |
|
| 273 |
TEST(XdsBootstrapTest, MissingChannelCreds) { |
| 274 |
const char* json_str = |
| 275 |
"{" |
| 276 |
" \"xds_servers\": [" |
| 277 |
" {" |
| 278 |
" \"server_uri\": \"fake:///lb\"" |
| 279 |
" }" |
| 280 |
" ]" |
| 281 |
"}"; |
| 282 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 283 |
EXPECT_EQ(bootstrap.status().message(), |
| 284 |
"errors validating JSON: [" |
| 285 |
"field:xds_servers[0].channel_creds error:field not present]") |
| 286 |
<< bootstrap.status(); |
| 287 |
} |
| 288 |
|
| 289 |
TEST(XdsBootstrapTest, NoKnownChannelCreds) { |
| 290 |
const char* json_str = |
| 291 |
"{" |
| 292 |
" \"xds_servers\": [" |
| 293 |
" {" |
| 294 |
" \"server_uri\": \"fake:///lb\"," |
| 295 |
" \"channel_creds\": [{\"type\": \"unknown\"}]" |
| 296 |
" }" |
| 297 |
" ]" |
| 298 |
"}"; |
| 299 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 300 |
EXPECT_EQ(bootstrap.status().message(), |
| 301 |
"errors validating JSON: [" |
| 302 |
"field:xds_servers[0].channel_creds " |
| 303 |
"error:no known creds type found]") |
| 304 |
<< bootstrap.status(); |
| 305 |
} |
| 306 |
|
| 307 |
TEST(XdsBootstrapTest, MissingXdsServers) { |
| 308 |
auto bootstrap = GrpcXdsBootstrap::Create("{}"); |
| 309 |
EXPECT_EQ( |
| 310 |
bootstrap.status().message(), |
| 311 |
"errors validating JSON: [field:xds_servers error:field not present]") |
| 312 |
<< bootstrap.status(); |
| 313 |
} |
| 314 |
|
| 315 |
TEST(XdsBootstrapTest, TopFieldsWrongTypes) { |
| 316 |
const char* json_str = |
| 317 |
"{" |
| 318 |
" \"xds_servers\":1," |
| 319 |
" \"node\":1," |
| 320 |
" \"server_listener_resource_name_template\":1," |
| 321 |
" \"certificate_providers\":1" |
| 322 |
"}"; |
| 323 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 324 |
EXPECT_EQ( |
| 325 |
bootstrap.status().message(), |
| 326 |
"errors validating JSON: [" |
| 327 |
"field:certificate_providers error:is not an object; " |
| 328 |
"field:node error:is not an object; " |
| 329 |
"field:server_listener_resource_name_template error:is not a string; " |
| 330 |
"field:xds_servers error:is not an array]") |
| 331 |
<< bootstrap.status(); |
| 332 |
} |
| 333 |
|
| 334 |
TEST(XdsBootstrapTest, XdsServerMissingFields) { |
| 335 |
const char* json_str = |
| 336 |
"{" |
| 337 |
" \"xds_servers\":[{}]" |
| 338 |
"}"; |
| 339 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 340 |
EXPECT_EQ(bootstrap.status().message(), |
| 341 |
"errors validating JSON: [" |
| 342 |
"field:xds_servers[0].channel_creds error:field not present; " |
| 343 |
"field:xds_servers[0].server_uri error:field not present]") |
| 344 |
<< bootstrap.status(); |
| 345 |
} |
| 346 |
|
| 347 |
TEST(XdsBootstrapTest, XdsServerUriAndCredsWrongTypes) { |
| 348 |
const char* json_str = |
| 349 |
"{" |
| 350 |
" \"xds_servers\":[" |
| 351 |
" {" |
| 352 |
" \"server_uri\":1," |
| 353 |
" \"channel_creds\":1" |
| 354 |
" }" |
| 355 |
" ]" |
| 356 |
"}"; |
| 357 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 358 |
EXPECT_EQ(bootstrap.status().message(), |
| 359 |
"errors validating JSON: [" |
| 360 |
"field:xds_servers[0].channel_creds error:is not an array; " |
| 361 |
"field:xds_servers[0].server_uri error:is not a string]") |
| 362 |
<< bootstrap.status(); |
| 363 |
} |
| 364 |
|
| 365 |
TEST(XdsBootstrapTest, ChannelCredsFieldsWrongTypes) { |
| 366 |
const char* json_str = |
| 367 |
"{" |
| 368 |
" \"xds_servers\":[" |
| 369 |
" {" |
| 370 |
" \"server_uri\":\"foo\"," |
| 371 |
" \"channel_creds\":[" |
| 372 |
" {" |
| 373 |
" \"type\":0," |
| 374 |
" \"config\":1" |
| 375 |
" }" |
| 376 |
" ]" |
| 377 |
" }" |
| 378 |
" ]" |
| 379 |
"}"; |
| 380 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 381 |
EXPECT_EQ( |
| 382 |
bootstrap.status().message(), |
| 383 |
"errors validating JSON: [" |
| 384 |
"field:xds_servers[0].channel_creds[0].config error:is not an object; " |
| 385 |
"field:xds_servers[0].channel_creds[0].type error:is not a string]") |
| 386 |
<< bootstrap.status(); |
| 387 |
} |
| 388 |
|
| 389 |
TEST(XdsBootstrapTest, NodeFieldsWrongTypes) { |
| 390 |
const char* json_str = |
| 391 |
"{" |
| 392 |
" \"node\":{" |
| 393 |
" \"id\":0," |
| 394 |
" \"cluster\":0," |
| 395 |
" \"locality\":0," |
| 396 |
" \"metadata\":0" |
| 397 |
" }" |
| 398 |
"}"; |
| 399 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 400 |
EXPECT_EQ(bootstrap.status().message(), |
| 401 |
"errors validating JSON: [" |
| 402 |
"field:node.cluster error:is not a string; " |
| 403 |
"field:node.id error:is not a string; " |
| 404 |
"field:node.locality error:is not an object; " |
| 405 |
"field:node.metadata error:is not an object; " |
| 406 |
"field:xds_servers error:field not present]") |
| 407 |
<< bootstrap.status(); |
| 408 |
} |
| 409 |
|
| 410 |
TEST(XdsBootstrapTest, LocalityFieldsWrongType) { |
| 411 |
const char* json_str = |
| 412 |
"{" |
| 413 |
" \"node\":{" |
| 414 |
" \"locality\":{" |
| 415 |
" \"region\":0," |
| 416 |
" \"zone\":0," |
| 417 |
" \"sub_zone\":0" |
| 418 |
" }" |
| 419 |
" }" |
| 420 |
"}"; |
| 421 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 422 |
EXPECT_EQ(bootstrap.status().message(), |
| 423 |
"errors validating JSON: [" |
| 424 |
"field:node.locality.region error:is not a string; " |
| 425 |
"field:node.locality.sub_zone error:is not a string; " |
| 426 |
"field:node.locality.zone error:is not a string; " |
| 427 |
"field:xds_servers error:field not present]") |
| 428 |
<< bootstrap.status(); |
| 429 |
} |
| 430 |
|
| 431 |
TEST(XdsBootstrapTest, CertificateProvidersElementWrongType) { |
| 432 |
const char* json_str = |
| 433 |
"{" |
| 434 |
" \"xds_servers\": [" |
| 435 |
" {" |
| 436 |
" \"server_uri\": \"fake:///lb\"," |
| 437 |
" \"channel_creds\": [{\"type\": \"fake\"}]" |
| 438 |
" }" |
| 439 |
" ]," |
| 440 |
" \"certificate_providers\": {" |
| 441 |
" \"plugin\":1" |
| 442 |
" }" |
| 443 |
"}"; |
| 444 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 445 |
EXPECT_EQ(bootstrap.status().message(), |
| 446 |
"errors validating JSON: [" |
| 447 |
"field:certificate_providers[\"plugin\"] error:is not an object]") |
| 448 |
<< bootstrap.status(); |
| 449 |
} |
| 450 |
|
| 451 |
TEST(XdsBootstrapTest, CertificateProvidersPluginNameWrongType) { |
| 452 |
const char* json_str = |
| 453 |
"{" |
| 454 |
" \"xds_servers\": [" |
| 455 |
" {" |
| 456 |
" \"server_uri\": \"fake:///lb\"," |
| 457 |
" \"channel_creds\": [{\"type\": \"fake\"}]" |
| 458 |
" }" |
| 459 |
" ]," |
| 460 |
" \"certificate_providers\": {" |
| 461 |
" \"plugin\": {" |
| 462 |
" \"plugin_name\":1" |
| 463 |
" }" |
| 464 |
" }" |
| 465 |
"}"; |
| 466 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 467 |
EXPECT_EQ(bootstrap.status().message(), |
| 468 |
"errors validating JSON: [" |
| 469 |
"field:certificate_providers[\"plugin\"].plugin_name error:" |
| 470 |
"is not a string]") |
| 471 |
<< bootstrap.status(); |
| 472 |
} |
| 473 |
|
| 474 |
TEST(XdsBootstrapTest, CertificateProvidersUnrecognizedPluginName) { |
| 475 |
const char* json_str = |
| 476 |
"{" |
| 477 |
" \"xds_servers\": [" |
| 478 |
" {" |
| 479 |
" \"server_uri\": \"fake:///lb\"," |
| 480 |
" \"channel_creds\": [{\"type\": \"fake\"}]" |
| 481 |
" }" |
| 482 |
" ]," |
| 483 |
" \"certificate_providers\": {" |
| 484 |
" \"plugin\": {" |
| 485 |
" \"plugin_name\":\"unknown\"" |
| 486 |
" }" |
| 487 |
" }" |
| 488 |
"}"; |
| 489 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 490 |
EXPECT_EQ(bootstrap.status().message(), |
| 491 |
"errors validating JSON: [" |
| 492 |
"field:certificate_providers[\"plugin\"].plugin_name error:" |
| 493 |
"Unrecognized plugin name: unknown]") |
| 494 |
<< bootstrap.status(); |
| 495 |
} |
| 496 |
|
| 497 |
TEST(XdsBootstrapTest, AuthorityXdsServerInvalidResourceTemplate) { |
| 498 |
SetEnv("GRPC_EXPERIMENTAL_XDS_FEDERATION", "true"); |
| 499 |
const char* json_str = |
| 500 |
"{" |
| 501 |
" \"xds_servers\": [" |
| 502 |
" {" |
| 503 |
" \"server_uri\": \"fake:///lb\"," |
| 504 |
" \"channel_creds\": [{\"type\": \"fake\"}]" |
| 505 |
" }" |
| 506 |
" ]," |
| 507 |
" \"authorities\": {" |
| 508 |
" \"xds.example.com\": {" |
| 509 |
" \"client_listener_resource_name_template\": " |
| 510 |
"\"xds://xds.example.com/envoy.config.listener.v3.Listener/grpc/server/" |
| 511 |
"%s\"," |
| 512 |
" \"xds_servers\": [" |
| 513 |
" {" |
| 514 |
" \"server_uri\": \"fake:///xds_server\"," |
| 515 |
" \"channel_creds\": [" |
| 516 |
" {" |
| 517 |
" \"type\": \"fake\"" |
| 518 |
" }" |
| 519 |
" ]," |
| 520 |
" \"server_features\": [\"xds_v3\"]" |
| 521 |
" }" |
| 522 |
" ]" |
| 523 |
" }" |
| 524 |
" }" |
| 525 |
"}"; |
| 526 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 527 |
EXPECT_EQ(bootstrap.status().message(), |
| 528 |
"errors validating JSON: [" |
| 529 |
"field:authorities[\"xds.example.com\"]" |
| 530 |
".client_listener_resource_name_template error:" |
| 531 |
"field must begin with \"xdstp://xds.example.com/\"]") |
| 532 |
<< bootstrap.status(); |
| 533 |
UnsetEnv("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 534 |
} |
| 535 |
|
| 536 |
TEST(XdsBootstrapTest, AuthorityXdsServerMissingServerUri) { |
| 537 |
SetEnv("GRPC_EXPERIMENTAL_XDS_FEDERATION", "true"); |
| 538 |
const char* json_str = |
| 539 |
"{" |
| 540 |
" \"xds_servers\": [" |
| 541 |
" {" |
| 542 |
" \"server_uri\": \"fake:///lb\"," |
| 543 |
" \"channel_creds\": [{\"type\": \"fake\"}]" |
| 544 |
" }" |
| 545 |
" ]," |
| 546 |
" \"authorities\": {" |
| 547 |
" \"xds.example.com\": {" |
| 548 |
" \"client_listener_resource_name_template\": " |
| 549 |
"\"xdstp://xds.example.com/envoy.config.listener.v3.Listener/grpc/server/" |
| 550 |
"%s\"," |
| 551 |
" \"xds_servers\":[{}]" |
| 552 |
" }" |
| 553 |
" }" |
| 554 |
"}"; |
| 555 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 556 |
EXPECT_EQ( |
| 557 |
bootstrap.status().message(), |
| 558 |
"errors validating JSON: [" |
| 559 |
"field:authorities[\"xds.example.com\"].xds_servers[0].channel_creds " |
| 560 |
"error:field not present; " |
| 561 |
"field:authorities[\"xds.example.com\"].xds_servers[0].server_uri " |
| 562 |
"error:field not present]") |
| 563 |
<< bootstrap.status(); |
| 564 |
UnsetEnv("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 565 |
} |
| 566 |
|
| 567 |
class FakeCertificateProviderFactory : public CertificateProviderFactory { |
| 568 |
public: |
| 569 |
class Config : public CertificateProviderFactory::Config { |
| 570 |
public: |
| 571 |
explicit Config(int value) : value_(value) {} |
| 572 |
|
| 573 |
int value() const { return value_; } |
| 574 |
|
| 575 |
const char* name() const override { return "fake"; } |
| 576 |
|
| 577 |
std::string ToString() const override { |
| 578 |
return absl::StrFormat( |
| 579 |
"{\n" |
| 580 |
" value=%d" |
| 581 |
"}", |
| 582 |
value_); |
| 583 |
} |
| 584 |
|
| 585 |
private: |
| 586 |
int value_; |
| 587 |
}; |
| 588 |
|
| 589 |
const char* name() const override { return "fake"; } |
| 590 |
|
| 591 |
RefCountedPtr<CertificateProviderFactory::Config> |
| 592 |
CreateCertificateProviderConfig(const Json& config_json, |
| 593 |
grpc_error_handle* error) override { |
| 594 |
std::vector<grpc_error_handle> error_list; |
| 595 |
EXPECT_EQ(config_json.type(), Json::Type::OBJECT); |
| 596 |
auto it = config_json.object_value().find("value"); |
| 597 |
if (it == config_json.object_value().end()) { |
| 598 |
return MakeRefCounted<FakeCertificateProviderFactory::Config>(0); |
| 599 |
} else if (it->second.type() != Json::Type::NUMBER) { |
| 600 |
*error = GRPC_ERROR_CREATE("field:config field:value not of type number"); |
| 601 |
} else { |
| 602 |
int value = 0; |
| 603 |
EXPECT_TRUE(absl::SimpleAtoi(it->second.string_value(), &value)); |
| 604 |
return MakeRefCounted<FakeCertificateProviderFactory::Config>(value); |
| 605 |
} |
| 606 |
return nullptr; |
| 607 |
} |
| 608 |
|
| 609 |
RefCountedPtr<grpc_tls_certificate_provider> CreateCertificateProvider( |
| 610 |
RefCountedPtr<CertificateProviderFactory::Config> /*config*/) override { |
| 611 |
return nullptr; |
| 612 |
} |
| 613 |
}; |
| 614 |
|
| 615 |
TEST(XdsBootstrapTest, CertificateProvidersFakePluginParsingError) { |
| 616 |
const char* json_str = |
| 617 |
"{" |
| 618 |
" \"xds_servers\": [" |
| 619 |
" {" |
| 620 |
" \"server_uri\": \"fake:///lb\"," |
| 621 |
" \"channel_creds\": [{\"type\": \"fake\"}]" |
| 622 |
" }" |
| 623 |
" ]," |
| 624 |
" \"certificate_providers\": {" |
| 625 |
" \"fake_plugin\": {" |
| 626 |
" \"plugin_name\": \"fake\"," |
| 627 |
" \"config\": {" |
| 628 |
" \"value\": \"10\"" |
| 629 |
" }" |
| 630 |
" }" |
| 631 |
" }" |
| 632 |
"}"; |
| 633 |
auto bootstrap = GrpcXdsBootstrap::Create(json_str); |
| 634 |
EXPECT_THAT( |
| 635 |
// Explicit conversion to std::string to work around |
| 636 |
// https://github.com/google/googletest/issues/3949. |
| 637 |
std::string(bootstrap.status().message()), |
| 638 |
::testing::MatchesRegex( |
| 639 |
"errors validating JSON: \\[" |
| 640 |
"field:certificate_providers\\[\"fake_plugin\"\\].config " |
| 641 |
"error:UNKNOWN:field:config field:value not of type number.*\\]")) |
| 642 |
<< bootstrap.status(); |
| 643 |
} |
| 644 |
|
| 645 |
TEST(XdsBootstrapTest, CertificateProvidersFakePluginParsingSuccess) { |
| 646 |
const char* json_str = |
| 647 |
"{" |
| 648 |
" \"xds_servers\": [" |
| 649 |
" {" |
| 650 |
" \"server_uri\": \"fake:///lb\"," |
| 651 |
" \"channel_creds\": [{\"type\": \"fake\"}]" |
| 652 |
" }" |
| 653 |
" ]," |
| 654 |
" \"certificate_providers\": {" |
| 655 |
" \"fake_plugin\": {" |
| 656 |
" \"plugin_name\": \"fake\"," |
| 657 |
" \"config\": {" |
| 658 |
" \"value\": 10" |
| 659 |
" }" |
| 660 |
" }" |
| 661 |
" }" |
| 662 |
"}"; |
| 663 |
auto bootstrap_or = GrpcXdsBootstrap::Create(json_str); |
| 664 |
ASSERT_TRUE(bootstrap_or.ok()) << bootstrap_or.status(); |
| 665 |
auto bootstrap = std::move(*bootstrap_or); |
| 666 |
const CertificateProviderStore::PluginDefinition& fake_plugin = |
| 667 |
bootstrap->certificate_providers().at("fake_plugin"); |
| 668 |
ASSERT_EQ(fake_plugin.plugin_name, "fake"); |
| 669 |
ASSERT_STREQ(fake_plugin.config->name(), "fake"); |
| 670 |
ASSERT_EQ(static_cast<RefCountedPtr<FakeCertificateProviderFactory::Config>>( |
| 671 |
fake_plugin.config) |
| 672 |
->value(), |
| 673 |
10); |
| 674 |
} |
| 675 |
|
| 676 |
TEST(XdsBootstrapTest, CertificateProvidersFakePluginEmptyConfig) { |
| 677 |
const char* json_str = |
| 678 |
"{" |
| 679 |
" \"xds_servers\": [" |
| 680 |
" {" |
| 681 |
" \"server_uri\": \"fake:///lb\"," |
| 682 |
" \"channel_creds\": [{\"type\": \"fake\"}]" |
| 683 |
" }" |
| 684 |
" ]," |
| 685 |
" \"certificate_providers\": {" |
| 686 |
" \"fake_plugin\": {" |
| 687 |
" \"plugin_name\": \"fake\"" |
| 688 |
" }" |
| 689 |
" }" |
| 690 |
"}"; |
| 691 |
auto bootstrap_or = GrpcXdsBootstrap::Create(json_str); |
| 692 |
ASSERT_TRUE(bootstrap_or.ok()) << bootstrap_or.status(); |
| 693 |
auto bootstrap = std::move(*bootstrap_or); |
| 694 |
const CertificateProviderStore::PluginDefinition& fake_plugin = |
| 695 |
bootstrap->certificate_providers().at("fake_plugin"); |
| 696 |
ASSERT_EQ(fake_plugin.plugin_name, "fake"); |
| 697 |
ASSERT_STREQ(fake_plugin.config->name(), "fake"); |
| 698 |
ASSERT_EQ(static_cast<RefCountedPtr<FakeCertificateProviderFactory::Config>>( |
| 699 |
fake_plugin.config) |
| 700 |
->value(), |
| 701 |
0); |
| 702 |
} |
| 703 |
|
| 704 |
TEST(XdsBootstrapTest, XdsServerToJsonAndParse) { |
| 705 |
SetEnv("GRPC_EXPERIMENTAL_XDS_FEDERATION", "true"); |
| 706 |
const char* json_str = |
| 707 |
" {" |
| 708 |
" \"server_uri\": \"fake:///lb\"," |
| 709 |
" \"channel_creds\": [" |
| 710 |
" {" |
| 711 |
" \"type\": \"fake\"," |
| 712 |
" \"ignore\": 0" |
| 713 |
" }" |
| 714 |
" ]," |
| 715 |
" \"ignore\": 0" |
| 716 |
" }"; |
| 717 |
auto json = Json::Parse(json_str); |
| 718 |
ASSERT_TRUE(json.ok()) << json.status(); |
| 719 |
auto xds_server = LoadFromJson<GrpcXdsBootstrap::GrpcXdsServer>(*json); |
| 720 |
ASSERT_TRUE(xds_server.ok()) << xds_server.status(); |
| 721 |
Json output = xds_server->ToJson(); |
| 722 |
auto output_xds_server = |
| 723 |
LoadFromJson<GrpcXdsBootstrap::GrpcXdsServer>(output); |
| 724 |
ASSERT_TRUE(output_xds_server.ok()) << output_xds_server.status(); |
| 725 |
EXPECT_EQ(*xds_server, *output_xds_server); |
| 726 |
UnsetEnv("GRPC_EXPERIMENTAL_XDS_FEDERATION"); |
| 727 |
} |
| 728 |
|
| 729 |
} // namespace |
| 730 |
} // namespace testing |
| 731 |
} // namespace grpc_core |
| 732 |
|
| 733 |
int main(int argc, char** argv) { |
| 734 |
::testing::InitGoogleTest(&argc, argv); |
| 735 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 736 |
grpc_core::CoreConfiguration::RegisterBuilder( |
| 737 |
[](grpc_core::CoreConfiguration::Builder* builder) { |
| 738 |
builder->certificate_provider_registry() |
| 739 |
->RegisterCertificateProviderFactory( |
| 740 |
std::make_unique< |
| 741 |
grpc_core::testing::FakeCertificateProviderFactory>()); |
| 742 |
}); |
| 743 |
grpc_init(); |
| 744 |
int ret = RUN_ALL_TESTS(); |
| 745 |
grpc_shutdown(); |
| 746 |
return ret; |
| 747 |
} |