接入本机人工处置协议和终端并记录Linux回归进度
This commit is contained in:
1 parent
bf4caebaae
commit
0f2e6bfcce
24 files changed
+1534
-48
No files matched your search
@@ -9,15 +9,16 @@ import (
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
conn net.Conn
|
||||
reader *bufio.Reader
|
||||
nonce string
|
||||
methods []Method
|
||||
recoveryAvailable bool
|
||||
stage int
|
||||
stop func() bool
|
||||
mu sync.Mutex
|
||||
once sync.Once
|
||||
conn net.Conn
|
||||
reader *bufio.Reader
|
||||
nonce string
|
||||
methods []Method
|
||||
recoveryAvailable bool
|
||||
dispositionAvailable bool
|
||||
stage int
|
||||
stop func() bool
|
||||
mu sync.Mutex
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// Begin 只接受已通过 Dial 校验身份的连接;调用过程不会通过参数或文件传递密码。
|
||||
@@ -37,13 +38,14 @@ func Begin(ctx context.Context, conn net.Conn) (*Client, error) {
|
||||
client.stop = context.AfterFunc(ctx, func() { _ = conn.Close() })
|
||||
var response Response
|
||||
if err := readFrame(client.reader, &response); err != nil || !validChallenge(response) ||
|
||||
len(response.Methods) == 0 || len(response.Methods) > 8 || response.RecoveryPage != nil || response.Inspection != nil || response.InspectionPart != nil {
|
||||
len(response.Methods) == 0 || len(response.Methods) > 8 || response.RecoveryPage != nil || response.Inspection != nil || response.InspectionPart != nil || response.Preparation != nil || response.Disposition != nil || (response.DispositionAvailable && !response.RecoveryAvailable) {
|
||||
_ = client.Close()
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
client.nonce = response.Nonce
|
||||
client.methods = response.Methods
|
||||
client.recoveryAvailable = response.RecoveryAvailable
|
||||
client.dispositionAvailable = response.DispositionAvailable
|
||||
return client, nil
|
||||
}
|
||||
|
||||
@@ -77,7 +79,7 @@ func (c *Client) exchange(request Request, stage int) error {
|
||||
_ = c.Close()
|
||||
return ErrProtocol
|
||||
}
|
||||
if response.RecoveryAvailable || response.RecoveryPage != nil || response.Inspection != nil || response.InspectionPart != nil {
|
||||
if response.RecoveryAvailable || response.RecoveryPage != nil || response.Inspection != nil || response.InspectionPart != nil || hasDispositionPayload(response) {
|
||||
c.stage = -1
|
||||
_ = c.Close()
|
||||
return ErrProtocol
|
||||
|
||||
@@ -40,26 +40,32 @@ type Method struct {
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
Version int `json:"version"`
|
||||
Nonce string `json:"nonce"`
|
||||
Action string `json:"action"`
|
||||
Method string `json:"method,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Cursor string `json:"cursor,omitempty"`
|
||||
TransactionID string `json:"transaction_id,omitempty"`
|
||||
Version int `json:"version"`
|
||||
Nonce string `json:"nonce"`
|
||||
Action string `json:"action"`
|
||||
Method string `json:"method,omitempty"`
|
||||
Code string `json:"code,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
Cursor string `json:"cursor,omitempty"`
|
||||
TransactionID string `json:"transaction_id,omitempty"`
|
||||
RecoveryAction deployment.RecoveryAction `json:"recovery_action,omitempty"`
|
||||
DispositionID string `json:"disposition_id,omitempty"`
|
||||
ConfirmationToken string `json:"confirmation_token,omitempty"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Version int `json:"version"`
|
||||
OK bool `json:"ok"`
|
||||
Code string `json:"code"`
|
||||
Nonce string `json:"nonce,omitempty"`
|
||||
Methods []Method `json:"methods,omitempty"`
|
||||
RecoveryAvailable bool `json:"recovery_available,omitempty"`
|
||||
RecoveryPage *deployment.PendingRecoveryPage `json:"recovery_page,omitempty"`
|
||||
Inspection *deployment.RecoveryInspection `json:"inspection,omitempty"`
|
||||
InspectionPart *inspectionPart `json:"inspection_part,omitempty"`
|
||||
Version int `json:"version"`
|
||||
OK bool `json:"ok"`
|
||||
Code string `json:"code"`
|
||||
Nonce string `json:"nonce,omitempty"`
|
||||
Methods []Method `json:"methods,omitempty"`
|
||||
RecoveryAvailable bool `json:"recovery_available,omitempty"`
|
||||
RecoveryPage *deployment.PendingRecoveryPage `json:"recovery_page,omitempty"`
|
||||
Inspection *deployment.RecoveryInspection `json:"inspection,omitempty"`
|
||||
InspectionPart *inspectionPart `json:"inspection_part,omitempty"`
|
||||
DispositionAvailable bool `json:"disposition_available,omitempty"`
|
||||
Preparation *RecoveryPreparation `json:"preparation,omitempty"`
|
||||
Disposition *deployment.RecoveryDispositionSummary `json:"disposition,omitempty"`
|
||||
}
|
||||
|
||||
// Session 由主服务创建,身份验证及密码持久化仍由主服务负责。
|
||||
@@ -233,6 +239,9 @@ func (s *Server) handle(parent context.Context, conn net.Conn, factory Factory)
|
||||
if index == 0 {
|
||||
response.Methods = methods
|
||||
response.RecoveryAvailable = hasRecovery
|
||||
if actions, ok := session.(RecoveryDispositionSession); ok && hasRecovery {
|
||||
response.DispositionAvailable = actions.DispositionAvailable()
|
||||
}
|
||||
}
|
||||
if writeFrame(conn, response) != nil {
|
||||
return
|
||||
@@ -276,7 +285,7 @@ func (s *Server) handle(parent context.Context, conn net.Conn, factory Factory)
|
||||
}
|
||||
|
||||
func validRequest(request Request, nonce, action string) bool {
|
||||
if request.Cursor != "" || request.TransactionID != "" {
|
||||
if request.Cursor != "" || request.TransactionID != "" || request.RecoveryAction != "" || request.DispositionID != "" || request.ConfirmationToken != "" {
|
||||
return false
|
||||
}
|
||||
if request.Version != Version || request.Action != action ||
|
||||
|
||||
@@ -23,9 +23,15 @@ func validRecoveryRequest(request Request, nonce string) bool {
|
||||
}
|
||||
switch request.Action {
|
||||
case "recovery_list":
|
||||
return request.TransactionID == "" && (request.Cursor == "" || validRecoveryID(request.Cursor))
|
||||
return request.TransactionID == "" && (request.Cursor == "" || validRecoveryID(request.Cursor)) && noDispositionRequest(request)
|
||||
case "recovery_inspect":
|
||||
return request.Cursor == "" && validRecoveryID(request.TransactionID)
|
||||
return request.Cursor == "" && validRecoveryID(request.TransactionID) && noDispositionRequest(request)
|
||||
case "recovery_prepare":
|
||||
return request.Cursor == "" && validRecoveryID(request.TransactionID) && validRecoveryAction(request.RecoveryAction) && request.DispositionID == "" && request.ConfirmationToken == ""
|
||||
case "recovery_execute":
|
||||
return request.Cursor == "" && request.TransactionID == "" && request.RecoveryAction == "" && validRecoverySecret(request.DispositionID) && validRecoverySecret(request.ConfirmationToken)
|
||||
case "recovery_result":
|
||||
return request.Cursor == "" && request.TransactionID == "" && request.RecoveryAction == "" && validRecoverySecret(request.DispositionID) && request.ConfirmationToken == ""
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -71,17 +77,20 @@ func (c *Client) recoveryExchange(request Request) (Response, error) {
|
||||
if !c.recoveryAvailable || (c.stage != 0 && c.stage != 3) {
|
||||
return fail()
|
||||
}
|
||||
if request.Action != "recovery_list" && request.Action != "recovery_inspect" && !c.dispositionAvailable {
|
||||
return fail()
|
||||
}
|
||||
request.Version, request.Nonce = Version, c.nonce
|
||||
if !validRecoveryRequest(request, c.nonce) || writeFrame(c.conn, request) != nil {
|
||||
return fail()
|
||||
}
|
||||
var response Response
|
||||
if readFrame(c.reader, &response) != nil || !validChallenge(response) || response.Nonce == c.nonce ||
|
||||
len(response.Methods) != 0 || response.RecoveryAvailable {
|
||||
len(response.Methods) != 0 || response.RecoveryAvailable || response.DispositionAvailable {
|
||||
return fail()
|
||||
}
|
||||
if request.Action == "recovery_list" {
|
||||
if response.RecoveryPage == nil || response.Inspection != nil || response.InspectionPart != nil || len(response.RecoveryPage.Items) > deployment.RecoveryPageSize {
|
||||
if response.RecoveryPage == nil || response.Inspection != nil || response.InspectionPart != nil || hasDispositionPayload(response) || len(response.RecoveryPage.Items) > deployment.RecoveryPageSize {
|
||||
return fail()
|
||||
}
|
||||
previous := request.Cursor
|
||||
@@ -95,7 +104,10 @@ func (c *Client) recoveryExchange(request Request) (Response, error) {
|
||||
(len(response.RecoveryPage.Items) != deployment.RecoveryPageSize || cursor != previous) {
|
||||
return fail()
|
||||
}
|
||||
} else {
|
||||
} else if request.Action == "recovery_inspect" {
|
||||
if hasDispositionPayload(response) {
|
||||
return fail()
|
||||
}
|
||||
if response.InspectionPart != nil {
|
||||
inspection, err := readInspectionParts(c.reader, response)
|
||||
if err != nil {
|
||||
@@ -106,6 +118,8 @@ func (c *Client) recoveryExchange(request Request) (Response, error) {
|
||||
if !validInspectionPayload(response.Inspection) || response.RecoveryPage != nil || response.Inspection.TransactionID != request.TransactionID {
|
||||
return fail()
|
||||
}
|
||||
} else if !c.dispositionAvailable || !validDispositionResponse(request, response) {
|
||||
return fail()
|
||||
}
|
||||
c.nonce, c.stage = response.Nonce, 3
|
||||
return response, nil
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package localreset
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
)
|
||||
|
||||
// RecoveryPreparation 只用于受保护的本机通道,不包含可重构服务端确认对象的绑定数据。
|
||||
type RecoveryPreparation struct {
|
||||
DispositionID string `json:"disposition_id"`
|
||||
Token string `json:"token"`
|
||||
Summary deployment.RecoveryReviewSummary `json:"summary"`
|
||||
}
|
||||
|
||||
type RecoveryDispositionSession interface {
|
||||
DispositionAvailable() bool
|
||||
PrepareRecovery(context.Context, string, deployment.RecoveryAction) (RecoveryPreparation, error)
|
||||
ExecuteRecovery(context.Context, string, string) (deployment.RecoveryDispositionSummary, error)
|
||||
GetRecoveryDisposition(context.Context, string) (deployment.RecoveryDispositionSummary, error)
|
||||
}
|
||||
|
||||
func validRecoveryAction(action deployment.RecoveryAction) bool {
|
||||
return action == deployment.RecoveryActionRecord || action == deployment.RecoveryActionCommit || action == deployment.RecoveryActionRollback
|
||||
}
|
||||
|
||||
func validRecoverySecret(value string) bool {
|
||||
if len(value) != 64 || value == strings.Repeat("0", 64) {
|
||||
return false
|
||||
}
|
||||
for _, c := range value {
|
||||
if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func noDispositionRequest(request Request) bool {
|
||||
return request.RecoveryAction == "" && request.DispositionID == "" && request.ConfirmationToken == ""
|
||||
}
|
||||
|
||||
func hasDispositionPayload(response Response) bool {
|
||||
return response.DispositionAvailable || response.Preparation != nil || response.Disposition != nil
|
||||
}
|
||||
|
||||
func validPreparation(value *RecoveryPreparation) bool {
|
||||
if value == nil || !validRecoverySecret(value.DispositionID) || !validRecoverySecret(value.Token) {
|
||||
return false
|
||||
}
|
||||
return value.Summary.Validate() == nil
|
||||
}
|
||||
|
||||
func validDispositionResponse(request Request, response Response) bool {
|
||||
if response.RecoveryPage != nil || response.Inspection != nil || response.InspectionPart != nil {
|
||||
return false
|
||||
}
|
||||
if request.Action == "recovery_prepare" {
|
||||
return response.Disposition == nil && validPreparation(response.Preparation) &&
|
||||
response.Preparation.Summary.TransactionID == request.TransactionID && response.Preparation.Summary.Action == request.RecoveryAction
|
||||
}
|
||||
return response.Preparation == nil && response.Disposition != nil && response.Disposition.Validate() == nil && response.Disposition.ID == request.DispositionID
|
||||
}
|
||||
|
||||
func dispatchRecoveryDisposition(ctx context.Context, session RecoverySession, request Request, response *Response) error {
|
||||
actions, ok := session.(RecoveryDispositionSession)
|
||||
if !ok || !actions.DispositionAvailable() {
|
||||
return ErrUnavailable
|
||||
}
|
||||
var err error
|
||||
switch request.Action {
|
||||
case "recovery_prepare":
|
||||
var value RecoveryPreparation
|
||||
value, err = actions.PrepareRecovery(ctx, request.TransactionID, request.RecoveryAction)
|
||||
response.Preparation = &value
|
||||
case "recovery_execute":
|
||||
var value deployment.RecoveryDispositionSummary
|
||||
value, err = actions.ExecuteRecovery(ctx, request.DispositionID, request.ConfirmationToken)
|
||||
response.Disposition = &value
|
||||
case "recovery_result":
|
||||
var value deployment.RecoveryDispositionSummary
|
||||
value, err = actions.GetRecoveryDisposition(ctx, request.DispositionID)
|
||||
response.Disposition = &value
|
||||
default:
|
||||
return ErrProtocol
|
||||
}
|
||||
if err != nil || !validDispositionResponse(request, *response) {
|
||||
return ErrUnavailable
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) DispositionAvailable() bool { return c.dispositionAvailable }
|
||||
|
||||
func (c *Client) PrepareRecovery(id string, action deployment.RecoveryAction) (RecoveryPreparation, error) {
|
||||
response, err := c.recoveryExchange(Request{Action: "recovery_prepare", TransactionID: id, RecoveryAction: action})
|
||||
if err != nil {
|
||||
return RecoveryPreparation{}, err
|
||||
}
|
||||
return *response.Preparation, nil
|
||||
}
|
||||
|
||||
func (c *Client) ExecuteRecovery(id, token string) (deployment.RecoveryDispositionSummary, error) {
|
||||
response, err := c.recoveryExchange(Request{Action: "recovery_execute", DispositionID: id, ConfirmationToken: token})
|
||||
if err != nil {
|
||||
return deployment.RecoveryDispositionSummary{}, err
|
||||
}
|
||||
return *response.Disposition, nil
|
||||
}
|
||||
|
||||
func (c *Client) GetRecoveryDisposition(id string) (deployment.RecoveryDispositionSummary, error) {
|
||||
response, err := c.recoveryExchange(Request{Action: "recovery_result", DispositionID: id})
|
||||
if err != nil {
|
||||
return deployment.RecoveryDispositionSummary{}, err
|
||||
}
|
||||
return *response.Disposition, nil
|
||||
}
|
||||
@@ -0,0 +1,305 @@
|
||||
package localreset
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
)
|
||||
|
||||
type dispositionTestSession struct {
|
||||
recoveryTestSession
|
||||
prepares atomic.Int32
|
||||
executes atomic.Int32
|
||||
queries atomic.Int32
|
||||
prepared RecoveryPreparation
|
||||
result deployment.RecoveryDispositionSummary
|
||||
fail bool
|
||||
}
|
||||
|
||||
func (*dispositionTestSession) DispositionAvailable() bool { return true }
|
||||
func (s *dispositionTestSession) PrepareRecovery(_ context.Context, id string, action deployment.RecoveryAction) (RecoveryPreparation, error) {
|
||||
s.prepares.Add(1)
|
||||
now := time.Now().UTC()
|
||||
s.prepared = RecoveryPreparation{DispositionID: strings.Repeat("b", 64), Token: strings.Repeat("c", 64),
|
||||
Summary: deployment.RecoveryReviewSummary{TransactionID: id, ServerID: "server-1", Phase: "reload_attempted", Action: action, HistoricalEvidence: true, ObservedAt: now, ExpiresAt: now.Add(time.Minute)}}
|
||||
return s.prepared, nil
|
||||
}
|
||||
func (s *dispositionTestSession) ExecuteRecovery(_ context.Context, id, token string) (deployment.RecoveryDispositionSummary, error) {
|
||||
s.executes.Add(1)
|
||||
if id != s.prepared.DispositionID || token != s.prepared.Token || s.fail {
|
||||
return deployment.RecoveryDispositionSummary{}, errors.New("secret-executor-error")
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
results := map[deployment.RecoveryAction]string{deployment.RecoveryActionRecord: "observed", deployment.RecoveryActionCommit: "committed", deployment.RecoveryActionRollback: "rolled_back"}
|
||||
s.result = deployment.RecoveryDispositionSummary{ID: id, TransactionID: s.prepared.Summary.TransactionID, ServerID: "server-1", Action: s.prepared.Summary.Action, Status: "succeeded", ResultCode: results[s.prepared.Summary.Action], CreatedAt: now, UpdatedAt: now}
|
||||
s.prepared = RecoveryPreparation{}
|
||||
return s.result, nil
|
||||
}
|
||||
func (s *dispositionTestSession) GetRecoveryDisposition(context.Context, string) (deployment.RecoveryDispositionSummary, error) {
|
||||
s.queries.Add(1)
|
||||
return s.result, nil
|
||||
}
|
||||
|
||||
func TestDispositionProtocolThreeActionsAndQuery(t *testing.T) {
|
||||
for _, action := range []deployment.RecoveryAction{deployment.RecoveryActionRecord, deployment.RecoveryActionCommit, deployment.RecoveryActionRollback} {
|
||||
t.Run(string(action), func(t *testing.T) {
|
||||
session := &dispositionTestSession{}
|
||||
_, conn, done := recoveryConnection(t, session)
|
||||
client, err := Begin(t.Context(), conn)
|
||||
if err != nil || !client.DispositionAvailable() {
|
||||
t.Fatalf("capability: %v", err)
|
||||
}
|
||||
prepared, err := client.PrepareRecovery("tx-1", action)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := client.ExecuteRecovery(prepared.DispositionID, prepared.Token)
|
||||
if err != nil || result.Action != action || result.Status != "succeeded" {
|
||||
t.Fatalf("execute: %+v %v", result, err)
|
||||
}
|
||||
query, err := client.GetRecoveryDisposition(result.ID)
|
||||
if err != nil || query != result {
|
||||
t.Fatalf("query: %+v %v", query, err)
|
||||
}
|
||||
if _, err := client.ExecuteRecovery(prepared.DispositionID, prepared.Token); err == nil {
|
||||
t.Fatal("replay accepted")
|
||||
}
|
||||
_ = client.Close()
|
||||
<-done
|
||||
if session.prepares.Load() != 1 || session.executes.Load() != 2 || session.queries.Load() != 1 || session.resets.Load() != 0 {
|
||||
t.Fatal("unexpected business calls")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispositionRequestFieldIsolation(t *testing.T) {
|
||||
nonce := strings.Repeat("a", 64)
|
||||
requests := []Request{
|
||||
{Action: "recovery_list"}, {Action: "recovery_inspect", TransactionID: "tx-1"},
|
||||
{Action: "recovery_prepare", TransactionID: "tx-1", RecoveryAction: deployment.RecoveryActionRecord},
|
||||
{Action: "recovery_execute", DispositionID: strings.Repeat("b", 64), ConfirmationToken: strings.Repeat("c", 64)},
|
||||
{Action: "recovery_result", DispositionID: strings.Repeat("b", 64)},
|
||||
}
|
||||
fields := []struct {
|
||||
name string
|
||||
set func(*Request)
|
||||
allowed map[string]bool
|
||||
}{
|
||||
{"method", func(r *Request) { r.Method = "email" }, nil},
|
||||
{"password", func(r *Request) { r.Password = "secret" }, nil},
|
||||
{"code", func(r *Request) { r.Code = "12345678" }, nil},
|
||||
{"cursor", func(r *Request) { r.Cursor = "tx-0" }, map[string]bool{"recovery_list": true}},
|
||||
{"transaction", func(r *Request) { r.TransactionID = "tx-1" }, map[string]bool{"recovery_inspect": true, "recovery_prepare": true}},
|
||||
{"action", func(r *Request) { r.RecoveryAction = deployment.RecoveryActionRecord }, map[string]bool{"recovery_prepare": true}},
|
||||
{"id", func(r *Request) { r.DispositionID = strings.Repeat("b", 64) }, map[string]bool{"recovery_execute": true, "recovery_result": true}},
|
||||
{"token", func(r *Request) { r.ConfirmationToken = strings.Repeat("c", 64) }, map[string]bool{"recovery_execute": true}},
|
||||
}
|
||||
for _, request := range requests {
|
||||
request.Version, request.Nonce = Version, nonce
|
||||
if !validRecoveryRequest(request, nonce) {
|
||||
t.Fatalf("valid request rejected: %s", request.Action)
|
||||
}
|
||||
for _, field := range fields {
|
||||
if field.allowed[request.Action] {
|
||||
continue
|
||||
}
|
||||
mixed := request
|
||||
field.set(&mixed)
|
||||
if validRecoveryRequest(mixed, nonce) {
|
||||
t.Fatalf("mixed %s/%s accepted", request.Action, field.name)
|
||||
}
|
||||
}
|
||||
if validRecoveryRequest(request, strings.Repeat("d", 64)) {
|
||||
t.Fatal("old nonce accepted")
|
||||
}
|
||||
}
|
||||
for _, field := range fields[5:] {
|
||||
request := Request{Version: Version, Nonce: nonce, Action: "select", Method: "email"}
|
||||
field.set(&request)
|
||||
if validRequest(request, nonce, "select") {
|
||||
t.Fatalf("reset accepts %s", field.name)
|
||||
}
|
||||
}
|
||||
for _, bad := range []string{"", ".", "..", strings.Repeat("0", 64), strings.Repeat("A", 64), strings.Repeat("b", 65)} {
|
||||
if validRecoverySecret(bad) {
|
||||
t.Fatal("bad secret accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispositionReplayNonceAndMixedPayloadNeverExecute(t *testing.T) {
|
||||
for _, mode := range []string{"old-nonce", "password", "cursor", "transaction", "action", "prepare-token", "query-token"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
session := &dispositionTestSession{}
|
||||
_, conn, done := recoveryConnection(t, session)
|
||||
reader := bufio.NewReaderSize(conn, MaxFrameSize+1)
|
||||
var first Response
|
||||
if err := readFrame(reader, &first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := writeFrame(conn, Request{Version: Version, Nonce: first.Nonce, Action: "recovery_prepare", TransactionID: "tx-1", RecoveryAction: deployment.RecoveryActionRecord}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var prepared Response
|
||||
if err := readFrame(reader, &prepared); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := Request{Version: Version, Nonce: prepared.Nonce, Action: "recovery_execute", DispositionID: prepared.Preparation.DispositionID, ConfirmationToken: prepared.Preparation.Token}
|
||||
switch mode {
|
||||
case "old-nonce":
|
||||
request.Nonce = first.Nonce
|
||||
case "password":
|
||||
request.Password = "secret"
|
||||
case "cursor":
|
||||
request.Cursor = "tx-0"
|
||||
case "transaction":
|
||||
request.TransactionID = "tx-1"
|
||||
case "action":
|
||||
request.RecoveryAction = deployment.RecoveryActionRollback
|
||||
case "prepare-token":
|
||||
request.Action, request.TransactionID, request.RecoveryAction = "recovery_prepare", "tx-1", deployment.RecoveryActionRecord
|
||||
case "query-token":
|
||||
request.Action = "recovery_result"
|
||||
}
|
||||
if err := writeFrame(conn, request); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var response Response
|
||||
if err := readFrame(reader, &response); err != nil || response.OK || response.Code != "INVALID_REQUEST" {
|
||||
t.Fatalf("response: %+v %v", response, err)
|
||||
}
|
||||
<-done
|
||||
if session.executes.Load() != 0 || session.queries.Load() != 0 || session.prepares.Load() != 1 {
|
||||
t.Fatal("invalid request reached service")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispositionClientRejectsMalformedPrepare(t *testing.T) {
|
||||
for _, mode := range []string{"token", "id", "transaction", "action", "lifetime", "missing-evidence", "mixed-result", "mixed-page", "capability"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
server, conn := net.Pipe()
|
||||
defer server.Close()
|
||||
_ = server.SetDeadline(time.Now().Add(2 * time.Second))
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
defer server.Close()
|
||||
_ = writeFrame(server, Response{Version: Version, OK: true, Code: "CHALLENGE", Nonce: strings.Repeat("a", 64), Methods: []Method{{ID: "email"}}, RecoveryAvailable: true, DispositionAvailable: true})
|
||||
var request Request
|
||||
if readFrame(bufio.NewReaderSize(server, MaxFrameSize+1), &request) != nil {
|
||||
return
|
||||
}
|
||||
session := &dispositionTestSession{}
|
||||
value, _ := session.PrepareRecovery(t.Context(), "tx-1", deployment.RecoveryActionCommit)
|
||||
response := Response{Version: Version, OK: true, Code: "CHALLENGE", Nonce: strings.Repeat("b", 64), Preparation: &value}
|
||||
switch mode {
|
||||
case "token":
|
||||
value.Token = "bad"
|
||||
case "id":
|
||||
value.DispositionID = strings.Repeat("0", 64)
|
||||
case "transaction":
|
||||
value.Summary.TransactionID = "tx-2"
|
||||
case "action":
|
||||
value.Summary.Action = deployment.RecoveryActionRollback
|
||||
case "lifetime":
|
||||
value.Summary.ExpiresAt = value.Summary.ObservedAt.Add(time.Hour)
|
||||
case "missing-evidence":
|
||||
value.Summary.HistoricalEvidence = false
|
||||
case "mixed-result":
|
||||
response.Disposition = &deployment.RecoveryDispositionSummary{}
|
||||
case "mixed-page":
|
||||
response.RecoveryPage = &deployment.PendingRecoveryPage{}
|
||||
case "capability":
|
||||
response.DispositionAvailable = true
|
||||
}
|
||||
_ = writeFrame(server, response)
|
||||
}()
|
||||
client, err := Begin(t.Context(), conn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value, err := client.PrepareRecovery("tx-1", deployment.RecoveryActionCommit); err == nil || value.Token != "" {
|
||||
t.Fatal("malformed preparation returned")
|
||||
}
|
||||
_ = client.Close()
|
||||
<-done
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispositionFieldsRejectedInInspectionParts(t *testing.T) {
|
||||
inspection := longInspectionFixture()
|
||||
var raw bytes.Buffer
|
||||
if err := writeRecoveryResponse(t.Context(), &raw, Response{Version: Version, OK: true, Code: "CHALLENGE", Nonce: strings.Repeat("a", 64), Inspection: &inspection}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, field := range []string{"preparation", "disposition", "capability"} {
|
||||
for _, index := range []int{0, 1} {
|
||||
lines := bytes.Split(bytes.TrimSuffix(raw.Bytes(), []byte{'\n'}), []byte{'\n'})
|
||||
var changed Response
|
||||
if err := json.Unmarshal(lines[index], &changed); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
switch field {
|
||||
case "preparation":
|
||||
changed.Preparation = &RecoveryPreparation{}
|
||||
case "disposition":
|
||||
changed.Disposition = &deployment.RecoveryDispositionSummary{}
|
||||
case "capability":
|
||||
changed.DispositionAvailable = true
|
||||
}
|
||||
lines[index], _ = json.Marshal(changed)
|
||||
reader := bufio.NewReaderSize(bytes.NewReader(append(bytes.Join(lines, []byte{'\n'}), '\n')), MaxFrameSize+1)
|
||||
var first Response
|
||||
if err := readFrame(reader, &first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value, err := readInspectionParts(reader, first); err == nil || value != nil {
|
||||
t.Fatalf("mixed fragment %s/%d accepted", field, index)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDispositionQueryAfterReconnectDoesNotExecute(t *testing.T) {
|
||||
session := &dispositionTestSession{}
|
||||
_, conn, done := recoveryConnection(t, session)
|
||||
client, err := Begin(t.Context(), conn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prepared, err := client.PrepareRecovery("tx-1", deployment.RecoveryActionRecord)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := client.ExecuteRecovery(prepared.DispositionID, prepared.Token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = client.Close()
|
||||
<-done
|
||||
next := &dispositionTestSession{result: session.result}
|
||||
_, conn, _ = recoveryConnection(t, next)
|
||||
client, err = Begin(t.Context(), conn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
if result, err := client.GetRecoveryDisposition(prepared.DispositionID); err != nil || result.Status != "succeeded" {
|
||||
t.Fatalf("query: %+v %v", result, err)
|
||||
}
|
||||
if next.prepares.Load() != 0 || next.executes.Load() != 0 || next.queries.Load() != 1 {
|
||||
t.Fatal("query replayed operation")
|
||||
}
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func writeRecoveryResponse(ctx context.Context, writer io.Writer, response Respo
|
||||
if response.Inspection == nil {
|
||||
return writeFrame(writer, response)
|
||||
}
|
||||
if !validInspectionPayload(response.Inspection) || response.InspectionPart != nil || response.RecoveryPage != nil {
|
||||
if !validInspectionPayload(response.Inspection) || response.InspectionPart != nil || response.RecoveryPage != nil || hasDispositionPayload(response) {
|
||||
return ErrProtocol
|
||||
}
|
||||
encoded, err := json.Marshal(response)
|
||||
@@ -95,7 +95,7 @@ func readInspectionParts(reader *bufio.Reader, first Response) (*deployment.Reco
|
||||
}
|
||||
}
|
||||
part := response.InspectionPart
|
||||
if !validChallenge(response) || response.Nonce != first.Nonce || len(response.Methods) != 0 || response.RecoveryAvailable ||
|
||||
if !validChallenge(response) || response.Nonce != first.Nonce || len(response.Methods) != 0 || response.RecoveryAvailable || hasDispositionPayload(response) ||
|
||||
response.Inspection != nil || response.RecoveryPage != nil || part == nil || part.Index != index || part.Total != total ||
|
||||
len(part.Data) == 0 || len(part.Data) > inspectionPartSize || (index < total-1 && len(part.Data) != inspectionPartSize) {
|
||||
return nil, ErrProtocol
|
||||
|
||||
@@ -25,6 +25,8 @@ func (s *Server) handleRecovery(ctx context.Context, conn net.Conn, reader *bufi
|
||||
case "recovery_inspect":
|
||||
inspection, readErr := session.InspectRecovery(ctx, request.TransactionID)
|
||||
err, response.Inspection = readErr, &inspection
|
||||
case "recovery_prepare", "recovery_execute", "recovery_result":
|
||||
err = dispatchRecoveryDisposition(ctx, session, request, &response)
|
||||
default:
|
||||
err = ErrProtocol
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func (s *recoveryTestSession) InspectRecovery(_ context.Context, id string) (dep
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func recoveryConnection(t *testing.T, session *recoveryTestSession) (*Server, net.Conn, <-chan struct{}) {
|
||||
func recoveryConnection(t *testing.T, session Session) (*Server, net.Conn, <-chan struct{}) {
|
||||
t.Helper()
|
||||
serverConn, clientConn := net.Pipe()
|
||||
server := newServer(nil, func(net.Conn) (Peer, error) { return Peer{Identity: "test"}, nil }, nil)
|
||||
|
||||
Reference in new issue
Block a user