|
| 1 | +package httputil |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "encoding/pem" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + |
| 12 | + "github.com/go-logr/logr" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + defaultLogLevel = 4 |
| 17 | +) |
| 18 | + |
| 19 | +// Log the certificates that would be used for docker pull operations |
| 20 | +// Assumes a /etc/docker/certs.d like path, where the directory contains |
| 21 | +// <hostname>:<port> directories in which a CA certificate (generally |
| 22 | +// named "ca.crt") is located. |
| 23 | +func LogDockerCertificates(path string, log logr.Logger) { |
| 24 | + // These are the default paths that containers/images looks at for host:port certs |
| 25 | + // See containers/images: docker/docker_client.go |
| 26 | + paths := []string{"/etc/docker/certs.d", "/etc/containers/certs.d"} |
| 27 | + if path != "" { |
| 28 | + paths = []string{path} |
| 29 | + } |
| 30 | + for _, path := range paths { |
| 31 | + fi, err := os.Stat(path) |
| 32 | + if err != nil { |
| 33 | + log.Error(err, "statting directory", "directory", path) |
| 34 | + continue |
| 35 | + } |
| 36 | + if !fi.IsDir() { |
| 37 | + log.V(defaultLogLevel+1).Info("not a directory", "directory", path) |
| 38 | + continue |
| 39 | + } |
| 40 | + dirEntries, err := os.ReadDir(path) |
| 41 | + if err != nil { |
| 42 | + log.Error(err, "reading directory", "directory", path) |
| 43 | + continue |
| 44 | + } |
| 45 | + for _, e := range dirEntries { |
| 46 | + hostPath := filepath.Join(path, e.Name()) |
| 47 | + fi, err := os.Stat(hostPath) |
| 48 | + if err != nil { |
| 49 | + log.Error(err, "dumping certs", "path", hostPath) |
| 50 | + continue |
| 51 | + } |
| 52 | + if !fi.IsDir() { |
| 53 | + log.V(defaultLogLevel+1).Info("ignoring non-directory", "path", hostPath) |
| 54 | + continue |
| 55 | + } |
| 56 | + logPath(hostPath, "dump docker certs", log) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// This function unwraps the given error to find an CertificateVerificationError. |
| 62 | +// It then logs the list of certificates found in the unwrapped error |
| 63 | +// Returns: |
| 64 | +// * true if a CertificateVerificationError is found |
| 65 | +// * false if no CertificateVerificationError is found |
| 66 | +func LogCertificateVerificationError(err error, log logr.Logger) bool { |
| 67 | + for err != nil { |
| 68 | + var cvErr *tls.CertificateVerificationError |
| 69 | + if errors.As(err, &cvErr) { |
| 70 | + n := 1 |
| 71 | + for _, cert := range cvErr.UnverifiedCertificates { |
| 72 | + log.Error(err, "unverified cert", "n", n, "subject", cert.Subject, "issuer", cert.Issuer, "DNSNames", cert.DNSNames, "serial", cert.SerialNumber) |
| 73 | + n = n + 1 |
| 74 | + } |
| 75 | + return true |
| 76 | + } |
| 77 | + err = errors.Unwrap(err) |
| 78 | + } |
| 79 | + return false |
| 80 | +} |
| 81 | + |
| 82 | +func logPath(path, action string, log logr.Logger) { |
| 83 | + fi, err := os.Stat(path) |
| 84 | + if err != nil { |
| 85 | + log.Error(err, "error in os.Stat()", "path", path) |
| 86 | + return |
| 87 | + } |
| 88 | + if !fi.IsDir() { |
| 89 | + logFile(path, "", fmt.Sprintf("%s file", action), log) |
| 90 | + return |
| 91 | + } |
| 92 | + action = fmt.Sprintf("%s directory", action) |
| 93 | + dirEntries, err := os.ReadDir(path) |
| 94 | + if err != nil { |
| 95 | + log.Error(err, "error in os.ReadDir()", "path", path) |
| 96 | + return |
| 97 | + } |
| 98 | + for _, e := range dirEntries { |
| 99 | + file := filepath.Join(path, e.Name()) |
| 100 | + fi, err := os.Stat(file) |
| 101 | + if err != nil { |
| 102 | + log.Error(err, "error in os.Stat()", "file", file) |
| 103 | + continue |
| 104 | + } |
| 105 | + if fi.IsDir() { |
| 106 | + log.V(defaultLogLevel+1).Info("ignoring subdirectory", "directory", file) |
| 107 | + continue |
| 108 | + } |
| 109 | + logFile(e.Name(), path, action, log) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func logFile(filename, path, action string, log logr.Logger) { |
| 114 | + filepath := filepath.Join(path, filename) |
| 115 | + _, err := os.Stat(filepath) |
| 116 | + if err != nil { |
| 117 | + log.Error(err, "statting file", "file", filepath) |
| 118 | + return |
| 119 | + } |
| 120 | + data, err := os.ReadFile(filepath) |
| 121 | + if err != nil { |
| 122 | + log.Error(err, "error in os.ReadFile()", "file", filepath) |
| 123 | + return |
| 124 | + } |
| 125 | + logPem(data, filename, path, action, log) |
| 126 | +} |
| 127 | + |
| 128 | +func logPem(data []byte, filename, path, action string, log logr.Logger) { |
| 129 | + for len(data) > 0 { |
| 130 | + var block *pem.Block |
| 131 | + block, data = pem.Decode(data) |
| 132 | + if block == nil { |
| 133 | + log.Info("error: no block returned from pem.Decode()", "file", filename) |
| 134 | + return |
| 135 | + } |
| 136 | + crt, err := x509.ParseCertificate(block.Bytes) |
| 137 | + if err != nil { |
| 138 | + log.Error(err, "error in x509.ParseCertificate()", "file", filename) |
| 139 | + return |
| 140 | + } |
| 141 | + |
| 142 | + args := []any{} |
| 143 | + if path != "" { |
| 144 | + args = append(args, "directory", path) |
| 145 | + } |
| 146 | + // Find an appopriate certificate identifier |
| 147 | + args = append(args, "file", filename) |
| 148 | + if s := crt.Subject.String(); s != "" { |
| 149 | + args = append(args, "subject", s) |
| 150 | + } else if crt.DNSNames != nil { |
| 151 | + args = append(args, "DNSNames", crt.DNSNames) |
| 152 | + } else if s := crt.SerialNumber.String(); s != "" { |
| 153 | + args = append(args, "serial", s) |
| 154 | + } |
| 155 | + log.V(defaultLogLevel).Info(action, args...) |
| 156 | + } |
| 157 | +} |
0 commit comments