| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/dnsrec" |
| 9 | "github.com/carlosframework/platform/internal/hostops" |
| 10 | "github.com/carlosframework/platform/internal/mail" |
| 11 | "github.com/carlosframework/platform/internal/objstore" |
| 12 | ) |
| 13 | |
| 14 | // Purge teardown backends. Every call is a durable effect under the purge |
| 15 | // operation (fresh ID per attempt); the effect's permit is what lets the |
| 16 | // call through the guarded backend beneath. Methods a teardown never needs |
| 17 | // refuse. Named private fields only; every interface method written out. |
| 18 | |
| 19 | type claimPurgeEffects struct{ h *hostops.OperationHandle } |
| 20 | |
| 21 | func (e claimPurgeEffects) run(ctx context.Context, resource, action string, inputs map[string]string, call func(context.Context) error) error { |
| 22 | id, err := claimEffectID() |
| 23 | if err != nil { |
| 24 | return err |
| 25 | } |
| 26 | _, err = e.h.Runner(time.Now).Run(ctx, hostops.Effect{ID: id, Resource: resource, Action: action, Inputs: inputs}, func(ctx context.Context) (string, error) { |
| 27 | if err := call(ctx); err != nil { |
| 28 | return "", err |
| 29 | } |
| 30 | return "success", nil |
| 31 | }) |
| 32 | return err |
| 33 | } |
| 34 | |
| 35 | type claimPurgeStore struct { |
| 36 | raw objstore.Provisioner |
| 37 | e claimPurgeEffects |
| 38 | } |
| 39 | |
| 40 | var _ objstore.Provisioner = claimPurgeStore{} |
| 41 | |
| 42 | func (p claimPurgeStore) EnsureBucket(context.Context, string) (string, error) { |
| 43 | return "", hostops.ErrContract |
| 44 | } |
| 45 | func (p claimPurgeStore) EnsureUser(context.Context, string, string, string, *objstore.DenyTag) (string, error) { |
| 46 | return "", hostops.ErrContract |
| 47 | } |
| 48 | func (p claimPurgeStore) MintKey(context.Context, string) (string, string, error) { |
| 49 | return "", "", hostops.ErrContract |
| 50 | } |
| 51 | func (p claimPurgeStore) DeleteKey(ctx context.Context, user, id string) error { |
| 52 | return p.e.run(ctx, "iam-user:"+user, "delete-key", map[string]string{"user": user, "key": id}, func(ctx context.Context) error { return p.raw.DeleteKey(ctx, user, id) }) |
| 53 | } |
| 54 | func (p claimPurgeStore) ReapKeys(ctx context.Context, user string, keep []string) error { |
| 55 | raw, err := json.Marshal(keep) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | return p.e.run(ctx, "iam-user:"+user, "reap-keys", map[string]string{"user": user, "keep": string(raw)}, func(ctx context.Context) error { return p.raw.ReapKeys(ctx, user, keep) }) |
| 60 | } |
| 61 | func (p claimPurgeStore) EmptyBucket(ctx context.Context, bucket string) error { |
| 62 | return p.e.run(ctx, "bucket:"+bucket, "empty", map[string]string{"bucket": bucket}, func(ctx context.Context) error { return p.raw.EmptyBucket(ctx, bucket) }) |
| 63 | } |
| 64 | func (p claimPurgeStore) DeleteBucket(ctx context.Context, bucket string) error { |
| 65 | return p.e.run(ctx, "bucket:"+bucket, "delete", map[string]string{"bucket": bucket}, func(ctx context.Context) error { return p.raw.DeleteBucket(ctx, bucket) }) |
| 66 | } |
| 67 | func (p claimPurgeStore) DeleteUser(ctx context.Context, user string) error { |
| 68 | return p.e.run(ctx, "iam-user:"+user, "delete-user", map[string]string{"user": user}, func(ctx context.Context) error { return p.raw.DeleteUser(ctx, user) }) |
| 69 | } |
| 70 | |
| 71 | type claimPurgeScanner struct { |
| 72 | raw objstore.Scanner |
| 73 | e claimPurgeEffects |
| 74 | } |
| 75 | |
| 76 | var _ objstore.Scanner = claimPurgeScanner{} |
| 77 | |
| 78 | func (p claimPurgeScanner) EnsureEnabled(context.Context, string) error { return hostops.ErrContract } |
| 79 | func (p claimPurgeScanner) EnsureDisabled(ctx context.Context, bucket string) error { |
| 80 | return p.e.run(ctx, "scanner:"+bucket, "disable", map[string]string{"bucket": bucket}, func(ctx context.Context) error { return p.raw.EnsureDisabled(ctx, bucket) }) |
| 81 | } |
| 82 | func (p claimPurgeScanner) Verdict(ctx context.Context, bucket, key, version string) (objstore.Verdict, error) { |
| 83 | return p.raw.Verdict(ctx, bucket, key, version) |
| 84 | } |
| 85 | func (p claimPurgeScanner) Status(ctx context.Context, bucket, key string) (objstore.Verdict, objstore.ScanObject, error) { |
| 86 | return p.raw.Status(ctx, bucket, key) |
| 87 | } |
| 88 | func (p claimPurgeScanner) Scan(context.Context, string, string, string, int64) (objstore.Verdict, bool, error) { |
| 89 | return objstore.VerdictUnknown, false, hostops.ErrContract |
| 90 | } |
| 91 | func (p claimPurgeScanner) Enforcement() objstore.DenyTag { return p.raw.Enforcement() } |
| 92 | func (p claimPurgeScanner) Capacity(ctx context.Context) (int, int, error) { |
| 93 | return p.raw.Capacity(ctx) |
| 94 | } |
| 95 | func (p claimPurgeScanner) Provenance(ctx context.Context) (string, string, error) { |
| 96 | return p.raw.Provenance(ctx) |
| 97 | } |
| 98 | |
| 99 | type claimPurgeMail struct { |
| 100 | raw mail.Provisioner |
| 101 | e claimPurgeEffects |
| 102 | } |
| 103 | |
| 104 | var _ mail.Provisioner = claimPurgeMail{} |
| 105 | |
| 106 | func (p claimPurgeMail) EnsureTenant(context.Context, string) error { return hostops.ErrContract } |
| 107 | func (p claimPurgeMail) EnsureConfigSet(context.Context, string, string, string) error { |
| 108 | return hostops.ErrContract |
| 109 | } |
| 110 | func (p claimPurgeMail) SetIdentityConfigSet(context.Context, string, string, string) error { |
| 111 | return hostops.ErrContract |
| 112 | } |
| 113 | func (p claimPurgeMail) AssociateTenantResource(context.Context, string, string) error { |
| 114 | return hostops.ErrContract |
| 115 | } |
| 116 | func (p claimPurgeMail) DisassociateTenantResource(ctx context.Context, tenant, resource string) error { |
| 117 | return p.e.run(ctx, "ses-resource:"+resource, "disassociate", map[string]string{"tenant": tenant, "resource": resource}, func(ctx context.Context) error { |
| 118 | return p.raw.DisassociateTenantResource(ctx, tenant, resource) |
| 119 | }) |
| 120 | } |
| 121 | func (p claimPurgeMail) EnsureIdentity(context.Context, string, string, string, bool) (mail.Identity, error) { |
| 122 | return mail.Identity{}, hostops.ErrContract |
| 123 | } |
| 124 | func (p claimPurgeMail) IdentityStatus(ctx context.Context, region, domain string) (mail.Status, error) { |
| 125 | return p.raw.IdentityStatus(ctx, region, domain) |
| 126 | } |
| 127 | func (p claimPurgeMail) DeleteIdentity(ctx context.Context, region, domain string) error { |
| 128 | return p.e.run(ctx, "email-domain:"+region+":"+domain, "delete-identity", map[string]string{"region": region, "domain": domain}, func(ctx context.Context) error { |
| 129 | return p.raw.DeleteIdentity(ctx, region, domain) |
| 130 | }) |
| 131 | } |
| 132 | func (p claimPurgeMail) EnsureUser(context.Context, string, string, []string, []string, []string) (string, error) { |
| 133 | return "", hostops.ErrContract |
| 134 | } |
| 135 | func (p claimPurgeMail) MintKey(context.Context, string) (string, string, error) { |
| 136 | return "", "", hostops.ErrContract |
| 137 | } |
| 138 | func (p claimPurgeMail) SetKeyActive(ctx context.Context, user, key string, active bool) error { |
| 139 | if active { |
| 140 | return hostops.ErrContract |
| 141 | } |
| 142 | return p.e.run(ctx, "iam-user:"+user, "deactivate-key", map[string]string{"user": user, "key": key}, func(ctx context.Context) error { |
| 143 | return p.raw.SetKeyActive(ctx, user, key, false) |
| 144 | }) |
| 145 | } |
| 146 | func (p claimPurgeMail) DeleteKey(ctx context.Context, user, id string) error { |
| 147 | return p.e.run(ctx, "iam-user:"+user, "delete-key", map[string]string{"user": user, "key": id}, func(ctx context.Context) error { return p.raw.DeleteKey(ctx, user, id) }) |
| 148 | } |
| 149 | func (p claimPurgeMail) ReapKeys(ctx context.Context, user string, keep []string) error { |
| 150 | raw, err := json.Marshal(keep) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | return p.e.run(ctx, "iam-user:"+user, "reap-keys", map[string]string{"user": user, "keep": string(raw)}, func(ctx context.Context) error { return p.raw.ReapKeys(ctx, user, keep) }) |
| 155 | } |
| 156 | func (p claimPurgeMail) DeleteUser(ctx context.Context, user string) error { |
| 157 | return p.e.run(ctx, "iam-user:"+user, "delete-user", map[string]string{"user": user}, func(ctx context.Context) error { return p.raw.DeleteUser(ctx, user) }) |
| 158 | } |
| 159 | |
| 160 | type claimPurgeDNS struct { |
| 161 | raw dnsrec.Writer |
| 162 | e claimPurgeEffects |
| 163 | } |
| 164 | |
| 165 | var _ dnsrec.Writer = claimPurgeDNS{} |
| 166 | |
| 167 | func (p claimPurgeDNS) DeleteRecord(ctx context.Context, record dnsrec.Record) error { |
| 168 | raw, err := json.Marshal(record) |
| 169 | if err != nil { |
| 170 | return err |
| 171 | } |
| 172 | return p.e.run(ctx, "dns:"+record.Key(), "delete", map[string]string{"record": string(raw)}, func(ctx context.Context) error { return p.raw.DeleteRecord(ctx, record) }) |
| 173 | } |
| 174 | |
| 175 | // EnsureRecord: teardown of one member rewrites a shared domain's records |
| 176 | // for the remaining members. |
| 177 | func (p claimPurgeDNS) EnsureRecord(ctx context.Context, record dnsrec.Record) error { |
| 178 | raw, err := json.Marshal(record) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | return p.e.run(ctx, "dns:"+record.Key(), "ensure", map[string]string{"record": string(raw)}, func(ctx context.Context) error { return p.raw.EnsureRecord(ctx, record) }) |
| 183 | } |
| 184 | |