1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use anyhow::Result;
use async_trait::async_trait;
use sqlx::FromRow;
use sqlx::{Database, Executor, Postgres};
use tracing::info;
use uuid::Uuid;

use super::domain::{DomainFacadeDatabase, DomainFacadeMemory};
use super::{DatabaseFacade, Domain, InMemoryFacade, InMemoryFacadeGuard};
use crate::util::{now, to_i64, HOUR_IN_SECONDS};

#[derive(sqlx::Type, Debug, PartialEq, Clone)]
#[repr(i32)]
pub enum State {
    Ok = 0,
    Updating = 1,
}

#[derive(FromRow, Debug, Clone)]
pub struct Cert {
    pub id: String,
    pub update: i64,
    pub state: State,
    pub cert: Option<String>,
    pub private: Option<String>,
    #[sqlx(rename = "domain_id")]
    pub domain: String,
}

impl PartialEq for Cert {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id && self.cert == other.cert && self.private == other.private
    }
}

impl Cert {
    // remove expect
    fn new(domain: &Domain) -> Self {
        Cert {
            id: Uuid::new_v4().to_simple().to_string(),
            update: to_i64(&now()),
            state: State::Updating,
            cert: None,
            private: None,
            domain: domain.id.clone(),
        }
    }
}

#[async_trait]
pub trait CertFacade {
    async fn first_cert(&self) -> Result<Option<Cert>, sqlx::Error>;
    async fn update_cert(&self, cert: &Cert) -> Result<(), sqlx::Error>;
    async fn create_cert(&self, cert: &Cert) -> Result<(), sqlx::Error>;
    async fn start_cert(&self) -> Result<Option<Cert>>;
    async fn stop_cert(&self, memory_cert: &mut Cert) -> Result<(), sqlx::Error>;
}

#[async_trait]
trait CertFacadeDatabase<DB: Database> {
    async fn first_cert<'a, E: Executor<'a, Database = DB>>(
        &self,
        executor: E,
    ) -> Result<Option<Cert>, sqlx::Error>;

    async fn update_cert<'a, E: Executor<'a, Database = DB>>(
        &self,
        executor: E,
        cert: &Cert,
    ) -> Result<(), sqlx::Error>;

    async fn create_cert<'a, E: Executor<'a, Database = DB>>(
        &self,
        executor: E,
        cert: &Cert,
    ) -> Result<(), sqlx::Error>;
}

#[async_trait]
impl CertFacadeDatabase<Postgres> for DatabaseFacade<Postgres> {
    async fn first_cert<'a, E: Executor<'a, Database = Postgres>>(
        &self,
        executor: E,
    ) -> Result<Option<Cert>, sqlx::Error> {
        sqlx::query_as("SELECT * FROM cert LIMIT 1")
            .fetch_optional(executor)
            .await
    }

    async fn update_cert<'a, E: Executor<'a, Database = Postgres>>(
        &self,
        executor: E,
        cert: &Cert,
    ) -> Result<(), sqlx::Error> {
        sqlx::query("UPDATE cert SET update = $1, state = $2, cert = $3, private = $4, domain_id = $5 WHERE id = $6")
            .bind(&cert.update)
            .bind(&cert.state)
            .bind(&cert.cert)
            .bind(&cert.private)
            .bind(&cert.domain)
            .bind(&cert.id)
            .execute(executor)
            .await?;

        Ok(())
    }

    async fn create_cert<'a, E: Executor<'a, Database = Postgres>>(
        &self,
        executor: E,
        cert: &Cert,
    ) -> Result<(), sqlx::Error> {
        sqlx::query("INSERT INTO cert (id, update, state, cert, private, domain_id) VALUES ($1, $2, $3, $4, $5, $6)")
            .bind(&cert.id)
            .bind(&cert.update)
            .bind(&cert.state)
            .bind(&cert.cert)
            .bind(&cert.private)
            .bind(&cert.domain)
            .execute(executor)
            .await?;

        Ok(())
    }
}

#[async_trait]
impl CertFacade for DatabaseFacade<Postgres> {
    async fn first_cert(&self) -> Result<Option<Cert>, sqlx::Error> {
        CertFacadeDatabase::first_cert(self, &self.pool).await
    }

    async fn update_cert(&self, cert: &Cert) -> Result<(), sqlx::Error> {
        CertFacadeDatabase::update_cert(self, &self.pool, cert).await
    }

    async fn create_cert(&self, cert: &Cert) -> Result<(), sqlx::Error> {
        CertFacadeDatabase::create_cert(self, &self.pool, cert).await
    }

    async fn start_cert(&self) -> Result<Option<Cert>> {
        let mut transaction = self.pool.begin().await?;

        let cert = CertFacadeDatabase::first_cert(self, &mut transaction).await?;

        let cert = match cert {
            Some(mut cert) if cert.state == State::Ok => {
                cert.state = State::Updating;
                CertFacadeDatabase::update_cert(self, &mut transaction, &cert).await?;
                Some(cert)
            }
            // cert is in updating state as there are only to cert.state
            Some(mut cert) => {
                let now = to_i64(&now());
                let one_hour_ago = now - HOUR_IN_SECONDS as i64;
                // longer ago than 1 hour so probably timed out
                if cert.update < one_hour_ago {
                    cert.update = now;
                    // todo: dont think this is needed
                    cert.state = State::Updating;
                    CertFacadeDatabase::update_cert(self, &mut transaction, &cert).await?;
                    Some(cert)
                } else {
                    info!("job still in progress");
                    None
                }
            }
            None => {
                let domain = Domain::new()?;
                let cert = Cert::new(&domain);

                DomainFacadeDatabase::create_domain(self, &mut transaction, &domain).await?;
                CertFacadeDatabase::create_cert(self, &mut transaction, &cert).await?;
                Some(cert)
            }
        };

        transaction.commit().await?;

        Ok(cert)
    }

    async fn stop_cert(&self, memory_cert: &mut Cert) -> Result<(), sqlx::Error> {
        let mut transaction = self.pool.begin().await?;

        match CertFacadeDatabase::first_cert(self, &mut transaction).await? {
            // only stop cert if the update times match and no other interval picked up the job
            Some(cert) if cert.state == State::Updating && cert.update == memory_cert.update => {
                memory_cert.state = State::Ok;
                CertFacadeDatabase::update_cert(self, &self.pool, &memory_cert).await?;
            }
            _ => {}
        }

        transaction.commit().await?;
        Ok(())
    }
}

trait CertFacadeMemory {
    fn first_cert(&self, lock: &mut InMemoryFacadeGuard<'_>) -> Option<Cert> {
        lock.certs.values().next().map(Clone::clone)
    }

    fn update_cert(&self, lock: &mut InMemoryFacadeGuard<'_>, cert: &Cert) {
        *lock.certs.get_mut(&cert.id).unwrap() = cert.clone();
    }

    fn create_cert(&self, lock: &mut InMemoryFacadeGuard<'_>, cert: &Cert) {
        lock.certs.insert(cert.id.clone(), cert.clone());
    }
}

impl CertFacadeMemory for InMemoryFacade {}

#[async_trait]
impl CertFacade for InMemoryFacade {
    async fn first_cert(&self) -> Result<Option<Cert>, sqlx::Error> {
        let mut lock = self.0.lock();
        let cert = CertFacadeMemory::first_cert(self, &mut lock);
        Ok(cert)
    }

    async fn update_cert(&self, cert: &Cert) -> Result<(), sqlx::Error> {
        let mut lock = self.0.lock();
        CertFacadeMemory::update_cert(self, &mut lock, cert);

        Ok(())
    }

    async fn create_cert(&self, cert: &Cert) -> Result<(), sqlx::Error> {
        let mut lock = self.0.lock();
        CertFacadeMemory::create_cert(self, &mut lock, cert);

        Ok(())
    }

    async fn start_cert(&self) -> Result<Option<Cert>> {
        let mut transaction = self.0.lock();
        let cert = CertFacadeMemory::first_cert(self, &mut transaction);

        let cert = match cert {
            Some(mut cert) if cert.state == State::Ok => {
                cert.state = State::Updating;
                CertFacadeMemory::update_cert(self, &mut transaction, &cert);
                Some(cert)
            }
            Some(mut cert) => {
                let now = to_i64(&now());
                let one_hour_ago = now - HOUR_IN_SECONDS as i64;
                // longer ago than 1 hour so probably timed out
                if cert.update < one_hour_ago {
                    cert.update = now;
                    cert.state = State::Updating;
                    CertFacadeMemory::update_cert(self, &mut transaction, &cert);
                    Some(cert)
                } else {
                    info!("job still in progress");
                    None
                }
            }
            None => {
                let domain = Domain::new()?;
                let cert = Cert::new(&domain);

                DomainFacadeMemory::create_domain(self, &mut transaction, &domain);
                CertFacadeMemory::create_cert(self, &mut transaction, &cert);
                Some(cert)
            }
        };

        Ok(cert)
    }

    async fn stop_cert(&self, memory_cert: &mut Cert) -> Result<(), sqlx::Error> {
        let mut transaction = self.0.lock();

        match CertFacadeMemory::first_cert(self, &mut transaction) {
            Some(cert) if cert.state == State::Updating && cert.update == memory_cert.update => {
                memory_cert.state = State::Ok;
                CertFacadeMemory::update_cert(self, &mut transaction, &memory_cert);
            }
            _ => {}
        }

        Ok(())
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use testcontainers::clients::Cli;
    use testcontainers::images::postgres::Postgres;
    use testcontainers::Docker;

    use super::{Cert, CertFacade, DatabaseFacade, State};
    use crate::setup_database;
    use crate::util::{now, to_i64};

    pub(crate) fn create_cert() -> Cert {
        Cert {
            id: "1".to_owned(),
            update: to_i64(&now()),
            state: State::Ok,
            // todo: this is the wrong cert and key only to fix compilation
            cert: Some(include_str!("../../tests/ca.crt").to_owned()),
            private: Some(include_str!("../../tests/cert.key").to_owned()),
            domain: "acme-dns-rust.com".to_owned(),
        }
    }

    #[cfg(not(feature = "disable-docker"))]
    //#[tokio::test]
    async fn _test_postgres_cert_facade() {
        let docker = Cli::default();
        let node = docker.run(Postgres::default());

        let connection_string = &format!(
            "postgres://postgres:postgres@localhost:{}/postgres",
            node.get_host_port(5432).unwrap()
        );

        let pool = setup_database(connection_string).await.unwrap();
        let facade = DatabaseFacade::from(pool);
        let mut cert = create_cert();

        facade.create_cert(&cert).await.unwrap();

        let actual = facade.first_cert().await.unwrap().unwrap();
        assert_eq!(cert, actual);

        cert.state = State::Updating;
        facade.update_cert(&cert).await.unwrap();
        let actual = facade.first_cert().await.unwrap().unwrap();
        assert_eq!(cert, actual);
    }
}