mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
test: pin answer precedence and certificate-chain boundaries
This commit is contained in:
1 parent
1e9983fc01
commit
ceeeca9e84
4 files changed
+970
-822
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,109 @@
|
||||
package answer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"google.golang.org/grpc/balancer"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/connectivity"
|
||||
"google.golang.org/grpc/status"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReviewContextAndDefiniteAnswerPrecedence(t *testing.T) {
|
||||
for _, code := range []codes.Code{codes.Canceled, codes.DeadlineExceeded} {
|
||||
t.Run(code.String(), func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
e := &rpcEvidence{}
|
||||
e.dial.Store(&dialObservation{kind: Trust, cause: errors.New("earlier picker trust failure")})
|
||||
got := e.record(ctx, status.Error(code, "caller ended the WaitForReady call"))
|
||||
p, check := PolicyOf(got)
|
||||
if check != nil || p.Code != "canceled" || !errors.Is(got, context.Canceled) {
|
||||
t.Fatalf("caller lost to picker: %+v %v %v", p, check, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
e := &rpcEvidence{}
|
||||
e.writeStarted.Store(true)
|
||||
info := &connectionTLSInfo{}
|
||||
info.late.Store(&observedTLSFailure{kind: Certificate})
|
||||
e.tls.Store(info)
|
||||
ctx := context.WithValue(context.Background(), rpcEvidenceKey{}, e)
|
||||
got := (&HandshakeEvidence{}).record(ctx, status.Error(codes.FailedPrecondition, "definite refusal"))
|
||||
if status.Code(got) != codes.FailedPrecondition {
|
||||
t.Fatalf("status changed: %v", got)
|
||||
}
|
||||
p, err := PolicyOf(EnsureEvidence(PossiblyAdmitted, got))
|
||||
if err != nil || p.Code != "not_admitted" {
|
||||
t.Fatalf("late TLS replaced definite answer: %+v %v", p, err)
|
||||
}
|
||||
}
|
||||
|
||||
type capturedPickerConn struct {
|
||||
balancer.ClientConn
|
||||
state balancer.State
|
||||
}
|
||||
|
||||
func (c *capturedPickerConn) UpdateState(s balancer.State) { c.state = s }
|
||||
|
||||
type fixedErrorPicker struct{ err error }
|
||||
|
||||
func (p fixedErrorPicker) Pick(balancer.PickInfo) (balancer.PickResult, error) {
|
||||
return balancer.PickResult{}, p.err
|
||||
}
|
||||
func TestReviewOldPickerCannotBorrowNewPeerGeneration(t *testing.T) {
|
||||
pool := &peerPool{generation: 1, peers: map[string]*peerAttempt{}}
|
||||
cc := &capturedPickerConn{}
|
||||
wrapped := &evidenceClientConn{ClientConn: cc, pool: pool}
|
||||
wrapped.UpdateState(balancer.State{Picker: fixedErrorPicker{status.Error(codes.Unavailable, "old peer network failure")}})
|
||||
old := cc.state.Picker
|
||||
pool.generation = 2
|
||||
pool.peers["new"] = &peerAttempt{peer: "new", attempt: 3, completed: 4, state: connectivity.TransientFailure, kind: Trust, cause: errors.New("new peer trust"), singleAddress: true}
|
||||
e := &rpcEvidence{started: 2}
|
||||
ctx := context.WithValue(context.Background(), rpcEvidenceKey{}, e)
|
||||
_, _ = old.Pick(balancer.PickInfo{Ctx: ctx})
|
||||
if got := e.dial.Load(); got == nil || got.kind != Connection {
|
||||
t.Fatalf("old picker borrowed new proof: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReviewContractProjectionAndReadOnlyRecovery(t *testing.T) {
|
||||
for _, status := range []int{http.StatusConflict, http.StatusPreconditionFailed, http.StatusPreconditionRequired} {
|
||||
p, err := PolicyOf(HTTPStatusFailure(status, nil), Cluster)
|
||||
if err != nil || p.Exit != 2 {
|
||||
t.Errorf("HTTP %d cluster precondition: %+v %v", status, p, err)
|
||||
}
|
||||
}
|
||||
p, err := Lookup(PossiblyAdmitted, ForContract(Chunk, Domain("forbidden", false, "access refused")))
|
||||
if err != nil || p.Exit != 5 || p.Code != "forbidden" {
|
||||
t.Errorf("typed authorization: %+v %v", p, err)
|
||||
}
|
||||
p, err = Lookup(PossiblyAdmitted, ForContract(ChunkVerify, Connection))
|
||||
if err != nil || p.Exit != 1 || !strings.Contains(p.Cure, "verification") || strings.Contains(p.Cure, "replay key") {
|
||||
t.Errorf("read-only recovery: %+v %v", p, err)
|
||||
}
|
||||
}
|
||||
|
||||
type certFailTransport struct{}
|
||||
|
||||
func (certFailTransport) RoundTrip(*http.Request) (*http.Response, error) {
|
||||
return nil, x509.UnknownAuthorityError{Cert: &x509.Certificate{}}
|
||||
}
|
||||
func TestReviewAdmittedPollTLSFailureKeepsOperationRecoverable(t *testing.T) {
|
||||
for _, phase := range []Phase{Unsent, PossiblyAdmitted} {
|
||||
req, _ := http.NewRequest(http.MethodGet, "https://poll.example/operation", nil)
|
||||
_, err := DoHTTP(&http.Client{Transport: certFailTransport{}}, req, phase)
|
||||
p, check := PolicyOf(err, Chunk)
|
||||
want := "not_authorized"
|
||||
if phase == PossiblyAdmitted {
|
||||
want = "outcome_unknown"
|
||||
}
|
||||
if check != nil || p.Code != want {
|
||||
t.Errorf("phase %s: %+v %v", phase, p, check)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -77,6 +77,17 @@ func TestMasterLinkCARotationUsesTheVerifiedChain(t *testing.T) {
|
||||
}
|
||||
sc, sk := good.Issue(t, "master", now.Add(time.Hour)).WriteFiles(t, dir, "master")
|
||||
cc, ck := good.Issue(t, "operator", now.Add(time.Hour)).WriteFiles(t, dir, "operator")
|
||||
// A transmitted chain may retain an unused alternative too.
|
||||
// The peer builds its own chain; only our leaf is locally dated.
|
||||
for _, path := range []string{sc, cc} {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(path, append(raw, badPEM...), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
serverOpt, serverErr := link.ServerCredentials(link.TLSFiles{Cert: sc, Key: sk, CA: caPath})
|
||||
clientOpt, clientErr := link.ClientCredentials(link.TLSFiles{Cert: cc, Key: ck, CA: caPath}, "")
|
||||
if serverErr != nil {
|
||||
|
||||
@@ -51,3 +51,12 @@ func TestWorkspaceUsesSharedCredentialPolicies(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReviewUnsentCallerCancellationIsNotMasterRefusal(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
err := classifyCode("forced move", answer.RecordContext(ctx, answer.Unsent, context.Canceled))
|
||||
if OutcomeOf(err) != OutcomeUnsent {
|
||||
t.Fatalf("no master answer: outcome=%v err=%v", OutcomeOf(err), err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user