|
| 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 = 0 // 4 |
| 17 | +) |
| 18 | + |
| 19 | +func DumpCertificates(path string, log logr.Logger) { |
| 20 | + paths := []string{} |
| 21 | + if path == "" { |
| 22 | + paths = []string{"/etc/docker/certs.d", "/etc/containers/certs.d"} |
| 23 | + } else { |
| 24 | + paths = []string{path} |
| 25 | + } |
| 26 | + for _, path := range paths { |
| 27 | + fi, err := os.Stat(path) |
| 28 | + if err != nil { |
| 29 | + log.Error(err, "statting directory", "directory", path) |
| 30 | + continue |
| 31 | + } |
| 32 | + if !fi.IsDir() { |
| 33 | + log.V(defaultLogLevel).V(1).Info("not a directory", "directory", path) |
| 34 | + continue |
| 35 | + } |
| 36 | + dirEntries, err := os.ReadDir(path) |
| 37 | + if err != nil { |
| 38 | + log.Error(err, "reading directory", "directory", path) |
| 39 | + continue |
| 40 | + } |
| 41 | + count := 0 |
| 42 | + for _, e := range dirEntries { |
| 43 | + hostPath := filepath.Join(path, e.Name()) |
| 44 | + fi, err := os.Stat(hostPath) |
| 45 | + count++ |
| 46 | + if err != nil { |
| 47 | + log.Error(err, "dumping certs", "path", hostPath) |
| 48 | + continue |
| 49 | + } |
| 50 | + if !fi.IsDir() { |
| 51 | + log.V(defaultLogLevel).V(1).Info("ignoring non-directory", "path", hostPath) |
| 52 | + continue |
| 53 | + } |
| 54 | + logPath(hostPath, "dump docker certs", log) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func LogUnverifiedCertificate(err error, log logr.Logger) bool { |
| 60 | + for err != nil { |
| 61 | + var cvErr *tls.CertificateVerificationError |
| 62 | + if errors.As(err, &cvErr) { |
| 63 | + n := 1 |
| 64 | + for _, cert := range cvErr.UnverifiedCertificates { |
| 65 | + log.Error(err, "unverified cert", "n", n, "subject", cert.Subject, "issuer", cert.Issuer, "DNSNames", cert.DNSNames, "serial", cert.SerialNumber) |
| 66 | + n = n + 1 |
| 67 | + } |
| 68 | + return true |
| 69 | + } |
| 70 | + err = errors.Unwrap(err) |
| 71 | + } |
| 72 | + return false |
| 73 | +} |
| 74 | + |
| 75 | +func logPath(path, action string, log logr.Logger) { |
| 76 | + fi, err := os.Stat(path) |
| 77 | + if err != nil { |
| 78 | + log.Error(err, "error in os.Stat()", "path", path) |
| 79 | + return |
| 80 | + } |
| 81 | + if !fi.IsDir() { |
| 82 | + logFile(path, "", fmt.Sprintf("%s file", action), log) |
| 83 | + return |
| 84 | + } |
| 85 | + action = fmt.Sprintf("%s directory", action) |
| 86 | + dirEntries, err := os.ReadDir(path) |
| 87 | + if err != nil { |
| 88 | + log.Error(err, "error in os.ReadDir()", "path", path) |
| 89 | + return |
| 90 | + } |
| 91 | + for _, e := range dirEntries { |
| 92 | + file := filepath.Join(path, e.Name()) |
| 93 | + fi, err := os.Stat(file) |
| 94 | + if err != nil { |
| 95 | + log.Error(err, "error in os.Stat()", "file", file) |
| 96 | + continue |
| 97 | + } |
| 98 | + if fi.IsDir() { |
| 99 | + log.V(defaultLogLevel).V(1).Info("ignoring subdirectory", "directory", file) |
| 100 | + continue |
| 101 | + } |
| 102 | + logFile(e.Name(), path, action, log) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func logFile(filename, path, action string, log logr.Logger) { |
| 107 | + filepath := filepath.Join(path, filename) |
| 108 | + _, err := os.Stat(filepath) |
| 109 | + if err != nil { |
| 110 | + log.Error(err, "statting file", "file", filepath) |
| 111 | + return |
| 112 | + } |
| 113 | + data, err := os.ReadFile(filepath) |
| 114 | + if err != nil { |
| 115 | + log.Error(err, "error in os.ReadFile()", "file", filepath) |
| 116 | + return |
| 117 | + } |
| 118 | + logPem(data, filename, path, action, log) |
| 119 | +} |
| 120 | + |
| 121 | +func logPem(data []byte, filename, path, action string, log logr.Logger) { |
| 122 | + for len(data) > 0 { |
| 123 | + var block *pem.Block |
| 124 | + block, data = pem.Decode(data) |
| 125 | + if block == nil { |
| 126 | + log.Info("error: no block returned from pem.Decode()", "file", filename) |
| 127 | + return |
| 128 | + } |
| 129 | + crt, err := x509.ParseCertificate(block.Bytes) |
| 130 | + if err != nil { |
| 131 | + log.Error(err, "error in x509.ParseCertificate()", "file", filename) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + args := []any{} |
| 136 | + if path != "" { |
| 137 | + args = append(args, "directory", path) |
| 138 | + } |
| 139 | + // Find an appopriate certificate identifier |
| 140 | + args = append(args, "file", filename) |
| 141 | + if s := crt.Subject.String(); s != "" { |
| 142 | + args = append(args, "subject", s) |
| 143 | + } else if crt.DNSNames != nil { |
| 144 | + args = append(args, "DNSNames", crt.DNSNames) |
| 145 | + } else if s := crt.SerialNumber.String(); s != "" { |
| 146 | + args = append(args, "serial", s) |
| 147 | + } |
| 148 | + log.V(defaultLogLevel).Info(action, args...) |
| 149 | + } |
| 150 | +} |
0 commit comments