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_SRC_CORE_EXT_XDS_XDS_BOOTSTRAP_GRPC_H
18 #define GRPC_SRC_CORE_EXT_XDS_XDS_BOOTSTRAP_GRPC_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <map>
23 #include <memory>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include "absl/status/statusor.h"
29 #include "absl/strings/string_view.h"
30 #include "absl/types/optional.h"
31 
32 #include "src/core/ext/xds/certificate_provider_store.h"
33 #include "src/core/ext/xds/xds_audit_logger_registry.h"
34 #include "src/core/ext/xds/xds_bootstrap.h"
35 #include "src/core/ext/xds/xds_cluster_specifier_plugin.h"
36 #include "src/core/ext/xds/xds_http_filters.h"
37 #include "src/core/ext/xds/xds_lb_policy_registry.h"
38 #include "src/core/lib/gprpp/validation_errors.h"
39 #include "src/core/lib/json/json.h"
40 #include "src/core/lib/json/json_args.h"
41 #include "src/core/lib/json/json_object_loader.h"
42 
43 namespace grpc_core {
44 
45 class GrpcXdsBootstrap : public XdsBootstrap {
46  public:
47   class GrpcNode : public Node {
48    public:
id()49     const std::string& id() const override { return id_; }
cluster()50     const std::string& cluster() const override { return cluster_; }
locality_region()51     const std::string& locality_region() const override {
52       return locality_.region;
53     }
locality_zone()54     const std::string& locality_zone() const override { return locality_.zone; }
locality_sub_zone()55     const std::string& locality_sub_zone() const override {
56       return locality_.sub_zone;
57     }
metadata()58     const Json::Object& metadata() const override { return metadata_; }
59 
60     static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
61 
62    private:
63     struct Locality {
64       std::string region;
65       std::string zone;
66       std::string sub_zone;
67 
68       static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
69     };
70 
71     std::string id_;
72     std::string cluster_;
73     Locality locality_;
74     Json::Object metadata_;
75   };
76 
77   class GrpcXdsServer : public XdsServer {
78    public:
server_uri()79     const std::string& server_uri() const override { return server_uri_; }
80 
81     bool IgnoreResourceDeletion() const override;
82 
83     bool Equals(const XdsServer& other) const override;
84 
channel_creds_type()85     const std::string& channel_creds_type() const {
86       return channel_creds_.type;
87     }
channel_creds_config()88     const Json::Object& channel_creds_config() const {
89       return channel_creds_.config;
90     }
91 
92     static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
93     void JsonPostLoad(const Json& json, const JsonArgs& args,
94                       ValidationErrors* errors);
95 
96     Json ToJson() const;
97 
98    private:
99     struct ChannelCreds {
100       std::string type;
101       Json::Object config;
102 
103       static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
104     };
105 
106     std::string server_uri_;
107     ChannelCreds channel_creds_;
108     std::set<std::string> server_features_;
109   };
110 
111   class GrpcAuthority : public Authority {
112    public:
server()113     const XdsServer* server() const override {
114       return servers_.empty() ? nullptr : &servers_[0];
115     }
116 
client_listener_resource_name_template()117     const std::string& client_listener_resource_name_template() const {
118       return client_listener_resource_name_template_;
119     }
120 
121     static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
122 
123    private:
124     std::vector<GrpcXdsServer> servers_;
125     std::string client_listener_resource_name_template_;
126   };
127 
128   // Creates bootstrap object from json_string.
129   static absl::StatusOr<std::unique_ptr<GrpcXdsBootstrap>> Create(
130       absl::string_view json_string);
131 
132   static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
133   void JsonPostLoad(const Json& json, const JsonArgs& args,
134                     ValidationErrors* errors);
135 
136   std::string ToString() const override;
137 
server()138   const XdsServer& server() const override { return servers_[0]; }
node()139   const Node* node() const override {
140     return node_.has_value() ? &*node_ : nullptr;
141   }
142   const Authority* LookupAuthority(const std::string& name) const override;
143   const XdsServer* FindXdsServer(const XdsServer& server) const override;
144 
client_default_listener_resource_name_template()145   const std::string& client_default_listener_resource_name_template() const {
146     return client_default_listener_resource_name_template_;
147   }
server_listener_resource_name_template()148   const std::string& server_listener_resource_name_template() const {
149     return server_listener_resource_name_template_;
150   }
certificate_providers()151   const CertificateProviderStore::PluginDefinitionMap& certificate_providers()
152       const {
153     return certificate_providers_;
154   }
http_filter_registry()155   const XdsHttpFilterRegistry& http_filter_registry() const {
156     return http_filter_registry_;
157   }
cluster_specifier_plugin_registry()158   const XdsClusterSpecifierPluginRegistry& cluster_specifier_plugin_registry()
159       const {
160     return cluster_specifier_plugin_registry_;
161   }
lb_policy_registry()162   const XdsLbPolicyRegistry& lb_policy_registry() const {
163     return lb_policy_registry_;
164   }
audit_logger_registry()165   const XdsAuditLoggerRegistry& audit_logger_registry() const {
166     return audit_logger_registry_;
167   }
168 
169   // Exposed for testing purposes only.
authorities()170   const std::map<std::string, GrpcAuthority>& authorities() const {
171     return authorities_;
172   }
173 
174  private:
175   std::vector<GrpcXdsServer> servers_;
176   absl::optional<GrpcNode> node_;
177   std::string client_default_listener_resource_name_template_;
178   std::string server_listener_resource_name_template_;
179   std::map<std::string, GrpcAuthority> authorities_;
180   CertificateProviderStore::PluginDefinitionMap certificate_providers_;
181   XdsHttpFilterRegistry http_filter_registry_;
182   XdsClusterSpecifierPluginRegistry cluster_specifier_plugin_registry_;
183   XdsLbPolicyRegistry lb_policy_registry_;
184   XdsAuditLoggerRegistry audit_logger_registry_;
185 };
186 
187 }  // namespace grpc_core
188 
189 #endif  // GRPC_SRC_CORE_EXT_XDS_XDS_BOOTSTRAP_GRPC_H
190