Skip to content

Commit ad36db7

Browse files
authored
Add support for multi-tenant-temp-creds (#213)
1 parent ca92f4d commit ad36db7

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

pkg/awsauth/auth.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package awsauth
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/aws/aws-sdk-go-v2/aws"
78
"github.com/grafana/grafana-plugin-sdk-go/backend"
89
"strings"
@@ -44,8 +45,10 @@ func (rcp *awsConfigProvider) GetConfig(ctx context.Context, authSettings Settin
4445
case AuthTypeDefault: // nothing else to do here
4546
case AuthTypeKeys:
4647
options = append(options, authSettings.WithStaticCredentials(rcp.client))
47-
case AuthTypeSharedCreds, AuthTypeGrafanaAssumeRole:
48+
case AuthTypeSharedCreds:
4849
options = append(options, authSettings.WithSharedCredentials())
50+
case AuthTypeGrafanaAssumeRole:
51+
options = append(options, authSettings.WithGrafanaAssumeRole(ctx, rcp.client))
4952
case AuthTypeEC2IAMRole:
5053
// TODO: test this
5154
options = append(options, authSettings.WithEC2RoleCredentials(rcp.client))

pkg/awsauth/settings.go

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package awsauth
22

33
import (
4+
"context"
45
"fmt"
5-
"github.com/aws/aws-sdk-go-v2/aws/middleware"
66
"hash/fnv"
77
"net/http"
88
"os"
99
"runtime"
1010
"strconv"
1111
"strings"
1212

13+
"github.com/aws/aws-sdk-go-v2/aws/middleware"
14+
1315
"github.com/aws/aws-sdk-go-v2/aws"
1416
"github.com/aws/aws-sdk-go-v2/config"
1517
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
@@ -20,6 +22,12 @@ import (
2022
"github.com/grafana/grafana-plugin-sdk-go/build"
2123
)
2224

25+
const (
26+
// awsTempCredsAccessKey and awsTempCredsSecretKey are the files containing the
27+
awsTempCredsAccessKey = "~/tmp/aws.credentials/access-key-id"
28+
awsTempCredsSecretKey = "~/tmp/aws.credentials/secret-access-key"
29+
)
30+
2331
// Settings carries configuration for authenticating with AWS
2432
type Settings struct {
2533
AuthType AuthType
@@ -104,12 +112,39 @@ func (s Settings) WithStaticCredentials(client AWSAPIClient) LoadOptionsFunc {
104112

105113
// WithSharedCredentials returns a LoadOptionsFunc to initialize config from a credentials file
106114
func (s Settings) WithSharedCredentials() LoadOptionsFunc {
107-
profile := s.CredentialsProfile
108-
if s.GetAuthType() == AuthTypeGrafanaAssumeRole {
109-
profile = "assume_role_credentials"
115+
return func(options *config.LoadOptions) error {
116+
options.SharedConfigProfile = s.CredentialsProfile
117+
if s.CredentialsPath != "" {
118+
options.SharedCredentialsFiles = []string{s.CredentialsPath}
119+
}
120+
return nil
110121
}
122+
}
123+
124+
// WithGrafanaAssumeRole returns a LoadOptionsFunc to initialize config for Grafana Assume Role
125+
func (s Settings) WithGrafanaAssumeRole(ctx context.Context, client AWSAPIClient) LoadOptionsFunc {
126+
if IsEnabled(ctx, FlagMultiTenantTempCredentials) {
127+
accessKey, err := os.ReadFile(awsTempCredsAccessKey)
128+
if err != nil {
129+
return func(opts *config.LoadOptions) error {
130+
return err
131+
}
132+
}
133+
secretKey, err := os.ReadFile(awsTempCredsSecretKey)
134+
if err != nil {
135+
return func(opts *config.LoadOptions) error {
136+
return err
137+
}
138+
}
139+
return func(opts *config.LoadOptions) error {
140+
opts.Credentials = client.NewStaticCredentialsProvider(string(accessKey), string(secretKey), "")
141+
return nil
142+
}
143+
}
144+
145+
// if it is running in single tenant use the credentials file
111146
return func(options *config.LoadOptions) error {
112-
options.SharedConfigProfile = profile
147+
options.SharedConfigProfile = awsds.ProfileName
113148
if s.CredentialsPath != "" {
114149
options.SharedCredentialsFiles = []string{s.CredentialsPath}
115150
}

pkg/awsauth/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package awsauth
2+
3+
import (
4+
"context"
5+
6+
"github.com/grafana/grafana-plugin-sdk-go/backend"
7+
)
8+
9+
const (
10+
FlagMultiTenantTempCredentials = "multiTenantTempCredentials"
11+
)
12+
13+
func IsEnabled(ctx context.Context, feature string) bool {
14+
return backend.GrafanaConfigFromContext(ctx).FeatureToggles().IsEnabled(feature)
15+
}

0 commit comments

Comments
 (0)