-
Notifications
You must be signed in to change notification settings - Fork 619
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
Fix credential rotation issue for ECS-A Windows #3184
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,52 @@ | ||
//go:build linux | ||
|
||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package instancecreds | ||
|
||
import ( | ||
"github.com/aws/amazon-ecs-agent/agent/credentials/providers" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/defaults" | ||
"github.com/cihub/seelog" | ||
) | ||
|
||
// GetCredentials returns the instance credentials chain. This is the default chain | ||
// credentials plus the "rotating shared credentials provider", so credentials will | ||
// be checked in this order: | ||
// 1. Env vars (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY). | ||
// 2. Shared credentials file (https://docs.aws.amazon.com/ses/latest/DeveloperGuide/create-shared-credentials-file.html) (file at ~/.aws/credentials containing access key id and secret access key). | ||
// 3. EC2 role credentials. This is an IAM role that the user specifies when they launch their EC2 container instance (ie ecsInstanceRole (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html)). | ||
// 4. Rotating shared credentials file located at /rotatingcreds/credentials | ||
func GetCredentials(isExternal bool) *credentials.Credentials { | ||
mu.Lock() | ||
if credentialChain == nil { | ||
credProviders := defaults.CredProviders(defaults.Config(), defaults.Handlers()) | ||
credProviders = append(credProviders, providers.NewRotatingSharedCredentialsProvider()) | ||
credentialChain = credentials.NewCredentials(&credentials.ChainProvider{ | ||
VerboseErrors: false, | ||
Providers: credProviders, | ||
}) | ||
} | ||
mu.Unlock() | ||
|
||
// credentials.Credentials is concurrency-safe, so lock not needed here | ||
v, err := credentialChain.Get() | ||
if err != nil { | ||
seelog.Errorf("Error getting ECS instance credentials from default chain: %s", err) | ||
} else { | ||
seelog.Infof("Successfully got ECS instance credentials from provider: %s", v.ProviderName) | ||
} | ||
return credentialChain | ||
} |
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
31 changes: 31 additions & 0 deletions
31
agent/credentials/instancecreds/instancecreds_unsupported.go
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,31 @@ | ||
//go:build !linux && !windows | ||
|
||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package instancecreds | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
) | ||
|
||
// GetCredentials returns the instance credentials chain. This is the default chain | ||
// credentials plus the "rotating shared credentials provider", so credentials will | ||
// be checked in this order: | ||
// 1. Env vars (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY). | ||
// 2. Shared credentials file (https://docs.aws.amazon.com/ses/latest/DeveloperGuide/create-shared-credentials-file.html) (file at ~/.aws/credentials containing access key id and secret access key). | ||
// 3. EC2 role credentials. This is an IAM role that the user specifies when they launch their EC2 container instance (ie ecsInstanceRole (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html)). | ||
// 4. Rotating shared credentials file located at /rotatingcreds/credentials | ||
func GetCredentials(isExternal bool) *credentials.Credentials { | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//go:build windows | ||
|
||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package instancecreds | ||
|
||
import ( | ||
"github.com/aws/amazon-ecs-agent/agent/credentials/providers" | ||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/defaults" | ||
"github.com/cihub/seelog" | ||
) | ||
|
||
// GetCredentials returns the instance credentials chain. This is the default chain | ||
// credentials plus the "rotating shared credentials provider", so credentials will | ||
// be checked in this order: | ||
// 1. Env vars (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY). | ||
// 2. Shared credentials file (https://docs.aws.amazon.com/ses/latest/DeveloperGuide/create-shared-credentials-file.html) (file at ~/.aws/credentials containing access key id and secret access key). | ||
// 3. EC2 role credentials. This is an IAM role that the user specifies when they launch their EC2 container instance (ie ecsInstanceRole (https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html)). | ||
// 4. Rotating shared credentials file located at /rotatingcreds/credentials | ||
// | ||
// The default credential chain provided by the SDK includes: | ||
// * EnvProvider | ||
// * SharedCredentialsProvider | ||
// * RemoteCredProvider (EC2RoleProvider) | ||
// | ||
// In the case of ECS-A on Windows, the `SharedCredentialsProvider` takes | ||
// precedence over the `RotatingSharedCredentialsProvider` and this results | ||
// in the credentials not being refreshed. To mitigate this issue, we will | ||
// reorder the credential chain and ensure that `RotatingSharedCredentialsProvider` | ||
// takes precedence over the `SharedCredentialsProvider` for ECS-A. | ||
func GetCredentials(isExternal bool) *credentials.Credentials { | ||
mu.Lock() | ||
credProviders := defaults.CredProviders(defaults.Config(), defaults.Handlers()) | ||
if isExternal { | ||
credProviders = append(credProviders[:1], append([]credentials.Provider{providers.NewRotatingSharedCredentialsProvider()}, credProviders[1:]...)...) | ||
vsiddharth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
credProviders = append(credProviders, providers.NewRotatingSharedCredentialsProvider()) | ||
} | ||
credentialChain = credentials.NewCredentials(&credentials.ChainProvider{ | ||
VerboseErrors: false, | ||
Providers: credProviders, | ||
}) | ||
mu.Unlock() | ||
|
||
// credentials.Credentials is concurrency-safe, so lock not needed here | ||
v, err := credentialChain.Get() | ||
if err != nil { | ||
seelog.Errorf("Error getting ECS instance credentials from default chain: %s", err) | ||
} else { | ||
seelog.Infof("Successfully got ECS instance credentials from provider: %s", v.ProviderName) | ||
} | ||
return credentialChain | ||
} |
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,22 @@ | ||
//go:build linux | ||
vsiddharth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package providers | ||
|
||
const ( | ||
// defaultRotatingCredentialsFilename is the default location of the credentials file | ||
// for RotatingSharedCredentialsProvider. | ||
defaultRotatingCredentialsFilename = "/rotatingcreds/credentials" | ||
) |
22 changes: 22 additions & 0 deletions
22
agent/credentials/providers/credentials_filename_unsupported.go
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,22 @@ | ||
//go:build !linux && !windows | ||
|
||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package providers | ||
|
||
const ( | ||
// defaultRotatingCredentialsFilename is the default location of the credentials file | ||
// for RotatingSharedCredentialsProvider. | ||
defaultRotatingCredentialsFilename = "/unsupported/rotatingcreds/credentials" | ||
) |
20 changes: 20 additions & 0 deletions
20
agent/credentials/providers/credentials_filename_windows.go
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,20 @@ | ||
//go:build windows | ||
vsiddharth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
// not use this file except in compliance with the License. A copy of the | ||
// License is located at | ||
// | ||
// http://aws.amazon.com/apache2.0/ | ||
// | ||
// or in the "license" file accompanying this file. This file is distributed | ||
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
// express or implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package providers | ||
|
||
// defaultRotatingCredentialsFilename is the default location of the credentials file | ||
// for RotatingSharedCredentialsProvider. | ||
const defaultRotatingCredentialsFilename = "C:\\Windows\\System32\\config\\systemprofile\\.aws\\credentials" |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Linux mark this as
_
since its unused right?