@@ -3,6 +3,7 @@ package mongodb
3
3
import (
4
4
"crypto/sha256"
5
5
"fmt"
6
+ "strings"
6
7
7
8
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8
9
@@ -87,12 +88,12 @@ func getTLSConfigModification(getUpdateCreator secret.GetUpdateCreator, mdb mdbv
87
88
return automationconfig .NOOP (), nil
88
89
}
89
90
90
- cert , key , err := getCertAndKey (getUpdateCreator , mdb )
91
+ certKey , err := getCertAndKey (getUpdateCreator , mdb )
91
92
if err != nil {
92
93
return automationconfig .NOOP (), err
93
94
}
94
95
95
- err = ensureTLSSecret (getUpdateCreator , mdb , cert , key )
96
+ err = ensureTLSSecret (getUpdateCreator , mdb , certKey )
96
97
if err != nil {
97
98
return automationconfig .NOOP (), err
98
99
}
@@ -101,37 +102,43 @@ func getTLSConfigModification(getUpdateCreator secret.GetUpdateCreator, mdb mdbv
101
102
// The agent needs these to be in place before the config is updated.
102
103
// Once the config is updated, the agents will gradually enable TLS in accordance with: https://docs.mongodb.com/manual/tutorial/upgrade-cluster-to-ssl/
103
104
if hasRolledOutTLS (mdb ) {
104
- return tlsConfigModification (mdb , cert , key ), nil
105
+ return tlsConfigModification (mdb , certKey ), nil
105
106
}
106
107
107
108
return automationconfig .NOOP (), nil
108
109
}
109
110
110
111
// 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 ) {
112
113
cert , err := secret .ReadKey (getter , tlsSecretCertName , mdb .TLSSecretNamespacedName ())
113
114
if err != nil {
114
- return "" , "" , err
115
+ return "" , err
115
116
}
116
117
117
118
key , err := secret .ReadKey (getter , tlsSecretKeyName , mdb .TLSSecretNamespacedName ())
118
119
if err != nil {
119
- return "" , "" , err
120
+ return "" , err
120
121
}
121
122
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 )
123
130
}
124
131
125
132
// ensureTLSSecret will create or update the operator-managed Secret containing
126
133
// 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 {
128
135
// Calculate file name from certificate and key
129
- fileName := tlsOperatorSecretFileName (cert , key )
136
+ fileName := tlsOperatorSecretFileName (certKey )
130
137
131
138
operatorSecret := secret .Builder ().
132
139
SetName (mdb .TLSOperatorSecretNamespacedName ().Name ).
133
140
SetNamespace (mdb .TLSOperatorSecretNamespacedName ().Namespace ).
134
- SetField (fileName , cert + key ).
141
+ SetField (fileName , certKey ).
135
142
SetOwnerReferences ([]metav1.OwnerReference {getOwnerReference (mdb )}).
136
143
Build ()
137
144
@@ -144,15 +151,15 @@ func ensureTLSSecret(getUpdateCreator secret.GetUpdateCreator, mdb mdbv1.MongoDB
144
151
// the agent to perform a restart.
145
152
// The user-provided secret is being watched and will trigger a reconciliation
146
153
// 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 ))
149
156
return fmt .Sprintf ("%x.pem" , hash )
150
157
}
151
158
152
159
// 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 {
154
161
caCertificatePath := tlsCAMountPath + tlsCACertName
155
- certificateKeyPath := tlsOperatorSecretMountPath + tlsOperatorSecretFileName (cert , key )
162
+ certificateKeyPath := tlsOperatorSecretMountPath + tlsOperatorSecretFileName (certKey )
156
163
157
164
mode := automationconfig .TLSModeRequired
158
165
if mdb .Spec .Security .TLS .Optional {
0 commit comments