-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate tsh common to aws sdk v2 (#49449)
* add AWS SDK v2 migration utils package * migrate tool/tsh/common to AWS SDK v2 * adapt AWSAccessMiddleware to AWS SDK v2
- Loading branch information
1 parent
02cadb4
commit 3208c3d
Showing
4 changed files
with
192 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package migration | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
awsv2 "github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/gravitational/trace" | ||
) | ||
|
||
// NewProviderAdapter returns a [ProviderAdapter] that can be used as an AWS SDK | ||
// v1 credentials provider. | ||
func NewProviderAdapter(providerV2 awsv2.CredentialsProvider) *ProviderAdapter { | ||
return &ProviderAdapter{ | ||
providerV2: providerV2, | ||
} | ||
} | ||
|
||
var _ credentials.ProviderWithContext = (*ProviderAdapter)(nil) | ||
|
||
// ProviderAdapter adapts an [awsv2.CredentialsProvider] to an AWS SDK v1 | ||
// credentials provider. | ||
type ProviderAdapter struct { | ||
providerV2 awsv2.CredentialsProvider | ||
|
||
m sync.RWMutex | ||
// creds are retrieved and saved to satisfy IsExpired. | ||
creds awsv2.Credentials | ||
} | ||
|
||
func (a *ProviderAdapter) IsExpired() bool { | ||
a.m.RLock() | ||
defer a.m.RUnlock() | ||
|
||
var emptyCreds awsv2.Credentials | ||
return a.creds == emptyCreds || a.creds.Expired() | ||
} | ||
|
||
func (a *ProviderAdapter) Retrieve() (credentials.Value, error) { | ||
return a.RetrieveWithContext(context.Background()) | ||
} | ||
|
||
func (a *ProviderAdapter) RetrieveWithContext(ctx context.Context) (credentials.Value, error) { | ||
creds, err := a.retrieveLocked(ctx) | ||
if err != nil { | ||
return credentials.Value{}, trace.Wrap(err) | ||
} | ||
|
||
return credentials.Value{ | ||
AccessKeyID: creds.AccessKeyID, | ||
SecretAccessKey: creds.SecretAccessKey, | ||
SessionToken: creds.SessionToken, | ||
ProviderName: creds.Source, | ||
}, nil | ||
} | ||
|
||
func (a *ProviderAdapter) retrieveLocked(ctx context.Context) (awsv2.Credentials, error) { | ||
a.m.Lock() | ||
defer a.m.Unlock() | ||
|
||
var emptyCreds awsv2.Credentials | ||
if a.creds != emptyCreds && !a.creds.Expired() { | ||
return a.creds, nil | ||
} | ||
|
||
creds, err := a.providerV2.Retrieve(ctx) | ||
if err != nil { | ||
return emptyCreds, trace.Wrap(err) | ||
} | ||
|
||
a.creds = creds | ||
return creds, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters