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

ccessKeySecret   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
// RAMRoleArnCredential is a kind of credentials
17
type RAMRoleArnCredential struct {
18
	*credentialUpdater
19
	AccessKeyID           string
20
	AccessKeySecret       string
21
	RoleArn               string
22
	RoleSessionName       string
23
	RoleSessionExpiration int
24
	Policy                string
25
	sessionCredential     *sessionCredential
26
	runtime               *utils.Runtime
27
}
28
29
type ramRoleArnResponse struct {
30
	Credentials *credentialsInResponse `json:"Credentials" xml:"Credentials"`
31
}
32
33
type credentialsInResponse struct {
34
	AccessKeyID     string `json:"AccessKeyID" xml:"AccessKeyID"`
35
	AccessKeySecret string `json:"AccessKeySecret" xml:"AccessKeySecret"`
36
	SecurityToken   string `json:"SecurityToken" xml:"SecurityToken"`
37
	Expiration      string `json:"Expiration" xml:"Expiration"`
38
}
39
40
func newRAMRoleArnCredential(accessKeyID, accessKeySecret, roleArn, roleSessionName, policy string, roleSessionExpiration int, runtime *utils.Runtime) *RAMRoleArnCredential {
41
	return &RAMRoleArnCredential{
42
		AccessKeyID:           accessKeyID,
43
		AccessKeySecret:       accessKeySecret,
44
		RoleArn:               roleArn,
45
		RoleSessionName:       roleSessionName,
46
		RoleSessionExpiration: roleSessionExpiration,
47
		Policy:                policy,
48
		credentialUpdater:     new(credentialUpdater),
49
		runtime:               runtime,
50
	}
51
}
52
53
// GetAccessKeyID reutrns RamRoleArnCredential's AccessKeyID
54
// if AccessKeyID is not exist or out of date, the function will update it.
55
func (r *RAMRoleArnCredential) GetAccessKeyId() (string, error) {
56
	if r.sessionCredential == nil || r.needUpdateCredential() {
57
		err := r.updateCredential()
58
		if err != nil {
59
			return "", err
60
		}
61
	}
62
	return r.sessionCredential.AccessKeyID, nil
63
}
64
65
// GetAccessSecret reutrns RamRoleArnCredential's AccessKeySecret
66
// if AccessKeySecret is not exist or out of date, the function will update it.
67
func (r *RAMRoleArnCredential) GetAccessKeySecret() (string, error) {
68
	if r.sessionCredential == nil || r.needUpdateCredential() {
69
		err := r.updateCredential()
70
		if err != nil {
71
			return "", err
72
		}
73
	}
74
	return r.sessionCredential.AccessKeySecret, nil
75
}
76
77
// GetSecurityToken reutrns RamRoleArnCredential's SecurityToken
78
// if SecurityToken is not exist or out of date, the function will update it.
79
func (r *RAMRoleArnCredential) GetSecurityToken() (string, error) {
80
	if r.sessionCredential == nil || r.needUpdateCredential() {
81
		err := r.updateCredential()
82
		if err != nil {
83
			return "", err
84
		}
85
	}
86
	return r.sessionCredential.SecurityToken, nil
87
}
88
89
// GetBearerToken is useless RamRoleArnCredential
90
func (r *RAMRoleArnCredential) GetBearerToken() string {
91
	return ""
92
}
93
94
// GetType reutrns RamRoleArnCredential's type
95
func (r *RAMRoleArnCredential) GetType() string {
96
	return "ram_role_arn"
97
}
98
99
func (r *RAMRoleArnCredential) updateCredential() (err error) {
100
	if r.runtime == nil {
101
		r.runtime = new(utils.Runtime)
102
	}
103
	request := request.NewCommonRequest()
104
	request.Domain = "sts.aliyuncs.com"
105
	request.Scheme = "HTTPS"
106
	request.Method = "GET"
107
	request.QueryParams["AccessKeyId"] = r.AccessKeyID
108
	request.QueryParams["Action"] = "AssumeRole"
109
	request.QueryParams["Format"] = "JSON"
110
	if r.RoleSessionExpiration > 0 {
111
		if r.RoleSessionExpiration >= 900 && r.RoleSessionExpiration <= 3600 {
112
			request.QueryParams["DurationSeconds"] = strconv.Itoa(r.RoleSessionExpiration)
113
		} else {
114
			err = errors.New("[InvalidParam]:Assume Role session duration should be in the range of 15min - 1Hr")
115
			return
116
		}
117
	} else {
118
		request.QueryParams["DurationSeconds"] = strconv.Itoa(defaultDurationSeconds)
119
	}
120
	request.QueryParams["RoleArn"] = r.RoleArn
121
	if r.Policy != "" {
122
		request.QueryParams["Policy"] = r.Policy
123
	}
124
	request.QueryParams["RoleSessionName"] = r.RoleSessionName
125
	request.QueryParams["SignatureMethod"] = "HMAC-SHA1"
126
	request.QueryParams["SignatureVersion"] = "1.0"
127
	request.QueryParams["Version"] = "2015-04-01"
128
	request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601()
129
	request.QueryParams["SignatureNonce"] = utils.GetUUID()
130
	signature := utils.ShaHmac1(request.BuildStringToSign(), r.AccessKeySecret+"&")
131
	request.QueryParams["Signature"] = signature
132
	request.Headers["Host"] = request.Domain
133
	request.Headers["Accept-Encoding"] = "identity"
134
	request.URL = request.BuildURL()
135
	content, err := doAction(request, r.runtime)
136
	if err != nil {
137
		return fmt.Errorf("refresh RoleArn sts token err: %s", err.Error())
138
	}
139
	var resp *ramRoleArnResponse
140
	err = json.Unmarshal(content, &resp)
141
	if err != nil {
142
		return fmt.Errorf("refresh RoleArn sts token err: Json.Unmarshal fail: %s", err.Error())
143
	}
144
	if resp == nil || resp.Credentials == nil {
145
		return fmt.Errorf("refresh RoleArn sts token err: Credentials is empty")
146
	}
147
	respCredentials := resp.Credentials
148
	if respCredentials.AccessKeyID == "" || respCredentials.AccessKeySecret == "" || respCredentials.SecurityToken == "" || respCredentials.Expiration == "" {
149
		return fmt.Errorf("refresh RoleArn sts token err: AccessKeyID: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", respCredentials.AccessKeyID, respCredentials.AccessKeySecret, respCredentials.SecurityToken, respCredentials.Expiration)
150
	}
151
152
	expirationTime, err := time.Parse("2006-01-02T15:04:05Z", respCredentials.Expiration)
153
	r.lastUpdateTimestamp = time.Now().Unix()
154
	r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
155
	r.sessionCredential = &sessionCredential{
156
		AccessKeyID:     respCredentials.AccessKeyID,
157
		AccessKeySecret: respCredentials.AccessKeySecret,
158
		SecurityToken:   respCredentials.SecurityToken,
159
	}
160
161
	return
162
}
163