9
9
"strconv"
10
10
"strings"
11
11
12
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
14
"github.com/mitchellh/go-homedir"
14
15
sdkcommon "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
@@ -2513,7 +2514,21 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
2513
2514
}
2514
2515
2515
2516
func genClientWithCAM (tcClient * TencentCloudClient , roleName string ) error {
2516
- camResp , err := tccommon .GetAuthFromCAM (roleName )
2517
+ var camResp * tccommon.CAMResponse
2518
+ err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
2519
+ result , e := tccommon .GetAuthFromCAM (roleName )
2520
+ if e != nil {
2521
+ return tccommon .RetryError (e )
2522
+ }
2523
+
2524
+ if result == nil {
2525
+ return resource .NonRetryableError (fmt .Errorf ("Get cam failed, Response is nil." ))
2526
+ }
2527
+
2528
+ camResp = result
2529
+ return nil
2530
+ })
2531
+
2517
2532
if err != nil {
2518
2533
return err
2519
2534
}
@@ -2531,6 +2546,7 @@ func genClientWithCAM(tcClient *TencentCloudClient, roleName string) error {
2531
2546
func genClientWithSTS (tcClient * TencentCloudClient , assumeRoleArn , assumeRoleSessionName string , assumeRoleSessionDuration int , assumeRolePolicy string , assumeRoleExternalId string ) error {
2532
2547
// applying STS credentials
2533
2548
request := sdksts .NewAssumeRoleRequest ()
2549
+ response := sdksts .NewAssumeRoleResponse ()
2534
2550
request .RoleArn = helper .String (assumeRoleArn )
2535
2551
request .RoleSessionName = helper .String (assumeRoleSessionName )
2536
2552
request .DurationSeconds = helper .IntUint64 (assumeRoleSessionDuration )
@@ -2542,12 +2558,29 @@ func genClientWithSTS(tcClient *TencentCloudClient, assumeRoleArn, assumeRoleSes
2542
2558
request .ExternalId = helper .String (assumeRoleExternalId )
2543
2559
}
2544
2560
2545
- ratelimit .Check (request .GetAction ())
2546
- response , err := tcClient .apiV3Conn .UseStsClient ().AssumeRole (request )
2561
+ err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
2562
+ ratelimit .Check (request .GetAction ())
2563
+ result , e := tcClient .apiV3Conn .UseStsClient ().AssumeRole (request )
2564
+ if e != nil {
2565
+ return tccommon .RetryError (e )
2566
+ }
2567
+
2568
+ if result == nil || result .Response == nil || result .Response .Credentials == nil {
2569
+ return resource .NonRetryableError (fmt .Errorf ("Get Assume Role failed, Response is nil." ))
2570
+ }
2571
+
2572
+ response = result
2573
+ return nil
2574
+ })
2575
+
2547
2576
if err != nil {
2548
2577
return err
2549
2578
}
2550
2579
2580
+ if response .Response .Credentials .TmpSecretId == nil || response .Response .Credentials .TmpSecretKey == nil || response .Response .Credentials .Token == nil {
2581
+ return fmt .Errorf ("Get Assume Role failed, Credentials is nil." )
2582
+ }
2583
+
2551
2584
// using STS credentials
2552
2585
tcClient .apiV3Conn .Credential = sdkcommon .NewTokenCredential (
2553
2586
* response .Response .Credentials .TmpSecretId ,
@@ -2561,20 +2594,37 @@ func genClientWithSTS(tcClient *TencentCloudClient, assumeRoleArn, assumeRoleSes
2561
2594
func genClientWithSamlSTS (tcClient * TencentCloudClient , assumeRoleArn , assumeRoleSessionName string , assumeRoleSessionDuration int , assumeRoleSamlAssertion , assumeRolePrincipalArn string ) error {
2562
2595
// applying STS credentials
2563
2596
request := sdksts .NewAssumeRoleWithSAMLRequest ()
2597
+ response := sdksts .NewAssumeRoleWithSAMLResponse ()
2564
2598
request .RoleArn = helper .String (assumeRoleArn )
2565
2599
request .RoleSessionName = helper .String (assumeRoleSessionName )
2566
2600
request .DurationSeconds = helper .IntUint64 (assumeRoleSessionDuration )
2567
2601
request .SAMLAssertion = helper .String (assumeRoleSamlAssertion )
2568
2602
request .PrincipalArn = helper .String (assumeRolePrincipalArn )
2569
-
2570
- ratelimit .Check (request .GetAction ())
2571
2603
var stsExtInfo connectivity.StsExtInfo
2572
2604
stsExtInfo .Authorization = "SKIP"
2573
- response , err := tcClient .apiV3Conn .UseStsClient (stsExtInfo ).AssumeRoleWithSAML (request )
2605
+ err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
2606
+ ratelimit .Check (request .GetAction ())
2607
+ result , e := tcClient .apiV3Conn .UseStsClient (stsExtInfo ).AssumeRoleWithSAML (request )
2608
+ if e != nil {
2609
+ return tccommon .RetryError (e )
2610
+ }
2611
+
2612
+ if result == nil || result .Response == nil || result .Response .Credentials == nil {
2613
+ return resource .NonRetryableError (fmt .Errorf ("Get Assume Role with SAML failed, Response is nil." ))
2614
+ }
2615
+
2616
+ response = result
2617
+ return nil
2618
+ })
2619
+
2574
2620
if err != nil {
2575
2621
return err
2576
2622
}
2577
2623
2624
+ if response .Response .Credentials .TmpSecretId == nil || response .Response .Credentials .TmpSecretKey == nil || response .Response .Credentials .Token == nil {
2625
+ return fmt .Errorf ("Get Assume Role failed, Credentials is nil." )
2626
+ }
2627
+
2578
2628
// using STS credentials
2579
2629
tcClient .apiV3Conn .Credential = sdkcommon .NewTokenCredential (
2580
2630
* response .Response .Credentials .TmpSecretId ,
@@ -2588,20 +2638,37 @@ func genClientWithSamlSTS(tcClient *TencentCloudClient, assumeRoleArn, assumeRol
2588
2638
func genClientWithOidcSTS (tcClient * TencentCloudClient , assumeRoleArn , assumeRoleSessionName string , assumeRoleSessionDuration int , assumeRolePolicy string ) error {
2589
2639
// applying STS credentials
2590
2640
request := sdksts .NewAssumeRoleWithWebIdentityRequest ()
2641
+ response := sdksts .NewAssumeRoleWithWebIdentityResponse ()
2591
2642
request .ProviderId = helper .String ("OIDC" )
2592
2643
request .RoleArn = helper .String (assumeRoleArn )
2593
2644
request .RoleSessionName = helper .String (assumeRoleSessionName )
2594
2645
request .DurationSeconds = helper .IntInt64 (assumeRoleSessionDuration )
2595
2646
request .WebIdentityToken = helper .String (assumeRolePolicy )
2596
-
2597
- ratelimit .Check (request .GetAction ())
2598
2647
var stsExtInfo connectivity.StsExtInfo
2599
2648
stsExtInfo .Authorization = "SKIP"
2600
- response , err := tcClient .apiV3Conn .UseStsClient (stsExtInfo ).AssumeRoleWithWebIdentity (request )
2649
+ err := resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
2650
+ ratelimit .Check (request .GetAction ())
2651
+ result , e := tcClient .apiV3Conn .UseStsClient (stsExtInfo ).AssumeRoleWithWebIdentity (request )
2652
+ if e != nil {
2653
+ return tccommon .RetryError (e )
2654
+ }
2655
+
2656
+ if result == nil || result .Response == nil || result .Response .Credentials == nil {
2657
+ return resource .NonRetryableError (fmt .Errorf ("Get Assume Role with OIDC failed, Response is nil." ))
2658
+ }
2659
+
2660
+ response = result
2661
+ return nil
2662
+ })
2663
+
2601
2664
if err != nil {
2602
2665
return err
2603
2666
}
2604
2667
2668
+ if response .Response .Credentials .TmpSecretId == nil || response .Response .Credentials .TmpSecretKey == nil || response .Response .Credentials .Token == nil {
2669
+ return fmt .Errorf ("Get Assume Role failed, Credentials is nil." )
2670
+ }
2671
+
2605
2672
// using STS credentials
2606
2673
tcClient .apiV3Conn .Credential = sdkcommon .NewTokenCredential (
2607
2674
* response .Response .Credentials .TmpSecretId ,
@@ -2707,6 +2774,7 @@ func genClientWithPodOidc(tcClient *TencentCloudClient) error {
2707
2774
if err != nil {
2708
2775
return err
2709
2776
}
2777
+
2710
2778
assumeResp , err := provider .GetCredential ()
2711
2779
if err != nil {
2712
2780
return err
@@ -2731,21 +2799,37 @@ func getCallerIdentity(tcClient *TencentCloudClient) (indentity *sdksts.GetCalle
2731
2799
cpf .HttpProfile .Endpoint = "sts.tencentcloudapi.com"
2732
2800
client , _ := sdksts .NewClient (credential , region , cpf )
2733
2801
request := sdksts .NewGetCallerIdentityRequest ()
2734
- response , err := client .GetCallerIdentity (request )
2802
+ response := sdksts .NewGetCallerIdentityResponse ()
2803
+ err = resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
2804
+ result , e := client .GetCallerIdentity (request )
2805
+ if e != nil {
2806
+ return tccommon .RetryError (e )
2807
+ }
2808
+
2809
+ if result == nil || result .Response == nil {
2810
+ return resource .NonRetryableError (fmt .Errorf ("Get caller identity failed, Response is nil." ))
2811
+ }
2812
+
2813
+ response = result
2814
+ return nil
2815
+ })
2816
+
2735
2817
if err != nil {
2736
2818
return
2737
2819
}
2738
2820
2739
- if response == nil || response .Response == nil {
2740
- return nil , fmt .Errorf ("Get GetCallerIdentity failed, Response is nil." )
2741
- }
2742
-
2743
2821
indentity = response .Response
2744
2822
return
2745
2823
}
2746
2824
2747
2825
func verifyAccountIDAllowed (indentity * sdksts.GetCallerIdentityResponseParams , allowedAccountIds , forbiddenAccountIds []string ) error {
2748
- accountId := * indentity .AccountId
2826
+ var accountId string
2827
+ if indentity .AccountId != nil {
2828
+ accountId = * indentity .AccountId
2829
+ } else {
2830
+ return fmt .Errorf ("Caller identity accountId is illegal, The value is nil." )
2831
+ }
2832
+
2749
2833
if len (allowedAccountIds ) > 0 {
2750
2834
found := false
2751
2835
for _ , allowedAccountID := range allowedAccountIds {
0 commit comments