itsi-scheduler 0.1.5 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +120 -52
  3. data/README.md +57 -24
  4. data/Rakefile +0 -4
  5. data/ext/itsi_acme/Cargo.toml +86 -0
  6. data/ext/itsi_acme/examples/high_level.rs +63 -0
  7. data/ext/itsi_acme/examples/high_level_warp.rs +52 -0
  8. data/ext/itsi_acme/examples/low_level.rs +87 -0
  9. data/ext/itsi_acme/examples/low_level_axum.rs +66 -0
  10. data/ext/itsi_acme/src/acceptor.rs +81 -0
  11. data/ext/itsi_acme/src/acme.rs +354 -0
  12. data/ext/itsi_acme/src/axum.rs +86 -0
  13. data/ext/itsi_acme/src/cache.rs +39 -0
  14. data/ext/itsi_acme/src/caches/boxed.rs +80 -0
  15. data/ext/itsi_acme/src/caches/composite.rs +69 -0
  16. data/ext/itsi_acme/src/caches/dir.rs +106 -0
  17. data/ext/itsi_acme/src/caches/mod.rs +11 -0
  18. data/ext/itsi_acme/src/caches/no.rs +78 -0
  19. data/ext/itsi_acme/src/caches/test.rs +136 -0
  20. data/ext/itsi_acme/src/config.rs +172 -0
  21. data/ext/itsi_acme/src/https_helper.rs +69 -0
  22. data/ext/itsi_acme/src/incoming.rs +142 -0
  23. data/ext/itsi_acme/src/jose.rs +161 -0
  24. data/ext/itsi_acme/src/lib.rs +142 -0
  25. data/ext/itsi_acme/src/resolver.rs +59 -0
  26. data/ext/itsi_acme/src/state.rs +424 -0
  27. data/ext/itsi_error/Cargo.toml +1 -0
  28. data/ext/itsi_error/src/lib.rs +106 -7
  29. data/ext/itsi_error/target/debug/build/clang-sys-da71b0344e568175/out/common.rs +355 -0
  30. data/ext/itsi_error/target/debug/build/clang-sys-da71b0344e568175/out/dynamic.rs +276 -0
  31. data/ext/itsi_error/target/debug/build/clang-sys-da71b0344e568175/out/macros.rs +49 -0
  32. data/ext/itsi_error/target/debug/build/rb-sys-49f554618693db24/out/bindings-0.9.110-mri-arm64-darwin23-3.4.2.rs +8865 -0
  33. data/ext/itsi_error/target/debug/incremental/itsi_error-1mmt5sux7jb0i/s-h510z7m8v9-0bxu7yd.lock +0 -0
  34. data/ext/itsi_error/target/debug/incremental/itsi_error-2vn3jey74oiw0/s-h5113n0e7e-1v5qzs6.lock +0 -0
  35. data/ext/itsi_error/target/debug/incremental/itsi_error-37uv9dicz7awp/s-h510ykifhe-0tbnep2.lock +0 -0
  36. data/ext/itsi_error/target/debug/incremental/itsi_error-37uv9dicz7awp/s-h510yyocpj-0tz7ug7.lock +0 -0
  37. data/ext/itsi_error/target/debug/incremental/itsi_error-37uv9dicz7awp/s-h510z0xc8g-14ol18k.lock +0 -0
  38. data/ext/itsi_error/target/debug/incremental/itsi_error-3g5qf4y7d54uj/s-h5113n0e7d-1trk8on.lock +0 -0
  39. data/ext/itsi_error/target/debug/incremental/itsi_error-3lpfftm45d3e2/s-h510z7m8r3-1pxp20o.lock +0 -0
  40. data/ext/itsi_error/target/debug/incremental/itsi_error-3o4qownhl3d7n/s-h510ykifek-1uxasnk.lock +0 -0
  41. data/ext/itsi_error/target/debug/incremental/itsi_error-3o4qownhl3d7n/s-h510yyocki-11u37qm.lock +0 -0
  42. data/ext/itsi_error/target/debug/incremental/itsi_error-3o4qownhl3d7n/s-h510z0xc93-0pmy0zm.lock +0 -0
  43. data/ext/itsi_rb_helpers/Cargo.toml +1 -0
  44. data/ext/itsi_rb_helpers/src/heap_value.rs +18 -0
  45. data/ext/itsi_rb_helpers/src/lib.rs +63 -12
  46. data/ext/itsi_rb_helpers/target/debug/build/clang-sys-da71b0344e568175/out/common.rs +355 -0
  47. data/ext/itsi_rb_helpers/target/debug/build/clang-sys-da71b0344e568175/out/dynamic.rs +276 -0
  48. data/ext/itsi_rb_helpers/target/debug/build/clang-sys-da71b0344e568175/out/macros.rs +49 -0
  49. data/ext/itsi_rb_helpers/target/debug/build/rb-sys-eb9ed4ff3a60f995/out/bindings-0.9.110-mri-arm64-darwin23-3.4.2.rs +8865 -0
  50. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-040pxg6yhb3g3/s-h5113n7a1b-03bwlt4.lock +0 -0
  51. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-131g1u4dzkt1a/s-h51113xnh3-1eik1ip.lock +0 -0
  52. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-131g1u4dzkt1a/s-h5111704jj-0g4rj8x.lock +0 -0
  53. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-1q2d3drtxrzs5/s-h5113n79yl-0bxcqc5.lock +0 -0
  54. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-374a9h7ovycj0/s-h51113xoox-10de2hp.lock +0 -0
  55. data/ext/itsi_rb_helpers/target/debug/incremental/itsi_rb_helpers-374a9h7ovycj0/s-h5111704w7-0vdq7gq.lock +0 -0
  56. data/ext/itsi_scheduler/Cargo.toml +1 -1
  57. data/ext/itsi_scheduler/src/itsi_scheduler.rs +9 -3
  58. data/ext/itsi_scheduler/src/lib.rs +1 -0
  59. data/ext/itsi_server/Cargo.lock +2956 -0
  60. data/ext/itsi_server/Cargo.toml +73 -29
  61. data/ext/itsi_server/src/default_responses/mod.rs +11 -0
  62. data/ext/itsi_server/src/env.rs +43 -0
  63. data/ext/itsi_server/src/lib.rs +114 -75
  64. data/ext/itsi_server/src/prelude.rs +2 -0
  65. data/ext/itsi_server/src/{body_proxy → ruby_types/itsi_body_proxy}/big_bytes.rs +10 -5
  66. data/ext/itsi_server/src/{body_proxy/itsi_body_proxy.rs → ruby_types/itsi_body_proxy/mod.rs} +29 -8
  67. data/ext/itsi_server/src/ruby_types/itsi_grpc_call.rs +344 -0
  68. data/ext/itsi_server/src/ruby_types/itsi_grpc_response_stream/mod.rs +264 -0
  69. data/ext/itsi_server/src/ruby_types/itsi_http_request.rs +362 -0
  70. data/ext/itsi_server/src/{response/itsi_response.rs → ruby_types/itsi_http_response.rs} +84 -40
  71. data/ext/itsi_server/src/ruby_types/itsi_server/file_watcher.rs +233 -0
  72. data/ext/itsi_server/src/ruby_types/itsi_server/itsi_server_config.rs +565 -0
  73. data/ext/itsi_server/src/ruby_types/itsi_server.rs +86 -0
  74. data/ext/itsi_server/src/ruby_types/mod.rs +48 -0
  75. data/ext/itsi_server/src/server/{bind.rs → binds/bind.rs} +59 -24
  76. data/ext/itsi_server/src/server/binds/listener.rs +444 -0
  77. data/ext/itsi_server/src/server/binds/mod.rs +4 -0
  78. data/ext/itsi_server/src/server/{tls → binds/tls}/locked_dir_cache.rs +57 -19
  79. data/ext/itsi_server/src/server/{tls.rs → binds/tls.rs} +120 -31
  80. data/ext/itsi_server/src/server/byte_frame.rs +32 -0
  81. data/ext/itsi_server/src/server/http_message_types.rs +97 -0
  82. data/ext/itsi_server/src/server/io_stream.rs +2 -1
  83. data/ext/itsi_server/src/server/lifecycle_event.rs +3 -0
  84. data/ext/itsi_server/src/server/middleware_stack/middleware.rs +170 -0
  85. data/ext/itsi_server/src/server/middleware_stack/middlewares/allow_list.rs +63 -0
  86. data/ext/itsi_server/src/server/middleware_stack/middlewares/auth_api_key.rs +94 -0
  87. data/ext/itsi_server/src/server/middleware_stack/middlewares/auth_basic.rs +94 -0
  88. data/ext/itsi_server/src/server/middleware_stack/middlewares/auth_jwt.rs +343 -0
  89. data/ext/itsi_server/src/server/middleware_stack/middlewares/cache_control.rs +151 -0
  90. data/ext/itsi_server/src/server/middleware_stack/middlewares/compression.rs +316 -0
  91. data/ext/itsi_server/src/server/middleware_stack/middlewares/cors.rs +301 -0
  92. data/ext/itsi_server/src/server/middleware_stack/middlewares/csp.rs +193 -0
  93. data/ext/itsi_server/src/server/middleware_stack/middlewares/deny_list.rs +64 -0
  94. data/ext/itsi_server/src/server/middleware_stack/middlewares/error_response/default_responses.rs +192 -0
  95. data/ext/itsi_server/src/server/middleware_stack/middlewares/error_response.rs +171 -0
  96. data/ext/itsi_server/src/server/middleware_stack/middlewares/etag.rs +198 -0
  97. data/ext/itsi_server/src/server/middleware_stack/middlewares/header_interpretation.rs +82 -0
  98. data/ext/itsi_server/src/server/middleware_stack/middlewares/intrusion_protection.rs +209 -0
  99. data/ext/itsi_server/src/server/middleware_stack/middlewares/log_requests.rs +82 -0
  100. data/ext/itsi_server/src/server/middleware_stack/middlewares/max_body.rs +47 -0
  101. data/ext/itsi_server/src/server/middleware_stack/middlewares/mod.rs +116 -0
  102. data/ext/itsi_server/src/server/middleware_stack/middlewares/proxy.rs +411 -0
  103. data/ext/itsi_server/src/server/middleware_stack/middlewares/rate_limit.rs +142 -0
  104. data/ext/itsi_server/src/server/middleware_stack/middlewares/redirect.rs +55 -0
  105. data/ext/itsi_server/src/server/middleware_stack/middlewares/request_headers.rs +54 -0
  106. data/ext/itsi_server/src/server/middleware_stack/middlewares/response_headers.rs +51 -0
  107. data/ext/itsi_server/src/server/middleware_stack/middlewares/ruby_app.rs +126 -0
  108. data/ext/itsi_server/src/server/middleware_stack/middlewares/static_assets.rs +187 -0
  109. data/ext/itsi_server/src/server/middleware_stack/middlewares/static_response.rs +55 -0
  110. data/ext/itsi_server/src/server/middleware_stack/middlewares/string_rewrite.rs +173 -0
  111. data/ext/itsi_server/src/server/middleware_stack/middlewares/token_source.rs +31 -0
  112. data/ext/itsi_server/src/server/middleware_stack/mod.rs +381 -0
  113. data/ext/itsi_server/src/server/mod.rs +7 -5
  114. data/ext/itsi_server/src/server/process_worker.rs +65 -14
  115. data/ext/itsi_server/src/server/redirect_type.rs +26 -0
  116. data/ext/itsi_server/src/server/request_job.rs +11 -0
  117. data/ext/itsi_server/src/server/serve_strategy/cluster_mode.rs +150 -50
  118. data/ext/itsi_server/src/server/serve_strategy/mod.rs +9 -6
  119. data/ext/itsi_server/src/server/serve_strategy/single_mode.rs +399 -165
  120. data/ext/itsi_server/src/server/signal.rs +33 -26
  121. data/ext/itsi_server/src/server/size_limited_incoming.rs +107 -0
  122. data/ext/itsi_server/src/server/thread_worker.rs +218 -107
  123. data/ext/itsi_server/src/services/cache_store.rs +74 -0
  124. data/ext/itsi_server/src/services/itsi_http_service.rs +257 -0
  125. data/ext/itsi_server/src/services/mime_types.rs +1416 -0
  126. data/ext/itsi_server/src/services/mod.rs +6 -0
  127. data/ext/itsi_server/src/services/password_hasher.rs +83 -0
  128. data/ext/itsi_server/src/services/rate_limiter.rs +580 -0
  129. data/ext/itsi_server/src/services/static_file_server.rs +1340 -0
  130. data/ext/itsi_tracing/Cargo.toml +1 -0
  131. data/ext/itsi_tracing/src/lib.rs +362 -33
  132. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-0994n8rpvvt9m/s-h510hfz1f6-1kbycmq.lock +0 -0
  133. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-0bob7bf4yq34i/s-h5113125h5-0lh4rag.lock +0 -0
  134. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2fcodulrxbbxo/s-h510h2infk-0hp5kjw.lock +0 -0
  135. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2iak63r1woi1l/s-h510h2in4q-0kxfzw1.lock +0 -0
  136. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2kk4qj9gn5dg2/s-h5113124kv-0enwon2.lock +0 -0
  137. data/ext/itsi_tracing/target/debug/incremental/itsi_tracing-2mwo0yas7dtw4/s-h510hfz1ha-1udgpei.lock +0 -0
  138. data/itsi-scheduler-100.png +0 -0
  139. data/lib/itsi/scheduler/version.rb +1 -1
  140. data/lib/itsi/scheduler.rb +11 -6
  141. metadata +117 -24
  142. data/CHANGELOG.md +0 -5
  143. data/CODE_OF_CONDUCT.md +0 -132
  144. data/LICENSE.txt +0 -21
  145. data/ext/itsi_error/src/from.rs +0 -71
  146. data/ext/itsi_server/extconf.rb +0 -6
  147. data/ext/itsi_server/src/body_proxy/mod.rs +0 -2
  148. data/ext/itsi_server/src/request/itsi_request.rs +0 -277
  149. data/ext/itsi_server/src/request/mod.rs +0 -1
  150. data/ext/itsi_server/src/response/mod.rs +0 -1
  151. data/ext/itsi_server/src/server/itsi_ca/itsi_ca.crt +0 -13
  152. data/ext/itsi_server/src/server/itsi_ca/itsi_ca.key +0 -5
  153. data/ext/itsi_server/src/server/itsi_server.rs +0 -244
  154. data/ext/itsi_server/src/server/listener.rs +0 -327
  155. /data/ext/itsi_server/src/server/{bind_protocol.rs → binds/bind_protocol.rs} +0 -0
@@ -0,0 +1,86 @@
1
+ use crate::{AcmeAccept, AcmeAcceptor};
2
+ use rustls::ServerConfig;
3
+ use std::future::Future;
4
+ use std::io;
5
+ use std::io::ErrorKind;
6
+ use std::pin::Pin;
7
+ use std::sync::Arc;
8
+ use std::task::{Context, Poll};
9
+ use tokio::io::{AsyncRead, AsyncWrite};
10
+ use tokio_rustls::Accept;
11
+
12
+ #[derive(Clone)]
13
+ pub struct AxumAcceptor {
14
+ acme_acceptor: AcmeAcceptor,
15
+ config: Arc<ServerConfig>,
16
+ }
17
+
18
+ impl AxumAcceptor {
19
+ pub fn new(acme_acceptor: AcmeAcceptor, config: Arc<ServerConfig>) -> Self {
20
+ Self {
21
+ acme_acceptor,
22
+ config,
23
+ }
24
+ }
25
+ }
26
+
27
+ impl<I: AsyncRead + AsyncWrite + Unpin + Send + 'static, S: Send + 'static>
28
+ axum_server::accept::Accept<I, S> for AxumAcceptor
29
+ {
30
+ type Stream = tokio_rustls::server::TlsStream<I>;
31
+ type Service = S;
32
+ type Future = AxumAccept<I, S>;
33
+
34
+ fn accept(&self, stream: I, service: S) -> Self::Future {
35
+ let acme_accept = self.acme_acceptor.accept(stream);
36
+ Self::Future {
37
+ config: self.config.clone(),
38
+ acme_accept,
39
+ tls_accept: None,
40
+ service: Some(service),
41
+ }
42
+ }
43
+ }
44
+
45
+ pub struct AxumAccept<I: AsyncRead + AsyncWrite + Unpin + Send + 'static, S: Send + 'static> {
46
+ config: Arc<ServerConfig>,
47
+ acme_accept: AcmeAccept<I>,
48
+ tls_accept: Option<Accept<I>>,
49
+ service: Option<S>,
50
+ }
51
+
52
+ impl<I: AsyncRead + AsyncWrite + Unpin + Send + 'static, S: Send + 'static> Unpin
53
+ for AxumAccept<I, S>
54
+ {
55
+ }
56
+
57
+ impl<I: AsyncRead + AsyncWrite + Unpin + Send + 'static, S: Send + 'static> Future
58
+ for AxumAccept<I, S>
59
+ {
60
+ type Output = io::Result<(tokio_rustls::server::TlsStream<I>, S)>;
61
+
62
+ fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
63
+ loop {
64
+ if let Some(tls_accept) = &mut self.tls_accept {
65
+ return match Pin::new(&mut *tls_accept).poll(cx) {
66
+ Poll::Ready(Ok(tls)) => Poll::Ready(Ok((tls, self.service.take().unwrap()))),
67
+ Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
68
+ Poll::Pending => Poll::Pending,
69
+ };
70
+ }
71
+ return match Pin::new(&mut self.acme_accept).poll(cx) {
72
+ Poll::Ready(Ok(Some(start_handshake))) => {
73
+ let config = self.config.clone();
74
+ self.tls_accept = Some(start_handshake.into_stream(config));
75
+ continue;
76
+ }
77
+ Poll::Ready(Ok(None)) => Poll::Ready(Err(io::Error::new(
78
+ ErrorKind::Other,
79
+ "TLS-ALPN-01 validation request",
80
+ ))),
81
+ Poll::Ready(Err(err)) => Poll::Ready(Err(err)),
82
+ Poll::Pending => Poll::Pending,
83
+ };
84
+ }
85
+ }
86
+ }
@@ -0,0 +1,39 @@
1
+ use std::fmt::Debug;
2
+
3
+ use async_trait::async_trait;
4
+
5
+ pub trait Cache: CertCache + AccountCache {}
6
+
7
+ impl<T> Cache for T where T: CertCache + AccountCache {}
8
+
9
+ #[async_trait]
10
+ pub trait CertCache: Send + Sync {
11
+ type EC: Debug;
12
+ async fn load_cert(
13
+ &self,
14
+ domains: &[String],
15
+ directory_url: &str,
16
+ ) -> Result<Option<Vec<u8>>, Self::EC>;
17
+ async fn store_cert(
18
+ &self,
19
+ domains: &[String],
20
+ directory_url: &str,
21
+ cert: &[u8],
22
+ ) -> Result<(), Self::EC>;
23
+ }
24
+
25
+ #[async_trait]
26
+ pub trait AccountCache: Send + Sync {
27
+ type EA: Debug;
28
+ async fn load_account(
29
+ &self,
30
+ contact: &[String],
31
+ directory_url: &str,
32
+ ) -> Result<Option<Vec<u8>>, Self::EA>;
33
+ async fn store_account(
34
+ &self,
35
+ contact: &[String],
36
+ directory_url: &str,
37
+ account: &[u8],
38
+ ) -> Result<(), Self::EA>;
39
+ }
@@ -0,0 +1,80 @@
1
+ use crate::{AccountCache, CertCache};
2
+ use async_trait::async_trait;
3
+ use std::fmt::Debug;
4
+
5
+ pub struct BoxedErrCache<T: Send + Sync> {
6
+ inner: T,
7
+ }
8
+
9
+ impl<T: Send + Sync> BoxedErrCache<T> {
10
+ pub fn new(inner: T) -> Self {
11
+ Self { inner }
12
+ }
13
+ pub fn into_inner(self) -> T {
14
+ self.inner
15
+ }
16
+ }
17
+
18
+ fn box_err(e: impl Debug + 'static) -> Box<dyn Debug> {
19
+ Box::new(e)
20
+ }
21
+
22
+ #[async_trait]
23
+ impl<T: CertCache> CertCache for BoxedErrCache<T>
24
+ where
25
+ <T as CertCache>::EC: 'static,
26
+ {
27
+ type EC = Box<dyn Debug>;
28
+ async fn load_cert(
29
+ &self,
30
+ domains: &[String],
31
+ directory_url: &str,
32
+ ) -> Result<Option<Vec<u8>>, Self::EC> {
33
+ self.inner
34
+ .load_cert(domains, directory_url)
35
+ .await
36
+ .map_err(box_err)
37
+ }
38
+
39
+ async fn store_cert(
40
+ &self,
41
+ domains: &[String],
42
+ directory_url: &str,
43
+ cert: &[u8],
44
+ ) -> Result<(), Self::EC> {
45
+ self.inner
46
+ .store_cert(domains, directory_url, cert)
47
+ .await
48
+ .map_err(box_err)
49
+ }
50
+ }
51
+
52
+ #[async_trait]
53
+ impl<T: AccountCache> AccountCache for BoxedErrCache<T>
54
+ where
55
+ <T as AccountCache>::EA: 'static,
56
+ {
57
+ type EA = Box<dyn Debug>;
58
+ async fn load_account(
59
+ &self,
60
+ contact: &[String],
61
+ directory_url: &str,
62
+ ) -> Result<Option<Vec<u8>>, Self::EA> {
63
+ self.inner
64
+ .load_account(contact, directory_url)
65
+ .await
66
+ .map_err(box_err)
67
+ }
68
+
69
+ async fn store_account(
70
+ &self,
71
+ contact: &[String],
72
+ directory_url: &str,
73
+ account: &[u8],
74
+ ) -> Result<(), Self::EA> {
75
+ self.inner
76
+ .store_account(contact, directory_url, account)
77
+ .await
78
+ .map_err(box_err)
79
+ }
80
+ }
@@ -0,0 +1,69 @@
1
+ use crate::{AccountCache, CertCache};
2
+ use async_trait::async_trait;
3
+
4
+ pub struct CompositeCache<C: CertCache + Send + Sync, A: AccountCache + Send + Sync> {
5
+ pub cert_cache: C,
6
+ pub account_cache: A,
7
+ }
8
+
9
+ impl<C: CertCache + Send + Sync, A: AccountCache + Send + Sync> CompositeCache<C, A> {
10
+ pub fn new(cert_cache: C, account_cache: A) -> Self {
11
+ Self {
12
+ cert_cache,
13
+ account_cache,
14
+ }
15
+ }
16
+ pub fn into_inner(self) -> (C, A) {
17
+ (self.cert_cache, self.account_cache)
18
+ }
19
+ }
20
+
21
+ #[async_trait]
22
+ impl<C: CertCache + Send + Sync, A: AccountCache + Send + Sync> CertCache for CompositeCache<C, A> {
23
+ type EC = C::EC;
24
+ async fn load_cert(
25
+ &self,
26
+ domains: &[String],
27
+ directory_url: &str,
28
+ ) -> Result<Option<Vec<u8>>, Self::EC> {
29
+ self.cert_cache.load_cert(domains, directory_url).await
30
+ }
31
+
32
+ async fn store_cert(
33
+ &self,
34
+ domains: &[String],
35
+ directory_url: &str,
36
+ cert: &[u8],
37
+ ) -> Result<(), Self::EC> {
38
+ self.cert_cache
39
+ .store_cert(domains, directory_url, cert)
40
+ .await
41
+ }
42
+ }
43
+
44
+ #[async_trait]
45
+ impl<C: CertCache + Send + Sync, A: AccountCache + Send + Sync> AccountCache
46
+ for CompositeCache<C, A>
47
+ {
48
+ type EA = A::EA;
49
+ async fn load_account(
50
+ &self,
51
+ contact: &[String],
52
+ directory_url: &str,
53
+ ) -> Result<Option<Vec<u8>>, Self::EA> {
54
+ self.account_cache
55
+ .load_account(contact, directory_url)
56
+ .await
57
+ }
58
+
59
+ async fn store_account(
60
+ &self,
61
+ contact: &[String],
62
+ directory_url: &str,
63
+ account: &[u8],
64
+ ) -> Result<(), Self::EA> {
65
+ self.account_cache
66
+ .store_account(contact, directory_url, account)
67
+ .await
68
+ }
69
+ }
@@ -0,0 +1,106 @@
1
+ use crate::{AccountCache, CertCache};
2
+ use async_trait::async_trait;
3
+ use base64::engine::general_purpose::URL_SAFE_NO_PAD;
4
+ use base64::Engine;
5
+ use ring::digest::{Context, SHA256};
6
+ use std::io::ErrorKind;
7
+ use std::path::Path;
8
+ use tokio::fs;
9
+
10
+ pub struct DirCache<P: AsRef<Path> + Send + Sync> {
11
+ inner: P,
12
+ }
13
+
14
+ impl<P: AsRef<Path> + Send + Sync> DirCache<P> {
15
+ pub fn new(dir: P) -> Self {
16
+ Self { inner: dir }
17
+ }
18
+ async fn read_if_exist(
19
+ &self,
20
+ file: impl AsRef<Path>,
21
+ ) -> Result<Option<Vec<u8>>, std::io::Error> {
22
+ let path = self.inner.as_ref().join(file);
23
+ match fs::read(path).await {
24
+ Ok(content) => Ok(Some(content)),
25
+ Err(err) => match err.kind() {
26
+ ErrorKind::NotFound => Ok(None),
27
+ _ => Err(err),
28
+ },
29
+ }
30
+ }
31
+ async fn write(
32
+ &self,
33
+ file: impl AsRef<Path>,
34
+ contents: impl AsRef<[u8]>,
35
+ ) -> Result<(), std::io::Error> {
36
+ fs::create_dir_all(&self.inner).await?;
37
+ let path = self.inner.as_ref().join(file);
38
+ fs::write(path, contents).await
39
+ }
40
+
41
+ fn cached_account_file_name(contact: &[String], directory_url: impl AsRef<str>) -> String {
42
+ let mut ctx = Context::new(&SHA256);
43
+ for el in contact {
44
+ ctx.update(el.as_ref());
45
+ ctx.update(&[0])
46
+ }
47
+ ctx.update(directory_url.as_ref().as_bytes());
48
+ let hash = URL_SAFE_NO_PAD.encode(ctx.finish());
49
+ format!("cached_account_{}", hash)
50
+ }
51
+ fn cached_cert_file_name(domains: &[String], directory_url: impl AsRef<str>) -> String {
52
+ let mut ctx = Context::new(&SHA256);
53
+ for domain in domains {
54
+ ctx.update(domain.as_ref());
55
+ ctx.update(&[0])
56
+ }
57
+ ctx.update(directory_url.as_ref().as_bytes());
58
+ let hash = URL_SAFE_NO_PAD.encode(ctx.finish());
59
+ format!("cached_cert_{}", hash)
60
+ }
61
+ }
62
+
63
+ #[async_trait]
64
+ impl<P: AsRef<Path> + Send + Sync> CertCache for DirCache<P> {
65
+ type EC = std::io::Error;
66
+ async fn load_cert(
67
+ &self,
68
+ domains: &[String],
69
+ directory_url: &str,
70
+ ) -> Result<Option<Vec<u8>>, Self::EC> {
71
+ let file_name = Self::cached_cert_file_name(domains, directory_url);
72
+ self.read_if_exist(file_name).await
73
+ }
74
+ async fn store_cert(
75
+ &self,
76
+ domains: &[String],
77
+ directory_url: &str,
78
+ cert: &[u8],
79
+ ) -> Result<(), Self::EC> {
80
+ let file_name = Self::cached_cert_file_name(domains, directory_url);
81
+ self.write(file_name, cert).await
82
+ }
83
+ }
84
+
85
+ #[async_trait]
86
+ impl<P: AsRef<Path> + Send + Sync> AccountCache for DirCache<P> {
87
+ type EA = std::io::Error;
88
+ async fn load_account(
89
+ &self,
90
+ contact: &[String],
91
+ directory_url: &str,
92
+ ) -> Result<Option<Vec<u8>>, Self::EA> {
93
+ let file_name = Self::cached_account_file_name(contact, directory_url);
94
+ self.read_if_exist(file_name).await
95
+ }
96
+
97
+ async fn store_account(
98
+ &self,
99
+ contact: &[String],
100
+ directory_url: &str,
101
+ account: &[u8],
102
+ ) -> Result<(), Self::EA> {
103
+ let file_name = Self::cached_account_file_name(contact, directory_url);
104
+ self.write(file_name, account).await
105
+ }
106
+ }
@@ -0,0 +1,11 @@
1
+ mod boxed;
2
+ mod composite;
3
+ mod dir;
4
+ mod no;
5
+ mod test;
6
+
7
+ pub use boxed::*;
8
+ pub use composite::*;
9
+ pub use dir::*;
10
+ pub use no::*;
11
+ pub use test::*;
@@ -0,0 +1,78 @@
1
+ use crate::{AccountCache, CertCache};
2
+ use async_trait::async_trait;
3
+ use std::convert::Infallible;
4
+ use std::fmt::Debug;
5
+ use std::marker::PhantomData;
6
+ use std::sync::atomic::AtomicPtr;
7
+
8
+ /// No-op cache, which does nothing.
9
+ /// ```rust
10
+ /// # use tokio_rustls_acme::caches::NoCache;
11
+ /// # type EC = std::io::Error;
12
+ /// # type EA = EC;
13
+ /// let no_cache = NoCache::<EC, EA>::new();
14
+ /// ```
15
+ #[derive(Copy, Clone)]
16
+ pub struct NoCache<EC: Debug = Infallible, EA: Debug = Infallible> {
17
+ _cert_error: PhantomData<AtomicPtr<Box<EC>>>,
18
+ _account_error: PhantomData<AtomicPtr<Box<EA>>>,
19
+ }
20
+
21
+ impl<EC: Debug, EA: Debug> Default for NoCache<EC, EA> {
22
+ fn default() -> Self {
23
+ Self {
24
+ _cert_error: Default::default(),
25
+ _account_error: Default::default(),
26
+ }
27
+ }
28
+ }
29
+
30
+ impl<EC: Debug, EA: Debug> NoCache<EC, EA> {
31
+ pub fn new() -> Self {
32
+ Self::default()
33
+ }
34
+ }
35
+
36
+ #[async_trait]
37
+ impl<EC: Debug, EA: Debug> CertCache for NoCache<EC, EA> {
38
+ type EC = EC;
39
+ async fn load_cert(
40
+ &self,
41
+ _domains: &[String],
42
+ _directory_url: &str,
43
+ ) -> Result<Option<Vec<u8>>, Self::EC> {
44
+ log::info!("no cert cache configured, could not load certificate");
45
+ Ok(None)
46
+ }
47
+ async fn store_cert(
48
+ &self,
49
+ _domains: &[String],
50
+ _directory_url: &str,
51
+ _cert: &[u8],
52
+ ) -> Result<(), Self::EC> {
53
+ log::info!("no cert cache configured, could not store certificate");
54
+ Ok(())
55
+ }
56
+ }
57
+
58
+ #[async_trait]
59
+ impl<EC: Debug, EA: Debug> AccountCache for NoCache<EC, EA> {
60
+ type EA = EA;
61
+ async fn load_account(
62
+ &self,
63
+ _contact: &[String],
64
+ _directory_url: &str,
65
+ ) -> Result<Option<Vec<u8>>, Self::EA> {
66
+ log::info!("no account cache configured, could not load account");
67
+ Ok(None)
68
+ }
69
+ async fn store_account(
70
+ &self,
71
+ _contact: &[String],
72
+ _directory_url: &str,
73
+ _account: &[u8],
74
+ ) -> Result<(), Self::EA> {
75
+ log::info!("no account cache configured, could not store account");
76
+ Ok(())
77
+ }
78
+ }
@@ -0,0 +1,136 @@
1
+ use crate::{AccountCache, CertCache};
2
+ use async_trait::async_trait;
3
+ use rcgen::{
4
+ date_time_ymd, BasicConstraints, CertificateParams, DistinguishedName, DnType, IsCa,
5
+ KeyUsagePurpose, PKCS_ECDSA_P256_SHA256,
6
+ };
7
+ use std::fmt::Debug;
8
+ use std::marker::PhantomData;
9
+ use std::sync::atomic::AtomicPtr;
10
+ use std::sync::Arc;
11
+
12
+ /// Test cache, which generates certificates for ACME incompatible test environments.
13
+ /// ```rust
14
+ /// # use tokio_rustls_acme::{AcmeConfig};
15
+ /// # use tokio_rustls_acme::caches::{DirCache, TestCache};
16
+ /// # let test_environment = true;
17
+ /// let mut config = AcmeConfig::new(["example.com"])
18
+ /// .cache(DirCache::new("./cache"));
19
+ /// if test_environment {
20
+ /// config = config.cache(TestCache::new());
21
+ /// }
22
+ /// ```
23
+ #[derive(Clone)]
24
+ pub struct TestCache<EC: Debug = std::io::Error, EA: Debug = std::io::Error> {
25
+ ca_cert: Arc<rcgen::Certificate>,
26
+ ca_pem: Arc<String>,
27
+ ca_key_pair: Arc<rcgen::KeyPair>,
28
+ _cert_error: PhantomData<AtomicPtr<Box<EC>>>,
29
+ _account_error: PhantomData<AtomicPtr<Box<EA>>>,
30
+ }
31
+
32
+ impl<EC: Debug, EA: Debug> Default for TestCache<EC, EA> {
33
+ fn default() -> Self {
34
+ let mut params = CertificateParams::default();
35
+ let mut distinguished_name = DistinguishedName::new();
36
+ distinguished_name.push(DnType::CountryName, "US");
37
+ distinguished_name.push(DnType::OrganizationName, "Test CA");
38
+ distinguished_name.push(DnType::CommonName, "Test CA");
39
+ params.distinguished_name = distinguished_name;
40
+
41
+ params.is_ca = IsCa::Ca(BasicConstraints::Unconstrained);
42
+ params.key_usages = vec![KeyUsagePurpose::KeyCertSign, KeyUsagePurpose::CrlSign];
43
+ params.not_before = date_time_ymd(2000, 1, 1);
44
+ params.not_after = date_time_ymd(3000, 1, 1);
45
+
46
+ let key_pair = rcgen::KeyPair::generate_for(&PKCS_ECDSA_P256_SHA256).unwrap();
47
+ let ca_cert = params.self_signed(&key_pair).unwrap();
48
+ let ca_pem = ca_cert.pem();
49
+ Self {
50
+ ca_cert: ca_cert.into(),
51
+ ca_key_pair: key_pair.into(),
52
+ ca_pem: ca_pem.into(),
53
+ _cert_error: Default::default(),
54
+ _account_error: Default::default(),
55
+ }
56
+ }
57
+ }
58
+
59
+ impl<EC: Debug, EA: Debug> TestCache<EC, EA> {
60
+ pub fn new() -> Self {
61
+ Self::default()
62
+ }
63
+
64
+ pub fn ca_pem(&self) -> &str {
65
+ &self.ca_pem
66
+ }
67
+ }
68
+
69
+ #[async_trait]
70
+ impl<EC: Debug, EA: Debug> CertCache for TestCache<EC, EA> {
71
+ type EC = EC;
72
+ async fn load_cert(
73
+ &self,
74
+ domains: &[String],
75
+ _directory_url: &str,
76
+ ) -> Result<Option<Vec<u8>>, Self::EC> {
77
+ let key_pair = rcgen::KeyPair::generate_for(&PKCS_ECDSA_P256_SHA256).unwrap();
78
+ let mut params = CertificateParams::new(domains).unwrap();
79
+ let mut distinguished_name = DistinguishedName::new();
80
+ distinguished_name.push(DnType::CommonName, "Test Cert");
81
+ params.distinguished_name = distinguished_name;
82
+ params.not_before = date_time_ymd(2000, 1, 1);
83
+ params.not_after = date_time_ymd(3000, 1, 1);
84
+
85
+ let cert = match params.signed_by(&key_pair, &self.ca_cert, &self.ca_key_pair) {
86
+ Ok(cert) => cert,
87
+ Err(err) => {
88
+ log::error!("test cache: generation error: {:?}", err);
89
+ return Ok(None);
90
+ }
91
+ };
92
+
93
+ let cert_pem = cert.pem();
94
+
95
+ let pem = [
96
+ &key_pair.serialize_pem(),
97
+ "\n",
98
+ &cert_pem,
99
+ "\n",
100
+ &self.ca_pem,
101
+ ]
102
+ .concat();
103
+ Ok(Some(pem.into_bytes()))
104
+ }
105
+ async fn store_cert(
106
+ &self,
107
+ _domains: &[String],
108
+ _directory_url: &str,
109
+ _cert: &[u8],
110
+ ) -> Result<(), Self::EC> {
111
+ log::info!("test cache configured, could not store certificate");
112
+ Ok(())
113
+ }
114
+ }
115
+
116
+ #[async_trait]
117
+ impl<EC: Debug, EA: Debug> AccountCache for TestCache<EC, EA> {
118
+ type EA = EA;
119
+ async fn load_account(
120
+ &self,
121
+ _contact: &[String],
122
+ _directory_url: &str,
123
+ ) -> Result<Option<Vec<u8>>, Self::EA> {
124
+ log::info!("test cache configured, could not load account");
125
+ Ok(None)
126
+ }
127
+ async fn store_account(
128
+ &self,
129
+ _contact: &[String],
130
+ _directory_url: &str,
131
+ _account: &[u8],
132
+ ) -> Result<(), Self::EA> {
133
+ log::info!("test cache configured, could not store account");
134
+ Ok(())
135
+ }
136
+ }