-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmod.rs
More file actions
223 lines (198 loc) · 7.48 KB
/
Copy pathmod.rs
File metadata and controls
223 lines (198 loc) · 7.48 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
use kube::CustomResource;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
commons::{
networking::HostName, secret_class::SecretClassVolume, tls_verification::TlsClientDetails,
},
versioned::versioned,
};
mod v1alpha1_impl;
// FIXME (@Techassi): This should be versioned as well, but the macro cannot
// handle new-type structs yet.
/// Use this type in you operator!
pub type ResolvedConnection = v1alpha1::ConnectionSpec;
#[versioned(
version(name = "v1alpha1"),
crates(
kube_core = "kube::core",
k8s_openapi = "k8s_openapi",
schemars = "schemars",
)
)]
pub mod versioned {
pub mod v1alpha1 {
pub use v1alpha1_impl::ConnectionError;
}
/// S3 connection definition as a resource.
/// Learn more on the [S3 concept documentation](DOCS_BASE_URL_PLACEHOLDER/concepts/s3).
#[versioned(crd(
group = "s3.stackable.tech",
kind = "S3Connection",
plural = "s3connections",
namespaced
))]
#[derive(CustomResource, Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ConnectionSpec {
/// Host of the S3 server without any protocol or port. For example: `west1.my-cloud.com`.
pub host: HostName,
/// Port the S3 server listens on.
/// If not specified the product will determine the port to use.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub port: Option<u16>,
/// Bucket region used for signing headers (sigv4).
///
/// This defaults to `us-east-1` which is compatible with other implementations such as Minio.
///
/// WARNING: Some products use the Hadoop S3 implementation which falls back to us-east-2.
#[serde(default)]
pub region: Region,
/// Which access style to use.
/// Defaults to virtual hosted-style as most of the data products out there.
/// Have a look at the [AWS documentation](https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html).
#[serde(default)]
pub access_style: S3AccessStyle,
/// If the S3 uses authentication you have to specify you S3 credentials.
/// In the most cases a [SecretClass](DOCS_BASE_URL_PLACEHOLDER/secret-operator/secretclass)
/// providing `accessKey` and `secretKey` is sufficient.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub credentials: Option<SecretClassVolume>,
/// Use a TLS connection. If not specified no TLS will be used.
#[serde(flatten)]
pub tls: TlsClientDetails,
}
#[derive(
strum::Display, Clone, Debug, Default, Deserialize, Eq, JsonSchema, PartialEq, Serialize,
)]
#[strum(serialize_all = "PascalCase")]
pub enum S3AccessStyle {
/// Use path-style access as described in <https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html#path-style-access>
Path,
/// Use as virtual hosted-style access as described in <https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html#virtual-hosted-style-access>
#[default]
VirtualHosted,
}
/// Set a named S3 Bucket region.
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Region {
#[serde(default = "v1alpha1::Region::default_region_name")]
pub name: String,
}
#[derive(Clone, Debug, Deserialize, Eq, JsonSchema, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
// TODO: This probably should be serde(untagged), but this would be a breaking change
pub enum InlineConnectionOrReference {
Inline(ConnectionSpec),
Reference(String),
}
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use url::Url;
use super::*;
use crate::commons::{
secret_class::SecretClassVolume,
tls_verification::{CaCert, Tls, TlsClientDetails, TlsServerVerification, TlsVerification},
};
// We can't test the correct resolve, as we can't mock the k8s API.
#[test]
fn http_endpoint() {
let s3 = ResolvedConnection {
host: "minio".parse().unwrap(),
port: None,
access_style: Default::default(),
credentials: None,
tls: TlsClientDetails { tls: None },
region: Default::default(),
};
let (volumes, mounts) = s3.volumes_and_mounts().unwrap();
assert_eq!(s3.endpoint().unwrap(), Url::parse("http://minio").unwrap());
assert_eq!(volumes, vec![]);
assert_eq!(mounts, vec![]);
}
#[test]
fn https_endpoint() {
let s3 = ResolvedConnection {
host: "s3-eu-central-2.ionoscloud.com".parse().unwrap(),
port: None,
access_style: Default::default(),
credentials: Some(SecretClassVolume {
secret_class: "ionos-s3-credentials".to_string(),
scope: None,
}),
tls: TlsClientDetails {
tls: Some(Tls {
verification: TlsVerification::Server(TlsServerVerification {
ca_cert: CaCert::WebPki {},
}),
}),
},
region: Default::default(),
};
let (mut volumes, mut mounts) = s3.volumes_and_mounts().unwrap();
assert_eq!(
s3.endpoint().unwrap(),
Url::parse("https://s3-eu-central-2.ionoscloud.com").unwrap()
);
assert_eq!(volumes.len(), 1);
let volume = volumes.remove(0);
assert_eq!(mounts.len(), 1);
let mount = mounts.remove(0);
assert_eq!(&volume.name, "ionos-s3-credentials-s3-credentials");
assert_eq!(
&volume
.ephemeral
.unwrap()
.volume_claim_template
.unwrap()
.metadata
.unwrap()
.annotations
.unwrap(),
&BTreeMap::from([
(
"secrets.stackable.tech/class".to_string(),
"ionos-s3-credentials".to_string()
),
(
"secrets.stackable.tech/provision-parts".to_string(),
"public-private".to_string()
)
]),
);
assert_eq!(mount.name, volume.name);
assert_eq!(mount.mount_path, "/stackable/secrets/ionos-s3-credentials");
assert_eq!(
s3.credentials_mount_paths(),
Some((
"/stackable/secrets/ionos-s3-credentials/accessKey".to_string(),
"/stackable/secrets/ionos-s3-credentials/secretKey".to_string()
))
);
}
#[test]
fn https_without_verification() {
let s3 = ResolvedConnection {
host: "minio".parse().unwrap(),
port: Some(1234),
access_style: Default::default(),
credentials: None,
tls: TlsClientDetails {
tls: Some(Tls {
verification: TlsVerification::None {},
}),
},
region: Default::default(),
};
let (volumes, mounts) = s3.volumes_and_mounts().unwrap();
assert_eq!(
s3.endpoint().unwrap(),
Url::parse("https://minio:1234").unwrap()
);
assert_eq!(volumes, vec![]);
assert_eq!(mounts, vec![]);
}
}