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
Push — master ( be5676...789770 )
by
unknown
09:31
created

credentials.checkoutAssumeRamoidc   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 1
dl 0
loc 10
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
	OIDCProviderArn       *string  `json:"oidc_provider_arn"`
41
	OIDCTokenFilePath     *string  `json:"oidc_token"`
42
	RoleArn               *string  `json:"role_arn"`
43
	RoleSessionName       *string  `json:"role_session_name"`
44
	PublicKeyId           *string  `json:"public_key_id"`
45
	RoleName              *string  `json:"role_name"`
46
	SessionExpiration     *int     `json:"session_expiration"`
47
	PrivateKeyFile        *string  `json:"private_key_file"`
48
	BearerToken           *string  `json:"bearer_token"`
49
	SecurityToken         *string  `json:"security_token"`
50
	RoleSessionExpiration *int     `json:"role_session_expiratioon"`
51
	Policy                *string  `json:"policy"`
52
	Host                  *string  `json:"host"`
53
	Timeout               *int     `json:"timeout"`
54
	ConnectTimeout        *int     `json:"connect_timeout"`
55
	Proxy                 *string  `json:"proxy"`
56
	InAdvanceScale        *float64 `json:"inAdvanceScale"`
57
	Url                   *string  `json:"url"`
58
}
59
60
func (s Config) String() string {
61
	return tea.Prettify(s)
62
}
63
64
func (s Config) GoString() string {
65
	return s.String()
66
}
67
68
func (s *Config) SetAccessKeyId(v string) *Config {
69
	s.AccessKeyId = &v
70
	return s
71
}
72
73
func (s *Config) SetAccessKeySecret(v string) *Config {
74
	s.AccessKeySecret = &v
75
	return s
76
}
77
78
func (s *Config) SetSecurityToken(v string) *Config {
79
	s.SecurityToken = &v
80
	return s
81
}
82
83
func (s *Config) SetRoleArn(v string) *Config {
84
	s.RoleArn = &v
85
	return s
86
}
87
88
func (s *Config) SetRoleSessionName(v string) *Config {
89
	s.RoleSessionName = &v
90
	return s
91
}
92
93
func (s *Config) SetPublicKeyId(v string) *Config {
94
	s.PublicKeyId = &v
95
	return s
96
}
97
98
func (s *Config) SetRoleName(v string) *Config {
99
	s.RoleName = &v
100
	return s
101
}
102
103
func (s *Config) SetSessionExpiration(v int) *Config {
104
	s.SessionExpiration = &v
105
	return s
106
}
107
108
func (s *Config) SetPrivateKeyFile(v string) *Config {
109
	s.PrivateKeyFile = &v
110
	return s
111
}
112
113
func (s *Config) SetBearerToken(v string) *Config {
114
	s.BearerToken = &v
115
	return s
116
}
117
118
func (s *Config) SetRoleSessionExpiration(v int) *Config {
119
	s.RoleSessionExpiration = &v
120
	return s
121
}
122
123
func (s *Config) SetPolicy(v string) *Config {
124
	s.Policy = &v
125
	return s
126
}
127
128
func (s *Config) SetHost(v string) *Config {
129
	s.Host = &v
130
	return s
131
}
132
133
func (s *Config) SetTimeout(v int) *Config {
134
	s.Timeout = &v
135
	return s
136
}
137
138
func (s *Config) SetConnectTimeout(v int) *Config {
139
	s.ConnectTimeout = &v
140
	return s
141
}
142
143
func (s *Config) SetProxy(v string) *Config {
144
	s.Proxy = &v
145
	return s
146
}
147
148
func (s *Config) SetType(v string) *Config {
149
	s.Type = &v
150
	return s
151
}
152
153
func (s *Config) SetOIDCTokenFilePath(v string) *Config {
154
	s.OIDCTokenFilePath = &v
155
	return s
156
}
157
158
func (s *Config) SetOIDCProviderArn(v string) *Config {
159
	s.OIDCProviderArn = &v
160
	return s
161
}
162
163
func (s *Config) SetURLCredential(v string) *Config {
164
	if v == "" {
165
		v = os.Getenv("ALIBABA_CLOUD_CREDENTIALS_URI")
166
	}
167
	s.Url = &v
168
	return s
169
}
170
171
// NewCredential return a credential according to the type in config.
172
// if config is nil, the function will use default provider chain to get credential.
173
// please see README.md for detail.
174
func NewCredential(config *Config) (credential Credential, err error) {
175
	if config == nil {
176
		config, err = defaultChain.resolve()
177
		if err != nil {
178
			return
179
		}
180
		return NewCredential(config)
181
	}
182
	switch tea.StringValue(config.Type) {
183
	case "credentials_uri":
184
		credential = newURLCredential(tea.StringValue(config.Url))
185
	case "oidc_role_arn":
186
		err = checkoutAssumeRamoidc(config)
187
		if err != nil {
188
			return
189
		}
190
		runtime := &utils.Runtime{
191
			Host:           tea.StringValue(config.Host),
192
			Proxy:          tea.StringValue(config.Proxy),
193
			ReadTimeout:    tea.IntValue(config.Timeout),
194
			ConnectTimeout: tea.IntValue(config.ConnectTimeout),
195
		}
196
		credential = newOIDCRoleArnCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret), tea.StringValue(config.RoleArn), tea.StringValue(config.OIDCProviderArn), tea.StringValue(config.OIDCTokenFilePath), tea.StringValue(config.RoleSessionName), tea.StringValue(config.Policy), tea.IntValue(config.RoleSessionExpiration), runtime)
197
	case "access_key":
198
		err = checkAccessKey(config)
199
		if err != nil {
200
			return
201
		}
202
		credential = newAccessKeyCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret))
203
	case "sts":
204
		err = checkSTS(config)
205
		if err != nil {
206
			return
207
		}
208
		credential = newStsTokenCredential(tea.StringValue(config.AccessKeyId), tea.StringValue(config.AccessKeySecret), tea.StringValue(config.SecurityToken))
209
	case "ecs_ram_role":
210
		checkEcsRAMRole(config)
211
		runtime := &utils.Runtime{
212
			Host:           tea.StringValue(config.Host),
213
			Proxy:          tea.StringValue(config.Proxy),
214
			ReadTimeout:    tea.IntValue(config.Timeout),
215
			ConnectTimeout: tea.IntValue(config.ConnectTimeout),
216
		}
217
		credential = newEcsRAMRoleCredential(tea.StringValue(config.RoleName), tea.Float64Value(config.InAdvanceScale), runtime)
218
	case "ram_role_arn":
219
		err = checkRAMRoleArn(config)
220
		if err != nil {
221
			return
222
		}
223
		runtime := &utils.Runtime{
224
			Host:           tea.StringValue(config.Host),
225
			Proxy:          tea.StringValue(config.Proxy),
226
			ReadTimeout:    tea.IntValue(config.Timeout),
227
			ConnectTimeout: tea.IntValue(config.ConnectTimeout),
228
		}
229
		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)
230
	case "rsa_key_pair":
231
		err = checkRSAKeyPair(config)
232
		if err != nil {
233
			return
234
		}
235
		file, err1 := os.Open(tea.StringValue(config.PrivateKeyFile))
236
		if err1 != nil {
237
			err = fmt.Errorf("InvalidPath: Can not open PrivateKeyFile, err is %s", err1.Error())
238
			return
239
		}
240
		defer file.Close()
241
		var privateKey string
242
		scan := bufio.NewScanner(file)
243
		for scan.Scan() {
244
			if strings.HasPrefix(scan.Text(), "----") {
245
				continue
246
			}
247
			privateKey += scan.Text() + "\n"
248
		}
249
		runtime := &utils.Runtime{
250
			Host:           tea.StringValue(config.Host),
251
			Proxy:          tea.StringValue(config.Proxy),
252
			ReadTimeout:    tea.IntValue(config.Timeout),
253
			ConnectTimeout: tea.IntValue(config.ConnectTimeout),
254
		}
255
		credential = newRsaKeyPairCredential(privateKey, tea.StringValue(config.PublicKeyId), tea.IntValue(config.SessionExpiration), runtime)
256
	case "bearer":
257
		if tea.StringValue(config.BearerToken) == "" {
258
			err = errors.New("BearerToken cannot be empty")
259
			return
260
		}
261
		credential = newBearerTokenCredential(tea.StringValue(config.BearerToken))
262
	default:
263
		err = errors.New("Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair")
264
		return
265
	}
266
	return credential, nil
267
}
268
269
func checkRSAKeyPair(config *Config) (err error) {
270
	if tea.StringValue(config.PrivateKeyFile) == "" {
271
		err = errors.New("PrivateKeyFile cannot be empty")
272
		return
273
	}
274
	if tea.StringValue(config.PublicKeyId) == "" {
275
		err = errors.New("PublicKeyId cannot be empty")
276
		return
277
	}
278
	return
279
}
280
281
func checkoutAssumeRamoidc(config *Config) (err error) {
282
	if tea.StringValue(config.RoleArn) == "" {
283
		err = errors.New("RoleArn cannot be empty")
284
		return
285
	}
286
	if tea.StringValue(config.OIDCProviderArn) == "" {
287
		err = errors.New("OIDCProviderArn cannot be empty")
288
		return
289
	}
290
	return
291
}
292
293
func checkRAMRoleArn(config *Config) (err error) {
294
	if tea.StringValue(config.AccessKeySecret) == "" {
295
		err = errors.New("AccessKeySecret cannot be empty")
296
		return
297
	}
298
	if tea.StringValue(config.RoleArn) == "" {
299
		err = errors.New("RoleArn cannot be empty")
300
		return
301
	}
302
	if tea.StringValue(config.RoleSessionName) == "" {
303
		err = errors.New("RoleSessionName cannot be empty")
304
		return
305
	}
306
	if tea.StringValue(config.AccessKeyId) == "" {
307
		err = errors.New("AccessKeyId cannot be empty")
308
		return
309
	}
310
	return
311
}
312
313
func checkEcsRAMRole(config *Config) (err error) {
314
	return
315
}
316
317
func checkSTS(config *Config) (err error) {
318
	if tea.StringValue(config.AccessKeyId) == "" {
319
		err = errors.New("AccessKeyId cannot be empty")
320
		return
321
	}
322
	if tea.StringValue(config.AccessKeySecret) == "" {
323
		err = errors.New("AccessKeySecret cannot be empty")
324
		return
325
	}
326
	if tea.StringValue(config.SecurityToken) == "" {
327
		err = errors.New("SecurityToken cannot be empty")
328
		return
329
	}
330
	return
331
}
332
333
func checkAccessKey(config *Config) (err error) {
334
	if tea.StringValue(config.AccessKeyId) == "" {
335
		err = errors.New("AccessKeyId cannot be empty")
336
		return
337
	}
338
	if tea.StringValue(config.AccessKeySecret) == "" {
339
		err = errors.New("AccessKeySecret cannot be empty")
340
		return
341
	}
342
	return
343
}
344
345
func doAction(request *request.CommonRequest, runtime *utils.Runtime) (content []byte, err error) {
346
	var urlEncoded string
347
	if request.BodyParams != nil {
348
		urlEncoded = utils.GetURLFormedMap(request.BodyParams)
349
	}
350
	httpRequest, err := http.NewRequest(request.Method, request.URL, strings.NewReader(urlEncoded))
351
	if err != nil {
352
		return
353
	}
354
	httpRequest.Proto = "HTTP/1.1"
355
	httpRequest.Host = request.Domain
356
	debuglog("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto)
357
	debuglog("> Host: %s", httpRequest.Host)
358
	for key, value := range request.Headers {
359
		if value != "" {
360
			debuglog("> %s: %s", key, value)
361
			httpRequest.Header[key] = []string{value}
362
		}
363
	}
364
	debuglog(">")
365
	httpClient := &http.Client{}
366
	httpClient.Timeout = time.Duration(runtime.ReadTimeout) * time.Second
367
	proxy := &url.URL{}
368
	if runtime.Proxy != "" {
369
		proxy, err = url.Parse(runtime.Proxy)
370
		if err != nil {
371
			return
372
		}
373
	}
374
	trans := &http.Transport{}
375
	if proxy != nil && runtime.Proxy != "" {
376
		trans.Proxy = http.ProxyURL(proxy)
377
	}
378
	trans.DialContext = utils.Timeout(time.Duration(runtime.ConnectTimeout) * time.Second)
379
	httpClient.Transport = trans
380
	httpResponse, err := hookDo(httpClient.Do)(httpRequest)
381
	if err != nil {
382
		return
383
	}
384
	debuglog("< %s %s", httpResponse.Proto, httpResponse.Status)
385
	for key, value := range httpResponse.Header {
386
		debuglog("< %s: %v", key, strings.Join(value, ""))
387
	}
388
	debuglog("<")
389
390
	resp := &response.CommonResponse{}
391
	err = hookParse(resp.ParseFromHTTPResponse(httpResponse))
392
	if err != nil {
393
		return
394
	}
395
	debuglog("%s", resp.GetHTTPContentString())
396
	if resp.GetHTTPStatus() != http.StatusOK {
397
		err = fmt.Errorf("httpStatus: %d, message = %s", resp.GetHTTPStatus(), resp.GetHTTPContentString())
398
		return
399
	}
400
	return resp.GetHTTPContentBytes(), nil
401
}
402