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
package openbao
import (
"context"
"encoding/json"
"net/http"
"reflect"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/guestidentity"
)
type vmStore struct {
p *Plane
}
var _ guestidentity.VMStore = vmStore{}
func (v vmStore) Get(_ context.Context, name string) (*v1alpha1.ComputeVM, error) {
return v.p.vm(name), nil
}
func (v vmStore) RecordGuest(ctx context.Context, name string, report v1alpha1.ComputeVMGuestReport) error {
if report.Address == "" && len(report.HostKeys) == 0 && report.ReportedAt == "" {
return nil
}
current := v.p.vm(name)
if current == nil {
return guestidentity.ErrUnknownGuest
}
if held := guestidentity.GuestReportOf(current); held != nil && reflect.DeepEqual(*held, report) {
return nil
}
return v.p.deps.Guests.RecordGuest(ctx, name, report)
}
type grantStore struct {
p *Plane
}
var _ guestidentity.GrantStore = grantStore{}
func (g grantStore) List(context.Context) ([]v1alpha1.GuestIdentityGrant, error) {
return g.p.grants(), nil
}
func (g grantStore) Apply(ctx context.Context, grant v1alpha1.GuestIdentityGrant) error {
return g.p.deps.Grants.Apply(ctx, grant)
}
func (g grantStore) Delete(ctx context.Context, name string) error {
return g.p.deps.Grants.Delete(ctx, name)
}
func (p *Plane) withGuestLogin(reply guestidentity.Reply) guestidentity.Reply {
if reply.Status != http.StatusOK {
return reply
}
body := map[string]any{}
if err := json.Unmarshal(reply.Body, &body); err != nil {
return reply
}
name, _ := body["name"].(string)
if name == "" {
return reply
}
vm := p.vm(name)
if vm == nil {
return reply
}
ref, err := guestidentity.GuestLoginRef(*vm, p.guestLoginScopes(name))
if err != nil {
return reply
}
body["guestLogin"] = ref
raw, err := json.Marshal(body)
if err != nil {
return reply
}
reply.Body = raw
return reply
}
func (p *Plane) guestLoginScopes(name string) []string {
scopes := []string{}
for _, grant := range p.grants() {
if grant.Spec.ComputeVMRef.Name != name {
continue
}
scopes = append(scopes, grant.Spec.Scopes...)
}
return scopes
}