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
// Copyright {{.Year}} Conductor OSS
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
use crate::error::Result;
use crate::http::{ApiClient, ApiPath};
use crate::models::MetadataTag;
use std::collections::HashSet;
/// Client for managing secrets.
#[derive(Clone)]
pub struct SecretClient {
api: ApiClient,
}
impl SecretClient {
/// Create a new secret client.
#[must_use]
pub fn new(api: ApiClient) -> Self {
Self { api }
}
/// Store a secret.
///
/// # Errors
///
/// 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.
pub async fn put_secret(&self, key: &str, value: &str) -> Result<()> {
let path = format!("/secrets/{key}");
self.api
.put_raw(ApiPath::templated(&path, "/secrets/{key}"), value)
.await
}
/// Get a secret value
///
/// The server returns this as `text/plain` (the raw secret value, not a
/// JSON string), so this uses `get_text` rather than the generic JSON
/// `get` -- see `ApiClient::get_text`.
///
/// # Errors
///
/// 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.
pub async fn get_secret(&self, key: &str) -> Result<String> {
let path = format!("/secrets/{key}");
self.api
.get_text(ApiPath::templated(&path, "/secrets/{key}"))
.await
}
/// List all secret names
///
/// This is `POST /secrets`, not `GET`. Both server families map the two
/// verbs to different operations: `POST` is the primary "list all secret
/// names" endpoint (`SecretController.listAllNames` on OSS,
/// `SecretResource.listAllSecretNames` on Orkes), while `GET` returns only
/// the names the caller has access to. On OSS the two happen to agree
/// because there is no RBAC, which is why the wrong verb went unnoticed.
///
/// # Errors
///
/// 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.
pub async fn list_all_secret_names(&self) -> Result<HashSet<String>> {
let names: Vec<String> = self.api.post_no_body("/secrets").await?;
Ok(names.into_iter().collect())
}
/// List secrets the caller has access to
///
/// `GET /secrets`. Orkes filters by the `access` query parameter (defaulting
/// to `READ`, with AND semantics across multiple values); OSS has no RBAC and
/// returns every name.
///
/// # Errors
///
/// 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.
pub async fn list_secrets_that_user_can_grant_access_to(&self) -> Result<Vec<String>> {
self.api.get("/secrets").await
}
/// Delete a secret.
///
/// # Errors
///
/// 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.
pub async fn delete_secret(&self, key: &str) -> Result<()> {
let path = format!("/secrets/{key}");
self.api
.delete_no_content(ApiPath::templated(&path, "/secrets/{key}"))
.await
}
/// Check if a secret exists.
///
/// # Errors
///
/// 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.
pub async fn secret_exists(&self, key: &str) -> Result<bool> {
let path = format!("/secrets/{key}/exists");
self.api
.get(ApiPath::templated(&path, "/secrets/{key}/exists"))
.await
}
/// Set tags for a secret.
///
/// # Errors
///
/// 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.
pub async fn set_secret_tags(&self, tags: &[MetadataTag], key: &str) -> Result<()> {
let path = format!("/secrets/{key}/tags");
self.api
.put_no_response(ApiPath::templated(&path, "/secrets/{key}/tags"), tags)
.await
}
/// Get tags for a secret.
///
/// # Errors
///
/// 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.
pub async fn get_secret_tags(&self, key: &str) -> Result<Vec<MetadataTag>> {
let path = format!("/secrets/{key}/tags");
self.api
.get(ApiPath::templated(&path, "/secrets/{key}/tags"))
.await
}
/// Delete tags from a secret.
///
/// # Errors
///
/// 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.
pub async fn delete_secret_tags(&self, tags: &[MetadataTag], key: &str) -> Result<()> {
let path = format!("/secrets/{key}/tags");
self.api
.delete_with_body(ApiPath::templated(&path, "/secrets/{key}/tags"), tags)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Configuration;
#[test]
fn test_secret_client_creation() {
let config = Configuration::new("http://localhost:8080/api");
let api = ApiClient::new(config).unwrap();
let _client = SecretClient::new(api);
}
}