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 (#9)
by zuochao
06:30
created

credentials.*EcsRAMRoleCredential.updateCredential   C

Complexity

Conditions 9

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 25
dl 0
loc 33
rs 6.6666
c 0
b 0
f 0
nop 0
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) GetAccessSecret() (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 (e *EcsRAMRoleCredential) updateCredential() (err error) {
85
	if e.runtime == nil {
86
		e.runtime = new(utils.Runtime)
87
	}
88
	request := request.NewCommonRequest()
89
	request.URL = securityCredURL + e.RoleName
90
	request.Method = "GET"
91
	content, err := doAction(request, e.runtime)
92
	if err != nil {
93
		return fmt.Errorf("refresh Ecs sts token err: %s", err.Error())
94
	}
95
	var resp *ecsRAMRoleResponse
96
	err = json.Unmarshal(content, &resp)
97
	if err != nil {
98
		return fmt.Errorf("refresh Ecs sts token err: Json Unmarshal fail: %s", err.Error())
99
	}
100
	if resp.Code != "Success" {
101
		return fmt.Errorf("refresh Ecs sts token err: Code is not Success")
102
	}
103
	if resp.AccessKeyID == "" || resp.AccessKeySecret == "" || resp.SecurityToken == "" || resp.Expiration == "" {
104
		return fmt.Errorf("refresh Ecs sts token err: AccessKeyID: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", resp.AccessKeyID, resp.AccessKeySecret, resp.SecurityToken, resp.Expiration)
105
	}
106
107
	expirationTime, err := time.Parse("2006-01-02T15:04:05Z", resp.Expiration)
108
	e.lastUpdateTimestamp = time.Now().Unix()
109
	e.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
110
	e.sessionCredential = &sessionCredential{
111
		AccessKeyID:     resp.AccessKeyID,
112
		AccessKeySecret: resp.AccessKeySecret,
113
		SecurityToken:   resp.SecurityToken,
114
	}
115
116
	return
117
}
118