| 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 "absl/strings/str_cat.h" |
| 24 |
|
| 25 |
#include <grpc/support/log.h> |
| 26 |
|
| 27 |
#include "src/core/lib/config/core_configuration.h" |
| 28 |
#include "src/core/lib/gprpp/status_helper.h" |
| 29 |
#include "src/core/lib/iomgr/error.h" |
| 30 |
#include "src/core/lib/security/certificate_provider/certificate_provider_registry.h" |
| 31 |
|
| 32 |
namespace grpc_core { |
| 33 |
|
| 34 |
// |
| 35 |
// CertificateProviderStore::PluginDefinition |
| 36 |
// |
| 37 |
|
| 38 |
const JsonLoaderInterface* |
| 39 |
CertificateProviderStore::PluginDefinition::JsonLoader(const JsonArgs&) { |
| 40 |
static const auto* loader = |
| 41 |
JsonObjectLoader<PluginDefinition>() |
| 42 |
.Field("plugin_name", &PluginDefinition::plugin_name) |
| 43 |
.Finish(); |
| 44 |
return loader; |
| 45 |
} |
| 46 |
|
| 47 |
void CertificateProviderStore::PluginDefinition::JsonPostLoad( |
| 48 |
const Json& json, const JsonArgs&, ValidationErrors* errors) { |
| 49 |
// Check that plugin is supported. |
| 50 |
CertificateProviderFactory* factory = nullptr; |
| 51 |
if (!plugin_name.empty()) { |
| 52 |
ValidationErrors::ScopedField field(errors, ".plugin_name"); |
| 53 |
factory = CoreConfiguration::Get() |
| 54 |
.certificate_provider_registry() |
| 55 |
.LookupCertificateProviderFactory(plugin_name); |
| 56 |
if (factory == nullptr) { |
| 57 |
errors->AddError(absl::StrCat("Unrecognized plugin name: ", plugin_name)); |
| 58 |
return; // No point checking config. |
| 59 |
} |
| 60 |
} |
| 61 |
// Parse the config field. |
| 62 |
{ |
| 63 |
ValidationErrors::ScopedField field(errors, ".config"); |
| 64 |
auto it = json.object_value().find("config"); |
| 65 |
// The config field is optional; if not present, we use an empty JSON |
| 66 |
// object. |
| 67 |
Json::Object config_json; |
| 68 |
if (it != json.object_value().end()) { |
| 69 |
if (it->second.type() != Json::Type::OBJECT) { |
| 70 |
errors->AddError("is not an object"); |
| 71 |
return; // No point parsing config. |
| 72 |
} else { |
| 73 |
config_json = it->second.object_value(); |
| 74 |
} |
| 75 |
} |
| 76 |
if (factory == nullptr) return; |
| 77 |
// Use plugin to validate and parse config. |
| 78 |
grpc_error_handle parse_error; |
| 79 |
config = |
| 80 |
factory->CreateCertificateProviderConfig(config_json, &parse_error); |
| 81 |
if (!parse_error.ok()) { |
| 82 |
errors->AddError(StatusToString(parse_error)); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// |
| 88 |
// CertificateProviderStore::CertificateProviderWrapper |
| 89 |
// |
| 90 |
|
| 91 |
UniqueTypeName CertificateProviderStore::CertificateProviderWrapper::type() |
| 92 |
const { |
| 93 |
static UniqueTypeName::Factory kFactory("Wrapper"); |
| 94 |
return kFactory.Create(); |
| 95 |
} |
| 96 |
|
| 97 |
// If a certificate provider is created, the CertificateProviderStore |
| 98 |
// maintains a raw pointer to the created CertificateProviderWrapper so that |
| 99 |
// future calls to `CreateOrGetCertificateProvider()` with the same key result |
| 100 |
// in returning a ref to this created certificate provider. This entry is |
| 101 |
// deleted when the refcount to this provider reaches zero. |
| 102 |
RefCountedPtr<grpc_tls_certificate_provider> |
| 103 |
CertificateProviderStore::CreateOrGetCertificateProvider( |
| 104 |
absl::string_view key) { |
| 105 |
RefCountedPtr<CertificateProviderWrapper> result; |
| 106 |
MutexLock lock(&mu_); |
| 107 |
auto it = certificate_providers_map_.find(key); |
| 108 |
if (it == certificate_providers_map_.end()) { |
| 109 |
result = CreateCertificateProviderLocked(key); |
| 110 |
if (result != nullptr) { |
| 111 |
certificate_providers_map_.insert({result->key(), result.get()}); |
| 112 |
} |
| 113 |
} else { |
| 114 |
result = it->second->RefIfNonZero(); |
| 115 |
if (result == nullptr) { |
| 116 |
result = CreateCertificateProviderLocked(key); |
| 117 |
it->second = result.get(); |
| 118 |
} |
| 119 |
} |
| 120 |
return result; |
| 121 |
} |
| 122 |
|
| 123 |
RefCountedPtr<CertificateProviderStore::CertificateProviderWrapper> |
| 124 |
CertificateProviderStore::CreateCertificateProviderLocked( |
| 125 |
absl::string_view key) { |
| 126 |
auto plugin_config_it = plugin_config_map_.find(std::string(key)); |
| 127 |
if (plugin_config_it == plugin_config_map_.end()) { |
| 128 |
return nullptr; |
| 129 |
} |
| 130 |
CertificateProviderFactory* factory = |
| 131 |
CoreConfiguration::Get() |
| 132 |
.certificate_provider_registry() |
| 133 |
.LookupCertificateProviderFactory( |
| 134 |
plugin_config_it->second.plugin_name); |
| 135 |
if (factory == nullptr) { |
| 136 |
// This should never happen since an entry is only inserted in the |
| 137 |
// plugin_config_map_ if the corresponding factory was found when parsing |
| 138 |
// the xDS bootstrap file. |
| 139 |
gpr_log(GPR_ERROR, "Certificate provider factory %s not found", |
| 140 |
plugin_config_it->second.plugin_name.c_str()); |
| 141 |
return nullptr; |
| 142 |
} |
| 143 |
return MakeRefCounted<CertificateProviderWrapper>( |
| 144 |
factory->CreateCertificateProvider(plugin_config_it->second.config), |
| 145 |
Ref(), plugin_config_it->first); |
| 146 |
} |
| 147 |
|
| 148 |
void CertificateProviderStore::ReleaseCertificateProvider( |
| 149 |
absl::string_view key, CertificateProviderWrapper* wrapper) { |
| 150 |
MutexLock lock(&mu_); |
| 151 |
auto it = certificate_providers_map_.find(key); |
| 152 |
if (it != certificate_providers_map_.end()) { |
| 153 |
if (it->second == wrapper) { |
| 154 |
certificate_providers_map_.erase(it); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
} // namespace grpc_core |