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 (#25)
by zuochao
08:24
created

credentials.*Config.SetConnectTimeout   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
package credentials
2
3
import (
4
	"bufio"
5
	"errors"
6
	"fmt"
7
	"net/http"
8
	"net/url"
9
	"os"
10
	"strings"
11
	"time"
12
13
	"github.com/alibabacloud-go/debug/debug"
14
	"github.com/alibabacloud-go/tea/tea"
15
	"github.com/aliyun/credentials-go/credentials/request"
16
	"github.com/aliyun/credentials-go/credentials/response"
17
	"github.com/aliyun/credentials-go/credentials/utils"
18
)
19
20
var debuglog = debug.Init("credential")
21
22
var hookParse = func(err error) error {
23
	return err
24
}
25
26
// Credential is an interface for getting actual credential
27
type Credential interface {
28
	GetAccessKeyId() (*string, error)
29
	GetAccessKeySecret() (*string, error)
30
	GetSecurityToken() (*string, error)
31
	GetBearerToken() *string
32
	GetType() *string
33
}
34
35
// Config is important when call NewCredential
36
type Config struct {
37
	Type                  *string `json:"type"`
38
	AccessKeyId           *string `json:"access_key_id"`
39
	AccessKeySecret       *string `json:"access_key_secret"`
40
	RoleArn               *string `json:"role_arn"`
41
	RoleSessionName       *string `json:"role_session_name"`
42
	PublicKeyId           *string `json:"public_key_id"`
43
	RoleName              *string `json:"role_name"`
44
	SessionExpiration     *int    `json:"session_expiration"`
45
	PrivateKeyFile        *string `json:"private_key_file"`
46
	BearerToken           *string `json:"bearer_token"`
47
	SecurityToken         *string `json:"security_token"`
48
	RoleSessionExpiration *int    `json:"role_session_expiratioon"`
49
	Policy                *string `json:"policy"`
50
	Host                  *string `json:"host"`
51
	Timeout               *int    `json:"timeout"`
52
	ConnectTimeout        *int    `json:"connect_timeout"`
53
	Proxy                 *string `json:"proxy"`
54
}
55
56
func (s Config) String() string {
57
	return tea.Prettify(s)
58
}
59
60
func (s Config) GoString() string {
61
	return s.String()
62
}
63
64
func (s *Config) SetAccessKeyId(v string) *Config {
65
	s.AccessKeyId = &v
66
	return s
67
}
68
69
func (s *Config) SetAccessKeySecret(v string) *Config {
70
	s.AccessKeySecret = &v
71
	return s
72
}
73
74
func (s *Config) SetSecurityToken(v string) *Config {
75
	s.SecurityToken = &v
76
	return s
77
}
78
79
func (s *Config) SetRoleArn(v string) *Config {
80
	s.RoleArn = &v
81
	return s
82
}
83
84
func (s *Config) SetRoleSessionName(v string) *Config {
85
	s.RoleSessionName = &v
86
	return s
87
}
88
89
func (s *Config) SetPublicKeyId(v string) *Config {
90
	s.PublicKeyId = &v
91
	return s
92
}
93
94
func (s *Config) SetRoleName(v string) *Config {
95
	s.RoleName = &v
96
	return s
97
}
98
99
func (s *Config) SetSessionExpiration(v int) *Config {
100
	s.SessionExpiration = &v
101
	return s
102
}
103
104
func (s *Config) SetPrivateKeyFile(v string) *Config {
105
	s.PrivateKeyFile = &v
106
	return s
107
}
108
109
func (s *Config) SetBearerToken(v string) *Config {
110
	s.BearerToken = &v
111
	return s
112
}
113
114
func (s *Config) SetRoleSessionExpiration(v int) *Config {
115
	s.RoleSessionExpiration = &v
116
	return s
117
}
118
119
func (s *Config) SetPolicy(v string) *Config {
120
	s.Policy = &v
121
	return s
122
}
123
124
func (s *Config) SetHost(v string) *Config {
125
	s.Host = &v
126
	return s
127
}
128
129
func (s *Config) SetTimeout(v int) *Config {
130
	s.Timeout = &v
131
	return s
132
}
133
134
func (s *Config) SetConnectTimeout(v int) *Config {
135
	s.ConnectTimeout = &v
136
	return s
137
}
138
139
func (s *Config) SetProxy(v string) *Config {
140
	s.Proxy = &v
141
	return s
142
}
143
144
func (s *Config) SetType(v string) *Config {
145
	s.Type = &v
146
	return s
147
}
148
149
// NewCredential return a credential according to the type in config.
150
// if config is nil, the function will use default provider chain to get credential.
151
// please see README.md for detail.
152
func NewCredential(config *Config) (credential Credential, err error) {
153
	if config == nil {
154
		config, err = defaultChain.resolve()
155
		if err != nil {
156
			return
157
		}
158
		return NewCredential(config)
159
	}
160
	switch tea.StringValue(config.Type) {
161
	case "access_key":
162
		err = checkAccessKey(config)
163
		if err != nil {
164
			return
165
		}
166
		credential = newAccessKeyCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret))
167
	case "sts":
168
		err = checkSTS(config)
169
		if err != nil {
170
			return
171
		}
172
		credential = newStsTokenCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret), tea.StringValue(config.SecurityToken))
173
	case "ecs_ram_role":
174
		checkEcsRAMRole(config)
175
		runtime := &utils.Runtime{
176
			Host:           tea.StringValue(config.Host),
177
			Proxy:          tea.StringValue(config.Proxy),
178
			ReadTimeout:    tea.IntValue(config.Timeout),
179
			ConnectTimeout: tea.IntValue(config.ConnectTimeout),
180
		}
181
		credential = newEcsRAMRoleCredential(tea.StringValue(config.RoleName), runtime)
182
	case "ram_role_arn":
183
		err = checkRAMRoleArn(config)
184
		if err != nil {
185
			return
186
		}
187
		runtime := &utils.Runtime{
188
			Host:           tea.StringValue(config.Host),
189
			Proxy:          tea.StringValue(config.Proxy),
190
			ReadTimeout:    tea.IntValue(config.Timeout),
191
			ConnectTimeout: tea.IntValue(config.ConnectTimeout),
192
		}
193
		credential = newRAMRoleArnCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret), tea.StringValue(config.RoleArn), tea.StringValue(config.RoleSessionName), tea.StringValue(config.Policy), tea.IntValue(config.RoleSessionExpiration), runtime)
194
	case "rsa_key_pair":
195
		err = checkRSAKeyPair(config)
196
		if err != nil {
197
			return
198
		}
199
		file, err1 := os.Open(tea.StringValue(config.PrivateKeyFile))
200
		if err1 != nil {
201
			err = fmt.Errorf("InvalidPath: Can not open PrivateKeyFile, err is %s", err1.Error())
202
			return
203
		}
204
		defer file.Close()
205
		var privateKey string
206
		scan := bufio.NewScanner(file)
207
		for scan.Scan() {
208
			if strings.HasPrefix(scan.Text(), "----") {
209
				continue
210
			}
211
			privateKey += scan.Text() + "\n"
212
		}
213
		runtime := &utils.Runtime{
214
			Host:           tea.StringValue(config.Host),
215
			Proxy:          tea.StringValue(config.Proxy),
216
			ReadTimeout:    tea.IntValue(config.Timeout),
217
			ConnectTimeout: tea.IntValue(config.ConnectTimeout),
218
		}
219
		credential = newRsaKeyPairCredential(privateKey, tea.StringValue(config.PublicKeyId), tea.IntValue(config.SessionExpiration), runtime)
220
	case "bearer":
221
		if tea.StringValue(config.BearerToken) == "" {
222
			err = errors.New("BearerToken cannot be empty")
223
			return
224
		}
225
		credential = newBearerTokenCredential(tea.StringValue(config.BearerToken))
226
	default:
227
		err = errors.New("Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair")
228
		return
229
	}
230
	return credential, nil
231
}
232
233
func checkRSAKeyPair(config *Config) (err error) {
234
	if tea.StringValue(config.PrivateKeyFile) == "" {
235
		err = errors.New("PrivateKeyFile cannot be empty")
236
		return
237
	}
238
	if tea.StringValue(config.PublicKeyId) == "" {
239
		err = errors.New("PublicKeyId cannot be empty")
240
		return
241
	}
242
	return
243
}
244
245
func checkRAMRoleArn(config *Config) (err error) {
246
	if tea.StringValue(config.AccessKeySecret) == "" {
247
		err = errors.New("AccessKeySecret cannot be empty")
248
		return
249
	}
250
	if tea.StringValue(config.RoleArn) == "" {
251
		err = errors.New("RoleArn cannot be empty")
252
		return
253
	}
254
	if tea.StringValue(config.RoleSessionName) == "" {
255
		err = errors.New("RoleSessionName cannot be empty")
256
		return
257
	}
258
	if tea.StringValue(config.AccessKeyId) == "" {
259
		err = errors.New("AccessKeyId cannot be empty")
260
		return
261
	}
262
	return
263
}
264
265
func checkEcsRAMRole(config *Config) (err error) {
266
	return
267
}
268
269
func checkSTS(config *Config) (err error) {
270
	if tea.StringValue(config.AccessKeyId) == "" {
271
		err = errors.New("AccessKeyId cannot be empty")
272
		return
273
	}
274
	if tea.StringValue(config.AccessKeySecret) == "" {
275
		err = errors.New("AccessKeySecret cannot be empty")
276
		return
277
	}
278
	if tea.StringValue(config.SecurityToken) == "" {
279
		err = errors.New("SecurityToken cannot be empty")
280
		return
281
	}
282
	return
283
}
284
285
func checkAccessKey(config *Config) (err error) {
286
	if tea.StringValue(config.AccessKeyId) == "" {
287
		err = errors.New("AccessKeyId cannot be empty")
288
		return
289
	}
290
	if tea.StringValue(config.AccessKeySecret) == "" {
291
		err = errors.New("AccessKeySecret cannot be empty")
292
		return
293
	}
294
	return
295
}
296
297
func doAction(request *request.CommonRequest, runtime *utils.Runtime) (content []byte, err error) {
298
	httpRequest, err := http.NewRequest(request.Method, request.URL, strings.NewReader(""))
299
	if err != nil {
300
		return
301
	}
302
	httpRequest.Proto = "HTTP/1.1"
303
	httpRequest.Host = request.Domain
304
	debuglog("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto)
305
	debuglog("> Host: %s", httpRequest.Host)
306
	for key, value := range request.Headers {
307
		if value != "" {
308
			debuglog("> %s: %s", key, value)
309
			httpRequest.Header[key] = []string{value}
310
		}
311
	}
312
	debuglog(">")
313
	httpClient := &http.Client{}
314
	httpClient.Timeout = time.Duration(runtime.ReadTimeout) * time.Second
315
	proxy := &url.URL{}
316
	if runtime.Proxy != "" {
317
		proxy, err = url.Parse(runtime.Proxy)
318
		if err != nil {
319
			return
320
		}
321
	}
322
	trans := &http.Transport{}
323
	if proxy != nil && runtime.Proxy != "" {
324
		trans.Proxy = http.ProxyURL(proxy)
325
	}
326
	trans.DialContext = utils.Timeout(time.Duration(runtime.ConnectTimeout) * time.Second)
327
	httpClient.Transport = trans
328
	httpResponse, err := hookDo(httpClient.Do)(httpRequest)
329
	if err != nil {
330
		return
331
	}
332
	debuglog("< %s %s", httpResponse.Proto, httpResponse.Status)
333
	for key, value := range httpResponse.Header {
334
		debuglog("< %s: %v", key, strings.Join(value, ""))
335
	}
336
	debuglog("<")
337
338
	resp := &response.CommonResponse{}
339
	err = hookParse(resp.ParseFromHTTPResponse(httpResponse))
340
	if err != nil {
341
		return
342
	}
343
	debuglog("%s", resp.GetHTTPContentString())
344
	if resp.GetHTTPStatus() != http.StatusOK {
345
		err = fmt.Errorf("httpStatus: %d, message = %s", resp.GetHTTPStatus(), resp.GetHTTPContentString())
346
		return
347
	}
348
	return resp.GetHTTPContentBytes(), nil
349
}
350