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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
//! Top level connection object that hold a http client (either synchronous or
//! asynchronous), arango URL, and buffered accessible databases object.
//!
//! ## Establishing connections
//! There is three way to establish connections:
//! - jwt
//! - basic auth
//! - no authentication
//!
//! So are the `arangors` API:
//! Example:
//!
//! - With authentication
//!
//! ```rust
//! use arangors::Connection;
//!
//! # #[cfg_attr(any(feature="reqwest_async"), maybe_async::maybe_async, tokio::main)]
//! # #[cfg_attr(any(feature="surf_async"), maybe_async::maybe_async, async_std::main)]
//! # #[cfg_attr(feature = "blocking", maybe_async::must_be_sync)]
//! # async fn main() {
//! let conn = Connection::establish_jwt("http://localhost:8529", "username", "password")
//!     .await
//!     .unwrap();
//! let conn = Connection::establish_basic_auth("http://localhost:8529", "username", "password")
//!     .await
//!     .unwrap();
//! # }
//! ```
//!
//! - No authentication
//! ```rust, ignore
//! use arangors::Connection;
//! let conn = Connection::establish_without_auth("http://localhost:8529").await.unwrap();
//! ```

use std::{collections::HashMap, fmt::Debug, sync::Arc};

use http::header::{HeaderMap, AUTHORIZATION, SERVER};
use log::{info, trace};
use serde::{Deserialize, Serialize};
use url::Url;

use maybe_async::maybe_async;

use crate::{client::ClientExt, response::ArangoResult, ClientError};

use super::{database::Database, response::deserialize_response};

use self::{
    auth::Auth,
    role::{Admin, Normal},
};

mod auth;

pub mod role {
    #[derive(Debug)]
    pub struct Normal;

    #[derive(Debug)]
    pub struct Admin;
}

#[derive(Deserialize, Serialize, Debug)]
pub enum Permission {
    #[serde(rename = "none")]
    NoAccess,
    #[serde(rename = "ro")]
    ReadOnly,
    #[serde(rename = "rw")]
    ReadWrite,
}

#[derive(Debug, Deserialize)]
pub struct Version {
    pub server: String,
    pub version: String,
    pub license: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DatabaseDetails {
    pub name: String,
    pub id: String,
    pub path: String,
    pub is_system: bool,
}

#[cfg(any(feature = "reqwest_async", feature = "reqwest_blocking"))]
pub type Connection = GenericConnection<crate::client::reqwest::ReqwestClient>;

#[cfg(feature = "surf_async")]
pub type Connection = GenericConnection<crate::client::surf::SurfClient>;

/// Connection is the top level API for this crate.
/// It contains a http client, information about authentication, arangodb url.
#[derive(Debug, Clone)]
pub struct GenericConnection<C: ClientExt, S = Normal> {
    session: Arc<C>,
    arango_url: Url,
    username: String,
    state: S,
    pub(crate) phantom: (),
}

impl<S, C: ClientExt> GenericConnection<C, S> {
    /// Validate the server at given arango url
    ///
    /// Cast `failure::Error` if
    /// - Connection failed
    /// - SERVER header in response header is not `ArangoDB` or empty
    #[maybe_async]
    pub async fn validate_server(&self) -> Result<(), ClientError> {
        let arango_url = self.arango_url.as_str();
        let client = &self.session;
        let resp = client.get(arango_url.parse().unwrap(), "").await?;
        // have `Server` in header
        match resp.headers().get(SERVER) {
            Some(server) => {
                // value of `Server` is `ArangoDB`
                let server_value = server.to_str().unwrap();
                if server_value.eq_ignore_ascii_case("ArangoDB") {
                    trace!("Validate arangoDB server done.");
                    Ok(())
                } else {
                    Err(ClientError::InvalidServer(server_value.to_owned()))
                }
            }
            None => Err(ClientError::InvalidServer("Unknown".to_owned())),
        }
    }

    /// Get url for remote arangoDB server.
    pub fn get_url(&self) -> &Url {
        &self.arango_url
    }

    /// Get HTTP session.
    ///
    /// Users can use this method to get a authorized session to access
    /// arbitrary path on arangoDB Server.
    ///
    /// TODO This method should only be public in this crate when all features
    ///     are implemented.
    pub fn get_session(&self) -> Arc<C> {
        Arc::clone(&self.session)
    }

    /// Get database object with name.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn db(&self, name: &str) -> Result<Database<'_, C>, ClientError> {
        let db = Database::new(&self, name);
        db.info().await?;
        Ok(db)
    }

    /// Get a list of accessible database
    ///
    /// This function uses the API that is used to retrieve a list of
    /// all databases the current user can access.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn accessible_databases(&self) -> Result<HashMap<String, Permission>, ClientError> {
        let url = self
            .arango_url
            .join(&format!("/_api/user/{}/database", &self.username))
            .unwrap();
        let resp = self.session.get(url, "").await?;
        let result: ArangoResult<HashMap<String, Permission>> = deserialize_response(resp.body())?;
        Ok(result.unwrap())
    }
}

impl<C: ClientExt> GenericConnection<C, Normal> {
    /// Establish connection to ArangoDB sever with Auth.
    ///
    /// The connection is establish in the following steps:
    /// 1. validate if it is a arangoDB server at the given base url
    /// 1. set authentication in header
    /// 1. build a http client that holds authentication tokens
    /// 1. construct databases objects for later use
    ///
    /// The most secure way to connect to a arangoDB server is via JWT
    /// token authentication, along with TLS encryption.
    #[maybe_async]
    async fn establish<T: Into<String>>(
        arango_url: T,
        auth: Auth<'_>,
    ) -> Result<GenericConnection<C, Normal>, ClientError> {
        let url = arango_url.into();
        let mut conn = GenericConnection {
            arango_url: Url::parse(&url)
                .expect(&format!("invaid url: {}", url))
                .join("/")
                .unwrap(),
            username: String::new(),
            session: Arc::new(C::new(None)?),
            state: Normal,
            phantom: (),
        };
        conn.validate_server().await?;

        let user: String;
        let authorization = match auth {
            Auth::Basic(cred) => {
                user = String::from(cred.username);

                let token = base64::encode(&format!("{}:{}", cred.username, cred.password));
                Some(format!("Basic {}", token))
            }
            Auth::Jwt(cred) => {
                user = String::from(cred.username);

                let token = conn.jwt_login(cred.username, cred.password).await?;
                Some(format!("Bearer {}", token))
            }
            Auth::None => {
                user = String::from("root");
                None
            }
        };

        let mut headers = HeaderMap::new();
        if let Some(value) = authorization {
            headers.insert(AUTHORIZATION, value.parse().unwrap());
        }

        conn.username = user;
        conn.session = Arc::new(C::new(headers)?);
        info!("Established");
        Ok(conn)
    }

    /// Establish connection to ArangoDB sever without Authentication.
    ///
    /// The target server **MUST DISABLE** authentication for all requests,
    /// which should only used for **test purpose**.
    ///
    /// Disable authentication means all operations are performed by root user.
    ///
    /// Example:
    /// ```rust, ignore
    /// use arangors::Connection;
    ///
    /// let conn = Connection::establish_without_auth("http://localhost:8529").await.unwrap();
    /// ```
    #[maybe_async]
    pub async fn establish_without_auth<T: Into<String>>(
        arango_url: T,
    ) -> Result<GenericConnection<C, Normal>, ClientError> {
        trace!("Establish without auth");
        GenericConnection::establish(arango_url.into(), Auth::None).await
    }

    /// Establish connection to ArangoDB sever with basic auth.
    ///
    /// Example:
    /// ```rust
    /// use arangors::Connection;
    ///
    /// # #[cfg_attr(any(feature="reqwest_async"), maybe_async::maybe_async, tokio::main)]
    /// # #[cfg_attr(any(feature="surf_async"), maybe_async::maybe_async, async_std::main)]
    /// # #[cfg_attr(feature="blocking", maybe_async::must_be_sync)]
    /// # async fn main() {
    /// let conn = Connection::establish_basic_auth("http://localhost:8529", "username", "password")
    ///     .await
    ///     .unwrap();
    /// # }
    /// ```
    #[maybe_async]
    pub async fn establish_basic_auth(
        arango_url: &str,
        username: &str,
        password: &str,
    ) -> Result<GenericConnection<C, Normal>, ClientError> {
        trace!("Establish with basic auth");
        GenericConnection::establish(arango_url, Auth::basic(username, password)).await
    }

    /// Establish connection to ArangoDB sever with jwt authentication.
    ///
    /// Prefered way to interact with arangoDB server.
    ///
    /// JWT token expires after 1 month.
    ///
    /// Example:
    ///
    /// ```rust
    /// use arangors::Connection;
    ///
    /// # #[cfg_attr(any(feature="reqwest_async"), maybe_async::maybe_async, tokio::main)]
    /// # #[cfg_attr(any(feature="surf_async"), maybe_async::maybe_async, async_std::main)]
    /// # #[cfg_attr(feature = "blocking", maybe_async::must_be_sync)]
    /// # async fn main() {
    /// let conn = Connection::establish_jwt("http://localhost:8529", "username", "password")
    ///     .await
    ///     .unwrap();
    /// # }
    /// ```
    #[maybe_async]
    pub async fn establish_jwt(
        arango_url: &str,
        username: &str,
        password: &str,
    ) -> Result<GenericConnection<C, Normal>, ClientError> {
        trace!("Establish with jwt");
        GenericConnection::establish(arango_url, Auth::jwt(username, password)).await
    }

    #[maybe_async]
    async fn jwt_login<T: Into<String>>(
        &self,
        username: T,
        password: T,
    ) -> Result<String, ClientError> {
        #[derive(Deserialize)]
        struct JWT {
            pub jwt: String,
        }
        let url = self.arango_url.join("/_open/auth").unwrap();

        let mut map = HashMap::new();
        map.insert("username", username.into());
        map.insert("password", password.into());

        let jwt: JWT = serde_json::from_str(
            self.session
                .post(url, &serde_json::to_string(&map)?)
                .await?
                .body(),
        )
        .unwrap();
        Ok(jwt.jwt)
    }

    /// Create a database via HTTP request and add it into `self.databases`.
    ///
    /// If creation fails, an Error is cast. Otherwise, a bool is returned to
    /// indicate whether the database is correctly created.
    ///
    /// # Example
    /// ```rust
    /// use arangors::Connection;
    /// # #[cfg_attr(any(feature="reqwest_async"), maybe_async::maybe_async, tokio::main)]
    /// # #[cfg_attr(any(feature="surf_async"), maybe_async::maybe_async, async_std::main)]
    /// # #[cfg_attr(feature = "blocking", maybe_async::must_be_sync)]
    /// # async fn main() {
    /// let conn = Connection::establish_jwt("http://localhost:8529", "root", "KWNngteTps7XjrNv")
    ///     .await
    ///     .unwrap();
    /// let result = conn.create_database("new_db").await.unwrap();
    /// println!("{:?}", result);
    ///
    /// let result = conn.drop_database("new_db").await.unwrap();
    /// println!("{:?}", result);
    /// # }
    /// ```
    /// TODO tweak options on creating database
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn create_database(&self, name: &str) -> Result<Database<'_, C>, ClientError> {
        let mut map = HashMap::new();
        map.insert("name", name);
        let url = self.arango_url.join("/_api/database").unwrap();

        let resp = self
            .session
            .post(url, &serde_json::to_string(&map)?)
            .await?;

        deserialize_response::<ArangoResult<bool>>(resp.body())?;
        self.db(name).await
    }

    /// Drop database with name.
    ///
    /// # Note
    /// this function would make a request to arango server.
    #[maybe_async]
    pub async fn drop_database(&self, name: &str) -> Result<(), ClientError> {
        let url_path = format!("/_api/database/{}", name);
        let url = self.arango_url.join(&url_path).unwrap();

        let resp = self.session.delete(url, "").await?;
        deserialize_response::<ArangoResult<bool>>(resp.body())?;
        Ok(())
    }

    #[maybe_async]
    pub async fn into_admin(self) -> Result<GenericConnection<C, Admin>, ClientError> {
        let dbs = self.accessible_databases().await?;
        let db = dbs
            .get("_system")
            .ok_or(ClientError::InsufficientPermission {
                permission: Permission::NoAccess,
                operation: String::from("access to _system database"),
            })?;
        match db {
            Permission::ReadWrite => Ok(self.into()),
            _ => Err(ClientError::InsufficientPermission {
                permission: Permission::ReadOnly,
                operation: String::from("write to _system database"),
            }),
        }
    }
}

impl<C: ClientExt> GenericConnection<C, Admin> {
    pub fn into_normal(self) -> GenericConnection<C, Normal> {
        self.into()
    }
}

impl<C: ClientExt> From<GenericConnection<C, Normal>> for GenericConnection<C, Admin> {
    fn from(conn: GenericConnection<C, Normal>) -> GenericConnection<C, Admin> {
        GenericConnection {
            arango_url: conn.arango_url,
            session: conn.session,
            username: conn.username,
            state: Admin,
            phantom: (),
        }
    }
}

impl<C: ClientExt> From<GenericConnection<C, Admin>> for GenericConnection<C, Normal> {
    fn from(conn: GenericConnection<C, Admin>) -> GenericConnection<C, Normal> {
        GenericConnection {
            arango_url: conn.arango_url,
            session: conn.session,
            username: conn.username,
            state: Normal,
            phantom: (),
        }
    }
}