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 ( 747592...c6841b )
by
unknown
10:06
created

iderBuilder.WithProfileFile   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 providers
2
3
import (
4
	"encoding/json"
5
	"errors"
6
	"fmt"
7
	"io/ioutil"
8
	"os"
9
	"path"
10
	"strings"
11
12
	"github.com/aliyun/credentials-go/credentials/internal/utils"
13
)
14
15
type CLIProfileCredentialsProvider struct {
16
	profileFile   string
17
	profileName   string
18
	innerProvider CredentialsProvider
19
}
20
21
type CLIProfileCredentialsProviderBuilder struct {
22
	provider *CLIProfileCredentialsProvider
23
}
24
25
func (b *CLIProfileCredentialsProviderBuilder) WithProfileFile(profileFile string) *CLIProfileCredentialsProviderBuilder {
26
	b.provider.profileFile = profileFile
27
	return b
28
}
29
30
func (b *CLIProfileCredentialsProviderBuilder) WithProfileName(profileName string) *CLIProfileCredentialsProviderBuilder {
31
	b.provider.profileName = profileName
32
	return b
33
}
34
35
func (b *CLIProfileCredentialsProviderBuilder) Build() (provider *CLIProfileCredentialsProvider, err error) {
36
	// 优先级:
37
	// 1. 使用显示指定的 profileFile
38
	// 2. 使用环境变量(ALIBABA_CLOUD_CONFIG_FILE)指定的 profileFile
39
	// 3. 兜底使用 path.Join(homeDir, ".aliyun/config") 作为 profileFile
40
	if b.provider.profileFile == "" {
41
		b.provider.profileFile = os.Getenv("ALIBABA_CLOUD_CONFIG_FILE")
42
	}
43
	// 优先级:
44
	// 1. 使用显示指定的 profileName
45
	// 2. 使用环境变量(ALIBABA_CLOUD_PROFILE)制定的 profileName
46
	// 3. 使用 CLI 配置中的当前 profileName
47
	if b.provider.profileName == "" {
48
		b.provider.profileName = os.Getenv("ALIBABA_CLOUD_PROFILE")
49
	}
50
51
	if strings.ToLower(os.Getenv("ALIBABA_CLOUD_CLI_PROFILE_DISABLED")) == "true" {
52
		err = errors.New("the CLI profile is disabled")
53
		return
54
	}
55
56
	provider = b.provider
57
	return
58
}
59
60
func NewCLIProfileCredentialsProviderBuilder() *CLIProfileCredentialsProviderBuilder {
61
	return &CLIProfileCredentialsProviderBuilder{
62
		provider: &CLIProfileCredentialsProvider{},
63
	}
64
}
65
66
type profile struct {
67
	Name            string `json:"name"`
68
	Mode            string `json:"mode"`
69
	AccessKeyID     string `json:"access_key_id"`
70
	AccessKeySecret string `json:"access_key_secret"`
71
	RegionID        string `json:"region_id"`
72
	RoleArn         string `json:"ram_role_arn"`
73
	RoleSessionName string `json:"ram_session_name"`
74
	DurationSeconds int    `json:"expired_seconds"`
75
	StsRegion       string `json:"sts_region"`
76
	EnableVpc       bool   `json:"enable_vpc"`
77
	SourceProfile   string `json:"source_profile"`
78
	RoleName        string `json:"ram_role_name"`
79
	OIDCTokenFile   string `json:"oidc_token_file"`
80
	OIDCProviderARN string `json:"oidc_provider_arn"`
81
	Policy          string `json:"policy"`
82
	ExternalId      string `json:"external_id"`
83
}
84
85
type configuration struct {
86
	Current  string     `json:"current"`
87
	Profiles []*profile `json:"profiles"`
88
}
89
90
func newConfigurationFromPath(cfgPath string) (conf *configuration, err error) {
91
	bytes, err := ioutil.ReadFile(cfgPath)
92
	if err != nil {
93
		err = fmt.Errorf("reading aliyun cli config from '%s' failed %v", cfgPath, err)
94
		return
95
	}
96
97
	conf = &configuration{}
98
99
	err = json.Unmarshal(bytes, conf)
100
	if err != nil {
101
		err = fmt.Errorf("unmarshal aliyun cli config from '%s' failed: %s", cfgPath, string(bytes))
102
		return
103
	}
104
105
	if conf.Profiles == nil || len(conf.Profiles) == 0 {
106
		err = fmt.Errorf("no any configured profiles in '%s'", cfgPath)
107
		return
108
	}
109
110
	return
111
}
112
113
func (conf *configuration) getProfile(name string) (profile *profile, err error) {
114
	for _, p := range conf.Profiles {
115
		if p.Name == name {
116
			profile = p
117
			return
118
		}
119
	}
120
121
	err = fmt.Errorf("unable to get profile with '%s'", name)
122
	return
123
}
124
125
func (provider *CLIProfileCredentialsProvider) getCredentialsProvider(conf *configuration, profileName string) (credentialsProvider CredentialsProvider, err error) {
126
	p, err := conf.getProfile(profileName)
127
	if err != nil {
128
		return
129
	}
130
131
	switch p.Mode {
132
	case "AK":
133
		credentialsProvider, err = NewStaticAKCredentialsProviderBuilder().
134
			WithAccessKeyId(p.AccessKeyID).
135
			WithAccessKeySecret(p.AccessKeySecret).
136
			Build()
137
	case "RamRoleArn":
138
		previousProvider, err1 := NewStaticAKCredentialsProviderBuilder().
139
			WithAccessKeyId(p.AccessKeyID).
140
			WithAccessKeySecret(p.AccessKeySecret).
141
			Build()
142
		if err1 != nil {
143
			return nil, err1
144
		}
145
146
		credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder().
147
			WithCredentialsProvider(previousProvider).
148
			WithRoleArn(p.RoleArn).
149
			WithRoleSessionName(p.RoleSessionName).
150
			WithDurationSeconds(p.DurationSeconds).
151
			WithStsRegionId(p.StsRegion).
152
			WithEnableVpc(p.EnableVpc).
153
			WithPolicy(p.Policy).
154
			WithExternalId(p.ExternalId).
155
			Build()
156
	case "EcsRamRole":
157
		credentialsProvider, err = NewECSRAMRoleCredentialsProviderBuilder().WithRoleName(p.RoleName).Build()
158
	case "OIDC":
159
		credentialsProvider, err = NewOIDCCredentialsProviderBuilder().
160
			WithOIDCTokenFilePath(p.OIDCTokenFile).
161
			WithOIDCProviderARN(p.OIDCProviderARN).
162
			WithRoleArn(p.RoleArn).
163
			WithStsRegionId(p.StsRegion).
164
			WithEnableVpc(p.EnableVpc).
165
			WithDurationSeconds(p.DurationSeconds).
166
			WithRoleSessionName(p.RoleSessionName).
167
			WithPolicy(p.Policy).
168
			Build()
169
	case "ChainableRamRoleArn":
170
		previousProvider, err1 := provider.getCredentialsProvider(conf, p.SourceProfile)
171
		if err1 != nil {
172
			err = fmt.Errorf("get source profile failed: %s", err1.Error())
173
			return
174
		}
175
		credentialsProvider, err = NewRAMRoleARNCredentialsProviderBuilder().
176
			WithCredentialsProvider(previousProvider).
177
			WithRoleArn(p.RoleArn).
178
			WithRoleSessionName(p.RoleSessionName).
179
			WithDurationSeconds(p.DurationSeconds).
180
			WithStsRegionId(p.StsRegion).
181
			WithEnableVpc(p.EnableVpc).
182
			WithPolicy(p.Policy).
183
			WithExternalId(p.ExternalId).
184
			Build()
185
	default:
186
		err = fmt.Errorf("unsupported profile mode '%s'", p.Mode)
187
	}
188
189
	return
190
}
191
192
// 默认设置为 GetHomePath,测试时便于 mock
193
var getHomePath = utils.GetHomePath
194
195
func (provider *CLIProfileCredentialsProvider) GetCredentials() (cc *Credentials, err error) {
196
	if provider.innerProvider == nil {
197
		cfgPath := provider.profileFile
198
		if cfgPath == "" {
199
			homeDir := getHomePath()
200
			if homeDir == "" {
201
				err = fmt.Errorf("cannot found home dir")
202
				return
203
			}
204
205
			cfgPath = path.Join(homeDir, ".aliyun/config.json")
206
		}
207
208
		conf, err1 := newConfigurationFromPath(cfgPath)
209
		if err1 != nil {
210
			err = err1
211
			return
212
		}
213
214
		if provider.profileName == "" {
215
			provider.profileName = conf.Current
216
		}
217
218
		provider.innerProvider, err = provider.getCredentialsProvider(conf, provider.profileName)
219
		if err != nil {
220
			return
221
		}
222
	}
223
224
	innerCC, err := provider.innerProvider.GetCredentials()
225
	if err != nil {
226
		return
227
	}
228
229
	providerName := innerCC.ProviderName
230
	if providerName == "" {
231
		providerName = provider.innerProvider.GetProviderName()
232
	}
233
234
	cc = &Credentials{
235
		AccessKeyId:     innerCC.AccessKeyId,
236
		AccessKeySecret: innerCC.AccessKeySecret,
237
		SecurityToken:   innerCC.SecurityToken,
238
		ProviderName:    fmt.Sprintf("%s/%s", provider.GetProviderName(), providerName),
239
	}
240
241
	return
242
}
243
244
func (provider *CLIProfileCredentialsProvider) GetProviderName() string {
245
	return "cli_profile"
246
}
247