|
| 1 | +/* |
| 2 | + * MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 3 | + * Copyright 2026 MinIO, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package minio |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "errors" |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "sync" |
| 26 | + "testing" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/minio/minio-go/v7/pkg/credentials" |
| 30 | +) |
| 31 | + |
| 32 | +// blockingProvider is a credentials.Provider whose retrieval blocks until |
| 33 | +// released, honoring the CredContext caller context like real providers do. |
| 34 | +type blockingProvider struct { |
| 35 | + startedOnce sync.Once |
| 36 | + started chan struct{} |
| 37 | + release chan struct{} |
| 38 | +} |
| 39 | + |
| 40 | +func (p *blockingProvider) RetrieveWithCredContext(cc *credentials.CredContext) (credentials.Value, error) { |
| 41 | + p.startedOnce.Do(func() { close(p.started) }) |
| 42 | + var done <-chan struct{} |
| 43 | + if cc != nil && cc.Context != nil { |
| 44 | + done = cc.Context.Done() |
| 45 | + } |
| 46 | + select { |
| 47 | + case <-p.release: |
| 48 | + case <-done: |
| 49 | + return credentials.Value{}, cc.Context.Err() |
| 50 | + } |
| 51 | + return credentials.Value{ |
| 52 | + AccessKeyID: "accessKey", |
| 53 | + SecretAccessKey: "secret", |
| 54 | + SignerType: credentials.SignatureV4, |
| 55 | + }, nil |
| 56 | +} |
| 57 | + |
| 58 | +func (p *blockingProvider) Retrieve() (credentials.Value, error) { |
| 59 | + return p.RetrieveWithCredContext(nil) |
| 60 | +} |
| 61 | + |
| 62 | +func (p *blockingProvider) IsExpired() bool { return true } |
| 63 | + |
| 64 | +// TestCredsCancelDoesNotPoisonWaiters verifies that canceling one caller |
| 65 | +// during a de-duplicated credential retrieval fails only that caller: the |
| 66 | +// shared retrieval is detached from any single caller's context, so a |
| 67 | +// concurrent waiter on the same bucket still succeeds. |
| 68 | +func TestCredsCancelDoesNotPoisonWaiters(t *testing.T) { |
| 69 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 70 | + w.WriteHeader(http.StatusOK) |
| 71 | + })) |
| 72 | + defer srv.Close() |
| 73 | + |
| 74 | + provider := &blockingProvider{started: make(chan struct{}), release: make(chan struct{})} |
| 75 | + clnt, err := New(srv.Listener.Addr().String(), &Options{ |
| 76 | + Creds: credentials.New(provider), |
| 77 | + Region: "us-east-1", |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + |
| 83 | + ctx, cancel := context.WithCancel(context.Background()) |
| 84 | + defer cancel() |
| 85 | + |
| 86 | + canceledErr := make(chan error, 1) |
| 87 | + go func() { |
| 88 | + _, err := clnt.BucketExists(ctx, "test-bucket") |
| 89 | + canceledErr <- err |
| 90 | + }() |
| 91 | + |
| 92 | + <-provider.started |
| 93 | + |
| 94 | + waiterErr := make(chan error, 1) |
| 95 | + go func() { |
| 96 | + _, err := clnt.BucketExists(context.Background(), "test-bucket") |
| 97 | + waiterErr <- err |
| 98 | + }() |
| 99 | + |
| 100 | + // Cancel the first caller mid-retrieval; the waiter must not inherit |
| 101 | + // that cancellation, whether it blocks on the credentials mutex or |
| 102 | + // joins the de-dup group. |
| 103 | + cancel() |
| 104 | + |
| 105 | + if err := <-canceledErr; !errors.Is(err, context.Canceled) { |
| 106 | + t.Fatalf("Expected context.Canceled for the canceled caller, got %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + close(provider.release) |
| 110 | + |
| 111 | + if err := <-waiterErr; err != nil { |
| 112 | + t.Fatalf("Expected the concurrent waiter to succeed, got %v", err) |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// panicProvider is a credentials.Provider whose retrieval always panics. |
| 117 | +type panicProvider struct{} |
| 118 | + |
| 119 | +func (panicProvider) RetrieveWithCredContext(*credentials.CredContext) (credentials.Value, error) { |
| 120 | + panic("boom") |
| 121 | +} |
| 122 | + |
| 123 | +func (p panicProvider) Retrieve() (credentials.Value, error) { |
| 124 | + return p.RetrieveWithCredContext(nil) |
| 125 | +} |
| 126 | + |
| 127 | +func (panicProvider) IsExpired() bool { return true } |
| 128 | + |
| 129 | +// TestCredsRetrievalPanicPropagates verifies that a provider panic inside |
| 130 | +// the de-duplicated credential retrieval resumes on the caller's goroutine |
| 131 | +// instead of crashing the process from the retrieval goroutine. |
| 132 | +func TestCredsRetrievalPanicPropagates(t *testing.T) { |
| 133 | + clnt, err := New("s3.amazonaws.com", &Options{ |
| 134 | + Creds: credentials.New(panicProvider{}), |
| 135 | + Region: "us-east-1", |
| 136 | + }) |
| 137 | + if err != nil { |
| 138 | + t.Fatal(err) |
| 139 | + } |
| 140 | + |
| 141 | + defer func() { |
| 142 | + if r := recover(); r != "boom" { |
| 143 | + t.Fatalf("Expected panic value %q on the caller goroutine, got %v", "boom", r) |
| 144 | + } |
| 145 | + }() |
| 146 | + exists, err := clnt.BucketExists(context.Background(), "test-bucket") |
| 147 | + t.Fatalf("Expected a panic before return, got exists=%v err=%v", exists, err) |
| 148 | +} |
| 149 | + |
| 150 | +// TestPresignCredsCallerContext verifies that a direct (non-de-duplicated) |
| 151 | +// credential retrieval receives the caller context: a canceled caller |
| 152 | +// context aborts presigning inside the provider. |
| 153 | +func TestPresignCredsCallerContext(t *testing.T) { |
| 154 | + provider := &blockingProvider{started: make(chan struct{}), release: make(chan struct{})} |
| 155 | + clnt, err := New("s3.amazonaws.com", &Options{ |
| 156 | + Creds: credentials.New(provider), |
| 157 | + Region: "us-east-1", |
| 158 | + }) |
| 159 | + if err != nil { |
| 160 | + t.Fatal(err) |
| 161 | + } |
| 162 | + |
| 163 | + ctx, cancel := context.WithCancel(context.Background()) |
| 164 | + cancel() |
| 165 | + |
| 166 | + policy := NewPostPolicy() |
| 167 | + if err := policy.SetBucket("test-bucket"); err != nil { |
| 168 | + t.Fatal(err) |
| 169 | + } |
| 170 | + if err := policy.SetKey("obj"); err != nil { |
| 171 | + t.Fatal(err) |
| 172 | + } |
| 173 | + if err := policy.SetExpires(time.Now().UTC().Add(time.Hour)); err != nil { |
| 174 | + t.Fatal(err) |
| 175 | + } |
| 176 | + |
| 177 | + _, _, err = clnt.PresignedPostPolicy(ctx, policy) |
| 178 | + if !errors.Is(err, context.Canceled) { |
| 179 | + t.Fatalf("Expected context.Canceled from the credential provider, got %v", err) |
| 180 | + } |
| 181 | +} |
0 commit comments