From 70bf4107034085068fb6847667657fbb78367be4 Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 6 Dec 2023 17:04:55 -0500 Subject: [PATCH] Change config loading logic --- config/env_config.go | 48 ++++++++++++++++++++++------------------- config/shared_config.go | 3 ++- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/config/env_config.go b/config/env_config.go index 1a640ce47bf..88550198cce 100644 --- a/config/env_config.go +++ b/config/env_config.go @@ -12,6 +12,7 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" + smithyrequestcompression "github.com/aws/smithy-go/private/requestcompression" ) // CredentialsSourceName provides a name of the provider when config is @@ -334,7 +335,7 @@ func NewEnvConfig() (EnvConfig, error) { if err := setBoolPtrFromEnvVal(&cfg.DisableRequestCompression, []string{awsDisableRequestCompression}); err != nil { return cfg, err } - if err := setRequestMinCompressSizeBytes(&cfg.RequestMinCompressSizeBytes); err != nil { + if err := setInt64PtrFromEnvVal(&cfg.RequestMinCompressSizeBytes, []string{awsRequestMinCompressionSizeBytes}, smithyrequestcompression.MaxRequestMinCompressSizeBytes); err != nil { return cfg, err } @@ -402,27 +403,6 @@ func (c EnvConfig) getAppID(context.Context) (string, bool, error) { return c.AppID, len(c.AppID) > 0, nil } -func setRequestMinCompressSizeBytes(bytes **int64) error { - b := os.Getenv(awsRequestMinCompressionSizeBytes) - if b == "" { - return nil - } - - byte, err := strconv.ParseInt(b, 10, 64) - if err != nil { - return fmt.Errorf("invalid value for env var, %s=%s, need int64", - awsRequestMinCompressionSizeBytes, b) - } else if byte < 0 || byte > 10485760 { - return fmt.Errorf("invalid range for env var min request compression size bytes %q, must be within 0 and 10485760 inclusively", byte) - } - if *bytes == nil { - *bytes = new(int64) - } - **bytes = byte - - return nil -} - func (c EnvConfig) getDisableRequestCompression(context.Context) (bool, bool, error) { if c.DisableRequestCompression == nil { return false, false, nil @@ -693,6 +673,30 @@ func setBoolPtrFromEnvVal(dst **bool, keys []string) error { return nil } +func setInt64PtrFromEnvVal(dst **int64, keys []string, max int64) error { + for _, k := range keys { + value := os.Getenv(k) + if len(value) == 0 { + continue + } + + v, err := strconv.ParseInt(value, 10, 64) + if err != nil { + return fmt.Errorf("invalid value for env var, %s=%s, need int64", k, value) + } else if v < 0 || v > max { + return fmt.Errorf("invalid range for env var min request compression size bytes %q, must be within 0 and 10485760 inclusively", v) + } + if *dst == nil { + *dst = new(int64) + } + + **dst = v + break + } + + return nil +} + func setEndpointDiscoveryTypeFromEnvVal(dst *aws.EndpointDiscoveryEnableState, keys []string) error { for _, k := range keys { value := os.Getenv(k) diff --git a/config/shared_config.go b/config/shared_config.go index 177051480c0..422dfc55bea 100644 --- a/config/shared_config.go +++ b/config/shared_config.go @@ -17,6 +17,7 @@ import ( "github.com/aws/aws-sdk-go-v2/internal/ini" "github.com/aws/aws-sdk-go-v2/internal/shareddefaults" "github.com/aws/smithy-go/logging" + smithyrequestcompression "github.com/aws/smithy-go/private/requestcompression" ) const ( @@ -1147,7 +1148,7 @@ func updateRequestMinCompressSizeBytes(bytes **int64, sec ini.Section, key strin if !ok { return fmt.Errorf("invalid value for min request compression size bytes %s, need int64", sec.String(key)) } - if v < 0 || v > 10485760 { + if v < 0 || v > smithyrequestcompression.MaxRequestMinCompressSizeBytes { return fmt.Errorf("invalid range for min request compression size bytes %d, must be within 0 and 10485760 inclusively", v) } *bytes = new(int64)