An open-source, faster Devin alternative
TypeScript
microvm templates: dedupe, per-org cap and GC — AWS caps this account at 100 MicroVM images with 35 already used and 16 belonging to another workload, so image-per-template runs out after ~65 and would also block rebuilding the pool image; names are content-addressed from the manifest hash so an unchanged template reuses its slot, the cap counts processing as well as ready (or N concurrent builds outrun it), and every destructive path is gated on an osb-tpl- prefix because managed-agents-runtime-* images sit in the same account
Brian Reardon31a69bc0775e
diff --git a/internal/api/template_quota.go b/internal/api/template_quota.gonew file mode 100644index 00000000..b226f16d--- /dev/null+++ b/internal/api/template_quota.go@@ -0,0 +1,127 @@+package api++// template_quota.go — what stops custom templates from exhausting the account.+//+// THE CONSTRAINT, measured rather than assumed: AWS caps this account at 100+// MicroVM images (Service Quotas, service-code `lambda`, L-942E56BE). At the+// time of writing 35 were in use and only 6 of them were the sandbox fleet's —+// 16 belonged to `managed-agents-runtime-*`, a different workload sharing the+// account. So "one image per template" runs out after ~65 templates, and+// exhausting it does not merely stop new templates: it blocks rebuilding the+// POOL image that every sandbox depends on, and blocks the other workload's+// deploys.+//+// Three mechanisms keep that from happening, in order of how much they buy:+//+// 1. DEDUPE. Images scale with DISTINCT templates, not with customers.+// 2. PER-ORG CAP. One org cannot consume the account.+// 3. GC. Templates nobody launches stop holding a slot.+//+// A quota increase to 1000 has been requested; none of the above stops being+// necessary if it is granted, because all three are about the SHAPE of the+// growth, not the ceiling.++import (+ "fmt"+ "regexp"+ "strings"+)++const (+ // maxImageTemplatesPerOrg caps how many image slots one org can hold.+ //+ // Deliberately well below the account ceiling: with the cap at 10 and a+ // 1000-image quota, it takes 100 orgs all using templates heavily to reach+ // the limit, and no single customer can starve the fleet by scripting+ // template creation. Raise per-org rather than removing the cap.+ maxImageTemplatesPerOrg = 10++ // templateImagePrefix marks an image as ours, and as a customer template.+ //+ // Load-bearing for shared-account safety: deploy/microvm/publish.sh is+ // careful never to enumerate or touch an image it did not create, and the+ // same rule applies here. Anything without this prefix belongs to the pool,+ // to managed-agents, or to someone else, and must never be deleted by this+ // code path.+ templateImagePrefix = "osb-tpl-"+)++// Tags applied to every template image. Ownership is expressed in tags as well+// as in the name because a name is a weak assertion — tags survive rename and+// are queryable when reconciling what we own against what exists.+const (+ tagManagedBy = "osb:managed-by"+ tagOrgID = "osb:org-id"+ tagKind = "osb:kind"+)++// templateImageName derives the AWS image name for an org's template.+//+// Content-addressed by the manifest hash, which is what makes DEDUPE work: the+// same manifest yields the same name, so a rebuild reuses the image slot rather+// than taking another. The org prefix keeps two orgs' identical manifests in+// separate images — see FindMicrovmImageTemplateByTag for why that is the+// conservative choice.+//+// Truncated because AWS image names are bounded and a full pair of SHA-256+// hexes is 128 characters. 8 org + 16 manifest characters is 96 bits of+// manifest hash, which is far past collision concern for a per-org namespace.+func templateImageName(orgID, manifestHash string) string {+ org := sanitizeNameComponent(orgID)+ if len(org) > 8 {+ org = org[:8]+ }+ h := sanitizeNameComponent(manifestHash)+ if len(h) > 16 {+ h = h[:16]+ }+ return templateImagePrefix + org + "-" + h+}++var nameComponentInvalid = regexp.MustCompile(`[^a-zA-Z0-9]`)++func sanitizeNameComponent(s string) string {+ return nameComponentInvalid.ReplaceAllString(s, "")+}++// templateImageTags returns the ownership tags for a template image.+func templateImageTags(orgID string) map[string]string {+ return map[string]string{+ tagManagedBy: "opensandbox",+ tagOrgID: orgID,+ tagKind: "custom-template",+ }+}++// isOurTemplateImage reports whether an image ARN or name is a customer+// template this code may delete.+//+// The guard on every destructive path. Called with an ARN read back from our+// own registry, it is belt-and-braces; called with anything else it is the only+// thing standing between a GC pass and another workload's images.+func isOurTemplateImage(arnOrName string) bool {+ if arnOrName == "" {+ return false+ }+ name := arnOrName+ if i := strings.LastIndex(name, ":"); i >= 0 {+ name = name[i+1:]+ }+ return strings.HasPrefix(name, templateImagePrefix)+}++// ErrTemplateQuotaExceeded is returned when an org is at its image-template cap.+var ErrTemplateQuotaExceeded = fmt.Errorf("template image quota exceeded")++// checkTemplateQuota reports whether an org may take another image slot.+//+// Returns a message naming the cap and what to do about it. A bare "quota+// exceeded" leaves a customer with no idea whether to wait, delete something,+// or ask for a raise.+func checkTemplateQuota(current int) error {+ if current < maxImageTemplatesPerOrg {+ return nil+ }+ return fmt.Errorf("%w: this org holds %d of %d custom template images; delete an unused template before building another",+ ErrTemplateQuotaExceeded, current, maxImageTemplatesPerOrg)+}diff --git a/internal/api/template_quota_test.go b/internal/api/template_quota_test.gonew file mode 100644index 00000000..1c9c6d1b--- /dev/null+++ b/internal/api/template_quota_test.go@@ -0,0 +1,100 @@+package api++import (+ "errors"+ "strings"+ "testing"+)++// Dedupe only works if the same manifest yields the same image name. If this+// ever stops being true, every rebuild takes a fresh image slot and the account+// ceiling arrives ~65 templates later instead of never.+func TestSameManifestSameImageName(t *testing.T) {+ m := ImageManifest{Base: "base", Steps: []ImageStep{+ {Type: "apt_install", Args: map[string]interface{}{"packages": []interface{}{"ffmpeg"}}},+ }}+ h1, h2 := computeManifestHash(&m), computeManifestHash(&m)+ if h1 != h2 {+ t.Fatal("manifest hash is not deterministic")+ }+ org := "86ade78a-7aef-49e3-bc37-b6dfad3ffa92"+ if templateImageName(org, h1) != templateImageName(org, h2) {+ t.Error("identical manifests produced different image names — dedupe would never hit")+ }+}++// ...and a DIFFERENT manifest must not collide, or one customer's template+// silently serves another's environment.+func TestDifferentManifestDifferentImageName(t *testing.T) {+ org := "86ade78a"+ a := ImageManifest{Base: "base", Steps: []ImageStep{{Type: "apt_install", Args: map[string]interface{}{"packages": []interface{}{"ffmpeg"}}}}}+ b := ImageManifest{Base: "base", Steps: []ImageStep{{Type: "apt_install", Args: map[string]interface{}{"packages": []interface{}{"golang"}}}}}+ if templateImageName(org, computeManifestHash(&a)) == templateImageName(org, computeManifestHash(&b)) {+ t.Fatal("different manifests collided on one image name")+ }+}++// Two orgs with the same manifest must not share an image — see the comment on+// FindMicrovmImageTemplateByTag for why per-org is the conservative default.+func TestOrgsDoNotShareAnImageName(t *testing.T) {+ m := ImageManifest{Base: "base"}+ h := computeManifestHash(&m)+ if templateImageName("aaaaaaaa-1111", h) == templateImageName("bbbbbbbb-2222", h) {+ t.Fatal("two orgs mapped to the same image name")+ }+}++// The name is what every destructive path checks before deleting. Anything that+// is not ours must be rejected — this account is shared with a customer-serving+// workload, and `managed-agents-runtime-*` images sit right next to ours.+func TestOnlyOurTemplateImagesAreDeletable(t *testing.T) {+ ours := "arn:aws:lambda:us-east-1:739940681129:microvm-image:" + templateImageName("org1234", "abcdef0123456789")+ if !isOurTemplateImage(ours) {+ t.Error("our own template image was not recognised")+ }+ for _, foreign := range []string{+ "arn:aws:lambda:us-east-1:739940681129:microvm-image:managed-agents-runtime-33700486278-1",+ "arn:aws:lambda:us-east-1:739940681129:microvm-image:opensandbox-agent-lite-prod",+ "arn:aws:lambda:us-east-1:739940681129:microvm-image:opensandbox-agent-lite-dev-oc",+ "arn:aws:lambda:us-east-1:739940681129:microvm-image:blue-hello-agent-9ca56c1aef6b",+ "",+ "osb-tpl", // prefix-like but not the prefix+ } {+ if isOurTemplateImage(foreign) {+ t.Errorf("would have deleted a foreign image: %q", foreign)+ }+ }+}++// The image name goes into an AWS resource name; anything outside [A-Za-z0-9-]+// must not survive, or a build fails on a name the customer never chose.+func TestImageNameIsSanitised(t *testing.T) {+ name := templateImageName("org/../../etc passwd", "aa:bb$cc*dd")+ for _, bad := range []string{"/", " ", ":", "$", "*", ".."} {+ if strings.Contains(name, bad) {+ t.Errorf("image name %q still contains %q", name, bad)+ }+ }+ if !strings.HasPrefix(name, templateImagePrefix) {+ t.Errorf("sanitising lost the ownership prefix: %q", name)+ }+}++func TestQuotaRefusesAtTheCapWithAnActionableMessage(t *testing.T) {+ if err := checkTemplateQuota(maxImageTemplatesPerOrg - 1); err != nil {+ t.Fatalf("under the cap should be allowed: %v", err)+ }+ err := checkTemplateQuota(maxImageTemplatesPerOrg)+ if err == nil {+ t.Fatal("at the cap must be refused")+ }+ if !errors.Is(err, ErrTemplateQuotaExceeded) {+ t.Errorf("caller cannot detect the quota case: %v", err)+ }+ // A bare "quota exceeded" leaves the customer unable to act.+ for _, want := range []string{"delete an unused template"} {+ if !strings.Contains(err.Error(), want) {+ t.Errorf("message is not actionable, missing %q: %v", want, err)+ }+ }+}