| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/dnsrec" |
| 8 | "github.com/carlosframework/platform/internal/hostops" |
| 9 | "github.com/carlosframework/platform/internal/mail" |
| 10 | "github.com/carlosframework/platform/internal/objstore" |
| 11 | ) |
| 12 | |
| 13 | type StoreProvisionerBoundary struct { |
| 14 | raw objstore.Provisioner |
| 15 | check hostops.MutationCheck |
| 16 | } |
| 17 | |
| 18 | func NewStoreProvisionerBoundary(raw objstore.Provisioner, check hostops.MutationCheck) (objstore.Provisioner, error) { |
| 19 | if raw == nil || check == nil { |
| 20 | return nil, hostops.ErrContract |
| 21 | } |
| 22 | return &StoreProvisionerBoundary{raw: raw, check: check}, nil |
| 23 | } |
| 24 | |
| 25 | var _ objstore.Provisioner = (*StoreProvisionerBoundary)(nil) |
| 26 | |
| 27 | func (w *StoreProvisionerBoundary) EnsureBucket(ctx context.Context, name string) (string, error) { |
| 28 | body, err := json.Marshal(map[string]any{"name": name}) |
| 29 | if err != nil { |
| 30 | return "", err |
| 31 | } |
| 32 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Provisioner", Method: "EnsureBucket", Key: name, After: body}) |
| 33 | if err != nil { |
| 34 | return "", err |
| 35 | } |
| 36 | return w.raw.EnsureBucket(ctx, name) |
| 37 | } |
| 38 | func (w *StoreProvisionerBoundary) EnsureUser(ctx context.Context, name string, path string, bucketARN string, deny *objstore.DenyTag) (string, error) { |
| 39 | body, err := json.Marshal(map[string]any{"name": name, "path": path, "bucketARN": bucketARN, "deny": deny}) |
| 40 | if err != nil { |
| 41 | return "", err |
| 42 | } |
| 43 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Provisioner", Method: "EnsureUser", Key: name, After: body}) |
| 44 | if err != nil { |
| 45 | return "", err |
| 46 | } |
| 47 | return w.raw.EnsureUser(ctx, name, path, bucketARN, deny) |
| 48 | } |
| 49 | func (w *StoreProvisionerBoundary) MintKey(ctx context.Context, user string) (string, string, error) { |
| 50 | body, err := json.Marshal(map[string]any{"user": user}) |
| 51 | if err != nil { |
| 52 | return "", "", err |
| 53 | } |
| 54 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Provisioner", Method: "MintKey", Key: user, After: body}) |
| 55 | if err != nil { |
| 56 | return "", "", err |
| 57 | } |
| 58 | return w.raw.MintKey(ctx, user) |
| 59 | } |
| 60 | func (w *StoreProvisionerBoundary) DeleteKey(ctx context.Context, user string, id string) error { |
| 61 | body, err := json.Marshal(map[string]any{"user": user, "id": id}) |
| 62 | if err != nil { |
| 63 | return err |
| 64 | } |
| 65 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Provisioner", Method: "DeleteKey", Key: user, After: body}) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | return w.raw.DeleteKey(ctx, user, id) |
| 70 | } |
| 71 | func (w *StoreProvisionerBoundary) ReapKeys(ctx context.Context, user string, keep []string) error { |
| 72 | body, err := json.Marshal(map[string]any{"user": user, "keep": keep}) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Provisioner", Method: "ReapKeys", Key: user, After: body}) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | return w.raw.ReapKeys(ctx, user, keep) |
| 81 | } |
| 82 | func (w *StoreProvisionerBoundary) EmptyBucket(ctx context.Context, name string) error { |
| 83 | body, err := json.Marshal(map[string]any{"name": name}) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Provisioner", Method: "EmptyBucket", Key: name, After: body}) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | return w.raw.EmptyBucket(ctx, name) |
| 92 | } |
| 93 | func (w *StoreProvisionerBoundary) DeleteBucket(ctx context.Context, name string) error { |
| 94 | body, err := json.Marshal(map[string]any{"name": name}) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Provisioner", Method: "DeleteBucket", Key: name, After: body}) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | return w.raw.DeleteBucket(ctx, name) |
| 103 | } |
| 104 | func (w *StoreProvisionerBoundary) DeleteUser(ctx context.Context, user string) error { |
| 105 | body, err := json.Marshal(map[string]any{"user": user}) |
| 106 | if err != nil { |
| 107 | return err |
| 108 | } |
| 109 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Provisioner", Method: "DeleteUser", Key: user, After: body}) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | return w.raw.DeleteUser(ctx, user) |
| 114 | } |
| 115 | |
| 116 | type ScannerBoundary struct { |
| 117 | raw objstore.Scanner |
| 118 | check hostops.MutationCheck |
| 119 | } |
| 120 | |
| 121 | func NewScannerBoundary(raw objstore.Scanner, check hostops.MutationCheck) (objstore.Scanner, error) { |
| 122 | if raw == nil || check == nil { |
| 123 | return nil, hostops.ErrContract |
| 124 | } |
| 125 | return &ScannerBoundary{raw: raw, check: check}, nil |
| 126 | } |
| 127 | |
| 128 | var _ objstore.Scanner = (*ScannerBoundary)(nil) |
| 129 | |
| 130 | func (w *ScannerBoundary) EnsureEnabled(ctx context.Context, bucket string) error { |
| 131 | body, err := json.Marshal(map[string]any{"bucket": bucket}) |
| 132 | if err != nil { |
| 133 | return err |
| 134 | } |
| 135 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Scanner", Method: "EnsureEnabled", Key: bucket, After: body}) |
| 136 | if err != nil { |
| 137 | return err |
| 138 | } |
| 139 | return w.raw.EnsureEnabled(ctx, bucket) |
| 140 | } |
| 141 | func (w *ScannerBoundary) EnsureDisabled(ctx context.Context, bucket string) error { |
| 142 | body, err := json.Marshal(map[string]any{"bucket": bucket}) |
| 143 | if err != nil { |
| 144 | return err |
| 145 | } |
| 146 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Scanner", Method: "EnsureDisabled", Key: bucket, After: body}) |
| 147 | if err != nil { |
| 148 | return err |
| 149 | } |
| 150 | return w.raw.EnsureDisabled(ctx, bucket) |
| 151 | } |
| 152 | func (w *ScannerBoundary) Verdict(ctx context.Context, bucket string, key string, version string) (objstore.Verdict, error) { |
| 153 | return w.raw.Verdict(ctx, bucket, key, version) |
| 154 | } |
| 155 | func (w *ScannerBoundary) Status(ctx context.Context, bucket string, key string) (objstore.Verdict, objstore.ScanObject, error) { |
| 156 | return w.raw.Status(ctx, bucket, key) |
| 157 | } |
| 158 | func (w *ScannerBoundary) Scan(ctx context.Context, bucket string, key string, version string, size int64) (objstore.Verdict, bool, error) { |
| 159 | body, err := json.Marshal(map[string]any{"bucket": bucket, "key": key, "version": version, "size": size}) |
| 160 | if err != nil { |
| 161 | return objstore.VerdictUnknown, false, err |
| 162 | } |
| 163 | err = w.check(ctx, hostops.MutationTarget{Surface: "objstore.Scanner", Method: "Scan", Key: bucket, After: body}) |
| 164 | if err != nil { |
| 165 | return objstore.VerdictUnknown, false, err |
| 166 | } |
| 167 | return w.raw.Scan(ctx, bucket, key, version, size) |
| 168 | } |
| 169 | func (w *ScannerBoundary) Enforcement() objstore.DenyTag { |
| 170 | return w.raw.Enforcement() |
| 171 | } |
| 172 | func (w *ScannerBoundary) Capacity(ctx context.Context) (int, int, error) { |
| 173 | return w.raw.Capacity(ctx) |
| 174 | } |
| 175 | func (w *ScannerBoundary) Provenance(ctx context.Context) (string, string, error) { |
| 176 | return w.raw.Provenance(ctx) |
| 177 | } |
| 178 | |
| 179 | type MailProvisionerBoundary struct { |
| 180 | raw mail.Provisioner |
| 181 | check hostops.MutationCheck |
| 182 | } |
| 183 | |
| 184 | func NewMailProvisionerBoundary(raw mail.Provisioner, check hostops.MutationCheck) (mail.Provisioner, error) { |
| 185 | if raw == nil || check == nil { |
| 186 | return nil, hostops.ErrContract |
| 187 | } |
| 188 | return &MailProvisionerBoundary{raw: raw, check: check}, nil |
| 189 | } |
| 190 | |
| 191 | var _ mail.Provisioner = (*MailProvisionerBoundary)(nil) |
| 192 | |
| 193 | func (w *MailProvisionerBoundary) EnsureTenant(ctx context.Context, name string) error { |
| 194 | body, err := json.Marshal(map[string]any{"name": name}) |
| 195 | if err != nil { |
| 196 | return err |
| 197 | } |
| 198 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "EnsureTenant", Key: name, After: body}) |
| 199 | if err != nil { |
| 200 | return err |
| 201 | } |
| 202 | return w.raw.EnsureTenant(ctx, name) |
| 203 | } |
| 204 | func (w *MailProvisionerBoundary) EnsureConfigSet(ctx context.Context, region string, name string, snsTopicARN string) error { |
| 205 | body, err := json.Marshal(map[string]any{"region": region, "name": name, "snsTopicARN": snsTopicARN}) |
| 206 | if err != nil { |
| 207 | return err |
| 208 | } |
| 209 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "EnsureConfigSet", Key: region, After: body}) |
| 210 | if err != nil { |
| 211 | return err |
| 212 | } |
| 213 | return w.raw.EnsureConfigSet(ctx, region, name, snsTopicARN) |
| 214 | } |
| 215 | func (w *MailProvisionerBoundary) SetIdentityConfigSet(ctx context.Context, region string, domain string, configSet string) error { |
| 216 | body, err := json.Marshal(map[string]any{"region": region, "domain": domain, "configSet": configSet}) |
| 217 | if err != nil { |
| 218 | return err |
| 219 | } |
| 220 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "SetIdentityConfigSet", Key: region, After: body}) |
| 221 | if err != nil { |
| 222 | return err |
| 223 | } |
| 224 | return w.raw.SetIdentityConfigSet(ctx, region, domain, configSet) |
| 225 | } |
| 226 | func (w *MailProvisionerBoundary) AssociateTenantResource(ctx context.Context, tenant string, resourceARN string) error { |
| 227 | body, err := json.Marshal(map[string]any{"tenant": tenant, "resourceARN": resourceARN}) |
| 228 | if err != nil { |
| 229 | return err |
| 230 | } |
| 231 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "AssociateTenantResource", Key: tenant, After: body}) |
| 232 | if err != nil { |
| 233 | return err |
| 234 | } |
| 235 | return w.raw.AssociateTenantResource(ctx, tenant, resourceARN) |
| 236 | } |
| 237 | func (w *MailProvisionerBoundary) DisassociateTenantResource(ctx context.Context, tenant string, resourceARN string) error { |
| 238 | body, err := json.Marshal(map[string]any{"tenant": tenant, "resourceARN": resourceARN}) |
| 239 | if err != nil { |
| 240 | return err |
| 241 | } |
| 242 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "DisassociateTenantResource", Key: tenant, After: body}) |
| 243 | if err != nil { |
| 244 | return err |
| 245 | } |
| 246 | return w.raw.DisassociateTenantResource(ctx, tenant, resourceARN) |
| 247 | } |
| 248 | func (w *MailProvisionerBoundary) EnsureIdentity(ctx context.Context, region string, domain string, mailFrom string, adopt bool) (mail.Identity, error) { |
| 249 | body, err := json.Marshal(map[string]any{"region": region, "domain": domain, "mailFrom": mailFrom, "adopt": adopt}) |
| 250 | if err != nil { |
| 251 | return mail.Identity{}, err |
| 252 | } |
| 253 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "EnsureIdentity", Key: region, After: body}) |
| 254 | if err != nil { |
| 255 | return mail.Identity{}, err |
| 256 | } |
| 257 | return w.raw.EnsureIdentity(ctx, region, domain, mailFrom, adopt) |
| 258 | } |
| 259 | func (w *MailProvisionerBoundary) IdentityStatus(ctx context.Context, region string, domain string) (mail.Status, error) { |
| 260 | return w.raw.IdentityStatus(ctx, region, domain) |
| 261 | } |
| 262 | func (w *MailProvisionerBoundary) DeleteIdentity(ctx context.Context, region string, domain string) error { |
| 263 | body, err := json.Marshal(map[string]any{"region": region, "domain": domain}) |
| 264 | if err != nil { |
| 265 | return err |
| 266 | } |
| 267 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "DeleteIdentity", Key: region, After: body}) |
| 268 | if err != nil { |
| 269 | return err |
| 270 | } |
| 271 | return w.raw.DeleteIdentity(ctx, region, domain) |
| 272 | } |
| 273 | func (w *MailProvisionerBoundary) EnsureUser(ctx context.Context, name string, path string, identityARNs []string, configSetARNs []string, domains []string) (string, error) { |
| 274 | body, err := json.Marshal(map[string]any{"name": name, "path": path, "identityARNs": identityARNs, "configSetARNs": configSetARNs, "domains": domains}) |
| 275 | if err != nil { |
| 276 | return "", err |
| 277 | } |
| 278 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "EnsureUser", Key: name, After: body}) |
| 279 | if err != nil { |
| 280 | return "", err |
| 281 | } |
| 282 | return w.raw.EnsureUser(ctx, name, path, identityARNs, configSetARNs, domains) |
| 283 | } |
| 284 | func (w *MailProvisionerBoundary) MintKey(ctx context.Context, user string) (string, string, error) { |
| 285 | body, err := json.Marshal(map[string]any{"user": user}) |
| 286 | if err != nil { |
| 287 | return "", "", err |
| 288 | } |
| 289 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "MintKey", Key: user, After: body}) |
| 290 | if err != nil { |
| 291 | return "", "", err |
| 292 | } |
| 293 | return w.raw.MintKey(ctx, user) |
| 294 | } |
| 295 | func (w *MailProvisionerBoundary) SetKeyActive(ctx context.Context, user string, keyID string, active bool) error { |
| 296 | body, err := json.Marshal(map[string]any{"user": user, "keyID": keyID, "active": active}) |
| 297 | if err != nil { |
| 298 | return err |
| 299 | } |
| 300 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "SetKeyActive", Key: user, After: body}) |
| 301 | if err != nil { |
| 302 | return err |
| 303 | } |
| 304 | return w.raw.SetKeyActive(ctx, user, keyID, active) |
| 305 | } |
| 306 | func (w *MailProvisionerBoundary) DeleteKey(ctx context.Context, user string, keyID string) error { |
| 307 | body, err := json.Marshal(map[string]any{"user": user, "keyID": keyID}) |
| 308 | if err != nil { |
| 309 | return err |
| 310 | } |
| 311 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "DeleteKey", Key: user, After: body}) |
| 312 | if err != nil { |
| 313 | return err |
| 314 | } |
| 315 | return w.raw.DeleteKey(ctx, user, keyID) |
| 316 | } |
| 317 | func (w *MailProvisionerBoundary) ReapKeys(ctx context.Context, user string, keep []string) error { |
| 318 | body, err := json.Marshal(map[string]any{"user": user, "keep": keep}) |
| 319 | if err != nil { |
| 320 | return err |
| 321 | } |
| 322 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "ReapKeys", Key: user, After: body}) |
| 323 | if err != nil { |
| 324 | return err |
| 325 | } |
| 326 | return w.raw.ReapKeys(ctx, user, keep) |
| 327 | } |
| 328 | func (w *MailProvisionerBoundary) DeleteUser(ctx context.Context, user string) error { |
| 329 | body, err := json.Marshal(map[string]any{"user": user}) |
| 330 | if err != nil { |
| 331 | return err |
| 332 | } |
| 333 | err = w.check(ctx, hostops.MutationTarget{Surface: "mail.Provisioner", Method: "DeleteUser", Key: user, After: body}) |
| 334 | if err != nil { |
| 335 | return err |
| 336 | } |
| 337 | return w.raw.DeleteUser(ctx, user) |
| 338 | } |
| 339 | |
| 340 | type DNSWriterBoundary struct { |
| 341 | raw dnsrec.Writer |
| 342 | check hostops.MutationCheck |
| 343 | } |
| 344 | |
| 345 | func NewDNSWriterBoundary(raw dnsrec.Writer, check hostops.MutationCheck) (dnsrec.Writer, error) { |
| 346 | if raw == nil || check == nil { |
| 347 | return nil, hostops.ErrContract |
| 348 | } |
| 349 | return &DNSWriterBoundary{raw: raw, check: check}, nil |
| 350 | } |
| 351 | |
| 352 | var _ dnsrec.Writer = (*DNSWriterBoundary)(nil) |
| 353 | |
| 354 | func (w *DNSWriterBoundary) EnsureRecord(ctx context.Context, r dnsrec.Record) error { |
| 355 | body, err := json.Marshal(map[string]any{"r": r}) |
| 356 | if err != nil { |
| 357 | return err |
| 358 | } |
| 359 | err = w.check(ctx, hostops.MutationTarget{Surface: "dnsrec.Writer", Method: "EnsureRecord", Key: "", After: body}) |
| 360 | if err != nil { |
| 361 | return err |
| 362 | } |
| 363 | return w.raw.EnsureRecord(ctx, r) |
| 364 | } |
| 365 | func (w *DNSWriterBoundary) DeleteRecord(ctx context.Context, r dnsrec.Record) error { |
| 366 | body, err := json.Marshal(map[string]any{"r": r}) |
| 367 | if err != nil { |
| 368 | return err |
| 369 | } |
| 370 | err = w.check(ctx, hostops.MutationTarget{Surface: "dnsrec.Writer", Method: "DeleteRecord", Key: "", After: body}) |
| 371 | if err != nil { |
| 372 | return err |
| 373 | } |
| 374 | return w.raw.DeleteRecord(ctx, r) |
| 375 | } |
| 376 | |