Skip to content

Commit b943a13

Browse files
committed
Add TLS listener with app internal routes in SAN to Envoy
Add another listener to Envoy that proxies port 61443 to 8080 inside of container. It serves the SSL certificate that contains SAN with all application internal routes. [#180173340](https://www.pivotaltracker.com/story/show/180173340)
1 parent 2d5aae4 commit b943a13

File tree

9 files changed

+513
-289
lines changed

9 files changed

+513
-289
lines changed

depot/containerstore/containerstorefakes/fake_cred_handler.go

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

depot/containerstore/containerstorefakes/fake_proxymanager.go

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

depot/containerstore/credmanager.go

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"code.cloudfoundry.org/executor"
2222
"code.cloudfoundry.org/garden"
2323
"code.cloudfoundry.org/lager"
24+
"code.cloudfoundry.org/routing-info/internalroutes"
2425
)
2526

2627
const (
@@ -29,6 +30,11 @@ const (
2930
CredCreationFailedCount = "CredCreationFailedCount"
3031
)
3132

33+
type Credentials struct {
34+
InstanceIdentityCredential Credential
35+
C2CCredential Credential
36+
}
37+
3238
type Credential struct {
3339
Cert string
3440
Key string
@@ -85,12 +91,12 @@ type CredentialHandler interface {
8591
RemoveDir(logger lager.Logger, container executor.Container) error
8692

8793
// Called periodically as new valid certificate/key pair are generated
88-
Update(credentials Credential, container executor.Container) error
94+
Update(credentials Credentials, container executor.Container) error
8995

9096
// Called when the CredManager is preparing to exit. This is mainly to update
9197
// the EnvoyProxy with invalid certificates and prevent it from accepting
9298
// more incoming traffic from the gorouter
93-
Close(invalidCredentials Credential, container executor.Container) error
99+
Close(invalidCredentials Credentials, container executor.Container) error
94100
}
95101

96102
func NewCredManager(
@@ -242,30 +248,54 @@ const (
242248
privateKeyPEMBlockType = "RSA PRIVATE KEY"
243249
)
244250

245-
func (c *credManager) generateCreds(logger lager.Logger, container executor.Container, certGUID string) (Credential, error) {
251+
func (c *credManager) generateCreds(logger lager.Logger, container executor.Container, certGUID string) (Credentials, error) {
246252
logger = logger.Session("generating-credentials")
247253
logger.Debug("starting")
248254
defer logger.Debug("complete")
249255

256+
ipForCert := container.InternalIP
257+
if len(ipForCert) == 0 {
258+
ipForCert = container.ExternalIP
259+
}
260+
261+
logger.Debug("generating-credentials-for-instance-identity")
262+
idCred, err := c.generateCredForSAN(logger,
263+
certificateSAN{IPAddress: ipForCert, OrganizationalUnits: container.CertificateProperties.OrganizationalUnit},
264+
certGUID,
265+
)
266+
if err != nil {
267+
return Credentials{}, err
268+
}
269+
270+
logger.Debug("generating-credentials-for-c2c")
271+
c2cCred, err := c.generateCredForSAN(logger,
272+
certificateSAN{InternalRoutes: container.InternalRoutes, OrganizationalUnits: container.CertificateProperties.OrganizationalUnit},
273+
certGUID,
274+
)
275+
if err != nil {
276+
return Credentials{}, err
277+
}
278+
279+
return Credentials{
280+
InstanceIdentityCredential: idCred,
281+
C2CCredential: c2cCred,
282+
}, nil
283+
}
284+
285+
func (c *credManager) generateCredForSAN(logger lager.Logger, certSAN certificateSAN, certGUID string) (Credential, error) {
250286
logger.Debug("generating-private-key")
251287
privateKey, err := rsa.GenerateKey(c.entropyReader, 2048)
252288
if err != nil {
253289
return Credential{}, err
254290
}
255291
logger.Debug("generated-private-key")
256292

257-
ipForCert := container.InternalIP
258-
if len(ipForCert) == 0 {
259-
ipForCert = container.ExternalIP
260-
}
261-
262293
startValidity := c.clock.Now()
263294

264-
template := createCertificateTemplate(ipForCert,
265-
certGUID,
295+
template := createCertificateTemplate(certGUID,
296+
certSAN,
266297
startValidity,
267298
startValidity.Add(c.validityPeriod),
268-
container.CertificateProperties.OrganizationalUnit,
269299
)
270300

271301
logger.Debug("generating-serial-number")
@@ -306,11 +336,10 @@ func (c *credManager) generateCreds(logger lager.Logger, container executor.Cont
306336
return Credential{}, err
307337
}
308338

309-
creds := Credential{
339+
return Credential{
310340
Cert: certificateBuf.String(),
311341
Key: keyBuf.String(),
312-
}
313-
return creds, nil
342+
}, nil
314343
}
315344

316345
func pemEncode(bytes []byte, blockType string, writer io.Writer) error {
@@ -321,21 +350,32 @@ func pemEncode(bytes []byte, blockType string, writer io.Writer) error {
321350
return pem.Encode(writer, block)
322351
}
323352

324-
func createCertificateTemplate(ipaddress, guid string, notBefore, notAfter time.Time, organizationalUnits []string) *x509.Certificate {
353+
type certificateSAN struct {
354+
IPAddress string
355+
InternalRoutes internalroutes.InternalRoutes
356+
OrganizationalUnits []string
357+
}
358+
359+
func createCertificateTemplate(guid string, certSAN certificateSAN, notBefore, notAfter time.Time) *x509.Certificate {
325360
var ipaddr []net.IP
326-
if len(ipaddress) == 0 {
361+
if len(certSAN.IPAddress) == 0 {
327362
ipaddr = []net.IP{}
328363
} else {
329-
ipaddr = []net.IP{net.ParseIP(ipaddress)}
364+
ipaddr = []net.IP{net.ParseIP(certSAN.IPAddress)}
365+
}
366+
dnsNames := []string{guid}
367+
for _, route := range certSAN.InternalRoutes {
368+
dnsNames = append(dnsNames, route.Hostname)
330369
}
370+
331371
return &x509.Certificate{
332372
SerialNumber: big.NewInt(0),
333373
Subject: pkix.Name{
334374
CommonName: guid,
335-
OrganizationalUnit: organizationalUnits,
375+
OrganizationalUnit: certSAN.OrganizationalUnits,
336376
},
337377
IPAddresses: ipaddr,
338-
DNSNames: []string{guid},
378+
DNSNames: dnsNames,
339379
NotBefore: notBefore,
340380
NotAfter: notAfter,
341381
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment | x509.KeyUsageKeyAgreement,

0 commit comments

Comments
 (0)