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

#[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,

    /// Not Implemented
    NotImplemented
}

impl Error for GoogleCloudStorerError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match *self {
            GoogleCloudStorerError::InternalError { ref source } => Some(source.as_ref()),
            GoogleCloudStorerError::NotFound => None,
            GoogleCloudStorerError::NotImplemented => 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")
            }
            GoogleCloudStorerError::NotImplemented => {
                write!(f, "This method is not implemented")
            }
        }
    }
}

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),
            },
            GoogleCloudStorerError::NotImplemented => CryptoError::NotImplemented {},
        }
    }
}

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

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

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

#[async_trait]
impl Storer for GoogleCloudStorer {
    async fn get_indexed<T: StorableType>(
        &self,
        path: &str,
        _index: &Option<Document>,
    ) -> 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 list_indexed<T: StorableType>(
        &self,
        _path: &str,
        _skip: u64,
        _page_size: i64,
        _index: &Option<Document>,
    ) -> Result<Vec<Entry<T>>, CryptoError> {
        Err(GoogleCloudStorerError::NotImplemented {}.into())
    }

    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()),
        }
    }
}