@@ -33,29 +33,28 @@ import (
33
33
// certificateData is a structure containing the data about a K8S secret required
34
34
// to store SSL information required for BackendSets and Listeners
35
35
type certificateData struct {
36
- CACert string
37
- PublicCert string
38
- PrivateKey string
39
- Passphrase string
36
+ Name string
37
+ CACert []byte
38
+ PublicCert []byte
39
+ PrivateKey []byte
40
+ Passphrase []byte
40
41
}
41
42
42
43
type sslSecretReader interface {
43
- readSSLSecret (secretType string , svc * v1. Service ) (sslSecret * certificateData , err error )
44
+ readSSLSecret (ns , name string ) (sslSecret * certificateData , err error )
44
45
}
45
46
46
47
type noopSSLSecretReader struct {}
47
48
48
- func (ssr noopSSLSecretReader ) readSSLSecret (secretType string , svc * v1. Service ) (sslSecret * certificateData , err error ) {
49
- return & certificateData {} , nil
49
+ func (ssr noopSSLSecretReader ) readSSLSecret (ns , name string ) (sslSecret * certificateData , err error ) {
50
+ return nil , nil
50
51
}
51
52
52
53
// SSLConfig is a description of a SSL certificate.
53
54
type SSLConfig struct {
54
- Name string
55
- Type string
56
- Ports sets.Int
57
- ListenerSSLSecret * certificateData
58
- BackendSetSSLSecret * certificateData
55
+ Ports sets.Int
56
+ ListenerSSLSecretName string
57
+ BackendSetSSLSecretName string
59
58
60
59
sslSecretReader
61
60
}
@@ -66,15 +65,15 @@ func requiresCertificate(svc *v1.Service) bool {
66
65
}
67
66
68
67
// NewSSLConfig constructs a new SSLConfig.
69
- func NewSSLConfig (name string , sslType string , ports []int , ssr sslSecretReader ) * SSLConfig {
68
+ func NewSSLConfig (listenerSecretName , backendSetSecretName string , ports []int , ssr sslSecretReader ) * SSLConfig {
70
69
if ssr == nil {
71
70
ssr = noopSSLSecretReader {}
72
71
}
73
72
return & SSLConfig {
74
- Name : name ,
75
- Type : sslType ,
76
- Ports : sets . NewInt ( ports ... ) ,
77
- sslSecretReader : ssr ,
73
+ Ports : sets . NewInt ( ports ... ) ,
74
+ ListenerSSLSecretName : listenerSecretName ,
75
+ BackendSetSSLSecretName : backendSetSecretName ,
76
+ sslSecretReader : ssr ,
78
77
}
79
78
}
80
79
@@ -90,16 +89,15 @@ type LBSpec struct {
90
89
91
90
Ports map [string ]portSpec
92
91
SourceCIDRs []string
93
- ListenerSSLConfig * SSLConfig
94
- BackendSetSSLConfig * SSLConfig
92
+ SSLConfig * SSLConfig
95
93
securityListManager securityListManager
96
94
97
95
service * v1.Service
98
96
nodes []* v1.Node
99
97
}
100
98
101
99
// NewLBSpec creates a LB Spec from a Kubernetes service and a slice of nodes.
102
- func NewLBSpec (svc * v1.Service , nodes []* v1.Node , defaultSubnets []string , listenerSSLConfig * SSLConfig , backendSetSSLConfig * SSLConfig , secListFactory securityListManagerFactory ) (* LBSpec , error ) {
100
+ func NewLBSpec (svc * v1.Service , nodes []* v1.Node , defaultSubnets []string , sslConfig * SSLConfig , secListFactory securityListManagerFactory ) (* LBSpec , error ) {
103
101
if len (defaultSubnets ) != 2 {
104
102
return nil , errors .New ("default subnets incorrectly configured" )
105
103
}
@@ -145,7 +143,7 @@ func NewLBSpec(svc *v1.Service, nodes []*v1.Node, defaultSubnets []string, liste
145
143
}
146
144
}
147
145
148
- listeners , err := getListeners (svc , listenerSSLConfig )
146
+ listeners , err := getListeners (svc , sslConfig )
149
147
if err != nil {
150
148
return nil , err
151
149
}
@@ -156,12 +154,11 @@ func NewLBSpec(svc *v1.Service, nodes []*v1.Node, defaultSubnets []string, liste
156
154
Internal : internal ,
157
155
Subnets : subnets ,
158
156
Listeners : listeners ,
159
- BackendSets : getBackendSets (svc , nodes , backendSetSSLConfig ),
157
+ BackendSets : getBackendSets (svc , nodes , sslConfig ),
160
158
161
- Ports : getPorts (svc ),
162
- ListenerSSLConfig : listenerSSLConfig ,
163
- BackendSetSSLConfig : backendSetSSLConfig ,
164
- SourceCIDRs : sourceCIDRs ,
159
+ Ports : getPorts (svc ),
160
+ SSLConfig : sslConfig ,
161
+ SourceCIDRs : sourceCIDRs ,
165
162
166
163
service : svc ,
167
164
nodes : nodes ,
@@ -171,26 +168,42 @@ func NewLBSpec(svc *v1.Service, nodes []*v1.Node, defaultSubnets []string, liste
171
168
}
172
169
173
170
// Certificates builds a map of required SSL certificates.
174
- func buildCertificates (sslConfig * SSLConfig , svc * v1.Service , certs map [string ]loadbalancer.CertificateDetails ) error {
175
- if sslConfig == nil {
176
- return nil
171
+ func (s * LBSpec ) Certificates () (map [string ]loadbalancer.CertificateDetails , error ) {
172
+ certs := make (map [string ]loadbalancer.CertificateDetails )
173
+ if s .SSLConfig == nil {
174
+ return certs , nil
177
175
}
178
- sslSecret , err := sslConfig .readSSLSecret (sslConfig .Type , svc )
176
+ //Read listener Kubernetes Secret
177
+ sslSecret , err := s .SSLConfig .readSSLSecret (s .service .Namespace , s .SSLConfig .ListenerSSLSecretName )
179
178
if err != nil {
180
- return errors .Wrap (err , "reading SSL Secret" )
179
+ return nil , errors .Wrap (err , "reading SSL Listener Secret" )
181
180
}
182
- if sslSecret .PublicCert == "" || sslSecret .PrivateKey == "" {
183
- return nil
181
+ if len ( sslSecret .PublicCert ) == 0 || len ( sslSecret .PrivateKey ) == 0 {
182
+ return certs , nil
184
183
}
185
184
186
- certs [sslConfig .Name ] = loadbalancer.CertificateDetails {
187
- CertificateName : & sslConfig .Name ,
188
- PublicCertificate : & sslSecret .PublicCert ,
189
- CaCertificate : & sslSecret .CACert ,
190
- PrivateKey : & sslSecret .PrivateKey ,
191
- Passphrase : & sslSecret .Passphrase ,
185
+ certs [s .SSLConfig .ListenerSSLSecretName ] = loadbalancer.CertificateDetails {
186
+ CertificateName : & s .SSLConfig .ListenerSSLSecretName ,
187
+ PublicCertificate : common .String (string (sslSecret .PublicCert )),
188
+ PrivateKey : common .String (string (sslSecret .PrivateKey )),
192
189
}
193
- return nil
190
+ // Read backendSet Kubernetes Secret
191
+ sslSecret , err = s .SSLConfig .readSSLSecret (s .service .Namespace , s .SSLConfig .BackendSetSSLSecretName )
192
+ if err != nil {
193
+ return nil , errors .Wrap (err , "reading SSL BackendSet Secret" )
194
+ }
195
+ if len (sslSecret .PublicCert ) == 0 || len (sslSecret .PrivateKey ) == 0 {
196
+ return certs , nil
197
+ }
198
+
199
+ certs [s .SSLConfig .BackendSetSSLSecretName ] = loadbalancer.CertificateDetails {
200
+ CertificateName : & s .SSLConfig .BackendSetSSLSecretName ,
201
+ CaCertificate : common .String (string (sslSecret .CACert )),
202
+ PublicCertificate : common .String (string (sslSecret .PublicCert )),
203
+ PrivateKey : common .String (string (sslSecret .PrivateKey )),
204
+ Passphrase : common .String (string (sslSecret .Passphrase )),
205
+ }
206
+ return certs , nil
194
207
}
195
208
196
209
// TODO(apryde): aggregate errors using an error list.
@@ -263,7 +276,11 @@ func getBackendSets(svc *v1.Service, nodes []*v1.Node, sslCfg *SSLConfig) map[st
263
276
for _ , servicePort := range svc .Spec .Ports {
264
277
name := getBackendSetName (string (servicePort .Protocol ), int (servicePort .Port ))
265
278
port := int (servicePort .Port )
266
- sslConfig := getSSLConfiguration (sslCfg , port )
279
+ var secretName string
280
+ if sslCfg != nil {
281
+ secretName = sslCfg .BackendSetSSLSecretName
282
+ }
283
+ sslConfig := getSSLConfiguration (sslCfg , secretName , port )
267
284
if sslConfig != nil {
268
285
backendSets [name ] = loadbalancer.BackendSetDetails {
269
286
Policy : common .String (DefaultLoadBalancerPolicy ),
@@ -304,12 +321,12 @@ func getHealthChecker(cfg *SSLConfig, port int, svc *v1.Service) *loadbalancer.H
304
321
}
305
322
}
306
323
307
- func getSSLConfiguration (cfg * SSLConfig , port int ) * loadbalancer.SslConfigurationDetails {
324
+ func getSSLConfiguration (cfg * SSLConfig , name string , port int ) * loadbalancer.SslConfigurationDetails {
308
325
if cfg == nil || ! cfg .Ports .Has (port ) {
309
326
return nil
310
327
}
311
328
return & loadbalancer.SslConfigurationDetails {
312
- CertificateName : & cfg . Name ,
329
+ CertificateName : & name ,
313
330
VerifyDepth : common .Int (0 ),
314
331
VerifyPeerCertificate : common .Bool (false ),
315
332
}
@@ -347,7 +364,11 @@ func getListeners(svc *v1.Service, sslCfg *SSLConfig) (map[string]loadbalancer.L
347
364
}
348
365
}
349
366
port := int (servicePort .Port )
350
- sslConfiguration := getSSLConfiguration (sslCfg , port )
367
+ var secretName string
368
+ if sslCfg != nil {
369
+ secretName = sslCfg .ListenerSSLSecretName
370
+ }
371
+ sslConfiguration := getSSLConfiguration (sslCfg , secretName , port )
351
372
name := getListenerName (protocol , port , sslConfiguration )
352
373
353
374
listener := loadbalancer.ListenerDetails {
0 commit comments