b2creds 0.1.0

Library for accessing b2 credentials
Documentation
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
use thiserror::Error;

use std::{io, path::Path, path::PathBuf};

type Result<T> = std::result::Result<T, CredentialsError>;

/// Error enum for crate functions. Used for all `Result` returns as the error
/// enum.
#[derive(Debug, Error)]
pub enum CredentialsError {
    /// Describes any errors from std::io::Error
    #[error("Failed to read credentials file")]
    Io(#[from] io::Error),

    /// Describes any errors from rusqlite
    #[error("Failed to parse sqlite file")]
    SqlLite(#[from] rusqlite::Error),

    /// Describes any errors for parsing environmental variables
    #[error("Failed to parse env vars")]
    Env(#[from] std::env::VarError),

    /// Set when no credentials exist
    #[error("No credentials exist")]
    NoCreds,

    /// Set when it's impossible to find your base directory
    #[error("No base directory on this OS. Unable to find default b2 accounts")]
    NoBaseDirs,
}

const KEY_ENV_VAR_NAME: &str = "B2_APPLICATION_KEY";
const KEY_ID_ENV_VAR_NAME: &str = "B2_APPLICATION_KEY_ID";

/// Holds the application key id and application key which make up your
/// credentials
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Credentials {
    /// The application key id
    pub application_key_id: String,

    /// The application key
    pub application_key: String,
}

impl Credentials {
    /// Returns the default credentials for b2. This function will search for b2
    /// credentials in the following order:
    ///
    /// 1. In the B2_APPLICATION_KEY and B2_APPLICATION_KEY_ID environmentals
    ///    variables
    ///
    /// 2. In the sqlite database pointed to by the environmental variable
    ///    B2_ACCOUNT_INFO
    ///
    /// 3. In the default sqlite database ~/.b2_account_info
    ///
    /// If any of those searches run into an unexpected error, that error is
    /// returned. Otherwise `CredentialsError::NoCreds` is returned.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let creds = b2creds::Credentials::default().unwrap();
    /// println!("Key ID: {} Key: {}", creds.application_key_id, creds.application_key);
    /// ```
    pub fn default() -> Result<Self> {
        match Self::from_env() {
            Ok(res) => Ok(res),
            Err(_) => Self::from_file(None, None),
        }
    }

    /// Attempts to extract b2 credentials from environmental variables.
    /// Specifically, it will search the B2_APPLICATION_KEY and
    /// B2_APPLICATION_KEY_ID environmentals variables.
    ///
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let creds = b2creds::Credentials::from_env().unwrap();
    /// println!("Key ID: {} Key: {}", creds.application_key_id, creds.application_key);
    /// ```
    pub fn from_env() -> Result<Self> {
        let key = match std::env::var(KEY_ENV_VAR_NAME) {
            Ok(value) => value,
            Err(e) => match e {
                std::env::VarError::NotPresent => return Err(CredentialsError::NoCreds),
                _ => return Err(CredentialsError::Env(e)),
            },
        };
        let key_id = match std::env::var(KEY_ID_ENV_VAR_NAME) {
            Ok(value) => value,
            Err(e) => match e {
                std::env::VarError::NotPresent => return Err(CredentialsError::NoCreds),
                _ => return Err(CredentialsError::Env(e)),
            },
        };

        Ok(Self {
            application_key_id: key_id,
            application_key: key,
        })
    }

    /// Attempts to extract b2 credentials from a b2 account info file. The path
    /// to this file maybe specified via the `db_path` argument. If that argument
    /// is None, the path set in the env variable B2_ACCOUNT_INFO is used, and if
    /// that environmental variable is not set, the path searched defaults to
    /// ~/.b2_account_info.
    ///
    /// The account info file may have multiple accounts stored inside. By
    /// default, the first account is chosen by users may specified by setting
    /// the `account_id` argument.
    ///
    /// # Arguments
    ///
    /// * `db_path` - The (optional) path to the credentials. If not set, it
    ///               defaults to the B2_ACCOUNT_INFO env variable and then ~/.b2_account_info.
    ///
    /// * `account_id` - The ID of the account whose credentials we are querying
    ///                  for. If None, the first account is used.
    ///
    ///
    /// # Examples
    ///
    /// ```no_run
    /// let creds = b2creds::Credentials::from_file(None, None).unwrap();
    /// println!("Key ID: {} Key: {}", creds.application_key_id, creds.application_key);
    /// ```
    pub fn from_file(db_path: Option<&Path>, account_id: Option<&str>) -> Result<Self> {
        let db_path = if let Some(path) = db_path {
            path.to_path_buf()
        } else if let Ok(env_path) = std::env::var("B2_ACCOUNT_INFO") {
            PathBuf::from(env_path)
        } else {
            default_creds_file()?
        };
        Self::from_file_internal(&db_path, account_id)
    }

    fn from_file_internal(db_path: &std::path::Path, account_id: Option<&str>) -> Result<Self> {
        if !db_path.exists() {
            return Err(CredentialsError::NoCreds);
        }

        let conn = rusqlite::Connection::open_with_flags(
            db_path,
            rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY,
        )?;

        let mut query = String::from(
            "SELECT account_id, application_key, account_id_or_app_key_id FROM account",
        );
        if let Some(account_id) = account_id {
            query = format!("{} WHERE account_id = \"{}\"", query, account_id);
        }

        let mut stmt = conn.prepare(&query)?;

        let creds_iter = stmt.query_map(rusqlite::NO_PARAMS, |row| {
            Ok(Credentials {
                application_key_id: row.get(2).unwrap(),
                application_key: row.get(1).unwrap(),
            })
        })?;

        let mut creds_iter = creds_iter.filter_map(std::result::Result::ok);

        if let Some(cred) = creds_iter.next() {
            Ok(cred)
        } else {
            Err(CredentialsError::NoCreds)
        }
    }
}

/// Returns the default credentials file path.
/// ```
/// let cred_path = b2creds::default_creds_file().unwrap();
/// println!("B2 Creds Path: {}", cred_path.display());
/// ```
pub fn default_creds_file() -> Result<PathBuf> {
    let home_dir = directories::BaseDirs::new().ok_or(CredentialsError::NoBaseDirs)?;
    Ok(PathBuf::from(home_dir.home_dir()).join(".b2_account_info"))
}

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

    #[cfg(target_os = "linux")]
    #[test]
    fn default_path_is_expected() -> Result<()> {
        let cred_path = default_creds_file()?;
        let home = std::env::var("HOME")?;
        let expected_path = PathBuf::from(home).join(".b2_account_info");
        #[cfg(target_os = "linux")]
        assert_eq!(cred_path, expected_path);
        Ok(())
    }

    #[test]
    fn from_env_fails_with_no_key_or_key_id() {
        clear_env();
        assert!(matches!(
            Credentials::from_env().unwrap_err(),
            CredentialsError::NoCreds
        ));
    }

    #[test]
    fn from_env_fails_with_no_key() {
        clear_env();
        std::env::set_var(KEY_ID_ENV_VAR_NAME, "keyid");
        assert!(matches!(
            Credentials::from_env().unwrap_err(),
            CredentialsError::NoCreds
        ));
    }
    #[test]
    fn from_env_fails_with_no_key_id() {
        clear_env();
        std::env::set_var(KEY_ENV_VAR_NAME, "key");
        assert!(matches!(
            Credentials::from_env().unwrap_err(),
            CredentialsError::NoCreds
        ));
    }

    #[test]
    fn from_env_works() -> Result<()> {
        clear_env();

        let key_id = "keyid";
        let key = "key";
        std::env::set_var(KEY_ENV_VAR_NAME, key);
        std::env::set_var(KEY_ID_ENV_VAR_NAME, key_id);

        let creds = Credentials::from_env()?;
        assert_eq!(creds.application_key, key);
        assert_eq!(creds.application_key_id, key_id);
        Ok(())
    }

    #[test]
    fn non_existant_path_fails() {
        clear_env();

        let bad_path = PathBuf::from("asgasgasldghuaskdjgkkajsjuugasdgasg");
        let creds = Credentials::from_file(Some(&bad_path), None);
        assert!(matches!(creds.unwrap_err(), CredentialsError::NoCreds));
    }

    #[test]
    fn non_sqlite_path_fails() -> Result<()> {
        clear_env();

        let file = tempfile::NamedTempFile::new()?;
        let creds = Credentials::from_file(Some(file.path()), None);
        assert!(matches!(creds.unwrap_err(), CredentialsError::SqlLite(_)));

        Ok(())
    }

    #[test]
    fn invalid_sqlite_db_fails() -> Result<()> {
        clear_env();

        let file = tempfile::NamedTempFile::new()?;

        let conn = rusqlite::Connection::open(file.path())?;

        conn.execute(
            "CREATE TABLE person (
                    id              INTEGER PRIMARY KEY,
                    name            TEXT NOT NULL,
                    data            BLOB
                    )",
            rusqlite::params![],
        )?;
        conn.execute(
            "INSERT INTO person (name, data) VALUES (?1, ?2)",
            rusqlite::params!["Matt".to_string(), 0],
        )?;
        conn.flush_prepared_statement_cache();
        conn.close().unwrap();

        let creds = Credentials::from_file(Some(file.path()), None);
        assert!(matches!(creds.unwrap_err(), CredentialsError::SqlLite(_)));

        Ok(())
    }

    #[test]
    fn valid_sqlite_db_works() -> Result<()> {
        clear_env();

        let account_id = "123";
        let key = "key";
        let key_id = "key_id";

        let file = tempfile::NamedTempFile::new()?;

        let conn = rusqlite::Connection::open(file.path())?;

        conn.execute(
            "CREATE TABLE account (
                    account_id TEXT NOT NULL,
                    application_key TEXT NOT NULL,
                    account_id_or_app_key_id TEXT
                    )",
            rusqlite::params![],
        )?;

        conn.execute(
            "INSERT INTO account (account_id, application_key, account_id_or_app_key_id) VALUES (?1, ?2, ?3)",
            rusqlite::params![account_id, key, key_id],
        )?;
        conn.flush_prepared_statement_cache();
        conn.close().unwrap();

        let creds = Credentials::from_file(Some(file.path()), None);
        assert!(matches!(creds, Ok(_)));
        let creds = creds.unwrap();
        assert_eq!(creds.application_key, key);
        assert_eq!(creds.application_key_id, key_id);

        Ok(())
    }

    #[test]
    fn empty_table_fails() -> Result<()> {
        clear_env();

        let file = tempfile::NamedTempFile::new()?;

        let conn = rusqlite::Connection::open(file.path())?;

        conn.execute(
            "CREATE TABLE account (
                    account_id TEXT NOT NULL,
                    application_key TEXT NOT NULL,
                    account_id_or_app_key_id TEXT
                    )",
            rusqlite::params![],
        )?;
        conn.flush_prepared_statement_cache();
        conn.close().unwrap();

        let creds = Credentials::from_file(Some(file.path()), None);
        assert!(matches!(creds.unwrap_err(), CredentialsError::NoCreds));

        Ok(())
    }

    #[test]
    fn account_id_works() -> Result<()> {
        clear_env();

        let account1_id = "123";
        let account1_key = "key";
        let account1_key_id = "key_id";

        let account2_id = "456";
        let account2_key = "yek";
        let account2_key_id = "id_key";

        let file = tempfile::NamedTempFile::new()?;

        let conn = rusqlite::Connection::open(file.path())?;

        conn.execute(
            "CREATE TABLE account (
                    account_id TEXT NOT NULL,
                    application_key TEXT NOT NULL,
                    account_id_or_app_key_id TEXT
                    )",
            rusqlite::params![],
        )?;

        conn.execute(
            "INSERT INTO account (account_id, application_key, account_id_or_app_key_id) VALUES (?1, ?2, ?3)",
            rusqlite::params![account1_id, account1_key, account1_key_id],
        )?;
        conn.execute(
            "INSERT INTO account (account_id, application_key, account_id_or_app_key_id) VALUES (?1, ?2, ?3)",
            rusqlite::params![account2_id, account2_key, account2_key_id],
        )?;
        conn.flush_prepared_statement_cache();
        conn.close().unwrap();

        let creds = Credentials::from_file(Some(file.path()), Some(account1_id));
        assert!(matches!(creds, Ok(_)));
        let creds = creds.unwrap();
        assert_eq!(creds.application_key, account1_key);
        assert_eq!(creds.application_key_id, account1_key_id);

        let creds = Credentials::from_file(Some(file.path()), Some(account2_id));
        assert!(matches!(creds, Ok(_)));
        let creds = creds.unwrap();
        assert_eq!(creds.application_key, account2_key);
        assert_eq!(creds.application_key_id, account2_key_id);

        let creds = Credentials::from_file(Some(file.path()), Some("DNE"));
        assert!(matches!(creds.unwrap_err(), CredentialsError::NoCreds));

        Ok(())
    }

    fn clear_env() {
        std::env::remove_var(KEY_ID_ENV_VAR_NAME);
        std::env::remove_var(KEY_ENV_VAR_NAME);
    }
}