file_watcher_certificate_provider_factory.cc
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 
20 
22 
23 #include <algorithm>
24 #include <memory>
25 #include <vector>
26 
27 #include "absl/memory/memory.h"
28 #include "absl/strings/str_format.h"
29 #include "absl/strings/str_join.h"
30 
31 #include <grpc/support/log.h>
32 #include <grpc/support/time.h>
33 
37 
38 namespace grpc_core {
39 
40 namespace {
41 
42 const char* kFileWatcherPlugin = "file_watcher";
43 
44 } // namespace
45 
46 //
47 // FileWatcherCertificateProviderFactory::Config
48 //
49 
51  return kFileWatcherPlugin;
52 }
53 
55  std::vector<std::string> parts;
56  parts.push_back("{");
57  if (!identity_cert_file_.empty()) {
58  parts.push_back(
59  absl::StrFormat("certificate_file=\"%s\", ", identity_cert_file_));
60  }
61  if (!identity_cert_file_.empty()) {
62  parts.push_back(
63  absl::StrFormat("private_key_file=\"%s\", ", private_key_file_));
64  }
65  if (!identity_cert_file_.empty()) {
66  parts.push_back(
67  absl::StrFormat("ca_certificate_file=\"%s\", ", root_cert_file_));
68  }
69  parts.push_back(
70  absl::StrFormat("refresh_interval=%ldms}", refresh_interval_.millis()));
71  return absl::StrJoin(parts, "");
72 }
73 
77  auto config = MakeRefCounted<FileWatcherCertificateProviderFactory::Config>();
78  if (config_json.type() != Json::Type::OBJECT) {
80  "error:config type should be OBJECT.");
81  return nullptr;
82  }
83  std::vector<grpc_error_handle> error_list;
84  ParseJsonObjectField(config_json.object_value(), "certificate_file",
85  &config->identity_cert_file_, &error_list, false);
86  ParseJsonObjectField(config_json.object_value(), "private_key_file",
87  &config->private_key_file_, &error_list, false);
88  if (config->identity_cert_file_.empty() !=
89  config->private_key_file_.empty()) {
90  error_list.push_back(GRPC_ERROR_CREATE_FROM_STATIC_STRING(
91  "fields \"certificate_file\" and \"private_key_file\" must be both set "
92  "or both unset."));
93  }
94  ParseJsonObjectField(config_json.object_value(), "ca_certificate_file",
95  &config->root_cert_file_, &error_list, false);
96  if (config->identity_cert_file_.empty() && config->root_cert_file_.empty()) {
97  error_list.push_back(GRPC_ERROR_CREATE_FROM_STATIC_STRING(
98  "At least one of \"certificate_file\" and \"ca_certificate_file\" must "
99  "be specified."));
100  }
102  config_json.object_value(), "refresh_interval",
103  &config->refresh_interval_, &error_list, false)) {
104  config->refresh_interval_ = Duration::Minutes(10); // 10 minutes default
105  }
106  if (!error_list.empty()) {
108  "Error parsing file watcher certificate provider config", &error_list);
109  return nullptr;
110  }
111  return config;
112 }
113 
114 //
115 // FileWatcherCertificateProviderFactory
116 //
117 
119  return kFileWatcherPlugin;
120 }
121 
124  const Json& config_json, grpc_error_handle* error) {
126  error);
127 }
128 
132  if (config->name() != name()) {
133  gpr_log(GPR_ERROR, "Wrong config type Actual:%s vs Expected:%s",
134  config->name(), name());
135  return nullptr;
136  }
137  auto* file_watcher_config =
139  return MakeRefCounted<FileWatcherCertificateProvider>(
140  file_watcher_config->private_key_file(),
141  file_watcher_config->identity_cert_file(),
142  file_watcher_config->root_cert_file(),
143  file_watcher_config->refresh_interval().millis() / GPR_MS_PER_SEC);
144 }
145 
148  absl::make_unique<FileWatcherCertificateProviderFactory>());
149 }
150 
152 
153 } // namespace grpc_core
log.h
grpc_core::CertificateProviderRegistry::RegisterCertificateProviderFactory
static void RegisterCertificateProviderFactory(std::unique_ptr< CertificateProviderFactory > factory)
Definition: certificate_provider_registry.cc:85
grpc_core::FileWatcherCertificateProviderFactory::Config::name
const char * name() const override
Definition: file_watcher_certificate_provider_factory.cc:50
grpc_core::Json::type
Type type() const
Definition: src/core/lib/json/json.h:174
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
grpc_core::FileWatcherCertificateProviderFactory::Config
Definition: file_watcher_certificate_provider_factory.h:39
grpc_core::Json::Type::OBJECT
@ OBJECT
grpc_core
Definition: call_metric_recorder.h:31
file_watcher_certificate_provider_factory.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_core::Json::object_value
const Object & object_value() const
Definition: src/core/lib/json/json.h:177
time.h
GRPC_ERROR_CREATE_FROM_VECTOR
#define GRPC_ERROR_CREATE_FROM_VECTOR(desc, error_list)
Definition: error.h:314
grpc_tls_certificate_provider.h
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
absl::StrJoin
std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, Formatter &&fmt)
Definition: abseil-cpp/absl/strings/str_join.h:239
config
struct config_s config
Json
JSON (JavaScript Object Notation).
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:227
grpc_core::FileWatcherCertificateProviderShutdown
void FileWatcherCertificateProviderShutdown()
Definition: file_watcher_certificate_provider_factory.cc:151
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
json_util.h
grpc_core::FileWatcherCertificateProviderFactory::Config::ToString
std::string ToString() const override
Definition: file_watcher_certificate_provider_factory.cc:54
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_core::FileWatcherCertificateProviderFactory::Config::Parse
static RefCountedPtr< Config > Parse(const Json &config_json, grpc_error_handle *error)
Definition: file_watcher_certificate_provider_factory.cc:75
GRPC_ERROR_CREATE_FROM_STATIC_STRING
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
Definition: error.h:291
grpc_core::FileWatcherCertificateProviderInit
void FileWatcherCertificateProviderInit()
Definition: file_watcher_certificate_provider_factory.cc:146
certificate_provider_registry.h
config_s
Definition: bloaty/third_party/zlib/deflate.c:120
GPR_MS_PER_SEC
#define GPR_MS_PER_SEC
Definition: include/grpc/support/time.h:39
grpc_core::ParseJsonObjectFieldAsDuration
bool ParseJsonObjectFieldAsDuration(const Json::Object &object, absl::string_view field_name, Duration *output, std::vector< grpc_error_handle > *error_list, bool required)
Definition: src/core/lib/json/json_util.cc:107
grpc_error
Definition: error_internal.h:42
grpc_core::FileWatcherCertificateProviderFactory::CreateCertificateProviderConfig
RefCountedPtr< CertificateProviderFactory::Config > CreateCertificateProviderConfig(const Json &config_json, grpc_error_handle *error) override
Definition: file_watcher_certificate_provider_factory.cc:123
grpc_core::FileWatcherCertificateProviderFactory::name
const char * name() const override
Definition: file_watcher_certificate_provider_factory.cc:118
grpc_core::ParseJsonObjectField
bool ParseJsonObjectField(const Json::Object &object, absl::string_view field_name, T *output, std::vector< grpc_error_handle > *error_list, bool required=true)
Definition: src/core/lib/json/json_util.h:136
grpc_core::Duration::Minutes
static constexpr Duration Minutes(int64_t minutes)
Definition: src/core/lib/gprpp/time.h:147
grpc_core::FileWatcherCertificateProviderFactory::CreateCertificateProvider
RefCountedPtr< grpc_tls_certificate_provider > CreateCertificateProvider(RefCountedPtr< CertificateProviderFactory::Config > config) override
Definition: file_watcher_certificate_provider_factory.cc:130
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:23