Debian Codegraph @

crt/s2n/bindings/rust/aws-kms-tls-auth/src/epoch_schedule.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 //! The key epochs and fetching behavior are designed to accomplish the following
5 //! goals:
6 //! Requirement 1: if fetching new keys fails, there should be at least 24 hours before
7 //! handshakes actually start failing.
8 //! Requirement 2: traffic to KMS should be smooth, avoiding any spikes at e.g.
9 //! the top of the hour.
10 //!
11 //! ```text
12 //! Epoch
13 //! 0 1 2 3 4
14 //! |-----|-----|-----|-----|
15 //! ^
16 //! epoch 3 start
17 //!```
18 //!
19 //! To satisfy these requirements, we fetch the key for epoch `n` during epoch
20 //! `n - 2`. Each peer adds [0, 24 * 3600) seconds of delay to smooth out traffic
21 //! to KMS. This is referred to as a "smoothing factor".
22 //!
23 //! ```text
24 //! Epoch
25 //! 0 1 2 3 4
26 //! |-----|-----|-----|-----|
27 //! ++++++ ^
28 //! ^ epoch 3 start
29 //! |
30 //! fetch window for epoch 3
31 //! ```
32
33 use rand::Rng;
34 use std::time::{Duration, SystemTime};
35
36 /// The epoch duration controls how long an epoch secret is used for.
37 pub(crate) const EPOCH_DURATION: Duration = Duration::from_secs(3_600 * 24);
38
39 /// Return a "smoothing factor" indicating how long the actor should wait before
40 /// fetching the key for some epoch
41 pub fn smoothing_factor() -> Duration {
42 rand::rng().random_range(Duration::from_secs(0)..EPOCH_DURATION)
43 }
44
45 pub fn current_epoch() -> u64 {
46 // SAFETY: this method will panic if the current system clock is set to
47 // a time before the unix epoch. This is not a recoverable error, so we
48 // panic
49 let now = SystemTime::now()
50 .duration_since(SystemTime::UNIX_EPOCH)
51 .expect("expected system time to be after UNIX epoch");
52 now.as_secs() / (EPOCH_DURATION.as_secs())
53 }
54
55 /// Return the instant in time that `epoch` starts
56 pub fn epoch_start(epoch: u64) -> SystemTime {
57 SystemTime::UNIX_EPOCH + (EPOCH_DURATION * (epoch as u32))
58 }
59
60 /// The Duration between now and the start of epoch
61 ///
62 /// returns None if the epoch has already started
63 pub fn until_epoch_start(epoch: u64) -> Option<Duration> {
64 epoch_start(epoch).duration_since(SystemTime::now()).ok()
65 }
66
67 /// The Duration between now and when the actor should make the network call
68 /// to KMS to retrieve the secret for `epoch`.
69 ///
70 /// returns None if the fetch should already have occurred
71 pub(crate) fn until_fetch(epoch: u64, smoothing_factor: Duration) -> Option<Duration> {
72 // we always want to fetch the key at least one epoch (24 hours) before the
73 // key is needed.
74 let fetch_time = {
75 let fetch_epoch = epoch - 2;
76
77 let fetch_epoch_start = epoch_start(fetch_epoch);
78
79 fetch_epoch_start + smoothing_factor
80 };
81
82 fetch_time.duration_since(SystemTime::now()).ok()
83 }
84
85 // Note that these tests technically have a "race condition". We assume that
86 // "current_epoch + 1" is in the future, but if the test is started right before the
87 // epoch boundary that might not be true.
88 //
89 // However the failure rate is approximately 1/ 3,200,000,000 for these tests, so
90 // the simpler approach is worth the theoretical risk.
91 // Flakiness Probability:
92 // test runtime: 27.48 us -> unit test runtime, window of "flaky"
93 // probability = 27.48 us / 24 hr
94 // approximately 1 / 3_200_000_000
95 #[cfg(test)]
96 mod tests {
97 use super::*;
98 #[test]
99 fn current() {
100 let current_epoch = current_epoch();
101 let start = epoch_start(current_epoch);
102 assert!(SystemTime::now().duration_since(start).is_ok());
103 let future_start = epoch_start(current_epoch + 1);
104 assert!(future_start.duration_since(SystemTime::now()).is_ok());
105 }
106
107 #[test]
108 fn until_start() {
109 let current = current_epoch();
110 // epoch start was in the past, and should return none
111 assert!(until_epoch_start(current).is_none());
112 assert!(until_epoch_start(current + 1).is_some());
113 }
114
115 #[test]
116 fn fetch() {
117 const ZERO_DURATION: Duration = Duration::from_secs(0);
118
119 let current_epoch = current_epoch();
120 assert!(until_fetch(current_epoch, ZERO_DURATION).is_none());
121 assert!(until_fetch(current_epoch + 1, ZERO_DURATION).is_none());
122 assert!(until_fetch(current_epoch + 2, ZERO_DURATION).is_none());
123 assert!(until_fetch(current_epoch + 2, EPOCH_DURATION).is_some());
124 }
125
126 #[test]
127 fn smoothing_factor() {
128 for _ in 0..10 {
129 assert!(super::smoothing_factor() < EPOCH_DURATION);
130 }
131 }
132 }