-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logging to certpoolwatcher and client
Logging now indicates what certificate (by file and X.509 name) is being watched When an unverified certificate error is returned to the client, log the cert Signed-off-by: Todd Short <[email protected]>
- Loading branch information
Showing
4 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package httputil | ||
|
||
import ( | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/go-logr/logr" | ||
) | ||
|
||
func logPath(path, action string, log logr.Logger) { | ||
fi, err := os.Stat(path) | ||
if err != nil { | ||
log.Error(err, "error in os.Stat()", "path", path) | ||
return | ||
} | ||
if !fi.IsDir() { | ||
logFile(path, "", fmt.Sprintf("%s file", action), log) | ||
return | ||
} | ||
action = fmt.Sprintf("%s directory", action) | ||
dirEntries, err := os.ReadDir(path) | ||
if err != nil { | ||
log.Error(err, "error in os.ReadDir()", "path", path) | ||
return | ||
} | ||
for _, e := range dirEntries { | ||
file := filepath.Join(path, e.Name()) | ||
fi, err := os.Stat(file) | ||
if err != nil { | ||
log.Error(err, "error in os.Stat()", "file", file) | ||
continue | ||
} | ||
if fi.IsDir() { | ||
log.Info("ignoring subdirectory", "directory", file) | ||
continue | ||
} | ||
logFile(e.Name(), path, action, log) | ||
} | ||
} | ||
|
||
func logFile(filename, path, action string, log logr.Logger) { | ||
filepath := filepath.Join(path, filename) | ||
data, err := os.ReadFile(filepath) | ||
if err != nil { | ||
log.Error(err, "error in os.ReadFile()", "file", filename) | ||
return | ||
} | ||
logPem(data, filename, path, action, log) | ||
} | ||
|
||
func logPem(data []byte, filename, path, action string, log logr.Logger) { | ||
for len(data) > 0 { | ||
var block *pem.Block | ||
block, data = pem.Decode(data) | ||
if block == nil { | ||
log.Error(nil, "no block returned from pem.Decode()", "file", filename) | ||
return | ||
} | ||
crt, err := x509.ParseCertificate(block.Bytes) | ||
if err != nil { | ||
log.Error(err, "error in x509.ParseCertificate()", "file", filename) | ||
return | ||
} | ||
|
||
args := []any{} | ||
if path != "" { | ||
args = append(args, "directory", path) | ||
} | ||
// Find an appopriate certificate identifier | ||
args = append(args, "file", filename) | ||
if s := crt.Subject.String(); s != "" { | ||
args = append(args, "subject", s) | ||
} else if crt.DNSNames != nil { | ||
args = append(args, "DNSNames", crt.DNSNames) | ||
} else if s := crt.SerialNumber.String(); s != "" { | ||
args = append(args, "serial", s) | ||
} | ||
log.Info(action, args...) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters