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
use crate::{CryptoError, Entry, NonIndexedTypeStorer, StorableType, Storer};
use async_trait::async_trait;
use cloud_storage::Client;
use cloud_storage::Error::Other;
use serde::{Deserialize, Serialize};
use std::{
    error::Error,
    fmt::{self, Display, Formatter},
};

#[derive(Debug)]
pub enum GoogleCloudStorerError {
    /// Represents an error which occurred in some internal system
    InternalError {
        source: Box<dyn Error + Send + Sync>,
    },

    /// Requested document was not found
    NotFound,
}

impl Error for GoogleCloudStorerError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            GoogleCloudStorerError::InternalError { ref source } => Some(source.as_ref()),
            GoogleCloudStorerError::NotFound => None,
        }
    }
}

impl Display for GoogleCloudStorerError {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match *self {
            GoogleCloudStorerError::InternalError { .. } => {
                write!(f, "Internal error occurred")
            }
            GoogleCloudStorerError::NotFound => {
                write!(f, "Requested document not found")
            }
        }
    }
}

impl From<GoogleCloudStorerError> for CryptoError {
    fn from(gcse: GoogleCloudStorerError) -> Self {
        match gcse {
            GoogleCloudStorerError::InternalError { .. } => CryptoError::InternalError {
                source: Box::new(gcse),
            },
            GoogleCloudStorerError::NotFound => CryptoError::NotFound {
                source: Box::new(gcse),
            },
        }
    }
}

/// Stores an instance of a mongodb-backed key storer
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct GoogleCloudStorer {
    bucket_name: String,
}

impl From<GoogleCloudStorer> for NonIndexedTypeStorer {
    fn from(gcs: GoogleCloudStorer) -> Self {
        NonIndexedTypeStorer::GoogleCloud(gcs)
    }
}

impl GoogleCloudStorer {
    pub fn new(bucket_name: String) -> Self {
        return GoogleCloudStorer { bucket_name };
    }
}

#[async_trait]
impl Storer for GoogleCloudStorer {
    async fn get<T: StorableType>(&self, path: &str) -> Result<Entry<T>, CryptoError> {
        let client = Client::new();
        let bytes = client
            .object()
            .download(&self.bucket_name, path)
            .await
            .map_err(|e| match e {
                Other(_) => GoogleCloudStorerError::NotFound {}.into(),
                _ => GoogleCloudStorerError::InternalError {
                    source: Box::new(e),
                },
            })?;

        let s = String::from_utf8(bytes).map_err(|e| GoogleCloudStorerError::InternalError {
            source: Box::new(e),
        })?;

        Ok(
            serde_json::from_str(&s).map_err(|e| GoogleCloudStorerError::InternalError {
                source: Box::new(e),
            })?,
        )
    }

    async fn create<T: StorableType>(&self, entry: Entry<T>) -> Result<Entry<T>, CryptoError> {
        let entry_string =
            serde_json::to_string(&entry).map_err(|e| GoogleCloudStorerError::InternalError {
                source: Box::new(e),
            })?;
        let client = Client::new();

        match client
            .object()
            .create(
                &self.bucket_name,
                entry_string.as_bytes().to_vec(),
                &entry.path.clone(),
                "application/json",
            )
            .await
        {
            Ok(_) => Ok(entry),
            Err(e) => Err(GoogleCloudStorerError::InternalError {
                source: Box::new(e),
            }
            .into()),
        }
    }
}