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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! Vault Client
//!
//! This is a thin wrapper around the HTTP API for [Vault](https://www.vaultproject.io/).
#![allow(
    missing_copy_implementations,
    missing_debug_implementations,
    unknown_lints
)]
#![deny(
    const_err,
    dead_code,
    deprecated,
    exceeding_bitshifts,
    improper_ctypes,
    missing_docs,
    mutable_transmutes,
    no_mangle_const_items,
    non_camel_case_types,
    non_shorthand_field_patterns,
    non_upper_case_globals,
    overflowing_literals,
    path_statements,
    stable_features,
    trivial_casts,
    trivial_numeric_casts,
    unconditional_recursion,
    unknown_crate_types,
    unreachable_code,
    unused_allocation,
    unused_assignments,
    unused_attributes,
    unused_comparisons,
    unused_extern_crates,
    unused_features,
    unused_imports,
    unused_import_braces,
    unused_qualifications,
    unused_must_use,
    unused_mut,
    unused_parens,
    unused_results,
    unused_unsafe,
    unused_variables,
    variant_size_differences,
    warnings,
    while_true
)]
#![doc(test(attr(allow(unused_variables), deny(warnings))))]

mod error;
mod utils;

pub mod secrets;
pub mod sys;

pub use error::Error;
pub use reqwest::Method;

use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::fs::File;
use std::io::Read;
use std::ops::Deref;

use async_trait::async_trait;
use log::{debug, info, warn};
use reqwest::{Certificate, Client as HttpClient, ClientBuilder};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

/// A wrapper around a String with custom implementation of Display and Debug to not leak
/// secrets during logging.
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct Secret(pub String);

impl Deref for Secret {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl Debug for Secret {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "***")
    }
}

impl fmt::Display for Secret {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "***")
    }
}

impl AsRef<str> for Secret {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<String> for Secret {
    fn from(s: String) -> Self {
        Secret(s)
    }
}

/// Vault API Client
#[derive(Clone, Debug)]
pub struct Client {
    token: Secret,
    address: String,
    client: HttpClient,
    revoke_self_on_drop: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub(crate) struct Empty;

/// Generic Vault Response
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(untagged)]
#[allow(clippy::large_enum_variant, variant_size_differences)]
pub enum Response {
    /// An error response
    Error {
        /// List of errors returned from Vault
        errors: Vec<String>,
    },
    /// A successful response
    Response(ResponseData),
    /// An Empty (succesful) Response. Usually returned from `DELETE` operations
    Empty,
}

/// Vault Generic Response Data
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct ResponseData {
    /// Request UUID
    pub request_id: String,
    /// Lease ID for secrets
    pub lease_id: String,
    /// Renewable for secrets
    pub renewable: bool,
    /// Lease duration for secrets
    pub lease_duration: u64,
    /// Warnings, if any
    #[serde(default)]
    pub warnings: Option<Vec<String>>,

    /// Auth data for authentication requests
    #[serde(default)]
    pub auth: Option<Authentication>,

    /// Data for secrets requests
    #[serde(default)]
    pub data: Option<serde_json::Value>,
    // Missing and ignored fields:
    // - wrap_info
}

/// Wrapped Vault Secret with Lease Data
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct LeasedData<T> {
    /// Lease ID for secrets
    pub lease_id: String,
    /// Renewable for secrets
    pub renewable: bool,
    /// Lease duration for secrets
    pub lease_duration: u64,
    /// Wrapped Data
    pub data: T,
}

impl<T> LeasedData<T> {
    /// Unwrap and discard the lease data, returning the wrapped data
    pub fn unwrap(self) -> T {
        self.data
    }
}

/// Authentication data from Vault
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct Authentication {
    /// The actual token
    pub client_token: Secret,
    /// The accessor for the Token
    pub accessor: String,
    /// List of policies for token, including from Identity
    pub policies: Vec<String>,
    /// List of tokens directly assigned to token
    pub token_policies: Vec<String>,
    /// Arbitrary metadata
    pub metadata: HashMap<String, String>,
    /// Lease Duration for the token
    pub lease_duration: u64,
    /// Whether the token is renewable
    pub renewable: bool,
    /// UUID for the entity
    pub entity_id: String,
    /// Type of token
    pub token_type: TokenType,
}

/// Type of token from Vault
/// See [Vault Documentation](https://www.vaultproject.io/docs/concepts/tokens.html#token-types-in-detail)
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
#[serde(rename_all = "lowercase")]
pub enum TokenType {
    /// Long lived service tokens
    Service,
    /// Short lived batch tokens
    Batch,
}

/// Trait implementing the basic API operations for Vault
#[async_trait]
pub trait Vault {
    /// Read a generic Path from Vault
    async fn read(&self, path: &str, method: Method) -> Result<Response, Error>;

    /// Read a generic Path from Vault with Query
    async fn read_with_query<T: Serialize + Send + Sync + ?Sized>(
        &self,
        path: &str,
        method: Method,
        query: &T,
    ) -> Result<Response, Error>;

    /// Write to a generic Path in Vault.
    async fn write<T: Serialize + Send + Sync>(
        &self,
        path: &str,
        payload: &T,
        method: Method,
        response_expected: bool,
    ) -> Result<Response, Error>;

    /// Convenience method to Get a generic path from Vault
    async fn get(&self, path: &str) -> Result<Response, Error> {
        self.read(path, Method::GET).await
    }

    /// Convenience method to Get a generic path from VaultResult
    async fn get_with_query<T: Serialize + Send + Sync + ?Sized>(
        &self,
        path: &str,
        query: &T,
    ) -> Result<Response, Error> {
        self.read_with_query(path, Method::GET, query).await
    }

    /// Convenience method to List a generic path from Vault
    async fn list(&self, path: &str) -> Result<Response, Error> {
        self.read(path, Method::from_bytes(b"LIST").expect("Not to fail"))
            .await
    }

    /// Convenience method to Post to a generic path to Vault
    async fn post<T: Serialize + Send + Sync>(
        &self,
        path: &str,
        payload: &T,
        response_expected: bool,
    ) -> Result<Response, Error> {
        self.write(path, payload, Method::POST, response_expected)
            .await
    }

    /// Convenience method to Put to a generic path to Vault
    async fn put<T: Serialize + Send + Sync>(
        &self,
        path: &str,
        payload: &T,
        response_expected: bool,
    ) -> Result<Response, Error> {
        self.write(path, payload, Method::PUT, response_expected)
            .await
    }

    /// Convenience method to Delete a Path from Vault
    async fn delete(&self, path: &str, response_expected: bool) -> Result<Response, Error> {
        self.write(path, &Empty, Method::DELETE, response_expected)
            .await
    }
}

impl Client {
    fn internal_new<S1, S2>(
        vault_address: S1,
        vault_token: S2,
        revoke_self_on_drop: bool,
        client: Option<HttpClient>,
    ) -> Result<Self, Error>
    where
        S1: AsRef<str>,
        S2: AsRef<str>,
    {
        let client = match client {
            Some(client) => client,
            None => ClientBuilder::new().build()?,
        };

        Ok(Self {
            address: vault_address.as_ref().to_string(),
            token: Secret(vault_token.as_ref().to_string()),
            revoke_self_on_drop,
            client,
        })
    }

    /// Create a new Vault Client.
    ///
    /// You can optionally provide a `reqwest::Client` if you have specific needs like custom root
    /// CA certificate or require client authentication
    #[allow(clippy::new_ret_no_self)]
    pub fn new<S1, S2, S3>(
        vault_address: Option<S1>,
        vault_token: Option<S2>,
        root_ca: Option<S3>,
        revoke_self_on_drop: bool,
    ) -> Result<Self, Error>
    where
        S1: AsRef<str>,
        S2: AsRef<str>,
        S3: AsRef<str>,
    {
        let mut client = Self::from_environment(vault_address, vault_token, root_ca)?;
        client.revoke_self_on_drop = revoke_self_on_drop;
        Ok(client)
    }

    /// Create a client from environment variables. You can provide alternative sources of
    /// the parameters with the optional arguments
    ///
    /// The vnrionment variables are:
    ///
    /// - `VAULT_ADDR`: Vault Address
    /// - `VAULT_TOKEN`: Vault Token
    /// - `VAULT_CACERT`: Path to the CA Certificate for Vault
    pub fn from_environment<S1, S2, S3>(
        address: Option<S1>,
        token: Option<S2>,
        ca_cert: Option<S3>,
    ) -> Result<Self, Error>
    where
        S1: AsRef<str>,
        S2: AsRef<str>,
        S3: AsRef<str>,
    {
        let address = Self::environment_variable_or_provided("VAULT_ADDR", address)
            .ok_or_else(|| Error::MissingAddress)?;
        let token = Self::environment_variable_or_provided("VAULT_TOKEN", token)
            .ok_or_else(|| Error::MissingToken)?;
        let root_ca = Self::environment_variable_or_provided("VAULT_CACERT", ca_cert);

        let client = if let Some(cert) = root_ca {
            let cert = Certificate::from_pem(&read_file(cert)?)?;

            Some(ClientBuilder::new().add_root_certificate(cert).build()?)
        } else {
            None
        };

        // TODOs
        // VAULT_CLIENT_CERT
        // VAULT_CLIENT_KEY
        // VAULT_TLS_SERVER_NAME
        Self::internal_new(&address, &token, false, client)
    }

    fn environment_variable_or_provided<S>(
        env: &'static str,
        alternative: Option<S>,
    ) -> Option<String>
    where
        S: AsRef<str>,
    {
        alternative
            .map(|s| s.as_ref().to_string())
            .or_else(|| std::env::var(env).ok())
    }

    /// Returns the Vault address
    pub fn address(&self) -> &str {
        &self.address
    }

    async fn execute_request<T>(client: &HttpClient, request: reqwest::Request) -> Result<T, Error>
    where
        T: DeserializeOwned + Debug,
    {
        debug!("Executing request: {:#?}", request);
        let response = client.execute(request).await?;
        debug!("Response received: {:#?}", response);
        let body = response.text().await?;
        debug!("Response body: {}", body);
        let result = serde_json::from_str(&body)?;
        debug!("Deserialized body: {:#?}", result);
        Ok(result)
    }

    async fn execute_request_no_body(
        client: &HttpClient,
        request: reqwest::Request,
    ) -> Result<(), Error> {
        debug!("Executing request: {:#?}", request);
        let response = client.execute(request).await?;
        debug!("Response received: {:#?}", response);
        let body = response.text().await?;
        if !body.is_empty() {
            return Err(Error::UnexpectedResponse(body));
        }
        Ok(())
    }

    fn build_request<S: AsRef<str>>(
        &self,
        path: S,
        method: Method,
    ) -> Result<reqwest::RequestBuilder, Error> {
        let vault_address = url::Url::parse(self.address())?;
        let vault_address = vault_address.join(&format!("/v1/{}", path.as_ref()))?;

        Ok(self
            .client
            .request(method, vault_address)
            .header("X-Vault-Token", self.token.as_str()))
    }

    /// Revoke the Vault token itself
    ///
    /// If successful, the Vault Token can no longer be used
    pub async fn revoke_self(&self) -> Result<(), Error> {
        info!("Revoking self Vault Token");

        let request = self.build_revoke_self_request()?;
        // HTTP 204 is returned
        Self::execute_request_no_body(&self.client, request).await?;
        Ok(())
    }

    fn build_revoke_self_request(&self) -> Result<reqwest::Request, Error> {
        let vault_address = url::Url::parse(self.address())?;
        let vault_address = vault_address.join("/v1/auth/token/revoke-self")?;

        Ok(self
            .client
            .post(vault_address)
            .header("X-Vault-Token", self.token.as_str())
            .build()?)
    }
}

#[async_trait]
#[allow(clippy::trivially_copy_pass_by_ref)]
impl<T> Vault for &T
where
    T: Vault + Send + Sync,
{
    async fn read(&self, path: &str, method: Method) -> Result<Response, Error> {
        T::read(&self, path, method).await
    }

    async fn read_with_query<Q: Serialize + Send + Sync + ?Sized>(
        &self,
        path: &str,
        method: Method,
        query: &Q,
    ) -> Result<Response, Error> {
        T::read_with_query(&self, path, method, query).await
    }

    async fn write<P: Serialize + Send + Sync>(
        &self,
        path: &str,
        payload: &P,
        method: Method,
        response_expected: bool,
    ) -> Result<Response, Error> {
        T::write(&self, path, payload, method, response_expected).await
    }
}

#[async_trait]
impl Vault for Client {
    async fn read(&self, path: &str, method: Method) -> Result<Response, Error> {
        let request = self.build_request(path, method)?.build()?;

        Self::execute_request(&self.client, request).await
    }

    async fn read_with_query<T: Serialize + Send + Sync + ?Sized>(
        &self,
        path: &str,
        method: Method,
        query: &T,
    ) -> Result<Response, Error> {
        let request = self.build_request(path, method)?.query(&query).build()?;
        Self::execute_request(&self.client, request).await
    }

    async fn write<T: Serialize + Send + Sync>(
        &self,
        path: &str,
        payload: &T,
        method: Method,
        response_expected: bool,
    ) -> Result<Response, Error> {
        let request = self.build_request(path, method)?.json(payload).build()?;
        if response_expected {
            Self::execute_request(&self.client, request).await
        } else {
            Self::execute_request_no_body(&self.client, request)
                .await
                .map(|_| Response::Empty)
        }
    }
}

impl Drop for Client {
    fn drop(&mut self) {
        if self.revoke_self_on_drop {
            info!("Vault Client is being dropped. Revoking its own Token");
            match futures::executor::block_on(self.revoke_self()) {
                Ok(()) => {}
                Err(e) => warn!("Error revoking self: {}", e),
            }
        }
    }
}

impl Response {
    /// Transform the Response into `Result`, where any successful response is `Ok`,
    /// and an error from Vault is an `Err`.
    pub fn ok(self) -> Result<Option<ResponseData>, Error> {
        match self {
            Response::Error { errors } => Err(Error::VaultError(errors.join("; "))),
            Response::Response(data) => Ok(Some(data)),
            Response::Empty => Ok(None),
        }
    }
    /// Turns a Response into `Result`, where a response with data is `Ok`, and anything else
    /// is an `Err`
    pub fn data_value(&self) -> Result<&serde_json::Value, Error> {
        match self {
            Response::Error { errors } => Err(Error::VaultError(errors.join("; "))),
            Response::Empty => Err(Error::MissingData(Box::new(self.clone()))),
            Response::Response(data) => match &data.data {
                None => Err(Error::MissingData(Box::new(self.clone()))),
                Some(data) => Ok(data),
            },
        }
    }

    /// Decode the response into the appropriate data type
    pub fn data<T: DeserializeOwned>(&self) -> Result<T, Error> {
        match self {
            Response::Error { errors } => Err(Error::VaultError(errors.join("; "))),
            Response::Empty => Err(Error::MissingData(Box::new(self.clone()))),
            Response::Response(response_data) => match &response_data.data {
                None => Err(Error::MissingData(Box::new(self.clone()))),
                Some(data) => Ok(serde_json::from_value(data.clone())?),
            },
        }
    }

    /// Decode the response into the appropriate data type along with lease data
    pub fn leased_data<T: DeserializeOwned>(&self) -> Result<LeasedData<T>, Error> {
        match self {
            Response::Error { errors } => Err(Error::VaultError(errors.join("; "))),
            Response::Empty => Err(Error::MissingData(Box::new(self.clone()))),
            Response::Response(response_data) => match &response_data.data {
                None => Err(Error::MissingData(Box::new(self.clone()))),
                Some(data) => {
                    let deserialized = serde_json::from_value(data.clone())?;
                    Ok(LeasedData {
                        lease_id: response_data.lease_id.clone(),
                        renewable: response_data.renewable,
                        lease_duration: response_data.lease_duration,
                        data: deserialized,
                    })
                }
            },
        }
    }
}

fn read_file<P: AsRef<std::path::Path>>(path: P) -> Result<Vec<u8>, Error> {
    let metadata = std::fs::metadata(&path)?;
    let size = metadata.len();
    let mut file = File::open(&path)?;
    let mut buffer = Vec::with_capacity(size as usize);
    let _ = file.read_to_end(&mut buffer)?;
    Ok(buffer)
}

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

    pub(crate) fn vault_client() -> Client {
        Client::from_environment::<_, &str, &str>(
            Some("http://127.0.0.1:8200"),
            Some("12345"),
            None,
        )
        .unwrap()
    }

    pub(crate) fn uuid() -> String {
        uuid::Uuid::new_v4().to_simple().to_string()
    }

    pub(crate) fn uuid_prefix(prefix: &str) -> String {
        format!("{}-{}", prefix, uuid::Uuid::new_v4().to_simple())
    }

    #[tokio::test]
    async fn can_read_self_capabilities() {
        let client = vault_client();
        let _ = client.get("/auth/token/lookup-self").await.unwrap();
    }

    #[tokio::test]
    async fn can_list_kv() {
        let client = vault_client();
        let _ = client.list("secrets").await.unwrap();
    }
}