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
package kcpclient
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"strings"
"sync"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/clusterhome"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
)
const (
clusterClientCertificates = "clusterclientcertificates"
certWatchIdle = 10 * time.Minute
certWatchReady = 3 * time.Second
certWatchRetry = time.Second
)
func CertSerialKey(serial string) string {
return strings.TrimLeft(strings.ToLower(strings.NewReplacer(":", "", "-", "").Replace(serial)), "0")
}
func ClusterClientCertificateName(serial string) string {
return "ccc-" + CertSerialKey(serial)
}
type CertificateRecord struct {
DID string
Cluster string
Serial string
NotAfter time.Time
NoExpiry bool
}
type CertificateState struct {
Present bool
Revoked bool
}
func (c *Client) RecordCertificate(ctx context.Context, rec CertificateRecord) error {
home := clusterhome.HomePath(rec.DID, rec.Cluster)
cluster, err := c.readCluster(ctx, home, rec.Cluster)
if err != nil {
return fmt.Errorf("kcpclient: the cluster %s that owns the certificate record: %w", home, err)
}
r, err := c.resource(home, socialwebResource)
if err != nil {
return err
}
obj := v1alpha1.ClusterClientCertificate{
TypeMeta: metav1.TypeMeta{APIVersion: socialwebResource.String(), Kind: "ClusterClientCertificate"},
ObjectMeta: metav1.ObjectMeta{
Name: ClusterClientCertificateName(rec.Serial),
OwnerReferences: []metav1.OwnerReference{{APIVersion: socialwebResource.String(), Kind: "Cluster",
Name: cluster.Name, UID: cluster.UID}},
},
Spec: v1alpha1.ClusterClientCertificateSpec{ClusterRef: commonv1.LocalObjectRef{Name: rec.Cluster}, Did: rec.DID, Serial: CertSerialKey(rec.Serial),
NotAfter: rec.NotAfter.UTC().Format(time.RFC3339), NoExpiry: rec.NoExpiry},
}
body, err := json.Marshal(obj)
if err != nil {
return err
}
err = r.Post().Resource(clusterClientCertificates).Body(body).Do(ctx).Error()
if err != nil && !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kcpclient: the certificate record in %s: %w", home, err)
}
return nil
}
func (c *Client) MarkCertificateRevoked(ctx context.Context, did, cluster, serial string) error {
home := clusterhome.HomePath(did, cluster)
r, err := c.resource(home, socialwebResource)
if err != nil {
return err
}
name := ClusterClientCertificateName(serial)
err = r.Get().Resource(clusterClientCertificates).Name(name).Do(ctx).Error()
if apierrors.IsNotFound(err) {
return nil
}
if err != nil {
return fmt.Errorf("kcpclient: read the certificate record %s in %s: %w", name, home, err)
}
doc := certificateDoc(name)
doc["spec"] = map[string]any{"revoked": true}
err = applyDoc(ctx, r.Patch(types.ApplyPatchType).Resource(clusterClientCertificates).Name(name), fieldManagerRevocation, true, doc, false)
if apierrors.IsNotFound(err) {
return nil
}
if err != nil {
return fmt.Errorf("kcpclient: revoke the certificate record %s in %s: %w", name, home, err)
}
doc = certificateDoc(name)
doc["status"] = map[string]any{"revokedAt": time.Now().UTC().Format(time.RFC3339)}
err = applyDoc(ctx, r.Patch(types.ApplyPatchType).SubResource("status").Resource(clusterClientCertificates).Name(name),
fieldManagerRevocation, true, doc, true)
if err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("kcpclient: the revokedAt of the certificate record %s in %s: %w", name, home, err)
}
return nil
}
func certificateDoc(name string) map[string]any {
return map[string]any{"apiVersion": socialwebResource.String(), "kind": "ClusterClientCertificate", "metadata": map[string]any{"name": name}}
}
func (c *Client) CertificateState(ctx context.Context, did, cluster, serial string) (CertificateState, error) {
w := c.certWatcher(did, cluster)
w.touch()
select {
case <-w.ready:
case <-time.After(certWatchReady):
return CertificateState{}, fmt.Errorf("kcpclient: the certificate records of %s are not synced: %v", w.home, w.lastError())
case <-ctx.Done():
return CertificateState{}, ctx.Err()
}
if st := w.state(CertSerialKey(serial)); st.Present {
return st, nil
}
st, err := c.readCertificate(ctx, w.home, serial)
if err != nil {
return CertificateState{}, err
}
if st.Present {
c.dropCertWatch(w)
slog.Info("kcpclient: a certificate record its watch had not seen was read directly; the watch starts again", "home", w.home,
"record", ClusterClientCertificateName(serial))
}
return st, nil
}
func (c *Client) readCertificate(ctx context.Context, home, serial string) (CertificateState, error) {
r, err := c.resource(home, socialwebResource)
if err != nil {
return CertificateState{}, err
}
name := ClusterClientCertificateName(serial)
raw, err := r.Get().Resource(clusterClientCertificates).Name(name).DoRaw(ctx)
switch {
case apierrors.IsNotFound(err):
return CertificateState{}, nil
case err != nil:
return CertificateState{}, fmt.Errorf("kcpclient: read the certificate record %s in %s: %w", name, home, err)
}
var rec v1alpha1.ClusterClientCertificate
if err := json.Unmarshal(raw, &rec); err != nil {
return CertificateState{}, fmt.Errorf("kcpclient: decode the certificate record %s in %s: %w", name, home, err)
}
if CertSerialKey(rec.Spec.Serial) != CertSerialKey(serial) {
return CertificateState{}, nil
}
return CertificateState{Present: true, Revoked: rec.Spec.Revoked}, nil
}
func (c *Client) dropCertWatch(w *certWatch) {
c.certMu.Lock()
if c.certWatches[w.home] == w {
delete(c.certWatches, w.home)
}
c.certMu.Unlock()
w.mu.Lock()
idle := w.idle
w.mu.Unlock()
if idle != nil {
idle.Stop()
}
w.cancel()
}
type certWatch struct {
home string
ready chan struct{}
once sync.Once
cancel context.CancelFunc
mu sync.Mutex
items map[string]bool
idle *time.Timer
err error
}
func (w *certWatch) touch() {
w.mu.Lock()
idle := w.idle
w.mu.Unlock()
if idle != nil {
idle.Reset(certWatchIdle)
}
}
func (w *certWatch) lastError() error {
w.mu.Lock()
defer w.mu.Unlock()
return w.err
}
func (w *certWatch) state(key string) CertificateState {
w.mu.Lock()
defer w.mu.Unlock()
revoked, ok := w.items[key]
return CertificateState{Present: ok, Revoked: revoked}
}
func (w *certWatch) replace(items []v1alpha1.ClusterClientCertificate) {
next := make(map[string]bool, len(items))
for _, it := range items {
next[CertSerialKey(it.Spec.Serial)] = it.Spec.Revoked
}
w.mu.Lock()
w.items, w.err = next, nil
w.mu.Unlock()
w.once.Do(func() { close(w.ready) })
}
func (w *certWatch) apply(deleted bool, it v1alpha1.ClusterClientCertificate) error {
key := CertSerialKey(it.Spec.Serial)
w.mu.Lock()
defer w.mu.Unlock()
if deleted {
delete(w.items, key)
return nil
}
w.items[key] = it.Spec.Revoked
return nil
}
func (w *certWatch) fail(err error) {
w.mu.Lock()
w.err = err
w.mu.Unlock()
}
func (c *Client) certWatcher(did, cluster string) *certWatch {
home := clusterhome.HomePath(did, cluster)
c.certMu.Lock()
defer c.certMu.Unlock()
if w := c.certWatches[home]; w != nil {
return w
}
ctx, cancel := context.WithCancel(context.Background())
w := &certWatch{home: home, ready: make(chan struct{}), cancel: cancel, items: map[string]bool{}}
if c.certWatches == nil {
c.certWatches = map[string]*certWatch{}
}
c.certWatches[home] = w
idle := time.AfterFunc(certWatchIdle, func() {
c.certMu.Lock()
if c.certWatches[home] == w {
delete(c.certWatches, home)
}
c.certMu.Unlock()
cancel()
})
w.mu.Lock()
w.idle = idle
w.mu.Unlock()
go c.runCertWatch(ctx, w)
return w
}
func (c *Client) runCertWatch(ctx context.Context, w *certWatch) {
r, err := c.resource(w.home, socialwebResource)
if err != nil {
w.fail(err)
return
}
src := source[v1alpha1.ClusterClientCertificate]{r: r, resource: clusterClientCertificates, where: w.home}
for ctx.Err() == nil {
items, rv, err := src.list(ctx)
if err == nil {
w.replace(items)
err = follow(ctx, src, rv, w.apply, func(items []v1alpha1.ClusterClientCertificate) error {
w.replace(items)
return nil
})
}
if ctx.Err() != nil {
return
}
if err != nil && !errors.Is(err, context.Canceled) {
w.fail(err)
slog.Warn("kcpclient: the certificate record watch restarts", "home", w.home, "err", err.Error())
}
select {
case <-ctx.Done():
return
case <-time.After(certWatchRetry):
}
}
}