Skip to content

Commit 196205e

Browse files
authored
Merge pull request #36581 from xiaozhu36/creds
backend/oss: Supports more standard environment variables to keep same with provider setting
2 parents 2b5101f + 8462c5f commit 196205e

File tree

3 files changed

+59
-48
lines changed

3 files changed

+59
-48
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: ENHANCEMENTS
2+
body: 'backend/oss: Supports more standard environment variables to keep same with provider setting'
3+
time: 2025-03-03T17:18:38.679213+08:00
4+
custom:
5+
Issue: "36581"

internal/backend/remote-state/oss/backend.go

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ func deprecatedAssumeRoleSchema() *schema.Schema {
5050
Type: schema.TypeString,
5151
Required: true,
5252
Description: "The ARN of a RAM role to assume prior to making API calls.",
53-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_ASSUME_ROLE_ARN", ""),
53+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_ASSUME_ROLE_ARN", "ALIBABA_CLOUD_ROLE_ARN"}, ""),
5454
},
5555
"session_name": {
5656
Type: schema.TypeString,
5757
Optional: true,
5858
Description: "The session name to use when assuming the role.",
59-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_ASSUME_ROLE_SESSION_NAME", ""),
59+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_ASSUME_ROLE_SESSION_NAME", "ALIBABA_CLOUD_ROLE_SESSION_NAME"}, ""),
6060
},
6161
"policy": {
6262
Type: schema.TypeString,
@@ -95,53 +95,53 @@ func New() backend.Backend {
9595
Type: schema.TypeString,
9696
Optional: true,
9797
Description: "Alibaba Cloud Access Key ID",
98-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_ACCESS_KEY", os.Getenv("ALICLOUD_ACCESS_KEY_ID")),
98+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_ACCESS_KEY", "ALIBABA_CLOUD_ACCESS_KEY_ID", "ALICLOUD_ACCESS_KEY_ID"}, ""),
9999
},
100100

101101
"secret_key": {
102102
Type: schema.TypeString,
103103
Optional: true,
104104
Description: "Alibaba Cloud Access Secret Key",
105-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_SECRET_KEY", os.Getenv("ALICLOUD_ACCESS_KEY_SECRET")),
105+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_SECRET_KEY", "ALIBABA_CLOUD_ACCESS_KEY_SECRET", "ALICLOUD_ACCESS_KEY_SECRET"}, ""),
106106
},
107107

108108
"security_token": {
109109
Type: schema.TypeString,
110110
Optional: true,
111111
Description: "Alibaba Cloud Security Token",
112-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_SECURITY_TOKEN", ""),
112+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_SECURITY_TOKEN", "ALIBABA_CLOUD_SECURITY_TOKEN"}, ""),
113113
},
114114

115115
"ecs_role_name": {
116116
Type: schema.TypeString,
117117
Optional: true,
118-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_ECS_ROLE_NAME", os.Getenv("ALICLOUD_ECS_ROLE_NAME")),
118+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_ECS_ROLE_NAME", "ALIBABA_CLOUD_ECS_METADATA"}, ""),
119119
Description: "The RAM Role Name attached on a ECS instance for API operations. You can retrieve this from the 'Access Control' section of the Alibaba Cloud console.",
120120
},
121121

122122
"region": {
123123
Type: schema.TypeString,
124124
Optional: true,
125125
Description: "The region of the OSS bucket.",
126-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_REGION", os.Getenv("ALICLOUD_DEFAULT_REGION")),
126+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_REGION", "ALIBABA_CLOUD_REGION", "ALICLOUD_DEFAULT_REGION"}, ""),
127127
},
128128
"sts_endpoint": {
129129
Type: schema.TypeString,
130130
Optional: true,
131131
Description: "A custom endpoint for the STS API",
132-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_STS_ENDPOINT", ""),
132+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_STS_ENDPOINT", "ALIBABA_CLOUD_STS_ENDPOINT"}, ""),
133133
},
134134
"tablestore_endpoint": {
135135
Type: schema.TypeString,
136136
Optional: true,
137137
Description: "A custom endpoint for the TableStore API",
138-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_TABLESTORE_ENDPOINT", ""),
138+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_TABLESTORE_ENDPOINT", "ALIBABA_CLOUD_TABLESTORE_ENDPOINT"}, ""),
139139
},
140140
"endpoint": {
141141
Type: schema.TypeString,
142142
Optional: true,
143143
Description: "A custom endpoint for the OSS API",
144-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_OSS_ENDPOINT", os.Getenv("OSS_ENDPOINT")),
144+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_OSS_ENDPOINT", "ALIBABA_CLOUD_OSS_ENDPOINT", "OSS_ENDPOINT"}, ""),
145145
},
146146

147147
"bucket": {
@@ -217,27 +217,27 @@ func New() backend.Backend {
217217
"shared_credentials_file": {
218218
Type: schema.TypeString,
219219
Optional: true,
220-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_SHARED_CREDENTIALS_FILE", ""),
220+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_SHARED_CREDENTIALS_FILE", "ALIBABA_CLOUD_CREDENTIALS_FILE"}, ""),
221221
Description: "This is the path to the shared credentials file. If this is not set and a profile is specified, `~/.aliyun/config.json` will be used.",
222222
},
223223
"profile": {
224224
Type: schema.TypeString,
225225
Optional: true,
226226
Description: "This is the Alibaba Cloud profile name as set in the shared credentials file. It can also be sourced from the `ALICLOUD_PROFILE` environment variable.",
227-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_PROFILE", ""),
227+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_PROFILE", "ALIBABA_CLOUD_PROFILE"}, ""),
228228
},
229229
"assume_role": deprecatedAssumeRoleSchema(),
230230
"assume_role_role_arn": {
231231
Type: schema.TypeString,
232232
Optional: true,
233233
Description: "The ARN of a RAM role to assume prior to making API calls.",
234-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_ASSUME_ROLE_ARN", ""),
234+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_ASSUME_ROLE_ARN", "ALIBABA_CLOUD_ROLE_ARN"}, ""),
235235
},
236236
"assume_role_session_name": {
237237
Type: schema.TypeString,
238238
Optional: true,
239239
Description: "The session name to use when assuming the role.",
240-
DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_ASSUME_ROLE_SESSION_NAME", ""),
240+
DefaultFunc: schema.MultiEnvDefaultFunc([]string{"ALICLOUD_ASSUME_ROLE_SESSION_NAME", "ALIBABA_CLOUD_ROLE_SESSION_NAME"}, ""),
241241
},
242242
"assume_role_policy": {
243243
Type: schema.TypeString,
@@ -301,24 +301,26 @@ func (b *Backend) configure(ctx context.Context) error {
301301
b.serverSideEncryption = d.Get("encrypt").(bool)
302302
b.acl = d.Get("acl").(string)
303303

304-
var getBackendConfig = func(str string, key string) string {
305-
if str == "" {
306-
value, err := getConfigFromProfile(d, key)
307-
if err == nil && value != nil {
308-
str = value.(string)
304+
var getBackendConfig = func(schemaKey string, profileKey string) string {
305+
if schemaKey != "" {
306+
if v, ok := d.GetOk(schemaKey); ok && v != nil && v.(string) != "" {
307+
return v.(string)
309308
}
310309
}
311-
return str
310+
if v, err := getConfigFromProfile(d, profileKey); err == nil && v != nil {
311+
return v.(string)
312+
}
313+
return ""
312314
}
313315

314-
accessKey := getBackendConfig(d.Get("access_key").(string), "access_key_id")
315-
secretKey := getBackendConfig(d.Get("secret_key").(string), "access_key_secret")
316-
securityToken := getBackendConfig(d.Get("security_token").(string), "sts_token")
317-
region := getBackendConfig(d.Get("region").(string), "region_id")
316+
accessKey := getBackendConfig("access_key", "access_key_id")
317+
secretKey := getBackendConfig("secret_key", "access_key_secret")
318+
region := getBackendConfig("region", "region_id")
319+
securityToken := getBackendConfig("security_token", "sts_token")
318320

319321
stsEndpoint := d.Get("sts_endpoint").(string)
320322
endpoint := d.Get("endpoint").(string)
321-
schma := "https"
323+
protocol := "https"
322324

323325
roleArn := getBackendConfig("", "ram_role_arn")
324326
sessionName := getBackendConfig("", "ram_session_name")
@@ -370,7 +372,7 @@ func (b *Backend) configure(ctx context.Context) error {
370372
}
371373

372374
if accessKey == "" {
373-
ecsRoleName := getBackendConfig(d.Get("ecs_role_name").(string), "ram_role_name")
375+
ecsRoleName := getBackendConfig("ecs_role_name", "ram_role_name")
374376
subAccessKeyId, subAccessKeySecret, subSecurityToken, err := getAuthCredentialByEcsRoleName(ecsRoleName)
375377
if err != nil {
376378
return err
@@ -403,7 +405,7 @@ func (b *Backend) configure(ctx context.Context) error {
403405
}
404406
}
405407
if !strings.HasPrefix(endpoint, "http") {
406-
endpoint = fmt.Sprintf("%s://%s", schma, endpoint)
408+
endpoint = fmt.Sprintf("%s://%s", protocol, endpoint)
407409
}
408410
log.Printf("[DEBUG] Instantiate OSS client using endpoint: %#v", endpoint)
409411
var options []oss.ClientOption
@@ -423,7 +425,7 @@ func (b *Backend) configure(ctx context.Context) error {
423425
otsInstanceName := d.Get("tablestore_instance_name").(string)
424426
if otsEndpoint != "" {
425427
if !strings.HasPrefix(otsEndpoint, "http") {
426-
otsEndpoint = fmt.Sprintf("%s://%s", schma, otsEndpoint)
428+
otsEndpoint = fmt.Sprintf("%s://%s", protocol, otsEndpoint)
427429
}
428430
b.otsEndpoint = otsEndpoint
429431
if otsInstanceName == "" {
@@ -563,9 +565,13 @@ func getConfigFromProfile(d *schema.ResourceData, ProfileKey string) (interface{
563565
}
564566
current := d.Get("profile").(string)
565567
// Set CredsFilename, expanding home directory
566-
profilePath, err := homedir.Expand(d.Get("shared_credentials_file").(string))
567-
if err != nil {
568-
return nil, err
568+
var profilePath string
569+
if v, ok := d.GetOk("shared_credentials_file"); ok {
570+
path, err := homedir.Expand(v.(string))
571+
if err != nil {
572+
return nil, err
573+
}
574+
profilePath = path
569575
}
570576
if profilePath == "" {
571577
profilePath = fmt.Sprintf("%s/.aliyun/config.json", os.Getenv("HOME"))
@@ -574,7 +580,7 @@ func getConfigFromProfile(d *schema.ResourceData, ProfileKey string) (interface{
574580
}
575581
}
576582
providerConfig = make(map[string]interface{})
577-
_, err = os.Stat(profilePath)
583+
_, err := os.Stat(profilePath)
578584
if !os.IsNotExist(err) {
579585
data, err := ioutil.ReadFile(profilePath)
580586
if err != nil {

website/docs/language/backend/oss.mdx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,32 @@ data "terraform_remote_state" "network" {
7575

7676
The following configuration options or environment variables are supported:
7777

78-
* `access_key` - (Optional) Alibaba Cloud access key. It supports environment variables `ALICLOUD_ACCESS_KEY` and `ALICLOUD_ACCESS_KEY_ID`.
78+
* `access_key` - (Optional) Alibaba Cloud access key. It supports environment variables `ALICLOUD_ACCESS_KEY` and `ALIBABA_CLOUD_ACCESS_KEY_ID`(Recommended).
7979

80-
* `secret_key` - (Optional) Alibaba Cloud secret access key. It supports environment variables `ALICLOUD_SECRET_KEY` and `ALICLOUD_ACCESS_KEY_SECRET`.
80+
* `secret_key` - (Optional) Alibaba Cloud secret access key. It supports environment variables `ALICLOUD_SECRET_KEY` and `ALIBABA_CLOUD_ACCESS_KEY_SECRET`(Recommended).
8181

82-
* `security_token` - (Optional) STS access token. It supports environment variable `ALICLOUD_SECURITY_TOKEN`.
82+
* `security_token` - (Optional) STS access token. It supports environment variable `ALICLOUD_SECURITY_TOKEN` and `ALIBABA_CLOUD_SECURITY_TOKEN`(Recommended).
8383

84-
* `ecs_role_name` - (Optional, Available in 0.12.14+) The RAM Role Name attached on a ECS instance for API operations. You can retrieve this from the 'Access Control' section of the Alibaba Cloud console.
84+
* `ecs_role_name` - (Optional) The RAM Role Name attached on a ECS instance for API operations. You can retrieve this from the 'Access Control' section of the Alibaba Cloud console.
8585

86-
* `region` - (Optional) The region of the OSS bucket. It supports environment variables `ALICLOUD_REGION` and `ALICLOUD_DEFAULT_REGION`.
86+
* `region` - (Optional) The region of the OSS bucket. It supports environment variables `ALICLOUD_REGION` and `ALIBABA_CLOUD_REGION`(Recommended).
8787

88-
* `endpoint` - (Optional) A custom endpoint for the OSS API. It supports environment variables `ALICLOUD_OSS_ENDPOINT` and `OSS_ENDPOINT`.
88+
* `endpoint` - (Optional) A custom endpoint for the OSS API. It supports environment variables `ALICLOUD_OSS_ENDPOINT` and `ALIBABA_CLOUD_OSS_ENDPOINT`(Recommended).
8989

9090
* `bucket` - (Required) The name of the OSS bucket.
9191

9292
* `prefix` - (Opeional) The path directory of the state file will be stored. Default to "env:".
9393

9494
* `key` - (Optional) The name of the state file. Defaults to `terraform.tfstate`.
9595

96-
* `tablestore_endpoint` / `ALICLOUD_TABLESTORE_ENDPOINT` - (Optional) A custom endpoint for the TableStore API.
96+
* `tablestore_endpoint` - (Optional) A custom endpoint for the TableStore API. It supports environment variables `ALICLOUD_TABLESTORE_ENDPOINT` and `ALIBABA_CLOUD_TABLESTORE_ENDPOINT`(Recommended).
9797

9898
* `tablestore_instance_name` - (Optional) Specifies the name of an instance that `TableStore` belongs to. By default, Terraform parses the name from `tablestore_endpoint`.
9999
You should set the access URL explicitly when the `tablestore` endpoint is a VPC access URL.
100100

101101
* `tablestore_table` - (Optional) A TableStore table for state locking and consistency. The table must have a primary key named `LockID` of type `String`.
102102

103-
* `sts_endpoint` - (Optional, Available in 1.0.11+) Custom endpoint for the AliCloud Security Token Service (STS) API. It supports environment variable `ALICLOUD_STS_ENDPOINT`.
103+
* `sts_endpoint` - (Optional) Custom endpoint for the AliCloud Security Token Service (STS) API. It supports environment variable `ALICLOUD_STS_ENDPOINT` and `ALIBABA_CLOUD_STS_ENDPOINT`(Recommended).
104104

105105
* `encrypt` - (Optional) Whether to enable server side
106106
encryption of the state file. If it is true, OSS will use 'AES256' encryption algorithm to encrypt state file.
@@ -109,24 +109,24 @@ The following configuration options or environment variables are supported:
109109
ACL](https://www.alibabacloud.com/help/doc-detail/52284.htm)
110110
to be applied to the state file.
111111

112-
* `shared_credentials_file` - (Optional, Available in 0.12.8+) This is the path to the shared credentials file. It can also be sourced from the `ALICLOUD_SHARED_CREDENTIALS_FILE` environment variable. If this is not set and a profile is specified, `~/.aliyun/config.json` will be used.
112+
* `shared_credentials_file` - (Optional) This is the path to the shared credentials file. It can also be sourced from the `ALICLOUD_SHARED_CREDENTIALS_FILE` or `ALIBABA_CLOUD_CREDENTIALS_FILE`(Recommended) environment variable. If this is not set and a profile is specified, `~/.aliyun/config.json` will be used.
113113

114-
* `profile` - (Optional, Available in 0.12.8+) This is the Alibaba Cloud profile name as set in the shared credentials file. It can also be sourced from the `ALICLOUD_PROFILE` environment variable.
114+
* `profile` - (Optional) This is the Alibaba Cloud profile name as set in the shared credentials file. It supports environment variable `ALICLOUD_PROFILE` and `ALIBABA_CLOUD_PROFILE`(Recommended).
115115

116-
* `assume_role_role_arn` - (Optional, Available in 1.1.0+) The ARN of the role to assume. If ARN is set to an empty string, it does not perform role switching. It supports the environment variable `ALICLOUD_ASSUME_ROLE_ARN`.
116+
* `assume_role_role_arn` - (Optional) The ARN of the role to assume. If ARN is set to an empty string, it does not perform role switching. It supports the environment variable `ALICLOUD_ASSUME_ROLE_ARN` and `ALIBABA_CLOUD_ROLE_ARN`(Recommended).
117117
Terraform executes configuration on account with provided credentials.
118118

119-
* `assume_role_policy` - (Optional, Available in 1.1.0+) 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 this policy to grant permissions that exceed those of the role that is being assumed.
119+
* `assume_role_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 this policy to grant permissions that exceed those of the role that is being assumed.
120120

121-
* `assume_role_session_name` - (Optional, Available in 1.1.0+) The session name to use when assuming the role. If omitted, 'terraform' is passed to the AssumeRole call as session name. It supports environment variable `ALICLOUD_ASSUME_ROLE_SESSION_NAME`.
121+
* `assume_role_session_name` - (Optional) The session name to use when assuming the role. If omitted, 'terraform' is passed to the AssumeRole call as session name. It supports environment variable `ALICLOUD_ASSUME_ROLE_SESSION_NAME` and `ALIBABA_CLOUD_ROLE_SESSION_NAME`(Recommended).
122122

123-
* `assume_role_session_expiration` - (Optional, Available in 1.1.0+) The time after which the established session for assuming role expires. Valid value range: \[900-3600] seconds. Default to 3600 (in this case Alibaba Cloud uses its own default value). It supports environment variable `ALICLOUD_ASSUME_ROLE_SESSION_EXPIRATION`.
123+
* `assume_role_session_expiration` - (Optional) The time after which the established session for assuming role expires. Valid value range: \[900-3600] seconds. Default to 3600 (in this case Alibaba Cloud uses its own default value). It supports environment variable `ALICLOUD_ASSUME_ROLE_SESSION_EXPIRATION`.
124124

125-
* `assume_role` - (**Deprecated as of 1.1.0+**, Available in 0.12.6+) If provided with a role ARN, will attempt to assume this role using the supplied credentials. It will be ignored when `assume_role_role_arn` is specified.
125+
* `assume_role` - (**Deprecated as of 1.1.0+**) If provided with a role ARN, will attempt to assume this role using the supplied credentials. It will be ignored when `assume_role_role_arn` is specified.
126126

127127
**Deprecated in favor of flattening assume_role_\* options**
128128

129-
* `role_arn` - (Required) The ARN of the role to assume. If ARN is set to an empty string, it does not perform role switching. It supports the environment variable `ALICLOUD_ASSUME_ROLE_ARN`.
129+
* `role_arn` - (Required) The ARN of the role to assume. If ARN is set to an empty string, it does not perform role switching. It supports the environment variable `ALICLOUD_ASSUME_ROLE_ARN` and `ALIBABA_CLOUD_ROLE_ARN`(Recommended).
130130
Terraform executes configuration on account with provided credentials.
131131

132132
* `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 this policy to grant permissions that exceed those of the role that is being assumed.

0 commit comments

Comments
 (0)