Skip to main content

orca/models/
read.rs

1use crate::model::{self, ActorModel, Transition, TransitionAction, TransitionResult};
2
3use crate::{error::Error, run::EventRecord, state::*};
4use kanidm_client::KanidmClient;
5
6use async_trait::async_trait;
7use rand::RngExt;
8use rand_chacha::ChaCha8Rng;
9
10use std::time::Duration;
11
12enum State {
13    Unauthenticated,
14    Authenticated,
15}
16
17pub struct ActorReader {
18    state: State,
19    randomised_backoff_time: Duration,
20}
21
22impl ActorReader {
23    pub fn new(mut cha_rng: ChaCha8Rng, warmup_time_ms: u64) -> Self {
24        let max_backoff_time_in_ms = warmup_time_ms - 1000;
25        let randomised_backoff_time =
26            Duration::from_millis(cha_rng.random_range(0..max_backoff_time_in_ms));
27        ActorReader {
28            state: State::Unauthenticated,
29            randomised_backoff_time,
30        }
31    }
32}
33
34#[async_trait]
35impl ActorModel for ActorReader {
36    async fn transition(
37        &mut self,
38        client: &KanidmClient,
39        person: &Person,
40    ) -> Result<Vec<EventRecord>, Error> {
41        let transition = self.next_transition();
42
43        if let Some(delay) = transition.delay {
44            tokio::time::sleep(delay).await;
45        }
46
47        // Once we get to here, we want the transition to go ahead.
48        let (result, event) = match transition.action {
49            TransitionAction::Login => model::login(client, person).await,
50            TransitionAction::Logout => model::logout(client, person).await,
51            TransitionAction::PrivilegeReauth
52            | TransitionAction::WriteAttributePersonMail
53            | TransitionAction::ReadSelfAccount
54            | TransitionAction::WriteSelfPassword => return Err(Error::InvalidState),
55            TransitionAction::ReadSelfMemberOf => {
56                model::person_get_self_memberof(client, person).await
57            }
58        }?;
59
60        self.next_state(transition.action, result);
61
62        Ok(event)
63    }
64}
65
66impl ActorReader {
67    fn next_transition(&mut self) -> Transition {
68        match self.state {
69            State::Unauthenticated => Transition {
70                delay: Some(self.randomised_backoff_time),
71                action: TransitionAction::Login,
72            },
73            State::Authenticated => Transition {
74                delay: Some(Duration::from_secs(1)),
75                action: TransitionAction::ReadSelfMemberOf,
76            },
77        }
78    }
79
80    fn next_state(&mut self, action: TransitionAction, result: TransitionResult) {
81        // Is this a design flaw? We probably need to know what the state was that we
82        // requested to move to?
83        match (&self.state, action, result) {
84            (State::Unauthenticated, TransitionAction::Login, TransitionResult::Ok) => {
85                self.state = State::Authenticated;
86            }
87            (State::Authenticated, TransitionAction::ReadSelfMemberOf, TransitionResult::Ok) => {
88                self.state = State::Authenticated;
89            }
90            #[allow(clippy::unreachable)]
91            (_, _, TransitionResult::Ok) => unreachable!(),
92
93            (_, _, TransitionResult::Error) => {
94                self.state = State::Unauthenticated {};
95            }
96        }
97    }
98}