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.
Passed
Pull Request — master (#5)
by
unknown
01:19
created

credentials.newEcsRamRoleCredential   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
nop 2
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
type EcsRamRoleCredential struct {
0 ignored issues
show
introduced by
exported type EcsRamRoleCredential should have comment or be unexported
Loading history...
introduced by
type EcsRamRoleCredential should be EcsRAMRoleCredential
Loading history...
15
	*credentialUpdater
16
	RoleName          string
17
	sessionCredential *sessionCredential
18
	runtime           *utils.Runtime
19
}
20
21
type EcsRamRoleResponse struct {
0 ignored issues
show
introduced by
exported type EcsRamRoleResponse should have comment or be unexported
Loading history...
introduced by
type EcsRamRoleResponse should be EcsRAMRoleResponse
Loading history...
22
	Code            string `json:"Code" xml:"Code"`
23
	AccessKeyID     string `json:"AccessKeyID" xml:"AccessKeyID"`
24
	AccessKeySecret string `json:"AccessKeySecret" xml:"AccessKeySecret"`
25
	SecurityToken   string `json:"SecurityToken" xml:"SecurityToken"`
26
	Expiration      string `json:"Expiration" xml:"Expiration"`
27
}
28
29
func newEcsRamRoleCredential(roleName string, runtime *utils.Runtime) *EcsRamRoleCredential {
0 ignored issues
show
introduced by
func newEcsRamRoleCredential should be newEcsRAMRoleCredential
Loading history...
30
	return &EcsRamRoleCredential{
31
		RoleName:          roleName,
32
		credentialUpdater: new(credentialUpdater),
33
		runtime:           runtime,
34
	}
35
}
36
37
func (e *EcsRamRoleCredential) GetAccessKeyID() (string, error) {
0 ignored issues
show
introduced by
exported method EcsRamRoleCredential.GetAccessKeyID should have comment or be unexported
Loading history...
38
	if e.sessionCredential == nil || e.needUpdateCredential() {
39
		err := e.updateCredential()
40
		if err != nil {
41
			return "", err
42
		}
43
	}
44
	return e.sessionCredential.AccessKeyID, nil
45
}
46
47
func (e *EcsRamRoleCredential) GetAccessSecret() (string, error) {
0 ignored issues
show
introduced by
exported method EcsRamRoleCredential.GetAccessSecret should have comment or be unexported
Loading history...
48
	if e.sessionCredential == nil || e.needUpdateCredential() {
49
		err := e.updateCredential()
50
		if err != nil {
51
			return "", err
52
		}
53
	}
54
	return e.sessionCredential.AccessKeySecret, nil
55
}
56
57
func (e *EcsRamRoleCredential) GetSecurityToken() (string, error) {
0 ignored issues
show
introduced by
exported method EcsRamRoleCredential.GetSecurityToken should have comment or be unexported
Loading history...
58
	if e.sessionCredential == nil || e.needUpdateCredential() {
59
		err := e.updateCredential()
60
		if err != nil {
61
			return "", err
62
		}
63
	}
64
	return e.sessionCredential.SecurityToken, nil
65
}
66
67
func (e *EcsRamRoleCredential) GetBearerToken() string {
0 ignored issues
show
introduced by
exported method EcsRamRoleCredential.GetBearerToken should have comment or be unexported
Loading history...
68
	return ""
69
}
70
71
func (e *EcsRamRoleCredential) GetType() string {
0 ignored issues
show
introduced by
exported method EcsRamRoleCredential.GetType should have comment or be unexported
Loading history...
72
	return "ecs_ram_role"
73
}
74
75
func (e *EcsRamRoleCredential) updateCredential() (err error) {
76
	if e.runtime == nil {
77
		e.runtime = new(utils.Runtime)
78
	}
79
	request := request.NewCommonRequest()
80
	request.Url = securityCredURL + e.RoleName
81
	request.Method = "GET"
82
	content, err := doAction(request, e.runtime)
83
	if err != nil {
84
		return fmt.Errorf("refresh Ecs sts token err: %s", err.Error())
85
	}
86
	var resp *EcsRamRoleResponse
87
	err = json.Unmarshal(content, &resp)
88
	if err != nil {
89
		return fmt.Errorf("refresh Ecs sts token err: Json Unmarshal fail: %s", err.Error())
90
	}
91
	if resp.Code != "Success" {
92
		return fmt.Errorf("refresh Ecs sts token err: Code is not Success")
93
	}
94
	if resp.AccessKeyID == "" || resp.AccessKeySecret == "" || resp.SecurityToken == "" || resp.Expiration == "" {
95
		return fmt.Errorf("refresh Ecs sts token err: AccessKeyID: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", resp.AccessKeyID, resp.AccessKeySecret, resp.SecurityToken, resp.Expiration)
96
	}
97
98
	expirationTime, err := time.Parse("2006-01-02T15:04:05Z", resp.Expiration)
99
	e.lastUpdateTimestamp = time.Now().Unix()
100
	e.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
101
	e.sessionCredential = &sessionCredential{
102
		AccessKeyID:     resp.AccessKeyID,
103
		AccessKeySecret: resp.AccessKeySecret,
104
		SecurityToken:   resp.SecurityToken,
105
	}
106
107
	return
108
}
109