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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! A simple Client for the Lucid KV
//!
//! Currently supports operations for `get`, `put`, `delete` and `exists`.
//!
//! `lock`, `unlock`, `increment`, `decrement` and `ttl` are still being implemented.
//!
//! Notifications currently unsupported

#[macro_use]
extern crate failure;

#[macro_use]
extern crate fehler;

use bytes::Bytes;
use jsonwebtoken::EncodingKey;
use reqwest::header::{self, HeaderMap, HeaderValue};
use reqwest::{Body, Client, ClientBuilder, StatusCode, Url};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};

cfg_if::cfg_if! {
    if #[cfg(feature = "flexbuffers")] {
        use flexbuffers as serde_mod;
    } else {
        use serde_json as serde_mod;
    }
}

#[cfg(feature = "rustls-tls")]
pub use reqwest::Certificate;

/// Errors when doing Client operations
#[derive(Fail, Debug)]
pub enum Error {
    #[fail(display = "invalid url")]
    InvalidUrl,
    #[fail(display = "invalid client")]
    InvalidClient(reqwest::Error),
    #[fail(display = "invalid response")]
    InvalidResponse,
    #[fail(display = "invalid request: {}", _0)]
    InvalidRequest(reqwest::Error),
    #[fail(display = "unauthorized")]
    Unauthorized,
    #[fail(display = "conflict")]
    Conflict,
    #[fail(display = "serialize error")]
    SerializeError,
    #[fail(display = "deserialize error")]
    DeserializeError,
    #[fail(display = "invalid JWT key")]
    InvalidJWTKey,
}

/// Whether a Key was created or not
#[repr(u16)]
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum PutStatus {
    Ok,
    Created,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
struct Claims {
    pub sub: String,
    pub iss: String,
    pub iat: i64,
    pub exp: i64,
}

/// The main Client
#[derive(Clone, Debug)]
pub struct LucidClient {
    client: Client,
    url: Url,
    jwt_key: Option<EncodingKey>,
}

/// A builder for adding custom options to the LucidClient
#[derive(Debug)]
pub struct Builder<'a> {
    client: ClientBuilder,
    url: &'a str,
    jwt_key: Option<EncodingKey>,
}

impl<'a> Builder<'a> {
    /// Create a new Client Builder with a base URL (e.g. `http://localhost:7020`)
    pub fn new<U: AsRef<str> + ?Sized>(base_url: &'a U) -> Self {
        Self {
            client: ClientBuilder::new(),
            url: base_url.as_ref(),
            jwt_key: None,
        }
    }

    /// Add a JWT secret to authenticate against
    pub fn add_jwt_key<T: AsRef<[u8]> + ?Sized>(mut self, key: &T) -> Self {
        self.jwt_key = Some(EncodingKey::from_secret(key.as_ref()));
        self
    }

    #[cfg(feature = "rustls-tls")]
    /// Add a custom root certificate
    pub fn add_root_certificate(mut self, cert: Certificate) -> Self {
        self.client = self.client.add_root_certificate(cert);
        self
    }

    /// Build the LucidClient itself
    #[throws]
    pub fn build(self) -> LucidClient {
        LucidClient {
            client: self.client.build().map_err(Error::InvalidClient)?,
            url: Url::parse(self.url).map_err(|_| Error::InvalidUrl)?,
            jwt_key: self.jwt_key,
        }
    }
}

impl LucidClient {
    /// Build a basic Client. This is equivalent to `Builder::new(url).build()`
    #[throws]
    pub fn new<U: AsRef<str> + ?Sized>(base_url: &U) -> Self {
        Builder::new(base_url).build()?
    }

    /// Configure a Client with the Builder
    pub fn builder<'a, U: AsRef<str> + ?Sized>(base_url: &'a U) -> Builder<'a> {
        Builder::new(base_url)
    }

    /// Store a string or bytes as a value for a key. Creates a new key if it does not exist
    #[throws]
    pub async fn put_raw<K: AsRef<str>, V: Into<Body>>(&self, key: K, value: V) -> PutStatus {
        let res = self
            .client
            .put(self.key_url(key)?)
            .headers(self.authorization()?)
            .body(value)
            .send()
            .await
            .map_err(Error::InvalidRequest)?;
        match res.status() {
            StatusCode::OK => PutStatus::Ok,
            StatusCode::CREATED => PutStatus::Created,
            StatusCode::UNAUTHORIZED => throw!(Error::Unauthorized),
            StatusCode::CONFLICT => throw!(Error::Conflict),
            _ => throw!(Error::InvalidResponse),
        }
    }

    /// Gets raw bytes from a key's value
    #[throws]
    pub async fn get_raw<K: AsRef<str>>(&self, key: K) -> Option<Bytes> {
        let res = self
            .client
            .get(self.key_url(key)?)
            .headers(self.authorization()?)
            .send()
            .await
            .map_err(Error::InvalidRequest)?;
        match res.status() {
            StatusCode::OK => Some(res.bytes().await.map_err(|_| Error::InvalidResponse)?),
            StatusCode::NOT_FOUND => None,
            _ => throw!(Error::InvalidResponse),
        }
    }

    /// Delete a key's value. Returns `true` if the key existed and was actually deleted
    #[throws]
    pub async fn delete<K: AsRef<str>>(&self, key: K) -> bool {
        let res = self
            .client
            .delete(self.key_url(key)?)
            .headers(self.authorization()?)
            .send()
            .await
            .map_err(Error::InvalidRequest)?;
        match res.status() {
            StatusCode::OK | StatusCode::NO_CONTENT => true,
            StatusCode::NOT_FOUND => false,
            StatusCode::UNAUTHORIZED => throw!(Error::Unauthorized),
            _ => throw!(Error::InvalidResponse),
        }
    }

    /// Check if a key exists
    #[throws]
    pub async fn exists<K: AsRef<str>>(&self, key: K) -> bool {
        let res = self
            .client
            .head(self.key_url(key)?)
            .headers(self.authorization()?)
            .send()
            .await
            .map_err(Error::InvalidRequest)?;
        match res.status() {
            StatusCode::OK | StatusCode::NO_CONTENT => true,
            StatusCode::NOT_FOUND => false,
            StatusCode::UNAUTHORIZED => throw!(Error::Unauthorized),
            _ => throw!(Error::InvalidResponse),
        }
    }

    /// Serialize a rust object and store as the value for a key
    #[throws]
    pub async fn put<K: AsRef<str>, V: Serialize>(&self, key: K, value: &V) -> PutStatus {
        self.put_raw(
            key,
            serde_mod::to_vec(value).map_err(|_| Error::SerializeError)?,
        )
        .await?
    }

    /// Get the value for a key and deserialize it into a rust object
    #[throws]
    pub async fn get<K: AsRef<str>, V: DeserializeOwned>(&self, key: K) -> Option<V> {
        let bytes = self.get_raw(key).await?;
        match bytes {
            None => None,
            Some(bytes) => {
                Some(serde_mod::from_slice(bytes.as_ref()).map_err(|_| Error::DeserializeError)?)
            }
        }
    }

    #[inline]
    #[throws]
    fn key_url<K: AsRef<str>>(&self, key: K) -> Url {
        let encoded =
            percent_encoding::utf8_percent_encode(key.as_ref(), percent_encoding::NON_ALPHANUMERIC)
                .to_string();
        self.url
            .join(&format!("api/kv/{}", encoded))
            .map_err(|_| Error::InvalidUrl)?
    }

    #[inline]
    #[throws]
    fn authorization(&self) -> HeaderMap<HeaderValue> {
        let mut headers = HeaderMap::default();
        let key = if let Some(ref key) = self.jwt_key {
            key
        } else {
            return headers;
        };

        let iat = match SystemTime::now().duration_since(UNIX_EPOCH) {
            Ok(n) => n.as_secs() as i64,
            Err(_) => panic!("SystemTime before UNIX EPOCH!"),
        };
        let claims = Claims {
            iat,
            exp: iat + 60,
            ..Default::default()
        };
        let token = jsonwebtoken::encode(&jsonwebtoken::Header::default(), &claims, &key)
            .map_err(|_| Error::InvalidJWTKey)?;

        headers.append(
            header::AUTHORIZATION,
            format!("Bearer {}", token)
                .parse()
                .map_err(|_| Error::InvalidJWTKey)?,
        );
        headers
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, Serialize, Deserialize, Eq, PartialEq)]
    struct TestStruct {
        a: u32,
        b: String,
        c: Vec<u8>,
    }

    #[throws]
    fn client() -> LucidClient {
        #[allow(unused_mut, unused_assignments)]
        let mut builder = LucidClient::builder("http://localhost:7020");
        #[cfg(feature = "rustls-tls")]
        {
            builder = LucidClient::builder("https://localhost:7021");
            let ca_cert = Certificate::from_pem(
                std::fs::read("test_assets/ssl/ca-cert.pem")
                    .unwrap()
                    .as_ref(),
            )
            .unwrap();
            builder = builder.add_root_certificate(ca_cert);
        }
        builder.add_jwt_key("secret").build()?
    }

    #[test]
    #[throws]
    fn build() {
        LucidClient::new("http://localhost:7020")?;
        client()?;
    }

    #[tokio::test]
    async fn put_raw() -> Result<(), Error> {
        let client = client()?;
        client.put_raw("put_raw", "value1").await?;
        Ok(())
    }

    #[tokio::test]
    async fn put_raw_bytes() -> Result<(), Error> {
        let client = client()?;
        client
            .put_raw::<_, &[u8]>("put_raw_bytes", &[0, 1, 2, 3, 4])
            .await?;
        Ok(())
    }

    #[tokio::test]
    async fn get_raw() -> Result<(), Error> {
        let client = client()?;
        let test_value = "value1";
        client.put_raw("get_raw", test_value).await?;
        let db_value = client.get_raw("get_raw").await?;
        assert_eq!(
            test_value,
            String::from_utf8_lossy(db_value.unwrap().as_ref())
        );
        Ok(())
    }

    #[tokio::test]
    async fn update_raw() -> Result<(), Error> {
        let client = client()?;
        let key = "update_raw";

        let test_value1 = "value1";
        client.put_raw(key, test_value1).await?;
        let db_value = client.get_raw(key).await?;
        assert_eq!(
            test_value1,
            String::from_utf8_lossy(db_value.unwrap().as_ref())
        );

        let test_value2 = "value2";
        client.put_raw(key, test_value2).await?;
        let db_value = client.get_raw(key).await?;
        assert_eq!(
            test_value2,
            String::from_utf8_lossy(db_value.unwrap().as_ref())
        );

        Ok(())
    }

    #[tokio::test]
    async fn delete_missing() -> Result<(), Error> {
        let client = client()?;
        assert!(!client.delete("delete_missing").await?);
        Ok(())
    }

    #[tokio::test]
    async fn delete() -> Result<(), Error> {
        let client = client()?;
        let key = "delete";

        let test_value = "value";
        client.put_raw(key, test_value).await?;
        let db_value = client.get_raw(key).await?;
        assert_eq!(
            test_value,
            String::from_utf8_lossy(db_value.unwrap().as_ref())
        );

        assert!(client.delete(key).await?);
        let db_value = client.get_raw(key).await?;
        assert!(db_value.is_none());

        Ok(())
    }

    #[tokio::test]
    async fn exists_false() -> Result<(), Error> {
        let client = client()?;
        assert!(!client.exists("exists_false").await?);
        Ok(())
    }

    #[tokio::test]
    async fn exists_true() -> Result<(), Error> {
        let client = client()?;
        client.put_raw("exists_true", "value").await?;
        assert!(client.exists("exists_true").await?);
        Ok(())
    }

    #[cfg(feature = "serde")]
    #[tokio::test]
    async fn put() -> Result<(), Error> {
        let client = client()?;
        let value = TestStruct {
            a: 1,
            b: "cool".to_string(),
            c: vec![1, 2, 3],
        };
        client.put("put", &value).await?;
        Ok(())
    }

    #[cfg(feature = "serde")]
    #[tokio::test]
    async fn get() -> Result<(), Error> {
        let client = client()?;
        let test_value = TestStruct {
            a: 1,
            b: "cool".to_string(),
            c: vec![1, 2, 3],
        };
        client.put("get", &test_value).await?;
        let db_value = client.get("get").await?;
        assert_eq!(Some(test_value), db_value);
        Ok(())
    }
}