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.*RamRoleArnCredential.GetAccessKeyID   A

Complexity

Conditions 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
package credentials
2
3
import (
4
	"encoding/json"
5
	"errors"
6
	"fmt"
7
	"strconv"
8
	"time"
9
10
	"github.com/aliyun/credentials-go/credentials/request"
11
	"github.com/aliyun/credentials-go/credentials/utils"
12
)
13
14
const defaultDurationSeconds = 3600
15
16
type RamRoleArnCredential struct {
0 ignored issues
show
introduced by
exported type RamRoleArnCredential should have comment or be unexported
Loading history...
introduced by
type RamRoleArnCredential should be RAMRoleArnCredential
Loading history...
17
	*credentialUpdater
18
	AccessKeyID           string
19
	AccessKeySecret       string
20
	RoleArn               string
21
	RoleSessionName       string
22
	RoleSessionExpiration int
23
	Policy                string
24
	sessionCredential     *sessionCredential
25
	runtime               *utils.Runtime
26
}
27
28
type RamRoleArnResponse struct {
0 ignored issues
show
introduced by
exported type RamRoleArnResponse should have comment or be unexported
Loading history...
introduced by
type RamRoleArnResponse should be RAMRoleArnResponse
Loading history...
29
	Credentials *CredentialsInResponse `json:"Credentials" xml:"Credentials"`
30
}
31
32
type CredentialsInResponse struct {
0 ignored issues
show
introduced by
exported type CredentialsInResponse should have comment or be unexported
Loading history...
introduced by
type name will be used as credentials.CredentialsInResponse by other packages, and that stutters; consider calling this InResponse
Loading history...
33
	AccessKeyID     string `json:"AccessKeyID" xml:"AccessKeyID"`
34
	AccessKeySecret string `json:"AccessKeySecret" xml:"AccessKeySecret"`
35
	SecurityToken   string `json:"SecurityToken" xml:"SecurityToken"`
36
	Expiration      string `json:"Expiration" xml:"Expiration"`
37
}
38
39
func newRamRoleArnCredential(accessKeyID, accessKeySecret, roleArn, roleSessionName, policy string, roleSessionExpiration int, runtime *utils.Runtime) *RamRoleArnCredential {
0 ignored issues
show
introduced by
func newRamRoleArnCredential should be newRAMRoleArnCredential
Loading history...
40
	return &RamRoleArnCredential{
41
		AccessKeyID:           accessKeyID,
42
		AccessKeySecret:       accessKeySecret,
43
		RoleArn:               roleArn,
44
		RoleSessionName:       roleSessionName,
45
		RoleSessionExpiration: roleSessionExpiration,
46
		Policy:                policy,
47
		credentialUpdater:     new(credentialUpdater),
48
		runtime:               runtime,
49
	}
50
}
51
52
func (r *RamRoleArnCredential) GetAccessKeyID() (string, error) {
0 ignored issues
show
introduced by
exported method RamRoleArnCredential.GetAccessKeyID should have comment or be unexported
Loading history...
53
	if r.sessionCredential == nil || r.needUpdateCredential() {
54
		err := r.updateCredential()
55
		if err != nil {
56
			return "", err
57
		}
58
	}
59
	return r.sessionCredential.AccessKeyID, nil
60
}
61
62
func (r *RamRoleArnCredential) GetAccessSecret() (string, error) {
0 ignored issues
show
introduced by
exported method RamRoleArnCredential.GetAccessSecret should have comment or be unexported
Loading history...
63
	if r.sessionCredential == nil || r.needUpdateCredential() {
64
		err := r.updateCredential()
65
		if err != nil {
66
			return "", err
67
		}
68
	}
69
	return r.sessionCredential.AccessKeySecret, nil
70
}
71
72
func (r *RamRoleArnCredential) GetSecurityToken() (string, error) {
0 ignored issues
show
introduced by
exported method RamRoleArnCredential.GetSecurityToken should have comment or be unexported
Loading history...
73
	if r.sessionCredential == nil || r.needUpdateCredential() {
74
		err := r.updateCredential()
75
		if err != nil {
76
			return "", err
77
		}
78
	}
79
	return r.sessionCredential.SecurityToken, nil
80
}
81
82
func (r *RamRoleArnCredential) GetBearerToken() string {
0 ignored issues
show
introduced by
exported method RamRoleArnCredential.GetBearerToken should have comment or be unexported
Loading history...
83
	return ""
84
}
85
86
func (r *RamRoleArnCredential) GetType() string {
0 ignored issues
show
introduced by
exported method RamRoleArnCredential.GetType should have comment or be unexported
Loading history...
87
	return "ram_role_arn"
88
}
89
90
func (r *RamRoleArnCredential) updateCredential() (err error) {
91
	if r.runtime == nil {
92
		r.runtime = new(utils.Runtime)
93
	}
94
	request := request.NewCommonRequest()
95
	request.Domain = "sts.aliyuncs.com"
96
	request.Scheme = "HTTPS"
97
	request.Method = "GET"
98
	request.QueryParams["AccessKeyId"] = r.AccessKeyID
99
	request.QueryParams["Action"] = "AssumeRole"
100
	request.QueryParams["Format"] = "JSON"
101
	if r.RoleSessionExpiration > 0 {
102
		if r.RoleSessionExpiration >= 900 && r.RoleSessionExpiration <= 3600 {
103
			request.QueryParams["DurationSeconds"] = strconv.Itoa(r.RoleSessionExpiration)
104
		} else {
105
			err = errors.New("[InvalidParam]:Assume Role session duration should be in the range of 15min - 1Hr")
106
			return
107
		}
108
	} else {
109
		request.QueryParams["DurationSeconds"] = strconv.Itoa(defaultDurationSeconds)
110
	}
111
	request.QueryParams["RoleArn"] = r.RoleArn
112
	if r.Policy != "" {
113
		request.QueryParams["Policy"] = r.Policy
114
	}
115
	request.QueryParams["RoleSessionName"] = r.RoleSessionName
116
	request.QueryParams["SignatureMethod"] = "HMAC-SHA1"
117
	request.QueryParams["SignatureVersion"] = "1.0"
118
	request.QueryParams["Version"] = "2015-04-01"
119
	request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601()
120
	request.QueryParams["SignatureNonce"] = utils.GetUUID()
121
	signature := utils.ShaHmac1(request.BuildStringToSign(), r.AccessKeySecret+"&")
122
	request.QueryParams["Signature"] = signature
123
	request.Headers["Host"] = request.Domain
124
	request.Headers["Accept-Encoding"] = "identity"
125
	request.Url = request.BuildUrl()
126
	content, err := doAction(request, r.runtime)
127
	if err != nil {
128
		return fmt.Errorf("refresh RoleArn sts token err: %s", err.Error())
129
	}
130
	var resp *RamRoleArnResponse
131
	err = json.Unmarshal(content, &resp)
132
	if err != nil {
133
		return fmt.Errorf("refresh RoleArn sts token err: Json.Unmarshal fail: %s", err.Error())
134
	}
135
	if resp == nil || resp.Credentials == nil {
136
		return fmt.Errorf("refresh RoleArn sts token err: Credentials is empty")
137
	}
138
	respCredentials := resp.Credentials
139
	if respCredentials.AccessKeyID == "" || respCredentials.AccessKeySecret == "" || respCredentials.SecurityToken == "" || respCredentials.Expiration == "" {
140
		return fmt.Errorf("refresh RoleArn sts token err: AccessKeyID: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", respCredentials.AccessKeyID, respCredentials.AccessKeySecret, respCredentials.SecurityToken, respCredentials.Expiration)
141
	}
142
143
	expirationTime, err := time.Parse("2006-01-02T15:04:05Z", respCredentials.Expiration)
144
	r.lastUpdateTimestamp = time.Now().Unix()
145
	r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
146
	r.sessionCredential = &sessionCredential{
147
		AccessKeyID:     respCredentials.AccessKeyID,
148
		AccessKeySecret: respCredentials.AccessKeySecret,
149
		SecurityToken:   respCredentials.SecurityToken,
150
	}
151
152
	return
153
}
154