@@ -120,34 +120,41 @@ func generateRSACert(hosts []string, keyOut, certOut io.Writer) error {
120
120
return nil
121
121
}
122
122
123
- // MustCreateCASignedCertSecret will create a secret using a CA Certificate, and public and private key for that certificate.
124
- func MustCreateCASignedCertSecret (t * testing.T , namespace , secretName string , hosts []string ) * corev1.Secret {
123
+ // MustCreateCASignedCertConfigMap will create a ConfigMap containing a CA Certificate, given a TLS Secret
124
+ // for that CA certificate.
125
+ func MustCreateCASignedCertConfigMap (t * testing.T , namespace , configMapName string , hosts []string ) * corev1.ConfigMap {
125
126
require .NotEmpty (t , hosts , "require a non-empty hosts for Subject Alternate Name values" )
126
127
127
- var serverKey , serverCert bytes.Buffer
128
-
129
- require .NoError (t , generateCACert (hosts , & serverKey , & serverCert ), "failed to generate CA certificate" )
128
+ caBytes , err := generateCACert (hosts )
129
+ if err != nil {
130
+ t .Errorf ("failed to generate CA certificate: %v" , err )
131
+ return nil
132
+ }
130
133
131
- data := map [string ][]byte {
132
- corev1 .TLSCertKey : serverCert .Bytes (),
133
- corev1 .TLSPrivateKeyKey : serverKey .Bytes (),
134
+ var certData bytes.Buffer
135
+ if err := pem .Encode (& certData , & pem.Block {Type : "CERTIFICATE" , Bytes : caBytes }); err != nil {
136
+ t .Errorf ("failed creating cert: %w" , err )
137
+ return nil
134
138
}
135
139
136
- newSecret := & corev1.Secret {
140
+ // Store the certificate in a ConfigMap.
141
+ caConfigMap := & corev1.ConfigMap {
137
142
ObjectMeta : metav1.ObjectMeta {
138
143
Namespace : namespace ,
139
- Name : secretName ,
144
+ Name : configMapName ,
145
+ },
146
+ Data : map [string ]string {
147
+ "ca.crt" : certData .String (),
140
148
},
141
- Type : corev1 .SecretTypeTLS ,
142
- Data : data ,
143
149
}
144
-
145
- return newSecret
150
+ return caConfigMap
146
151
}
147
152
148
- // generateCACert generates a CA Certificate signed certificate valid for a year.
149
- func generateCACert (hosts []string , keyOut , certOut io.Writer ) error {
150
- // Create the CA certificate.
153
+ // generateCACert generates a ConfigMap containing a CA Certificate signed certificate valid for a year.
154
+ func generateCACert (hosts []string ) ([]byte , error ) {
155
+ var caBytes []byte
156
+
157
+ // Create the CA certificate template.
151
158
ca := & x509.Certificate {
152
159
SerialNumber : big .NewInt (2024 ),
153
160
Subject : pkix.Name {
@@ -162,7 +169,7 @@ func generateCACert(hosts []string, keyOut, certOut io.Writer) error {
162
169
NotAfter : time .Now ().AddDate (1 , 0 , 0 ),
163
170
IsCA : true , // Indicates this is a CA Certificate.
164
171
ExtKeyUsage : []x509.ExtKeyUsage {x509 .ExtKeyUsageClientAuth , x509 .ExtKeyUsageServerAuth },
165
- KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageCertSign ,
172
+ KeyUsage : x509 .KeyUsageDigitalSignature | x509 .KeyUsageCertSign | x509 . KeyUsageKeyEncipherment ,
166
173
BasicConstraintsValid : true ,
167
174
}
168
175
@@ -175,26 +182,19 @@ func generateCACert(hosts []string, keyOut, certOut io.Writer) error {
175
182
}
176
183
}
177
184
178
- // Generate the private key.
185
+ // Generate the private key to sign certificates .
179
186
caPrivKey , err := rsa .GenerateKey (rand .Reader , rsaBits )
180
187
if err != nil {
181
- return err
188
+ return caBytes , fmt . Errorf ( "error generating key for CA: %v" , err )
182
189
}
183
190
184
- // Generate the certificate using the CA certificate.
185
- caBytes , err : = x509 .CreateCertificate (rand .Reader , ca , ca , & caPrivKey .PublicKey , caPrivKey )
191
+ // Create the self-signed certificate using the CA certificate.
192
+ caBytes , err = x509 .CreateCertificate (rand .Reader , ca , ca , & caPrivKey .PublicKey , caPrivKey )
186
193
if err != nil {
187
- return err
188
- }
189
-
190
- if err := pem .Encode (certOut , & pem.Block {Type : "CERTIFICATE" , Bytes : caBytes }); err != nil {
191
- return fmt .Errorf ("failed creating cert: %w" , err )
194
+ return caBytes , fmt .Errorf ("error creating CA: %v" , err )
192
195
}
193
196
194
- if err := pem .Encode (keyOut , & pem.Block {Type : "RSA PRIVATE KEY" , Bytes : x509 .MarshalPKCS1PrivateKey (caPrivKey )}); err != nil {
195
- return fmt .Errorf ("failed creating key: %w" , err )
196
- }
197
- return nil
197
+ return caBytes , nil
198
198
}
199
199
200
200
// validateHost ensures that the host name length is no more than 253 characters.
0 commit comments