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
package openbao
import (
"cmp"
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/url"
"strings"
"sync"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/atprotorepo"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/guestcp"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/host"
"github.com/johnandersen777/socialweb-computer-kcp/internal/guestidentity"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/opt"
)
const (
DefaultVaultClient = "guest-kcp"
DefaultTimeout = 10 * time.Second
DefaultIssuerRetry = 30 * time.Second
hostKeyProbe = time.Minute
)
type Config struct {
Handle string
DID string
Mount string
VaultClient string
Timeout time.Duration
GrantSync time.Duration
IssuerRetry time.Duration
HostKeyAttempts int
}
type Deps struct {
Inputs guestcp.Source
Guests guestcp.Guests
Grants guestcp.Grants
RBAC atprotorepo.RecordReader
FallbackIssuer func() string
Publish guestcp.Publish
Clock host.Clock
Log *slog.Logger
}
type Plane struct {
cfg Config
deps Deps
log *slog.Logger
clock host.Clock
mu sync.RWMutex
inputs guestcp.Inputs
probing map[string]bool
retrying bool
kick chan struct{}
issuerKick chan struct{}
identity *guestidentity.Client
verifier *guestidentity.Verifier
guest *guestidentity.Guest
syncer *guestidentity.Syncer
known map[string]string
cancel context.CancelFunc
unwatch func()
wg sync.WaitGroup
once sync.Once
}
var _ guestcp.Plane = (*Plane)(nil)
func New(cfg Config, deps Deps) (*Plane, error) {
switch {
case strings.TrimSpace(cfg.Handle) == "":
return nil, errors.New("guestcp: the guest control plane names no bidder handle")
case deps.Inputs == nil:
return nil, errors.New("guestcp: the guest control plane has no input source")
case deps.Guests == nil:
return nil, errors.New("guestcp: the guest control plane has no guest report writer")
case deps.Grants == nil:
return nil, errors.New("guestcp: the guest control plane has no grant writer")
case deps.RBAC == nil:
return nil, errors.New("guestcp: the guest control plane has no RBAC record reader")
}
cfg.VaultClient = cmp.Or(strings.TrimSpace(cfg.VaultClient), DefaultVaultClient)
cfg.Timeout = opt.Positive(cfg.Timeout, DefaultTimeout)
cfg.IssuerRetry = opt.Positive(cfg.IssuerRetry, DefaultIssuerRetry)
log := deps.Log
if log == nil {
log = slog.New(slog.DiscardHandler)
}
clock := deps.Clock
if clock == nil {
clock = systemClock{}
}
return &Plane{
cfg: cfg, deps: deps, log: log, clock: clock,
probing: map[string]bool{}, known: map[string]string{},
kick: make(chan struct{}, 1), issuerKick: make(chan struct{}, 1),
}, nil
}
func (p *Plane) Start(ctx context.Context) error {
initial, err := p.deps.Inputs.Inputs(ctx)
if err != nil {
return fmt.Errorf("guestcp: reading the guest control plane inputs: %w", err)
}
p.update(initial)
runCtx, cancel := context.WithCancel(ctx)
p.cancel = cancel
identity, err := guestidentity.NewClient(guestidentity.Options{Mount: p.cfg.Mount, Endpoint: p.endpoint, Timeout: p.cfg.Timeout})
if err != nil {
p.Close()
return err
}
p.identity = identity
issuer := func() string { return guestidentity.OIDCIssuer(p.IssuerURL()) }
p.verifier = guestidentity.NewVerifier(guestidentity.IdentityKeys(identity), issuer)
syncer, err := guestidentity.NewSyncer(runCtx, guestidentity.SyncerOptions{
Store: grantStore{p: p},
RBAC: &guestidentity.RepoRBAC{Reader: p.deps.RBAC, Log: p.log},
Issuer: issuer,
Log: p.log,
Resync: p.cfg.GrantSync,
})
if err != nil {
p.Close()
return err
}
p.syncer = syncer
guest, err := guestidentity.NewGuest(runCtx, guestidentity.GuestOptions{
Handle: p.cfg.Handle,
Identity: identity,
Verifier: p.verifier,
VMs: vmStore{p: p},
Log: p.log,
Now: p.clock.Now,
HostKeyAttempts: p.cfg.HostKeyAttempts,
Publish: p.publish,
})
if err != nil {
p.Close()
return err
}
p.guest = guest
p.unwatch = p.deps.Inputs.Watch(p.update)
p.wg.Go(func() { p.reconcileLoop(runCtx) })
p.wg.Go(func() { p.issuerLoop(runCtx) })
return nil
}
func (p *Plane) Close() {
p.once.Do(func() {
if p.unwatch != nil {
p.unwatch()
}
if p.cancel != nil {
p.cancel()
}
if p.guest != nil {
p.guest.Close()
}
if p.syncer != nil {
p.syncer.Close()
}
p.wg.Wait()
})
}
func (p *Plane) Ready() error {
if p == nil || p.guest == nil {
return errors.New("guestcp: the guest identity proxy is not configured")
}
return nil
}
func (p *Plane) DID() string {
p.mu.RLock()
defer p.mu.RUnlock()
return cmp.Or(strings.TrimSpace(p.cfg.DID), strings.TrimSpace(p.inputs.DID))
}
func (p *Plane) IssuerURL() string {
p.mu.RLock()
raw := exposureIssuer(p.inputs.Issuer)
p.mu.RUnlock()
if raw == "" && p.deps.FallbackIssuer != nil {
raw = strings.TrimSpace(p.deps.FallbackIssuer())
}
return strings.TrimRight(raw, "/")
}
func (p *Plane) OIDCIssuer() string {
raw := p.IssuerURL()
if raw == "" {
return ""
}
return guestidentity.OIDCIssuer(raw)
}
func (p *Plane) Recheck() {
nudge(p.issuerKick)
}
func (p *Plane) LoginStep(ctx context.Context, step string, input map[string]any) guestcp.Reply {
return guestcp.Reply(p.guest.LoginStep(ctx, step, input))
}
func (p *Plane) OIDCToken(ctx context.Context, role, vaultToken string) guestcp.Reply {
return guestcp.Reply(p.guest.OIDCToken(ctx, role, vaultToken))
}
func (p *Plane) Discovery(ctx context.Context) guestcp.Reply {
return guestcp.Reply(p.guest.Discovery(ctx))
}
func (p *Plane) Keys(ctx context.Context) guestcp.Reply {
return guestcp.Reply(p.guest.Keys(ctx))
}
func (p *Plane) Events(ctx context.Context, authorization string, request guestcp.EventsRequest) guestcp.Reply {
reply := p.guest.Events(ctx, authorization, guestidentity.EventsRequest(request))
return guestcp.Reply(p.withGuestLogin(reply))
}
func (p *Plane) publish(ctx context.Context, event guestidentity.GuestEvent) error {
if p.deps.Publish == nil {
return guestcp.ErrNoPublisher
}
record, err := json.Marshal(event.Record)
if err != nil {
return err
}
return p.deps.Publish(ctx, guestcp.GuestEvent{ComputeVM: event.ComputeVM, Collection: event.Collection, Tag: event.Tag, Kind: event.Kind, Record: record})
}
func (p *Plane) Authenticate(ctx context.Context, authorization string) (string, error) {
name, err := p.guest.Authenticate(ctx, authorization)
switch {
case errors.Is(err, guestidentity.ErrUnauthenticated):
return "", fmt.Errorf("%w: %v", guestcp.ErrUnauthenticated, err)
case errors.Is(err, guestidentity.ErrUnknownGuest):
return "", fmt.Errorf("%w: %v", guestcp.ErrUnknownGuest, err)
}
return name, err
}
func (p *Plane) update(in guestcp.Inputs) {
p.mu.Lock()
p.inputs = in
p.mu.Unlock()
nudge(p.kick)
nudge(p.issuerKick)
}
func (p *Plane) current() guestcp.Inputs {
p.mu.RLock()
defer p.mu.RUnlock()
return p.inputs
}
func (p *Plane) vm(name string) *v1alpha1.ComputeVM {
p.mu.RLock()
defer p.mu.RUnlock()
for i := range p.inputs.ComputeVMs {
if p.inputs.ComputeVMs[i].Name == name {
return p.inputs.ComputeVMs[i].DeepCopy()
}
}
return nil
}
func (p *Plane) grants() []v1alpha1.GuestIdentityGrant {
p.mu.RLock()
defer p.mu.RUnlock()
out := make([]v1alpha1.GuestIdentityGrant, 0, len(p.inputs.Grants))
for i := range p.inputs.Grants {
out = append(out, *p.inputs.Grants[i].DeepCopy())
}
return out
}
func (p *Plane) endpoint(context.Context) (guestidentity.Endpoint, error) {
p.mu.RLock()
status := p.inputs.OpenBao
p.mu.RUnlock()
if status == nil {
return guestidentity.Endpoint{}, errors.New("guestcp: the OpenBaoServer reports no status yet")
}
return guestidentity.EndpointFromServer(*status, p.cfg.VaultClient)
}
func exposureIssuer(raw string) string {
raw = strings.TrimRight(strings.TrimSpace(raw), "/")
parsed, err := url.Parse(raw)
if err != nil || parsed.Host == "" || (parsed.Scheme != "https" && parsed.Scheme != "http") {
return ""
}
return raw
}
func nudge(ch chan struct{}) {
select {
case ch <- struct{}{}:
default:
}
}
type systemClock struct{}
func (systemClock) Now() time.Time { return time.Now() }
func (systemClock) After(d time.Duration) <-chan time.Time { return time.After(d) }