Skip to content

Add aws-sdk-go-v2 credentials provider (session wrapper) #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.23.2

require (
github.com/aws/aws-sdk-go v1.55.5
github.com/aws/aws-sdk-go-v2/credentials v1.17.47
github.com/google/go-cmp v0.6.0
github.com/grafana/grafana-plugin-sdk-go v0.258.0
github.com/grafana/sqlds/v4 v4.1.3
Expand All @@ -17,6 +18,8 @@ require (
require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/apache/arrow/go/v15 v15.0.2 // indirect
github.com/aws/aws-sdk-go-v2 v1.32.6 // indirect
github.com/aws/smithy-go v1.22.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ github.com/apache/arrow/go/v15 v15.0.2 h1:60IliRbiyTWCWjERBCkO1W4Qun9svcYoZrSLcy
github.com/apache/arrow/go/v15 v15.0.2/go.mod h1:DGXsR3ajT524njufqf95822i+KTh+yea1jass9YXgjA=
github.com/aws/aws-sdk-go v1.55.5 h1:KKUZBfBoyqy5d3swXyiC7Q76ic40rYcbqH7qjh59kzU=
github.com/aws/aws-sdk-go v1.55.5/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/aws/aws-sdk-go-v2 v1.32.6 h1:7BokKRgRPuGmKkFMhEg/jSul+tB9VvXhcViILtfG8b4=
github.com/aws/aws-sdk-go-v2 v1.32.6/go.mod h1:P5WJBrYqqbWVaOxgH0X/FYYD47/nooaPOZPlQdmiN2U=
github.com/aws/aws-sdk-go-v2/credentials v1.17.47 h1:48bA+3/fCdi2yAwVt+3COvmatZ6jUDNkDTIsqDiMUdw=
github.com/aws/aws-sdk-go-v2/credentials v1.17.47/go.mod h1:+KdckOejLW3Ks3b0E3b5rHsr2f9yuORBum0WPnE5o5w=
github.com/aws/smithy-go v1.22.1 h1:/HPHZQ0g7f4eUeK6HKglFz8uwVfZKgoI25rb/J+dnro=
github.com/aws/smithy-go v1.22.1/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
Expand Down
32 changes: 32 additions & 0 deletions pkg/awsds/sessions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package awsds

import (
"context"
"crypto/sha256"
"fmt"
"net/http"
Expand All @@ -19,6 +20,8 @@ import (
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"

awsV2 "github.com/aws/aws-sdk-go-v2/aws"
)

type envelope struct {
Expand Down Expand Up @@ -345,3 +348,32 @@ func getSTSEndpoint(endpoint string) string {
}
return endpoint
}

// CredentialsProviderV2 provides a CredentialsProvider suitable for use with aws-sdk-go-v2,
// to be used while migrating datasources.
// Experimental: this works but is not thoroughly tested yet
func (sc *SessionCache) CredentialsProviderV2(ctx context.Context, cfg GetSessionConfig) (awsV2.CredentialsProvider, error) {
authSettings := ReadAuthSettings(ctx)
sess, err := sc.GetSessionWithAuthSettings(cfg, *authSettings)
if err != nil {
return nil, err
}
return &SessionCredentialsProvider{sess}, nil

}

type SessionCredentialsProvider struct {
session *session.Session
}

func (scp *SessionCredentialsProvider) Retrieve(_ context.Context) (awsV2.Credentials, error) {
creds := awsV2.Credentials{}
v1creds, err := scp.session.Config.Credentials.Get()
if err != nil {
return creds, err
}
creds.AccessKeyID = v1creds.AccessKeyID
creds.SecretAccessKey = v1creds.SecretAccessKey
creds.SessionToken = v1creds.SessionToken
return creds, nil
}