diff --git a/weed/s3api/iceberg/iceberg_credential_vending_test.go b/weed/s3api/iceberg/iceberg_credential_vending_test.go index a10b035691..6170a78659 100644 --- a/weed/s3api/iceberg/iceberg_credential_vending_test.go +++ b/weed/s3api/iceberg/iceberg_credential_vending_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "net/http/httptest" + "strings" "testing" "time" ) @@ -176,3 +177,50 @@ func TestCredentialDurationHeaderReachesTheVendor(t *testing.T) { } } } + +// A load or view answer that carries vended credentials is never stored by a +// cache; one without them keeps its usual caching. +func TestVendedLoadAnswerIsNoStore(t *testing.T) { + for _, tc := range []struct { + name string + delegate bool + wantNoStore bool + }{ + {"vended", true, true}, + {"not vended", false, false}, + } { + t.Run(tc.name, func(t *testing.T) { + vendor := &stubVendor{credentials: &VendedCredentials{ + AccessKeyID: "ASIAEXAMPLE", SecretAccessKey: "secret", SessionToken: "token", + }} + s := vendingServer(vendor) + r := httptest.NewRequest("GET", "/v1/namespaces/ns/tables/t", nil) + if tc.delegate { + r.Header.Set(accessDelegationHeader, "vended-credentials") + } + config, storageCredentials := s.buildFileIOConfig(r, "s3://warehouse/ns/t", nil) + if tc.delegate && config["s3.session-token"] != "token" { + t.Fatalf("nothing vended: %v", config) + } + if !tc.delegate && config["s3.endpoint"] == "" { + t.Fatalf("no FileIO config built: %v", config) + } + table := LoadTableResult{Config: config, StorageCredentials: storageCredentials} + for _, result := range []interface{}{ + table, + &table, + LoadTableResult{StorageCredentials: storageCredentials}, + ViewResponse{Config: config}, + } { + w := httptest.NewRecorder() + writeLoadResult(w, 200, result) + if got := w.Header().Get("Cache-Control") == "no-store"; got != tc.wantNoStore { + t.Errorf("%T: Cache-Control = %q, want no-store=%v", result, w.Header().Get("Cache-Control"), tc.wantNoStore) + } + if got := w.Header().Get("Vary"); !strings.Contains(got, accessDelegationHeader) { + t.Errorf("%T: Vary = %q, want it to name %q", result, got, accessDelegationHeader) + } + } + }) + } +} diff --git a/weed/s3api/iceberg/utils.go b/weed/s3api/iceberg/utils.go index ca890ec76f..e6a38898f5 100644 --- a/weed/s3api/iceberg/utils.go +++ b/weed/s3api/iceberg/utils.go @@ -145,12 +145,51 @@ func requestedCredentialDuration(r *http.Request) int64 { // writeLoadResult writes a table or view load response. The FileIO config it // carries depends on the delegation and the session lifetime the client asked // for, so a cache between us and the client must key on those headers and not -// on the URL alone. +// on the URL alone. A response that carries vended credentials must not be +// stored by any cache at all (Cache-Control: no-store, as for the OAuth token +// response). func writeLoadResult(w http.ResponseWriter, status int, v interface{}) { w.Header().Set("Vary", accessDelegationHeader+", "+credentialDurationHeader) + if carriesCredentials(v) { + w.Header().Set("Cache-Control", "no-store") + } writeJSON(w, status, v) } +// carriesCredentials reports whether a load response holds vended +// credentials, in storage-credentials or in its FileIO config. +func carriesCredentials(v interface{}) bool { + var config iceberg.Properties + switch result := v.(type) { + case LoadTableResult: + if len(result.StorageCredentials) > 0 { + return true + } + config = result.Config + case *LoadTableResult: + if result == nil { + return false + } + if len(result.StorageCredentials) > 0 { + return true + } + config = result.Config + case ViewResponse: + config = result.Config + case *ViewResponse: + if result == nil { + return false + } + config = result.Config + } + for _, key := range []string{"s3.access-key-id", "s3.secret-access-key", "s3.session-token"} { + if config[key] != "" { + return true + } + } + return false +} + // writeJSON writes a JSON response. func writeJSON(w http.ResponseWriter, status int, v interface{}) { w.Header().Set("Content-Type", "application/json")