@@ -3,6 +3,7 @@ package mongodb
33import (
44 "crypto/sha256"
55 "fmt"
6+ "strings"
67
78 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
89
@@ -87,12 +88,12 @@ func getTLSConfigModification(getUpdateCreator secret.GetUpdateCreator, mdb mdbv
8788 return automationconfig .NOOP (), nil
8889 }
8990
90- cert , key , err := getCertAndKey (getUpdateCreator , mdb )
91+ certKey , err := getCertAndKey (getUpdateCreator , mdb )
9192 if err != nil {
9293 return automationconfig .NOOP (), err
9394 }
9495
95- err = ensureTLSSecret (getUpdateCreator , mdb , cert , key )
96+ err = ensureTLSSecret (getUpdateCreator , mdb , certKey )
9697 if err != nil {
9798 return automationconfig .NOOP (), err
9899 }
@@ -101,37 +102,43 @@ func getTLSConfigModification(getUpdateCreator secret.GetUpdateCreator, mdb mdbv
101102 // The agent needs these to be in place before the config is updated.
102103 // Once the config is updated, the agents will gradually enable TLS in accordance with: https://docs.mongodb.com/manual/tutorial/upgrade-cluster-to-ssl/
103104 if hasRolledOutTLS (mdb ) {
104- return tlsConfigModification (mdb , cert , key ), nil
105+ return tlsConfigModification (mdb , certKey ), nil
105106 }
106107
107108 return automationconfig .NOOP (), nil
108109}
109110
110111// getCertAndKey will fetch the certificate and key from the user-provided Secret.
111- func getCertAndKey (getter secret.Getter , mdb mdbv1.MongoDB ) (string , string , error ) {
112+ func getCertAndKey (getter secret.Getter , mdb mdbv1.MongoDB ) (string , error ) {
112113 cert , err := secret .ReadKey (getter , tlsSecretCertName , mdb .TLSSecretNamespacedName ())
113114 if err != nil {
114- return "" , "" , err
115+ return "" , err
115116 }
116117
117118 key , err := secret .ReadKey (getter , tlsSecretKeyName , mdb .TLSSecretNamespacedName ())
118119 if err != nil {
119- return "" , "" , err
120+ return "" , err
120121 }
121122
122- return cert , key , nil
123+ return combineCertificateAndKey (cert , key ), nil
124+ }
125+
126+ func combineCertificateAndKey (cert , key string ) string {
127+ trimmedCert := strings .TrimRight (cert , "\n " )
128+ trimmedKey := strings .TrimRight (key , "\n " )
129+ return fmt .Sprintf ("%s\n %s" , trimmedCert , trimmedKey )
123130}
124131
125132// ensureTLSSecret will create or update the operator-managed Secret containing
126133// the concatenated certificate and key from the user-provided Secret.
127- func ensureTLSSecret (getUpdateCreator secret.GetUpdateCreator , mdb mdbv1.MongoDB , cert , key string ) error {
134+ func ensureTLSSecret (getUpdateCreator secret.GetUpdateCreator , mdb mdbv1.MongoDB , certKey string ) error {
128135 // Calculate file name from certificate and key
129- fileName := tlsOperatorSecretFileName (cert , key )
136+ fileName := tlsOperatorSecretFileName (certKey )
130137
131138 operatorSecret := secret .Builder ().
132139 SetName (mdb .TLSOperatorSecretNamespacedName ().Name ).
133140 SetNamespace (mdb .TLSOperatorSecretNamespacedName ().Namespace ).
134- SetField (fileName , cert + key ).
141+ SetField (fileName , certKey ).
135142 SetOwnerReferences ([]metav1.OwnerReference {getOwnerReference (mdb )}).
136143 Build ()
137144
@@ -144,15 +151,15 @@ func ensureTLSSecret(getUpdateCreator secret.GetUpdateCreator, mdb mdbv1.MongoDB
144151// the agent to perform a restart.
145152// The user-provided secret is being watched and will trigger a reconciliation
146153// on changes. This enables the operator to automatically handle cert rotations.
147- func tlsOperatorSecretFileName (cert , key string ) string {
148- hash := sha256 .Sum256 ([]byte (cert + key ))
154+ func tlsOperatorSecretFileName (certKey string ) string {
155+ hash := sha256 .Sum256 ([]byte (certKey ))
149156 return fmt .Sprintf ("%x.pem" , hash )
150157}
151158
152159// tlsConfigModification will enable TLS in the automation config.
153- func tlsConfigModification (mdb mdbv1.MongoDB , cert , key string ) automationconfig.Modification {
160+ func tlsConfigModification (mdb mdbv1.MongoDB , certKey string ) automationconfig.Modification {
154161 caCertificatePath := tlsCAMountPath + tlsCACertName
155- certificateKeyPath := tlsOperatorSecretMountPath + tlsOperatorSecretFileName (cert , key )
162+ certificateKeyPath := tlsOperatorSecretMountPath + tlsOperatorSecretFileName (certKey )
156163
157164 mode := automationconfig .TLSModeRequired
158165 if mdb .Spec .Security .TLS .Optional {
0 commit comments