• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
CreateOrGetCertificateProvider(absl::string_view key)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     result = CreateCertificateProviderLocked(key);
40     if (result != nullptr) {
41       certificate_providers_map_.insert({result->key(), result.get()});
42     }
43   } else {
44     result = it->second->RefIfNonZero();
45     if (result == nullptr) {
46       result = CreateCertificateProviderLocked(key);
47       it->second = result.get();
48     }
49   }
50   return result;
51 }
52 
53 RefCountedPtr<CertificateProviderStore::CertificateProviderWrapper>
CreateCertificateProviderLocked(absl::string_view key)54 CertificateProviderStore::CreateCertificateProviderLocked(
55     absl::string_view key) {
56   auto plugin_config_it = plugin_config_map_.find(std::string(key));
57   if (plugin_config_it == plugin_config_map_.end()) {
58     return nullptr;
59   }
60   CertificateProviderFactory* factory =
61       CertificateProviderRegistry::LookupCertificateProviderFactory(
62           plugin_config_it->second.plugin_name);
63   if (factory == nullptr) {
64     // This should never happen since an entry is only inserted in the
65     // plugin_config_map_ if the corresponding factory was found when parsing
66     // the xDS bootstrap file.
67     gpr_log(GPR_ERROR, "Certificate provider factory %s not found",
68             plugin_config_it->second.plugin_name.c_str());
69     return nullptr;
70   }
71   return MakeRefCounted<CertificateProviderWrapper>(
72       factory->CreateCertificateProvider(plugin_config_it->second.config),
73       Ref(), plugin_config_it->first);
74 }
75 
ReleaseCertificateProvider(absl::string_view key,CertificateProviderWrapper * wrapper)76 void CertificateProviderStore::ReleaseCertificateProvider(
77     absl::string_view key, CertificateProviderWrapper* wrapper) {
78   MutexLock lock(&mu_);
79   auto it = certificate_providers_map_.find(key);
80   if (it != certificate_providers_map_.end()) {
81     if (it->second == wrapper) {
82       certificate_providers_map_.erase(it);
83     }
84   }
85 }
86 
87 }  // namespace grpc_core
88