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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package pds
import (
"cmp"
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/bluesky-social/indigo/atproto/atcrypto"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
"github.com/bluesky-social/indigo/util/ssrf"
"k8s.io/utils/ptr"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/oauth"
atpds "github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/pds"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/pds/files"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/pds/plcsubmit"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/repo"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bus"
"github.com/johnandersen777/socialweb-computer-kcp/internal/component"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostos"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/kcp"
)
const (
shutdownTimeout = 10 * time.Second
directoryTimeout = 10 * time.Second
)
type Service struct {
spec v1alpha1.PdsSpec
deps Deps
mode atpds.Mode
stateDir string
publicBaseURL string
plcURL string
mu sync.Mutex
started bool
endpoint component.Endpoint
key component.Key
baseURL string
server *atpds.Server
stopCh chan struct{}
stopOnce sync.Once
readiness component.ReadySignal
closeOnce sync.Once
repoStore *repo.SQLiteStore
accountStore *atpds.Store
keyStore atpds.RepoKeyStore
}
var _ component.Service = (*Service)(nil)
func New(spec v1alpha1.PdsSpec, deps Deps) (*Service, error) {
s := &Service{
spec: spec,
deps: deps,
mode: atpds.ModeFull,
stateDir: strings.TrimSpace(deps.StateDir),
publicBaseURL: strings.TrimRight(strings.TrimSpace(spec.PublicBaseURL), "/"),
plcURL: strings.TrimRight(strings.TrimSpace(spec.PlcURL), "/"),
}
if spec.Mode == v1alpha1.PdsModeRepo {
s.mode = atpds.ModeRepo
}
if s.mode == atpds.ModeRepo && spec.OAuth != nil {
return nil, errors.New("pds: oauth is not available in read-only repo mode")
}
if spec.Storage != nil {
s.stateDir = cmp.Or(strings.TrimSpace(spec.Storage.DataDir), s.stateDir)
}
if s.plcURL == "" && spec.Network != nil {
s.plcURL = strings.TrimRight(strings.TrimSpace(spec.Network.PLCDirectoryURL), "/")
}
if spec.RequireIngress && s.publicBaseURL != "" {
if err := publicEndpoint(s.publicBaseURL); err != nil {
return nil, err
}
}
return s, nil
}
func (s *Service) componentPath(name string) string {
if s.deps.StateDir == "" {
return ""
}
return filepath.Join(s.deps.StateDir, name)
}
func (s *Service) serviceDID() string {
if s.spec.Identity == nil {
return ""
}
return strings.TrimSpace(s.spec.Identity.Did)
}
func (s *Service) alsoKnownAs() []string {
if s.spec.Identity == nil {
return nil
}
if handle := strings.TrimSpace(s.spec.Identity.Handle); handle != "" {
return []string{"at://" + handle}
}
return nil
}
func (s *Service) serviceAuthAudiences() []string {
if s.spec.ServiceAuth == nil {
return nil
}
return append([]string(nil), s.spec.ServiceAuth.Audience...)
}
func publicEndpoint(raw string) error {
u, err := url.Parse(raw)
if err != nil || u.Scheme == "" || u.Host == "" {
return fmt.Errorf("pds: the ingress public URL %q is not a scheme, host and optional port", raw)
}
if !strings.EqualFold(u.Scheme, "https") {
return fmt.Errorf("pds: the ingress public URL %q must be https", raw)
}
if u.User != nil || (u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.Fragment != "" {
return fmt.Errorf("pds: the ingress public URL %q must carry no userinfo, path, query or fragment", raw)
}
return nil
}
func (s *Service) Start(ctx context.Context) error {
s.mu.Lock()
if s.started {
s.mu.Unlock()
return errors.New("pds: the service is already started")
}
s.mu.Unlock()
stateDir := s.stateDir
if stateDir == "" {
return errors.New("pds: a state directory is required")
}
if err := os.MkdirAll(stateDir, 0o700); err != nil {
return fmt.Errorf("pds: creating the state directory: %w", err)
}
log := s.deps.Log
var err error
unlock, err := lockStateDir(stateDir)
if err != nil {
return err
}
defer unlock()
var (
repoStore *repo.SQLiteStore
accountStore *atpds.Store
)
success := false
cleanup := func() {
if accountStore != nil {
_ = accountStore.Close()
}
if repoStore != nil {
_ = repoStore.Close()
}
}
defer func() {
if !success {
cleanup()
}
}()
key := s.deps.Key()
key.Kind = cmp.Or(key.Kind, Kind)
key.Name = cmp.Or(key.Name, "default")
logicalURL := "http://" + logicalHost(key)
baseURL := cmp.Or(s.publicBaseURL, logicalURL)
plcURL := s.plcURL
if plcURL != "" && log != nil {
log.Info("pds plc directory", "url", plcURL, "publicBaseURL", baseURL)
}
handlers := s.deps.Bus
var plcClient *http.Client
if plcURL != "" {
if handlers != nil && strings.HasPrefix(strings.ToLower(plcURL), "http://") && !isLoopbackURL(plcURL) {
plcClient = handlers.HTTPClient()
} else {
plcClient = s.deps.Egress.Client(directoryTimeout)
}
}
repoStore, err = repo.NewSQLiteStore(ctx, filepath.Join(stateDir, "repos.db"))
if err != nil {
return fmt.Errorf("pds: opening the repo store: %w", err)
}
if s.mode == atpds.ModeFull {
accountStore, err = atpds.NewStore(ctx, filepath.Join(stateDir, "accounts.db"))
if err != nil {
return fmt.Errorf("pds: opening the account store: %w", err)
}
}
blobDir := filepath.Join(stateDir, "blobs")
if s.spec.Storage != nil {
blobDir = cmp.Or(strings.TrimSpace(s.spec.Storage.BlobsDir), blobDir)
}
blobStore, err := files.NewFileBlobStore(blobDir)
if err != nil {
return err
}
var sessionSecret []byte
if s.mode == atpds.ModeFull {
sessionSecret, err = files.SessionSecret(stateDir, s.componentPath(SessionSecretFile))
if err != nil {
return err
}
}
keyStore, err := files.RepoKeyStore(s.componentPath(RepoKeyDir))
if err != nil {
return err
}
if s.mode == atpds.ModeFull && keyStore == nil {
return errors.New("pds: full mode needs a repo key store (a state dir for repo-keys)")
}
directory := buildDirectory(plcURL, plcClient, handlers, s.deps.Egress)
var submitter atpds.PLCSubmitter
var rotation atcrypto.PrivateKey
if s.mode == atpds.ModeFull && plcURL != "" {
rotation, err = files.RotationKey(stateDir, s.componentPath(RotationKeyFile))
if err != nil {
return err
}
submitter, err = plcsubmit.New(plcURL, rotation, plcClient)
if err != nil {
return err
}
}
if s.mode == atpds.ModeFull && submitter == nil && !s.spec.AllowExplicitDID {
return errors.New("pds: full mode needs a PLC directory URL or submitter")
}
rotationDID := ""
if rotation != nil {
pub, err := rotation.PublicKey()
if err != nil {
return fmt.Errorf("pds: deriving the rotation key: %w", err)
}
rotationDID = pub.DIDKey()
}
oauthConfig, err := s.oauthConfig(stateDir, baseURL)
if err != nil {
return err
}
server, err := atpds.New(atpds.Config{
Mode: s.mode,
Repos: repoStore,
Store: accountStore,
KeyStore: keyStore,
Directory: directory,
PLC: submitter,
Blobs: blobStore,
OAuth: oauthConfig,
ServiceDID: s.serviceDID(),
PublicBaseURL: baseURL,
AlsoKnownAs: s.alsoKnownAs(),
RotationKey: rotationDID,
ContactEmail: strings.TrimSpace(s.spec.ContactEmail),
SessionSecret: sessionSecret,
ServiceAuthAudiences: s.serviceAuthAudiences(),
MaxBlobSize: ptr.Deref(s.spec.MaxBlobSize, 0),
MaxRequestBytes: ptr.Deref(s.spec.MaxRequestBytes, 0),
AllowExplicitDID: s.spec.AllowExplicitDID,
})
if err != nil {
return err
}
if handlers == nil {
return errors.New("pds: no handler registry is available")
}
if err := handlers.RegisterHTTP(key, server.Handler()); err != nil {
return fmt.Errorf("pds: registering the handler: %w", err)
}
stopCh := make(chan struct{})
endpoint := component.Endpoint{
Name: "http",
Protocol: component.ProtocolHTTP,
URL: logicalURL,
}
s.mu.Lock()
s.started = true
s.endpoint = endpoint
s.key = key
s.baseURL = logicalURL
s.stopCh = stopCh
s.stopOnce = sync.Once{}
s.server = server
s.repoStore = repoStore
s.accountStore = accountStore
s.keyStore = keyStore
s.mu.Unlock()
s.readiness.Signal()
success = true
syncCtx, cancelSync := context.WithCancel(ctx)
var wg sync.WaitGroup
if s.spec.RequireIngress && s.publicBaseURL != "" && s.mode == atpds.ModeFull && submitter != nil {
wg.Go(func() { s.syncServiceEndpoints(syncCtx, accountStore, submitter, s.publicBaseURL) })
}
select {
case <-ctx.Done():
case <-stopCh:
}
cancelSync()
wg.Wait()
s.mu.Lock()
s.started = false
s.stopCh = nil
s.mu.Unlock()
return nil
}
func (s *Service) syncServiceEndpoints(ctx context.Context, store *atpds.Store, submitter atpds.PLCSubmitter, publicURL string) {
if store == nil || submitter == nil || strings.TrimSpace(publicURL) == "" {
return
}
dids, err := store.ListDIDs(ctx)
if err != nil {
return
}
for _, did := range dids {
if !strings.HasPrefix(string(did), "did:plc:") {
continue
}
if err := submitter.UpdateService(ctx, did, publicURL); err != nil && s.deps.Log != nil {
s.deps.Log.Warn("syncing the PDS service endpoint", "did", did.String(), "err", err.Error())
}
}
}
func (s *Service) oauthConfig(stateDir, baseURL string) (*atpds.OAuthConfig, error) {
if s.mode != atpds.ModeFull || s.spec.OAuth == nil {
return nil, nil
}
opts := s.spec.OAuth
signerKey, err := files.OAuthSigner(stateDir)
if err != nil {
return nil, err
}
serviceDID := s.serviceDID()
if serviceDID == "" {
parsed, err := url.Parse(baseURL)
if err != nil || parsed.Host == "" {
return nil, errors.New("pds: cannot derive the service DID")
}
serviceDID = "did:web:" + url.QueryEscape(parsed.Host)
}
signerDID, err := syntax.ParseDID(serviceDID)
if err != nil {
return nil, fmt.Errorf("pds: invalid service DID: %w", err)
}
issuer := cmp.Or(strings.TrimRight(strings.TrimSpace(opts.Issuer), "/"), baseURL)
return &atpds.OAuthConfig{
Issuer: issuer,
Signer: oauth.Signer{DID: signerDID, Key: signerKey},
AccessTokenTTL: time.Duration(opts.AccessTokenTTLSeconds) * time.Second,
RefreshTokenTTL: time.Duration(opts.RefreshTokenTTLSeconds) * time.Second,
DisableLocalhostClients: opts.DisableLocalhostClients || !opts.AllowLocalhostClients,
Scopes: append([]string(nil), opts.Scopes...),
}, nil
}
func buildDirectory(plcURL string, client *http.Client, handlers *bus.Bus, egress component.Egress) identity.Directory {
if plcURL == "" {
return identity.DefaultDirectory()
}
if handlers != nil && strings.HasPrefix(strings.ToLower(plcURL), "http://") && !isLoopbackURL(plcURL) {
httpClient := handlers.HTTPClient()
return &identity.BaseDirectory{
PLCURL: plcURL,
HTTPClient: *httpClient,
PLCClient: httpClient,
}
}
plcClient := client
if plcClient == nil {
plcClient = egress.Client(directoryTimeout)
}
return &identity.BaseDirectory{
PLCURL: plcURL,
HTTPClient: http.Client{
Timeout: directoryTimeout,
Transport: ssrf.PublicOnlyTransport(),
},
PLCClient: plcClient,
}
}
func isLoopbackURL(raw string) bool {
u, err := url.Parse(raw)
if err != nil {
return false
}
host := u.Hostname()
if strings.EqualFold(host, "localhost") {
return true
}
ip := net.ParseIP(host)
return ip != nil && ip.IsLoopback()
}
func logicalHost(key component.Key) string {
kind := strings.ToLower(strings.TrimSpace(key.Kind))
if kind == "" {
return ""
}
if name := strings.ToLower(strings.TrimSpace(key.Name)); name != "" {
return kind + "." + name
}
return kcp.ServiceFQDN(kind, "default", key.Workspace, kcp.DefaultServiceDomain)
}
func (s *Service) Ready() component.Readiness {
return &s.readiness
}
func (s *Service) Endpoints() []component.Endpoint {
s.mu.Lock()
defer s.mu.Unlock()
if !s.started || s.endpoint.URL == "" {
return nil
}
return []component.Endpoint{s.endpoint}
}
func (s *Service) Stop(ctx context.Context) error {
s.mu.Lock()
stopCh := s.stopCh
s.mu.Unlock()
if stopCh != nil {
s.stopOnce.Do(func() { close(stopCh) })
}
s.closeResources()
s.mu.Lock()
s.started = false
s.mu.Unlock()
return nil
}
func (s *Service) closeResources() {
s.closeOnce.Do(func() {
if s.accountStore != nil {
_ = s.accountStore.Close()
}
if s.repoStore != nil {
_ = s.repoStore.Close()
}
})
}
func (s *Service) Server() *atpds.Server {
s.mu.Lock()
defer s.mu.Unlock()
if !s.started {
return nil
}
return s.server
}
func (s *Service) KeyStore() atpds.RepoKeyStore {
s.mu.Lock()
defer s.mu.Unlock()
return s.keyStore
}
func (s *Service) ServiceDID() string {
server := s.Server()
if server == nil {
return ""
}
return server.ServiceDID()
}
func (s *Service) UpdateService(ctx context.Context, did syntax.DID, service string) error {
server := s.Server()
if server == nil {
return errors.New("pds: the service is not started")
}
return server.UpdateService(ctx, did, service)
}
func lockStateDir(dir string) (func(), error) {
f, err := os.OpenFile(filepath.Join(dir, "lock"), os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
return nil, fmt.Errorf("pds: the state dir lock: %w", err)
}
if err := hostos.Lock(f, hostos.LockExclusive|hostos.LockNonBlocking); err != nil {
_ = f.Close()
return nil, fmt.Errorf("pds: the state dir %s is locked by another PDS: %w", dir, err)
}
return func() {
_ = hostos.Unlock(f)
_ = f.Close()
}, nil
}