Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error message #72

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions credentials/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ func NewCredential(config *Config) (credential Credential, err error) {
tea.StringValue(config.AccessKeySecret),
tea.StringValue(config.SecurityToken))
case "ecs_ram_role":
checkEcsRAMRole(config)
runtime := &utils.Runtime{
Host: tea.StringValue(config.Host),
Proxy: tea.StringValue(config.Proxy),
Expand Down Expand Up @@ -317,7 +316,7 @@ func NewCredential(config *Config) (credential Credential, err error) {
}
credential = newBearerTokenCredential(tea.StringValue(config.BearerToken))
default:
err = errors.New("Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair")
err = errors.New("invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair")
return
}
return credential, nil
Expand Down Expand Up @@ -371,10 +370,6 @@ func checkRAMRoleArn(config *Config) (err error) {
return
}

func checkEcsRAMRole(config *Config) (err error) {
return
}

func checkSTS(config *Config) (err error) {
if tea.StringValue(config.AccessKeyId) == "" {
err = errors.New("AccessKeyId cannot be empty")
Expand Down
4 changes: 2 additions & 2 deletions credentials/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestNewCredentialWithNil(t *testing.T) {
os.Unsetenv(EnvVarAccessKeySecret)
cred, err = NewCredential(nil)
assert.NotNil(t, err)
assert.Equal(t, "No credential found", err.Error())
assert.Equal(t, "no credential found", err.Error())
assert.Nil(t, cred)
}

Expand Down Expand Up @@ -252,7 +252,7 @@ func TestNewCredentialWithInvalidType(t *testing.T) {
config.SetType("sdk")
cred, err := NewCredential(config)
assert.NotNil(t, err)
assert.Equal(t, "Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair", err.Error())
assert.Equal(t, "invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair", err.Error())
assert.Nil(t, cred)
}

Expand Down
2 changes: 1 addition & 1 deletion credentials/ecs_ram_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (e *EcsRAMRoleCredential) updateCredential() (err error) {
if e.EnableIMDSv2 {
err = e.getMetadataToken()
if err != nil {
return fmt.Errorf("Failed to get token from ECS Metadata Service: %s", err.Error())
return fmt.Errorf("failed to get token from ECS Metadata Service: %s", err.Error())
}
request.Headers["X-aliyun-ecs-metadata-token"] = e.metadataToken
}
Expand Down
14 changes: 7 additions & 7 deletions credentials/ecs_ram_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Test_EcsRAmRoleCredential(t *testing.T) {
}
}
auth.RoleName = ""
accesskeyId, err = auth.GetAccessKeyId()
_, err = auth.GetAccessKeyId()
assert.NotNil(t, err)
assert.Equal(t, "refresh Ecs sts token err: httpStatus: 400, message = role", err.Error())

Expand All @@ -62,7 +62,7 @@ func Test_EcsRAmRoleCredential(t *testing.T) {
return mockResponse(200, `role`, nil)
}
}
accesskeyId, err = auth.GetAccessKeyId()
_, err = auth.GetAccessKeyId()
assert.NotNil(t, err)
assert.Equal(t, "refresh Ecs sts token err: Json Unmarshal fail: invalid character 'r' looking for beginning of value", err.Error())
hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
Expand Down Expand Up @@ -155,13 +155,13 @@ func Test_EcsRAmRoleCredentialEnableIMDSv2(t *testing.T) {
auth = newEcsRAMRoleCredentialWithEnableIMDSv2("go sdk", true, 0, 0.5, nil)
accesskeyId, err = auth.GetAccessKeyId()
assert.NotNil(t, err)
assert.Equal(t, "Failed to get token from ECS Metadata Service: sdk test", err.Error())
assert.Equal(t, "failed to get token from ECS Metadata Service: sdk test", err.Error())
assert.Equal(t, "", *accesskeyId)

auth = newEcsRAMRoleCredentialWithEnableIMDSv2("go sdk", true, 180, 0.5, nil)
accesskeyId, err = auth.GetAccessKeyId()
assert.NotNil(t, err)
assert.Equal(t, "Failed to get token from ECS Metadata Service: sdk test", err.Error())
assert.Equal(t, "failed to get token from ECS Metadata Service: sdk test", err.Error())
assert.Equal(t, "", *accesskeyId)

hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
Expand All @@ -171,7 +171,7 @@ func Test_EcsRAmRoleCredentialEnableIMDSv2(t *testing.T) {
}
accesskeyId, err = auth.GetAccessKeyId()
assert.NotNil(t, err)
assert.Equal(t, "Failed to get token from ECS Metadata Service: httpStatus: 300, message = ", err.Error())
assert.Equal(t, "failed to get token from ECS Metadata Service: httpStatus: 300, message = ", err.Error())
assert.Equal(t, "", *accesskeyId)

hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
Expand All @@ -180,7 +180,7 @@ func Test_EcsRAmRoleCredentialEnableIMDSv2(t *testing.T) {
}
}
auth.RoleName = ""
accesskeyId, err = auth.GetAccessKeyId()
_, err = auth.GetAccessKeyId()
assert.NotNil(t, err)
assert.Equal(t, "refresh Ecs sts token err: httpStatus: 400, message = role", err.Error())

Expand All @@ -189,7 +189,7 @@ func Test_EcsRAmRoleCredentialEnableIMDSv2(t *testing.T) {
return mockResponse(200, `role`, nil)
}
}
accesskeyId, err = auth.GetAccessKeyId()
_, err = auth.GetAccessKeyId()
assert.NotNil(t, err)
assert.Equal(t, "refresh Ecs sts token err: Json Unmarshal fail: invalid character 'r' looking for beginning of value", err.Error())
hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
Expand Down
34 changes: 17 additions & 17 deletions credentials/profile_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,21 @@ func (p *profileProvider) resolve() (*Config, error) {
}
return config, nil
default:
return nil, errors.New("Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair")
return nil, errors.New("invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair")
}
}

func getRSAKeyPair(section *ini.Section) (*Config, error) {
publicKeyId, err := section.GetKey("public_key_id")
if err != nil {
return nil, errors.New("Missing required public_key_id option in profile for rsa_key_pair")
return nil, errors.New("missing required public_key_id option in profile for rsa_key_pair")
}
if publicKeyId.String() == "" {
return nil, errors.New("public_key_id cannot be empty")
}
privateKeyFile, err := section.GetKey("private_key_file")
if err != nil {
return nil, errors.New("Missing required private_key_file option in profile for rsa_key_pair")
return nil, errors.New("missing required private_key_file option in profile for rsa_key_pair")
}
if privateKeyFile.String() == "" {
return nil, errors.New("private_key_file cannot be empty")
Expand Down Expand Up @@ -143,28 +143,28 @@ func getRSAKeyPair(section *ini.Section) (*Config, error) {
func getRAMRoleArn(section *ini.Section) (*Config, error) {
accessKeyId, err := section.GetKey("access_key_id")
if err != nil {
return nil, errors.New("Missing required access_key_id option in profile for ram_role_arn")
return nil, errors.New("missing required access_key_id option in profile for ram_role_arn")
}
if accessKeyId.String() == "" {
return nil, errors.New("access_key_id cannot be empty")
}
accessKeySecret, err := section.GetKey("access_key_secret")
if err != nil {
return nil, errors.New("Missing required access_key_secret option in profile for ram_role_arn")
return nil, errors.New("missing required access_key_secret option in profile for ram_role_arn")
}
if accessKeySecret.String() == "" {
return nil, errors.New("access_key_secret cannot be empty")
}
roleArn, err := section.GetKey("role_arn")
if err != nil {
return nil, errors.New("Missing required role_arn option in profile for ram_role_arn")
return nil, errors.New("missing required role_arn option in profile for ram_role_arn")
}
if roleArn.String() == "" {
return nil, errors.New("role_arn cannot be empty")
}
roleSessionName, err := section.GetKey("role_session_name")
if err != nil {
return nil, errors.New("Missing required role_session_name option in profile for ram_role_arn")
return nil, errors.New("missing required role_session_name option in profile for ram_role_arn")
}
if roleSessionName.String() == "" {
return nil, errors.New("role_session_name cannot be empty")
Expand Down Expand Up @@ -210,7 +210,7 @@ func getEcsRAMRole(section *ini.Section) (*Config, error) {
func getBearerToken(section *ini.Section) (*Config, error) {
bearerToken, err := section.GetKey("bearer_token")
if err != nil {
return nil, errors.New("Missing required bearer_token option in profile for bearer")
return nil, errors.New("missing required bearer_token option in profile for bearer")
}
if bearerToken.String() == "" {
return nil, errors.New("bearer_token cannot be empty")
Expand All @@ -225,21 +225,21 @@ func getBearerToken(section *ini.Section) (*Config, error) {
func getSTS(section *ini.Section) (*Config, error) {
accesskeyid, err := section.GetKey("access_key_id")
if err != nil {
return nil, errors.New("Missing required access_key_id option in profile for sts")
return nil, errors.New("missing required access_key_id option in profile for sts")
}
if accesskeyid.String() == "" {
return nil, errors.New("access_key_id cannot be empty")
}
accessKeySecret, err := section.GetKey("access_key_secret")
if err != nil {
return nil, errors.New("Missing required access_key_secret option in profile for sts")
return nil, errors.New("missing required access_key_secret option in profile for sts")
}
if accessKeySecret.String() == "" {
return nil, errors.New("access_key_secret cannot be empty")
}
securityToken, err := section.GetKey("security_token")
if err != nil {
return nil, errors.New("Missing required security_token option in profile for sts")
return nil, errors.New("missing required security_token option in profile for sts")
}
if securityToken.String() == "" {
return nil, errors.New("security_token cannot be empty")
Expand All @@ -256,14 +256,14 @@ func getSTS(section *ini.Section) (*Config, error) {
func getAccessKey(section *ini.Section) (*Config, error) {
accesskeyid, err := section.GetKey("access_key_id")
if err != nil {
return nil, errors.New("Missing required access_key_id option in profile for access_key")
return nil, errors.New("missing required access_key_id option in profile for access_key")
}
if accesskeyid.String() == "" {
return nil, errors.New("access_key_id cannot be empty")
}
accessKeySecret, err := section.GetKey("access_key_secret")
if err != nil {
return nil, errors.New("Missing required access_key_secret option in profile for access_key")
return nil, errors.New("missing required access_key_secret option in profile for access_key")
}
if accessKeySecret.String() == "" {
return nil, errors.New("access_key_secret cannot be empty")
Expand All @@ -289,7 +289,7 @@ func getType(path, profile string) (*ini.Key, *ini.Section, error) {

value, err := section.GetKey("type")
if err != nil {
return nil, nil, errors.New("Missing required type option " + err.Error())
return nil, nil, errors.New("missing required type option " + err.Error())
}
return value, section, nil
}
Expand All @@ -312,7 +312,7 @@ func getHomePath() string {
func checkDefaultPath() (path string, err error) {
path = getHomePath()
if path == "" {
return "", errors.New("The default credential file path is invalid")
return "", errors.New("the default credential file path is invalid")
}
path = strings.Replace("~/.alibabacloud/credentials", "~", path, 1)
_, err = hookState(os.Stat(path))
Expand All @@ -333,14 +333,14 @@ func setRuntimeToConfig(config *Config, section *ini.Section) error {
if rawConnectTimeout != nil {
connectTimeout, err := rawConnectTimeout.Int()
if err != nil {
return fmt.Errorf("Please set connect_timeout with an int value")
return fmt.Errorf("please set connect_timeout with an int value")
}
config.ConnectTimeout = tea.Int(connectTimeout)
}
if rawTimeout != nil {
timeout, err := rawTimeout.Int()
if err != nil {
return fmt.Errorf("Please set timeout with an int value")
return fmt.Errorf("please set timeout with an int value")
}
config.Timeout = tea.Int(timeout)
}
Expand Down
Loading
Loading