Skip to content

Commit 5212e8f

Browse files
committed
Fix credentials issue with ECS-A Windows
Credentials were not being rotated properly on ECS-A Windows instances. This patch addresses the issue by using the correct file-paths for credentials on supported platforms. The credential chain hierarchy is also updated on ECS-A windows to ensure that credential chain is not broken for other launch types. Signed-off-by: Siddharth Vinothkumar <[email protected]>
1 parent 5ece05b commit 5212e8f

6 files changed

+90
-5
lines changed

agent/credentials/instancecreds/instancecreds.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package instancecreds
1515

1616
import (
17+
"os"
1718
"sync"
1819

1920
"github.com/aws/amazon-ecs-agent/agent/credentials/providers"
@@ -38,7 +39,25 @@ func GetCredentials() *credentials.Credentials {
3839
mu.Lock()
3940
if credentialChain == nil {
4041
credProviders := defaults.CredProviders(defaults.Config(), defaults.Handlers())
41-
credProviders = append(credProviders, providers.NewRotatingSharedCredentialsProvider())
42+
/*
43+
The default credential chain provided by the SDK includes:
44+
* EnvProvider
45+
* SharedCredentialsProvider
46+
* RemoteCredProvider
47+
48+
In the case of ECS-A on Windows, the `SharedCredentialsProvider` takes
49+
precedence over the `RotatingSharedCredentialsProvider` and this results
50+
in the credentials not being refreshed. To mitigate this issue, we will
51+
use the environment variable `ECS_EXTERNAL` to reorder the credential
52+
chain and ensure that `RotatingSharedCredentialsProvider` takes precedence
53+
over the `SharedCredentialsProvider`.
54+
55+
*/
56+
if _, ok := os.LookupEnv("ECS_EXTERNAL"); ok {
57+
credProviders = append(credProviders[:1], append([]credentials.Provider{providers.NewRotatingSharedCredentialsProvider()}, credProviders[1:]...)...)
58+
} else {
59+
credProviders = append(credProviders, providers.NewRotatingSharedCredentialsProvider())
60+
}
4261
credentialChain = credentials.NewCredentials(&credentials.ChainProvider{
4362
VerboseErrors: false,
4463
Providers: credProviders,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build linux
2+
3+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
6+
// not use this file except in compliance with the License. A copy of the
7+
// License is located at
8+
//
9+
// http://aws.amazon.com/apache2.0/
10+
//
11+
// or in the "license" file accompanying this file. This file is distributed
12+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
// express or implied. See the License for the specific language governing
14+
// permissions and limitations under the License.
15+
16+
package providers
17+
18+
const (
19+
// defaultRotatingCredentialsFilename is the default location of the credentials file
20+
// for RotatingSharedCredentialsProvider.
21+
defaultRotatingCredentialsFilename = "/rotatingcreds/credentials"
22+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build !windows && !linux
2+
3+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
6+
// not use this file except in compliance with the License. A copy of the
7+
// License is located at
8+
//
9+
// http://aws.amazon.com/apache2.0/
10+
//
11+
// or in the "license" file accompanying this file. This file is distributed
12+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
// express or implied. See the License for the specific language governing
14+
// permissions and limitations under the License.
15+
16+
package providers
17+
18+
const (
19+
// defaultRotatingCredentialsFilename is the default location of the credentials file
20+
// for RotatingSharedCredentialsProvider.
21+
defaultRotatingCredentialsFilename = "/unsupported/file_path/file_name"
22+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build windows
2+
3+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
6+
// not use this file except in compliance with the License. A copy of the
7+
// License is located at
8+
//
9+
// http://aws.amazon.com/apache2.0/
10+
//
11+
// or in the "license" file accompanying this file. This file is distributed
12+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
// express or implied. See the License for the specific language governing
14+
// permissions and limitations under the License.
15+
16+
package providers
17+
18+
import "path/filepath"
19+
20+
var (
21+
// defaultRotatingCredentialsFilename is the default location of the credentials file
22+
// for RotatingSharedCredentialsProvider.
23+
defaultRotatingCredentialsFilename = filepath.Join("C:\\Windows\\System32\\config\\systemprofile\\.aws", "credentials")
24+
)

agent/credentials/providers/rotating_shared_credentials_provider.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
const (
2525
// defaultRotationInterval is how frequently to expire and re-retrieve the credentials from file.
2626
defaultRotationInterval = time.Minute
27-
// defaultFilename is the default location of the credentials file within the container.
28-
defaultFilename = "/rotatingcreds/credentials"
2927
// RotatingSharedCredentialsProviderName is the name of this provider
3028
RotatingSharedCredentialsProviderName = "RotatingSharedCredentialsProvider"
3129
)
@@ -46,7 +44,7 @@ func NewRotatingSharedCredentialsProvider() *RotatingSharedCredentialsProvider {
4644
return &RotatingSharedCredentialsProvider{
4745
RotationInterval: defaultRotationInterval,
4846
sharedCredentialsProvider: &credentials.SharedCredentialsProvider{
49-
Filename: defaultFilename,
47+
Filename: defaultRotatingCredentialsFilename,
5048
Profile: "default",
5149
},
5250
}

agent/credentials/providers/rotating_shared_credentials_provider_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestNewRotatingSharedCredentialsProvider(t *testing.T) {
2929
p := NewRotatingSharedCredentialsProvider()
3030
require.Equal(t, time.Minute, p.RotationInterval)
3131
require.Equal(t, "default", p.sharedCredentialsProvider.Profile)
32-
require.Equal(t, "/rotatingcreds/credentials", p.sharedCredentialsProvider.Filename)
32+
require.Equal(t, defaultRotatingCredentialsFilename, p.sharedCredentialsProvider.Filename)
3333
}
3434

3535
func TestRotatingSharedCredentialsProvider_RetrieveFail_BadPath(t *testing.T) {

0 commit comments

Comments
 (0)