Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / src / core / ext / xds / certificate_provider_store.cc
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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/ext/xds/certificate_provider_store.h"
22
23 #include "src/core/ext/xds/certificate_provider_registry.h"
24
25 namespace grpc_core {
26
27 // If a certificate provider is created, the CertificateProviderStore
28 // maintains a raw pointer to the created CertificateProviderWrapper so that
29 // future calls to `CreateOrGetCertificateProvider()` with the same key result
30 // in returning a ref to this created certificate provider. This entry is
31 // deleted when the refcount to this provider reaches zero.
32 RefCountedPtr<grpc_tls_certificate_provider>
33 CertificateProviderStore::CreateOrGetCertificateProvider(
34     absl::string_view key) {
35   RefCountedPtr<CertificateProviderWrapper> result;
36   MutexLock lock(&mu_);
37   auto it = certificate_providers_map_.find(key);
38   if (it == certificate_providers_map_.end()) {
39     it = certificate_providers_map_.insert({key, nullptr}).first;
40   } else {
41     result = it->second->RefIfNonZero();
42   }
43   if (result == nullptr) {
44     result = CreateCertificateProviderLocked(key);
45     it->second = result.get();
46   }
47   return result;
48 }
49
50 RefCountedPtr<CertificateProviderStore::CertificateProviderWrapper>
51 CertificateProviderStore::CreateCertificateProviderLocked(
52     absl::string_view key) {
53   auto plugin_config_it = plugin_config_map_.find(std::string(key));
54   if (plugin_config_it == plugin_config_map_.end()) {
55     return nullptr;
56   }
57   CertificateProviderFactory* factory =
58       CertificateProviderRegistry::LookupCertificateProviderFactory(
59           plugin_config_it->second.plugin_name);
60   if (factory == nullptr) {
61     // This should never happen since an entry is only inserted in the
62     // plugin_config_map_ if the corresponding factory was found when parsing
63     // the xDS bootstrap file.
64     gpr_log(GPR_ERROR, "Certificate provider factory %s not found",
65             plugin_config_it->second.plugin_name.c_str());
66     return nullptr;
67   }
68   return MakeRefCounted<CertificateProviderWrapper>(
69       factory->CreateCertificateProvider(plugin_config_it->second.config), this,
70       plugin_config_it->first);
71 }
72
73 void CertificateProviderStore::ReleaseCertificateProvider(
74     absl::string_view key, CertificateProviderWrapper* wrapper) {
75   MutexLock lock(&mu_);
76   auto it = certificate_providers_map_.find(key);
77   if (it != certificate_providers_map_.end()) {
78     if (it->second == wrapper) {
79       certificate_providers_map_.erase(it);
80     }
81   }
82 }
83
84 }  // namespace grpc_core