| 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 <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include "src/core/ext/xds/xds_bootstrap_grpc.h" |
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <set> |
| 25 |
#include <utility> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
#include "absl/status/status.h" |
| 29 |
#include "absl/status/statusor.h" |
| 30 |
#include "absl/strings/match.h" |
| 31 |
#include "absl/strings/str_cat.h" |
| 32 |
#include "absl/strings/str_format.h" |
| 33 |
#include "absl/strings/str_join.h" |
| 34 |
#include "absl/strings/string_view.h" |
| 35 |
#include "absl/types/optional.h" |
| 36 |
|
| 37 |
#include "src/core/lib/config/core_configuration.h" |
| 38 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 39 |
#include "src/core/lib/json/json.h" |
| 40 |
#include "src/core/lib/json/json_object_loader.h" |
| 41 |
#include "src/core/lib/security/credentials/channel_creds_registry.h" |
| 42 |
|
| 43 |
namespace grpc_core { |
| 44 |
|
| 45 |
// |
| 46 |
// GrpcXdsBootstrap::GrpcNode::Locality |
| 47 |
// |
| 48 |
|
| 49 |
const JsonLoaderInterface* GrpcXdsBootstrap::GrpcNode::Locality::JsonLoader( |
| 50 |
const JsonArgs&) { |
| 51 |
static const auto* loader = |
| 52 |
JsonObjectLoader<Locality>() |
| 53 |
.OptionalField("region", &Locality::region) |
| 54 |
.OptionalField("zone", &Locality::zone) |
| 55 |
.OptionalField("sub_zone", &Locality::sub_zone) |
| 56 |
.Finish(); |
| 57 |
return loader; |
| 58 |
} |
| 59 |
|
| 60 |
// |
| 61 |
// GrpcXdsBootstrap::GrpcNode |
| 62 |
// |
| 63 |
|
| 64 |
const JsonLoaderInterface* GrpcXdsBootstrap::GrpcNode::JsonLoader( |
| 65 |
const JsonArgs&) { |
| 66 |
static const auto* loader = |
| 67 |
JsonObjectLoader<GrpcNode>() |
| 68 |
.OptionalField("id", &GrpcNode::id_) |
| 69 |
.OptionalField("cluster", &GrpcNode::cluster_) |
| 70 |
.OptionalField("locality", &GrpcNode::locality_) |
| 71 |
.OptionalField("metadata", &GrpcNode::metadata_) |
| 72 |
.Finish(); |
| 73 |
return loader; |
| 74 |
} |
| 75 |
|
| 76 |
// |
| 77 |
// GrpcXdsBootstrap::GrpcXdsServer::ChannelCreds |
| 78 |
// |
| 79 |
|
| 80 |
const JsonLoaderInterface* |
| 81 |
GrpcXdsBootstrap::GrpcXdsServer::ChannelCreds::JsonLoader(const JsonArgs&) { |
| 82 |
static const auto* loader = |
| 83 |
JsonObjectLoader<ChannelCreds>() |
| 84 |
.Field("type", &ChannelCreds::type) |
| 85 |
.OptionalField("config", &ChannelCreds::config) |
| 86 |
.Finish(); |
| 87 |
return loader; |
| 88 |
} |
| 89 |
|
| 90 |
// |
| 91 |
// GrpcXdsBootstrap::GrpcXdsServer |
| 92 |
// |
| 93 |
|
| 94 |
namespace { |
| 95 |
|
| 96 |
constexpr absl::string_view kServerFeatureIgnoreResourceDeletion = |
| 97 |
"ignore_resource_deletion"; |
| 98 |
|
| 99 |
} // namespace |
| 100 |
|
| 101 |
bool GrpcXdsBootstrap::GrpcXdsServer::IgnoreResourceDeletion() const { |
| 102 |
return server_features_.find(std::string( |
| 103 |
kServerFeatureIgnoreResourceDeletion)) != server_features_.end(); |
| 104 |
} |
| 105 |
|
| 106 |
bool GrpcXdsBootstrap::GrpcXdsServer::Equals(const XdsServer& other) const { |
| 107 |
const auto& o = static_cast<const GrpcXdsServer&>(other); |
| 108 |
return (server_uri_ == o.server_uri_ && |
| 109 |
channel_creds_.type == o.channel_creds_.type && |
| 110 |
channel_creds_.config == o.channel_creds_.config && |
| 111 |
server_features_ == o.server_features_); |
| 112 |
} |
| 113 |
|
| 114 |
const JsonLoaderInterface* GrpcXdsBootstrap::GrpcXdsServer::JsonLoader( |
| 115 |
const JsonArgs&) { |
| 116 |
static const auto* loader = |
| 117 |
JsonObjectLoader<GrpcXdsServer>() |
| 118 |
.Field("server_uri", &GrpcXdsServer::server_uri_) |
| 119 |
.Finish(); |
| 120 |
return loader; |
| 121 |
} |
| 122 |
|
| 123 |
void GrpcXdsBootstrap::GrpcXdsServer::JsonPostLoad(const Json& json, |
| 124 |
const JsonArgs& args, |
| 125 |
ValidationErrors* errors) { |
| 126 |
// Parse "channel_creds". |
| 127 |
auto channel_creds_list = LoadJsonObjectField<std::vector<ChannelCreds>>( |
| 128 |
json.object_value(), args, "channel_creds", errors); |
| 129 |
if (channel_creds_list.has_value()) { |
| 130 |
ValidationErrors::ScopedField field(errors, ".channel_creds"); |
| 131 |
for (size_t i = 0; i < channel_creds_list->size(); ++i) { |
| 132 |
ValidationErrors::ScopedField field(errors, absl::StrCat("[", i, "]")); |
| 133 |
auto& creds = (*channel_creds_list)[i]; |
| 134 |
// Select the first channel creds type that we support. |
| 135 |
if (channel_creds_.type.empty() && |
| 136 |
CoreConfiguration::Get().channel_creds_registry().IsSupported( |
| 137 |
creds.type)) { |
| 138 |
if (!CoreConfiguration::Get().channel_creds_registry().IsValidConfig( |
| 139 |
creds.type, creds.config)) { |
| 140 |
errors->AddError(absl::StrCat( |
| 141 |
"invalid config for channel creds type \"", creds.type, "\"")); |
| 142 |
continue; |
| 143 |
} |
| 144 |
channel_creds_.type = std::move(creds.type); |
| 145 |
channel_creds_.config = std::move(creds.config); |
| 146 |
} |
| 147 |
} |
| 148 |
if (channel_creds_.type.empty()) { |
| 149 |
errors->AddError("no known creds type found"); |
| 150 |
} |
| 151 |
} |
| 152 |
// Parse "server_features". |
| 153 |
{ |
| 154 |
ValidationErrors::ScopedField field(errors, ".server_features"); |
| 155 |
auto it = json.object_value().find("server_features"); |
| 156 |
if (it != json.object_value().end()) { |
| 157 |
if (it->second.type() != Json::Type::ARRAY) { |
| 158 |
errors->AddError("is not an array"); |
| 159 |
} else { |
| 160 |
const Json::Array& array = it->second.array_value(); |
| 161 |
for (const Json& feature_json : array) { |
| 162 |
if (feature_json.type() == Json::Type::STRING && |
| 163 |
(feature_json.string_value() == |
| 164 |
kServerFeatureIgnoreResourceDeletion)) { |
| 165 |
server_features_.insert(feature_json.string_value()); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
Json GrpcXdsBootstrap::GrpcXdsServer::ToJson() const { |
| 174 |
Json::Object channel_creds_json{{"type", channel_creds_.type}}; |
| 175 |
if (!channel_creds_.config.empty()) { |
| 176 |
channel_creds_json["config"] = channel_creds_.config; |
| 177 |
} |
| 178 |
Json::Object json{ |
| 179 |
{"server_uri", server_uri_}, |
| 180 |
{"channel_creds", Json::Array{std::move(channel_creds_json)}}, |
| 181 |
}; |
| 182 |
if (!server_features_.empty()) { |
| 183 |
Json::Array server_features_json; |
| 184 |
for (auto& feature : server_features_) { |
| 185 |
server_features_json.emplace_back(feature); |
| 186 |
} |
| 187 |
json["server_features"] = std::move(server_features_json); |
| 188 |
} |
| 189 |
return json; |
| 190 |
} |
| 191 |
|
| 192 |
// |
| 193 |
// GrpcXdsBootstrap::GrpcAuthority |
| 194 |
// |
| 195 |
|
| 196 |
const JsonLoaderInterface* GrpcXdsBootstrap::GrpcAuthority::JsonLoader( |
| 197 |
const JsonArgs&) { |
| 198 |
static const auto* loader = |
| 199 |
JsonObjectLoader<GrpcAuthority>() |
| 200 |
.OptionalField( |
| 201 |
"client_listener_resource_name_template", |
| 202 |
&GrpcAuthority::client_listener_resource_name_template_) |
| 203 |
.OptionalField("xds_servers", &GrpcAuthority::servers_) |
| 204 |
.Finish(); |
| 205 |
return loader; |
| 206 |
} |
| 207 |
|
| 208 |
// |
| 209 |
// GrpcXdsBootstrap |
| 210 |
// |
| 211 |
|
| 212 |
absl::StatusOr<std::unique_ptr<GrpcXdsBootstrap>> GrpcXdsBootstrap::Create( |
| 213 |
absl::string_view json_string) { |
| 214 |
auto json = Json::Parse(json_string); |
| 215 |
if (!json.ok()) { |
| 216 |
return absl::InvalidArgumentError(absl::StrCat( |
| 217 |
"Failed to parse bootstrap JSON string: ", json.status().ToString())); |
| 218 |
} |
| 219 |
// Validate JSON. |
| 220 |
class XdsJsonArgs : public JsonArgs { |
| 221 |
public: |
| 222 |
bool IsEnabled(absl::string_view key) const override { |
| 223 |
if (key == "federation") return XdsFederationEnabled(); |
| 224 |
return true; |
| 225 |
} |
| 226 |
}; |
| 227 |
auto bootstrap = LoadFromJson<GrpcXdsBootstrap>(*json, XdsJsonArgs()); |
| 228 |
if (!bootstrap.ok()) return bootstrap.status(); |
| 229 |
return std::make_unique<GrpcXdsBootstrap>(std::move(*bootstrap)); |
| 230 |
} |
| 231 |
|
| 232 |
const JsonLoaderInterface* GrpcXdsBootstrap::JsonLoader(const JsonArgs&) { |
| 233 |
static const auto* loader = |
| 234 |
JsonObjectLoader<GrpcXdsBootstrap>() |
| 235 |
.Field("xds_servers", &GrpcXdsBootstrap::servers_) |
| 236 |
.OptionalField("node", &GrpcXdsBootstrap::node_) |
| 237 |
.OptionalField("certificate_providers", |
| 238 |
&GrpcXdsBootstrap::certificate_providers_) |
| 239 |
.OptionalField( |
| 240 |
"server_listener_resource_name_template", |
| 241 |
&GrpcXdsBootstrap::server_listener_resource_name_template_) |
| 242 |
.OptionalField("authorities", &GrpcXdsBootstrap::authorities_, |
| 243 |
"federation") |
| 244 |
.OptionalField("client_default_listener_resource_name_template", |
| 245 |
&GrpcXdsBootstrap:: |
| 246 |
client_default_listener_resource_name_template_, |
| 247 |
"federation") |
| 248 |
.Finish(); |
| 249 |
return loader; |
| 250 |
} |
| 251 |
|
| 252 |
void GrpcXdsBootstrap::JsonPostLoad(const Json& /*json*/, |
| 253 |
const JsonArgs& /*args*/, |
| 254 |
ValidationErrors* errors) { |
| 255 |
// Verify that each authority has the right prefix in the |
| 256 |
// client_listener_resource_name_template field. |
| 257 |
{ |
| 258 |
ValidationErrors::ScopedField field(errors, ".authorities"); |
| 259 |
for (const auto& p : authorities_) { |
| 260 |
const std::string& name = p.first; |
| 261 |
const GrpcAuthority& authority = |
| 262 |
static_cast<const GrpcAuthority&>(p.second); |
| 263 |
ValidationErrors::ScopedField field( |
| 264 |
errors, absl::StrCat("[\"", name, |
| 265 |
"\"].client_listener_resource_name_template")); |
| 266 |
std::string expected_prefix = absl::StrCat("xdstp://", name, "/"); |
| 267 |
if (!authority.client_listener_resource_name_template().empty() && |
| 268 |
!absl::StartsWith(authority.client_listener_resource_name_template(), |
| 269 |
expected_prefix)) { |
| 270 |
errors->AddError( |
| 271 |
absl::StrCat("field must begin with \"", expected_prefix, "\"")); |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
std::string GrpcXdsBootstrap::ToString() const { |
| 278 |
std::vector<std::string> parts; |
| 279 |
if (node_.has_value()) { |
| 280 |
parts.push_back( |
| 281 |
absl::StrFormat("node={\n" |
| 282 |
" id=\"%s\",\n" |
| 283 |
" cluster=\"%s\",\n" |
| 284 |
" locality={\n" |
| 285 |
" region=\"%s\",\n" |
| 286 |
" zone=\"%s\",\n" |
| 287 |
" sub_zone=\"%s\"\n" |
| 288 |
" },\n" |
| 289 |
" metadata=%s,\n" |
| 290 |
"},\n", |
| 291 |
node_->id(), node_->cluster(), node_->locality_region(), |
| 292 |
node_->locality_zone(), node_->locality_sub_zone(), |
| 293 |
Json{node_->metadata()}.Dump())); |
| 294 |
} |
| 295 |
parts.push_back( |
| 296 |
absl::StrFormat("servers=[\n%s\n],\n", servers_[0].ToJson().Dump())); |
| 297 |
if (!client_default_listener_resource_name_template_.empty()) { |
| 298 |
parts.push_back(absl::StrFormat( |
| 299 |
"client_default_listener_resource_name_template=\"%s\",\n", |
| 300 |
client_default_listener_resource_name_template_)); |
| 301 |
} |
| 302 |
if (!server_listener_resource_name_template_.empty()) { |
| 303 |
parts.push_back( |
| 304 |
absl::StrFormat("server_listener_resource_name_template=\"%s\",\n", |
| 305 |
server_listener_resource_name_template_)); |
| 306 |
} |
| 307 |
parts.push_back("authorities={\n"); |
| 308 |
for (const auto& entry : authorities_) { |
| 309 |
parts.push_back(absl::StrFormat(" %s={\n", entry.first)); |
| 310 |
parts.push_back( |
| 311 |
absl::StrFormat(" client_listener_resource_name_template=\"%s\",\n", |
| 312 |
entry.second.client_listener_resource_name_template())); |
| 313 |
if (entry.second.server() != nullptr) { |
| 314 |
parts.push_back(absl::StrFormat( |
| 315 |
" servers=[\n%s\n],\n", |
| 316 |
static_cast<const GrpcXdsServer*>(entry.second.server()) |
| 317 |
->ToJson() |
| 318 |
.Dump())); |
| 319 |
} |
| 320 |
parts.push_back(" },\n"); |
| 321 |
} |
| 322 |
parts.push_back("}\n"); |
| 323 |
parts.push_back("certificate_providers={\n"); |
| 324 |
for (const auto& entry : certificate_providers_) { |
| 325 |
parts.push_back( |
| 326 |
absl::StrFormat(" %s={\n" |
| 327 |
" plugin_name=%s\n" |
| 328 |
" config=%s\n" |
| 329 |
" },\n", |
| 330 |
entry.first, entry.second.plugin_name, |
| 331 |
entry.second.config->ToString())); |
| 332 |
} |
| 333 |
parts.push_back("}"); |
| 334 |
return absl::StrJoin(parts, ""); |
| 335 |
} |
| 336 |
|
| 337 |
const XdsBootstrap::Authority* GrpcXdsBootstrap::LookupAuthority( |
| 338 |
const std::string& name) const { |
| 339 |
auto it = authorities_.find(name); |
| 340 |
if (it != authorities_.end()) { |
| 341 |
return &it->second; |
| 342 |
} |
| 343 |
return nullptr; |
| 344 |
} |
| 345 |
|
| 346 |
const XdsBootstrap::XdsServer* GrpcXdsBootstrap::FindXdsServer( |
| 347 |
const XdsBootstrap::XdsServer& server) const { |
| 348 |
if (static_cast<const GrpcXdsServer&>(server) == servers_[0]) { |
| 349 |
return &servers_[0]; |
| 350 |
} |
| 351 |
for (auto& p : authorities_) { |
| 352 |
const auto* authority_server = |
| 353 |
static_cast<const GrpcXdsServer*>(p.second.server()); |
| 354 |
if (authority_server != nullptr && *authority_server == server) { |
| 355 |
return authority_server; |
| 356 |
} |
| 357 |
} |
| 358 |
return nullptr; |
| 359 |
} |
| 360 |
|
| 361 |
} // namespace grpc_core |