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
package gatewaymain
import (
"cmp"
"context"
"errors"
"log/slog"
"net/http"
"time"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/johnandersen777/socialweb-computer-kcp/internal/gateway"
"github.com/johnandersen777/socialweb-computer-kcp/internal/plcsweep"
"github.com/johnandersen777/socialweb-computer-kcp/internal/revocation"
"github.com/johnandersen777/socialweb-computer-kcp/internal/xrpcserver"
)
type Deps struct {
Store gateway.Store
Deny revocation.Deny
Feed gateway.Feed
Directory identity.Directory
Revoker gateway.Revoker
Certs gateway.CertRevoker
Tenant gateway.TenantAPI
Sweep *plcsweep.Sweeper
SweepInterval time.Duration
}
type Gateway struct {
handler http.Handler
surface *xrpcserver.Server
sweep *plcsweep.Sweeper
sweepInterval time.Duration
log *slog.Logger
}
func Build(ctx context.Context, cfg Config, d Deps, log *slog.Logger) (*Gateway, error) {
if log == nil {
log = slog.Default()
}
verbs := &gateway.Server{
Deny: d.Deny, Store: d.Store, Revoker: d.Revoker, Certs: d.Certs, Feed: d.Feed,
Tenant: d.Tenant,
}
surface, err := xrpcserver.New(ctx, xrpcserver.Options{
Gateway: verbs,
Audiences: cfg.Audiences,
Directory: d.Directory,
})
if err != nil {
return nil, err
}
interval := d.SweepInterval
interval = cmp.Or(interval, plcsweep.DefaultInterval)
g := &Gateway{
handler: surface.Handler(),
surface: surface,
sweep: d.Sweep,
sweepInterval: interval,
log: log,
}
g.report(d)
return g, nil
}
func (g *Gateway) Handler() http.Handler { return g.handler }
func (g *Gateway) Close() { g.surface.Close() }
func (g *Gateway) Run(ctx context.Context) error {
if g.sweep == nil {
<-ctx.Done()
return nil
}
err := g.sweep.Run(ctx, g.sweepInterval, g.onPass)
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return nil
}
return err
}
func (g *Gateway) onPass(outcomes []plcsweep.Outcome) {
for _, o := range outcomes {
switch {
case o.Skipped:
g.log.Warn("plc sweep: did not resolve", "did", o.DID, "why", o.Why)
case o.Revoke:
g.log.Info("plc sweep: deny written", "did", o.DID, "why", o.Why)
default:
g.log.Debug("plc sweep: live", "did", o.DID, "why", o.Why)
}
}
}
func (g *Gateway) report(d Deps) {
if g.sweep == nil {
g.log.Warn("the PLC sweep is UNWIRED",
"missing", "Deps.Sweep: plcsweep.Worklist, plcsweep.Resolver and plcsweep.Revoker are all implemented, and main.buildDeps builds and wires all three. This warning means the composition root skipped that construction, so g.Run -- the only caller of sweep.Run -- has nothing to run",
"reached by", "a caller that supplies a nil Deps.Sweep, which today is the wiring smoke tests ONLY",
"consequence", "an account deleted at PLC between logins is not noticed by this process")
}
if d.Revoker == nil {
g.log.Warn("revokeIdentity is routed but UNWRITABLE",
"missing", "gateway.Revoker -- the deny's write half has no implementation",
"consequence", "revokeIdentity answers 503 ClusterUnavailable")
}
}