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
package marketnetwork
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/app/compose"
"github.com/johnandersen777/socialweb-computer-kcp/internal/bus"
contractvault "github.com/johnandersen777/socialweb-computer-kcp/internal/contract/vault"
)
const (
NetworkDir = "network"
NetworkVaultServer = "network"
NetworkVaultClient = "components"
NetworkPKIMount = "pki"
NetworkOwnerMount = "owner"
)
func NetworkStateDir(instanceState string) string { return filepath.Join(instanceState, NetworkDir) }
func (ctl *Controller) vaultReady(ctx context.Context, ws string) error {
o, err := compose.Get[v1alpha1.OpenBaoServer](ctx, ctl.c, ws, compose.OpenBaoServerGVR, NetworkVaultServer)
if err != nil {
return err
}
st := o.Status
if st.Phase != v1alpha1.OpenBaoServerPhaseReady || st.Address == "" || st.Refs == nil {
return fmt.Errorf("the OpenBaoServer %s is %s, not Ready", NetworkVaultServer, st.Phase)
}
for _, x := range st.Clients {
if x.Name == NetworkVaultClient && x.ClientCert != "" {
return nil
}
}
return fmt.Errorf("the OpenBaoServer %s reports no client %s yet", NetworkVaultServer, NetworkVaultClient)
}
func vaultLinks(r compose.Root) []compose.Link {
return append([]compose.Link{compose.VaultPKILink(r.Ref(), NetworkVaultServer, NetworkPKIMount)}, verifierLinks(r)...)
}
func (ctl *Controller) pkiCA(ctx context.Context, r compose.Root) ([]byte, error) {
pki, err := bus.Resolve[contractvault.PKI](ctx, ctl.c.Bus(), r.Ref(), compose.VaultRefIn(NetworkVaultServer, r.Workspace),
bus.Capability{Name: contractvault.CapabilityPKIIssue, Scope: []string{NetworkPKIMount}})
if err != nil {
return nil, err
}
pem, err := pki.CA(ctx, NetworkPKIMount)
if err != nil {
return nil, fmt.Errorf("the network's PKI root CA: %w", err)
}
return []byte(pem), nil
}
func (ctl *Controller) caBundle(ctx context.Context, r compose.Root) (string, error) {
if len(ctl.pkiPEM) == 0 {
pem, err := ctl.pkiCA(ctx, r)
if err != nil {
return "", err
}
ctl.pkiPEM = pem
}
guest, err := os.ReadFile(filepath.Join(NetworkStateDir(ctl.c.StateRoot()), GuestTLSDir, guestCAFile))
if err != nil {
return "", err
}
return strings.TrimSpace(string(ctl.pkiPEM)) + "\n" + strings.TrimSpace(string(guest)) + "\n", nil
}
func (ctl *Controller) oneKcp(ws string, apps []v1alpha1.NetworkAppStatus) []string {
front := ctl.c.KcpServer()
workspaces := map[string]string{ws: "the network"}
var wrong []string
for _, a := range apps {
if a.KcpURL == "" {
continue
}
if f, _, _ := strings.Cut(strings.TrimSuffix(a.KcpURL, "/"), "/clusters/"); f != front {
wrong = append(wrong, fmt.Sprintf("%s reports the kcp %s, not the network's %s", a.Name, f, front))
}
if a.Workspace == "" {
wrong = append(wrong, a.Name+" reports no workspace")
continue
}
if other, ok := workspaces[a.Workspace]; ok {
wrong = append(wrong, fmt.Sprintf("%s shares the workspace %s with %s", a.Name, a.Workspace, other))
}
workspaces[a.Workspace] = a.Name
}
return wrong
}
func (ctl *Controller) verifierTransport(req *http.Request) http.RoundTripper {
handlers := ctl.c.Bus()
if handlers == nil || req == nil || req.URL == nil {
return nil
}
host := strings.ToLower(req.URL.Hostname())
if host == "" {
return nil
}
if _, ok := handlers.HandlerForHost(host); ok {
return handlers.RoundTripper()
}
return nil
}
var verifierChecks = []struct{ kind, name, path string }{
{compose.ComponentKindPlc, compose.NetworkPLCName, "/health"},
{compose.ComponentKindRelay, compose.NetworkRelayName, "/xrpc/_health"},
{compose.ComponentKindPds, NetworkAlicePdsName, "/xrpc/_health"},
{compose.ComponentKindPds, NetworkBobPdsName, "/xrpc/_health"},
{compose.ComponentKindIngressProxy, compose.NetworkIngressName, "/.well-known/did.json"},
}
func verifierLinks(r compose.Root) []compose.Link {
out := make([]compose.Link, 0, len(verifierChecks))
for _, check := range verifierChecks {
out = append(out, compose.RefLink(r.Ref(), compose.AppRef(check.kind, check.name, r.Workspace), compose.Cap(bus.CapabilityXRPC)))
}
return out
}
func (ctl *Controller) verify(ctx context.Context, snap compose.Snapshot, apps []v1alpha1.NetworkAppStatus) error {
for _, check := range verifierChecks {
v, ok := snap.View(check.kind, check.name)
if !ok {
return fmt.Errorf("%s %s is not there", check.kind, check.name)
}
if v.Status.Phase == v1alpha1.ComponentPhaseSuspended {
continue
}
if !v.Status.Ready {
return fmt.Errorf("%s %s is not ready", check.kind, check.name)
}
e, ok := compose.ComponentEndpoint(v, "http")
if !ok {
return fmt.Errorf("%s %s reports no endpoint", check.kind, check.name)
}
base := compose.EndpointURL(e)
if base == "" {
return fmt.Errorf("%s %s reports no endpoint URL", check.kind, check.name)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimRight(base, "/")+check.path, nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/json")
if check.kind == compose.ComponentKindIngressProxy {
host := GuestTLSHost
if ctl.value != nil && ctl.value.IngressProxyHost != "" {
if h, _, ok := strings.Cut(ctl.value.IngressProxyHost, ":"); ok && h != "" {
host = h
} else {
host = ctl.value.IngressProxyHost
}
}
req.Host = host
}
client := &http.Client{Timeout: VerifierProbeTimeout}
if transport := ctl.verifierTransport(req); transport != nil {
client.Transport = transport
}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("%s %s at %s: %w", check.kind, check.name, check.path, err)
}
resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("%s %s at %s returned HTTP %d", check.kind, check.name, check.path, resp.StatusCode)
}
}
for _, a := range apps {
if a.Kind == v1alpha1.ApplicationKindMarketRequester || a.Kind == v1alpha1.ApplicationKindSshGateway {
continue
}
if a.Message == StoppedMessage || a.Phase == v1alpha1.ApplicationPhaseSuspended {
continue
}
if a.Phase != v1alpha1.ApplicationPhaseReady {
return fmt.Errorf("the child application %s is %s: %s", a.Name, a.Phase, a.Message)
}
}
return nil
}