mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
136 lines
4.6 KiB
Go
136 lines
4.6 KiB
Go
package answer
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"sync/atomic"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials"
|
|
)
|
|
|
|
type observedTLSFailure struct{ kind Kind }
|
|
|
|
// Source stamps belong to the actual TLS attempt, never to the later
|
|
// asynchronous SubConn notification. Missing stamps cannot prove freshness.
|
|
type attemptFailure struct {
|
|
started, completed uint64
|
|
cause error
|
|
}
|
|
|
|
func (f *attemptFailure) Error() string { return f.cause.Error() }
|
|
func (f *attemptFailure) Unwrap() error { return f.cause }
|
|
|
|
// HandshakeEvidence reads the established connection's TLS cause. Initial
|
|
// attempts are observed at the SubConn/Picker boundary, including TCP failures
|
|
// which never enter a TLS callback. No channel-wide last TLS cause is used.
|
|
type HandshakeEvidence struct{}
|
|
|
|
// ObserveCredentials preserves typed TLS failures before gRPC converts a
|
|
// handshake/preface error into an opaque Unavailable status message.
|
|
func ObserveCredentials(inner credentials.TransportCredentials) (credentials.TransportCredentials, *HandshakeEvidence) {
|
|
return &observedCredentials{TransportCredentials: inner}, &HandshakeEvidence{}
|
|
}
|
|
|
|
type observedCredentials struct {
|
|
credentials.TransportCredentials
|
|
}
|
|
|
|
func (c *observedCredentials) Clone() credentials.TransportCredentials {
|
|
return &observedCredentials{TransportCredentials: c.TransportCredentials.Clone()}
|
|
}
|
|
func (c *observedCredentials) ClientHandshake(ctx context.Context, authority string, raw net.Conn) (net.Conn, credentials.AuthInfo, error) {
|
|
started := attemptSequence.Add(1)
|
|
conn, info, err := c.TransportCredentials.ClientHandshake(ctx, authority, raw)
|
|
if err != nil {
|
|
kind, _ := ErrorKind(err)
|
|
if certificateKind(kind) || kind == Credential || kind == Configuration {
|
|
return conn, info, &attemptFailure{started: started, completed: attemptSequence.Add(1), cause: err}
|
|
}
|
|
return conn, info, err
|
|
}
|
|
local := &connectionTLSInfo{AuthInfo: info, attempt: started}
|
|
return &observedTLSConn{Conn: conn, info: local}, local, nil
|
|
}
|
|
|
|
type connectionTLSInfo struct {
|
|
credentials.AuthInfo
|
|
attempt uint64
|
|
late atomic.Pointer[observedTLSFailure]
|
|
}
|
|
|
|
// Keep the underlying credentials' authority check: observing the connection
|
|
// must neither reject a valid per-call override nor authorize a mismatch.
|
|
func (info *connectionTLSInfo) ValidateAuthority(authority string) error {
|
|
if validator, ok := info.AuthInfo.(credentials.AuthorityValidator); ok {
|
|
return validator.ValidateAuthority(authority)
|
|
}
|
|
return fmt.Errorf("credentials type %q cannot validate authority %q", info.AuthType(), authority)
|
|
}
|
|
|
|
func (info *connectionTLSInfo) GetCommonAuthInfo() credentials.CommonAuthInfo {
|
|
if inner, ok := info.AuthInfo.(interface {
|
|
GetCommonAuthInfo() credentials.CommonAuthInfo
|
|
}); ok {
|
|
return inner.GetCommonAuthInfo()
|
|
}
|
|
return credentials.CommonAuthInfo{}
|
|
}
|
|
|
|
type observedTLSConn struct {
|
|
net.Conn
|
|
info *connectionTLSInfo
|
|
}
|
|
|
|
func (conn *observedTLSConn) Read(p []byte) (int, error) {
|
|
n, err := conn.Conn.Read(p)
|
|
if err != nil {
|
|
kind, _ := ErrorKind(err)
|
|
fact := &observedTLSFailure{kind: kind}
|
|
conn.info.late.Store(fact)
|
|
if certificateKind(kind) {
|
|
err = &attemptFailure{started: conn.info.attempt, completed: attemptSequence.Add(1), cause: err}
|
|
}
|
|
}
|
|
return n, err
|
|
}
|
|
|
|
// UnaryInterceptor only attaches a typed TLS fact. Classification still
|
|
// happens solely in Lookup; established calls use their own connection.
|
|
func (e *HandshakeEvidence) UnaryInterceptor(ctx context.Context, method string, req, reply any, conn *grpc.ClientConn, invoke grpc.UnaryInvoker, opts ...grpc.CallOption) error {
|
|
err := invoke(ctx, method, req, reply, conn, opts...)
|
|
return e.record(ctx, err)
|
|
}
|
|
|
|
func (e *HandshakeEvidence) StreamInterceptor(ctx context.Context, desc *grpc.StreamDesc, conn *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
|
|
stream, err := streamer(ctx, desc, conn, method, opts...)
|
|
if err != nil {
|
|
return nil, e.record(ctx, err)
|
|
}
|
|
return &observedStream{ClientStream: stream, record: func(err error) error { return e.record(ctx, err) }}, nil
|
|
}
|
|
|
|
func (e *HandshakeEvidence) record(ctx context.Context, err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
rpc, ok := ctx.Value(rpcEvidenceKey{}).(*rpcEvidence)
|
|
if !ok {
|
|
return err
|
|
}
|
|
phase := Unsent
|
|
var fact *observedTLSFailure
|
|
if rpc.writeStarted.Load() {
|
|
phase = PossiblyAdmitted
|
|
fact = nil
|
|
if info := rpc.tls.Load(); info != nil {
|
|
fact = info.late.Load()
|
|
}
|
|
}
|
|
if fact != nil && (fact.kind == Certificate || fact.kind == Trust || fact.kind == Name || fact.kind == Credential) {
|
|
return &failure{phase: phase, kind: fact.kind, cause: err}
|
|
}
|
|
return err
|
|
}
|