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
package gateway
import (
"context"
"errors"
"fmt"
"log/slog"
"slices"
"strings"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/internal/revocation"
)
const MinClientCertTTL = 60 * time.Second
const (
DefaultClientCertTTL = 8 * time.Hour
MaxClientCertTTL = 24 * time.Hour
)
var ErrIssuerUnavailable = errors.New("gateway: the certificate issuer cannot issue now")
type Caller string
type XRPCError struct {
Name string
Message string
Status int
}
func (e *XRPCError) Error() string { return e.Name + ": " + e.Message }
func (e *XRPCError) HTTPStatus() int { return e.Status }
var (
ErrInvalidRequest = &XRPCError{"InvalidRequest", "the request is malformed", 400}
ErrUnauthorized = &XRPCError{"Unauthorized", "the caller is not authenticated", 401}
ErrForbidden = &XRPCError{"Forbidden", "the caller may not act for that identity", 403}
ErrIdentityRevoked = &XRPCError{
"IdentityRevoked", "this identity has been revoked", 403,
}
ErrClusterNotFound = &XRPCError{"ClusterNotFound", "no such cluster", 404}
ErrClusterInitializing = &XRPCError{
"ClusterInitializing", "the control plane is coming up", 429,
}
ErrClusterDraining = &XRPCError{"ClusterDraining", "the cluster is draining", 409}
ErrClusterUnavailable = &XRPCError{
"ClusterUnavailable", "the identity store could not be read", 503,
}
ErrNoDenyWriter = &XRPCError{
"ClusterUnavailable",
"this gateway was built without a revocation writer, so no deny can be written",
503,
}
)
func errForDeny(err error) *XRPCError {
switch {
case err == nil:
return nil
case errors.Is(err, revocation.ErrRevoked):
return ErrIdentityRevoked
case errors.Is(err, revocation.ErrDenyRecordMissing):
return &XRPCError{
"IdentityRevoked",
"the account's identity record is missing, which is not a state a user can produce",
403,
}
case errors.Is(err, revocation.ErrNoDID):
return ErrUnauthorized
case errors.Is(err, revocation.ErrDenyUnreadable):
return ErrClusterUnavailable
default:
return unprepared(err)
}
}
const maxCause = 300
func unprepared(cause error) *XRPCError {
return &XRPCError{
"ClusterUnavailable",
"the cluster could not be prepared: " + oneLine(cause.Error()),
503,
}
}
func oneLine(msg string) string {
line := strings.Join(strings.Fields(msg), " ")
if len(line) > maxCause {
line = line[:maxCause] + "..."
}
return line
}
type ClusterView struct {
Name string
Phase string
URL string
Instance string
Running bool
TeardownBlocked bool
NodeCount int
}
type Store interface {
Ensure(ctx context.Context, caller Caller, name string) error
Kubeconfig(ctx context.Context, caller Caller, name string, ttl time.Duration, noExpiry bool) (string, error)
Get(ctx context.Context, caller Caller, name string) (ClusterView, bool, error)
List(ctx context.Context, caller Caller) ([]ClusterView, error)
SetRunning(ctx context.Context, caller Caller, name string, running bool) (ClusterView, error)
Delete(ctx context.Context, caller Caller, name string) (ClusterView, error)
}
type Revoker interface {
WriteDeny(ctx context.Context, did string) error
FanOut(ctx context.Context, did string) ([]revocation.ClusterView, error)
revocation.Eraser
}
type CertRevoker interface {
Revoke(ctx context.Context, did, cluster string) (int, error)
}
type Server struct {
Deny revocation.Deny
Store Store
Revoker Revoker
Certs CertRevoker
Feed Feed
Tenant TenantAPI
}
type NameRequest struct {
Name string
}
type KubeconfigRequest struct {
Name string
TTLSeconds *int64
NoExpiry bool
}
type GetKubeconfigResponse struct {
Kubeconfig string
Instance string
}
func (s *Server) GetKubeconfig(ctx context.Context, caller Caller, req KubeconfigRequest) (*GetKubeconfigResponse, error) {
if req.NoExpiry && req.TTLSeconds != nil {
return nil, ErrInvalidRequest
}
ttl, err := clientCertTTL(req.TTLSeconds)
if err != nil {
return nil, err
}
if req.NoExpiry {
ttl = 0
}
if err := s.create(ctx, "getKubeconfig", caller, req.Name); err != nil {
return nil, err
}
view, ok, err := s.Store.Get(ctx, caller, req.Name)
if err != nil {
return nil, fmt.Errorf("getKubeconfig: %w", err)
}
if !ok {
return nil, ErrClusterNotFound
}
if !view.Running {
return nil, ErrClusterNotRunning
}
kubeconfig, err := s.Store.Kubeconfig(ctx, caller, req.Name, ttl, req.NoExpiry)
if err != nil {
if errors.Is(err, ErrIssuerUnavailable) {
slog.Warn("getKubeconfig: the certificate issuer is not ready", "caller", string(caller), "name", req.Name, "err", err)
return nil, ErrClusterInitializing
}
return nil, fmt.Errorf("getKubeconfig: %w", err)
}
return &GetKubeconfigResponse{Kubeconfig: kubeconfig, Instance: view.Instance}, nil
}
func (s *Server) CreateCluster(ctx context.Context, caller Caller, req NameRequest) (ClusterView, error) {
if err := s.create(ctx, "createCluster", caller, req.Name); err != nil {
return ClusterView{}, err
}
view, err := s.Store.SetRunning(context.WithoutCancel(ctx), caller, req.Name, true)
if err != nil {
return ClusterView{}, err
}
if view.Phase == "Draining" {
return ClusterView{}, ErrClusterDraining
}
return view, nil
}
func clientCertTTL(seconds *int64) (time.Duration, error) {
if seconds == nil {
return DefaultClientCertTTL, nil
}
if *seconds < int64(MinClientCertTTL/time.Second) || *seconds > int64(MaxClientCertTTL/time.Second) {
return 0, ErrInvalidRequest
}
return time.Duration(*seconds) * time.Second, nil
}
func (s *Server) create(ctx context.Context, verb string, caller Caller, name string) error {
if caller == "" {
return ErrUnauthorized
}
if name == "" {
return ErrInvalidRequest
}
permit, err := revocation.CreateOnRead(ctx, s.Deny, string(caller), func(ctx context.Context) error {
return s.Store.Ensure(ctx, caller, name)
})
if err != nil {
slog.Error("gateway: the create-on-read path failed",
"verb", verb, "caller", string(caller), "name", name, "err", err)
if x := errForDeny(err); x != nil {
return x
}
return fmt.Errorf("%s: %w", verb, err)
}
if permit.Admitted() != string(caller) {
return fmt.Errorf("%s: permit is for %q, caller is %q", verb, permit.Admitted(), caller)
}
return nil
}
func (s *Server) ControlPlaneUp(ctx context.Context, caller Caller, req NameRequest) (ClusterView, error) {
if caller == "" {
return ClusterView{}, ErrUnauthorized
}
if _, err := revocation.Admit(ctx, s.Deny, string(caller)); err != nil {
slog.Warn("controlPlaneUp: the deny refused the caller", "caller", string(caller), "name", req.Name, "err", err)
return ClusterView{}, errForDeny(err)
}
view, err := s.lookup(ctx, caller, req)
if err != nil {
return ClusterView{}, err
}
switch view.Phase {
case "Draining":
return ClusterView{}, ErrClusterDraining
case "Initializing":
return ClusterView{}, ErrClusterInitializing
case "Unavailable":
if view.Running {
if _, err := s.Store.SetRunning(ctx, caller, req.Name, false); err != nil {
return ClusterView{}, err
}
}
}
return s.Store.SetRunning(ctx, caller, req.Name, true)
}
func (s *Server) ControlPlaneDown(ctx context.Context, caller Caller, req NameRequest) (ClusterView, error) {
if _, err := s.lookup(ctx, caller, req); err != nil {
return ClusterView{}, err
}
return s.Store.SetRunning(ctx, caller, req.Name, false)
}
func (s *Server) DeleteCluster(ctx context.Context, caller Caller, req NameRequest) (ClusterView, error) {
view, err := s.lookup(ctx, caller, req)
if err != nil {
return ClusterView{}, err
}
if view.Phase != "Draining" {
s.revokeCerts(ctx, "deleteCluster", caller, req.Name)
}
return s.Store.Delete(ctx, caller, req.Name)
}
func (s *Server) revokeCerts(ctx context.Context, verb string, caller Caller, cluster string) {
if s.Certs == nil {
return
}
n, err := s.Certs.Revoke(ctx, string(caller), cluster)
if err != nil {
slog.Warn(verb+": the PKI revoke failed; the deny or the delete already ends access", "caller", string(caller), "name", cluster, "err", err)
return
}
slog.Info(verb+": certificates revoked", "caller", string(caller), "name", cluster, "count", n)
}
func (s *Server) ListClusters(ctx context.Context, caller Caller) ([]ClusterView, error) {
if caller == "" {
return nil, ErrUnauthorized
}
return s.Store.List(ctx, caller)
}
type RevokeRequest struct {
DID string
Name string
}
type RevokeResponse struct {
Revoked []revocation.ReplyEntry
}
func (s *Server) RevokeIdentity(ctx context.Context, caller Caller, req RevokeRequest) (*RevokeResponse, error) {
if caller == "" {
return nil, ErrUnauthorized
}
if req.DID != "" && req.DID != string(caller) {
return nil, ErrForbidden
}
if s.Revoker == nil {
return nil, ErrNoDenyWriter
}
if err := s.Revoker.WriteDeny(ctx, string(caller)); err != nil {
return nil, fmt.Errorf("revokeIdentity: write the deny: %w", err)
}
observed, err := s.Revoker.FanOut(ctx, string(caller))
if err != nil {
return nil, fmt.Errorf("revokeIdentity: %w", err)
}
if req.Name != "" && !slices.ContainsFunc(observed, func(c revocation.ClusterView) bool { return c.Name == req.Name }) {
return nil, ErrClusterNotFound
}
effects, err := revocation.Erase(ctx, s.Revoker, string(caller), revocation.Plan(observed))
if err != nil {
slog.Warn("revokeIdentity: a ServiceAccount delete failed; that cluster stays RevocationPending", "caller", string(caller), "err", err)
}
slog.Info("revokeIdentity: the deny is written", "caller", string(caller), "plan", string(revocation.AccountState(effects)), "clusters", revocation.Detail(effects))
if req.Name != "" {
effects = slices.DeleteFunc(effects, func(e revocation.Effect) bool { return e.Cluster != req.Name })
}
for _, e := range effects {
if e.Cluster != "" {
s.revokeCerts(ctx, "revokeIdentity", caller, e.Cluster)
}
}
return &RevokeResponse{Revoked: revocation.Reply(effects)}, nil
}
func (s *Server) lookup(ctx context.Context, caller Caller, req NameRequest) (ClusterView, error) {
if caller == "" {
return ClusterView{}, ErrUnauthorized
}
if req.Name == "" {
return ClusterView{}, ErrInvalidRequest
}
view, ok, err := s.Store.Get(ctx, caller, req.Name)
if err != nil {
return ClusterView{}, err
}
if !ok {
return ClusterView{}, ErrClusterNotFound
}
return view, nil
}