GRPC Core  18.0.0
certificate_provider_store.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2020 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_CORE_EXT_XDS_CERTIFICATE_PROVIDER_STORE_H
20 #define GRPC_CORE_EXT_XDS_CERTIFICATE_PROVIDER_STORE_H
21 
23 
24 #include <map>
25 
26 #include "absl/strings/string_view.h"
27 
33 
34 namespace grpc_core {
35 
36 // Map for xDS based grpc_tls_certificate_provider instances.
38  : public InternallyRefCounted<CertificateProviderStore> {
39  public:
41  std::string plugin_name;
43  };
44 
45  // Maps plugin instance (opaque) name to plugin defition.
46  typedef std::map<std::string, PluginDefinition> PluginDefinitionMap;
47 
48  explicit CertificateProviderStore(PluginDefinitionMap plugin_config_map)
49  : plugin_config_map_(std::move(plugin_config_map)) {}
50 
51  // If a certificate provider corresponding to the instance name \a key is
52  // found, a ref to the grpc_tls_certificate_provider is returned. If no
53  // provider is found for the key, a new provider is created from the plugin
54  // definition map.
55  // Returns nullptr on failure to get or create a new certificate provider.
57  absl::string_view key);
58 
59  void Orphan() override { Unref(); }
60 
61  private:
62  // A thin wrapper around `grpc_tls_certificate_provider` which allows removing
63  // the entry from the CertificateProviderStore when the refcount reaches zero.
64  class CertificateProviderWrapper : public grpc_tls_certificate_provider {
65  public:
66  CertificateProviderWrapper(
68  RefCountedPtr<CertificateProviderStore> store, absl::string_view key)
69  : certificate_provider_(std::move(certificate_provider)),
70  store_(std::move(store)),
71  key_(key) {}
72 
73  ~CertificateProviderWrapper() override {
74  store_->ReleaseCertificateProvider(key_, this);
75  }
76 
78  const override {
79  return certificate_provider_->distributor();
80  }
81 
82  grpc_pollset_set* interested_parties() const override {
83  return certificate_provider_->interested_parties();
84  }
85 
86  absl::string_view key() const { return key_; }
87 
88  private:
89  RefCountedPtr<grpc_tls_certificate_provider> certificate_provider_;
90  RefCountedPtr<CertificateProviderStore> store_;
91  absl::string_view key_;
92  };
93 
94  RefCountedPtr<CertificateProviderWrapper> CreateCertificateProviderLocked(
95  absl::string_view key) ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_);
96 
97  // Releases a previously created certificate provider from the certificate
98  // provider map if the value matches \a wrapper.
99  void ReleaseCertificateProvider(absl::string_view key,
100  CertificateProviderWrapper* wrapper);
101 
102  Mutex mu_;
103  // Map of plugin configurations
104  PluginDefinitionMap plugin_config_map_ ABSL_GUARDED_BY(mu_);
105  // Underlying map for the providers.
106  std::map<absl::string_view, CertificateProviderWrapper*>
107  certificate_providers_map_ ABSL_GUARDED_BY(mu_);
108 };
109 
110 } // namespace grpc_core
111 
112 #endif // GRPC_CORE_EXT_XDS_CERTIFICATE_PROVIDER_STORE_H
Definition: certificate_provider_store.h:38
std::map< std::string, PluginDefinition > PluginDefinitionMap
Definition: certificate_provider_store.h:46
CertificateProviderStore(PluginDefinitionMap plugin_config_map)
Definition: certificate_provider_store.h:48
void Orphan() override
Definition: certificate_provider_store.h:59
RefCountedPtr< grpc_tls_certificate_provider > CreateOrGetCertificateProvider(absl::string_view key)
Definition: certificate_provider_store.cc:33
Definition: orphanable.h:76
Definition: ref_counted_ptr.h:35
Round Robin Policy.
Definition: backend_metric.cc:26
struct grpc_pollset_set grpc_pollset_set
Definition: pollset_set.h:31
Definition: certificate_provider_store.h:40
std::string plugin_name
Definition: certificate_provider_store.h:41
RefCountedPtr< CertificateProviderFactory::Config > config
Definition: certificate_provider_store.h:42
Definition: grpc_tls_certificate_provider.h:45