Skip to main content

fauna_api/
secrets.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::authentification::{Credentials, CredentialsOrApiKey};
5use crate::error::Result;
6use crate::groups::Group;
7use crate::http::{client, ok};
8
9/// A secret in a person's or a group's store, used by name: a password, an API key, anything an
10/// agent needs without ever seeing it. Its value is never returned. A Termite repository
11/// switches on the secrets its machines get (`termite::versioning::Repository::set_secret`).
12#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
13#[serde(rename_all = "camelCase")]
14pub struct Secret {
15    pub id: u64,
16    /// Like an environment variable: capital letters, digits and underscores, starting with a
17    /// letter. Programs on a machine see it under this name, holding a stand in.
18    pub name: String,
19    /// What it is, which the agent reads.
20    #[serde(default)]
21    pub description: String,
22    /// Sites it can be typed into, as hosts. Empty means any.
23    #[serde(default)]
24    pub sites: Vec<String>,
25    /// API hosts programs can send it to. Empty means programs can't use it.
26    #[serde(default)]
27    pub hosts: Vec<String>,
28    pub updated_at: DateTime<Utc>,
29}
30
31impl Secret {
32    pub fn from_json(json: &serde_json::Value) -> Option<Secret> {
33        serde_json::from_value(json.clone()).ok()
34    }
35}
36
37/// A secret to add, or what to replace in the one by that name.
38#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
39pub struct Draft {
40    pub name: String,
41    /// At most 8 KB. Required for a new secret; left out, a replace keeps the value.
42    #[serde(skip_serializing_if = "Option::is_none")]
43    pub value: Option<String>,
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub description: Option<String>,
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub sites: Option<Vec<String>>,
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub hosts: Option<Vec<String>>,
50}
51
52/// Your secrets, or a group's (any member), by name: never their values.
53pub async fn list(
54    authorization: &CredentialsOrApiKey,
55    group: Option<&Group>,
56) -> Result<Vec<Secret>> {
57    let mut request = client()
58        .get(format!("{}/secrets", crate::get_url()))
59        .header(
60            "Authorization",
61            match authorization {
62                CredentialsOrApiKey::Credentials(credentials) => {
63                    format!("Token {}", credentials.token)
64                }
65                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
66            },
67        );
68    if let Some(group) = group {
69        request = request.header("Group", &group.id);
70    }
71    let response = request.send().await?;
72    Ok(ok(response).await?.json().await?)
73}
74
75/// Adds a secret to your store, or a group's (its direct admins), or replaces the one by that
76/// name.
77pub async fn set(
78    credentials: &Credentials,
79    draft: &Draft,
80    group: Option<&Group>,
81) -> Result<Secret> {
82    let mut request = client()
83        .post(format!("{}/secrets", crate::get_url()))
84        .header("Authorization", format!("Token {}", credentials.token))
85        .json(draft);
86    if let Some(group) = group {
87        request = request.header("Group", &group.id);
88    }
89    let response = request.send().await?;
90    Ok(ok(response).await?.json().await?)
91}
92
93/// Removes a secret from your store, or a group's (its direct admins), and so from every
94/// repository that uses it.
95pub async fn remove(credentials: &Credentials, id: u64, group: Option<&Group>) -> Result<()> {
96    let mut request = client()
97        .delete(format!("{}/secrets/{id}", crate::get_url()))
98        .header("Authorization", format!("Token {}", credentials.token));
99    if let Some(group) = group {
100        request = request.header("Group", &group.id);
101    }
102    let response = request.send().await?;
103    ok(response).await?;
104    Ok(())
105}