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 ( 47c2ea...25ec51 )
by
unknown
05:34
created

er_getCredentialsProvider   B

Complexity

Conditions 1

Size

Total Lines 103
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 73
nop 1
dl 0
loc 103
rs 7.8836
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package providers
2
3
import (
4
	"os"
5
	"path"
6
	"strings"
7
	"testing"
8
9
	"github.com/aliyun/credentials-go/credentials/internal/utils"
10
	"github.com/stretchr/testify/assert"
11
)
12
13
func TestCLIProfileCredentialsProvider(t *testing.T) {
14
	rollback := utils.Memory("ALIBABA_CLOUD_PROFILE")
15
	defer rollback()
16
17
	b, err := NewCLIProfileCredentialsProviderBuilder().
18
		Build()
19
	assert.Nil(t, err)
20
	assert.Equal(t, "", b.profileName)
21
22
	// get from env
23
	os.Setenv("ALIBABA_CLOUD_PROFILE", "custom_profile")
24
	b, err = NewCLIProfileCredentialsProviderBuilder().
25
		Build()
26
	assert.Nil(t, err)
27
	assert.Equal(t, "custom_profile", b.profileName)
28
29
	b, err = NewCLIProfileCredentialsProviderBuilder().
30
		WithProfileName("profilename").
31
		Build()
32
	assert.Nil(t, err)
33
	assert.Equal(t, "profilename", b.profileName)
34
}
35
36
func Test_configuration(t *testing.T) {
37
	wd, _ := os.Getwd()
38
	_, err := newConfigurationFromPath(path.Join(wd, "fixtures/inexist_cli_config.json"))
39
	assert.NotNil(t, err)
40
	assert.True(t, strings.HasPrefix(err.Error(), "reading aliyun cli config from "))
41
42
	_, err = newConfigurationFromPath(path.Join(wd, "fixtures/invalid_cli_config.json"))
43
	assert.NotNil(t, err)
44
	assert.True(t, strings.HasPrefix(err.Error(), "unmarshal aliyun cli config from "))
45
46
	_, err = newConfigurationFromPath(path.Join(wd, "fixtures/mock_empty_cli_config.json"))
47
	assert.True(t, strings.HasPrefix(err.Error(), "no any configured profiles in "))
48
49
	conf, err := newConfigurationFromPath(path.Join(wd, "fixtures/mock_cli_config.json"))
50
	assert.Nil(t, err)
51
	assert.Equal(t, &configuration{
52
		Current: "default",
53
		Profiles: []*profile{
54
			{
55
				Mode:            "AK",
56
				Name:            "default",
57
				AccessKeyID:     "akid",
58
				AccessKeySecret: "secret",
59
			},
60
			{
61
				Mode:            "AK",
62
				Name:            "jacksontian",
63
				AccessKeyID:     "akid",
64
				AccessKeySecret: "secret",
65
			},
66
		},
67
	}, conf)
68
69
	_, err = conf.getProfile("inexists")
70
	assert.EqualError(t, err, "unable to get profile with 'inexists'")
71
72
	p, err := conf.getProfile("jacksontian")
73
	assert.Nil(t, err)
74
	assert.Equal(t, p.Name, "jacksontian")
75
	assert.Equal(t, p.Mode, "AK")
76
}
77
78
func TestCLIProfileCredentialsProvider_getCredentialsProvider(t *testing.T) {
79
	conf := &configuration{
80
		Current: "AK",
81
		Profiles: []*profile{
82
			{
83
				Mode:            "AK",
84
				Name:            "AK",
85
				AccessKeyID:     "akid",
86
				AccessKeySecret: "secret",
87
			},
88
			{
89
				Mode:            "RamRoleArn",
90
				Name:            "RamRoleArn",
91
				AccessKeyID:     "akid",
92
				AccessKeySecret: "secret",
93
				RoleArn:         "arn",
94
				StsRegion:       "cn-hangzhou",
95
				EnableVpc:       true,
96
				Policy:          "policy",
97
				ExternalId:      "externalId",
98
			},
99
			{
100
				Mode: "RamRoleArn",
101
				Name: "Invalid_RamRoleArn",
102
			},
103
			{
104
				Mode:     "EcsRamRole",
105
				Name:     "EcsRamRole",
106
				RoleName: "rolename",
107
			},
108
			{
109
				Mode:            "OIDC",
110
				Name:            "OIDC",
111
				RoleArn:         "role_arn",
112
				OIDCTokenFile:   "path/to/oidc/file",
113
				OIDCProviderARN: "provider_arn",
114
				StsRegion:       "cn-hangzhou",
115
				EnableVpc:       true,
116
				Policy:          "policy",
117
			},
118
			{
119
				Mode:          "ChainableRamRoleArn",
120
				Name:          "ChainableRamRoleArn",
121
				RoleArn:       "arn",
122
				SourceProfile: "AK",
123
			},
124
			{
125
				Mode:          "ChainableRamRoleArn",
126
				Name:          "ChainableRamRoleArn2",
127
				SourceProfile: "InvalidSource",
128
			},
129
			{
130
				Mode: "Unsupported",
131
				Name: "Unsupported",
132
			},
133
		},
134
	}
135
136
	provider, err := NewCLIProfileCredentialsProviderBuilder().Build()
137
	assert.Nil(t, err)
138
	_, err = provider.getCredentialsProvider(conf, "inexist")
139
	assert.EqualError(t, err, "unable to get profile with 'inexist'")
140
141
	// AK
142
	cp, err := provider.getCredentialsProvider(conf, "AK")
143
	assert.Nil(t, err)
144
	akcp, ok := cp.(*StaticAKCredentialsProvider)
145
	assert.True(t, ok)
146
	cc, err := akcp.GetCredentials()
147
	assert.Nil(t, err)
148
	assert.Equal(t, cc, &Credentials{AccessKeyId: "akid", AccessKeySecret: "secret", SecurityToken: "", ProviderName: "static_ak"})
149
	// RamRoleArn
150
	cp, err = provider.getCredentialsProvider(conf, "RamRoleArn")
151
	assert.Nil(t, err)
152
	_, ok = cp.(*RAMRoleARNCredentialsProvider)
153
	assert.True(t, ok)
154
	// RamRoleArn invalid ak
155
	_, err = provider.getCredentialsProvider(conf, "Invalid_RamRoleArn")
156
	assert.EqualError(t, err, "the access key id is empty")
157
	// EcsRamRole
158
	cp, err = provider.getCredentialsProvider(conf, "EcsRamRole")
159
	assert.Nil(t, err)
160
	_, ok = cp.(*ECSRAMRoleCredentialsProvider)
161
	assert.True(t, ok)
162
	// OIDC
163
	cp, err = provider.getCredentialsProvider(conf, "OIDC")
164
	assert.Nil(t, err)
165
	_, ok = cp.(*OIDCCredentialsProvider)
166
	assert.True(t, ok)
167
168
	// ChainableRamRoleArn
169
	cp, err = provider.getCredentialsProvider(conf, "ChainableRamRoleArn")
170
	assert.Nil(t, err)
171
	_, ok = cp.(*RAMRoleARNCredentialsProvider)
172
	assert.True(t, ok)
173
174
	// ChainableRamRoleArn with invalid source profile
175
	_, err = provider.getCredentialsProvider(conf, "ChainableRamRoleArn2")
176
	assert.EqualError(t, err, "get source profile failed: unable to get profile with 'InvalidSource'")
177
178
	// Unsupported
179
	_, err = provider.getCredentialsProvider(conf, "Unsupported")
180
	assert.EqualError(t, err, "unsupported profile mode 'Unsupported'")
181
}
182
183
func TestCLIProfileCredentialsProvider_GetCredentials(t *testing.T) {
184
	defer func() {
185
		getHomePath = utils.GetHomePath
186
	}()
187
188
	getHomePath = func() string {
189
		return ""
190
	}
191
	provider, err := NewCLIProfileCredentialsProviderBuilder().Build()
192
	assert.Nil(t, err)
193
	_, err = provider.GetCredentials()
194
	assert.EqualError(t, err, "cannot found home dir")
195
196
	getHomePath = func() string {
197
		return "/path/invalid/home/dir"
198
	}
199
	provider, err = NewCLIProfileCredentialsProviderBuilder().Build()
200
	assert.Nil(t, err)
201
	_, err = provider.GetCredentials()
202
	assert.EqualError(t, err, "reading aliyun cli config from '/path/invalid/home/dir/.aliyun/config.json' failed open /path/invalid/home/dir/.aliyun/config.json: no such file or directory")
203
204
	getHomePath = func() string {
205
		wd, _ := os.Getwd()
206
		return path.Join(wd, "fixtures")
207
	}
208
209
	// get credentials by current profile
210
	provider, err = NewCLIProfileCredentialsProviderBuilder().Build()
211
	assert.Nil(t, err)
212
	cc, err := provider.GetCredentials()
213
	assert.Nil(t, err)
214
	assert.Equal(t, &Credentials{AccessKeyId: "akid", AccessKeySecret: "secret", SecurityToken: "", ProviderName: "cli_profile/static_ak"}, cc)
215
216
	provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileName("inexist").Build()
217
	assert.Nil(t, err)
218
	_, err = provider.GetCredentials()
219
	assert.EqualError(t, err, "unable to get profile with 'inexist'")
220
221
	// The get_credentials_error profile is invalid
222
	provider, err = NewCLIProfileCredentialsProviderBuilder().WithProfileName("get_credentials_error").Build()
223
	assert.Nil(t, err)
224
	_, err = provider.GetCredentials()
225
	assert.Contains(t, err.Error(), "InvalidAccessKeyId.NotFound")
226
}
227