~james/core

core/spindle/debug_keys_test.go -rw-r--r-- 5.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package spindle

import (
	"context"
	"crypto/ed25519"
	"crypto/rand"
	"fmt"
	"strings"
	"testing"

	"github.com/bluesky-social/indigo/atproto/identity"
	"github.com/bluesky-social/indigo/atproto/syntax"
	gossh "golang.org/x/crypto/ssh"

	"tangled.org/core/api/org_tangled"
	"tangled.org/core/spindle/config"
	"tangled.org/core/spindle/db"
)

type debugKeyEnv struct {
	*ensureEnv
}

func newDebugKeyEnv(t *testing.T, collaborators ...syntax.DID) *debugKeyEnv {
	t.Helper()
	env := newEnsureEnv(t)
	env.serve("repo", collaborators...)
	env.ensure(t)
	dir := env.s.res.Directory().(*identity.MockDirectory)
	for _, did := range append([]syntax.DID{testStranger}, collaborators...) {
		dir.Insert(identity.Identity{
			DID: did,
			Services: map[string]identity.ServiceEndpoint{
				"atproto_pds": {Type: "AtprotoPersonalDataServer", URL: env.knot.srv.URL},
			},
		})
	}
	return &debugKeyEnv{env}
}

const testStranger = syntax.DID("did:plc:stranger")

func newSSHKey(t *testing.T) gossh.PublicKey {
	t.Helper()
	pub, _, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		t.Fatal(err)
	}
	key, err := gossh.NewPublicKey(pub)
	if err != nil {
		t.Fatal(err)
	}
	return key
}

func (e *debugKeyEnv) giveKey(who syntax.DID, rkey string, key gossh.PublicKey, usages ...string) {
	e.knot.put(who, org_tangled.KeySshKeyNSID, rkey, org_tangled.KeySshKey{
		LexiconTypeID: org_tangled.KeySshKeyNSID,
		Name:          rkey,
		CreatedAt:     "2026-01-01T00:00:00Z",
		PublicKey:     strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key))) + " laptop",
		Usages:        usages,
	})
}

func (e *debugKeyEnv) allowed(key gossh.PublicKey) bool {
	return e.s.DebugKeyAllowed(context.Background(), testRepoDid, key)
}

func TestDebugKeyOfTheOwnerGetsIn(t *testing.T) {
	e := newDebugKeyEnv(t)
	key := newSSHKey(t)
	e.giveKey(testOwner, "k1", key)
	if !e.allowed(key) {
		t.Fatal("the owner's key was refused")
	}
}

func TestDebugKeyOfACollaboratorGetsIn(t *testing.T) {
	e := newDebugKeyEnv(t, testBob)
	key := newSSHKey(t)
	e.giveKey(testBob, "k1", key)
	if !e.allowed(key) {
		t.Fatal("a collaborator's key was refused")
	}
}

func TestDebugKeyOfAStrangerStaysOut(t *testing.T) {
	e := newDebugKeyEnv(t, testBob)
	key := newSSHKey(t)
	e.giveKey(testStranger, "k1", key)
	e.giveKey(testBob, "k1", newSSHKey(t))
	if e.allowed(key) {
		t.Fatal("a key of someone without push access got in")
	}
}

func TestDebugKeyWhoseRecordIsGoneStaysOut(t *testing.T) {
	e := newDebugKeyEnv(t)
	key := newSSHKey(t)
	e.giveKey(testOwner, "k1", key)
	e.knot.erase(testOwner, org_tangled.KeySshKeyNSID, "k1")
	if e.allowed(key) {
		t.Fatal("a key whose record was deleted got in")
	}
}

func TestDebugKeyOfABannedCollaboratorStaysOut(t *testing.T) {
	e := newDebugKeyEnv(t, testBob)
	key := newSSHKey(t)
	e.giveKey(testBob, "k1", key)
	if err := e.s.db.PutBan(db.BanEntry{SubjectDid: testBob}); err != nil {
		t.Fatal(err)
	}
	if e.allowed(key) {
		t.Fatal("a banned collaborator's key got in")
	}
}

func TestDebugKeyOfAPausedOwnersRepoStaysOut(t *testing.T) {
	e := newDebugKeyEnv(t)
	key := newSSHKey(t)
	e.giveKey(testOwner, "k1", key)
	e.s.cfg.Server.Admission = config.AdmissionClosed
	if e.allowed(key) {
		t.Fatal("a key got in on a repo whose owner isn't admitted")
	}
}

func TestDebugKeyIsRefusedWhenThePDSErrors(t *testing.T) {
	e := newDebugKeyEnv(t, testBob)
	key := newSSHKey(t)
	e.giveKey(testOwner, "k1", key)
	e.knot.failCollection = org_tangled.KeySshKeyNSID
	if e.allowed(key) {
		t.Fatal("a key got in while its holder's pds was failing")
	}
}

func TestDebugKeyMatchesByTheMarshalledKeyNotTheComment(t *testing.T) {
	e := newDebugKeyEnv(t)
	key := newSSHKey(t)
	e.giveKey(testOwner, "k1", key)
	// a different comment and whitespace around the same key is still the same key
	e.knot.put(testOwner, org_tangled.KeySshKeyNSID, "k1", org_tangled.KeySshKey{
		Name:      "k1",
		CreatedAt: "2026-01-01T00:00:00Z",
		PublicKey: "  " + strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key))) + " other comment \n",
	})
	if !e.allowed(key) {
		t.Fatal("the same key under another comment was refused")
	}
}

func TestDebugKeyThatOnlySignsIsNotAnAuthKey(t *testing.T) {
	e := newDebugKeyEnv(t)
	key := newSSHKey(t)
	e.giveKey(testOwner, "k1", key, "sign")
	if e.allowed(key) {
		t.Fatal("a signing-only key got in")
	}
}

func TestDebugKeysAreCachedPerDidForAMoment(t *testing.T) {
	e := newDebugKeyEnv(t)
	key := newSSHKey(t)
	e.giveKey(testOwner, "k1", key)
	if !e.allowed(key) {
		t.Fatal("the owner's key was refused")
	}
	before := e.knot.count()
	for range 5 {
		if !e.allowed(key) {
			t.Fatal("the owner's key was refused on a repeat")
		}
	}
	if got := e.knot.count(); got != before {
		t.Fatalf("%d pds requests for repeats inside the cache window, want none", got-before)
	}
}

func TestDebugKeyLookupStopsAfterAFewHolders(t *testing.T) {
	var collaborators []syntax.DID
	for i := range 3 * debugKeyHolderCap {
		collaborators = append(collaborators, syntax.DID(fmt.Sprintf("did:plc:collab%03d", i)))
	}
	e := newDebugKeyEnv(t, collaborators...)
	before := e.knot.count()
	if e.allowed(newSSHKey(t)) {
		t.Fatal("a key nobody holds got in")
	}
	if got := e.knot.count() - before; got > debugKeyHolderCap {
		t.Fatalf("%d pds requests for one check, want at most %d", got, debugKeyHolderCap)
	}
}