Skip to content

Commit

Permalink
Merge pull request #3980 from timj-hh/fe_creds
Browse files Browse the repository at this point in the history
Move remaining credentials module to shared library
  • Loading branch information
timj-hh authored Oct 24, 2023
2 parents 46e21e6 + f58f228 commit 06931a5
Show file tree
Hide file tree
Showing 36 changed files with 1,893 additions and 17 deletions.
2 changes: 1 addition & 1 deletion agent/api/ecsclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (

"github.com/aws/amazon-ecs-agent/agent/api"
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/credentials/instancecreds"
"github.com/aws/amazon-ecs-agent/agent/ec2"
"github.com/aws/amazon-ecs-agent/agent/utils"
agentversion "github.com/aws/amazon-ecs-agent/agent/version"
apicontainerstatus "github.com/aws/amazon-ecs-agent/ecs-agent/api/container/status"
apierrors "github.com/aws/amazon-ecs-agent/ecs-agent/api/errors"
"github.com/aws/amazon-ecs-agent/ecs-agent/async"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials/instancecreds"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/ecs-agent/httpclient"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
Expand Down
2 changes: 1 addition & 1 deletion agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/aws/amazon-ecs-agent/agent/app/factory"
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/containermetadata"
"github.com/aws/amazon-ecs-agent/agent/credentials/instancecreds"
"github.com/aws/amazon-ecs-agent/agent/data"
"github.com/aws/amazon-ecs-agent/agent/dockerclient"
"github.com/aws/amazon-ecs-agent/agent/dockerclient/dockerapi"
Expand Down Expand Up @@ -60,6 +59,7 @@ import (
"github.com/aws/amazon-ecs-agent/ecs-agent/acs/session"
apierrors "github.com/aws/amazon-ecs-agent/ecs-agent/api/errors"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials/instancecreds"
"github.com/aws/amazon-ecs-agent/ecs-agent/doctor"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
"github.com/aws/amazon-ecs-agent/ecs-agent/eventstream"
Expand Down
2 changes: 1 addition & 1 deletion agent/ec2/ec2_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package ec2
import (
"strings"

"github.com/aws/amazon-ecs-agent/agent/credentials/instancecreds"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials/instancecreds"
"github.com/aws/amazon-ecs-agent/ecs-agent/ecs_client/model/ecs"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down
2 changes: 1 addition & 1 deletion agent/ec2/ec2_metadata_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"strings"
"time"

"github.com/aws/amazon-ecs-agent/agent/credentials/instancecreds"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials/instancecreds"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
Expand Down
2 changes: 1 addition & 1 deletion agent/ecr/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (

apicontainer "github.com/aws/amazon-ecs-agent/agent/api/container"
"github.com/aws/amazon-ecs-agent/agent/config"
"github.com/aws/amazon-ecs-agent/agent/credentials/instancecreds"
ecrapi "github.com/aws/amazon-ecs-agent/agent/ecr/model/ecr"
agentversion "github.com/aws/amazon-ecs-agent/agent/version"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials/instancecreds"
"github.com/aws/amazon-ecs-agent/ecs-agent/httpclient"
"github.com/aws/aws-sdk-go/aws"
awscreds "github.com/aws/aws-sdk-go/aws/credentials"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions agent/vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ github.com/aws/amazon-ecs-agent/ecs-agent/async
github.com/aws/amazon-ecs-agent/ecs-agent/async/mocks
github.com/aws/amazon-ecs-agent/ecs-agent/config
github.com/aws/amazon-ecs-agent/ecs-agent/credentials
github.com/aws/amazon-ecs-agent/ecs-agent/credentials/instancecreds
github.com/aws/amazon-ecs-agent/ecs-agent/credentials/mocks
github.com/aws/amazon-ecs-agent/ecs-agent/credentials/providers
github.com/aws/amazon-ecs-agent/ecs-agent/csiclient
github.com/aws/amazon-ecs-agent/ecs-agent/csiclient/mocks
github.com/aws/amazon-ecs-agent/ecs-agent/data
Expand Down
25 changes: 25 additions & 0 deletions ecs-agent/credentials/instancecreds/instancecreds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 (
"sync"

"github.com/aws/aws-sdk-go/aws/credentials"
)

var (
credentialChain *credentials.Credentials
mu sync.Mutex
)
55 changes: 55 additions & 0 deletions ecs-agent/credentials/instancecreds/instancecreds_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//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 (
"fmt"

"github.com/aws/amazon-ecs-agent/ecs-agent/credentials/providers"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
)

// 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 {
logger.Error(fmt.Sprintf("Error getting ECS instance credentials from default chain: %s", err))
} else {
logger.Info(fmt.Sprintf("Successfully got ECS instance credentials from provider: %s", v.ProviderName))
}
return credentialChain
}
32 changes: 32 additions & 0 deletions ecs-agent/credentials/instancecreds/instancecreds_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build !linux && !windows
// +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
}
72 changes: 72 additions & 0 deletions ecs-agent/credentials/instancecreds/instancecreds_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//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 (
"fmt"

"github.com/aws/amazon-ecs-agent/ecs-agent/credentials/providers"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/defaults"
)

// 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:]...)...)
} 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 {
logger.Error(fmt.Sprintf("Error getting ECS instance credentials from default chain: %s", err))
} else {
logger.Info(fmt.Sprintf("Successfully got ECS instance credentials from provider: %s", v.ProviderName))
}
return credentialChain
}
22 changes: 22 additions & 0 deletions ecs-agent/credentials/providers/credentials_filename_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//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 providers

const (
// defaultRotatingCredentialsFilename is the default location of the credentials file
// for RotatingSharedCredentialsProvider.
defaultRotatingCredentialsFilename = "/rotatingcreds/credentials"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//go:build !linux && !windows
// +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"
)
Loading

0 comments on commit 06931a5

Please sign in to comment.