@@ -190,9 +190,9 @@ func (l *L7) checkProxy() (err error) {
190
190
return nil
191
191
}
192
192
193
- func (l * L7 ) deleteOldSSLCerts () ( err error ) {
193
+ func (l * L7 ) deleteOldSSLCerts () {
194
194
if len (l .oldSSLCerts ) == 0 {
195
- return nil
195
+ return
196
196
}
197
197
certsMap := getMapfromCertList (l .sslCerts )
198
198
for _ , cert := range l .oldSSLCerts {
@@ -204,13 +204,11 @@ func (l *L7) deleteOldSSLCerts() (err error) {
204
204
// cert found in current map
205
205
continue
206
206
}
207
- glog .Infof ("Cleaning up old SSL Certificate %s" , cert .Name )
207
+ glog .V ( 3 ). Infof ("Cleaning up old SSL Certificate %s" , cert .Name )
208
208
if certErr := utils .IgnoreHTTPNotFound (l .cloud .DeleteSslCertificate (cert .Name )); certErr != nil {
209
209
glog .Errorf ("Old cert delete failed - %v" , certErr )
210
- err = certErr
211
210
}
212
211
}
213
- return err
214
212
}
215
213
216
214
// Returns the name portion of a link - which is the last section
@@ -317,47 +315,55 @@ func (l *L7) checkSSLCert() error {
317
315
return err
318
316
}
319
317
320
- var newCerts []* compute.SslCertificate
318
+ existingCertsMap := getMapfromCertList (l .sslCerts )
319
+ l .oldSSLCerts = l .sslCerts
320
+ l .sslCerts = make ([]* compute.SslCertificate , 0 )
321
+
321
322
// mapping of currently configured certs
322
- certsMap := getMapfromCertList ( l . sslCerts )
323
+ visitedCertMap := make ( map [ string ] string )
323
324
var failedCerts []string
324
325
325
326
for _ , tlsCert := range l .runtimeInfo .TLS {
326
327
ingCert := tlsCert .Cert
327
328
ingKey := tlsCert .Key
328
- newCertName := l .namer .SSLCertName (l .Name , tlsCert .CertHash )
329
+ gcpCertName := l .namer .SSLCertName (l .Name , tlsCert .CertHash )
330
+
331
+ if addedBy , exists := visitedCertMap [gcpCertName ]; exists {
332
+ glog .V (3 ).Infof ("Secret %q has a certificate already used by %v" , tlsCert .Name , addedBy )
333
+ continue
334
+ }
329
335
330
336
// PrivateKey is write only, so compare certs alone. We're assuming that
331
337
// no one will change just the key. We can remember the key and compare,
332
338
// but a bug could end up leaking it, which feels worse.
333
339
// If the cert contents have changed, its hash would be different, so would be the cert name. So it is enough
334
340
// to check if this cert name exists in the map.
335
- if certsMap != nil {
336
- if cert , ok := certsMap [newCertName ]; ok {
337
- glog .Infof ("Retaining cert - %s" , tlsCert .Name )
338
- newCerts = append (newCerts , cert )
341
+ if existingCertsMap != nil {
342
+ if cert , ok := existingCertsMap [gcpCertName ]; ok {
343
+ glog .V (3 ).Infof ("Secret %q already exists as certificate %q" , tlsCert .Name , gcpCertName )
344
+ visitedCertMap [gcpCertName ] = fmt .Sprintf ("certificate:%q" , gcpCertName )
345
+ l .sslCerts = append (l .sslCerts , cert )
339
346
continue
340
347
}
341
348
}
342
349
// Controller needs to create the certificate, no need to check if it exists and delete. If it did exist, it
343
350
// would have been listed in the populateSSLCert function and matched in the check above.
344
- glog .V (2 ).Infof ("Creating new sslCertificate %v for %v " , newCertName , l .Name )
351
+ glog .V (2 ).Infof ("Creating new sslCertificate %q for LB %q " , gcpCertName , l .Name )
345
352
cert , err := l .cloud .CreateSslCertificate (& compute.SslCertificate {
346
- Name : newCertName ,
353
+ Name : gcpCertName ,
347
354
Certificate : ingCert ,
348
355
PrivateKey : ingKey ,
349
356
})
350
357
if err != nil {
351
- glog .Errorf ("Failed to create new sslCertificate %v for %v - %s " , newCertName , l .Name , err )
352
- failedCerts = append (failedCerts , newCertName + " Error:" + err .Error ())
358
+ glog .Errorf ("Failed to create new sslCertificate %q for %q - %v " , gcpCertName , l .Name , err )
359
+ failedCerts = append (failedCerts , gcpCertName + " Error:" + err .Error ())
353
360
continue
354
361
}
355
- newCerts = append (newCerts , cert )
362
+ visitedCertMap [gcpCertName ] = fmt .Sprintf ("secret:%q" , tlsCert .Name )
363
+ l .sslCerts = append (l .sslCerts , cert )
356
364
}
357
365
358
366
// Save the old certs for cleanup after we update the target proxy.
359
- l .oldSSLCerts = l .sslCerts
360
- l .sslCerts = newCerts
361
367
if len (failedCerts ) > 0 {
362
368
return fmt .Errorf ("Cert creation failures - %s" , strings .Join (failedCerts , "," ))
363
369
}
@@ -627,19 +633,15 @@ func (l *L7) edgeHopHttp() error {
627
633
}
628
634
629
635
func (l * L7 ) edgeHopHttps () error {
636
+ defer l .deleteOldSSLCerts ()
630
637
if err := l .checkSSLCert (); err != nil {
631
638
return err
632
639
}
640
+
633
641
if err := l .checkHttpsProxy (); err != nil {
634
642
return err
635
643
}
636
- if err := l .checkHttpsForwardingRule (); err != nil {
637
- return err
638
- }
639
- if err := l .deleteOldSSLCerts (); err != nil {
640
- return err
641
- }
642
- return nil
644
+ return l .checkHttpsForwardingRule ()
643
645
}
644
646
645
647
// GetIP returns the ip associated with the forwarding rule for this l7.
0 commit comments