mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-09 21:17:09 +08:00
vendor: github.com/aws/aws-sdk-go-v2/config v1.26.6
vendor github.com/aws/aws-sdk-go-v2/config v1.26.6 and related dependencies. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
105
vendor/github.com/aws/aws-sdk-go-v2/config/resolve_credentials.go
generated
vendored
105
vendor/github.com/aws/aws-sdk-go-v2/config/resolve_credentials.go
generated
vendored
@ -3,7 +3,10 @@ package config
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
@ -21,11 +24,33 @@ import (
|
||||
|
||||
const (
|
||||
// valid credential source values
|
||||
credSourceEc2Metadata = "Ec2InstanceMetadata"
|
||||
credSourceEnvironment = "Environment"
|
||||
credSourceECSContainer = "EcsContainer"
|
||||
credSourceEc2Metadata = "Ec2InstanceMetadata"
|
||||
credSourceEnvironment = "Environment"
|
||||
credSourceECSContainer = "EcsContainer"
|
||||
httpProviderAuthFileEnvVar = "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE"
|
||||
)
|
||||
|
||||
// direct representation of the IPv4 address for the ECS container
|
||||
// "169.254.170.2"
|
||||
var ecsContainerIPv4 net.IP = []byte{
|
||||
169, 254, 170, 2,
|
||||
}
|
||||
|
||||
// direct representation of the IPv4 address for the EKS container
|
||||
// "169.254.170.23"
|
||||
var eksContainerIPv4 net.IP = []byte{
|
||||
169, 254, 170, 23,
|
||||
}
|
||||
|
||||
// direct representation of the IPv6 address for the EKS container
|
||||
// "fd00:ec2::23"
|
||||
var eksContainerIPv6 net.IP = []byte{
|
||||
0xFD, 0, 0xE, 0xC2,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0x23,
|
||||
}
|
||||
|
||||
var (
|
||||
ecsContainerEndpoint = "http://169.254.170.2" // not constant to allow for swapping during unit-testing
|
||||
)
|
||||
@ -222,6 +247,36 @@ func processCredentials(ctx context.Context, cfg *aws.Config, sharedConfig *Shar
|
||||
return nil
|
||||
}
|
||||
|
||||
// isAllowedHost allows host to be loopback or known ECS/EKS container IPs
|
||||
//
|
||||
// host can either be an IP address OR an unresolved hostname - resolution will
|
||||
// be automatically performed in the latter case
|
||||
func isAllowedHost(host string) (bool, error) {
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
return isIPAllowed(ip), nil
|
||||
}
|
||||
|
||||
addrs, err := lookupHostFn(host)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
if ip := net.ParseIP(addr); ip == nil || !isIPAllowed(ip) {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func isIPAllowed(ip net.IP) bool {
|
||||
return ip.IsLoopback() ||
|
||||
ip.Equal(ecsContainerIPv4) ||
|
||||
ip.Equal(eksContainerIPv4) ||
|
||||
ip.Equal(eksContainerIPv6)
|
||||
}
|
||||
|
||||
func resolveLocalHTTPCredProvider(ctx context.Context, cfg *aws.Config, endpointURL, authToken string, configs configs) error {
|
||||
var resolveErr error
|
||||
|
||||
@ -232,10 +287,12 @@ func resolveLocalHTTPCredProvider(ctx context.Context, cfg *aws.Config, endpoint
|
||||
host := parsed.Hostname()
|
||||
if len(host) == 0 {
|
||||
resolveErr = fmt.Errorf("unable to parse host from local HTTP cred provider URL")
|
||||
} else if isLoopback, loopbackErr := isLoopbackHost(host); loopbackErr != nil {
|
||||
resolveErr = fmt.Errorf("failed to resolve host %q, %v", host, loopbackErr)
|
||||
} else if !isLoopback {
|
||||
resolveErr = fmt.Errorf("invalid endpoint host, %q, only loopback hosts are allowed", host)
|
||||
} else if parsed.Scheme == "http" {
|
||||
if isAllowedHost, allowHostErr := isAllowedHost(host); allowHostErr != nil {
|
||||
resolveErr = fmt.Errorf("failed to resolve host %q, %v", host, allowHostErr)
|
||||
} else if !isAllowedHost {
|
||||
resolveErr = fmt.Errorf("invalid endpoint host, %q, only loopback/ecs/eks hosts are allowed", host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -252,6 +309,16 @@ func resolveHTTPCredProvider(ctx context.Context, cfg *aws.Config, url, authToke
|
||||
if len(authToken) != 0 {
|
||||
options.AuthorizationToken = authToken
|
||||
}
|
||||
if authFilePath := os.Getenv(httpProviderAuthFileEnvVar); authFilePath != "" {
|
||||
options.AuthorizationTokenProvider = endpointcreds.TokenProviderFunc(func() (string, error) {
|
||||
var contents []byte
|
||||
var err error
|
||||
if contents, err = ioutil.ReadFile(authFilePath); err != nil {
|
||||
return "", fmt.Errorf("failed to read authorization token from %v: %v", authFilePath, err)
|
||||
}
|
||||
return string(contents), nil
|
||||
})
|
||||
}
|
||||
options.APIOptions = cfg.APIOptions
|
||||
if cfg.Retryer != nil {
|
||||
options.Retryer = cfg.Retryer()
|
||||
@ -384,10 +451,6 @@ func assumeWebIdentity(ctx context.Context, cfg *aws.Config, filepath string, ro
|
||||
return fmt.Errorf("token file path is not set")
|
||||
}
|
||||
|
||||
if len(roleARN) == 0 {
|
||||
return fmt.Errorf("role ARN is not set")
|
||||
}
|
||||
|
||||
optFns := []func(*stscreds.WebIdentityRoleOptions){
|
||||
func(options *stscreds.WebIdentityRoleOptions) {
|
||||
options.RoleSessionName = sessionName
|
||||
@ -398,11 +461,29 @@ func assumeWebIdentity(ctx context.Context, cfg *aws.Config, filepath string, ro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if found {
|
||||
optFns = append(optFns, optFn)
|
||||
}
|
||||
|
||||
provider := stscreds.NewWebIdentityRoleProvider(sts.NewFromConfig(*cfg), roleARN, stscreds.IdentityTokenFile(filepath), optFns...)
|
||||
opts := stscreds.WebIdentityRoleOptions{
|
||||
RoleARN: roleARN,
|
||||
}
|
||||
|
||||
for _, fn := range optFns {
|
||||
fn(&opts)
|
||||
}
|
||||
|
||||
if len(opts.RoleARN) == 0 {
|
||||
return fmt.Errorf("role ARN is not set")
|
||||
}
|
||||
|
||||
client := opts.Client
|
||||
if client == nil {
|
||||
client = sts.NewFromConfig(*cfg)
|
||||
}
|
||||
|
||||
provider := stscreds.NewWebIdentityRoleProvider(client, roleARN, stscreds.IdentityTokenFile(filepath), optFns...)
|
||||
|
||||
cfg.Credentials = provider
|
||||
|
||||
|
Reference in New Issue
Block a user