3 // Copyright 2020 gRPC authors.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/ext/xds/certificate_provider_store.h"
23 #include "src/core/ext/xds/certificate_provider_registry.h"
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;
37 auto it = certificate_providers_map_.find(key);
38 if (it == certificate_providers_map_.end()) {
39 it = certificate_providers_map_.insert({key, nullptr}).first;
41 result = it->second->RefIfNonZero();
43 if (result == nullptr) {
44 result = CreateCertificateProviderLocked(key);
45 it->second = result.get();
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()) {
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());
68 return MakeRefCounted<CertificateProviderWrapper>(
69 factory->CreateCertificateProvider(plugin_config_it->second.config), this,
70 plugin_config_it->first);
73 void CertificateProviderStore::ReleaseCertificateProvider(
74 absl::string_view key, CertificateProviderWrapper* wrapper) {
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);
84 } // namespace grpc_core