Monorepo for Tangled tangled.org
840
fork

Configure Feed

Select the types of activity you want to include in your feed.

knotserver: add owners public keys to database on startup #270

open opened by willdot.net targeting master from willdot.net/tangled-fork: knot-owner-keys

When a knot is first created, the owners public keys are not available unless the owner manually adds them again via the appview.

This change will automatically get all the owners public keys from their PDS record and add them to the knotservers public keys database.

Labels

None yet.

assignee

None yet.

Participants 1
AT URI
at://did:plc:dadhhalkfcq3gucaq25hjqon/sh.tangled.repo.pull/3mknyooc7l422
+26 -70
Interdiff #1 #2
+5 -47
knotserver/router.go
··· 9 9 "strings" 10 10 "sync" 11 11 12 - comatproto "github.com/bluesky-social/indigo/api/atproto" 13 - "github.com/bluesky-social/indigo/atproto/syntax" 14 - indigoxrpc "github.com/bluesky-social/indigo/xrpc" 15 12 "github.com/go-chi/chi/v5" 16 - "tangled.org/core/api/tangled" 17 13 "tangled.org/core/idresolver" 18 14 "tangled.org/core/jetstream" 19 15 "tangled.org/core/knotserver/config" ··· 58 54 } 59 55 60 56 // configure owner 61 - if err = h.configureOwner(); err != nil { 57 + if err = h.configureOwner(ctx); err != nil { 62 58 return nil, err 63 59 } 64 60 h.l.Info("owner set", "did", h.c.Server.Owner) ··· 175 171 }) 176 172 } 177 173 178 - func (h *Knot) configureOwner() error { 174 + func (h *Knot) configureOwner(ctx context.Context) error { 179 175 cfgOwner := h.c.Server.Owner 180 176 181 177 rbacDomain := "thisserver" ··· 216 212 return fmt.Errorf("failed to add owner to RBAC: %w", err) 217 213 } 218 214 219 - h.addOwnersKeys(cfgOwner) 220 - 221 - return nil 222 - } 223 - 224 - func (h *Knot) addOwnersKeys(owner string) { 225 - id, err := h.resolver.Directory().LookupDID(context.Background(), syntax.DID(owner)) 226 - if err != nil { 227 - h.l.Warn("failed to lookup owners did while adding their keys") 228 - return 229 - } 230 - 231 - serviceEndpoint, ok := id.Services["atproto_pds"] 232 - if !ok { 233 - h.l.Warn("owners identity did not contain atproto_pds service while adding their keys") 234 - return 235 - } 236 - 237 - xrpcc := indigoxrpc.Client{Host: serviceEndpoint.URL} 238 - resp, err := comatproto.RepoListRecords(context.Background(), &xrpcc, tangled.PublicKeyNSID, "", 50, owner, false) 215 + err = h.fetchAndAddKeys(ctx, cfgOwner) 239 216 if err != nil { 240 - h.l.Error("fetching owners public key records from pds", "error", err) 241 - return 217 + h.l.Error("fetching and adding owners public keys", "error", err) 242 218 } 243 219 244 - for _, record := range resp.Records { 245 - if record == nil { 246 - continue 247 - } 248 - key := record.Value.Val.(*tangled.PublicKey) 249 - if key == nil { 250 - continue 251 - } 252 - pk := db.PublicKey{ 253 - Did: owner, 254 - PublicKey: *key, 255 - } 256 - err = h.db.AddPublicKey(pk) 257 - if err != nil { 258 - h.l.Error("adding one of the owners public keys to db", "error", err) 259 - } 260 - } 261 - 262 - h.l.Info("finished adding owners public keys") 220 + return nil 263 221 }
+21 -23
knotserver/ingester.go
··· 8 8 "net/http" 9 9 "net/url" 10 10 "path/filepath" 11 - "strings" 12 11 13 12 comatproto "github.com/bluesky-social/indigo/api/atproto" 14 13 "github.com/bluesky-social/indigo/atproto/syntax" 15 14 "github.com/bluesky-social/indigo/xrpc" 15 + indigoxrpc "github.com/bluesky-social/indigo/xrpc" 16 16 jmodels "github.com/bluesky-social/jetstream/pkg/models" 17 17 "tangled.org/core/api/tangled" 18 18 "tangled.org/core/appview/models" ··· 434 434 func (h *Knot) fetchAndAddKeys(ctx context.Context, did string) error { 435 435 l := log.FromContext(ctx) 436 436 437 - keysEndpoint, err := url.JoinPath(h.c.AppViewEndpoint, "keys", did) 437 + id, err := h.resolver.Directory().LookupDID(ctx, syntax.DID(did)) 438 438 if err != nil { 439 - l.Error("error building endpoint url", "did", did, "error", err.Error()) 440 - return fmt.Errorf("error building endpoint url: %w", err) 439 + l.Error("lookup did to fetch keys", "did", did, "error", err) 440 + return fmt.Errorf("lookup did to fetch keys: %w", err) 441 441 } 442 442 443 - resp, err := http.Get(keysEndpoint) 444 - if err != nil { 445 - l.Error("error getting keys", "did", did, "error", err) 446 - return fmt.Errorf("error getting keys: %w", err) 447 - } 448 - defer resp.Body.Close() 449 - 450 - if resp.StatusCode == http.StatusNotFound { 451 - l.Info("no keys found for did", "did", did) 443 + serviceEndpoint, ok := id.Services["atproto_pds"] 444 + if !ok { 445 + l.Warn("did identity did not contain atproto_pds service while adding their keys", "did", did) 452 446 return nil 453 447 } 454 448 455 - plaintext, err := io.ReadAll(resp.Body) 449 + xrpcc := indigoxrpc.Client{Host: serviceEndpoint.URL} 450 + resp, err := comatproto.RepoListRecords(context.Background(), &xrpcc, tangled.PublicKeyNSID, "", 50, did, false) 456 451 if err != nil { 457 - l.Error("error reading response body", "error", err) 458 - return fmt.Errorf("error reading response body: %w", err) 452 + return fmt.Errorf("fetching public keys for did: %w", err) 459 453 } 460 454 461 - for key := range strings.SplitSeq(string(plaintext), "\n") { 462 - if key == "" { 455 + for _, record := range resp.Records { 456 + if record == nil { 457 + continue 458 + } 459 + key := record.Value.Val.(*tangled.PublicKey) 460 + if key == nil { 463 461 continue 464 462 } 465 463 pk := db.PublicKey{ 466 - Did: did, 464 + Did: did, 465 + PublicKey: *key, 467 466 } 468 - pk.Key = key 469 - if err := h.db.AddPublicKey(pk); err != nil { 470 - l.Error("failed to add public key", "error", err) 471 - return fmt.Errorf("failed to add public key: %w", err) 467 + err = h.db.AddPublicKey(pk) 468 + if err != nil { 469 + return fmt.Errorf("adding public key to db: %w", err) 472 470 } 473 471 } 474 472 return nil

History

4 rounds 0 comments
sign up or login to add to the discussion
1 commit
expand
knotserver: add owners public keys to database on startup
merge conflicts detected
expand
  • blog/templates/index.html:9
  • blog/templates/post.html:20
expand 0 comments
1 commit
expand
knotserver: add owners public keys to database on startup
expand 0 comments
1 commit
expand
knotserver: add owners public keys to database on startup
expand 0 comments
1 commit
expand
knotserver: add owners public keys to database on startup
expand 0 comments