Skip to content

Commit 13074ee

Browse files
committed
implement configuration option
1 parent 8b85f01 commit 13074ee

File tree

4 files changed

+61
-37
lines changed

4 files changed

+61
-37
lines changed

internal/mode/static/state/dataplane/configuration.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@ func BuildConfiguration(
5858
BackendGroups: backendGroups,
5959
SSLKeyPairs: buildSSLKeyPairs(g.ReferencedSecrets, g.Gateway.Listeners),
6060
Version: configVersion,
61-
CertBundles: buildCertBundles(g.ReferencedCaCertConfigMaps, backendGroups),
62-
Telemetry: buildTelemetry(g),
63-
BaseHTTPConfig: baseHTTPConfig,
64-
Logging: buildLogging(g),
65-
MainSnippets: buildSnippetsForContext(g.SnippetsFilters, ngfAPI.NginxContextMain),
66-
AuxiliarySecrets: buildAuxiliarySecrets(g.PlusSecrets),
61+
CertBundles: buildCertBundles(
62+
buildRefCertificateBundles(g.ReferencedSecrets, g.ReferencedCaCertConfigMaps),
63+
backendGroups),
64+
Telemetry: buildTelemetry(g),
65+
BaseHTTPConfig: baseHTTPConfig,
66+
Logging: buildLogging(g),
67+
MainSnippets: buildSnippetsForContext(g.SnippetsFilters, ngfAPI.NginxContextMain),
68+
AuxiliarySecrets: buildAuxiliarySecrets(g.PlusSecrets),
6769
}
6870

6971
return config
@@ -224,8 +226,24 @@ func buildSSLKeyPairs(
224226
return keyPairs
225227
}
226228

229+
func buildRefCertificateBundles(
230+
secrets map[types.NamespacedName]*graph.Secret,
231+
configMaps map[types.NamespacedName]*graph.CaCertConfigMap) []graph.CertificateBundle {
232+
bundles := []graph.CertificateBundle{}
233+
234+
for _, secret := range secrets {
235+
bundles = append(bundles, *secret.CertBundle)
236+
}
237+
238+
for _, configMap := range configMaps {
239+
bundles = append(bundles, *configMap.CertBundle)
240+
}
241+
242+
return bundles
243+
}
244+
227245
func buildCertBundles(
228-
caCertConfigMaps map[types.NamespacedName]*graph.CaCertConfigMap,
246+
refCertBundles []graph.CertificateBundle,
229247
backendGroups []BackendGroup,
230248
) map[CertBundleID]CertBundle {
231249
bundles := make(map[CertBundleID]CertBundle)
@@ -247,18 +265,16 @@ func buildCertBundles(
247265
}
248266
}
249267

250-
for cmName, cm := range caCertConfigMaps {
251-
id := generateCertBundleID(cmName)
268+
for _, bundle := range refCertBundles {
269+
id := generateCertBundleID(bundle.Name)
252270
if _, exists := refByBG[id]; exists {
253-
if cm.CACert != nil || len(cm.CACert) > 0 {
254-
// the cert could be base64 encoded or plaintext
255-
data := make([]byte, base64.StdEncoding.DecodedLen(len(cm.CACert)))
256-
_, err := base64.StdEncoding.Decode(data, cm.CACert)
257-
if err != nil {
258-
data = cm.CACert
259-
}
260-
bundles[id] = data
271+
// the cert could be base64 encoded or plaintext
272+
data := make([]byte, base64.StdEncoding.DecodedLen(len(bundle.Cert.CACert)))
273+
_, err := base64.StdEncoding.Decode(data, bundle.Cert.CACert)
274+
if err != nil {
275+
data = bundle.Cert.CACert
261276
}
277+
bundles[id] = data
262278
}
263279
}
264280

internal/mode/static/state/graph/certificate_bundle.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,33 @@ const CAKey = "ca.crt"
1616
type CertificateBundle struct {
1717
Name types.NamespacedName
1818
Kind v1.Kind
19+
Cert *Certificate
20+
}
1921

20-
// Required ...
22+
type Certificate struct {
2123
TLSCert []byte
2224
TLSPrivateKey []byte
25+
CACert []byte
26+
}
2327

24-
// Optional
25-
CACert []byte
28+
func NewCertificateBundle(name types.NamespacedName, kind string, cert *Certificate) *CertificateBundle {
29+
return &CertificateBundle{
30+
Name: name,
31+
Kind: v1.Kind(kind),
32+
Cert: cert,
33+
}
2634
}
2735

28-
func (cb *CertificateBundle) validate() error {
29-
_, err := tls.X509KeyPair(cb.TLSCert, cb.TLSPrivateKey)
36+
func validateTLS(tlsCert, tlsPrivateKey []byte) error {
37+
_, err := tls.X509KeyPair(tlsCert, tlsPrivateKey)
3038
if err != nil {
3139
return fmt.Errorf("TLS secret is invalid: %w", err)
3240
}
3341

34-
if err = validateCA(cb.CACert); len(cb.CACert) >= 1 && err != nil {
35-
return fmt.Errorf("Certificate in secret is invalid: %w", err)
36-
}
37-
3842
return nil
3943
}
4044

41-
// validateCA validates the ca.crt entry in the ConfigMap. If it is valid, the function returns nil.
45+
// validateCA validates the ca.crt entry in the Certificate. If it is valid, the function returns nil.
4246
func validateCA(caData []byte) error {
4347
data := make([]byte, base64.StdEncoding.DecodedLen(len(caData)))
4448
_, err := base64.StdEncoding.Decode(data, caData)

internal/mode/static/state/graph/configmaps.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ type CaCertConfigMap struct {
1313
// Source holds the actual ConfigMap resource. Can be nil if the ConfigMap does not exist.
1414
Source *apiv1.ConfigMap
1515
// CACert holds the actual CA Cert data.
16-
CACert []byte
16+
CACert []byte
17+
CertBundle *CertificateBundle
1718
}
1819

1920
type caCertConfigMapEntry struct {
@@ -44,32 +45,32 @@ func (r *configMapResolver) resolve(nsname types.NamespacedName) error {
4445
cm, exist := r.clusterConfigMaps[nsname]
4546

4647
var validationErr error
47-
var caCert []byte
48+
cert := &Certificate{}
4849

4950
if !exist {
5051
validationErr = errors.New("ConfigMap does not exist")
5152
} else {
5253
if cm.Data != nil {
5354
if _, exists := cm.Data[CAKey]; exists {
5455
validationErr = validateCA([]byte(cm.Data[CAKey]))
55-
caCert = []byte(cm.Data[CAKey])
56+
cert.CACert = []byte(cm.Data[CAKey])
5657
}
5758
}
5859
if cm.BinaryData != nil {
5960
if _, exists := cm.BinaryData[CAKey]; exists {
6061
validationErr = validateCA(cm.BinaryData[CAKey])
61-
caCert = cm.BinaryData[CAKey]
62+
cert.CACert = cm.BinaryData[CAKey]
6263
}
6364
}
64-
if len(caCert) == 0 {
65+
if len(cert.CACert) == 0 {
6566
validationErr = fmt.Errorf("ConfigMap does not have the data or binaryData field %v", CAKey)
6667
}
6768
}
6869

6970
r.resolvedCaCertConfigMaps[nsname] = &caCertConfigMapEntry{
7071
caCertConfigMap: CaCertConfigMap{
71-
Source: cm,
72-
CACert: caCert,
72+
Source: cm,
73+
CertBundle: NewCertificateBundle(nsname, "ConfigMap", cert),
7374
},
7475
err: validationErr,
7576
}

internal/mode/static/state/graph/secret.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,20 @@ func (r *secretResolver) resolve(nsname types.NamespacedName) error {
5656

5757
default:
5858
// A TLS Secret is guaranteed to have these data fields.
59-
certBundle = &CertificateBundle{
59+
cert := &Certificate{
6060
TLSCert: secret.Data[apiv1.TLSCertKey],
6161
TLSPrivateKey: secret.Data[apiv1.TLSPrivateKeyKey],
6262
}
6363

6464
// Not always guaranteed to have a ca certificate in the secret.
6565
if _, exists := secret.Data[CAKey]; exists {
66-
certBundle.CACert = secret.Data[CAKey]
66+
cert.CACert = secret.Data[CAKey]
6767
}
6868

69-
validationErr = certBundle.validate()
69+
validationErr = validateTLS(cert.TLSCert, cert.TLSPrivateKey)
70+
validationErr = validateCA(cert.CACert)
71+
72+
certBundle = NewCertificateBundle(nsname, secret.Kind, cert)
7073
}
7174

7275
r.resolvedSecrets[nsname] = &secretEntry{

0 commit comments

Comments
 (0)