service/vendor/github.com/aliyun/credentials-go/credentials/env_provider.go

48 lines
1.2 KiB
Go
Raw Normal View History

2023-12-21 22:17:40 +08:00
package credentials
import (
"errors"
"os"
"github.com/alibabacloud-go/tea/tea"
)
type envProvider struct{}
var providerEnv = new(envProvider)
const (
// EnvVarAccessKeyId is a name of ALIBABA_CLOUD_ACCESS_KEY_Id
2024-04-26 10:46:37 +08:00
EnvVarAccessKeyId = "ALIBABA_CLOUD_ACCESS_KEY_Id"
EnvVarAccessKeyIdNew = "ALIBABA_CLOUD_ACCESS_KEY_ID"
2023-12-21 22:17:40 +08:00
// EnvVarAccessKeySecret is a name of ALIBABA_CLOUD_ACCESS_KEY_SECRET
EnvVarAccessKeySecret = "ALIBABA_CLOUD_ACCESS_KEY_SECRET"
)
func newEnvProvider() Provider {
return &envProvider{}
}
func (p *envProvider) resolve() (*Config, error) {
2024-04-26 10:46:37 +08:00
accessKeyId, ok1 := os.LookupEnv(EnvVarAccessKeyIdNew)
if !ok1 || accessKeyId == "" {
accessKeyId, ok1 = os.LookupEnv(EnvVarAccessKeyId)
}
2023-12-21 22:17:40 +08:00
accessKeySecret, ok2 := os.LookupEnv(EnvVarAccessKeySecret)
if !ok1 || !ok2 {
return nil, nil
}
if accessKeyId == "" {
2024-04-26 10:46:37 +08:00
return nil, errors.New(EnvVarAccessKeyIdNew + " or " + EnvVarAccessKeyId + " cannot be empty")
2023-12-21 22:17:40 +08:00
}
if accessKeySecret == "" {
return nil, errors.New(EnvVarAccessKeySecret + " cannot be empty")
}
config := &Config{
Type: tea.String("access_key"),
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret),
}
return config, nil
}