AWS: Update deprecated aws-sdk functions from env variable versions (#89643)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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{},
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user