Skip to content

Agent Mesh

address review: fail tests on discarded errors, surface RowsAffected error

Antonio Ojea3b7606b04c6c
diff --git a/internal/controlplane/biscuit_ttl_test.go b/internal/controlplane/biscuit_ttl_test.go
index e846e74c..fbe53eff 100644
--- a/internal/controlplane/biscuit_ttl_test.go
+++ b/internal/controlplane/biscuit_ttl_test.go
@@ -120,7 +120,10 @@ func refreshNode(t *testing.T, cpURL string, priv crypto.PrivKey, currentBiscuit
resp := postRefresh(t, cpURL, priv, currentBiscuit, "")
defer func() { _ = resp.Body.Close() }()
- body, _ := io.ReadAll(resp.Body)
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ t.Fatalf("read /refresh body: %v", err)
+ }
if resp.StatusCode != http.StatusOK {
t.Fatalf("/refresh status %s: %s", resp.Status, string(body))
}
@@ -136,9 +139,21 @@ func refreshNode(t *testing.T, cpURL string, priv crypto.PrivKey, currentBiscuit
// instead of failing on a non-200, for tests that expect a refusal.
func refreshStatus(t *testing.T, cpURL string, priv crypto.PrivKey, currentBiscuit []byte) int {
t.Helper()
- resp := postRefresh(t, cpURL, priv, currentBiscuit, "")
- _ = resp.Body.Close()
- return resp.StatusCode
+ code, _ := refreshAs(t, cpURL, priv, currentBiscuit, "")
+ return code
+}
+
+// refreshAs drives /refresh signing the challenge with priv and sending peerID
+// in the body, and returns the HTTP status and body without judging them.
+func refreshAs(t *testing.T, cpURL string, priv crypto.PrivKey, currentBiscuit []byte, peerID string) (int, []byte) {
+ t.Helper()
+ resp := postRefresh(t, cpURL, priv, currentBiscuit, peerID)
+ defer func() { _ = resp.Body.Close() }()
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ t.Fatalf("read /refresh body: %v", err)
+ }
+ return resp.StatusCode, body
}
// postRefresh posts a signed TokenRefreshRequest for priv's peer. peerID, when
diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.go
index 9c7d42d6..7971d857 100644
--- a/internal/controlplane/server_test.go
+++ b/internal/controlplane/server_test.go
@@ -3284,10 +3284,7 @@ func TestAutonomousRecovery(t *testing.T) {
// returns status and body.
refresh := func(t *testing.T, f *fixture, biscuit []byte) (int, []byte) {
t.Helper()
- resp := postRefresh(t, f.baseURL, f.priv, biscuit, f.pID.String())
- defer func() { _ = resp.Body.Close() }()
- body, _ := io.ReadAll(resp.Body)
- return resp.StatusCode, body
+ return refreshAs(t, f.baseURL, f.priv, biscuit, f.pID.String())
}
setRecovery := func(t *testing.T, f *fixture, peerID string, enabled bool) int {
@@ -3409,17 +3406,13 @@ func TestAutonomousRecovery(t *testing.T) {
// Garbage bearer, no peer_id: the pre-#367 refusal.
garbage := []byte("not-a-biscuit")
- baseline := postRefresh(t, f.baseURL, f.priv, garbage, "")
- baselineBody, _ := io.ReadAll(baseline.Body)
- _ = baseline.Body.Close()
- if baseline.StatusCode != http.StatusUnauthorized {
- t.Fatalf("baseline: got %d, want 401", baseline.StatusCode)
+ baselineCode, baselineBody := refreshAs(t, f.baseURL, f.priv, garbage, "")
+ if baselineCode != http.StatusUnauthorized {
+ t.Fatalf("baseline: got %d, want 401", baselineCode)
}
// Garbage bearer with an enrolled peer_id: byte-match fails.
- enrolledResp := postRefresh(t, f.baseURL, f.priv, garbage, f.pID.String())
- enrolledBody, _ := io.ReadAll(enrolledResp.Body)
- _ = enrolledResp.Body.Close()
+ enrolledCode, enrolledBody := refreshAs(t, f.baseURL, f.priv, garbage, f.pID.String())
// Garbage bearer with a never-enrolled peer_id: record lookup fails.
stranger, _, err := crypto.GenerateKeyPair(crypto.Ed25519, -1)
@@ -3430,19 +3423,17 @@ func TestAutonomousRecovery(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- strangerResp := postRefresh(t, f.baseURL, stranger, garbage, strangerID.String())
- strangerBody, _ := io.ReadAll(strangerResp.Body)
- _ = strangerResp.Body.Close()
+ strangerCode, strangerBody := refreshAs(t, f.baseURL, stranger, garbage, strangerID.String())
for name, got := range map[string]struct {
code int
body []byte
}{
- "enrolled peer": {enrolledResp.StatusCode, enrolledBody},
- "unknown peer": {strangerResp.StatusCode, strangerBody},
+ "enrolled peer": {enrolledCode, enrolledBody},
+ "unknown peer": {strangerCode, strangerBody},
} {
- if got.code != baseline.StatusCode || !bytes.Equal(got.body, baselineBody) {
- t.Errorf("%s: got %d %q, want the baseline %d %q", name, got.code, got.body, baseline.StatusCode, baselineBody)
+ if got.code != baselineCode || !bytes.Equal(got.body, baselineBody) {
+ t.Errorf("%s: got %d %q, want the baseline %d %q", name, got.code, got.body, baselineCode, baselineBody)
}
}
})
@@ -3506,10 +3497,8 @@ func TestAutonomousRecovery(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- resp := postRefresh(t, f.baseURL, f.priv, f.biscuit, otherID.String())
- _ = resp.Body.Close()
- if resp.StatusCode != http.StatusUnauthorized {
- t.Fatalf("valid biscuit with a foreign peer_id: got %d, want 401", resp.StatusCode)
+ if code, body := refreshAs(t, f.baseURL, f.priv, f.biscuit, otherID.String()); code != http.StatusUnauthorized {
+ t.Fatalf("valid biscuit with a foreign peer_id: got %d %s, want 401", code, body)
}
})
diff --git a/internal/node/enroll_test.go b/internal/node/enroll_test.go
index c63aa50a..3f57f14f 100644
--- a/internal/node/enroll_test.go
+++ b/internal/node/enroll_test.go
@@ -241,8 +241,14 @@ func TestStartRecoversStaleIdentityViaRefreshToken(t *testing.T) {
// /refresh with peer_id set, which the control plane honours if the operator
// opted the node in. The node must send peer_id and adopt the result.
func TestStartRecoversStaleIdentityViaAutonomousRefresh(t *testing.T) {
- cpPub, cpPriv, _ := ed25519.GenerateKey(nil)
- _, stalePriv, _ := ed25519.GenerateKey(nil)
+ cpPub, cpPriv, err := ed25519.GenerateKey(nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, stalePriv, err := ed25519.GenerateKey(nil)
+ if err != nil {
+ t.Fatal(err)
+ }
routerAddr := startMockRouterWithKey(t, cpPriv, api.RoleRouter)
@@ -298,7 +304,12 @@ func TestStartRecoversStaleIdentityViaAutonomousRefresh(t *testing.T) {
http.Error(w, "bad bearer", http.StatusBadRequest)
return
}
- body, _ := io.ReadAll(r.Body)
+ body, err := io.ReadAll(r.Body)
+ if err != nil {
+ t.Errorf("read /refresh body: %v", err)
+ http.Error(w, "bad request", http.StatusBadRequest)
+ return
+ }
var req api.TokenRefreshRequest
if err := proto.Unmarshal(body, &req); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
@@ -316,12 +327,19 @@ func TestStartRecoversStaleIdentityViaAutonomousRefresh(t *testing.T) {
http.Error(w, "Challenge verification failed", http.StatusUnauthorized)
return
}
- data, _ := proto.Marshal(&api.TokenRefreshResponse{
+ data, err := proto.Marshal(&api.TokenRefreshResponse{
BiscuitToken: mint(cpPriv, req.PeerId),
ExpiresAt: time.Now().Add(24 * time.Hour).Unix(),
})
+ if err != nil {
+ t.Errorf("marshal TokenRefreshResponse: %v", err)
+ http.Error(w, "internal", http.StatusInternalServerError)
+ return
+ }
w.Header().Set("Content-Type", "application/x-protobuf")
- _, _ = w.Write(data)
+ if _, err := w.Write(data); err != nil {
+ t.Errorf("write /refresh response: %v", err)
+ }
})
cpMux.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {
t.Error("/register must not be hit: a bootstrap node has no OIDC grant to re-enroll with")
diff --git a/internal/storage/round_trip_test.go b/internal/storage/round_trip_test.go
index 07cb172e..8fff469c 100644
--- a/internal/storage/round_trip_test.go
+++ b/internal/storage/round_trip_test.go
@@ -178,13 +178,21 @@ func TestSetNodeAutonomousRecovery(t *testing.T) {
if err := store.SetNodeAutonomousRecovery(ctx, node.PeerID, true); err != nil {
t.Fatalf("SetNodeAutonomousRecovery(true): %v", err)
}
- if got, _ = store.GetNode(ctx, node.PeerID); !got.AutonomousRecovery {
+ got, err = store.GetNode(ctx, node.PeerID)
+ if err != nil {
+ t.Fatalf("GetNode after enable: %v", err)
+ }
+ if !got.AutonomousRecovery {
t.Fatal("SetNodeAutonomousRecovery(true) did not persist")
}
if err := store.SetNodeAutonomousRecovery(ctx, node.PeerID, false); err != nil {
t.Fatalf("SetNodeAutonomousRecovery(false): %v", err)
}
- if got, _ = store.GetNode(ctx, node.PeerID); got.AutonomousRecovery {
+ got, err = store.GetNode(ctx, node.PeerID)
+ if err != nil {
+ t.Fatalf("GetNode after disable: %v", err)
+ }
+ if got.AutonomousRecovery {
t.Fatal("SetNodeAutonomousRecovery(false) did not persist")
}
diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.go
index b8d717d4..f44beec4 100644
--- a/internal/storage/sql_store.go
+++ b/internal/storage/sql_store.go
@@ -732,7 +732,11 @@ func (s *SQLStore) SetNodeAutonomousRecovery(ctx context.Context, peerID string,
if err != nil {
return err
}
- if n, err := res.RowsAffected(); err == nil && n == 0 {
+ n, err := res.RowsAffected()
+ if err != nil {
+ return err
+ }
+ if n == 0 {
return ErrNotFound
}
return nil