11package awsauth
22
33import (
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
2432type 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
106114func (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 }
0 commit comments