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
use crate::{
error::GoogleResponse,
hmac_key::{HmacKey, HmacMeta, HmacState},
};
/// Operations on [`HmacKey`](HmacKey)s.
#[derive(Debug)]
pub struct HmacKeyClient<'a>(pub(super) &'a super::Client);
impl<'a> HmacKeyClient<'a> {
/// Creates a new HMAC key for the specified service account.
///
/// The authenticated user must have `storage.hmacKeys.create` permission for the project in
/// which the key will be created.
///
/// For general information about HMAC keys in Cloud Storage, see
/// [HMAC Keys](https://cloud.google.com/storage/docs/authentication/hmackeys).
/// ### Example
/// ```
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::hmac_key::HmacKey;
///
/// let client = Client::default();
/// let hmac_key = client.hmac_key().create().await?;
/// # use cloud_storage::hmac_key::HmacState;
/// # client.hmac_key().update(&hmac_key.metadata.access_id, HmacState::Inactive).await?;
/// # client.hmac_key().delete(&hmac_key.metadata.access_id).await?;
/// # Ok(())
/// # }
/// ```
pub async fn create(&self) -> crate::Result<HmacKey> {
use reqwest::header::CONTENT_LENGTH;
let url = format!(
"{}/projects/{}/hmacKeys",
crate::BASE_URL,
crate::SERVICE_ACCOUNT.project_id
);
let query = [("serviceAccountEmail", &crate::SERVICE_ACCOUNT.client_email)];
let mut headers = self.0.get_headers().await?;
headers.insert(CONTENT_LENGTH, 0.into());
let result: GoogleResponse<HmacKey> = self
.0
.client
.post(&url)
.headers(headers)
.query(&query)
.send()
.await?
.json()
.await?;
match result {
GoogleResponse::Success(s) => Ok(s),
GoogleResponse::Error(e) => Err(e.into()),
}
}
/// Retrieves a list of HMAC keys matching the criteria. Since the HmacKey is secret, this does
/// not return a `HmacKey`, but a `HmacMeta`. This is a redacted version of a `HmacKey`, but
/// with the secret data omitted.
///
/// The authenticated user must have `storage.hmacKeys.list` permission for the project in which
/// the key exists.
///
/// For general information about HMAC keys in Cloud Storage, see
/// [HMAC Keys](https://cloud.google.com/storage/docs/authentication/hmackeys).
/// ### Example
/// ```
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::hmac_key::HmacKey;
///
/// let client = Client::default();
/// let all_hmac_keys = client.hmac_key().list().await?;
/// # Ok(())
/// # }
/// ```
pub async fn list(&self) -> crate::Result<Vec<HmacMeta>> {
let url = format!(
"{}/projects/{}/hmacKeys",
crate::BASE_URL,
crate::SERVICE_ACCOUNT.project_id
);
let response = self
.0
.client
.get(&url)
.headers(self.0.get_headers().await?)
.send()
.await?
.text()
.await?;
let result: Result<GoogleResponse<crate::hmac_key::ListResponse>, _> =
serde_json::from_str(&response);
// This function rquires more complicated error handling because when there is only one
// entry, Google will return the response `{ "kind": "storage#hmacKeysMetadata" }` instead
// of a list with one element. This breaks the parser.
match result {
Ok(parsed) => match parsed {
GoogleResponse::Success(s) => Ok(s.items),
GoogleResponse::Error(e) => Err(e.into()),
},
Err(_) => Ok(vec![]),
}
}
/// Retrieves an HMAC key's metadata. Since the HmacKey is secret, this does not return a
/// `HmacKey`, but a `HmacMeta`. This is a redacted version of a `HmacKey`, but with the secret
/// data omitted.
///
/// The authenticated user must have `storage.hmacKeys.get` permission for the project in which
/// the key exists.
///
/// For general information about HMAC keys in Cloud Storage, see
/// [HMAC Keys](https://cloud.google.com/storage/docs/authentication/hmackeys).
/// ### Example
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::hmac_key::HmacKey;
///
/// let client = Client::default();
/// let key = client.hmac_key().read("some identifier").await?;
/// # Ok(())
/// # }
pub async fn read(&self, access_id: &str) -> crate::Result<HmacMeta> {
let url = format!(
"{}/projects/{}/hmacKeys/{}",
crate::BASE_URL,
crate::SERVICE_ACCOUNT.project_id,
access_id
);
let result: GoogleResponse<HmacMeta> = self
.0
.client
.get(&url)
.headers(self.0.get_headers().await?)
.send()
.await?
.json()
.await?;
match result {
GoogleResponse::Success(s) => Ok(s),
GoogleResponse::Error(e) => Err(e.into()),
}
}
/// Updates the state of an HMAC key. See the HMAC Key resource descriptor for valid states.
/// Since the HmacKey is secret, this does not return a `HmacKey`, but a `HmacMeta`. This is a
/// redacted version of a `HmacKey`, but with the secret data omitted.
///
/// The authenticated user must have `storage.hmacKeys.update` permission for the project in
/// which the key exists.
///
/// For general information about HMAC keys in Cloud Storage, see
/// [HMAC Keys](https://cloud.google.com/storage/docs/authentication/hmackeys).
/// ### Example
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::hmac_key::{HmacKey, HmacState};
///
/// let client = Client::default();
/// let key = client.hmac_key().update("your key", HmacState::Active).await?;
/// # Ok(())
/// # }
pub async fn update(&self, access_id: &str, state: HmacState) -> crate::Result<HmacMeta> {
let url = format!(
"{}/projects/{}/hmacKeys/{}",
crate::BASE_URL,
crate::SERVICE_ACCOUNT.project_id,
access_id
);
serde_json::to_string(&crate::hmac_key::UpdateMeta { state })?;
let result: GoogleResponse<HmacMeta> = self
.0
.client
.put(&url)
.headers(self.0.get_headers().await?)
.json(&crate::hmac_key::UpdateMeta { state })
.send()
.await?
.json()
.await?;
match result {
GoogleResponse::Success(s) => Ok(s),
GoogleResponse::Error(e) => Err(e.into()),
}
}
/// Deletes an HMAC key. Note that a key must be set to `Inactive` first.
///
/// The authenticated user must have storage.hmacKeys.delete permission for the project in which
/// the key exists.
///
/// For general information about HMAC keys in Cloud Storage, see
/// [HMAC Keys](https://cloud.google.com/storage/docs/authentication/hmackeys).
/// ### Example
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use cloud_storage::Client;
/// use cloud_storage::hmac_key::{HmacKey, HmacState};
///
/// let client = Client::default();
/// let key = client.hmac_key().update("your key", HmacState::Inactive).await?; // this is required.
/// client.hmac_key().delete(&key.access_id).await?;
/// # Ok(())
/// # }
pub async fn delete(&self, access_id: &str) -> crate::Result<()> {
let url = format!(
"{}/projects/{}/hmacKeys/{}",
crate::BASE_URL,
crate::SERVICE_ACCOUNT.project_id,
access_id
);
let response = self
.0
.client
.delete(&url)
.headers(self.0.get_headers().await?)
.send()
.await?;
if response.status().is_success() {
Ok(())
} else {
Err(crate::Error::Google(response.json().await?))
}
}
}