@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"crypto/tls"
22
22
"crypto/x509"
23
+ "encoding/json"
23
24
"errors"
24
25
"fmt"
25
26
"log"
@@ -28,13 +29,14 @@ import (
28
29
"net/url"
29
30
"os"
30
31
"path/filepath"
32
+ "strconv"
31
33
"strings"
32
34
"testing"
33
35
"time"
34
36
35
- "github.com/elazarl/goproxy"
36
37
"github.com/google/uuid"
37
38
miniov7 "github.com/minio/minio-go/v7"
39
+ "github.com/minio/minio-go/v7/pkg/credentials"
38
40
"github.com/ory/dockertest/v3"
39
41
"github.com/ory/dockertest/v3/docker"
40
42
"gotest.tools/assert"
@@ -45,6 +47,7 @@ import (
45
47
"github.com/fluxcd/pkg/sourceignore"
46
48
47
49
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
50
+ testproxy "github.com/fluxcd/source-controller/tests/proxy"
48
51
)
49
52
50
53
const (
@@ -244,34 +247,142 @@ func TestFGetObject(t *testing.T) {
244
247
assert .NilError (t , err )
245
248
}
246
249
247
- func TestNewClientAndFGetObjectWithProxy (t * testing.T ) {
250
+ func TestNewClientAndFGetObjectWithSTSEndpoint (t * testing.T ) {
251
+ // start a mock STS server
252
+ stsListener , err := net .Listen ("tcp" , ":0" )
253
+ assert .NilError (t , err , "could not start STS listener" )
254
+ defer stsListener .Close ()
255
+ stsAddr := stsListener .Addr ().String ()
256
+ stsEndpoint := fmt .Sprintf ("http://%s" , stsAddr )
257
+ stsAddrParts := strings .Split (stsAddr , ":" )
258
+ stsPortStr := stsAddrParts [len (stsAddrParts )- 1 ]
259
+ stsPort , err := strconv .Atoi (stsPortStr )
260
+ assert .NilError (t , err )
261
+ stsHandler := http .NewServeMux ()
262
+ stsHandler .HandleFunc ("PUT " + credentials .TokenPath ,
263
+ func (w http.ResponseWriter , r * http.Request ) {
264
+ _ , err := w .Write ([]byte ("mock-token" ))
265
+ assert .NilError (t , err )
266
+ })
267
+ stsHandler .HandleFunc ("GET " + credentials .DefaultIAMSecurityCredsPath ,
268
+ func (w http.ResponseWriter , r * http.Request ) {
269
+ token := r .Header .Get (credentials .TokenRequestHeader )
270
+ assert .Equal (t , token , "mock-token" )
271
+ _ , err := w .Write ([]byte ("mock-role" ))
272
+ assert .NilError (t , err )
273
+ })
274
+ var roleCredsRetrieved bool
275
+ stsHandler .HandleFunc ("GET " + credentials .DefaultIAMSecurityCredsPath + "mock-role" ,
276
+ func (w http.ResponseWriter , r * http.Request ) {
277
+ token := r .Header .Get (credentials .TokenRequestHeader )
278
+ assert .Equal (t , token , "mock-token" )
279
+ err := json .NewEncoder (w ).Encode (map [string ]any {
280
+ "Code" : "Success" ,
281
+ "AccessKeyID" : testMinioRootUser ,
282
+ "SecretAccessKey" : testMinioRootPassword ,
283
+ })
284
+ assert .NilError (t , err )
285
+ roleCredsRetrieved = true
286
+ })
287
+ stsServer := & http.Server {
288
+ Addr : stsAddr ,
289
+ Handler : stsHandler ,
290
+ }
291
+ go stsServer .Serve (stsListener )
292
+ defer stsServer .Shutdown (context .Background ())
293
+
248
294
// start proxy
249
- proxyListener , err := net .Listen ("tcp" , ":0" )
250
- assert .NilError (t , err , "could not start proxy server" )
251
- defer proxyListener .Close ()
252
- proxyAddr := proxyListener .Addr ().String ()
253
- proxyHandler := goproxy .NewProxyHttpServer ()
254
- proxyHandler .Verbose = true
255
- proxyServer := & http.Server {
256
- Addr : proxyAddr ,
257
- Handler : proxyHandler ,
295
+ proxyAddr , proxyPort := testproxy .New (t )
296
+
297
+ tests := []struct {
298
+ name string
299
+ stsEndpoint string
300
+ opts []Option
301
+ errSubstring string
302
+ }{
303
+ {
304
+ name : "with correct endpoint" ,
305
+ stsEndpoint : stsEndpoint ,
306
+ },
307
+ {
308
+ name : "with incorrect endpoint" ,
309
+ stsEndpoint : fmt .Sprintf ("http://localhost:%d" , stsPort + 1 ),
310
+ errSubstring : "connection refused" ,
311
+ },
312
+ {
313
+ name : "with correct endpoint and proxy" ,
314
+ stsEndpoint : stsEndpoint ,
315
+ opts : []Option {WithProxyURL (& url.URL {Scheme : "http" , Host : proxyAddr })},
316
+ },
317
+ {
318
+ name : "with correct endpoint and incorrect proxy" ,
319
+ stsEndpoint : stsEndpoint ,
320
+ opts : []Option {WithProxyURL (& url.URL {Scheme : "http" , Host : fmt .Sprintf ("localhost:%d" , proxyPort + 1 )})},
321
+ errSubstring : "connection refused" ,
322
+ },
323
+ }
324
+
325
+ for _ , tt := range tests {
326
+ t .Run (tt .name , func (t * testing.T ) {
327
+ roleCredsRetrieved = false
328
+ bucket := bucketStub (bucket , testMinioAddress )
329
+ bucket .Spec .STSEndpoint = tt .stsEndpoint
330
+ minioClient , err := NewClient (bucket , append (tt .opts , WithTLSConfig (testTLSConfig ))... )
331
+ assert .NilError (t , err )
332
+ assert .Assert (t , minioClient != nil )
333
+ ctx := context .Background ()
334
+ tempDir := t .TempDir ()
335
+ path := filepath .Join (tempDir , sourceignore .IgnoreFile )
336
+ _ , err = minioClient .FGetObject (ctx , bucketName , objectName , path )
337
+ if tt .errSubstring != "" {
338
+ assert .ErrorContains (t , err , tt .errSubstring )
339
+ } else {
340
+ assert .NilError (t , err )
341
+ assert .Assert (t , roleCredsRetrieved )
342
+ }
343
+ })
344
+ }
345
+ }
346
+
347
+ func TestNewClientAndFGetObjectWithProxy (t * testing.T ) {
348
+ proxyAddr , proxyPort := testproxy .New (t )
349
+
350
+ tests := []struct {
351
+ name string
352
+ proxyURL * url.URL
353
+ errSubstring string
354
+ }{
355
+ {
356
+ name : "with correct proxy" ,
357
+ proxyURL : & url.URL {Scheme : "http" , Host : proxyAddr },
358
+ },
359
+ {
360
+ name : "with incorrect proxy" ,
361
+ proxyURL : & url.URL {Scheme : "http" , Host : fmt .Sprintf ("localhost:%d" , proxyPort + 1 )},
362
+ errSubstring : "connection refused" ,
363
+ },
258
364
}
259
- go proxyServer .Serve (proxyListener )
260
- defer proxyServer .Shutdown (context .Background ())
261
- proxyURL := & url.URL {Scheme : "http" , Host : proxyAddr }
262
365
263
366
// run test
264
- minioClient , err := NewClient (bucketStub (bucket , testMinioAddress ),
265
- WithSecret (secret .DeepCopy ()),
266
- WithTLSConfig (testTLSConfig ),
267
- WithProxyURL (proxyURL ))
268
- assert .NilError (t , err )
269
- assert .Assert (t , minioClient != nil )
270
- ctx := context .Background ()
271
- tempDir := t .TempDir ()
272
- path := filepath .Join (tempDir , sourceignore .IgnoreFile )
273
- _ , err = minioClient .FGetObject (ctx , bucketName , objectName , path )
274
- assert .NilError (t , err )
367
+ for _ , tt := range tests {
368
+ t .Run (tt .name , func (t * testing.T ) {
369
+ minioClient , err := NewClient (bucketStub (bucket , testMinioAddress ),
370
+ WithSecret (secret .DeepCopy ()),
371
+ WithTLSConfig (testTLSConfig ),
372
+ WithProxyURL (tt .proxyURL ))
373
+ assert .NilError (t , err )
374
+ assert .Assert (t , minioClient != nil )
375
+ ctx := context .Background ()
376
+ tempDir := t .TempDir ()
377
+ path := filepath .Join (tempDir , sourceignore .IgnoreFile )
378
+ _ , err = minioClient .FGetObject (ctx , bucketName , objectName , path )
379
+ if tt .errSubstring != "" {
380
+ assert .ErrorContains (t , err , tt .errSubstring )
381
+ } else {
382
+ assert .NilError (t , err )
383
+ }
384
+ })
385
+ }
275
386
}
276
387
277
388
func TestFGetObjectNotExists (t * testing.T ) {
0 commit comments