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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
package requester
import (
"bytes"
"cmp"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"slices"
"strconv"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
commonv1 "github.com/johnandersen777/socialweb-computer-kcp/api/common/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/api/v1alpha1"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/cloudinit"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/market"
atprotooauth "github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/oauth"
"github.com/johnandersen777/socialweb-computer-kcp/internal/contract/vault"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/conds"
)
const (
DefaultAtircURL = "https://irc.fedfork.com"
AtircServiceAudience = "did:web:irc.fedfork.com#atirc"
AtircMintNSID = "chat.atirc.mint"
AtircRevokeNSID = "chat.atirc.revoke"
AtircTokenEnv = "ATIRC_TOKEN"
AtircSubhandleEnv = "ATIRC_SUBHANDLE"
AtircExpEnv = "ATIRC_EXP"
AtircTokenLifetime = 24 * time.Hour
AtircParentPath = "identity/atirc-parent"
AtircMintedPath = "contracts/_atirc-minted"
DefaultSecretsMount = "requester"
atircReleaseKey = "atircReleasePending"
atircMintedKey = "atircMinted"
atircMintLeased = "Leased"
atircMintPlain = "Unleased"
atircMintPresent = "Present"
atircCallTimeout = 30 * time.Second
maxAtircLabel = 14
maxAtircResponse = 64 << 10
)
type atircError struct {
base string
status int
name string
message string
}
func (e *atircError) Error() string {
return fmt.Sprintf("%s answered %d %s %s", e.base, e.status, e.name, e.message)
}
type atircMintInput struct {
Subhandle string `json:"subhandle"`
Exp int64 `json:"exp"`
Lease *atircLease `json:"lease,omitempty"`
}
type atircRef struct {
URI string `json:"uri"`
CID string `json:"cid"`
}
type atircLease struct {
Association *atircRef `json:"association,omitempty"`
Receipt atircRef `json:"receipt"`
Termination string `json:"termination"`
Contract string `json:"contract,omitempty"`
}
func (l *atircLease) equal(o *atircLease) bool {
switch {
case l == nil || o == nil:
return l == o
case (l.Association == nil) != (o.Association == nil):
return false
case l.Association != nil && *l.Association != *o.Association:
return false
}
return l.Receipt == o.Receipt && l.Termination == o.Termination && l.Contract == o.Contract
}
func contractLease(state *v1alpha1.NodeClaimContract, association *commonv1.StrongRef) (*atircLease, error) {
if state == nil || state.Receipt == nil || state.Receipt.URI == "" || state.Receipt.CID == "" || state.ReceiptOk == nil || !*state.ReceiptOk {
return nil, errors.New("the contract has no verified receipt; no atirc token is minted before it")
}
bidder, _, _ := strings.Cut(strings.TrimPrefix(state.Receipt.URI, "at://"), "/")
if !strings.HasPrefix(bidder, "did:") {
return nil, fmt.Errorf("the receipt %s is not in a DID repo", state.Receipt.URI)
}
rkey, _ := market.EventRKeys(market.TerminationKey(state.Receipt.URI))
lease := &atircLease{
Receipt: atircRef{URI: state.Receipt.URI, CID: state.Receipt.CID},
Termination: "at://" + bidder + "/" + market.ComputeEventsVMTerminatedNSID + "/" + rkey,
}
if state.Accept != nil {
lease.Contract = state.Accept.URI
}
if association != nil && association.URI != "" && association.CID != "" {
lease.Association = &atircRef{URI: association.URI, CID: association.CID}
}
return lease, nil
}
func atircTokenLeases(token string) ([]atircLease, bool) {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return nil, false
}
raw, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, false
}
var claims struct {
Leases []atircLease `json:"leases"`
}
if json.Unmarshal(raw, &claims) != nil || len(claims.Leases) == 0 {
return nil, false
}
return claims.Leases, true
}
func checkLeaseChain(got, parent []atircLease, own *atircLease) error {
if len(got) != len(parent)+1 {
return fmt.Errorf("the token carries %d leases, not the %d of its parent and its own", len(got), len(parent)+1)
}
for i := range parent {
if !got[i].equal(&parent[i]) {
return fmt.Errorf("the token's lease %d is not its parent's", i)
}
}
if !got[len(parent)].equal(own) {
return errors.New("the token's own lease is not the one minted")
}
return nil
}
var errAtircLeaseMissing = errors.New("the atirc endpoint answered a leased mint with a token that carries no leases claim")
func agentMintFailReason(err error) string {
if errors.Is(err, errAtircLeaseMissing) {
return "LeaseClaimMissing"
}
return "MintFailed"
}
func agentMintFailMessage(attempt int, err error) string {
if errors.Is(err, errAtircLeaseMissing) {
return fmt.Sprintf("attempt %d, not retried: %s", attempt, err.Error())
}
return fmt.Sprintf("attempt %d of %d: %s", attempt, associationRetryAttempts, err.Error())
}
func liveAtircEntry(entries []SecretEntry, path string, now time.Time) bool {
for _, e := range entries {
if e.Path != path {
continue
}
if token := atircToken(e.Value); token != "" {
if exp, ok := atircTokenExp(token, e.Value); ok && now.Before(exp) {
return true
}
}
}
return false
}
type atircRevokeInput struct {
Subhandle string `json:"subhandle"`
}
type atircIssuer struct {
bearer string
base string
label string
exp time.Time
parent bool
leases []atircLease
}
func (r *contractRunner) atircIssuer(ctx context.Context, contract v1alpha1.ComputeContract) (atircIssuer, error) {
q := r.q
if ref := q.cfg.Spec.AtircParentRef; ref != nil {
if contract.Spec.SecretsRef == nil || ref.Mount != contract.Spec.SecretsRef.Mount || ref.Path != AtircParentPath ||
!sameVault(ref.Vault, contract.Spec.SecretsRef.Vault) {
return atircIssuer{}, fmt.Errorf("spec.atircParentRef %s/%s is not %s in the requester's own mount", ref.Mount, ref.Path, AtircParentPath)
}
if q.deps.Vault == nil {
return atircIssuer{}, errors.New("the requester names an atirc parent token and no vault")
}
kv, err := q.deps.Vault(ctx, *ref)
if err != nil {
return atircIssuer{}, err
}
data, err := kv.Read(ctx, ref.Mount, ref.Path, 0)
if err != nil {
return atircIssuer{}, fmt.Errorf("reading the atirc parent token: %w", err)
}
token, _ := data["token"].(string)
subhandle, _ := data["subhandle"].(string)
token, subhandle = strings.TrimSpace(token), strings.ToLower(strings.TrimSpace(subhandle))
if token == "" || subhandle == "" || strings.ContainsAny(token, " \t\r\n") {
return atircIssuer{}, errors.New("the atirc parent token secret holds no token and subhandle")
}
exp, ok := atircTokenExp(token, "")
if !ok {
if raw, _ := data["exp"].(string); raw != "" {
if unix, err := strconv.ParseInt(raw, 10, 64); err == nil && unix > 0 {
exp, ok = time.Unix(unix, 0), true
}
}
}
if !ok {
return atircIssuer{}, errors.New("the atirc parent token has no known expiry")
}
leases, _ := atircTokenLeases(token)
return atircIssuer{bearer: token, base: subhandle, label: "vm", exp: exp, parent: true, leases: leases}, nil
}
return q.serviceAtircIssuer(ctx)
}
func (q *Requester) serviceAtircIssuer(ctx context.Context) (atircIssuer, error) {
handle := strings.ToLower(strings.TrimSpace(q.handle))
switch {
case handle == "":
return atircIssuer{}, errors.New("the requester identity has no handle")
case q.atircAuth == nil:
return atircIssuer{}, errors.New("the requester identity signs no service auth")
}
ctx, cancel := context.WithTimeout(ctx, atircCallTimeout)
defer cancel()
bearer, err := q.atircAuth.ServiceAuth(ctx, AtircServiceAudience, AtircMintNSID)
if err != nil {
return atircIssuer{}, fmt.Errorf("service auth for %s: %w", AtircServiceAudience, err)
}
label, _, _ := strings.Cut(handle, ".")
if len(label) > maxAtircLabel {
label = label[:maxAtircLabel]
}
return atircIssuer{bearer: bearer, base: handle, label: strings.TrimRight(label, "-")}, nil
}
func atircChild(label, rkey, base string) string {
label, rkey = strings.TrimRight(label, "-"), strings.ToLower(rkey)
if label == "" || rkey == "" || base == "" {
return ""
}
return label + "-" + rkey + "." + base
}
func uriRKey(uri string) string {
return uri[strings.LastIndex(uri, "/")+1:]
}
func (r *contractRunner) ensureAtircToken(ctx context.Context, contract v1alpha1.ComputeContract, state *v1alpha1.NodeClaimContract) {
envFile, err := agentEnvFile(contract)
if err != nil || envFile == "" || r.released() {
return
}
rfpURI := ""
if state != nil {
rfpURI = state.RFPURI
}
reason := ""
lease, err := contractLease(state, nil)
if err == nil {
reason, err = r.writeAtircToken(ctx, contract, envFile, "", rfpURI, time.Time{}, lease)
}
if err == nil {
r.noteAgentMint(ctx, true, reason, "the atirc agent token is in the bundle ("+reason+")")
return
}
r.log().Warn("requester_atirc_mint_failed", "envFile", envFile, "attempt", 1, "err", err.Error())
r.noteAgentMint(ctx, false, agentMintFailReason(err), agentMintFailMessage(1, err))
if lease == nil || errors.Is(err, errAtircLeaseMissing) {
return
}
if _, retrying := r.q.atircMintRetrying.LoadOrStore(contract.Name, true); retrying {
return
}
r.q.wg.Go(func() {
defer r.q.atircMintRetrying.Delete(contract.Name)
for attempt := 2; attempt <= associationRetryAttempts; attempt++ {
if !sleepRetry(ctx, attempt-1) {
return
}
current := r.snapshot()
if current.Status.AtircRevokes != "" || r.released() {
return
}
reason, err := r.writeAtircToken(ctx, current, envFile, "", rfpURI, time.Time{}, lease)
if err == nil {
r.log().Info("requester_atirc_minted_after_retry", "envFile", envFile, "attempt", attempt)
r.noteAgentMint(ctx, true, reason, fmt.Sprintf("the atirc agent token is in the bundle (%s, attempt %d)", reason, attempt))
return
}
r.log().Warn("requester_atirc_mint_failed", "envFile", envFile, "attempt", attempt, "err", err.Error())
r.noteAgentMint(ctx, false, agentMintFailReason(err), agentMintFailMessage(attempt, err))
if errors.Is(err, errAtircLeaseMissing) {
return
}
}
r.log().Warn("requester_atirc_mint_gave_up", "envFile", envFile, "attempts", associationRetryAttempts)
r.noteAgentMint(ctx, false, "GaveUp", fmt.Sprintf("no atirc agent token after %d attempts", associationRetryAttempts))
})
}
type atircMintOutcome struct {
ok bool
reason string
message string
}
func (r *contractRunner) noteAgentMint(ctx context.Context, ok bool, reason, message string) {
if r.isGone() {
return
}
r.mu.Lock()
r.atircMint = &atircMintOutcome{ok: ok, reason: reason, message: message}
r.mu.Unlock()
contract := r.snapshot()
status := v1alpha1.ComputeContractStatus{Conditions: append([]metav1.Condition(nil), contract.Status.Conditions...)}
conds.SetStatusAt(&status.Conditions, contract.Generation, v1alpha1.ComputeContractConditionAtircTokenMinted, conds.Status(ok), reason, message, r.q.now())
if err := r.q.deps.Intents.MergeComputeContractStatus(ctx, r.name, status); err != nil && !notFound(err) {
r.log().Warn("requester_atirc_mint_status_failed", "err", err.Error())
}
}
func (r *contractRunner) agentMintOutcome() *atircMintOutcome {
r.mu.Lock()
defer r.mu.Unlock()
return r.atircMint
}
func (r *contractRunner) ensureGuestRequesterToken(ctx context.Context, contract v1alpha1.ComputeContract, state *v1alpha1.NodeClaimContract, association commonv1.StrongRef, deadline time.Time) error {
if !fromGuest(contract) || state == nil || state.Accept == nil {
return nil
}
lease, err := contractLease(state, &association)
if err != nil {
return err
}
_, err = r.writeAtircToken(ctx, contract, cloudinit.GuestRequesterAtircEnvFile, "r2", state.Accept.URI, deadline, lease)
return err
}
func (r *contractRunner) mintedEntry(ctx context.Context, contract v1alpha1.ComputeContract, entry SecretEntry, fresh bool) {
r.addSecretEntries(entry)
r.mu.Lock()
secrets := r.secrets
r.mu.Unlock()
withheld := secrets.minted(entry.Path)
if entry.Path == cloudinit.GuestRequesterAtircEnvFile || (!withheld && !fresh) {
return
}
if err := r.notifySecretsUpdated(ctx, contract, commonv1.StrongRef{URI: entry.Path}); err != nil {
r.log().Warn("requester_atirc_minted_notify_failed", "path", entry.Path, "err", err.Error())
}
}
func (r *contractRunner) postMint(ctx context.Context, bearer string, in atircMintInput) (string, error) {
mintCtx, cancel := context.WithTimeout(ctx, atircCallTimeout)
defer cancel()
var out struct {
Token string `json:"token"`
}
if err := r.postAtirc(mintCtx, AtircMintNSID, bearer, in, &out); err != nil {
return "", fmt.Errorf("mint %s: %w", in.Subhandle, err)
}
token := strings.TrimSpace(out.Token)
if token == "" || strings.ContainsAny(token, " \t\r\n") {
return "", errors.New(r.atircBase() + " answered no token")
}
return token, nil
}
func (r *contractRunner) mintAtirc(ctx context.Context, issuer atircIssuer, by atircRevokers, in atircMintInput) (string, string, error) {
token, err := r.postMint(ctx, issuer.bearer, in)
if err != nil {
return "", "", err
}
if in.Lease == nil {
return token, atircMintPlain, nil
}
refuse := func(cause error) (string, string, error) {
if _, revokeErr := r.revokeMinted(ctx, by, token, in.Subhandle); revokeErr != nil {
return "", "", &atircUnrevoked{token: token, subhandle: in.Subhandle,
err: fmt.Errorf("mint %s: %w; its revoke failed: %w", in.Subhandle, cause, revokeErr)}
}
return "", "", fmt.Errorf("mint %s: %w", in.Subhandle, cause)
}
leases, ok := atircTokenLeases(token)
if !ok {
return refuse(fmt.Errorf("%w (%s)", errAtircLeaseMissing, r.atircBase()))
}
if err := checkLeaseChain(leases, issuer.leases, in.Lease); err != nil {
return refuse(err)
}
return token, atircMintLeased, nil
}
func (r *contractRunner) writeAtircToken(ctx context.Context, contract v1alpha1.ComputeContract, path, label, uri string, deadline time.Time, lease *atircLease) (string, error) {
q := r.q
if err := CheckTokenPaths(contract, nil); err != nil {
return "", fmt.Errorf("no token is minted: %w", err)
}
switch {
case q.deps.VaultWrite == nil:
return "", errors.New("the requester has no vault write handle")
case contract.Spec.SecretsRef == nil:
return "", errors.New("the contract has no secrets bundle")
case uri == "":
return "", errors.New("the contract has no record to name the subhandle after yet")
case contract.Status.AtircRevokes != "" || r.snapshot().Status.AtircRevokes != "":
return "", errors.New("the contract's atirc tokens are revoked; none is minted again")
}
r.bundleMu.Lock()
defer r.bundleMu.Unlock()
ref := *contract.Spec.SecretsRef
kv, data, entries, err := r.readAtircBundle(ctx, ref)
if err != nil {
return "", fmt.Errorf("read the bundle: %w", err)
}
marker, err := markerIn(data)
if err != nil {
return "", fmt.Errorf("no token is minted: %w", err)
}
if marker.ownedBy(string(contract.UID)) || r.released() || q.revokesStarted(contract) {
return "", errors.New("the contract's release has started; no atirc token is minted")
}
current := SecretEntry{Path: path, Mode: "0600"}
at := -1
for i, e := range entries {
if e.Path == path {
current, at = e, i
}
}
if token := atircToken(current.Value); token != "" {
if exp, ok := atircTokenExp(token, current.Value); ok && q.now().Before(exp) {
r.mintedEntry(ctx, contract, current, false)
return atircMintPresent, nil
}
}
issuer, err := r.atircIssuer(ctx, contract)
if err != nil {
return "", err
}
if label == "" {
label = issuer.label
}
subhandle := atircChild(label, uriRKey(uri), issuer.base)
if subhandle == "" {
return "", fmt.Errorf("%q and %q make no subhandle", issuer.base, uri)
}
exp := q.now().Add(AtircTokenLifetime)
for _, bound := range []time.Time{deadline, issuer.exp} {
if !bound.IsZero() && bound.Before(exp) {
exp = bound
}
}
if !exp.After(q.now()) {
return "", fmt.Errorf("the token of %s would expire at %s", subhandle, exp.UTC().Format(time.RFC3339))
}
if err := q.indexMinted(ctx, contract, exp); err != nil {
r.log().Warn("requester_atirc_index_failed", "subhandle", subhandle, "err", err.Error())
return "", fmt.Errorf("no token is minted while the atirc minted index cannot record %s: %w", subhandle, err)
}
token, reason, err := r.mintAtirc(ctx, issuer, r.revokers(ctx, contract), atircMintInput{Subhandle: subhandle, Exp: exp.Unix(), Lease: lease})
var unrevoked *atircUnrevoked
if errors.As(err, &unrevoked) {
r.recordUnrevoked(ctx, kv, ref, data, contract, path, exp, unrevoked)
}
if err != nil {
return "", err
}
if claimed, ok := atircTokenExp(token, ""); ok {
exp = claimed
}
current.Value = withAtircToken(current.Value, token, subhandle, exp)
current.Mode = "0600"
if at < 0 {
entries = append(entries, current)
} else {
entries[at] = current
}
raw, err := json.Marshal(entries)
if err != nil {
return "", err
}
next := make(map[string]any, len(data)+1)
for k, v := range data {
next[k] = v
}
next["bundle"] = string(raw)
minted, err := withMinted(data, atircMinted{Path: path, Subhandle: subhandle, Token: token, Exp: exp.Unix(), UID: string(contract.UID)}, q.now())
if err != nil {
return "", err
}
next[atircMintedKey] = minted
if err := kv.Write(ctx, ref.Mount, ref.Path, next); err != nil {
return "", fmt.Errorf("write the bundle: %w", err)
}
r.mintedEntry(ctx, contract, current, true)
r.log().Info("requester_atirc_minted", "subhandle", subhandle, "path", path, "exp", exp.UTC().Format(time.RFC3339), "lease", reason)
return reason, nil
}
type atircUnrevoked struct {
token string
subhandle string
err error
}
func (e *atircUnrevoked) Error() string { return e.err.Error() }
func (e *atircUnrevoked) Unwrap() error { return e.err }
func (r *contractRunner) recordUnrevoked(ctx context.Context, kv vault.KV, ref commonv1.SecretRef, data map[string]any,
contract v1alpha1.ComputeContract, path string, exp time.Time, u *atircUnrevoked) {
if claimed, ok := atircTokenExp(u.token, ""); ok {
exp = claimed
}
minted, err := withMinted(data, atircMinted{Path: path, Subhandle: u.subhandle, Token: u.token, Exp: exp.Unix(), UID: string(contract.UID)}, r.q.now())
if err == nil {
next := make(map[string]any, len(data)+1)
for k, v := range data {
next[k] = v
}
next[atircMintedKey] = minted
err = kv.Write(ctx, ref.Mount, ref.Path, next)
}
if err != nil {
r.log().Warn("requester_atirc_unrevoked_unrecorded", "subhandle", u.subhandle, "path", path, "err", err.Error())
return
}
r.log().Warn("requester_atirc_unrevoked_recorded", "subhandle", u.subhandle, "path", path, "exp", exp.UTC().Format(time.RFC3339))
}
type atircMinted struct {
Path string `json:"path"`
Subhandle string `json:"subhandle"`
Token string `json:"token"`
Exp int64 `json:"exp"`
UID string `json:"uid,omitempty"`
Revoked int64 `json:"revoked,omitempty"`
}
func mintedOf(data map[string]any) ([]atircMinted, error) {
text, _ := data[atircMintedKey].(string)
if text == "" {
return nil, nil
}
var out []atircMinted
if err := json.Unmarshal([]byte(text), &out); err != nil {
return nil, fmt.Errorf("parse the minted atirc tokens: %w", err)
}
return out, nil
}
func withMinted(data map[string]any, add atircMinted, now time.Time) (string, error) {
minted, err := mintedOf(data)
if err != nil {
return "", err
}
kept := []atircMinted{add}
for _, m := range minted {
if m.Exp > now.Unix() && m != add {
kept = append(kept, m)
}
}
raw, err := json.Marshal(kept)
return string(raw), err
}
func atircTokenExp(token, env string) (time.Time, bool) {
parts := strings.Split(token, ".")
if len(parts) == 3 {
if raw, err := base64.RawURLEncoding.DecodeString(parts[1]); err == nil {
var claims struct {
Exp int64 `json:"exp"`
}
if json.Unmarshal(raw, &claims) == nil && claims.Exp > 0 {
return time.Unix(claims.Exp, 0), true
}
}
}
if exp, err := strconv.ParseInt(atircEnvValue(env, AtircExpEnv), 10, 64); err == nil && exp > 0 {
return time.Unix(exp, 0), true
}
return time.Time{}, false
}
func releaseTokenPaths(contract v1alpha1.ComputeContract) []string {
var paths []string
if envFile, err := agentEnvFile(contract); err == nil && envFile != "" {
paths = append(paths, envFile)
}
if fromGuest(contract) {
paths = append([]string{cloudinit.GuestRequesterAtircEnvFile}, paths...)
}
return paths
}
func sameVault(a, b *commonv1.ObjectRef) bool {
if a == nil || b == nil {
return a == b
}
return *a == *b
}
type atircReleaseMarker struct {
Paths []string `json:"paths"`
StartedAt int64 `json:"startedAt"`
UID string `json:"uid,omitempty"`
}
func markerIn(data map[string]any) (*atircReleaseMarker, error) {
text, _ := data[atircReleaseKey].(string)
if text == "" {
return nil, nil
}
var marker atircReleaseMarker
if err := json.Unmarshal([]byte(text), &marker); err != nil {
return nil, fmt.Errorf("parse the release marker: %w", err)
}
return &marker, nil
}
func (m *atircReleaseMarker) ownedBy(uid string) bool {
return m != nil && (m.UID == "" || uid == "" || m.UID == uid)
}
func ReleaseRevokesExpected(contract v1alpha1.ComputeContract) bool {
return contract.Spec.SecretsRef != nil && len(releaseTokenPaths(contract)) > 0
}
func ReleaseWait(contract v1alpha1.ComputeContract, base time.Duration) time.Duration {
wait := base
if fromGuest(contract) {
wait += GuestRequesterReleaseTimeout
}
if acceptedWithoutReceipt(contract.Status.Contract) {
wait += DeniedAcceptReceiptAttempts * DefaultReceiptTimeout
}
return wait
}
func (q *Requester) revokesStarted(contract v1alpha1.ComputeContract) bool {
_, started := q.atircRevokesStarted.Load(contract.Name + "/" + string(contract.UID))
return started
}
func (q *Requester) markRevokesStarted(contract v1alpha1.ComputeContract) {
q.atircRevokesStarted.Store(contract.Name+"/"+string(contract.UID), true)
}
func (r *contractRunner) revokeAtircToken(ctx context.Context) (v1alpha1.AtircRevokesState, func()) {
contract := r.snapshot()
r.q.markRevokesStarted(contract)
if !ReleaseRevokesExpected(contract) || r.q.deps.VaultWrite == nil {
return "", nil
}
if state := contract.Status.AtircRevokes; state == v1alpha1.AtircRevokesDone || state == v1alpha1.AtircRevokesAbandoned {
return state, nil
}
paths := releaseTokenPaths(contract)
marker := &atircReleaseMarker{Paths: paths, StartedAt: r.q.now().Unix(), UID: string(contract.UID)}
first, cancel := context.WithTimeout(context.WithoutCancel(ctx), time.Duration(len(paths)+1)*atircCallTimeout)
defer cancel()
if err := r.setReleaseMarker(first, contract, marker); err != nil {
r.log().Warn("requester_atirc_release_marker_failed", "err", err.Error())
return v1alpha1.AtircRevokesPending, func() { r.q.startReleaseRevokes(r, contract, marker, true) }
}
var left []string
for _, path := range paths {
if !r.revokeEnvToken(first, contract, path) {
left = append(left, path)
}
}
r.log().Info("requester_atirc_release_revoke", "attempt", 0, "left", len(left))
if len(left) == 0 {
if err := r.setReleaseMarker(first, contract, nil); err == nil {
return v1alpha1.AtircRevokesDone, nil
}
}
return v1alpha1.AtircRevokesPending, func() { r.q.startReleaseRevokes(r, contract, marker, false) }
}
func (r *contractRunner) holdsLeasedToken(ctx context.Context, contract v1alpha1.ComputeContract) bool {
r.bundleMu.Lock()
_, data, entries, err := r.readAtircBundle(ctx, *contract.Spec.SecretsRef)
r.bundleMu.Unlock()
if err != nil {
r.log().Warn("requester_atirc_revoke_before_destroy_skipped", "step", "read the bundle", "err", err.Error())
return false
}
minted, _ := mintedOf(data)
for _, m := range minted {
if _, leased := atircTokenLeases(m.Token); leased && r.q.now().Before(time.Unix(m.Exp, 0)) {
return true
}
}
paths := releaseTokenPaths(contract)
for _, e := range entries {
if _, leased := atircTokenLeases(atircToken(e.Value)); leased && slices.Contains(paths, e.Path) {
return true
}
}
return false
}
func (r *contractRunner) revokeBeforeDestroy(ctx context.Context) {
contract := r.snapshot()
state := contract.Status.AtircRevokes
if !ReleaseRevokesExpected(contract) || r.q.deps.VaultWrite == nil || state == v1alpha1.AtircRevokesDone || state == v1alpha1.AtircRevokesAbandoned {
return
}
read, cancel := context.WithTimeout(context.WithoutCancel(ctx), atircCallTimeout)
leased := r.holdsLeasedToken(read, contract)
cancel()
if !leased {
return
}
var (
start func()
ran bool
)
r.atircRevoke.Do(func() {
ran = true
state, start = r.revokeAtircToken(ctx)
})
if !ran || state == "" {
return
}
r.log().Info("requester_atirc_revoke_before_destroy", "atircRevokes", state)
r.mergeRevokes(context.WithoutCancel(ctx), state)
if start != nil {
start()
}
}
func (r *contractRunner) mergeRevokes(ctx context.Context, state v1alpha1.AtircRevokesState) {
if err := r.q.deps.Intents.MergeComputeContractStatus(ctx, r.name, v1alpha1.ComputeContractStatus{AtircRevokes: state}); err != nil && !notFound(err) {
r.q.log.Warn("requester_atirc_revokes_status_failed", "contract", r.name, "err", err.Error())
}
}
func (r *contractRunner) setReleaseMarker(ctx context.Context, contract v1alpha1.ComputeContract, marker *atircReleaseMarker) error {
r.bundleMu.Lock()
defer r.bundleMu.Unlock()
ref := *contract.Spec.SecretsRef
kv, data, _, err := r.readAtircBundle(ctx, ref)
if err != nil {
return err
}
if marker == nil {
current, err := markerIn(data)
if err == nil && current != nil && !current.ownedBy(string(contract.UID)) {
return nil
}
}
next := make(map[string]any, len(data)+1)
for k, v := range data {
if k != atircReleaseKey {
next[k] = v
}
}
if marker != nil {
raw, err := json.Marshal(marker)
if err != nil {
return err
}
next[atircReleaseKey] = string(raw)
} else if _, had := data[atircReleaseKey]; !had {
return nil
}
return kv.Write(ctx, ref.Mount, ref.Path, next)
}
func (r *contractRunner) releaseMarker(ctx context.Context, contract v1alpha1.ComputeContract) (*atircReleaseMarker, error) {
r.bundleMu.Lock()
defer r.bundleMu.Unlock()
_, data, _, err := r.readAtircBundle(ctx, *contract.Spec.SecretsRef)
if err != nil {
return nil, err
}
marker, err := markerIn(data)
if err != nil || !marker.ownedBy(string(contract.UID)) {
return nil, err
}
return marker, nil
}
func (q *Requester) startReleaseRevokes(r *contractRunner, contract v1alpha1.ComputeContract, marker *atircReleaseMarker, write bool) {
if _, running := q.releaseRevoking.LoadOrStore(contract.Name, true); running {
return
}
go func() {
defer q.releaseRevoking.Delete(contract.Name)
if write && !r.writeReleaseMarker(contract, marker) {
return
}
r.releaseRevokes(contract, marker)
}()
}
func (r *contractRunner) writeReleaseMarker(contract v1alpha1.ComputeContract, marker *atircReleaseMarker) bool {
stop := r.q.runContext()
for attempt := 1; ; attempt++ {
ctx, cancel := context.WithTimeout(stop, atircCallTimeout)
err := r.setReleaseMarker(ctx, contract, marker)
cancel()
if err == nil {
return true
}
r.q.log.Warn("requester_atirc_release_marker_failed", "contract", r.name, "attempt", attempt, "err", err.Error())
if !r.waitRetry(stop, attempt) {
return false
}
if !r.q.now().Before(time.Unix(marker.StartedAt, 0).Add(AtircTokenLifetime)) {
r.q.log.Warn("requester_atirc_release_revoke_gave_up", "contract", r.name, "step", "the release marker")
return false
}
}
}
func (r *contractRunner) waitRetry(stop context.Context, attempt int) bool {
timer := time.NewTimer(min(associationRetryFirst<<min(attempt-1, 10), associationRetryMax))
defer timer.Stop()
select {
case <-stop.Done():
r.q.log.Info("requester_atirc_release_revoke_left_for_restart", "contract", r.name)
return false
case <-timer.C:
return true
}
}
func (q *Requester) resumeReleaseRevokes(contract v1alpha1.ComputeContract) {
if !ReleaseRevokesExpected(contract) || q.deps.VaultWrite == nil {
return
}
q.markRevokesStarted(contract)
if state := contract.Status.AtircRevokes; state == v1alpha1.AtircRevokesDone || state == v1alpha1.AtircRevokesAbandoned {
return
}
if _, running := q.releaseRevoking.LoadOrStore(contract.Name, true); running {
return
}
r := newContractRunner(q, contract)
go func() {
var marker *atircReleaseMarker
for attempt := 1; ; attempt++ {
ctx, cancel := context.WithTimeout(q.runContext(), atircCallTimeout)
found, err := r.releaseMarker(ctx, contract)
cancel()
if err == nil {
marker = found
break
}
q.log.Warn("requester_atirc_release_marker_failed", "contract", contract.Name, "attempt", attempt, "err", err.Error())
if attempt >= associationRetryAttempts || !r.waitRetry(q.runContext(), attempt) {
q.releaseRevoking.Delete(contract.Name)
return
}
}
write := marker == nil || len(marker.Paths) == 0
if write {
marker = &atircReleaseMarker{Paths: releaseTokenPaths(contract), StartedAt: q.now().Unix(), UID: string(contract.UID)}
}
if contract.Status.AtircRevokes != v1alpha1.AtircRevokesPending {
pending, cancelPending := context.WithTimeout(q.runContext(), atircCallTimeout)
r.mergeRevokes(pending, v1alpha1.AtircRevokesPending)
cancelPending()
}
q.releaseRevoking.Delete(contract.Name)
q.log.Info("requester_atirc_release_revoke_resumed", "contract", contract.Name, "paths", marker.Paths, "rebuilt", write)
q.startReleaseRevokes(r, contract, marker, write)
}()
}
func (r *contractRunner) releaseRevokes(contract v1alpha1.ComputeContract, marker *atircReleaseMarker) {
stop := r.q.runContext()
done := map[string]bool{}
giveUp := time.Unix(marker.StartedAt, 0).Add(AtircTokenLifetime)
for attempt := 1; ; attempt++ {
ctx, cancel := context.WithTimeout(stop, atircCallTimeout)
left := 0
for _, path := range marker.Paths {
if !done[path] {
done[path] = r.revokeEnvToken(ctx, contract, path)
}
if !done[path] {
left++
}
}
cleared := true
if left == 0 {
if err := r.setReleaseMarker(ctx, contract, nil); err != nil {
cleared = false
r.q.log.Warn("requester_atirc_release_marker_failed", "contract", r.name, "step", "clear", "err", err.Error())
}
}
if left == 0 && cleared {
r.mergeRevokes(ctx, v1alpha1.AtircRevokesDone)
}
cancel()
r.q.log.Info("requester_atirc_release_revoke", "contract", r.name, "attempt", attempt, "left", left)
if left == 0 && cleared {
return
}
if !r.q.now().Before(giveUp) {
r.q.log.Warn("requester_atirc_release_revoke_gave_up", "contract", r.name, "left", left)
gone, cancelGone := context.WithTimeout(context.WithoutCancel(stop), atircCallTimeout)
r.mergeRevokes(gone, v1alpha1.AtircRevokesAbandoned)
cancelGone()
return
}
if !r.waitRetry(stop, attempt) {
return
}
}
}
func (r *contractRunner) revokeEnvToken(ctx context.Context, contract v1alpha1.ComputeContract, path string) bool {
r.bundleMu.Lock()
_, data, entries, err := r.readAtircBundle(ctx, *contract.Spec.SecretsRef)
r.bundleMu.Unlock()
if err != nil {
r.q.log.Warn("requester_atirc_revoke_failed", "contract", r.name, "step", "read the bundle", "err", err.Error())
return false
}
minted, err := mintedOf(data)
if err != nil {
r.q.log.Warn("requester_atirc_revoke_failed", "contract", r.name, "step", "read the minted tokens", "err", err.Error())
return false
}
recorded, done := false, true
by := r.revokers(ctx, contract)
var cleared []atircMinted
for _, m := range minted {
if m.Path != path {
continue
}
recorded = true
if m.Revoked != 0 || !r.q.now().Before(time.Unix(m.Exp, 0)) {
continue
}
result, err := r.revokeMinted(ctx, by, m.Token, m.Subhandle)
if err != nil {
done = false
continue
}
if result == atircRevokeServer {
cleared = append(cleared, m)
}
}
r.markMintedRevoked(ctx, *contract.Spec.SecretsRef, cleared)
if recorded {
return done
}
token, subhandle, env := "", "", ""
for _, e := range entries {
if e.Path == path {
token, subhandle, env = atircToken(e.Value), atircEnvSubhandle(e.Value), e.Value
}
}
if token == "" {
return true
}
derived := subhandle == ""
if derived {
subhandle = r.legacySubhandle(contract, path)
}
if subhandle == "" {
r.q.log.Warn("requester_atirc_revoke_failed", "contract", r.name, "path", path, "step", "the token names no subhandle and none can be derived")
return false
}
if exp, ok := atircTokenExp(token, env); ok && !r.q.now().Before(exp) {
return true
}
var result atircRevokeResult
if derived {
result, err = r.revokeAtircSubhandle(ctx, token, subhandle, atircRevokeViaToken)
} else {
result, err = r.revokeMinted(ctx, atircRevokers{account: by.account}, token, subhandle)
}
if err != nil {
return false
}
if result == atircRevokeAlready && derived {
r.q.log.Warn("requester_atirc_revoke_unconfirmed", "contract", r.name, "path", path, "subhandle", subhandle,
"reason", "a derived subhandle answered 401; the token stays pending until it expires")
return false
}
return true
}
func (r *contractRunner) markMintedRevoked(ctx context.Context, ref commonv1.SecretRef, cleared []atircMinted) {
if len(cleared) == 0 {
return
}
r.bundleMu.Lock()
defer r.bundleMu.Unlock()
kv, data, _, err := r.readAtircBundle(ctx, ref)
if err != nil {
r.log().Warn("requester_atirc_revoked_unmarked", "step", "read the bundle", "err", err.Error())
return
}
minted, err := mintedOf(data)
if err != nil {
r.log().Warn("requester_atirc_revoked_unmarked", "step", "read the minted tokens", "err", err.Error())
return
}
marked := 0
for i, m := range minted {
for _, c := range cleared {
if m.Revoked == 0 && m.Token == c.Token && m.Subhandle == c.Subhandle {
minted[i].Revoked = r.q.now().Unix()
marked++
}
}
}
if marked == 0 {
return
}
raw, err := json.Marshal(minted)
if err != nil {
return
}
next := make(map[string]any, len(data))
for k, v := range data {
next[k] = v
}
next[atircMintedKey] = string(raw)
if err := kv.Write(ctx, ref.Mount, ref.Path, next); err != nil {
r.log().Warn("requester_atirc_revoked_unmarked", "step", "write the bundle", "err", err.Error())
}
}
func (r *contractRunner) legacySubhandle(contract v1alpha1.ComputeContract, path string) string {
handle := strings.ToLower(strings.TrimSpace(r.q.handle))
state := contract.Status.Contract
if handle == "" || state == nil {
return ""
}
if path == cloudinit.GuestRequesterAtircEnvFile {
if state.Accept == nil {
return ""
}
return atircChild("r2", uriRKey(state.Accept.URI), handle)
}
label, _, _ := strings.Cut(handle, ".")
if len(label) > maxAtircLabel {
label = label[:maxAtircLabel]
}
return atircChild(label, uriRKey(state.RFPURI), handle)
}
type atircRevokeParent func() (atircIssuer, bool)
type atircRevokeAccount func() (string, bool)
type atircRevokers struct {
account atircRevokeAccount
parent atircRevokeParent
}
func (r *contractRunner) revokers(ctx context.Context, contract v1alpha1.ComputeContract) atircRevokers {
return atircRevokers{account: r.revokeAccount(ctx), parent: r.revokeParent(ctx, contract)}
}
func (r *contractRunner) revokeAccount(ctx context.Context) atircRevokeAccount {
q := r.q
handle := strings.ToLower(strings.TrimSpace(q.handle))
if handle == "" || q.serviceAuth == nil || q.cfg.IdentityMode != IdentityModeOAuth {
return nil
}
var (
granted bool
read bool
)
return func() (string, bool) {
if read {
return handle, granted
}
read = true
ok, err := q.oauthGranted(ctx, atprotooauth.RPCAtircRevoke)
switch {
case err != nil:
r.log().Warn("requester_atirc_revoke_account_unavailable", "err", err.Error())
case !ok:
r.log().Info("requester_atirc_revoke_account_unavailable", "reason", "the oauth session was not granted "+AtircRevokeNSID)
}
granted = err == nil && ok
return handle, granted
}
}
func (r *contractRunner) accountRevokeBearer(ctx context.Context) (string, error) {
ctx, cancel := context.WithTimeout(ctx, atircCallTimeout)
defer cancel()
bearer, err := r.q.serviceAuth.ServiceAuth(ctx, AtircServiceAudience, AtircRevokeNSID)
if err != nil {
return "", fmt.Errorf("service auth for %s %s: %w", AtircServiceAudience, AtircRevokeNSID, err)
}
return bearer, nil
}
func (r *contractRunner) revokeParent(ctx context.Context, contract v1alpha1.ComputeContract) atircRevokeParent {
if r.q.cfg.Spec.AtircParentRef == nil {
return nil
}
var (
issuer atircIssuer
ok bool
read bool
)
return func() (atircIssuer, bool) {
if read {
return issuer, ok
}
read = true
found, err := r.atircIssuer(ctx, contract)
switch {
case err != nil:
r.log().Warn("requester_atirc_revoke_parent_unavailable", "err", err.Error())
case !r.q.now().Before(found.exp):
r.log().Warn("requester_atirc_revoke_parent_unavailable", "subhandle", found.base, "exp", found.exp.UTC().Format(time.RFC3339))
default:
issuer, ok = found, true
}
return issuer, ok
}
}
func atircBelow(subhandle, base string) bool {
return base != "" && strings.HasSuffix(subhandle, "."+base)
}
func atircTokenSubhandle(token string) string {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return ""
}
raw, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return ""
}
var claims struct {
Subhandle string `json:"subhandle"`
}
if json.Unmarshal(raw, &claims) != nil {
return ""
}
return claims.Subhandle
}
type atircRevokeResult int
const (
atircRevokeAlready atircRevokeResult = iota
atircRevokeByLease
atircRevokeServer
)
func atircRefused(err error) bool {
var xerr *atircError
return errors.As(err, &xerr) && (xerr.status == http.StatusUnauthorized || xerr.status == http.StatusForbidden)
}
func (r *contractRunner) revokeMinted(ctx context.Context, by atircRevokers, token, subhandle string) (atircRevokeResult, error) {
claimed := atircTokenSubhandle(token) == subhandle
if by.account != nil && claimed {
if base, ok := by.account(); ok && atircBelow(subhandle, base) {
bearer, err := r.accountRevokeBearer(ctx)
if err == nil {
var result atircRevokeResult
if result, err = r.revokeAtircSubhandle(ctx, bearer, subhandle, atircRevokeViaAccount); err == nil || !atircRefused(err) {
return result, err
}
}
r.log().Warn("requester_atirc_revoke_account_refused", "subhandle", subhandle, "account", base, "err", err.Error())
}
}
if by.parent != nil && claimed {
if issuer, ok := by.parent(); ok && atircBelow(subhandle, issuer.base) {
result, err := r.revokeAtircSubhandle(ctx, issuer.bearer, subhandle, atircRevokeViaParent)
if err == nil || !atircRefused(err) {
return result, err
}
r.log().Warn("requester_atirc_revoke_parent_refused", "subhandle", subhandle, "parent", issuer.base, "err", err.Error())
}
}
return r.revokeAtircSubhandle(ctx, token, subhandle, atircRevokeViaToken)
}
func atircLeaseDead(e *atircError) bool {
return strings.Contains(strings.ToLower(e.name+" "+e.message), "lease is not alive")
}
const (
atircRevokeViaToken = "token"
atircRevokeViaParent = "parent"
atircRevokeViaAccount = "account"
)
func (r *contractRunner) revokeAtircSubhandle(ctx context.Context, bearer, subhandle, via string) (atircRevokeResult, error) {
ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), atircCallTimeout)
defer cancel()
var out struct {
RevokedAt int64 `json:"revokedAt"`
}
err := r.postAtirc(ctx, AtircRevokeNSID, bearer, atircRevokeInput{Subhandle: subhandle}, &out)
var xerr *atircError
switch {
case err != nil && via != atircRevokeViaToken:
return atircRevokeAlready, err
case errors.As(err, &xerr) && xerr.status == http.StatusUnauthorized && atircLeaseDead(xerr):
r.log().Info("requester_atirc_revoked", "subhandle", subhandle, "byLease", true)
return atircRevokeByLease, nil
case errors.As(err, &xerr) && xerr.status == http.StatusUnauthorized:
r.log().Info("requester_atirc_revoked", "subhandle", subhandle, "already", true)
return atircRevokeAlready, nil
case err != nil:
r.log().Warn("requester_atirc_revoke_failed", "subhandle", subhandle, "step", "revoke", "err", err.Error())
return atircRevokeAlready, err
}
r.log().Info("requester_atirc_revoked", "subhandle", subhandle, "via", via, "revokedAt", time.Unix(out.RevokedAt, 0).UTC().Format(time.RFC3339))
return atircRevokeServer, nil
}
func (r *contractRunner) readAtircBundle(ctx context.Context, ref commonv1.SecretRef) (vault.KV, map[string]any, []SecretEntry, error) {
kv, err := r.q.deps.VaultWrite(ctx, ref)
if err != nil {
return nil, nil, nil, err
}
data, err := kv.Read(ctx, ref.Mount, ref.Path, 0)
if err != nil {
return nil, nil, nil, err
}
text, _ := data["bundle"].(string)
entries := []SecretEntry{}
if text != "" {
if err := json.Unmarshal([]byte(text), &entries); err != nil {
return nil, nil, nil, fmt.Errorf("parse the bundle: %w", err)
}
}
return kv, data, entries, nil
}
func (r *contractRunner) atircBase() string {
return r.q.atircBase()
}
func (r *contractRunner) postAtirc(ctx context.Context, nsid, bearer string, in, out any) error {
return r.q.postAtirc(ctx, nsid, bearer, in, out)
}
func (q *Requester) atircBase() string {
return strings.TrimRight(cmp.Or(strings.TrimSpace(q.cfg.AtircURL), DefaultAtircURL), "/")
}
func (q *Requester) postAtirc(ctx context.Context, nsid, bearer string, in, out any) error {
body, err := json.Marshal(in)
if err != nil {
return err
}
base := q.atircBase()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, base+"/xrpc/"+nsid, bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+bearer)
req.Header.Set("Content-Type", "application/json")
client := q.deps.HTTP
if client == nil {
client = http.DefaultClient
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
raw, err := io.ReadAll(io.LimitReader(resp.Body, maxAtircResponse))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
var xerr struct {
Error string `json:"error"`
Message string `json:"message"`
}
_ = json.Unmarshal(raw, &xerr)
return &atircError{base: base, status: resp.StatusCode, name: xerr.Error, message: xerr.Message}
}
if err := json.Unmarshal(raw, out); err != nil {
return fmt.Errorf("%s answered no JSON: %w", base, err)
}
return nil
}
func atircEnvValue(env, name string) string {
for line := range strings.Lines(env) {
value, ok := strings.CutPrefix(strings.TrimSpace(line), name+"=")
if value = strings.TrimSpace(value); ok && value != "" {
return value
}
}
return ""
}
func atircToken(env string) string {
return atircEnvValue(env, AtircTokenEnv)
}
func atircEnvSubhandle(env string) string {
return atircEnvValue(env, AtircSubhandleEnv)
}
func withoutAtircToken(env string) string {
var b strings.Builder
for line := range strings.Lines(env) {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, AtircTokenEnv+"=") || strings.HasPrefix(trimmed, AtircSubhandleEnv+"=") || strings.HasPrefix(trimmed, AtircExpEnv+"=") {
continue
}
b.WriteString(line)
if !strings.HasSuffix(line, "\n") {
b.WriteString("\n")
}
}
return b.String()
}
func withAtircToken(env, token, subhandle string, exp time.Time) string {
var b strings.Builder
b.WriteString(withoutAtircToken(env))
b.WriteString(AtircExpEnv + "=" + strconv.FormatInt(exp.Unix(), 10) + "\n")
b.WriteString(AtircTokenEnv + "=" + token + "\n")
b.WriteString(AtircSubhandleEnv + "=" + subhandle + "\n")
return b.String()
}