Skip to content

Commit

Permalink
added basic TLS logging to all servers
Browse files Browse the repository at this point in the history
  • Loading branch information
johnabass committed Nov 20, 2024
1 parent 97777d1 commit 5a66612
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 7 deletions.
11 changes: 4 additions & 7 deletions xhttp/xhttpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,6 @@ type Options struct {
DisableHandlerLogger bool
}

func logTLSInformation(l *zap.Logger, r *http.Request) {
l.Info(
"TLS information",
)
}

// NewServerChain produces the standard constructor chain for a server, primarily using configuration.
func NewServerChain(o Options, l *zap.Logger, fbs ...sallusthttp.FieldBuilder) alice.Chain {
bs := sallusthttp.Builders{}
Expand All @@ -74,7 +68,10 @@ func NewServerChain(o Options, l *zap.Logger, fbs ...sallusthttp.FieldBuilder) a
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(response http.ResponseWriter, request *http.Request) {
requestLogger := bs.Build(request, l)
logTLSInformation(requestLogger, request)
requestLogger.Info(
"tls info",
connectionStateField("state", request.TLS),
)

next.ServeHTTP(
response,
Expand Down
104 changes: 104 additions & 0 deletions xhttp/xhttpserver/zap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-FileCopyrightText: 2017 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package xhttpserver

import (
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type stringArray []string

func (sa stringArray) MarshalLogArray(enc zapcore.ArrayEncoder) error {
for _, s := range sa {
enc.AppendString(s)
}

Check warning on line 19 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L16-L19

Added lines #L16 - L19 were not covered by tests

return nil

Check warning on line 21 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L21

Added line #L21 was not covered by tests
}

type pkixName pkix.Name

func (pn pkixName) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddArray("organization", stringArray(pn.Organization))
enc.AddArray("organizationalUnit", stringArray(pn.OrganizationalUnit))
enc.AddString("commonName", pn.CommonName)
enc.AddString("serialNumber", pn.SerialNumber)

return nil

Check warning on line 32 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L26-L32

Added lines #L26 - L32 were not covered by tests
}

type certificate x509.Certificate

func (c certificate) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddObject("issuer", pkixName(c.Issuer))
enc.AddObject("subject", pkixName(c.Subject))
enc.AddArray("dnsNames", stringArray(c.DNSNames))
enc.AddArray("emailAddresses", stringArray(c.EmailAddresses))
enc.AddArray("issuingCertificateURL", stringArray(c.IssuingCertificateURL))
enc.AddTime("notBefore", c.NotBefore)
enc.AddTime("notAfter", c.NotAfter)

if c.SerialNumber != nil {
enc.AddString("serialNumber", c.SerialNumber.String())
} else {
enc.AddString("serialNumber", "<none>")
}

Check warning on line 50 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L37-L50

Added lines #L37 - L50 were not covered by tests

return nil

Check warning on line 52 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L52

Added line #L52 was not covered by tests
}

type certificates []*x509.Certificate

func (cs certificates) MarshalLogArray(enc zapcore.ArrayEncoder) error {
for _, c := range cs {
if c != nil {
enc.AppendObject(certificate(*c))
}

Check warning on line 61 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L57-L61

Added lines #L57 - L61 were not covered by tests
}

return nil

Check warning on line 64 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L64

Added line #L64 was not covered by tests
}

func tlsVersionToString(v uint16) string {
switch v {
case tls.VersionTLS10:
return "1.0"

Check warning on line 70 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L67-L70

Added lines #L67 - L70 were not covered by tests

case tls.VersionTLS11:
return "1.1"

Check warning on line 73 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L72-L73

Added lines #L72 - L73 were not covered by tests

case tls.VersionTLS12:
return "1.2"

Check warning on line 76 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L75-L76

Added lines #L75 - L76 were not covered by tests

case tls.VersionTLS13:
return "1.3"

Check warning on line 79 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L78-L79

Added lines #L78 - L79 were not covered by tests

case tls.VersionSSL30: //nolint:staticcheck
return "SSLv3"

Check warning on line 82 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L81-L82

Added lines #L81 - L82 were not covered by tests

default:
return "unknown"

Check warning on line 85 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L84-L85

Added lines #L84 - L85 were not covered by tests
}
}

type connectionState tls.ConnectionState

func (cstate connectionState) MarshalLogObject(enc zapcore.ObjectEncoder) error {
enc.AddString("version", tlsVersionToString(cstate.Version))
enc.AddArray("peerCertificates", certificates(cstate.PeerCertificates))

return nil

Check warning on line 95 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L91-L95

Added lines #L91 - L95 were not covered by tests
}

func connectionStateField(field string, v *tls.ConnectionState) zap.Field {
if v != nil {
return zap.Object(field, connectionState(*v))

Check warning on line 100 in xhttp/xhttpserver/zap.go

View check run for this annotation

Codecov / codecov/patch

xhttp/xhttpserver/zap.go#L100

Added line #L100 was not covered by tests
} else {
return zap.Skip()
}
}

0 comments on commit 5a66612

Please sign in to comment.