From b4afef5663cdd32511748b40c47c53aed862bb9e Mon Sep 17 00:00:00 2001 From: Tulsi Shah Date: Tue, 26 Sep 2023 07:11:59 +0000 Subject: [PATCH] amend --- flags.go | 7 +++++ internal/storage/storageutil/client.go | 7 +++-- internal/storage/storageutil/client_test.go | 34 +++++++++++++++++++-- main.go | 1 + 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/flags.go b/flags.go index e48dd86612..3b06845e23 100644 --- a/flags.go +++ b/flags.go @@ -187,6 +187,11 @@ func newApp() (app *cli.App) { Usage: "File chunk size to read from GCS in one call. Need to specify the value in MB. ChunkSize less than 1MB is not supported", }, + cli.BoolFlag{ + Name: "disable-auth", + Usage: "Disable authorization", + }, + ///////////////////////// // Tuning ///////////////////////// @@ -378,6 +383,7 @@ type flagStorage struct { EgressBandwidthLimitBytesPerSecond float64 OpRateLimitHz float64 SequentialReadSizeMb int32 + DisableAuth bool // Tuning MaxRetrySleep time.Duration @@ -488,6 +494,7 @@ func populateFlags(c *cli.Context) (flags *flagStorage, err error) { EgressBandwidthLimitBytesPerSecond: c.Float64("limit-bytes-per-sec"), OpRateLimitHz: c.Float64("limit-ops-per-sec"), SequentialReadSizeMb: int32(c.Int("sequential-read-size-mb")), + DisableAuth: c.Bool("disable-auth"), // Tuning, MaxRetrySleep: c.Duration("max-retry-sleep"), diff --git a/internal/storage/storageutil/client.go b/internal/storage/storageutil/client.go index 3459bab17d..e086ba24e2 100644 --- a/internal/storage/storageutil/client.go +++ b/internal/storage/storageutil/client.go @@ -40,6 +40,7 @@ type StorageClientConfig struct { TokenUrl string ReuseTokenFromUrl bool ExperimentalEnableJsonRead bool + DisableAuth bool } func CreateHttpClient(storageClientConfig *StorageClientConfig) (httpClient *http.Client, err error) { @@ -91,9 +92,9 @@ func CreateHttpClient(storageClientConfig *StorageClientConfig) (httpClient *htt // is nil, it creates the token-source from the provided key-file or using ADC search // order (https://cloud.google.com/docs/authentication/application-default-credentials#order). func createTokenSource(storageClientConfig *StorageClientConfig) (tokenSrc oauth2.TokenSource, err error) { - if storageClientConfig.CustomEndpoint == nil { + // Create token src only if disable-auth flag is false. + if !storageClientConfig.DisableAuth { return auth.GetTokenSource(context.Background(), storageClientConfig.KeyFile, storageClientConfig.TokenUrl, storageClientConfig.ReuseTokenFromUrl) - } else { - return oauth2.StaticTokenSource(&oauth2.Token{}), nil } + return nil, nil } diff --git a/internal/storage/storageutil/client_test.go b/internal/storage/storageutil/client_test.go index c345359c50..eae768a1f4 100644 --- a/internal/storage/storageutil/client_test.go +++ b/internal/storage/storageutil/client_test.go @@ -29,11 +29,12 @@ type clientTest struct { func init() { RegisterTestSuite(&clientTest{}) } -func (t *clientTest) TestCreateTokenSrcWithCustomEndpoint() { +func (t *clientTest) TestCreateTokenSrcWithCustomEndpointWhenDisableAuthIsTrue() { url, err := url.Parse(CustomEndpoint) AssertEq(nil, err) sc := GetDefaultStorageClientConfig() sc.CustomEndpoint = url + sc.DisableAuth = true tokenSrc, err := createTokenSource(&sc) @@ -41,9 +42,36 @@ func (t *clientTest) TestCreateTokenSrcWithCustomEndpoint() { ExpectNe(nil, &tokenSrc) } -func (t *clientTest) TestCreateTokenSrcWhenCustomEndpointIsNil() { +func (t *clientTest) TestCreateTokenSrcWithCustomEndpointWhenDisableAuthIsFalse() { + url, err := url.Parse(CustomEndpoint) + AssertEq(nil, err) + sc := GetDefaultStorageClientConfig() + sc.CustomEndpoint = url + sc.DisableAuth = false + + // It will try to create the actual auth token and fail since key-file doesn't exist. + tokenSrc, err := createTokenSource(&sc) + + ExpectNe(nil, err) + ExpectThat(err, oglematchers.Error(oglematchers.HasSubstr("no such file or directory"))) + ExpectNe(nil, &tokenSrc) +} + +func (t *clientTest) TestCreateTokenSrcWhenCustomEndpointIsNilAndDisableAuthIsTrue() { + sc := GetDefaultStorageClientConfig() + sc.CustomEndpoint = nil + sc.DisableAuth = true + + tokenSrc, err := createTokenSource(&sc) + + ExpectEq(nil, err) + ExpectEq(nil, tokenSrc) +} + +func (t *clientTest) TestCreateTokenSrcWhenCustomEndpointIsNilAndDisableAuthIsFalse() { sc := GetDefaultStorageClientConfig() sc.CustomEndpoint = nil + sc.DisableAuth = false // It will try to create the actual auth token and fail since key-file doesn't exist. tokenSrc, err := createTokenSource(&sc) @@ -55,6 +83,7 @@ func (t *clientTest) TestCreateTokenSrcWhenCustomEndpointIsNil() { func (t *clientTest) TestCreateHttpClientWithHttp1() { sc := GetDefaultStorageClientConfig() // By default http1 enabled + sc.DisableAuth = true // Act: this method add tokenSource and clientOptions. httpClient, err := CreateHttpClient(&sc) @@ -67,6 +96,7 @@ func (t *clientTest) TestCreateHttpClientWithHttp1() { func (t *clientTest) TestCreateHttpClientWithHttp2() { sc := GetDefaultStorageClientConfig() + sc.DisableAuth = true // Act: this method add tokenSource and clientOptions. httpClient, err := CreateHttpClient(&sc) diff --git a/main.go b/main.go index cc628c1397..8b6d6cc2f1 100644 --- a/main.go +++ b/main.go @@ -95,6 +95,7 @@ func createStorageHandle(flags *flagStorage) (storageHandle storage.StorageHandl TokenUrl: flags.TokenUrl, ReuseTokenFromUrl: flags.ReuseTokenFromUrl, ExperimentalEnableJsonRead: flags.ExperimentalEnableJsonRead, + DisableAuth: flags.DisableAuth, } storageHandle, err = storage.NewStorageHandle(context.Background(), storageClientConfig)