|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + "github.com/turbot/steampipe-plugin-sdk/v6/plugin" |
| 11 | +) |
| 12 | + |
| 13 | +// connectionConfigCredentialsProvider is an aws.CredentialsProvider that reads |
| 14 | +// access_key / secret_key / session_token from the steampipe connection config |
| 15 | +// on every Retrieve() call (subject to the SDK's CredentialsCache, which we |
| 16 | +// hint at via a short Expires below). |
| 17 | +// |
| 18 | +// Background |
| 19 | +// |
| 20 | +// The steampipe plugin SDK mutates the connection config in place when a new |
| 21 | +// connection config arrives via UpdateConnectionConfigs (see |
| 22 | +// steampipe-plugin-sdk/plugin/plugin_connection_config.go upsertConnectionData, |
| 23 | +// which calls d.Connection.SetConfig(configStruct) under a write lock). The |
| 24 | +// SDK comment at that site explicitly acknowledges that a query may already be |
| 25 | +// executing with this Connection object, and that the AWS plugin in particular |
| 26 | +// "may refresh the Client using the previous credentials" — which is the bug |
| 27 | +// this provider fixes. |
| 28 | +// |
| 29 | +// A credentials.NewStaticCredentialsProvider built once at aws.Config |
| 30 | +// construction time captures the original token values. A goroutine holding |
| 31 | +// that aws.Config keeps signing requests with the original token regardless of |
| 32 | +// rotation. When the original token expires at AWS, every subsequent request |
| 33 | +// from that goroutine fails with ExpiredToken — even if a fresh valid token |
| 34 | +// has been delivered to Connection.Config by the SDK. |
| 35 | +// |
| 36 | +// By re-reading the connection config on every Retrieve(), in-flight goroutines |
| 37 | +// holding the same aws.Config pick up rotated credentials on the next signing |
| 38 | +// operation (modulo the CredentialsCache TTL we set below). |
| 39 | +type connectionConfigCredentialsProvider struct { |
| 40 | + connection *plugin.Connection |
| 41 | +} |
| 42 | + |
| 43 | +// credentialsExpiresInterval is how long the returned aws.Credentials are |
| 44 | +// considered fresh by the SDK's CredentialsCache. Overridable in tests. |
| 45 | +var credentialsExpiresInterval = 60 * time.Second |
| 46 | + |
| 47 | +// Retrieve implements aws.CredentialsProvider. Called by the AWS SDK on every |
| 48 | +// signed request, subject to the wrapping CredentialsCache. |
| 49 | +func (p *connectionConfigCredentialsProvider) Retrieve(_ context.Context) (creds aws.Credentials, err error) { |
| 50 | + // Belt-and-suspenders: the SDK's Connection.GetConfig acquires an RLock so |
| 51 | + // torn interface reads cannot happen via that path. This recover still |
| 52 | + // converts any other panic inside Retrieve into a clean error the AWS SDK |
| 53 | + // can retry, rather than propagating through the signing middleware. |
| 54 | + defer func() { |
| 55 | + if r := recover(); r != nil { |
| 56 | + err = fmt.Errorf("connectionConfigCredentialsProvider: panic during Retrieve for connection %q: %v", p.connectionName(), r) |
| 57 | + creds = aws.Credentials{} |
| 58 | + } |
| 59 | + }() |
| 60 | + |
| 61 | + if p.connection == nil { |
| 62 | + return aws.Credentials{}, errors.New("connectionConfigCredentialsProvider: connection is nil") |
| 63 | + } |
| 64 | + |
| 65 | + // Read the raw config through the SDK's Connection.GetConfig accessor |
| 66 | + // (which acquires the RLock) and type-assert directly here. Avoid the |
| 67 | + // local aws/connection_config.go GetConfig helper — it normalizes the |
| 68 | + // Regions slice in place and panics on regions = []. Neither belongs in |
| 69 | + // the AWS request signing path: Retrieve only needs the credential fields, |
| 70 | + // and a malformed connection config should not crash signing goroutines |
| 71 | + // deep inside the AWS SDK middleware. |
| 72 | + raw := p.connection.GetConfig() |
| 73 | + cfg, ok := raw.(awsConfig) |
| 74 | + if !ok { |
| 75 | + return aws.Credentials{}, fmt.Errorf("connectionConfigCredentialsProvider: connection %q config is %T, expected awsConfig", p.connection.Name, raw) |
| 76 | + } |
| 77 | + |
| 78 | + if cfg.AccessKey == nil { |
| 79 | + return aws.Credentials{}, fmt.Errorf("connectionConfigCredentialsProvider: connection %q has no access_key in config", p.connection.Name) |
| 80 | + } |
| 81 | + if cfg.SecretKey == nil { |
| 82 | + return aws.Credentials{}, fmt.Errorf("connectionConfigCredentialsProvider: connection %q has no secret_key in config", p.connection.Name) |
| 83 | + } |
| 84 | + |
| 85 | + var sessionToken string |
| 86 | + if cfg.SessionToken != nil { |
| 87 | + sessionToken = *cfg.SessionToken |
| 88 | + } |
| 89 | + |
| 90 | + return aws.Credentials{ |
| 91 | + AccessKeyID: *cfg.AccessKey, |
| 92 | + SecretAccessKey: *cfg.SecretKey, |
| 93 | + SessionToken: sessionToken, |
| 94 | + Source: "connectionConfigCredentialsProvider", |
| 95 | + // config.WithCredentialsProvider wraps any provider in a |
| 96 | + // CredentialsCache. The cache will NOT call Retrieve again until the |
| 97 | + // cached value's Expires has passed (or until cache invalidation), |
| 98 | + // so setting Expires too far out defeats the rotation-pickup goal: |
| 99 | + // the cache would hold the original creds in memory long after they |
| 100 | + // were rotated in Connection.Config. |
| 101 | + // |
| 102 | + // 60 seconds matches what the standalone reproduction harness |
| 103 | + // validated to keep rotation latency bounded. Reading |
| 104 | + // Connection.Config is an in-memory type assertion + struct copy, |
| 105 | + // so a short interval is essentially free. |
| 106 | + CanExpire: true, |
| 107 | + Expires: time.Now().Add(credentialsExpiresInterval), |
| 108 | + }, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (p *connectionConfigCredentialsProvider) connectionName() string { |
| 112 | + if p.connection == nil { |
| 113 | + return "<nil>" |
| 114 | + } |
| 115 | + return p.connection.Name |
| 116 | +} |
0 commit comments