Skip to content

Commit 13c3dd2

Browse files
committed
rerun
1 parent a40aeba commit 13c3dd2

7 files changed

Lines changed: 86 additions & 637 deletions

File tree

api/network.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"strings"
2424
"time"
2525

26-
"github.com/libp2p/go-libp2p/core/peer"
2726
"github.com/libp2p/go-libp2p/core/protocol"
2827
)
2928

@@ -77,14 +76,6 @@ const (
7776
TokenRefreshCheckInterval = 10 * time.Minute
7877
)
7978

80-
func CanonicalPeerID(s string) (string, error) {
81-
p, err := peer.Decode(s)
82-
if err != nil {
83-
return "", err
84-
}
85-
return p.String(), nil
86-
}
87-
8879
// EnrollChallenge is the payload a bootstrap enrollee signs to prove
8980
// possession of the private half of BootstrapEnrollRequest.public_key at
9081
// POST /enroll, carried in that message's timestamp/challenge_signature

internal/controlplane/mesh.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,19 @@ func (p *P2PMeshAdapter) PublishEvent(ctx context.Context, eventType api.MeshEve
137137
privKey = key
138138
}
139139

140+
var canonical string
141+
// Events can have an empty peer ID.
142+
if peerID != "" {
143+
pID, err := peer.Decode(peerID)
144+
if err != nil {
145+
return fmt.Errorf("invalid peer ID %q: %w", peerID, err)
146+
}
147+
canonical = pID.String()
148+
}
149+
140150
event := &api.MeshEvent{
141151
Type: eventType,
142-
PeerId: canonicalPeerID(peerID),
152+
PeerId: canonical,
143153
Timestamp: time.Now().UnixMilli(),
144154
NewPublicKey: payload,
145155
}
@@ -165,17 +175,6 @@ func (p *P2PMeshAdapter) PublishEvent(ctx context.Context, eventType api.MeshEve
165175
return nil
166176
}
167177

168-
func canonicalPeerID(peerID string) string {
169-
if peerID == "" {
170-
return ""
171-
}
172-
canonical, err := api.CanonicalPeerID(peerID)
173-
if err != nil {
174-
return peerID
175-
}
176-
return canonical
177-
}
178-
179178
func (p *P2PMeshAdapter) DiscoverServices(ctx context.Context, serviceType string) ([]*ServiceAnnouncement, error) {
180179
return nil, nil
181180
}

internal/controlplane/mesh_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ func TestP2PMeshAdapter_PublishAndSubscribe(t *testing.T) {
122122
time.Sleep(500 * time.Millisecond)
123123

124124
// 5. Test publishing BANNED event
125-
targetPeerID := "12D3KooWBannedPeerIDForTesting1234567890"
125+
_, targetID := newTestKey(t)
126+
targetPeerID := targetID.String()
126127
if err := adapter.PublishEvent(ctx, api.MeshEvent_BANNED, targetPeerID, nil); err != nil {
127128
t.Fatalf("failed to publish BANNED event: %v", err)
128129
}

internal/controlplane/peer_id_canonicalization_test.go

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package controlplane
1717
import (
1818
"bytes"
1919
"context"
20+
"crypto/ed25519"
21+
"crypto/rand"
2022
"encoding/base64"
2123
"net/http"
2224
"strconv"
@@ -26,6 +28,8 @@ import (
2628

2729
"github.com/google/sam/api"
2830
"github.com/google/sam/internal/storage"
31+
"github.com/libp2p/go-libp2p"
32+
pubsub "github.com/libp2p/go-libp2p-pubsub"
2933
"github.com/libp2p/go-libp2p/core/crypto"
3034
"github.com/libp2p/go-libp2p/core/peer"
3135
"google.golang.org/protobuf/proto"
@@ -131,12 +135,15 @@ func TestBannedNodeCannotRegisterUnderAnAlias(t *testing.T) {
131135
if err != nil {
132136
t.Fatal(err)
133137
}
134-
reqData, _ := proto.Marshal(&api.EnrollRequest{
138+
reqData, err := proto.Marshal(&api.EnrollRequest{
135139
Jwt: mintToken(map[string]interface{}{"sub": sub}),
136140
PeerId: peerID,
137141
PublicKey: pubBytes,
138142
RequestedRole: api.RoleNode,
139143
})
144+
if err != nil {
145+
t.Fatalf("failed to marshal enroll request: %v", err)
146+
}
140147
resp, err := client.Post(baseURL+"/register", "application/x-protobuf", bytes.NewReader(reqData))
141148
if err != nil {
142149
t.Fatalf("/register failed: %v", err)
@@ -155,8 +162,14 @@ func TestBannedNodeCannotRegisterUnderAnAlias(t *testing.T) {
155162
t.Fatalf("node was not stored under its canonical id: %v", err)
156163
}
157164

158-
revokeData, _ := proto.Marshal(&api.TokenRevokeRequest{PeerId: id.String()})
159-
req, _ := http.NewRequest(http.MethodPost, baseURL+"/admin/revoke", bytes.NewReader(revokeData))
165+
revokeData, err := proto.Marshal(&api.TokenRevokeRequest{PeerId: id.String()})
166+
if err != nil {
167+
t.Fatalf("failed to marshal revoke request: %v", err)
168+
}
169+
req, err := http.NewRequest(http.MethodPost, baseURL+"/admin/revoke", bytes.NewReader(revokeData))
170+
if err != nil {
171+
t.Fatalf("failed to create revoke request: %v", err)
172+
}
160173
req.Header.Set("Authorization", "Bearer super-secret-admin-token")
161174
resp, err := client.Do(req)
162175
if err != nil {
@@ -195,8 +208,14 @@ func TestRevokeBansTheIdentityNotTheSpelling(t *testing.T) {
195208
name: "admin revoke",
196209
revoke: func(t *testing.T, baseURL, peerID, userToken, adminToken string, client *http.Client) int {
197210
t.Helper()
198-
body, _ := proto.Marshal(&api.TokenRevokeRequest{PeerId: peerID})
199-
req, _ := http.NewRequest(http.MethodPost, baseURL+"/admin/revoke", bytes.NewReader(body))
211+
body, err := proto.Marshal(&api.TokenRevokeRequest{PeerId: peerID})
212+
if err != nil {
213+
t.Fatalf("failed to marshal revoke request: %v", err)
214+
}
215+
req, err := http.NewRequest(http.MethodPost, baseURL+"/admin/revoke", bytes.NewReader(body))
216+
if err != nil {
217+
t.Fatalf("failed to create revoke request: %v", err)
218+
}
200219
req.Header.Set("Authorization", "Bearer "+adminToken)
201220
resp, err := client.Do(req)
202221
if err != nil {
@@ -210,7 +229,10 @@ func TestRevokeBansTheIdentityNotTheSpelling(t *testing.T) {
210229
name: "user revoke",
211230
revoke: func(t *testing.T, baseURL, peerID, userToken, adminToken string, client *http.Client) int {
212231
t.Helper()
213-
req, _ := http.NewRequest(http.MethodPost, baseURL+"/user/revoke?id="+peerID, nil)
232+
req, err := http.NewRequest(http.MethodPost, baseURL+"/user/revoke?id="+peerID, nil)
233+
if err != nil {
234+
t.Fatalf("failed to create user revoke request: %v", err)
235+
}
214236
req.Header.Set("Authorization", "Bearer "+userToken)
215237
resp, err := client.Do(req)
216238
if err != nil {
@@ -323,7 +345,10 @@ func TestEnrollStatusReachesTheRecordThroughAnAlias(t *testing.T) {
323345
if err != nil {
324346
t.Fatalf("failed to sign challenge: %v", err)
325347
}
326-
req, _ := http.NewRequest(http.MethodGet, baseURL+"/enroll/status?peer_id="+query, nil)
348+
req, err := http.NewRequest(http.MethodGet, baseURL+"/enroll/status?peer_id="+query, nil)
349+
if err != nil {
350+
t.Fatalf("failed to create enroll status request: %v", err)
351+
}
327352
req.Header.Set(api.HeaderChallengeTimestamp, strconv.FormatInt(ts, 10))
328353
req.Header.Set(api.HeaderChallengeSignature, base64.RawURLEncoding.EncodeToString(sig))
329354
resp, err := client.Do(req)
@@ -345,24 +370,46 @@ func TestEnrollStatusReachesTheRecordThroughAnAlias(t *testing.T) {
345370
}
346371
}
347372

348-
func TestCanonicalPeerIDIsBestEffort(t *testing.T) {
349-
_, id := newTestKey(t)
350-
alias := cidAlias(t, id)
373+
func TestPublishEventValidatesPeerID(t *testing.T) {
374+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
375+
defer cancel()
351376

352-
for _, tc := range []struct {
353-
name string
354-
in string
355-
want string
356-
}{
357-
{name: "canonical id is stable", in: id.String(), want: id.String()},
358-
{name: "alias folds to canonical", in: alias, want: id.String()},
359-
{name: "empty stays empty", in: "", want: ""},
360-
{name: "undecodable passes through", in: "peer-node-a", want: "peer-node-a"},
361-
} {
362-
t.Run(tc.name, func(t *testing.T) {
363-
if got := canonicalPeerID(tc.in); got != tc.want {
364-
t.Errorf("canonicalPeerID(%q) = %q, want %q", tc.in, got, tc.want)
365-
}
366-
})
377+
store, err := storage.NewSQLStore("sqlite", ":memory:")
378+
if err != nil {
379+
t.Fatalf("failed to create store: %v", err)
380+
}
381+
defer func() { _ = store.Close() }()
382+
383+
pub, priv, err := ed25519.GenerateKey(rand.Reader)
384+
if err != nil {
385+
t.Fatalf("failed to generate key: %v", err)
386+
}
387+
if err := store.SaveInitialKey(ctx, priv, pub); err != nil {
388+
t.Fatalf("failed to save key: %v", err)
389+
}
390+
391+
h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/127.0.0.1/tcp/0"))
392+
if err != nil {
393+
t.Fatalf("failed to create host: %v", err)
394+
}
395+
defer func() { _ = h.Close() }()
396+
397+
ps, err := pubsub.NewGossipSub(ctx, h)
398+
if err != nil {
399+
t.Fatalf("failed to create pubsub: %v", err)
400+
}
401+
402+
adapter, err := NewP2PMeshAdapter(h, ps, store)
403+
if err != nil {
404+
t.Fatalf("failed to create P2PMeshAdapter: %v", err)
405+
}
406+
defer func() { _ = adapter.Close() }()
407+
408+
if err := adapter.PublishEvent(ctx, api.MeshEvent_POLICY_UPDATE, "", nil); err != nil {
409+
t.Errorf("empty peer ID should be accepted: %v", err)
410+
}
411+
412+
if err := adapter.PublishEvent(ctx, api.MeshEvent_BANNED, "peer-node-a", nil); err == nil {
413+
t.Error("expected an error for an undecodable peer ID, got nil")
367414
}
368415
}

internal/controlplane/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ func (s *Server) HandleEnrollStatus(w http.ResponseWriter, r *http.Request) {
14771477

14781478
pID, err := peer.Decode(peerID)
14791479
if err != nil {
1480-
http.Error(w, "Unauthorized", http.StatusUnauthorized)
1480+
http.Error(w, "Unauthorized, Invalid Peer ID", http.StatusUnauthorized)
14811481
return
14821482
}
14831483
canonical := pID.String()

0 commit comments

Comments
 (0)