-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
After switch to Go's http/server, the peer address comes wrapped, so use a different method to unwrap it. The tests haven't caught that, as they were using gRPC's server, so switch tests to use same approach as production, ans enable HTTP/2 over TLS, as otherwise h2c is a mess, and it doesn't abort connections properly for test purposes. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
8 changed files
with
219 additions
and
70 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
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,53 @@ | ||
// Copyright (c) 2024 Sidero Labs, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
|
||
// Package grpclog provides a logger that logs to a zap logger. | ||
package grpclog | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Adapter returns a logging.Logger that logs to the provided zap logger. | ||
func Adapter(l *zap.Logger) logging.Logger { | ||
return logging.LoggerFunc(func(_ context.Context, lvl logging.Level, msg string, fields ...any) { | ||
f := make([]zap.Field, 0, len(fields)/2) | ||
|
||
for i := 0; i < len(fields); i += 2 { | ||
key := fields[i].(string) //nolint:forcetypeassert,errcheck | ||
value := fields[i+1] | ||
|
||
switch v := value.(type) { | ||
case string: | ||
f = append(f, zap.String(key, v)) | ||
case int: | ||
f = append(f, zap.Int(key, v)) | ||
case bool: | ||
f = append(f, zap.Bool(key, v)) | ||
default: | ||
f = append(f, zap.Any(key, v)) | ||
} | ||
} | ||
|
||
logger := l.WithOptions(zap.AddCallerSkip(1)).With(f...) | ||
|
||
switch lvl { | ||
case logging.LevelDebug: | ||
logger.Debug(msg) | ||
case logging.LevelInfo: | ||
logger.Info(msg) | ||
case logging.LevelWarn: | ||
logger.Warn(msg) | ||
case logging.LevelError: | ||
logger.Error(msg) | ||
default: | ||
panic(fmt.Sprintf("unknown level %v", lvl)) | ||
} | ||
}) | ||
} |
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,97 @@ | ||
// Copyright (c) 2024 Sidero Labs, Inc. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
|
||
package server_test | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"math/big" | ||
"net" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var onceCert = sync.OnceValues(func() (*tls.Certificate, error) { | ||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
keyUsage := x509.KeyUsageDigitalSignature | ||
notBefore := time.Now() | ||
notAfter := notBefore.Add(time.Hour) | ||
|
||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) | ||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
template := x509.Certificate{ | ||
SerialNumber: serialNumber, | ||
Subject: pkix.Name{ | ||
Organization: []string{"SideroLabs Testing"}, | ||
}, | ||
NotBefore: notBefore, | ||
NotAfter: notAfter, | ||
|
||
KeyUsage: keyUsage, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback}, | ||
} | ||
|
||
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cert := tls.Certificate{ | ||
Certificate: [][]byte{derBytes}, | ||
PrivateKey: priv, | ||
} | ||
|
||
cert.Leaf, err = x509.ParseCertificate(derBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &cert, nil | ||
}) | ||
|
||
func GetServerTLSConfig(t *testing.T) *tls.Config { | ||
t.Helper() | ||
|
||
cert, err := onceCert() | ||
require.NoError(t, err) | ||
|
||
return &tls.Config{ | ||
Certificates: []tls.Certificate{*cert}, | ||
MinVersion: tls.VersionTLS12, | ||
} | ||
} | ||
|
||
func GetClientTLSConfig(t *testing.T) *tls.Config { | ||
t.Helper() | ||
|
||
cert, err := onceCert() | ||
require.NoError(t, err) | ||
|
||
certPool := x509.NewCertPool() | ||
certPool.AddCert(cert.Leaf) | ||
|
||
return &tls.Config{ | ||
RootCAs: certPool, | ||
MinVersion: tls.VersionTLS12, | ||
} | ||
} |
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
Oops, something went wrong.