-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdynamic.rs
More file actions
225 lines (201 loc) · 6.72 KB
/
dynamic.rs
File metadata and controls
225 lines (201 loc) · 6.72 KB
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
//! Support code for runtime-configurable dynamic [`SecretBackend`]s
use std::{
collections::HashSet,
fmt::{Debug, Display},
};
use async_trait::async_trait;
use snafu::{ResultExt, Snafu};
use stackable_operator::kube::runtime::reflector::ObjectRef;
use super::{
SecretBackend, SecretBackendError, SecretVolumeSelector, auto_tls,
kerberos_keytab::{self, KerberosProfile},
pod_info::{PodInfo, SchedulingPodInfo},
};
use crate::{crd::v1alpha2, utils::Unloggable};
pub struct DynError(Box<dyn SecretBackendError>);
impl Debug for DynError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl Display for DynError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
impl std::error::Error for DynError {
fn source(&self) -> Option<&(dyn snafu::Error + 'static)> {
self.0.source()
}
}
impl SecretBackendError for DynError {
fn grpc_code(&self) -> tonic::Code {
self.0.grpc_code()
}
fn secondary_object(&self) -> Option<ObjectRef<stackable_operator::kube::api::DynamicObject>> {
self.0.secondary_object()
}
}
pub struct DynamicAdapter<B>(B);
impl<B: Debug> Debug for DynamicAdapter<B> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[async_trait]
impl<B: SecretBackend + Send + Sync> SecretBackend for DynamicAdapter<B> {
type Error = DynError;
async fn get_secret_data(
&self,
selector: &super::SecretVolumeSelector,
pod_info: PodInfo,
) -> Result<super::SecretContents, Self::Error> {
self.0
.get_secret_data(selector, pod_info)
.await
.map_err(|err| DynError(Box::new(err)))
}
async fn get_trust_data(
&self,
selector: &super::TrustSelector,
) -> Result<super::SecretContents, Self::Error> {
self.0
.get_trust_data(selector)
.await
.map_err(|err| DynError(Box::new(err)))
}
async fn get_qualified_node_names(
&self,
selector: &SecretVolumeSelector,
pod_info: SchedulingPodInfo,
) -> Result<Option<HashSet<String>>, Self::Error> {
self.0
.get_qualified_node_names(selector, pod_info)
.await
.map_err(|err| DynError(Box::new(err)))
}
}
pub type Dynamic = dyn SecretBackend<Error = DynError>;
pub fn from(backend: impl SecretBackend + 'static) -> Box<Dynamic> {
Box::new(DynamicAdapter(backend))
}
#[derive(Debug, Snafu)]
#[snafu(module)]
pub enum FromClassError {
#[snafu(display("failed to initialize TLS backend"), context(false))]
Tls { source: auto_tls::Error },
#[snafu(
display("failed to initialize Kerberos Keytab backend"),
context(false)
)]
KerberosKeytab { source: kerberos_keytab::Error },
}
impl SecretBackendError for FromClassError {
fn grpc_code(&self) -> tonic::Code {
match self {
FromClassError::Tls { source } => source.grpc_code(),
FromClassError::KerberosKeytab { source } => source.grpc_code(),
}
}
fn secondary_object(&self) -> Option<ObjectRef<stackable_operator::kube::api::DynamicObject>> {
match self {
FromClassError::Tls { source } => source.secondary_object(),
FromClassError::KerberosKeytab { source } => source.secondary_object(),
}
}
}
pub async fn from_class(
client: &stackable_operator::client::Client,
class: v1alpha2::SecretClass,
) -> Result<Box<Dynamic>, FromClassError> {
Ok(match class.spec.backend {
v1alpha2::SecretClassBackend::K8sSearch(v1alpha2::K8sSearchBackend {
search_namespace,
trust_store_config_map_name,
}) => from(super::K8sSearch {
client: Unloggable(client.clone()),
search_namespace,
trust_store_config_map_name,
}),
v1alpha2::SecretClassBackend::AutoTls(v1alpha2::AutoTlsBackend {
ca,
additional_trust_roots,
max_certificate_lifetime,
}) => from(
super::TlsGenerate::get_or_create_k8s_certificate(
client,
&ca,
&additional_trust_roots,
max_certificate_lifetime,
)
.await?,
),
v1alpha2::SecretClassBackend::CertManager(config) => from(super::CertManager {
client: Unloggable(client.clone()),
config,
}),
v1alpha2::SecretClassBackend::KerberosKeytab(v1alpha2::KerberosKeytabBackend {
realm_name,
kdc,
admin,
admin_keytab_secret,
admin_principal,
}) => from(
super::KerberosKeytab::new_from_k8s_keytab(
client,
KerberosProfile {
realm_name,
kdc,
admin,
},
&admin_keytab_secret,
admin_principal,
)
.await?,
),
})
}
#[derive(Debug, Snafu)]
#[snafu(module)]
pub enum FromSelectorError {
#[snafu(display("failed to get {class}"))]
GetSecretClass {
source: stackable_operator::client::Error,
class: ObjectRef<v1alpha2::SecretClass>,
},
#[snafu(display("failed to initialize backend for {class}"))]
FromClass {
#[snafu(source(from(FromClassError, Box::new)))]
source: Box<FromClassError>,
class: ObjectRef<v1alpha2::SecretClass>,
},
}
impl SecretBackendError for FromSelectorError {
fn grpc_code(&self) -> tonic::Code {
match self {
FromSelectorError::GetSecretClass { .. } => tonic::Code::Unavailable,
FromSelectorError::FromClass { source, .. } => source.grpc_code(),
}
}
fn secondary_object(&self) -> Option<ObjectRef<stackable_operator::kube::api::DynamicObject>> {
match self {
FromSelectorError::GetSecretClass { class, .. } => Some(class.clone().erase()),
FromSelectorError::FromClass { source, class } => source
.secondary_object()
.or_else(|| Some(class.clone().erase())),
}
}
}
pub async fn from_selector(
client: &stackable_operator::client::Client,
selector: &SecretVolumeSelector,
) -> Result<Box<Dynamic>, FromSelectorError> {
let class_ref = || ObjectRef::new(&selector.class);
let class = client
.get::<v1alpha2::SecretClass>(&selector.class, &())
.await
.with_context(|_| from_selector_error::GetSecretClassSnafu { class: class_ref() })?;
from_class(client, class)
.await
.with_context(|_| from_selector_error::FromClassSnafu { class: class_ref() })
}