Skip to main content

orca/models/
write.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    AuthenticatedWithReauth,
16}
17
18pub struct ActorWriter {
19    state: State,
20    randomised_backoff_time: Duration,
21}
22
23impl ActorWriter {
24    pub fn new(mut cha_rng: ChaCha8Rng, warmup_time_ms: u64) -> Self {
25        let max_backoff_time_in_ms = 2 * warmup_time_ms / 3;
26        let randomised_backoff_time =
27            Duration::from_millis(cha_rng.random_range(0..max_backoff_time_in_ms));
28        ActorWriter {
29            state: State::Unauthenticated,
30            randomised_backoff_time,
31        }
32    }
33}
34
35#[async_trait]
36impl ActorModel for ActorWriter {
37    async fn transition(
38        &mut self,
39        client: &KanidmClient,
40        person: &Person,
41    ) -> Result<Vec<EventRecord>, Error> {
42        let transition = self.next_transition();
43
44        if let Some(delay) = transition.delay {
45            tokio::time::sleep(delay).await;
46        }
47
48        // Once we get to here, we want the transition to go ahead.
49        let (result, event) = match transition.action {
50            TransitionAction::Login => model::login(client, person).await,
51            TransitionAction::Logout => model::logout(client, person).await,
52            TransitionAction::PrivilegeReauth => model::privilege_reauth(client, person).await,
53            TransitionAction::ReadSelfMemberOf
54            | TransitionAction::ReadSelfAccount
55            | TransitionAction::WriteSelfPassword => return Err(Error::InvalidState),
56            TransitionAction::WriteAttributePersonMail => {
57                let mail = format!("{}@example.com", person.username);
58                let values = &[mail.as_str()];
59                model::person_set_self_mail(client, person, values).await
60            }
61        }?;
62
63        self.next_state(transition.action, result);
64
65        Ok(event)
66    }
67}
68
69impl ActorWriter {
70    fn next_transition(&mut self) -> Transition {
71        match self.state {
72            State::Unauthenticated => Transition {
73                delay: Some(self.randomised_backoff_time),
74                action: TransitionAction::Login,
75            },
76            State::Authenticated => Transition {
77                delay: Some(Duration::from_secs(2)),
78                action: TransitionAction::PrivilegeReauth,
79            },
80            State::AuthenticatedWithReauth => Transition {
81                delay: Some(Duration::from_secs(1)),
82                action: TransitionAction::WriteAttributePersonMail,
83            },
84        }
85    }
86
87    fn next_state(&mut self, action: TransitionAction, result: TransitionResult) {
88        // Is this a design flaw? We probably need to know what the state was that we
89        // requested to move to?
90        match (&self.state, action, result) {
91            (State::Unauthenticated, TransitionAction::Login, TransitionResult::Ok) => {
92                self.state = State::Authenticated;
93            }
94            (State::Authenticated, TransitionAction::PrivilegeReauth, TransitionResult::Ok) => {
95                self.state = State::AuthenticatedWithReauth;
96            }
97            (
98                State::AuthenticatedWithReauth,
99                TransitionAction::WriteAttributePersonMail,
100                TransitionResult::Ok,
101            ) => self.state = State::AuthenticatedWithReauth,
102
103            #[allow(clippy::unreachable)]
104            (_, _, TransitionResult::Ok) => {
105                unreachable!();
106            }
107            (_, _, TransitionResult::Error) => {
108                self.state = State::Unauthenticated;
109            }
110        }
111    }
112}