@@ -22,13 +22,16 @@ use stackable_operator::{
22
22
builder:: ObjectMetaBuilder ,
23
23
k8s_openapi:: {
24
24
api:: core:: v1:: { Secret , SecretReference } ,
25
+ chrono:: { self , FixedOffset , TimeZone } ,
25
26
ByteString ,
26
27
} ,
27
28
kube:: runtime:: reflector:: ObjectRef ,
28
29
} ;
29
30
use time:: { Duration , OffsetDateTime } ;
30
31
31
- use super :: { pod_info:: Address , pod_info:: PodInfo , SecretBackend , SecretBackendError , SecretFiles } ;
32
+ use super :: {
33
+ pod_info:: Address , pod_info:: PodInfo , SecretBackend , SecretBackendError , SecretContents ,
34
+ } ;
32
35
33
36
#[ derive( Debug , Snafu ) ]
34
37
pub enum Error {
@@ -238,12 +241,13 @@ impl SecretBackend for TlsGenerate {
238
241
/// Then add the ca certificate and return these files for provisioning to the volume.
239
242
async fn get_secret_data (
240
243
& self ,
241
- selector : super :: SecretVolumeSelector ,
244
+ selector : & super :: SecretVolumeSelector ,
242
245
pod_info : PodInfo ,
243
- ) -> Result < SecretFiles , Self :: Error > {
246
+ ) -> Result < SecretContents , Self :: Error > {
244
247
let now = OffsetDateTime :: now_utc ( ) ;
245
248
let not_before = now - Duration :: minutes ( 5 ) ;
246
249
let not_after = now + Duration :: days ( 1 ) ;
250
+ let expire_pod_after = not_after - Duration :: minutes ( 30 ) ;
247
251
let conf = Conf :: new ( ConfMethod :: default ( ) ) . unwrap ( ) ;
248
252
let pod_key = Rsa :: generate ( 2048 )
249
253
. and_then ( PKey :: try_from)
@@ -307,26 +311,53 @@ impl SecretBackend for TlsGenerate {
307
311
} )
308
312
. context ( BuildCertificateSnafu { tpe : CertType :: Pod } ) ?
309
313
. build ( ) ;
310
- Ok ( [
311
- (
312
- "ca.crt" . into ( ) ,
313
- self . ca_cert
314
- . to_pem ( )
315
- . context ( SerializeCertificateSnafu { tpe : CertType :: Pod } ) ?,
316
- ) ,
317
- (
318
- "tls.crt" . into ( ) ,
319
- pod_cert
320
- . to_pem ( )
321
- . context ( SerializeCertificateSnafu { tpe : CertType :: Pod } ) ?,
322
- ) ,
323
- (
324
- "tls.key" . into ( ) ,
325
- pod_key
326
- . private_key_to_pem_pkcs8 ( )
327
- . context ( SerializeCertificateSnafu { tpe : CertType :: Pod } ) ?,
314
+ Ok ( SecretContents :: new (
315
+ [
316
+ (
317
+ "ca.crt" . into ( ) ,
318
+ self . ca_cert
319
+ . to_pem ( )
320
+ . context ( SerializeCertificateSnafu { tpe : CertType :: Pod } ) ?,
321
+ ) ,
322
+ (
323
+ "tls.crt" . into ( ) ,
324
+ pod_cert
325
+ . to_pem ( )
326
+ . context ( SerializeCertificateSnafu { tpe : CertType :: Pod } ) ?,
327
+ ) ,
328
+ (
329
+ "tls.key" . into ( ) ,
330
+ pod_key
331
+ . private_key_to_pem_pkcs8 ( )
332
+ . context ( SerializeCertificateSnafu { tpe : CertType :: Pod } ) ?,
333
+ ) ,
334
+ ]
335
+ . into ( ) ,
336
+ )
337
+ . expires_after ( time_datetime_to_chrono ( expire_pod_after) ) )
338
+ }
339
+ }
340
+
341
+ fn time_datetime_to_chrono ( dt : time:: OffsetDateTime ) -> chrono:: DateTime < FixedOffset > {
342
+ let tz = chrono:: FixedOffset :: east ( dt. offset ( ) . whole_seconds ( ) ) ;
343
+ tz. timestamp ( dt. unix_timestamp ( ) , dt. nanosecond ( ) )
344
+ }
345
+
346
+ #[ cfg( test) ]
347
+ mod tests {
348
+ use time:: format_description:: well_known:: Rfc3339 ;
349
+
350
+ use super :: chrono;
351
+ use super :: time_datetime_to_chrono;
352
+
353
+ #[ test]
354
+ fn datetime_conversion ( ) {
355
+ // Conversion should preserve timezone and fractional seconds
356
+ assert_eq ! (
357
+ time_datetime_to_chrono(
358
+ time:: OffsetDateTime :: parse( "2021-02-04T05:23:00.123+01:00" , & Rfc3339 ) . unwrap( )
328
359
) ,
329
- ]
330
- . into ( ) )
360
+ chrono :: DateTime :: parse_from_rfc3339 ( "2021-02-04T06:23:00.123+02:00" ) . unwrap ( )
361
+ ) ;
331
362
}
332
363
}
0 commit comments