574369b666
Over TLS without -ca-cert the CLI was always skip-verify, with no way to ask for verification against the system roots even though the library supports it. The new common flag is wired to Options.RequireCertificateValidation in both the gateway and Galaxy dialers and echoed in the redacted JSON options. Test: tls_flag_test.go starts a self-signed TLS gRPC gateway; the default open-session succeeds (positive control) and the same call with the flag fails to dial (fails on the old code with 'flag provided but not defined').
106 lines
3.6 KiB
Go
106 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"crypto/elliptic"
|
|
"crypto/rand"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"crypto/x509/pkix"
|
|
"math/big"
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
pb "gitea.dohertylan.com/dohertj2/mxaccessgw/clients/go/internal/generated"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
)
|
|
|
|
// #199 — `--tls` without a CA used to be skip-verify with no way out from the
|
|
// CLI. -require-certificate-validation must make the CLI reject a certificate
|
|
// the system roots do not trust (the gateway's own self-signed one).
|
|
|
|
type openSessionFakeGateway struct {
|
|
pb.UnimplementedMxAccessGatewayServer
|
|
}
|
|
|
|
func (g *openSessionFakeGateway) OpenSession(context.Context, *pb.OpenSessionRequest) (*pb.OpenSessionReply, error) {
|
|
return &pb.OpenSessionReply{
|
|
SessionId: "tls-session",
|
|
ProtocolStatus: &pb.ProtocolStatus{Code: pb.ProtocolStatusCode_PROTOCOL_STATUS_CODE_OK},
|
|
}, nil
|
|
}
|
|
|
|
func startSelfSignedTLSGateway(t *testing.T) string {
|
|
t.Helper()
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
if err != nil {
|
|
t.Fatalf("generate key: %v", err)
|
|
}
|
|
template := &x509.Certificate{
|
|
SerialNumber: big.NewInt(1),
|
|
Subject: pkix.Name{CommonName: "localhost"},
|
|
DNSNames: []string{"localhost"},
|
|
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
|
|
NotBefore: time.Now().Add(-time.Hour),
|
|
NotAfter: time.Now().Add(time.Hour),
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
}
|
|
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
|
|
if err != nil {
|
|
t.Fatalf("create certificate: %v", err)
|
|
}
|
|
cert := tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key}
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("listen: %v", err)
|
|
}
|
|
server := grpc.NewServer(grpc.Creds(credentials.NewTLS(&tls.Config{
|
|
Certificates: []tls.Certificate{cert},
|
|
MinVersion: tls.VersionTLS12,
|
|
})))
|
|
pb.RegisterMxAccessGatewayServer(server, &openSessionFakeGateway{})
|
|
go func() { _ = server.Serve(listener) }()
|
|
t.Cleanup(func() {
|
|
server.Stop()
|
|
_ = listener.Close()
|
|
})
|
|
return listener.Addr().String()
|
|
}
|
|
|
|
func TestRequireCertificateValidationFlagRejectsSelfSignedGateway(t *testing.T) {
|
|
endpoint := startSelfSignedTLSGateway(t)
|
|
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
|
|
defer cancel()
|
|
base := []string{"open-session", "-endpoint", endpoint, "-api-key", "test", "-call-timeout", "5s"}
|
|
|
|
// Positive control: the lenient default accepts the self-signed certificate.
|
|
var okOut, okErr bytes.Buffer
|
|
if err := runWithIO(ctx, base, &okOut, &okErr); err != nil {
|
|
t.Fatalf("default TLS open-session error = %v; stderr = %s", err, okErr.String())
|
|
}
|
|
if got := strings.TrimSpace(okOut.String()); got != "tls-session" {
|
|
t.Fatalf("default TLS open-session output = %q", got)
|
|
}
|
|
|
|
// Strict: the handshake never verifies, so the connected-dial wait runs out.
|
|
// The dial error (not a flag-parse error) is what proves the flag reached the
|
|
// TLS configuration; the positive control above proves the endpoint is live.
|
|
strictCtx, strictCancel := context.WithTimeout(t.Context(), 2*time.Second)
|
|
defer strictCancel()
|
|
var strictOut, strictErr bytes.Buffer
|
|
err := runWithIO(strictCtx, append(append([]string{}, base...), "-require-certificate-validation"), &strictOut, &strictErr)
|
|
if err == nil {
|
|
t.Fatalf("strict TLS open-session succeeded against a self-signed gateway; stdout = %s", strictOut.String())
|
|
}
|
|
if !strings.Contains(err.Error(), "dial failed") {
|
|
t.Fatalf("strict TLS open-session error = %v; want a dial failure (stderr = %s)", err, strictErr.String())
|
|
}
|
|
}
|