@@ -6,6 +6,7 @@ package clients
66
77import (
88 "context"
9+ "k8s.io/apimachinery/pkg/runtime"
910 "reflect"
1011 "unsafe"
1112
@@ -23,7 +24,7 @@ import (
2324)
2425
2526const (
26- keyAccountId = "account_id"
27+ keyAccountID = "account_id"
2728)
2829
2930type SetupConfig struct {
@@ -34,15 +35,15 @@ func SelectTerraformSetup(config *SetupConfig) terraform.SetupFn { // nolint:goc
3435 return func (ctx context.Context , c client.Client , mg resource.Managed ) (terraform.Setup , error ) {
3536 pc := & v1beta1.ProviderConfig {}
3637 if err := c .Get (ctx , types.NamespacedName {Name : mg .GetProviderConfigReference ().Name }, pc ); err != nil {
37- return terraform.Setup {}, errors .Wrapf (err , "cannot get referenced Provider : %s " , mg .GetProviderConfigReference ().Name )
38+ return terraform.Setup {}, errors .Wrapf (err , "cannot get referenced ProviderConfig : %q " , mg .GetProviderConfigReference ().Name )
3839 }
3940 t := resource .NewProviderConfigUsageTracker (c , & v1beta1.ProviderConfigUsage {})
4041 if err := t .Track (ctx , mg ); err != nil {
41- return terraform.Setup {}, errors .Wrap (err , "cannot track ProviderConfig usage" )
42+ return terraform.Setup {}, errors .Wrapf (err , "cannot track ProviderConfig usage for %q" , mg . GetProviderConfigReference (). Name )
4243 }
4344
4445 ps := terraform.Setup {}
45- awsCfg , err := GetAWSConfigWithDefaultRegion (ctx , c , mg , pc )
46+ awsCfg , err := getAWSConfigWithDefaultRegion (ctx , c , mg , pc )
4647 if err != nil {
4748 return terraform.Setup {}, errors .Wrap (err , "cannot get aws config" )
4849 } else if awsCfg == nil {
@@ -61,12 +62,12 @@ func SelectTerraformSetup(config *SetupConfig) terraform.SetupFn { // nolint:goc
6162 }
6263 }
6364 ps .ClientMetadata = map [string ]string {
64- keyAccountId : account ,
65+ keyAccountID : account ,
6566 }
6667 if config .TerraformProvider == nil {
6768 return terraform.Setup {}, errors .New ("terraform provider cannot be nil" )
6869 }
69- return ps , errors .Wrap (configureNoForkAWSClient (ctx , & ps , config , awsCfg , creds , pc ), "could not configure the no-fork AWS client" )
70+ return ps , errors .Wrap (configureNoForkAWSClient (ctx , & ps , config , awsCfg . Region , creds , pc ), "could not configure the no-fork AWS client" )
7071 }
7172}
7273
@@ -80,12 +81,18 @@ func getAccountId(ctx context.Context, cfg *aws.Config, creds aws.Credentials) (
8081 return * identity .Account , nil
8182}
8283
83- func GetAWSConfigWithDefaultRegion (ctx context.Context , c client.Client , mg resource.Managed , pc * v1beta1.ProviderConfig ) (* aws.Config , error ) {
84- cfg , err := GetAWSConfigViaProviderConfig (ctx , c , mg , pc )
84+ // getAWSConfigWithDefaultRegion is a utility function that wraps the
85+ // GetAWSConfigWithoutTracking and fills empty region in the returned for
86+ // "iam.aws.upbound.io" group with a default "us-east-1" region. Although
87+ // this does not have an effect on the resource, as IAM group resources
88+ // has no concept of region, this is done to conform with the TF AWS config
89+ // which requires non-empty region
90+ func getAWSConfigWithDefaultRegion (ctx context.Context , c client.Client , obj runtime.Object , pc * v1beta1.ProviderConfig ) (* aws.Config , error ) {
91+ cfg , err := GetAWSConfigWithoutTracking (ctx , c , obj , pc )
8592 if err != nil {
86- return nil , errors . Wrap ( err , "cannot get AWS config" )
93+ return nil , err
8794 }
88- if cfg .Region == "" && mg .GetObjectKind ().GroupVersionKind ().Group == "iam.aws.upbound.io" {
95+ if cfg .Region == "" && obj .GetObjectKind ().GroupVersionKind ().Group == "iam.aws.upbound.io" {
8996 cfg .Region = "us-east-1"
9097 }
9198 return cfg , nil
@@ -102,22 +109,21 @@ func (m *metaOnlyPrimary) Meta() any {
102109// configureNoForkAWSClient populates the supplied *terraform.Setup with
103110// Terraform Plugin SDK style AWS client (Meta) and Terraform Plugin Framework
104111// style FrameworkProvider
105- func configureNoForkAWSClient (ctx context.Context , ps * terraform.Setup , config * SetupConfig , awsCfg * aws. Config , creds aws.Credentials , pc * v1beta1.ProviderConfig ) error { //nolint:gocyclo
112+ func configureNoForkAWSClient (ctx context.Context , ps * terraform.Setup , config * SetupConfig , region string , creds aws.Credentials , pc * v1beta1.ProviderConfig ) error { //nolint:gocyclo
106113 tfAwsConnsCfg := xpprovider.AWSConfig {
107- AccessKey : creds .AccessKeyID ,
108- EC2MetadataServiceEnableState : imds .ClientDefaultEnableState ,
109- Endpoints : map [string ]string {},
110- Region : awsCfg .Region ,
111- S3UsePathStyle : pc .Spec .S3UsePathStyle ,
112- SecretKey : creds .SecretAccessKey ,
113- SkipCredsValidation : true , // disabled to prevent extra AWS STS call
114- SkipRegionValidation : pc .Spec .SkipRegionValidation ,
115- SkipRequestingAccountId : true , // disabled to prevent extra AWS STS call
116- Token : creds .SessionToken ,
114+ AccessKey : creds .AccessKeyID ,
115+ Endpoints : map [string ]string {},
116+ Region : region ,
117+ S3UsePathStyle : pc .Spec .S3UsePathStyle ,
118+ SecretKey : creds .SecretAccessKey ,
119+ SkipCredsValidation : true , // disabled to prevent extra AWS STS call
120+ SkipRegionValidation : pc .Spec .SkipRegionValidation ,
121+ SkipRequestingAccountId : true , // disabled to prevent extra AWS STS call
122+ Token : creds .SessionToken ,
117123 }
118124
119125 if pc .Spec .SkipMetadataApiCheck {
120- tfAwsConnsCfg .EC2MetadataServiceEnableState = imds .ClientEnabled
126+ tfAwsConnsCfg .EC2MetadataServiceEnableState = imds .ClientDisabled
121127 }
122128
123129 if pc .Spec .Endpoint != nil {
@@ -147,7 +153,7 @@ func configureNoForkAWSClient(ctx context.Context, ps *terraform.Setup, config *
147153 // the resulting TF AWS Client has empty account ID.
148154 // Fill with previously calculated account ID.
149155 // No need for nil check on ps.ClientMetadata per golang spec.
150- tfAwsConnsClient .AccountID = ps .ClientMetadata [keyAccountId ]
156+ tfAwsConnsClient .AccountID = ps .ClientMetadata [keyAccountID ]
151157 ps .Meta = tfAwsConnsClient
152158 fwProvider := xpprovider .GetFrameworkProviderWithMeta (& metaOnlyPrimary {meta : tfAwsConnsClient })
153159 ps .FrameworkProvider = fwProvider
0 commit comments