diff --git a/weed/s3api/s3tables/handler_view_not_table_test.go b/weed/s3api/s3tables/handler_view_not_table_test.go new file mode 100644 index 0000000000..2546d298f5 --- /dev/null +++ b/weed/s3api/s3tables/handler_view_not_table_test.go @@ -0,0 +1,97 @@ +package s3tables + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// A view, or a table its create has not finished writing, is not a table: the +// table policy, maintenance and tag operations answer NoSuchTable for its name +// and write nothing to it. +func TestTableOperationsRefuseViewAndUnfinishedRecord(t *testing.T) { + fs, m := startRenameManager(t) + seedView(t, fs, "v") + nsPath := GetNamespacePath(renameTestBucket, "ns") + fs.Put(nsPath, "c", map[string][]byte{ + ExtendedKeyMetadata: []byte(`{"name":"c","namespace":"ns","ownerAccountId":"` + DefaultAccountID + `"}`), + ExtendedKeyDeleting: []byte(createClaim), + }) + bucketARN := mustBucketARN(t) + + for _, name := range []string{"v", "c"} { + before := extendedOf(t, fs, nsPath, name) + tableARN, err := BuildTableARN(DefaultRegion, DefaultAccountID, renameTestBucket, "ns", name) + require.NoError(t, err) + ops := []struct { + operation string + input interface{} + }{ + {"PutTablePolicy", &PutTablePolicyRequest{ + TableBucketARN: bucketARN, + Namespace: []string{"ns"}, + Name: name, + ResourcePolicy: `{"Statement":[{"Effect":"Allow","Principal":"*","Action":"s3tables:*","Resource":"*"}]}`, + }}, + {"GetTablePolicy", &GetTablePolicyRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: name}}, + {"DeleteTablePolicy", &DeleteTablePolicyRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: name}}, + {"PutTableMaintenanceConfiguration", &PutTableMaintenanceConfigurationRequest{ + TableBucketARN: bucketARN, + Namespace: []string{"ns"}, + Name: name, + Type: MaintenanceTypeIcebergCompaction, + Value: &MaintenanceConfigurationValue{Status: MaintenanceStatusEnabled}, + }}, + {"GetTableMaintenanceConfiguration", &GetTableMaintenanceConfigurationRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: name}}, + {"GetTableMaintenanceJobStatus", &GetTableMaintenanceJobStatusRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: name}}, + {"TagResource", &TagResourceRequest{ResourceARN: tableARN, Tags: map[string]string{"k": "v"}}}, + {"ListTagsForResource", &ListTagsForResourceRequest{ResourceARN: tableARN}}, + {"UntagResource", &UntagResourceRequest{ResourceARN: tableARN, TagKeys: []string{"k"}}}, + } + for _, op := range ops { + t.Run(name+"/"+op.operation, func(t *testing.T) { + status, resp := runHandler(t, m, fs.Client, op.operation, op.input) + assert.Equal(t, http.StatusNotFound, status, "%+v", resp) + assert.Equal(t, ErrCodeNoSuchTable, resp.Type, "%+v", resp) + }) + } + assert.Equal(t, before, extendedOf(t, fs, nsPath, name), "%s must not be written to", name) + } +} + +// An identity granted only s3tables:PutTablePolicy cannot plant a policy on +// another account's view through the table API and so open the view to itself. +func TestPutTablePolicyCannotOpenAView(t *testing.T) { + fs, m := startRenameManager(t) + m.SetTrusted(false) + m.SetDefaultAllow(false) + seedView(t, fs, "v") + bucketARN := mustBucketARN(t) + + call := func(operation string, input interface{}) int { + body, err := json.Marshal(input) + require.NoError(t, err) + identity := &testIdentity{Name: "other", Account: &testIdentityAccount{Id: "other"}, + Actions: []string{"s3tables:PutTablePolicy"}} + req, err := newManagerRequest(s3_constants.SetIdentityInContext(assignedCtx(), identity), operation, body, "other") + require.NoError(t, err) + rec := httptest.NewRecorder() + m.handler.HandleRequest(rec, req, NewManagerClient(fs.Client)) + return rec.Code + } + + assert.Equal(t, http.StatusNotFound, call("PutTablePolicy", &PutTablePolicyRequest{ + TableBucketARN: bucketARN, + Namespace: []string{"ns"}, + Name: "v", + ResourcePolicy: `{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":"*","Action":"s3tables:*","Resource":"*"}]}`, + })) + assert.NotEqual(t, http.StatusOK, call("GetView", &GetViewRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: "v"})) + assert.NotEqual(t, http.StatusOK, call("DeleteView", &DeleteViewRequest{TableBucketARN: bucketARN, Namespace: []string{"ns"}, Name: "v"})) + assert.NotNil(t, fs.Get(GetNamespacePath(renameTestBucket, "ns"), "v"), "the view must still be stored") +}