Skip to main content

conductor/client/
secret_client.rs

1// Copyright {{.Year}} Conductor OSS
2// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3
4use crate::error::Result;
5use crate::http::{ApiClient, ApiPath};
6use crate::models::MetadataTag;
7use std::collections::HashSet;
8
9/// Client for managing secrets.
10#[derive(Clone)]
11pub struct SecretClient {
12    api: ApiClient,
13}
14
15impl SecretClient {
16    /// Create a new secret client.
17    #[must_use]
18    pub fn new(api: ApiClient) -> Self {
19        Self { api }
20    }
21
22    /// Store a secret.
23    ///
24    /// # Errors
25    ///
26    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
27    pub async fn put_secret(&self, key: &str, value: &str) -> Result<()> {
28        let path = format!("/secrets/{key}");
29        self.api
30            .put_raw(ApiPath::templated(&path, "/secrets/{key}"), value)
31            .await
32    }
33
34    /// Get a secret value
35    ///
36    /// The server returns this as `text/plain` (the raw secret value, not a
37    /// JSON string), so this uses `get_text` rather than the generic JSON
38    /// `get` -- see `ApiClient::get_text`.
39    ///
40    /// # Errors
41    ///
42    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
43    pub async fn get_secret(&self, key: &str) -> Result<String> {
44        let path = format!("/secrets/{key}");
45        self.api
46            .get_text(ApiPath::templated(&path, "/secrets/{key}"))
47            .await
48    }
49
50    /// List all secret names
51    ///
52    /// This is `POST /secrets`, not `GET`. Both server families map the two
53    /// verbs to different operations: `POST` is the primary "list all secret
54    /// names" endpoint (`SecretController.listAllNames` on OSS,
55    /// `SecretResource.listAllSecretNames` on Orkes), while `GET` returns only
56    /// the names the caller has access to. On OSS the two happen to agree
57    /// because there is no RBAC, which is why the wrong verb went unnoticed.
58    ///
59    /// # Errors
60    ///
61    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
62    pub async fn list_all_secret_names(&self) -> Result<HashSet<String>> {
63        let names: Vec<String> = self.api.post_no_body("/secrets").await?;
64        Ok(names.into_iter().collect())
65    }
66
67    /// List secrets the caller has access to
68    ///
69    /// `GET /secrets`. Orkes filters by the `access` query parameter (defaulting
70    /// to `READ`, with AND semantics across multiple values); OSS has no RBAC and
71    /// returns every name.
72    ///
73    /// # Errors
74    ///
75    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
76    pub async fn list_secrets_that_user_can_grant_access_to(&self) -> Result<Vec<String>> {
77        self.api.get("/secrets").await
78    }
79
80    /// Delete a secret.
81    ///
82    /// # Errors
83    ///
84    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
85    pub async fn delete_secret(&self, key: &str) -> Result<()> {
86        let path = format!("/secrets/{key}");
87        self.api
88            .delete_no_content(ApiPath::templated(&path, "/secrets/{key}"))
89            .await
90    }
91
92    /// Check if a secret exists.
93    ///
94    /// # Errors
95    ///
96    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
97    pub async fn secret_exists(&self, key: &str) -> Result<bool> {
98        let path = format!("/secrets/{key}/exists");
99        self.api
100            .get(ApiPath::templated(&path, "/secrets/{key}/exists"))
101            .await
102    }
103
104    /// Set tags for a secret.
105    ///
106    /// # Errors
107    ///
108    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, or an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status.
109    pub async fn set_secret_tags(&self, tags: &[MetadataTag], key: &str) -> Result<()> {
110        let path = format!("/secrets/{key}/tags");
111        self.api
112            .put_no_response(ApiPath::templated(&path, "/secrets/{key}/tags"), tags)
113            .await
114    }
115
116    /// Get tags for a secret.
117    ///
118    /// # Errors
119    ///
120    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
121    pub async fn get_secret_tags(&self, key: &str) -> Result<Vec<MetadataTag>> {
122        let path = format!("/secrets/{key}/tags");
123        self.api
124            .get(ApiPath::templated(&path, "/secrets/{key}/tags"))
125            .await
126    }
127
128    /// Delete tags from a secret.
129    ///
130    /// # Errors
131    ///
132    /// Returns [`crate::error::ConductorError::Http`] if the request fails at the transport level, an [`crate::error::ConductorError::Auth`]/[`crate::error::ConductorError::Api`]/[`crate::error::ConductorError::Server`] variant if the server responds with a non-2xx status, or [`crate::error::ConductorError::Json`] if the response body can't be deserialized.
133    pub async fn delete_secret_tags(&self, tags: &[MetadataTag], key: &str) -> Result<()> {
134        let path = format!("/secrets/{key}/tags");
135        self.api
136            .delete_with_body(ApiPath::templated(&path, "/secrets/{key}/tags"), tags)
137            .await
138    }
139}
140
141#[cfg(test)]
142mod tests {
143    use super::*;
144    use crate::Configuration;
145
146    #[test]
147    fn test_secret_client_creation() {
148        let config = Configuration::new("http://localhost:8080/api");
149        let api = ApiClient::new(config).unwrap();
150        let _client = SecretClient::new(api);
151    }
152}