@@ -153,6 +153,15 @@ func main() {
153153 if err := processLicenseSecret (kubeClient , nginxManager , mgmtCfgParams , controllerNamespace ); err != nil {
154154 logEventAndExit (ctx , eventRecorder , pod , secretErrorReason , err )
155155 }
156+
157+ if err := processTrustedCertSecret (kubeClient , nginxManager , mgmtCfgParams , controllerNamespace ); err != nil {
158+ logEventAndExit (ctx , eventRecorder , pod , secretErrorReason , err )
159+ }
160+
161+ if err := processClientAuthSecret (kubeClient , nginxManager , mgmtCfgParams , controllerNamespace ); err != nil {
162+ logEventAndExit (ctx , eventRecorder , pod , secretErrorReason , err )
163+ }
164+
156165 }
157166
158167 templateExecutor , templateExecutorV2 := createTemplateExecutors (ctx )
@@ -204,7 +213,7 @@ func main() {
204213 AppProtectBundlePath : appProtectBundlePath ,
205214 }
206215
207- mustProcessNginxConfig (staticCfgParams , cfgParams , mgmtCfgParams , templateExecutor , nginxManager )
216+ mustWriteNginxMainConfig (staticCfgParams , cfgParams , mgmtCfgParams , templateExecutor , nginxManager )
208217
209218 if * enableTLSPassthrough {
210219 var emptyFile []byte
@@ -323,6 +332,44 @@ func main() {
323332 }
324333}
325334
335+ func processClientAuthSecret (kubeClient * kubernetes.Clientset , nginxManager nginx.Manager , mgmtCfgParams * configs.MGMTConfigParams , controllerNamespace string ) error {
336+ if mgmtCfgParams .Secrets .ClientAuth == "" {
337+ return nil
338+ }
339+
340+ clientAuthSecretNsName := controllerNamespace + "/" + mgmtCfgParams .Secrets .ClientAuth
341+
342+ secret , err := getAndValidateSecret (kubeClient , clientAuthSecretNsName , api_v1 .SecretTypeTLS )
343+ if err != nil {
344+ return fmt .Errorf ("error trying to get the client auth secret %v: %w" , clientAuthSecretNsName , err )
345+ }
346+
347+ bytes := configs .GenerateCertAndKeyFileContent (secret )
348+ nginxManager .CreateSecret (fmt .Sprintf ("mgmt/%s" , configs .ClientAuthCertSecretFileName ), bytes , nginx .ReadWriteOnlyFileMode )
349+ return nil
350+ }
351+
352+ func processTrustedCertSecret (kubeClient * kubernetes.Clientset , nginxManager nginx.Manager , mgmtCfgParams * configs.MGMTConfigParams , controllerNamespace string ) error {
353+ if mgmtCfgParams .Secrets .TrustedCert == "" {
354+ return nil
355+ }
356+
357+ trustedCertSecretNsName := controllerNamespace + "/" + mgmtCfgParams .Secrets .TrustedCert
358+
359+ secret , err := getAndValidateSecret (kubeClient , trustedCertSecretNsName , secrets .SecretTypeCA )
360+ if err != nil {
361+ return fmt .Errorf ("error trying to get the trusted cert secret %v: %w" , trustedCertSecretNsName , err )
362+ }
363+
364+ caBytes , crlBytes := configs .GenerateCAFileContent (secret )
365+ nginxManager .CreateSecret (fmt .Sprintf ("mgmt/%s" , configs .CACrtKey ), caBytes , nginx .ReadWriteOnlyFileMode )
366+ if _ , hasCRL := secret .Data [configs .CACrlKey ]; hasCRL {
367+ mgmtCfgParams .Secrets .TrustedCRL = secret .Name
368+ nginxManager .CreateSecret (fmt .Sprintf ("mgmt/%s" , configs .CACrlKey ), crlBytes , nginx .ReadWriteOnlyFileMode )
369+ }
370+ return nil
371+ }
372+
326373func mustCreateConfigAndKubeClient (ctx context.Context ) (* rest.Config , * kubernetes.Clientset ) {
327374 l := nl .LoggerFromContext (ctx )
328375 var config * rest.Config
@@ -666,9 +713,9 @@ func createGlobalConfigurationValidator() *cr_validation.GlobalConfigurationVali
666713 return cr_validation .NewGlobalConfigurationValidator (forbiddenListenerPorts )
667714}
668715
669- // mustProcessNginxConfig calls internally os.Exit
716+ // mustWriteNginxMainConfig calls internally os.Exit
670717// if can't generate a valid NGINX config.
671- func mustProcessNginxConfig (staticCfgParams * configs.StaticConfigParams , cfgParams * configs.ConfigParams , mgmtCfgParams * configs.MGMTConfigParams , templateExecutor * version1.TemplateExecutor , nginxManager nginx.Manager ) {
718+ func mustWriteNginxMainConfig (staticCfgParams * configs.StaticConfigParams , cfgParams * configs.ConfigParams , mgmtCfgParams * configs.MGMTConfigParams , templateExecutor * version1.TemplateExecutor , nginxManager nginx.Manager ) {
672719 l := nl .LoggerFromContext (cfgParams .Context )
673720 ngxConfig := configs .GenerateNginxMainConfig (staticCfgParams , cfgParams , mgmtCfgParams )
674721 content , err := templateExecutor .ExecuteMainConfigTemplate (ngxConfig )
@@ -701,7 +748,6 @@ func getSocketClient(sockPath string) *http.Client {
701748}
702749
703750// getAndValidateSecret gets and validates a secret.
704- // nolint:unparam
705751func getAndValidateSecret (kubeClient * kubernetes.Clientset , secretNsName string , secretType api_v1.SecretType ) (secret * api_v1.Secret , err error ) {
706752 ns , name , err := k8s .ParseNamespaceName (secretNsName )
707753 if err != nil {
@@ -722,7 +768,13 @@ func getAndValidateSecret(kubeClient *kubernetes.Clientset, secretNsName string,
722768 if err != nil {
723769 return nil , err
724770 }
771+ case secrets .SecretTypeCA :
772+ err = secrets .ValidateCASecret (secret )
773+ if err != nil {
774+ return nil , err
775+ }
725776 }
777+
726778 return secret , nil
727779}
728780
0 commit comments