@@ -2,46 +2,43 @@ package parser
22
33import (
44 "crypto/x509"
5+ "encoding/json"
56 "encoding/pem"
67 "errors"
8+ "fmt"
79 "time"
810
911 "github.com/kong/go-kong/kong"
10- "github.com/sirupsen/logrus"
1112 corev1 "k8s.io/api/core/v1"
13+ v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
14+ "sigs.k8s.io/controller-runtime/pkg/client"
1215
13- "github.com/kong/kubernetes-ingress-controller/v2/internal/dataplane/kongstate"
1416 "github.com/kong/kubernetes-ingress-controller/v2/internal/store"
1517)
1618
1719// getCACerts translates CA certificates Secrets to kong.CACertificates. It ensures every certificate's structure and
18- // validity. In case of violation of any validation rule, a secret gets skipped in a result and error message is logged
19- // with affected plugins for context.
20- func getCACerts (log logrus.FieldLogger , storer store.Storer , plugins []kongstate.Plugin ) []kong.CACertificate {
21- caCertSecrets , err := storer .ListCACerts ()
20+ // validity. It skips Secrets that do not contain a valid certificate and reports translation failures for them.
21+ func (p * Parser ) getCACerts () []kong.CACertificate {
22+ caCertSecrets , err := p .storer .ListCACerts ()
2223 if err != nil {
23- log .WithError (err ).Error ("failed to list CA certs" )
24+ p . logger .WithError (err ).Error ("failed to list CA certs" )
2425 return nil
2526 }
2627
2728 var caCerts []kong.CACertificate
2829 for _ , certSecret := range caCertSecrets {
29- log := log .WithFields (logrus.Fields {
30- "secret_name" : certSecret .Name ,
31- "secret_namespace" : certSecret .Namespace ,
32- })
33-
3430 idBytes , ok := certSecret .Data ["id" ]
3531 if ! ok {
36- log . Error ( "skipping synchronisation, invalid CA certificate: missing 'id' field in data" )
32+ p . registerTranslationFailure ( " invalid CA certificate: missing 'id' field in data", certSecret )
3733 continue
3834 }
3935 secretID := string (idBytes )
4036
4137 caCert , err := toKongCACertificate (certSecret , secretID )
4238 if err != nil {
43- logWithAffectedPlugins (log , plugins , secretID ).WithError (err ).
44- Error ("skipping synchronisation, invalid CA certificate" )
39+ relatedObjects := getPluginsAssociatedWithCACertSecret (secretID , p .storer )
40+ relatedObjects = append (relatedObjects , certSecret .DeepCopy ())
41+ p .registerTranslationFailure (fmt .Sprintf ("invalid CA certificate: %s" , err ), relatedObjects ... )
4542 continue
4643 }
4744
@@ -77,30 +74,33 @@ func toKongCACertificate(certSecret *corev1.Secret, secretID string) (kong.CACer
7774 }, nil
7875}
7976
80- func logWithAffectedPlugins (log logrus.FieldLogger , plugins []kongstate.Plugin , secretID string ) logrus.FieldLogger {
81- affectedPlugins := getPluginsAssociatedWithCACertSecret (plugins , secretID )
82- return log .WithField ("affected_plugins" , affectedPlugins )
83- }
84-
85- func getPluginsAssociatedWithCACertSecret (plugins []kongstate.Plugin , secretID string ) []string {
86- refersToSecret := func (pluginConfig map [string ]interface {}) bool {
87- caCertReferences , ok := pluginConfig ["ca_certificates" ].([]string )
88- if ! ok {
77+ func getPluginsAssociatedWithCACertSecret (secretID string , storer store.Storer ) []client.Object {
78+ refersToSecret := func (pluginConfig v1.JSON ) bool {
79+ cfg := struct {
80+ CACertificates []string `json:"ca_certificates,omitempty"`
81+ }{}
82+ err := json .Unmarshal (pluginConfig .Raw , & cfg )
83+ if err != nil {
8984 return false
9085 }
9186
92- for _ , reference := range caCertReferences {
87+ for _ , reference := range cfg . CACertificates {
9388 if reference == secretID {
9489 return true
9590 }
9691 }
9792 return false
9893 }
9994
100- var affectedPlugins []string
101- for _ , p := range plugins {
102- if refersToSecret (p .Config ) && p .Name != nil {
103- affectedPlugins = append (affectedPlugins , * p .Name )
95+ var affectedPlugins []client.Object
96+ for _ , p := range storer .ListKongPlugins () {
97+ if refersToSecret (p .Config ) {
98+ affectedPlugins = append (affectedPlugins , p .DeepCopy ())
99+ }
100+ }
101+ for _ , p := range storer .ListKongClusterPlugins () {
102+ if refersToSecret (p .Config ) {
103+ affectedPlugins = append (affectedPlugins , p .DeepCopy ())
104104 }
105105 }
106106
0 commit comments