@@ -4,6 +4,7 @@ use stackable_operator::schemars::{self, JsonSchema};
44use strum:: EnumDiscriminants ;
55
66use super :: { ConvertError , SecretFiles , convert} ;
7+ use crate :: utils:: ResultExt ;
78
89const FILE_PEM_CERT_CERT : & str = "tls.crt" ;
910const FILE_PEM_CERT_KEY : & str = "tls.key" ;
@@ -30,7 +31,7 @@ pub struct TlsPkcs12 {
3031
3132#[ derive( Debug ) ]
3233pub struct Kerberos {
33- pub keytab : Vec < u8 > ,
34+ pub keytab : Option < Vec < u8 > > ,
3435 pub krb5_conf : Vec < u8 > ,
3536}
3637
@@ -47,71 +48,88 @@ pub enum WellKnownSecretData {
4748}
4849
4950impl WellKnownSecretData {
50- pub fn into_files ( self , names : NamingOptions , only_identity : bool ) -> SecretFiles {
51+ pub fn into_files ( self , names : NamingOptions ) -> SecretFiles {
5152 match self {
5253 WellKnownSecretData :: TlsPem ( TlsPem {
5354 certificate_pem,
5455 key_pem,
5556 ca_pem,
56- } ) => {
57- let mut files = vec ! [ Some ( ( names. tls_pem_ca_name, ca_pem) ) ] ;
58-
59- if !only_identity {
60- files. extend ( [
61- Some ( names. tls_pem_cert_name ) . zip ( certificate_pem) ,
62- Some ( names. tls_pem_key_name ) . zip ( key_pem) ,
63- ] ) ;
64- }
65-
66- files. into_iter ( ) . flatten ( ) . collect ( )
67- }
57+ } ) => [
58+ Some ( ( names. tls_pem_ca_name , ca_pem) ) ,
59+ Some ( names. tls_pem_cert_name ) . zip ( certificate_pem) ,
60+ Some ( names. tls_pem_key_name ) . zip ( key_pem) ,
61+ ]
62+ . into_iter ( )
63+ . flatten ( )
64+ . collect ( ) ,
6865 WellKnownSecretData :: TlsPkcs12 ( TlsPkcs12 {
6966 keystore,
7067 truststore,
71- } ) => {
72- let mut files = vec ! [ Some ( ( names. tls_pkcs12_truststore_name, truststore) ) ] ;
73-
74- if !only_identity {
75- files. push ( Some ( names. tls_pkcs12_keystore_name ) . zip ( keystore) ) ;
76- }
77-
78- files. into_iter ( ) . flatten ( ) . collect ( )
79- }
80- WellKnownSecretData :: Kerberos ( Kerberos { keytab, krb5_conf } ) => {
81- let mut files = vec ! [ ( FILE_KERBEROS_KEYTAB_KRB5_CONF . to_string( ) , krb5_conf) ] ;
82-
83- if !only_identity {
84- files. push ( ( FILE_KERBEROS_KEYTAB_KEYTAB . to_string ( ) , keytab) ) ;
85- }
86-
87- SecretFiles :: from_iter ( files)
88- }
68+ } ) => [
69+ Some ( ( names. tls_pkcs12_truststore_name , truststore) ) ,
70+ Some ( names. tls_pkcs12_keystore_name ) . zip ( keystore) ,
71+ ]
72+ . into_iter ( )
73+ . flatten ( )
74+ . collect ( ) ,
75+ WellKnownSecretData :: Kerberos ( Kerberos { keytab, krb5_conf } ) => [
76+ Some ( ( FILE_KERBEROS_KEYTAB_KRB5_CONF . to_owned ( ) , krb5_conf) ) ,
77+ Some ( FILE_KERBEROS_KEYTAB_KEYTAB . to_owned ( ) ) . zip ( keytab) ,
78+ ]
79+ . into_iter ( )
80+ . flatten ( )
81+ . collect ( ) ,
8982 }
9083 }
9184
92- pub fn from_files ( mut files : SecretFiles ) -> Result < WellKnownSecretData , FromFilesError > {
85+ pub fn from_files (
86+ mut files : SecretFiles ,
87+ relaxed : bool ,
88+ ) -> Result < WellKnownSecretData , FromFilesError > {
89+ tracing:: debug!( relaxed, "Constructing well-known secret data from files" ) ;
90+
9391 let mut take_file = |format, file| {
9492 files
9593 . remove ( file)
9694 . context ( from_files_error:: MissingRequiredFileSnafu { format, file } )
9795 } ;
9896
99- if let Ok ( certificate_pem) = take_file ( SecretFormat :: TlsPem , FILE_PEM_CERT_CERT ) {
97+ // Which file is tried to be parsed first matters. To support the use-case of people bringing
98+ // their own non-sensitive data via a Secret and consumers only requiring access to
99+ // non-sensitve data (for example for CA verification), the non-senstive files are parsed
100+ // first. If the `relaxed` flag is provided, this function tries to parse sensitive files
101+ // but won't hard-error when they are not found.
102+
103+ if let Ok ( ca_pem) = take_file ( SecretFormat :: TlsPem , FILE_PEM_CERT_CA ) {
100104 let mut take_file = |file| take_file ( SecretFormat :: TlsPem , file) ;
105+
106+ let certificate_pem = take_file ( FILE_PEM_CERT_CERT ) . ok_if ( relaxed) ?;
107+ let key_pem = take_file ( FILE_PEM_CERT_KEY ) . ok_if ( relaxed) ?;
108+
101109 Ok ( WellKnownSecretData :: TlsPem ( TlsPem {
102- certificate_pem : Some ( certificate_pem ) ,
103- key_pem : Some ( take_file ( FILE_PEM_CERT_KEY ) ? ) ,
104- ca_pem : take_file ( FILE_PEM_CERT_CA ) ? ,
110+ certificate_pem,
111+ key_pem,
112+ ca_pem,
105113 } ) )
106- } else if let Ok ( keystore) = take_file ( SecretFormat :: TlsPkcs12 , FILE_PKCS12_CERT_KEYSTORE ) {
114+ } else if let Ok ( truststore) =
115+ take_file ( SecretFormat :: TlsPkcs12 , FILE_PKCS12_CERT_TRUSTSTORE )
116+ {
117+ let keystore =
118+ take_file ( SecretFormat :: TlsPkcs12 , FILE_PKCS12_CERT_KEYSTORE ) . ok_if ( relaxed) ?;
119+
107120 Ok ( WellKnownSecretData :: TlsPkcs12 ( TlsPkcs12 {
108- keystore : Some ( keystore ) ,
109- truststore : take_file ( SecretFormat :: TlsPkcs12 , FILE_PKCS12_CERT_TRUSTSTORE ) ? ,
121+ keystore,
122+ truststore,
110123 } ) )
111- } else if let Ok ( keytab) = take_file ( SecretFormat :: Kerberos , FILE_KERBEROS_KEYTAB_KEYTAB ) {
124+ } else if let Ok ( krb5_conf) =
125+ take_file ( SecretFormat :: Kerberos , FILE_KERBEROS_KEYTAB_KRB5_CONF )
126+ {
127+ let keytab =
128+ take_file ( SecretFormat :: Kerberos , FILE_KERBEROS_KEYTAB_KEYTAB ) . ok_if ( relaxed) ?;
129+
112130 Ok ( WellKnownSecretData :: Kerberos ( Kerberos {
113131 keytab,
114- krb5_conf : take_file ( SecretFormat :: Kerberos , FILE_KERBEROS_KEYTAB_KRB5_CONF ) ? ,
132+ krb5_conf,
115133 } ) )
116134 } else {
117135 from_files_error:: UnknownFormatSnafu {
0 commit comments