@@ -22,13 +22,16 @@ use stackable_operator::{
2222 builder:: ObjectMetaBuilder ,
2323 k8s_openapi:: {
2424 api:: core:: v1:: { Secret , SecretReference } ,
25+ chrono:: { self , FixedOffset , TimeZone } ,
2526 ByteString ,
2627 } ,
2728 kube:: runtime:: reflector:: ObjectRef ,
2829} ;
2930use time:: { Duration , OffsetDateTime } ;
3031
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+ } ;
3235
3336#[ derive( Debug , Snafu ) ]
3437pub enum Error {
@@ -238,12 +241,13 @@ impl SecretBackend for TlsGenerate {
238241 /// Then add the ca certificate and return these files for provisioning to the volume.
239242 async fn get_secret_data (
240243 & self ,
241- selector : super :: SecretVolumeSelector ,
244+ selector : & super :: SecretVolumeSelector ,
242245 pod_info : PodInfo ,
243- ) -> Result < SecretFiles , Self :: Error > {
246+ ) -> Result < SecretContents , Self :: Error > {
244247 let now = OffsetDateTime :: now_utc ( ) ;
245248 let not_before = now - Duration :: minutes ( 5 ) ;
246249 let not_after = now + Duration :: days ( 1 ) ;
250+ let expire_pod_after = not_after - Duration :: minutes ( 30 ) ;
247251 let conf = Conf :: new ( ConfMethod :: default ( ) ) . unwrap ( ) ;
248252 let pod_key = Rsa :: generate ( 2048 )
249253 . and_then ( PKey :: try_from)
@@ -307,26 +311,53 @@ impl SecretBackend for TlsGenerate {
307311 } )
308312 . context ( BuildCertificateSnafu { tpe : CertType :: Pod } ) ?
309313 . 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( )
328359 ) ,
329- ]
330- . into ( ) )
360+ chrono :: DateTime :: parse_from_rfc3339 ( "2021-02-04T06:23:00.123+02:00" ) . unwrap ( )
361+ ) ;
331362 }
332363}
0 commit comments