Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / src / core / ext / xds / xds_bootstrap.h
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/map.h"
33 #include "src/core/lib/gprpp/memory.h"
34 #include "src/core/lib/gprpp/ref_counted_ptr.h"
35 #include "src/core/lib/iomgr/error.h"
36 #include "src/core/lib/json/json.h"
37 #include "src/core/lib/security/credentials/credentials.h"
38
39 namespace grpc_core {
40
41 class XdsClient;
42
43 class XdsChannelCredsRegistry {
44  public:
45   static bool IsSupported(const std::string& creds_type);
46   static bool IsValidConfig(const std::string& creds_type, const Json& config);
47   static RefCountedPtr<grpc_channel_credentials> MakeChannelCreds(
48       const std::string& creds_type, const Json& config);
49 };
50
51 class XdsBootstrap {
52  public:
53   struct Node {
54     std::string id;
55     std::string cluster;
56     std::string locality_region;
57     std::string locality_zone;
58     std::string locality_subzone;
59     Json metadata;
60   };
61
62   struct XdsServer {
63     std::string server_uri;
64     std::string channel_creds_type;
65     Json channel_creds_config;
66     std::set<std::string> server_features;
67
68     bool ShouldUseV3() const;
69   };
70
71   // If *error is not GRPC_ERROR_NONE after returning, then there was an
72   // error reading the file.
73   static std::unique_ptr<XdsBootstrap> ReadFromFile(XdsClient* client,
74                                                     TraceFlag* tracer,
75                                                     grpc_error** error);
76
77   // Do not instantiate directly -- use ReadFromFile() above instead.
78   XdsBootstrap(Json json, grpc_error** error);
79
80   // TODO(roth): We currently support only one server. Fix this when we
81   // add support for fallback for the xds channel.
82   const XdsServer& server() const { return servers_[0]; }
83   const Node* node() const { return node_.get(); }
84
85   const CertificateProviderStore::PluginDefinitionMap& certificate_providers()
86       const {
87     return certificate_providers_;
88   }
89
90  private:
91   grpc_error* ParseXdsServerList(Json* json);
92   grpc_error* ParseXdsServer(Json* json, size_t idx);
93   grpc_error* ParseChannelCredsArray(Json* json, XdsServer* server);
94   grpc_error* ParseChannelCreds(Json* json, size_t idx, XdsServer* server);
95   grpc_error* ParseServerFeaturesArray(Json* json, XdsServer* server);
96   grpc_error* ParseNode(Json* json);
97   grpc_error* ParseLocality(Json* json);
98   grpc_error* ParseCertificateProviders(Json* json);
99   grpc_error* ParseCertificateProvider(const std::string& instance_name,
100                                        Json* certificate_provider_json);
101
102   absl::InlinedVector<XdsServer, 1> servers_;
103   std::unique_ptr<Node> node_;
104   CertificateProviderStore::PluginDefinitionMap certificate_providers_;
105 };
106
107 }  // namespace grpc_core
108
109 #endif /* GRPC_CORE_EXT_XDS_XDS_BOOTSTRAP_H */