|
| 1 | +//! #Signet Quincey builder permissioning system. |
| 2 | +//! |
| 3 | +//! The permissioning system decides which builder can perform a certain action |
| 4 | +//! at a given time. The permissioning system uses a simple round-robin design, |
| 5 | +//! where each builder is allowed to perform an action at a specific slot. |
| 6 | +//! Builders are permissioned based on their sub, which is present in the JWT |
| 7 | +//! token they acquire from our OAuth service. |
| 8 | +
|
| 9 | +use crate::{ |
| 10 | + perms::{SlotAuthzConfig, SlotAuthzConfigError}, |
| 11 | + utils::{ |
| 12 | + calc::SlotCalculator, |
| 13 | + from_env::{FromEnv, FromEnvErr, FromEnvVar}, |
| 14 | + }, |
| 15 | +}; |
| 16 | + |
| 17 | +/// The builder list env var. |
| 18 | +const BUILDERS: &str = "PERMISSIONED_BUILDERS"; |
| 19 | + |
| 20 | +fn now() -> u64 { |
| 21 | + chrono::Utc::now().timestamp().try_into().unwrap() |
| 22 | +} |
| 23 | + |
| 24 | +/// Possible errors when permissioning a builder. |
| 25 | +#[derive(Debug, thiserror::Error, Clone, Copy, PartialEq, Eq)] |
| 26 | +pub enum BuilderPermissionError { |
| 27 | + /// Action attempt too early. |
| 28 | + #[error("action attempt too early")] |
| 29 | + ActionAttemptTooEarly, |
| 30 | + |
| 31 | + /// Action attempt too late. |
| 32 | + #[error("action attempt too late")] |
| 33 | + ActionAttemptTooLate, |
| 34 | + |
| 35 | + /// Builder not permissioned for this slot. |
| 36 | + #[error("builder not permissioned for this slot")] |
| 37 | + NotPermissioned, |
| 38 | +} |
| 39 | + |
| 40 | +/// Possible errors when loading the builder configuration. |
| 41 | +#[derive(Debug, thiserror::Error, Clone, PartialEq, Eq)] |
| 42 | +pub enum BuilderConfigError { |
| 43 | + /// Error loading the environment variable. |
| 44 | + #[error( |
| 45 | + "failed to parse environment variable. Expected a comma-seperated list of UUIDs. Got: {input}" |
| 46 | + )] |
| 47 | + ParseError { |
| 48 | + /// The environment variable name. |
| 49 | + env_var: String, |
| 50 | + /// The contents of the environment variable. |
| 51 | + input: String, |
| 52 | + }, |
| 53 | + |
| 54 | + /// Error loading the slot authorization configuration. |
| 55 | + #[error(transparent)] |
| 56 | + SlotAutzConfig(#[from] SlotAuthzConfigError), |
| 57 | +} |
| 58 | + |
| 59 | +/// An individual builder. |
| 60 | +#[derive(Clone, Debug)] |
| 61 | +pub struct Builder { |
| 62 | + /// The sub of the builder. |
| 63 | + pub sub: String, |
| 64 | +} |
| 65 | + |
| 66 | +impl Builder { |
| 67 | + /// Create a new builder. |
| 68 | + pub fn new(sub: impl AsRef<str>) -> Self { |
| 69 | + Self { |
| 70 | + sub: sub.as_ref().to_owned(), |
| 71 | + } |
| 72 | + } |
| 73 | + /// Get the sub of the builder. |
| 74 | + #[allow(clippy::missing_const_for_fn)] // false positive, non-const deref |
| 75 | + pub fn sub(&self) -> &str { |
| 76 | + &self.sub |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Builders struct to keep track of the builders that are allowed to perform actions. |
| 81 | +#[derive(Clone, Debug)] |
| 82 | +pub struct Builders { |
| 83 | + /// The list of builders. |
| 84 | + /// |
| 85 | + /// This is configured in the environment variable `PERMISSIONED_BUILDERS`, |
| 86 | + /// as a list of comma-separated UUIDs. |
| 87 | + pub builders: Vec<Builder>, |
| 88 | + |
| 89 | + /// The slot authorization configuration. See [`SlotAuthzConfig`] for more |
| 90 | + /// information and env vars |
| 91 | + config: SlotAuthzConfig, |
| 92 | +} |
| 93 | + |
| 94 | +impl Builders { |
| 95 | + /// Create a new Builders struct. |
| 96 | + pub const fn new(builders: Vec<Builder>, config: SlotAuthzConfig) -> Self { |
| 97 | + Self { builders, config } |
| 98 | + } |
| 99 | + |
| 100 | + /// Get the calculator instance. |
| 101 | + pub const fn calc(&self) -> SlotCalculator { |
| 102 | + self.config.calc() |
| 103 | + } |
| 104 | + |
| 105 | + /// Get the slot authorization configuration. |
| 106 | + pub const fn config(&self) -> &SlotAuthzConfig { |
| 107 | + &self.config |
| 108 | + } |
| 109 | + |
| 110 | + /// Get the builder at a specific index. |
| 111 | + /// |
| 112 | + /// # Panics |
| 113 | + /// |
| 114 | + /// Panics if the index is out of bounds from the builders array. |
| 115 | + pub fn builder_at(&self, index: usize) -> &Builder { |
| 116 | + &self.builders[index] |
| 117 | + } |
| 118 | + |
| 119 | + /// Get the builder permissioned at a specific timestamp. |
| 120 | + pub fn builder_at_timestamp(&self, timestamp: u64) -> &Builder { |
| 121 | + self.builder_at(self.index(timestamp) as usize) |
| 122 | + } |
| 123 | + |
| 124 | + /// Get the index of the builder that is allowed to sign a block for a |
| 125 | + /// particular timestamp. |
| 126 | + pub fn index(&self, timestamp: u64) -> u64 { |
| 127 | + self.config.calc().calculate_slot(timestamp) % self.builders.len() as u64 |
| 128 | + } |
| 129 | + |
| 130 | + /// Get the index of the builder that is allowed to sign a block at the |
| 131 | + /// current timestamp. |
| 132 | + pub fn index_now(&self) -> u64 { |
| 133 | + self.index(now()) |
| 134 | + } |
| 135 | + |
| 136 | + /// Get the builder that is allowed to sign a block at the current timestamp. |
| 137 | + pub fn current_builder(&self) -> &Builder { |
| 138 | + self.builder_at(self.index_now() as usize) |
| 139 | + } |
| 140 | + |
| 141 | + /// Check the query bounds for the current timestamp. |
| 142 | + fn check_query_bounds(&self) -> Result<(), BuilderPermissionError> { |
| 143 | + let current_slot_time = self.calc().current_timepoint_within_slot(); |
| 144 | + if current_slot_time < self.config.block_query_start() { |
| 145 | + return Err(BuilderPermissionError::ActionAttemptTooEarly); |
| 146 | + } |
| 147 | + if current_slot_time > self.config.block_query_cutoff() { |
| 148 | + return Err(BuilderPermissionError::ActionAttemptTooLate); |
| 149 | + } |
| 150 | + Ok(()) |
| 151 | + } |
| 152 | + |
| 153 | + /// Checks if a builder is allowed to perform an action. |
| 154 | + /// This is based on the current timestamp and the builder's sub. It's a |
| 155 | + /// round-robin design, where each builder is allowed to perform an action |
| 156 | + /// at a specific slot, and what builder is allowed changes with each slot. |
| 157 | + pub fn is_builder_permissioned(&self, sub: &str) -> Result<(), BuilderPermissionError> { |
| 158 | + self.check_query_bounds()?; |
| 159 | + |
| 160 | + if sub != self.current_builder().sub { |
| 161 | + tracing::debug!( |
| 162 | + builder = %sub, |
| 163 | + permissioned_builder = %self.current_builder().sub, |
| 164 | + "Builder not permissioned for this slot" |
| 165 | + ); |
| 166 | + return Err(BuilderPermissionError::NotPermissioned); |
| 167 | + } |
| 168 | + |
| 169 | + Ok(()) |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl FromEnv for Builders { |
| 174 | + type Error = BuilderConfigError; |
| 175 | + |
| 176 | + fn from_env() -> Result<Self, FromEnvErr<Self::Error>> { |
| 177 | + let s = String::from_env_var(BUILDERS) |
| 178 | + .map_err(FromEnvErr::infallible_into::<BuilderConfigError>)?; |
| 179 | + let builders = s.split(',').map(Builder::new).collect(); |
| 180 | + |
| 181 | + let config = SlotAuthzConfig::from_env().map_err(FromEnvErr::from)?; |
| 182 | + |
| 183 | + Ok(Self { builders, config }) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +#[cfg(test)] |
| 188 | +mod test { |
| 189 | + |
| 190 | + use super::*; |
| 191 | + use crate::{perms, utils::calc}; |
| 192 | + |
| 193 | + #[test] |
| 194 | + fn load_builders() { |
| 195 | + unsafe { |
| 196 | + std::env::set_var(BUILDERS, "0,1,2,3,4,5"); |
| 197 | + |
| 198 | + std::env::set_var(calc::START_TIMESTAMP, "1"); |
| 199 | + std::env::set_var(calc::SLOT_OFFSET, "0"); |
| 200 | + std::env::set_var(calc::SLOT_DURATION, "12"); |
| 201 | + |
| 202 | + std::env::set_var(perms::config::BLOCK_QUERY_START, "1"); |
| 203 | + std::env::set_var(perms::config::BLOCK_QUERY_CUTOFF, "11"); |
| 204 | + }; |
| 205 | + |
| 206 | + let builders = Builders::from_env().unwrap(); |
| 207 | + assert_eq!(builders.builder_at(0).sub, "0"); |
| 208 | + assert_eq!(builders.builder_at(1).sub, "1"); |
| 209 | + assert_eq!(builders.builder_at(2).sub, "2"); |
| 210 | + assert_eq!(builders.builder_at(3).sub, "3"); |
| 211 | + assert_eq!(builders.builder_at(4).sub, "4"); |
| 212 | + assert_eq!(builders.builder_at(5).sub, "5"); |
| 213 | + |
| 214 | + assert_eq!(builders.calc().slot_offset(), 0); |
| 215 | + assert_eq!(builders.calc().slot_duration(), 12); |
| 216 | + assert_eq!(builders.calc().start_timestamp(), 1); |
| 217 | + |
| 218 | + assert_eq!(builders.config.block_query_start(), 1); |
| 219 | + assert_eq!(builders.config.block_query_cutoff(), 11); |
| 220 | + } |
| 221 | +} |
0 commit comments