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
Push — master ( b48a48...6b468f )
by zuochao
13s queued 10s
created

credentials.getRAMRoleArn   D

Complexity

Conditions 12

Size

Total Lines 50
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 38
dl 0
loc 50
rs 4.8
c 0
b 0
f 0
nop 1

How to fix   Complexity   

Complexity

Complex classes like credentials.getRAMRoleArn often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package credentials
2
3
import (
4
	"errors"
5
	"fmt"
6
	"os"
7
	"runtime"
8
	"strings"
9
10
	ini "gopkg.in/ini.v1"
11
)
12
13
type profileProvider struct {
14
	Profile string
15
}
16
17
var providerProfile = newProfileProvider()
18
19
var hookOS = func(goos string) string {
20
	return goos
21
}
22
23
var hookState = func(info os.FileInfo, err error) (os.FileInfo, error) {
24
	return info, err
25
}
26
27
// NewProfileProvider receive zero or more parameters,
28
// when length of name is 0, the value of field Profile will be "default",
29
// and when there are multiple inputs, the function will take the
30
// first one and  discard the other values.
31
func newProfileProvider(name ...string) Provider {
32
	p := new(profileProvider)
33
	if len(name) == 0 {
34
		p.Profile = "default"
35
	} else {
36
		p.Profile = name[0]
37
	}
38
	return p
39
}
40
41
// resolve implements the Provider interface
42
// when credential type is rsa_key_pair, the content of private_key file
43
// must be able to be parsed directly into the required string
44
// that NewRsaKeyPairCredential function needed
45
func (p *profileProvider) resolve() (*Configuration, error) {
46
	path, ok := os.LookupEnv(ENVCredentialFile)
47
	if !ok {
48
		path, err := checkDefaultPath()
49
		if err != nil {
50
			return nil, err
51
		}
52
		if path == "" {
53
			return nil, nil
54
		}
55
	} else if path == "" {
56
		return nil, errors.New(ENVCredentialFile + " cannot be empty")
57
	}
58
59
	value, section, err := getType(path, p.Profile)
60
	if err != nil {
61
		return nil, err
62
	}
63
	switch value.String() {
64
	case "access_key":
65
		config, err := getAccessKey(section)
66
		if err != nil {
67
			return nil, err
68
		}
69
		return config, nil
70
	case "sts":
71
		config, err := getSTS(section)
72
		if err != nil {
73
			return nil, err
74
		}
75
		return config, nil
76
	case "bearer":
77
		config, err := getBearerToken(section)
78
		if err != nil {
79
			return nil, err
80
		}
81
		return config, nil
82
	case "ecs_ram_role":
83
		config, err := getEcsRAMRole(section)
84
		if err != nil {
85
			return nil, err
86
		}
87
		return config, nil
88
	case "ram_role_arn":
89
		config, err := getRAMRoleArn(section)
90
		if err != nil {
91
			return nil, err
92
		}
93
		return config, nil
94
	case "rsa_key_pair":
95
		config, err := getRSAKeyPair(section)
96
		if err != nil {
97
			return nil, err
98
		}
99
		return config, nil
100
	default:
101
		return nil, errors.New("Invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair")
102
	}
103
}
104
105
func getRSAKeyPair(section *ini.Section) (*Configuration, error) {
106
	publicKeyID, err := section.GetKey("public_key_id")
107
	if err != nil {
108
		return nil, errors.New("Missing required public_key_id option in profile for rsa_key_pair")
109
	}
110
	if publicKeyID.String() == "" {
111
		return nil, errors.New("public_key_id cannot be empty")
112
	}
113
	privateKeyFile, err := section.GetKey("private_key_file")
114
	if err != nil {
115
		return nil, errors.New("Missing required private_key_file option in profile for rsa_key_pair")
116
	}
117
	if privateKeyFile.String() == "" {
118
		return nil, errors.New("private_key_file cannot be empty")
119
	}
120
	sessionExpiration, _ := section.GetKey("session_expiration")
121
	expiration := 0
122
	if sessionExpiration != nil {
123
		expiration, err = sessionExpiration.Int()
124
		if err != nil {
125
			return nil, errors.New("session_expiration must be an int")
126
		}
127
	}
128
	config := &Configuration{
129
		Type:              "rsa_key_pair",
130
		PublicKeyID:       publicKeyID.String(),
131
		PrivateKeyFile:    privateKeyFile.String(),
132
		SessionExpiration: expiration,
133
	}
134
	err = setRuntimeToConfig(config, section)
135
	if err != nil {
136
		return nil, err
137
	}
138
	return config, nil
139
}
140
141
func getRAMRoleArn(section *ini.Section) (*Configuration, error) {
142
	accessKeyID, err := section.GetKey("access_key_id")
143
	if err != nil {
144
		return nil, errors.New("Missing required access_key_id option in profile for ram_role_arn")
145
	}
146
	if accessKeyID.String() == "" {
147
		return nil, errors.New("access_key_id cannot be empty")
148
	}
149
	accessKeySecret, err := section.GetKey("access_key_secret")
150
	if err != nil {
151
		return nil, errors.New("Missing required access_key_secret option in profile for ram_role_arn")
152
	}
153
	if accessKeySecret.String() == "" {
154
		return nil, errors.New("access_key_secret cannot be empty")
155
	}
156
	roleArn, err := section.GetKey("role_arn")
157
	if err != nil {
158
		return nil, errors.New("Missing required role_arn option in profile for ram_role_arn")
159
	}
160
	if roleArn.String() == "" {
161
		return nil, errors.New("role_arn cannot be empty")
162
	}
163
	roleSessionName, err := section.GetKey("role_session_name")
164
	if err != nil {
165
		return nil, errors.New("Missing required role_session_name option in profile for ram_role_arn")
166
	}
167
	if roleSessionName.String() == "" {
168
		return nil, errors.New("role_session_name cannot be empty")
169
	}
170
	roleSessionExpiration, _ := section.GetKey("role_session_expiration")
171
	expiration := 0
172
	if roleSessionExpiration != nil {
173
		expiration, err = roleSessionExpiration.Int()
174
		if err != nil {
175
			return nil, errors.New("role_session_expiration must be an int")
176
		}
177
	}
178
	config := &Configuration{
179
		Type:                  "ram_role_arn",
180
		AccessKeyID:           accessKeyID.String(),
181
		AccessKeySecret:       accessKeySecret.String(),
182
		RoleArn:               roleArn.String(),
183
		RoleSessionName:       roleSessionName.String(),
184
		RoleSessionExpiration: expiration,
185
	}
186
	err = setRuntimeToConfig(config, section)
187
	if err != nil {
188
		return nil, err
189
	}
190
	return config, nil
191
}
192
193
func getEcsRAMRole(section *ini.Section) (*Configuration, error) {
194
	roleName, err := section.GetKey("role_name")
195
	if err != nil {
196
		return nil, errors.New("Missing required role_name option in profile for ecs_ram_role")
197
	}
198
	if roleName.String() == "" {
199
		return nil, errors.New("role_name cannot be empty")
200
	}
201
	config := &Configuration{
202
		Type:     "ecs_ram_role",
203
		RoleName: roleName.String(),
204
	}
205
	err = setRuntimeToConfig(config, section)
206
	if err != nil {
207
		return nil, err
208
	}
209
	return config, nil
210
}
211
212
func getBearerToken(section *ini.Section) (*Configuration, error) {
213
	bearerToken, err := section.GetKey("bearer_token")
214
	if err != nil {
215
		return nil, errors.New("Missing required bearer_token option in profile for bearer")
216
	}
217
	if bearerToken.String() == "" {
218
		return nil, errors.New("bearer_token cannot be empty")
219
	}
220
	config := &Configuration{
221
		Type:        "bearer",
222
		BearerToken: bearerToken.String(),
223
	}
224
	return config, nil
225
}
226
227
func getSTS(section *ini.Section) (*Configuration, error) {
228
	accesskeyid, err := section.GetKey("access_key_id")
229
	if err != nil {
230
		return nil, errors.New("Missing required access_key_id option in profile for sts")
231
	}
232
	if accesskeyid.String() == "" {
233
		return nil, errors.New("access_key_id cannot be empty")
234
	}
235
	accessKeySecret, err := section.GetKey("access_key_secret")
236
	if err != nil {
237
		return nil, errors.New("Missing required access_key_secret option in profile for sts")
238
	}
239
	if accessKeySecret.String() == "" {
240
		return nil, errors.New("access_key_secret cannot be empty")
241
	}
242
	securityToken, err := section.GetKey("security_token")
243
	if err != nil {
244
		return nil, errors.New("Missing required security_token option in profile for sts")
245
	}
246
	if securityToken.String() == "" {
247
		return nil, errors.New("security_token cannot be empty")
248
	}
249
	config := &Configuration{
250
		Type:            "sts",
251
		AccessKeyID:     accesskeyid.String(),
252
		AccessKeySecret: accessKeySecret.String(),
253
		SecurityToken:   securityToken.String(),
254
	}
255
	return config, nil
256
}
257
258
func getAccessKey(section *ini.Section) (*Configuration, error) {
259
	accesskeyid, err := section.GetKey("access_key_id")
260
	if err != nil {
261
		return nil, errors.New("Missing required access_key_id option in profile for access_key")
262
	}
263
	if accesskeyid.String() == "" {
264
		return nil, errors.New("access_key_id cannot be empty")
265
	}
266
	accessKeySecret, err := section.GetKey("access_key_secret")
267
	if err != nil {
268
		return nil, errors.New("Missing required access_key_secret option in profile for access_key")
269
	}
270
	if accessKeySecret.String() == "" {
271
		return nil, errors.New("access_key_secret cannot be empty")
272
	}
273
	config := &Configuration{
274
		Type:            "access_key",
275
		AccessKeyID:     accesskeyid.String(),
276
		AccessKeySecret: accessKeySecret.String(),
277
	}
278
	return config, nil
279
}
280
281
func getType(path, profile string) (*ini.Key, *ini.Section, error) {
282
	ini, err := ini.Load(path)
283
	if err != nil {
284
		return nil, nil, errors.New("ERROR: Can not open file " + err.Error())
285
	}
286
287
	section, err := ini.GetSection(profile)
288
	if err != nil {
289
		return nil, nil, errors.New("ERROR: Can not load section " + err.Error())
290
	}
291
292
	value, err := section.GetKey("type")
293
	if err != nil {
294
		return nil, nil, errors.New("Missing required type option " + err.Error())
295
	}
296
	return value, section, nil
297
}
298
299
func getHomePath() string {
300
	if hookOS(runtime.GOOS) == "windows" {
301
		path, ok := os.LookupEnv("USERPROFILE")
302
		if !ok {
303
			return ""
304
		}
305
		return path
306
	}
307
	path, ok := os.LookupEnv("HOME")
308
	if !ok {
309
		return ""
310
	}
311
	return path
312
}
313
314
func checkDefaultPath() (path string, err error) {
315
	path = getHomePath()
316
	if path == "" {
317
		return "", errors.New("The default credential file path is invalid")
318
	}
319
	path = strings.Replace("~/.alibabacloud/credentials", "~", path, 1)
320
	_, err = hookState(os.Stat(path))
321
	if err != nil {
322
		return "", nil
323
	}
324
	return path, nil
325
}
326
327
func setRuntimeToConfig(config *Configuration, section *ini.Section) error {
328
	rawTimeout, _ := section.GetKey("timeout")
329
	rawConnectTimeout, _ := section.GetKey("connect_timeout")
330
	rawProxy, _ := section.GetKey("proxy")
331
	rawHost, _ := section.GetKey("host")
332
	if rawProxy != nil {
333
		config.Proxy = rawProxy.String()
334
	}
335
	if rawConnectTimeout != nil {
336
		connectTimeout, err := rawConnectTimeout.Int()
337
		if err != nil {
338
			return fmt.Errorf("Please set connect_timeout with an int value")
339
		}
340
		config.ConnectTimeout = connectTimeout
341
	}
342
	if rawTimeout != nil {
343
		timeout, err := rawTimeout.Int()
344
		if err != nil {
345
			return fmt.Errorf("Please set timeout with an int value")
346
		}
347
		config.Timeout = timeout
348
	}
349
	if rawHost != nil {
350
		config.Host = rawHost.String()
351
	}
352
	return nil
353
}
354