Skip to content

Commit f206d42

Browse files
committed
add
1 parent 7d86bde commit f206d42

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

tencentcloud/provider.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ const (
131131
PROVIDER_ASSUME_ROLE_ARN = "TENCENTCLOUD_ASSUME_ROLE_ARN"
132132
PROVIDER_ASSUME_ROLE_SESSION_NAME = "TENCENTCLOUD_ASSUME_ROLE_SESSION_NAME"
133133
PROVIDER_ASSUME_ROLE_SESSION_DURATION = "TENCENTCLOUD_ASSUME_ROLE_SESSION_DURATION"
134+
PROVIDER_ASSUME_ROLE_EXTERNAL_ID = "TENCENTCLOUD_ASSUME_ROLE_EXTERNAL_ID"
134135
PROVIDER_ASSUME_ROLE_SAML_ASSERTION = "TENCENTCLOUD_ASSUME_ROLE_SAML_ASSERTION"
135136
PROVIDER_ASSUME_ROLE_PRINCIPAL_ARN = "TENCENTCLOUD_ASSUME_ROLE_PRINCIPAL_ARN"
136137
PROVIDER_ASSUME_ROLE_WEB_IDENTITY_TOKEN = "TENCENTCLOUD_ASSUME_ROLE_WEB_IDENTITY_TOKEN"
@@ -249,6 +250,12 @@ func Provider() *schema.Provider {
249250
Optional: true,
250251
Description: "A more restrictive policy when making the AssumeRole call. Its content must not contains `principal` elements. Notice: more syntax references, please refer to: [policies syntax logic](https://intl.cloud.tencent.com/document/product/598/10603).",
251252
},
253+
"external_id": {
254+
Type: schema.TypeString,
255+
Optional: true,
256+
DefaultFunc: schema.EnvDefaultFunc(PROVIDER_ASSUME_ROLE_EXTERNAL_ID, nil),
257+
Description: "External role ID, which can be obtained by clicking the role name in the CAM console. It can contain 2-128 letters, digits, and symbols (=,.@:/-). Regex: [\\w+=,.@:/-]*. It can be sourced from the `TENCENTCLOUD_ASSUME_ROLE_EXTERNAL_ID`.",
258+
},
252259
},
253260
},
254261
},
@@ -2249,6 +2256,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
22492256
assumeRoleSessionName string
22502257
assumeRoleSessionDuration int
22512258
assumeRolePolicy string
2259+
assumeRoleExternalId string
22522260
)
22532261

22542262
// get assume role from credential
@@ -2263,7 +2271,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
22632271
if assumeRoleArn != "" && assumeRoleSessionName != "" {
22642272
assumeRoleSessionDuration = 7200
22652273
assumeRolePolicy = ""
2266-
_ = genClientWithSTS(&tcClient, assumeRoleArn, assumeRoleSessionName, assumeRoleSessionDuration, assumeRolePolicy)
2274+
_ = genClientWithSTS(&tcClient, assumeRoleArn, assumeRoleSessionName, assumeRoleSessionDuration, assumeRolePolicy, assumeRoleExternalId)
22672275
}
22682276

22692277
// get assume role from env
@@ -2282,6 +2290,8 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
22822290
assumeRoleSessionDuration = 7200
22832291
}
22842292

2293+
assumeRoleExternalId = os.Getenv(PROVIDER_ASSUME_ROLE_EXTERNAL_ID)
2294+
22852295
// get assume role with saml from env
22862296
envSamlAssertion := os.Getenv(PROVIDER_ASSUME_ROLE_SAML_ASSERTION)
22872297
envPrincipalArn := os.Getenv(PROVIDER_ASSUME_ROLE_PRINCIPAL_ARN)
@@ -2290,7 +2300,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
22902300

22912301
if envSamlAssertion == "" && envPrincipalArn == "" && envWebIdentityToken == "" {
22922302
// use assume role
2293-
_ = genClientWithSTS(&tcClient, envRoleArn, envSessionName, assumeRoleSessionDuration, "")
2303+
_ = genClientWithSTS(&tcClient, envRoleArn, envSessionName, assumeRoleSessionDuration, "", assumeRoleExternalId)
22942304
} else if envSamlAssertion != "" && envPrincipalArn != "" && envWebIdentityToken != "" {
22952305
return nil, fmt.Errorf("can not set `TENCENTCLOUD_ASSUME_ROLE_SAML_ASSERTION`, `TENCENTCLOUD_ASSUME_ROLE_PRINCIPAL_ARN`, `TENCENTCLOUD_ASSUME_ROLE_WEB_IDENTITY_TOKEN` at the same time.\n")
22962306
} else if envSamlAssertion != "" && envPrincipalArn != "" {
@@ -2315,8 +2325,9 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
23152325
assumeRoleSessionName = assumeRole["session_name"].(string)
23162326
assumeRoleSessionDuration = assumeRole["session_duration"].(int)
23172327
assumeRolePolicy = assumeRole["policy"].(string)
2328+
assumeRoleExternalId = assumeRole["external_id"].(string)
23182329

2319-
_ = genClientWithSTS(&tcClient, assumeRoleArn, assumeRoleSessionName, assumeRoleSessionDuration, assumeRolePolicy)
2330+
_ = genClientWithSTS(&tcClient, assumeRoleArn, assumeRoleSessionName, assumeRoleSessionDuration, assumeRolePolicy, assumeRoleExternalId)
23202331
needSecret = true
23212332
}
23222333
}
@@ -2370,6 +2381,10 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
23702381
}
23712382
}
23722383

2384+
if camRoleName != "" && assumeRoleExternalId != "" {
2385+
needSecret = false
2386+
}
2387+
23732388
if needSecret && (secretId == "" || secretKey == "") {
23742389
return nil, fmt.Errorf("Please set your `secret_id` and `secret_key`.\n")
23752390
}
@@ -2393,7 +2408,7 @@ func genClientWithCAM(tcClient *TencentCloudClient, roleName string) error {
23932408
return nil
23942409
}
23952410

2396-
func genClientWithSTS(tcClient *TencentCloudClient, assumeRoleArn, assumeRoleSessionName string, assumeRoleSessionDuration int, assumeRolePolicy string) error {
2411+
func genClientWithSTS(tcClient *TencentCloudClient, assumeRoleArn, assumeRoleSessionName string, assumeRoleSessionDuration int, assumeRolePolicy string, assumeRoleExternalId string) error {
23972412
// applying STS credentials
23982413
request := sdksts.NewAssumeRoleRequest()
23992414
request.RoleArn = helper.String(assumeRoleArn)
@@ -2403,6 +2418,10 @@ func genClientWithSTS(tcClient *TencentCloudClient, assumeRoleArn, assumeRoleSes
24032418
request.Policy = helper.String(url.QueryEscape(assumeRolePolicy))
24042419
}
24052420

2421+
if assumeRoleExternalId != "" {
2422+
request.ExternalId = helper.String(assumeRoleExternalId)
2423+
}
2424+
24062425
ratelimit.Check(request.GetAction())
24072426
response, err := tcClient.apiV3Conn.UseStsClient().AssumeRole(request)
24082427
if err != nil {

website/docs/index.html.markdown

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ $ terraform plan
141141

142142
### Assume role
143143

144-
If provided with an assume role, Terraform will attempt to assume this role using the supplied credentials. Assume role can be provided by adding an `role_arn`, `session_name`, `session_duration` and `policy`(optional) in-line in the tencentcloud provider block:
144+
If provided with an assume role, Terraform will attempt to assume this role using the supplied credentials. Assume role can be provided by adding an `role_arn`, `session_name`, `session_duration`, `policy`(optional) and `external_id`(optional) in-line in the tencentcloud provider block:
145145

146146
Usage:
147147

@@ -160,7 +160,7 @@ provider "tencentcloud" {
160160
}
161161
```
162162

163-
The `role_arn`, `session_name`, `session_duration` can also provided via `TENCENTCLOUD_ASSUME_ROLE_ARN`, `TENCENTCLOUD_ASSUME_ROLE_SESSION_NAME` and `TENCENTCLOUD_ASSUME_ROLE_SESSION_DURATION` environment variables.
163+
The `role_arn`, `session_name`, `session_duration` and `external_id` can also provided via `TENCENTCLOUD_ASSUME_ROLE_ARN`, `TENCENTCLOUD_ASSUME_ROLE_SESSION_NAME`, `TENCENTCLOUD_ASSUME_ROLE_SESSION_DURATION` and `TENCENTCLOUD_ASSUME_ROLE_EXTERNAL_ID` environment variables.
164164

165165
Usage:
166166

@@ -254,7 +254,7 @@ provider "tencentcloud" {
254254

255255
### Cam role name
256256

257-
If provided with a Cam role name, Terraform will just access the metadata URL: http://metadata.tencentyun.com/latest/meta-data/cam/security-credentials/<cam_role_name> to obtain the STS credential. The CVM Instance Role also can be set using the TENCENTCLOUD_CAM_ROLE_NAME environment variables.
257+
If provided with a Cam role name, Terraform will just access the metadata URL: [Cam security credentials](http://metadata.tencentyun.com/latest/meta-data/cam/security-credentials/<cam_role_name>) to obtain the STS credential. The CVM Instance Role also can be set using the `TENCENTCLOUD_CAM_ROLE_NAME` environment variables.
258258

259259
-> **Note:** Cam-role-name is used to grant the role entity the permissions to access services and resources and perform operations in Tencent Cloud. You can associate the CAM role with a CVM instance to call other Tencent Cloud APIs from the instance using the periodically updated temporary Security Token Service (STS) key.
260260

@@ -268,6 +268,24 @@ provider "tencentcloud" {
268268
}
269269
```
270270

271+
It can also be authenticated together with method Assume role. Authentication process: Perform CAM authentication first, then proceed with Assume role authentication.
272+
273+
Usage:
274+
275+
```hcl
276+
provider "tencentcloud" {
277+
cam_role_name = "my-cam-role-name"
278+
279+
assume_role {
280+
role_arn = "my-role-arn"
281+
session_name = "my-session-name"
282+
policy = "my-role-policy"
283+
session_duration = 3600
284+
external_id = "my-external-id"
285+
}
286+
}
287+
```
288+
271289
### CDC cos usage
272290

273291
You can set the cos domain by setting the environment variable `TENCENTCLOUD_COS_DOMAIN`, and configure the cdc scenario as follows:
@@ -347,6 +365,7 @@ The nested `assume_role` block supports the following:
347365
* `session_name` - (Required) The session name to use when making the AssumeRole call. It can also be sourced from the `TENCENTCLOUD_ASSUME_ROLE_SESSION_NAME` environment variable.
348366
* `session_duration` - (Required) The duration of the session when making the AssumeRole call. Its value ranges from 0 to 43200(seconds), and default is 7200 seconds. It can also be sourced from the `TENCENTCLOUD_ASSUME_ROLE_SESSION_DURATION` environment variable.
349367
* `policy` - (Optional) A more restrictive policy to apply to the temporary credentials. This gives you a way to further restrict the permissions for the resulting temporary security credentials. You cannot use the passed policy to grant permissions that are in excess of those allowed by the access policy of the role that is being assumed.
368+
* `external_id` - (Optional) External role ID, which can be obtained by clicking the role name in the CAM console. It can contain 2-128 letters, digits, and symbols (=,.@\:/-). Regex: [\\w+=,.@\:/-]*. It can be sourced from the `TENCENTCLOUD_ASSUME_ROLE_EXTERNAL_ID`.
350369

351370
The nested `assume_role_with_saml` block supports the following:
352371
* `role_arn` - (Required) The ARN of the role to assume. It can also be sourced from the `TENCENTCLOUD_ASSUME_ROLE_ARN` environment variable.

0 commit comments

Comments
 (0)