GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#14)
by zuochao
05:30
created

credentials.*EcsRAMRoleCredential.updateCredential   C

Complexity

Conditions 11

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 29
dl 0
loc 39
rs 5.4
c 0
b 0
f 0
nop 0

How to fix   Complexity   

Complexity

Complex classes like credentials.*EcsRAMRoleCredential.updateCredential often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package credentials
2
3
import (
4
	"encoding/json"
5
	"fmt"
6
	"time"
7
8
	"github.com/aliyun/credentials-go/credentials/request"
9
	"github.com/aliyun/credentials-go/credentials/utils"
10
)
11
12
var securityCredURL = "http://100.100.100.200/latest/meta-data/ram/security-credentials/"
13
14
// EcsRAMRoleCredential is a kind of credential
15
type EcsRAMRoleCredential struct {
16
	*credentialUpdater
17
	RoleName          string
18
	sessionCredential *sessionCredential
19
	runtime           *utils.Runtime
20
}
21
22
type ecsRAMRoleResponse struct {
23
	Code            string `json:"Code" xml:"Code"`
24
	AccessKeyID     string `json:"AccessKeyID" xml:"AccessKeyID"`
25
	AccessKeySecret string `json:"AccessKeySecret" xml:"AccessKeySecret"`
26
	SecurityToken   string `json:"SecurityToken" xml:"SecurityToken"`
27
	Expiration      string `json:"Expiration" xml:"Expiration"`
28
}
29
30
func newEcsRAMRoleCredential(roleName string, runtime *utils.Runtime) *EcsRAMRoleCredential {
31
	return &EcsRAMRoleCredential{
32
		RoleName:          roleName,
33
		credentialUpdater: new(credentialUpdater),
34
		runtime:           runtime,
35
	}
36
}
37
38
// GetAccessKeyID reutrns  EcsRAMRoleCredential's AccessKeyID
39
// if AccessKeyID is not exist or out of date, the function will update it.
40
func (e *EcsRAMRoleCredential) GetAccessKeyId() (string, error) {
41
	if e.sessionCredential == nil || e.needUpdateCredential() {
42
		err := e.updateCredential()
43
		if err != nil {
44
			return "", err
45
		}
46
	}
47
	return e.sessionCredential.AccessKeyID, nil
48
}
49
50
// GetAccessSecret reutrns  EcsRAMRoleCredential's AccessKeySecret
51
// if AccessKeySecret is not exist or out of date, the function will update it.
52
func (e *EcsRAMRoleCredential) GetAccessKeySecret() (string, error) {
53
	if e.sessionCredential == nil || e.needUpdateCredential() {
54
		err := e.updateCredential()
55
		if err != nil {
56
			return "", err
57
		}
58
	}
59
	return e.sessionCredential.AccessKeySecret, nil
60
}
61
62
// GetSecurityToken reutrns  EcsRAMRoleCredential's SecurityToken
63
// if SecurityToken is not exist or out of date, the function will update it.
64
func (e *EcsRAMRoleCredential) GetSecurityToken() (string, error) {
65
	if e.sessionCredential == nil || e.needUpdateCredential() {
66
		err := e.updateCredential()
67
		if err != nil {
68
			return "", err
69
		}
70
	}
71
	return e.sessionCredential.SecurityToken, nil
72
}
73
74
// GetBearerToken is useless for EcsRAMRoleCredential
75
func (e *EcsRAMRoleCredential) GetBearerToken() string {
76
	return ""
77
}
78
79
// GetType reutrns  EcsRAMRoleCredential's type
80
func (e *EcsRAMRoleCredential) GetType() string {
81
	return "ecs_ram_role"
82
}
83
84
func getRoleName() (string, error) {
85
	runtime := utils.NewRuntime(1, 1, "", "")
86
	request := request.NewCommonRequest()
87
	request.URL = securityCredURL
88
	request.Method = "GET"
89
	content, err := doAction(request, runtime)
90
	if err != nil {
91
		return "", err
92
	}
93
	return string(content), nil
94
}
95
96
func (e *EcsRAMRoleCredential) updateCredential() (err error) {
97
	if e.runtime == nil {
98
		e.runtime = new(utils.Runtime)
99
	}
100
	request := request.NewCommonRequest()
101
	if e.RoleName == "" {
102
		e.RoleName, err = getRoleName()
103
		if err != nil {
104
			return fmt.Errorf("refresh Ecs sts token err: %s", err.Error())
105
		}
106
	}
107
	request.URL = securityCredURL + e.RoleName
108
	request.Method = "GET"
109
	content, err := doAction(request, e.runtime)
110
	if err != nil {
111
		return fmt.Errorf("refresh Ecs sts token err: %s", err.Error())
112
	}
113
	var resp *ecsRAMRoleResponse
114
	err = json.Unmarshal(content, &resp)
115
	if err != nil {
116
		return fmt.Errorf("refresh Ecs sts token err: Json Unmarshal fail: %s", err.Error())
117
	}
118
	if resp.Code != "Success" {
119
		return fmt.Errorf("refresh Ecs sts token err: Code is not Success")
120
	}
121
	if resp.AccessKeyID == "" || resp.AccessKeySecret == "" || resp.SecurityToken == "" || resp.Expiration == "" {
122
		return fmt.Errorf("refresh Ecs sts token err: AccessKeyID: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", resp.AccessKeyID, resp.AccessKeySecret, resp.SecurityToken, resp.Expiration)
123
	}
124
125
	expirationTime, err := time.Parse("2006-01-02T15:04:05Z", resp.Expiration)
126
	e.lastUpdateTimestamp = time.Now().Unix()
127
	e.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
128
	e.sessionCredential = &sessionCredential{
129
		AccessKeyID:     resp.AccessKeyID,
130
		AccessKeySecret: resp.AccessKeySecret,
131
		SecurityToken:   resp.SecurityToken,
132
	}
133
134
	return
135
}
136