-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathauthentication.rs
More file actions
157 lines (143 loc) · 6.32 KB
/
Copy pathauthentication.rs
File metadata and controls
157 lines (143 loc) · 6.32 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
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, Snafu};
use stackable_operator::{
client::Client,
crd::authentication::core,
kube::runtime::reflector::ObjectRef,
schemars::{self, JsonSchema},
versioned::versioned,
};
const SUPPORTED_AUTHENTICATION_CLASS: [&str; 1] = ["TLS"];
#[derive(Snafu, Debug)]
pub enum Error {
#[snafu(display("failed to retrieve AuthenticationClass [{}]", authentication_class))]
AuthenticationClassRetrieval {
source: stackable_operator::client::Error,
authentication_class: ObjectRef<core::v1alpha1::AuthenticationClass>,
},
// TODO: Adapt message if multiple authentication classes are supported
#[snafu(display(
"only one authentication class is currently supported. Possible Authentication classes are {SUPPORTED_AUTHENTICATION_CLASS:?}"
))]
MultipleAuthenticationClassesProvided,
#[snafu(display(
"failed to use authentication method [{method}] for authentication class [{authentication_class}] - supported mechanisms: {SUPPORTED_AUTHENTICATION_CLASS:?}",
))]
AuthenticationMethodNotSupported {
authentication_class: ObjectRef<core::v1alpha1::AuthenticationClass>,
method: String,
},
}
#[versioned(version(name = "v1alpha1"))]
pub mod versioned {
#[derive(Clone, Deserialize, Debug, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ZookeeperAuthentication {
/// The [AuthenticationClass](https://docs.stackable.tech/home/stable/concepts/authentication) to use.
///
/// ## mTLS
///
/// Only affects client connections. This setting controls:
/// - If clients need to authenticate themselves against the server via TLS
/// - Which ca.crt to use when validating the provided client certs
///
/// This will override the server TLS settings (if set) in `spec.clusterConfig.tls.serverSecretClass`.
pub authentication_class: String,
}
}
#[derive(Clone, Debug)]
/// Helper struct that contains dereferenced AuthenticationClasses to reduce network API calls.
pub struct DereferencedAuthenticationClasses {
dereferenced_authentication_classes: Vec<core::v1alpha1::AuthenticationClass>,
}
impl DereferencedAuthenticationClasses {
/// Fetch the referenced AuthenticationClasses from the Kubernetes API without validating them.
///
/// Call [`Self::validate`] on the result to enforce the constraints documented there.
pub async fn fetch_references(
client: &Client,
auth_classes: &Vec<v1alpha1::ZookeeperAuthentication>,
) -> Result<DereferencedAuthenticationClasses, Error> {
let mut dereferenced_authentication_classes: Vec<core::v1alpha1::AuthenticationClass> =
vec![];
for auth_class in auth_classes {
dereferenced_authentication_classes.push(
core::v1alpha1::AuthenticationClass::resolve(
client,
&auth_class.authentication_class,
)
.await
.context(AuthenticationClassRetrievalSnafu {
authentication_class: ObjectRef::<core::v1alpha1::AuthenticationClass>::new(
&auth_class.authentication_class,
),
})?,
);
}
Ok(DereferencedAuthenticationClasses {
dereferenced_authentication_classes,
})
}
/// Return the (first) TLS `AuthenticationClass` if available
pub fn get_tls_authentication_class(&self) -> Option<&core::v1alpha1::AuthenticationClass> {
self.dereferenced_authentication_classes
.iter()
.find(|auth| {
matches!(
auth.spec.provider,
core::v1alpha1::AuthenticationClassProvider::Tls(_)
)
})
}
/// Validates the dereferenced AuthenticationClasses.
/// Currently errors out if:
/// - More than one AuthenticationClass was provided
/// - AuthenticationClass mechanism was not supported
pub fn validate(&self) -> Result<Self, Error> {
if self.dereferenced_authentication_classes.len() > 1 {
return Err(Error::MultipleAuthenticationClassesProvided);
}
for auth_class in &self.dereferenced_authentication_classes {
match &auth_class.spec.provider {
core::v1alpha1::AuthenticationClassProvider::Tls(_) => {}
core::v1alpha1::AuthenticationClassProvider::Ldap(_)
| core::v1alpha1::AuthenticationClassProvider::Oidc(_)
| core::v1alpha1::AuthenticationClassProvider::Static(_)
| core::v1alpha1::AuthenticationClassProvider::Kerberos(_) => {
return Err(Error::AuthenticationMethodNotSupported {
authentication_class: ObjectRef::from_obj(auth_class),
method: auth_class.spec.provider.to_string(),
});
}
}
}
Ok(self.clone())
}
/// USE ONLY IN TESTS! We can not put it behind `#[cfg(test)]` because of <https://github.com/rust-lang/cargo/issues/8379>
pub fn new_for_tests() -> Self {
DereferencedAuthenticationClasses {
dereferenced_authentication_classes: vec![],
}
}
/// Builds a [`DereferencedAuthenticationClasses`] holding a single TLS `AuthenticationClass`.
/// Exercises the client-mTLS branches of [`crate::crd::security::ZookeeperSecurity`] (secure
/// client port and `ssl.clientAuth=need`), including the case where server TLS is off and the
/// auth class alone turns TLS on.
#[cfg(test)]
pub fn new_for_tests_with_tls_client_auth() -> Self {
use stackable_operator::crd::authentication::tls;
let auth_class = core::v1alpha1::AuthenticationClass::new(
"zk-client-auth-tls",
core::v1alpha1::AuthenticationClassSpec {
provider: core::v1alpha1::AuthenticationClassProvider::Tls(
tls::v1alpha1::AuthenticationProvider {
client_cert_secret_class: Some("zk-client-auth-secret".to_owned()),
},
),
},
);
DereferencedAuthenticationClasses {
dereferenced_authentication_classes: vec![auth_class],
}
}
}