|
1 | 1 | package k8s |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "crypto/x509" |
| 5 | + "encoding/pem" |
4 | 6 | "fmt" |
5 | 7 | "time" |
6 | 8 |
|
7 | 9 | "github.com/go-logr/logr" |
8 | 10 | "github.com/pmylund/go-cache" |
| 11 | + corev1 "k8s.io/api/core/v1" |
9 | 12 | "k8s.io/apimachinery/pkg/types" |
10 | 13 |
|
11 | 14 | "github.com/jetstack/preflight/api" |
@@ -39,9 +42,113 @@ func logCacheUpdateFailure(log logr.Logger, obj interface{}, operation string) { |
39 | 42 | log.Error(err, "Cache update failure", "operation", operation) |
40 | 43 | } |
41 | 44 |
|
| 45 | +// cacheFilterFunction is a function that takes an object and returns true if the |
| 46 | +// object should not be added to the cache, false otherwise. |
| 47 | +// This can be used to filter out objects that are not relevant for the data gatherer. |
| 48 | +type cacheFilterFunction func(interface{}) bool |
| 49 | + |
| 50 | +// IgnoreTLSSecretsContainingNonClientCertificates filters out all TLS secrets that do not |
| 51 | +// contain a client certificate in the `tls.crt` key. |
| 52 | +func IgnoreTLSSecretsContainingNonClientCertificates(obj interface{}) bool { |
| 53 | + _, ok := obj.(cacheResource) |
| 54 | + if !ok { |
| 55 | + // Not a cacheResource, skip it |
| 56 | + // Programming error if this happens |
| 57 | + return false |
| 58 | + } |
| 59 | + secret, ok := obj.(*corev1.Secret) |
| 60 | + if !ok { |
| 61 | + // Not a Secret, skip it |
| 62 | + return false |
| 63 | + } |
| 64 | + // Check if the secret contains the `tls.crt` key |
| 65 | + data, found := secret.Data[corev1.TLSCertKey] |
| 66 | + // If the key is not found or the data is empty, skip it |
| 67 | + |
| 68 | + if !found || len(data) == 0 { |
| 69 | + return false |
| 70 | + } |
| 71 | + |
| 72 | + // Try to parse the data as a PEM encoded X.509 certificate chain |
| 73 | + certs, err := parsePEMCertificateChain(data) |
| 74 | + if err != nil { |
| 75 | + return false |
| 76 | + } |
| 77 | + |
| 78 | + if len(certs) == 0 { |
| 79 | + return false |
| 80 | + } |
| 81 | + |
| 82 | + // Check if the leaf certificate is a client certificate |
| 83 | + if isClientCertificate(certs[0]) { |
| 84 | + return false |
| 85 | + } |
| 86 | + |
| 87 | + // Not a client certificate, drop it |
| 88 | + return true |
| 89 | +} |
| 90 | + |
| 91 | +// isClientCertificate checks if the given certificate is a client certificate |
| 92 | +// by checking if it has the ClientAuth EKU. |
| 93 | +func isClientCertificate(cert *x509.Certificate) bool { |
| 94 | + if cert == nil { |
| 95 | + return false |
| 96 | + } |
| 97 | + // Check if the certificate has the ClientAuth EKU |
| 98 | + for _, eku := range cert.ExtKeyUsage { |
| 99 | + if eku == x509.ExtKeyUsageClientAuth { |
| 100 | + return true |
| 101 | + } |
| 102 | + } |
| 103 | + return false |
| 104 | +} |
| 105 | + |
| 106 | +// parsePEMCertificateChain parses a PEM encoded certificate chain and returns |
| 107 | +// a slice of x509.Certificate pointers. It returns an error if the data cannot |
| 108 | +// be parsed as a certificate chain. |
| 109 | +// The supplied data can contain multiple PEM blocks, the function will parse |
| 110 | +// all of them and return a slice of certificates. |
| 111 | +func parsePEMCertificateChain(data []byte) ([]*x509.Certificate, error) { |
| 112 | + // Parse the PEM encoded certificate chain |
| 113 | + var certs []*x509.Certificate |
| 114 | + var block *pem.Block |
| 115 | + rest := data |
| 116 | + for { |
| 117 | + block, rest = pem.Decode(rest) |
| 118 | + if block == nil { |
| 119 | + break |
| 120 | + } |
| 121 | + if block.Type != "CERTIFICATE" || len(block.Bytes) == 0 { |
| 122 | + continue |
| 123 | + } |
| 124 | + cert, err := x509.ParseCertificate(block.Bytes) |
| 125 | + if err != nil { |
| 126 | + return nil, fmt.Errorf("failed to parse certificate: %w", err) |
| 127 | + } |
| 128 | + certs = append(certs, cert) |
| 129 | + } |
| 130 | + if len(certs) == 0 { |
| 131 | + return nil, fmt.Errorf("no certificates found") |
| 132 | + } |
| 133 | + return certs, nil |
| 134 | +} |
| 135 | + |
42 | 136 | // onAdd handles the informer creation events, adding the created runtime.Object |
43 | 137 | // to the data gatherer's cache. The cache key is the uid of the object |
44 | | -func onAdd(log logr.Logger, obj interface{}, dgCache *cache.Cache) { |
| 138 | +// The object is wrapped in a GatheredResource struct. |
| 139 | +// If the object is already present in the cache, it gets replaced. |
| 140 | +// The cache key is the uid of the object |
| 141 | +// The supplied filter functions can be used to filter out objects that |
| 142 | +// should not be added to the cache. |
| 143 | +// If multiple filter functions are supplied, the object is filtered out |
| 144 | +// if any of the filter functions returns true. |
| 145 | +func onAdd(log logr.Logger, obj interface{}, dgCache *cache.Cache, filters ...cacheFilterFunction) { |
| 146 | + for _, filter := range filters { |
| 147 | + if filter != nil && filter(obj) { |
| 148 | + return |
| 149 | + } |
| 150 | + } |
| 151 | + |
45 | 152 | item, ok := obj.(cacheResource) |
46 | 153 | if ok { |
47 | 154 | cacheObject := &api.GatheredResource{ |
|
0 commit comments