Skip to content

Commit

Permalink
Pass region to sts client
Browse files Browse the repository at this point in the history
First, this change explicitly sets the region from metadata.
Second, it uses .WithSTSRegionalEndpoint(endpoints.RegionalSTSEndpoint)
to enable the regional STS endpoint. This is required because, as of
SDK v1.42.23, the default is legacy (endpoints.LegacySTSEndpoint).

Signed-off-by: Nick Turner <[email protected]>
Signed-off-by: Rasita Pai <[email protected]>
Signed-off-by: Davanum Srinivas <[email protected]>
  • Loading branch information
dims committed Oct 16, 2022
1 parent af76f3b commit 715f2af
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion staging/src/k8s.io/legacy-cloud-providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,8 +1190,12 @@ func init() {
return nil, fmt.Errorf("unable to validate custom endpoint overrides: %v", err)
}

regionName, err := getRegionFromMetadata(cfg)
if err != nil {
return nil, err
}
sess, err := session.NewSessionWithOptions(session.Options{
Config: aws.Config{},
Config: *aws.NewConfig().WithRegion(regionName).WithSTSRegionalEndpoint(endpoints.RegionalSTSEndpoint),
SharedConfigState: session.SharedConfigEnable,
})
if err != nil {
Expand All @@ -1218,6 +1222,32 @@ func init() {
})
}

func getRegionFromMetadata(cfg *CloudConfig) (string, error) {
klog.Infof("Get AWS region from metadata client")

metadata, err := newAWSSDKProvider(nil, cfg).Metadata()
if err != nil {
return "", fmt.Errorf("error creating AWS metadata client: %q", err)
}

err = updateConfigZone(cfg, metadata)
if err != nil {
return "", fmt.Errorf("unable to determine AWS zone from cloud provider config or EC2 instance metadata: %v", err)
}

zone := cfg.Global.Zone
if len(zone) <= 1 {
return "", fmt.Errorf("invalid AWS zone in config file: %s", zone)
}

regionName, err := azToRegion(zone)
if err != nil {
return "", err
}

return regionName, nil
}

// readAWSCloudConfig reads an instance of AWSCloudConfig from config reader.
func readAWSCloudConfig(config io.Reader) (*CloudConfig, error) {
var cfg CloudConfig
Expand Down

0 comments on commit 715f2af

Please sign in to comment.