Skip to content

Commit 47015d1

Browse files
authored
Add support for multitenant temporary credentials to v1 path (#231)
1 parent 5d6526b commit 47015d1

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## 0.38.0
6+
7+
- Add support for multitenant temporary credentials to v1 path by @iwysiu in [#231](https://github.com/grafana/grafana-aws-sdk/pull/231)
8+
59
## 0.37.0
10+
611
- Fix: clone default transport instead of using it for PDC by @njvrzm in [#229](https://github.com/grafana/grafana-aws-sdk/pull/229)
712
- Fix paths for multitenant [#228](https://github.com/grafana/grafana-aws-sdk/pull/228)
813

914
## 0.36.0
15+
1016
- Add dimensions to msk connect and pipe metric namespaces by @rrhodes in [#223](https://github.com/grafana/grafana-aws-sdk/pull/223)
1117
- Fix: Use DefaultClient in awsauth if given nil HTTPClient by @njvrzm in [#226](https://github.com/grafana/grafana-aws-sdk/pull/226)
1218

pkg/awsds/authSettings.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ const (
3434
// SigV4VerboseLoggingEnvVarKeyName is the string literal for the sigv4 verbose logging environment variable key name
3535
SigV4VerboseLoggingEnvVarKeyName = "AWS_SIGV4_VERBOSE_LOGGING"
3636

37-
defaultAssumeRoleEnabled = true
38-
defaultListMetricsPageLimit = 500
39-
defaultSecureSocksDSProxyEnabled = false
37+
// FlagMultiTenantTempCredentials is the flag for whether temporary credentials is running multitenant
38+
FlagMultiTenantTempCredentials = "multiTenantTempCredentials"
39+
40+
defaultAssumeRoleEnabled = true
41+
defaultListMetricsPageLimit = 500
42+
defaultSecureSocksDSProxyEnabled = false
43+
defaultMultiTenantTempCredentials = false
4044
)
4145

4246
// ReadAuthSettings gets the Grafana auth settings from the context if its available, the environment variables if not
@@ -52,11 +56,12 @@ func ReadAuthSettings(ctx context.Context) *AuthSettings {
5256

5357
func defaultAuthSettings() *AuthSettings {
5458
return &AuthSettings{
55-
AllowedAuthProviders: []string{"default", "keys", "credentials"},
56-
AssumeRoleEnabled: defaultAssumeRoleEnabled,
57-
SessionDuration: &stscreds.DefaultDuration,
58-
ListMetricsPageLimit: defaultListMetricsPageLimit,
59-
SecureSocksDSProxyEnabled: defaultSecureSocksDSProxyEnabled,
59+
AllowedAuthProviders: []string{"default", "keys", "credentials"},
60+
AssumeRoleEnabled: defaultAssumeRoleEnabled,
61+
SessionDuration: &stscreds.DefaultDuration,
62+
ListMetricsPageLimit: defaultListMetricsPageLimit,
63+
MultiTenantTempCredentials: defaultMultiTenantTempCredentials,
64+
SecureSocksDSProxyEnabled: defaultSecureSocksDSProxyEnabled,
6065
}
6166
}
6267

@@ -128,6 +133,7 @@ func ReadAuthSettingsFromContext(ctx context.Context) (*AuthSettings, bool) {
128133
hasSettings = true
129134
}
130135

136+
settings.MultiTenantTempCredentials = cfg.FeatureToggles().IsEnabled(FlagMultiTenantTempCredentials)
131137
return settings, hasSettings
132138
}
133139

pkg/awsds/sessions.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/sha256"
66
"fmt"
77
"net/http"
8+
"os"
89
"strings"
910
"sync"
1011
"time"
@@ -24,6 +25,12 @@ import (
2425
awsV2 "github.com/aws/aws-sdk-go-v2/aws"
2526
)
2627

28+
const (
29+
// awsTempCredsAccessKey and awsTempCredsSecretKey are the files containing the
30+
awsTempCredsAccessKey = "/tmp/aws.credentials/access-key-id"
31+
awsTempCredsSecretKey = "/tmp/aws.credentials/secret-access-key"
32+
)
33+
2734
type envelope struct {
2835
session *session.Session
2936
expiration time.Time
@@ -220,9 +227,23 @@ func (sc *SessionCache) GetSession(c SessionConfig) (*session.Session, error) {
220227
cfgs = append(cfgs, &aws.Config{Credentials: newRemoteCredentials(sess)})
221228
case AuthTypeGrafanaAssumeRole:
222229
backend.Logger.Debug("Authenticating towards AWS with Grafana Assume Role", "region", c.Settings.Region)
223-
cfgs = append(cfgs, &aws.Config{
224-
Credentials: credentials.NewSharedCredentials(CredentialsPath, ProfileName),
225-
})
230+
if c.AuthSettings.MultiTenantTempCredentials {
231+
accessKey, err := os.ReadFile(awsTempCredsAccessKey)
232+
if err != nil {
233+
return nil, err
234+
}
235+
secretKey, err := os.ReadFile(awsTempCredsSecretKey)
236+
if err != nil {
237+
return nil, err
238+
}
239+
cfgs = append(cfgs, &aws.Config{
240+
Credentials: credentials.NewStaticCredentials(string(accessKey), string(secretKey), ""),
241+
})
242+
} else {
243+
cfgs = append(cfgs, &aws.Config{
244+
Credentials: credentials.NewSharedCredentials(CredentialsPath, ProfileName),
245+
})
246+
}
226247
default:
227248
return nil, fmt.Errorf("unrecognized authType: %d", c.Settings.AuthType)
228249
}

pkg/awsds/types.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ type AmazonSessionProvider func(region string, s AWSDatasourceSettings) (*sessio
1919

2020
// AuthSettings stores the AWS settings from Grafana
2121
type AuthSettings struct {
22-
AllowedAuthProviders []string
23-
AssumeRoleEnabled bool
24-
SessionDuration *time.Duration
25-
ExternalID string
26-
ListMetricsPageLimit int
22+
AllowedAuthProviders []string
23+
AssumeRoleEnabled bool
24+
SessionDuration *time.Duration
25+
ExternalID string
26+
ListMetricsPageLimit int
27+
MultiTenantTempCredentials bool
2728

2829
// necessary for a work around until https://github.com/grafana/grafana/issues/39089 is implemented
2930
SecureSocksDSProxyEnabled bool
@@ -54,7 +55,7 @@ const (
5455
QueryFailedUser
5556
)
5657

57-
// QueryExecutionError error type can be returned from datasource's Execute or QueryStatus methods
58+
// QueryExecutionError error type can be returned from datasource's Execute or QueryStatus methods
5859
// this will mark the downstream cause in errorResponse.Status
5960
type QueryExecutionError struct {
6061
Err error

0 commit comments

Comments
 (0)