10#define FMT_HEADER_ONLY
11#include <fmt/format.h>
18 size_t refresh_interval_s;
20 std::shared_ptr<ccf::kv::Consensus>
consensus;
21 std::shared_ptr<ccf::RPCSessions> rpcsessions;
22 std::shared_ptr<ccf::RPCMap> rpc_map;
25 std::atomic_size_t attempts;
31 size_t refresh_interval_s,
33 const std::shared_ptr<ccf::kv::Consensus>&
consensus,
34 const std::shared_ptr<ccf::RPCSessions>& rpcsessions,
35 const std::shared_ptr<ccf::RPCMap>& rpc_map,
38 refresh_interval_s(refresh_interval_s),
41 rpcsessions(rpcsessions),
43 node_sign_kp(
std::move(node_sign_kp)),
44 node_cert(
std::move(node_cert)),
57 if (!this->consensus->can_replicate())
59 LOG_DEBUG_FMT(
"JWT key auto-refresh: Node is not primary, skipping");
63 this->refresh_jwt_keys();
67 "JWT key auto-refresh: Scheduling in {}s", this->refresh_interval_s);
70 const std::chrono::seconds period(refresh_interval_s);
76 if (periodic_refresh_task !=
nullptr)
78 periodic_refresh_task->cancel_task();
84 LOG_DEBUG_FMT(
"JWT key one-off refresh: Scheduling without delay");
86 if (!this->consensus->can_replicate())
89 "JWT key one-off refresh: Node is not primary, skipping");
93 this->refresh_jwt_keys();
104 "jwt_keys/refresh"));
106 http::headers::CONTENT_TYPE, http::headervalues::contenttype::JSON);
108 auto body = nlohmann::json(msg).dump();
113 auto node_session = std::make_shared<ccf::SessionContext>(
114 ccf::InvalidSessionId, node_cert.
raw());
117 std::shared_ptr<ccf::RpcHandler> search =
118 ::http::fetch_rpc_handler(ctx, this->rpc_map);
120 search->process(ctx);
132 const std::string& issuer,
133 const std::optional<std::string>& issuer_constraint,
135 std::vector<uint8_t>&& data)
137 if (status != HTTP_STATUS_OK)
140 "JWT key auto-refresh: Error while requesting JWKS: {} {}{}",
142 ccf::http_status_str(status),
145 fmt::format(
" '{}'", std::string(data.begin(), data.end())));
151 "JWT key auto-refresh: Received JWKS for issuer '{}'", issuer);
158 catch (
const std::exception& e)
161 "JWT key auto-refresh: Cannot parse JWKS for issuer '{}': {}",
173 if (issuer_constraint.has_value())
175 for (
auto& key : jwks.
keys)
177 if (!key.issuer.has_value())
179 key.issuer = issuer_constraint;
188 const std::string& issuer,
189 std::shared_ptr<::tls::CA> ca,
191 std::vector<uint8_t>&& data)
193 if (status != HTTP_STATUS_OK)
196 "JWT key auto-refresh: Error while requesting OpenID metadata: {} "
199 ccf::http_status_str(status),
202 fmt::format(
" '{}'", std::string(data.begin(), data.end())));
208 "JWT key auto-refresh: Received OpenID metadata for issuer '{}'",
211 std::string jwks_url_str;
212 nlohmann::json metadata;
215 metadata = nlohmann::json::parse(data);
216 jwks_url_str = metadata.at(
"jwks_uri").get<std::string>();
218 catch (
const std::exception& e)
221 "JWT key auto-refresh: Cannot parse OpenID metadata for issuer '{}': "
233 catch (
const std::invalid_argument& e)
236 "JWT key auto-refresh: Cannot parse jwks_uri for issuer '{}': {}",
242 auto jwks_url_port = !jwks_url.
port.empty() ? jwks_url.
port :
"443";
244 auto ca_cert = std::make_shared<::tls::Cert>(
245 ca, std::nullopt, std::nullopt, jwks_url.
host);
247 std::optional<std::string> issuer_constraint{std::nullopt};
248 const auto constraint = metadata.find(
"issuer");
249 if (constraint != metadata.end())
251 issuer_constraint = *constraint;
255 "JWT key auto-refresh: Requesting JWKS at https://{}:{}{}",
259 auto http_client = rpcsessions->create_client(ca_cert);
262 http_client->connect(
263 std::string(jwks_url.
host),
264 std::string(jwks_url_port),
265 [
this, issuer, issuer_constraint](
268 std::vector<uint8_t>&& data) {
269 handle_jwt_jwks_response(
270 issuer, issuer_constraint, status, std::move(data));
274 r.
set_header(ccf::http::headers::HOST, std::string(jwks_url.
host));
275 http_client->send_request(std::move(r));
280 auto tx = network.
tables->create_read_only_tx();
283 jwt_issuers->foreach([
this, &ca_cert_bundles](
289 "JWT key auto-refresh: Skipping issuer '{}', auto-refresh is "
299 "JWT key auto-refresh: Refreshing keys for issuer '{}'", issuer);
301 auto ca_cert_bundle_pem = ca_cert_bundles->get(ca_cert_bundle_name);
302 if (!ca_cert_bundle_pem.has_value())
305 "JWT key auto-refresh: CA cert bundle with name '{}' for issuer "
314 auto metadata_url_str = issuer +
"/.well-known/openid-configuration";
316 auto metadata_url_port =
317 !metadata_url.port.empty() ? metadata_url.port :
"443";
321 auto ca = std::make_shared<::tls::CA>(ca_pems);
322 auto ca_cert = std::make_shared<::tls::Cert>(
323 ca, std::nullopt, std::nullopt, metadata_url.host);
326 "JWT key auto-refresh: Requesting OpenID metadata at https://{}:{}{}",
330 auto http_client = rpcsessions->create_client(ca_cert);
333 http_client->connect(
334 std::string(metadata_url.host),
335 std::string(metadata_url_port),
339 std::vector<uint8_t>&& data) {
340 handle_jwt_metadata_response(issuer, ca, status, std::move(data));
344 r.
set_header(ccf::http::headers::HOST, std::string(metadata_url.host));
345 http_client->send_request(std::move(r));
353 return attempts.load();
Definition jwt_key_auto_refresh.h:16
void refresh_jwt_keys()
Definition jwt_key_auto_refresh.h:278
~JwtKeyAutoRefresh()
Definition jwt_key_auto_refresh.h:48
void send_refresh_jwt_keys(T msg)
Definition jwt_key_auto_refresh.h:99
void start()
Definition jwt_key_auto_refresh.h:53
JwtKeyAutoRefresh(size_t refresh_interval_s, NetworkState &network, const std::shared_ptr< ccf::kv::Consensus > &consensus, const std::shared_ptr< ccf::RPCSessions > &rpcsessions, const std::shared_ptr< ccf::RPCMap > &rpc_map, ccf::crypto::ECKeyPairPtr node_sign_kp, ccf::crypto::Pem node_cert)
Definition jwt_key_auto_refresh.h:30
size_t get_attempts() const
Definition jwt_key_auto_refresh.h:351
void stop()
Definition jwt_key_auto_refresh.h:74
void handle_jwt_jwks_response(const std::string &issuer, const std::optional< std::string > &issuer_constraint, ccf::http_status status, std::vector< uint8_t > &&data)
Definition jwt_key_auto_refresh.h:131
void send_refresh_jwt_keys_error()
Definition jwt_key_auto_refresh.h:123
void handle_jwt_metadata_response(const std::string &issuer, std::shared_ptr<::tls::CA > ca, ccf::http_status status, std::vector< uint8_t > &&data)
Definition jwt_key_auto_refresh.h:187
void schedule_once()
Definition jwt_key_auto_refresh.h:82
std::vector< uint8_t > raw() const
Definition pem.h:71
void set_header(std::string k, const std::string &v)
Definition http_builder.h:45
void set_body(const std::vector< uint8_t > *b, bool overwrite_content_length=true)
Definition http_builder.h:72
Definition http_builder.h:117
std::vector< uint8_t > build_request(bool header_only=false) const
Definition http_builder.h:175
#define LOG_DEBUG_FMT
Definition internal_logger.h:14
#define LOG_FAIL_FMT
Definition internal_logger.h:16
std::vector< ccf::crypto::Pem > split_x509_cert_bundle(const std::string_view &pem)
Definition pem.cpp:37
std::shared_ptr< ECKeyPair > ECKeyPairPtr
Definition ec_key_pair.h:144
std::map< std::string, std::string, std::less<> > HeaderMap
Definition http_header_map.h:10
Task make_basic_task(Ts &&... ts)
Definition basic_task.h:33
void add_periodic_task(Task task, std::chrono::milliseconds initial_delay, std::chrono::milliseconds repeat_period)
Definition task_system.cpp:75
void add_task(Task task)
Definition task_system.cpp:65
std::shared_ptr< BaseTask > Task
Definition task.h:36
Definition app_interface.h:14
constexpr auto get_actor_prefix(ActorsType at)
Definition actors.h:24
std::string JwtIssuer
Definition jwt.h:35
llhttp_status http_status
Definition http_status.h:9
std::shared_ptr<::http::HttpRpcContext > make_rpc_context(std::shared_ptr< ccf::SessionContext > s, const std::vector< uint8_t > &packed)
Definition http_rpc_context.h:374
Definition consensus_types.h:23
URL parse_url_full(const std::string &url)
Definition http_parser.h:151
std::vector< ccf::crypto::JsonWebKeyData > keys
Definition jwt.h:78
Definition network_state.h:12
std::shared_ptr< ccf::kv::Store > tables
Definition network_tables.h:46
const JwtIssuers jwt_issuers
Definition network_tables.h:164
const CACertBundlePEMs ca_cert_bundles
Definition network_tables.h:163
Definition node_frontend.h:120
Definition http_parser.h:142
std::string host
Definition http_parser.h:144
std::string port
Definition http_parser.h:145
std::string path
Definition http_parser.h:146