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 #ifndef GRPC_CORE_EXT_XDS_XDS_BOOTSTRAP_H
18 #define GRPC_CORE_EXT_XDS_XDS_BOOTSTRAP_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <memory>
23 #include <set>
24 #include <string>
25 #include <vector>
26 
27 #include "absl/container/inlined_vector.h"
28 
29 #include <grpc/slice.h>
30 
31 #include "src/core/ext/xds/certificate_provider_store.h"
32 #include "src/core/lib/gprpp/memory.h"
33 #include "src/core/lib/gprpp/ref_counted_ptr.h"
34 #include "src/core/lib/iomgr/error.h"
35 #include "src/core/lib/json/json.h"
36 #include "src/core/lib/security/credentials/credentials.h"
37 
38 namespace grpc_core {
39 
40 class XdsClient;
41 
42 class XdsChannelCredsRegistry {
43  public:
44   static bool IsSupported(const std::string& creds_type);
45   static bool IsValidConfig(const std::string& creds_type, const Json& config);
46   static RefCountedPtr<grpc_channel_credentials> MakeChannelCreds(
47       const std::string& creds_type, const Json& config);
48 };
49 
50 class XdsBootstrap {
51  public:
52   struct Node {
53     std::string id;
54     std::string cluster;
55     std::string locality_region;
56     std::string locality_zone;
57     std::string locality_sub_zone;
58     Json metadata;
59   };
60 
61   struct XdsServer {
62     std::string server_uri;
63     std::string channel_creds_type;
64     Json channel_creds_config;
65     std::set<std::string> server_features;
66 
67     bool operator<(const XdsServer& other) const {
68       if (server_uri < other.server_uri) return true;
69       if (channel_creds_type < other.channel_creds_type) return true;
70       if (channel_creds_config.Dump() < other.channel_creds_config.Dump()) {
71         return true;
72       }
73       if (server_features < other.server_features) return true;
74       return false;
75     }
76 
77     bool ShouldUseV3() const;
78   };
79 
80   // Creates bootstrap object from json_string.
81   // If *error is not GRPC_ERROR_NONE after returning, then there was an
82   // error parsing the contents.
83   static std::unique_ptr<XdsBootstrap> Create(absl::string_view json_string,
84                                               grpc_error_handle* error);
85 
86   // Do not instantiate directly -- use Create() above instead.
87   XdsBootstrap(Json json, grpc_error_handle* error);
88 
89   std::string ToString() const;
90 
91   // TODO(roth): We currently support only one server. Fix this when we
92   // add support for fallback for the xds channel.
server()93   const XdsServer& server() const { return servers_[0]; }
node()94   const Node* node() const { return node_.get(); }
server_listener_resource_name_template()95   const std::string& server_listener_resource_name_template() const {
96     return server_listener_resource_name_template_;
97   }
98 
certificate_providers()99   const CertificateProviderStore::PluginDefinitionMap& certificate_providers()
100       const {
101     return certificate_providers_;
102   }
103 
104  private:
105   grpc_error_handle ParseXdsServerList(Json* json);
106   grpc_error_handle ParseXdsServer(Json* json, size_t idx);
107   grpc_error_handle ParseChannelCredsArray(Json* json, XdsServer* server);
108   grpc_error_handle ParseChannelCreds(Json* json, size_t idx,
109                                       XdsServer* server);
110   grpc_error_handle ParseServerFeaturesArray(Json* json, XdsServer* server);
111   grpc_error_handle ParseNode(Json* json);
112   grpc_error_handle ParseLocality(Json* json);
113   grpc_error_handle ParseCertificateProviders(Json* json);
114   grpc_error_handle ParseCertificateProvider(const std::string& instance_name,
115                                              Json* certificate_provider_json);
116 
117   absl::InlinedVector<XdsServer, 1> servers_;
118   std::unique_ptr<Node> node_;
119   std::string server_listener_resource_name_template_;
120   CertificateProviderStore::PluginDefinitionMap certificate_providers_;
121 };
122 
123 }  // namespace grpc_core
124 
125 #endif /* GRPC_CORE_EXT_XDS_XDS_BOOTSTRAP_H */
126