From 3ac109e759c61f2d8dd2b2b849a8aed944f8bb64 Mon Sep 17 00:00:00 2001 From: Isabella Siu Date: Wed, 26 Jun 2024 11:56:39 -0400 Subject: [PATCH] AWS: Update deprecated aws-sdk functions from env variable versions (#89643) --- go.mod | 2 +- go.sum | 3 +-- go.work.sum | 4 +-- .../http_client_provider.go | 18 ++++++++++++- pkg/registry/apis/datasource/middleware.go | 3 ++- pkg/tsdb/cloudwatch/cloudwatch.go | 9 +++---- pkg/tsdb/cloudwatch/cloudwatch_test.go | 7 +++-- pkg/tsdb/cloudwatch/models/settings.go | 3 ++- pkg/tsdb/cloudwatch/test_utils.go | 12 ++++----- pkg/tsdb/cloudwatch/time_series_query_test.go | 26 ++++++++++++------- 10 files changed, 54 insertions(+), 33 deletions(-) diff --git a/go.mod b/go.mod index be519c7bd83..3e0d4f4dd67 100644 --- a/go.mod +++ b/go.mod @@ -87,7 +87,7 @@ require ( github.com/grafana/dskit v0.0.0-20240104111617-ea101a3b86eb // @grafana/grafana-backend-group github.com/grafana/gofpdf v0.0.0-20231002120153-857cc45be447 // @grafana/sharing-squad github.com/grafana/gomemcache v0.0.0-20231023152154-6947259a0586 // @grafana/grafana-operator-experience-squad - github.com/grafana/grafana-aws-sdk v0.26.1 // @grafana/aws-datasources + github.com/grafana/grafana-aws-sdk v0.28.0 // @grafana/aws-datasources github.com/grafana/grafana-azure-sdk-go/v2 v2.0.4 // @grafana/partner-datasources github.com/grafana/grafana-google-sdk-go v0.1.0 // @grafana/partner-datasources github.com/grafana/grafana-openapi-client-go v0.0.0-20231213163343-bd475d63fb79 // @grafana/grafana-backend-group diff --git a/go.sum b/go.sum index d6616ff8998..94093f963fb 100644 --- a/go.sum +++ b/go.sum @@ -2317,8 +2317,7 @@ github.com/grafana/gofpdf v0.0.0-20231002120153-857cc45be447 h1:jxJJ5z0GxqhWFbQU github.com/grafana/gofpdf v0.0.0-20231002120153-857cc45be447/go.mod h1:IxsY6mns6Q5sAnWcrptrgUrSglTZJXH/kXr9nbpb/9I= github.com/grafana/gomemcache v0.0.0-20231023152154-6947259a0586 h1:/of8Z8taCPftShATouOrBVy6GaTTjgQd/VfNiZp/VXQ= github.com/grafana/gomemcache v0.0.0-20231023152154-6947259a0586/go.mod h1:PGk3RjYHpxMM8HFPhKKo+vve3DdlPUELZLSDEFehPuU= -github.com/grafana/grafana-aws-sdk v0.26.1 h1:Q9Bb3t1UKiRU3sso4d9gLcZo/r1JxJqIF5dKohsRRWY= -github.com/grafana/grafana-aws-sdk v0.26.1/go.mod h1:ZSVPU7IIJSi5lEg+K3Js+EUpZLXxUaBdaQWH+As1ihI= +github.com/grafana/grafana-aws-sdk v0.28.0 h1:ShdA+msLPGJGWWS1SFUYnF+ch1G3gUOlAdGJi6h4sgU= github.com/grafana/grafana-azure-sdk-go/v2 v2.0.4 h1:z6amQ286IJSBctHf6c+ibJq/v0+TvmEjVkrdMNBd4uY= github.com/grafana/grafana-azure-sdk-go/v2 v2.0.4/go.mod h1:aKlFPE36IDa8qccRg3KbgZX3MQ5xymS3RelT4j6kkVU= github.com/grafana/grafana-google-sdk-go v0.1.0 h1:LKGY8z2DSxKjYfr2flZsWgTRTZ6HGQbTqewE3JvRaNA= diff --git a/go.work.sum b/go.work.sum index 7eec5551ec7..439079c2cb9 100644 --- a/go.work.sum +++ b/go.work.sum @@ -480,7 +480,6 @@ github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjH github.com/alecthomas/kong v0.2.11 h1:RKeJXXWfg9N47RYfMm0+igkxBCTF4bzbneAxaqid0c4= github.com/alecthomas/kong v0.2.11/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE= github.com/alecthomas/participle/v2 v2.1.0 h1:z7dElHRrOEEq45F2TG5cbQihMtNTv8vwldytDj7Wrz4= -github.com/alecthomas/participle/v2 v2.1.0/go.mod h1:Y1+hAs8DHPmc3YUFzqllV+eSQ9ljPTk0ZkPMtEdAx2c= github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= github.com/alexflint/go-arg v1.4.2/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM= @@ -887,7 +886,6 @@ github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og= github.com/gobwas/ws v1.2.1 h1:F2aeBZrm2NDsc7vbovKrWSogd4wvfAxg0FQ89/iqOTk= github.com/gobwas/ws v1.3.0/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/KY= github.com/goccy/go-yaml v1.11.0 h1:n7Z+zx8S9f9KgzG6KtQKf+kwqXZlLNR2F6018Dgau54= -github.com/goccy/go-yaml v1.11.0/go.mod h1:H+mJrWtjPTJAHvRbV09MCK9xYwODM+wRTVFFTWckfng= github.com/gocql/gocql v0.0.0-20190301043612-f6df8288f9b4 h1:vF83LI8tAakwEwvWZtrIEx7pOySacl2TOxx6eXk4ePo= github.com/godbus/dbus v0.0.0-20151105175453-c7fdd8b5cd55/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw= github.com/godbus/dbus v0.0.0-20180201030542-885f9cc04c9c/go.mod h1:/YcGZj5zSblfDWMMoOzV4fas9FZnQYTkDnsGvmh2Grw= @@ -945,6 +943,8 @@ github.com/grafana/dataplane/sdata v0.0.7 h1:CImITypIyS1jxijCR6xqKx71JnYAxcwpH9C github.com/grafana/dataplane/sdata v0.0.7/go.mod h1:Jvs5ddpGmn6vcxT7tCTWAZ1mgi4sbcdFt9utQx5uMAU= github.com/grafana/e2e v0.1.1-0.20221018202458-cffd2bb71c7b h1:Ha+kSIoTutf4ytlVw/SaEclDUloYx0+FXDKJWKhNbE4= github.com/grafana/e2e v0.1.1-0.20221018202458-cffd2bb71c7b/go.mod h1:3UsooRp7yW5/NJQBlXcTsAHOoykEhNUYXkQ3r6ehEEY= +github.com/grafana/grafana-aws-sdk v0.28.0 h1:ShdA+msLPGJGWWS1SFUYnF+ch1G3gUOlAdGJi6h4sgU= +github.com/grafana/grafana-aws-sdk v0.28.0/go.mod h1:ZSVPU7IIJSi5lEg+K3Js+EUpZLXxUaBdaQWH+As1ihI= github.com/grafana/grafana-plugin-sdk-go v0.212.0/go.mod h1:qsI4ktDf0lig74u8SLPJf9zRdVxWV/W4Wi+Ox6gifgs= github.com/grafana/grafana-plugin-sdk-go v0.215.0/go.mod h1:nBsh3jRItKQUXDF2BQkiQCPxqrsSQeb+7hiFyJTO1RE= github.com/grafana/grafana-plugin-sdk-go v0.216.0/go.mod h1:FdvSvOliqpVLnytM7e89zCFyYPDE6VOn9SIjVQRvVxM= diff --git a/pkg/infra/httpclient/httpclientprovider/http_client_provider.go b/pkg/infra/httpclient/httpclientprovider/http_client_provider.go index 13ef9674c1c..c40dc894ee9 100644 --- a/pkg/infra/httpclient/httpclientprovider/http_client_provider.go +++ b/pkg/infra/httpclient/httpclientprovider/http_client_provider.go @@ -4,7 +4,9 @@ import ( "net/http" "time" + "github.com/grafana/grafana-aws-sdk/pkg/awsds" awssdk "github.com/grafana/grafana-aws-sdk/pkg/sigv4" + "github.com/grafana/grafana-plugin-sdk-go/backend/gtime" sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" "github.com/mwitkow/go-conntrack" @@ -42,7 +44,21 @@ func New(cfg *setting.Cfg, validator validations.PluginRequestValidator, tracer // SigV4 signing should be performed after all headers are added if cfg.SigV4AuthEnabled { - middlewares = append(middlewares, awssdk.SigV4Middleware(cfg.SigV4VerboseLogging)) + authSettings := awsds.AuthSettings{ + AllowedAuthProviders: cfg.AWSAllowedAuthProviders, + AssumeRoleEnabled: cfg.AWSAssumeRoleEnabled, + ExternalID: cfg.AWSExternalId, + ListMetricsPageLimit: cfg.AWSListMetricsPageLimit, + SecureSocksDSProxyEnabled: cfg.SecureSocksDSProxy.Enabled, + } + if cfg.AWSSessionDuration != "" { + sessionDuration, err := gtime.ParseDuration(cfg.AWSSessionDuration) + if err == nil { + authSettings.SessionDuration = &sessionDuration + } + } + + middlewares = append(middlewares, awssdk.SigV4MiddlewareWithAuthSettings(cfg.SigV4VerboseLogging, authSettings)) } setDefaultTimeoutOptions(cfg) diff --git a/pkg/registry/apis/datasource/middleware.go b/pkg/registry/apis/datasource/middleware.go index 95d8082320a..b0a4bbc312a 100644 --- a/pkg/registry/apis/datasource/middleware.go +++ b/pkg/registry/apis/datasource/middleware.go @@ -16,7 +16,8 @@ func contextualMiddlewares(ctx context.Context) context.Context { sigv4Settings := awsds.ReadSigV4Settings(ctx) if sigv4Settings.Enabled { - ctx = httpclient.WithContextualMiddleware(ctx, sigv4.SigV4Middleware(sigv4Settings.VerboseLogging)) + authSettings, _ := awsds.ReadAuthSettingsFromContext(ctx) + ctx = httpclient.WithContextualMiddleware(ctx, sigv4.SigV4MiddlewareWithAuthSettings(sigv4Settings.VerboseLogging, *authSettings)) } return ctx diff --git a/pkg/tsdb/cloudwatch/cloudwatch.go b/pkg/tsdb/cloudwatch/cloudwatch.go index 11a83b05572..9772e11de9d 100644 --- a/pkg/tsdb/cloudwatch/cloudwatch.go +++ b/pkg/tsdb/cloudwatch/cloudwatch.go @@ -80,7 +80,7 @@ type CloudWatchService struct { } type SessionCache interface { - GetSession(c awsds.SessionConfig) (*session.Session, error) + GetSessionWithAuthSettings(c awsds.GetSessionConfig, as awsds.AuthSettings) (*session.Session, error) } func newExecutor(im instancemgmt.InstanceManager, logger log.Logger) *cloudWatchExecutor { @@ -299,7 +299,7 @@ func (ds *DataSource) newSession(region string) (*session.Session, error) { } region = ds.Settings.Region } - sess, err := ds.sessions.GetSession(awsds.SessionConfig{ + sess, err := ds.sessions.GetSessionWithAuthSettings(awsds.GetSessionConfig{ // https://github.com/grafana/grafana/issues/46365 // HTTPClient: instance.HTTPClient, Settings: awsds.AWSDatasourceSettings{ @@ -313,9 +313,8 @@ func (ds *DataSource) newSession(region string) (*session.Session, error) { AccessKey: ds.Settings.AccessKey, SecretKey: ds.Settings.SecretKey, }, - UserAgentName: aws.String("Cloudwatch"), - AuthSettings: &ds.Settings.GrafanaSettings, - }) + UserAgentName: aws.String("Cloudwatch")}, + ds.Settings.GrafanaSettings) if err != nil { return nil, err } diff --git a/pkg/tsdb/cloudwatch/cloudwatch_test.go b/pkg/tsdb/cloudwatch/cloudwatch_test.go index c2287c279c5..5e36adcda4c 100644 --- a/pkg/tsdb/cloudwatch/cloudwatch_test.go +++ b/pkg/tsdb/cloudwatch/cloudwatch_test.go @@ -191,7 +191,7 @@ func Test_CheckHealth(t *testing.T) { im := datasource.NewInstanceManager(func(ctx context.Context, s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { return DataSource{ Settings: models.CloudWatchSettings{AWSDatasourceSettings: awsds.AWSDatasourceSettings{Region: "us-east-1"}}, - sessions: &fakeSessionCache{getSession: func(c awsds.SessionConfig) (*session.Session, error) { + sessions: &fakeSessionCache{getSessionWithAuthSettings: func(c awsds.GetSessionConfig, a awsds.AuthSettings) (*session.Session, error) { return nil, fmt.Errorf("some sessions error") }}, }, nil @@ -229,9 +229,8 @@ func TestNewSession_passes_authSettings(t *testing.T) { }, GrafanaSettings: expectedSettings, }, - sessions: &fakeSessionCache{getSession: func(c awsds.SessionConfig) (*session.Session, error) { - assert.NotNil(t, c.AuthSettings) - assert.Equal(t, expectedSettings, *c.AuthSettings) + sessions: &fakeSessionCache{getSessionWithAuthSettings: func(c awsds.GetSessionConfig, a awsds.AuthSettings) (*session.Session, error) { + assert.Equal(t, expectedSettings, a) return &session.Session{ Config: &aws.Config{}, }, nil diff --git a/pkg/tsdb/cloudwatch/models/settings.go b/pkg/tsdb/cloudwatch/models/settings.go index 9221fe42fb4..4941684917d 100644 --- a/pkg/tsdb/cloudwatch/models/settings.go +++ b/pkg/tsdb/cloudwatch/models/settings.go @@ -43,7 +43,8 @@ func LoadCloudWatchSettings(ctx context.Context, config backend.DataSourceInstan instance.LogsTimeout = Duration{30 * time.Minute} } - instance.GrafanaSettings = *awsds.ReadAuthSettings(ctx) + authSettings, _ := awsds.ReadAuthSettingsFromContext(ctx) + instance.GrafanaSettings = *authSettings return instance, nil } diff --git a/pkg/tsdb/cloudwatch/test_utils.go b/pkg/tsdb/cloudwatch/test_utils.go index 9e7f531e194..5d56ee96222 100644 --- a/pkg/tsdb/cloudwatch/test_utils.go +++ b/pkg/tsdb/cloudwatch/test_utils.go @@ -224,21 +224,21 @@ type mockSessionCache struct { mock.Mock } -func (c *mockSessionCache) GetSession(config awsds.SessionConfig) (*session.Session, error) { +func (c *mockSessionCache) GetSessionWithAuthSettings(config awsds.GetSessionConfig, auth awsds.AuthSettings) (*session.Session, error) { args := c.Called(config) return args.Get(0).(*session.Session), args.Error(1) } type fakeSessionCache struct { - getSession func(c awsds.SessionConfig) (*session.Session, error) - calledRegions []string + getSessionWithAuthSettings func(c awsds.GetSessionConfig, a awsds.AuthSettings) (*session.Session, error) + calledRegions []string } -func (s *fakeSessionCache) GetSession(c awsds.SessionConfig) (*session.Session, error) { +func (s *fakeSessionCache) GetSessionWithAuthSettings(c awsds.GetSessionConfig, a awsds.AuthSettings) (*session.Session, error) { s.calledRegions = append(s.calledRegions, c.Settings.Region) - if s.getSession != nil { - return s.getSession(c) + if s.getSessionWithAuthSettings != nil { + return s.getSessionWithAuthSettings(c, a) } return &session.Session{ Config: &aws.Config{}, diff --git a/pkg/tsdb/cloudwatch/time_series_query_test.go b/pkg/tsdb/cloudwatch/time_series_query_test.go index d1a1d13b47c..dcc6ba0cfb1 100644 --- a/pkg/tsdb/cloudwatch/time_series_query_test.go +++ b/pkg/tsdb/cloudwatch/time_series_query_test.go @@ -147,10 +147,12 @@ func Test_executeTimeSeriesQuery_getCWClient_is_called_once_per_region_and_GetMe return &mockMetricClient } - t.Run("Queries with the same region should call GetSession with that region 1 time and call GetMetricDataWithContext 1 time", func(t *testing.T) { + t.Run("Queries with the same region should call GetSessionWithAuthSettings with that region 1 time and call GetMetricDataWithContext 1 time", func(t *testing.T) { mockSessionCache := &mockSessionCache{} - mockSessionCache.On("GetSession", mock.MatchedBy( - func(config awsds.SessionConfig) bool { return config.Settings.Region == "us-east-1" })). // region from queries is asserted here + mockSessionCache.On("GetSessionWithAuthSettings", mock.MatchedBy( + func(config awsds.GetSessionConfig) bool { + return config.Settings.Region == "us-east-1" + })). // region from queries is asserted here Return(&session.Session{Config: &aws.Config{}}, nil).Once() im := datasource.NewInstanceManager(func(ctx context.Context, s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { return DataSource{Settings: models.CloudWatchSettings{}, sessions: mockSessionCache}, nil @@ -198,14 +200,18 @@ func Test_executeTimeSeriesQuery_getCWClient_is_called_once_per_region_and_GetMe // GetMetricData is asserted to have been called 1 time for the 1 region present in the queries }) - t.Run("3 queries with 2 regions calls GetSession 2 times and calls GetMetricDataWithContext 2 times", func(t *testing.T) { + t.Run("3 queries with 2 regions calls GetSessionWithAuthSettings 2 times and calls GetMetricDataWithContext 2 times", func(t *testing.T) { sessionCache := &mockSessionCache{} - sessionCache.On("GetSession", mock.MatchedBy( - func(config awsds.SessionConfig) bool { return config.Settings.Region == "us-east-1" })). - Return(&session.Session{Config: &aws.Config{}}, nil, nil).Once() - sessionCache.On("GetSession", mock.MatchedBy( - func(config awsds.SessionConfig) bool { return config.Settings.Region == "us-east-2" })). - Return(&session.Session{Config: &aws.Config{}}, nil, nil).Once() + sessionCache.On("GetSessionWithAuthSettings", mock.MatchedBy( + func(config awsds.GetSessionConfig) bool { + return config.Settings.Region == "us-east-1" + })). + Return(&session.Session{Config: &aws.Config{}}, nil).Once() + sessionCache.On("GetSessionWithAuthSettings", mock.MatchedBy( + func(config awsds.GetSessionConfig) bool { + return config.Settings.Region == "us-east-2" + })). + Return(&session.Session{Config: &aws.Config{}}, nil).Once() im := datasource.NewInstanceManager(func(ctx context.Context, s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { return DataSource{Settings: models.CloudWatchSettings{}, sessions: sessionCache}, nil