package objectgateway_test import ( "bytes" "net" "os" "testing" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/s3" g "knowledge-governance/internal/objectgateway" ) // A maintenance update of the target KB runs while the same bucket still // serves other writers. Publishing planned objects must neither require a // whole-bucket exclusivity attestation nor disturb an unrelated key or a // pre-existing object at a planned key. External writes here are made before // execute (coexistence), not interleaved concurrently with the PUT. func TestGatewaySharedBucketCoexistenceWithOtherWriters(t *testing.T) { ctx, pool, backend := fixture(t) endpoint := os.Getenv("KG_GATEWAY_TEST_ENDPOINT") scope := g.Digest([]byte("minio\x00" + endpoint + "\x00" + backend.Bucket)) listener, e := net.Listen("tcp", "127.0.0.1:0") if e != nil { t.Fatal(e) } addr := listener.Addr().String() listener.Close() data, control := g.Digest([]byte("data "+scope)), g.Digest([]byte("control "+scope)) client, e := g.NewClient("http://"+addr, data, scope) if e != nil { t.Fatal(e) } admin, _ := g.NewClient("http://"+addr, control, scope) // Deliberately no OBJECT_GATEWAY_AUDIT_FILE / OBJECT_GATEWAY_AUDIT_PUBLIC_KEY: // the default shared-bucket path must start without whole-bucket evidence. env := []string{ "PATH=" + os.Getenv("PATH"), "HOME=" + os.Getenv("HOME"), "OBJECT_GATEWAY_LISTEN=" + addr, "OBJECT_GATEWAY_DATABASE_URL=" + os.Getenv("KG_GATEWAY_TEST_DATABASE_URL"), "OBJECT_GATEWAY_PROVIDER=minio", "OBJECT_GATEWAY_ENDPOINT=" + endpoint, "OBJECT_GATEWAY_BUCKET=" + backend.Bucket, "OBJECT_GATEWAY_REGION=us-east-1", "OBJECT_GATEWAY_ACCESS_KEY=" + os.Getenv("KG_GATEWAY_TEST_ACCESS_KEY"), "OBJECT_GATEWAY_SECRET_KEY=" + os.Getenv("KG_GATEWAY_TEST_SECRET_KEY"), "OBJECT_GATEWAY_DATA_TOKEN=" + data, "OBJECT_GATEWAY_CONTROL_TOKEN=" + control, } startGateway(t, env, client) if e = client.ReadyForAcquisition(ctx); e == nil { t.Fatal("gateway without control authority advertised automatic grants") } // Another service keeps writing unrelated keys during the maintenance window. externalOwner := g.Digest([]byte("other-service")) unrelatedKey := "other-service/unrelated-" + g.Digest([]byte(backend.Bucket))[:8] unrelatedBody := []byte("unrelated service body") if _, e = backend.Client.PutObject(ctx, &s3.PutObjectInput{Bucket: aws.String(backend.Bucket), Key: aws.String(unrelatedKey), Body: bytes.NewReader(unrelatedBody), Metadata: map[string]string{"kg-release-owner": externalOwner}}); e != nil { t.Fatalf("external unrelated write failed: %v", e) } unrelated, _, e := backend.Observe(ctx, unrelatedKey, "") if e != nil { t.Fatal(e) } // A planned key already holds a foreign object: the plan must refuse it and // leave that object byte-identical. existingKey := "unchanged/existing-object" existingBody := []byte("pre-existing production content") if _, e = backend.Client.PutObject(ctx, &s3.PutObjectInput{Bucket: aws.String(backend.Bucket), Key: aws.String(existingKey), Body: bytes.NewReader(existingBody), Metadata: map[string]string{"kg-release-owner": externalOwner}}); e != nil { t.Fatal(e) } existing, _, e := backend.Observe(ctx, existingKey, "") if e != nil { t.Fatal(e) } grant := g.Grant{Epoch: 1, Owner: g.Digest([]byte("maintenance-owner")), Plan: g.Digest([]byte("maintenance-plan"))} fresh := request("shared-fresh", grant) clash := request("shared-clash", grant) clash.Key = existingKey if e = admin.InstallPlan(ctx, planFor(grant, fresh, clash)); e != nil { t.Fatal(e) } if e = admin.Advance(ctx, 0, grant); e != nil { t.Fatal(e) } receipt, e := client.Execute(ctx, fresh, []byte("gateway fixture shared-fresh")) if e != nil || receipt.State != "committed" { t.Fatalf("planned create blocked by unrelated writers: %v %s", e, receipt.State) } rejected, e := client.Execute(ctx, clash, []byte("gateway fixture shared-clash")) if e != nil || rejected.State != "rejected" { t.Fatalf("pre-existing planned key not rejected: %v %s", e, rejected.State) } after, exists, e := backend.Observe(ctx, existingKey, "") if e != nil || !exists || after.ID != existing.ID || after.SHA256 != g.Digest(existingBody) { t.Fatalf("pre-existing object modified: %v %+v", e, after) } still, exists, e := backend.Observe(ctx, unrelatedKey, "") if e != nil || !exists || still.ID != unrelated.ID { t.Fatalf("unrelated object disturbed: %v %+v", e, still) } // Precise rollback removes only the version this gateway created. cleanup(t, ctx, &g.Gateway{Pool: pool, Scope: scope, Backend: backend, MaxBytes: 1 << 20}, receipt) for key, keep := range map[string]g.Version{existingKey: existing, unrelatedKey: unrelated} { if e = backend.Delete(ctx, key, keep); e != nil { t.Fatalf("external fixture cleanup %s: %v", key, e) } } }