Conditions | 22 |
Total Lines | 133 |
Code Lines | 114 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
Complex classes like credentials.NewCredential 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 |
||
198 | func NewCredential(config *Config) (credential Credential, err error) { |
||
199 | if config == nil { |
||
200 | config, err = defaultChain.resolve() |
||
201 | if err != nil { |
||
202 | return |
||
203 | } |
||
204 | return NewCredential(config) |
||
205 | } |
||
206 | switch tea.StringValue(config.Type) { |
||
207 | case "credentials_uri": |
||
208 | credential = newURLCredential(tea.StringValue(config.Url)) |
||
209 | case "oidc_role_arn": |
||
210 | err = checkoutAssumeRamoidc(config) |
||
211 | if err != nil { |
||
212 | return |
||
213 | } |
||
214 | runtime := &utils.Runtime{ |
||
215 | Host: tea.StringValue(config.Host), |
||
216 | Proxy: tea.StringValue(config.Proxy), |
||
217 | ReadTimeout: tea.IntValue(config.Timeout), |
||
218 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
219 | STSEndpoint: tea.StringValue(config.STSEndpoint), |
||
220 | } |
||
221 | credential, err = newOIDCRoleArnCredential( |
||
222 | tea.StringValue(config.AccessKeyId), |
||
223 | tea.StringValue(config.AccessKeySecret), |
||
224 | tea.StringValue(config.RoleArn), |
||
225 | tea.StringValue(config.OIDCProviderArn), |
||
226 | tea.StringValue(config.OIDCTokenFilePath), |
||
227 | tea.StringValue(config.RoleSessionName), |
||
228 | tea.StringValue(config.Policy), |
||
229 | tea.IntValue(config.RoleSessionExpiration), |
||
230 | runtime) |
||
231 | if err != nil { |
||
232 | return |
||
233 | } |
||
234 | case "access_key": |
||
235 | provider, err := providers.NewStaticAKCredentialsProviderBuilder(). |
||
236 | WithAccessKeyId(tea.StringValue(config.AccessKeyId)). |
||
237 | WithAccessKeySecret(tea.StringValue(config.AccessKeySecret)). |
||
238 | Build() |
||
239 | if err != nil { |
||
240 | return nil, err |
||
241 | } |
||
242 | |||
243 | credential = fromCredentialsProvider("access_key", provider) |
||
244 | case "sts": |
||
245 | provider, err := providers.NewStaticSTSCredentialsProviderBuilder(). |
||
246 | WithAccessKeyId(tea.StringValue(config.AccessKeyId)). |
||
247 | WithAccessKeySecret(tea.StringValue(config.AccessKeySecret)). |
||
248 | WithSecurityToken(tea.StringValue(config.SecurityToken)). |
||
249 | Build() |
||
250 | if err != nil { |
||
251 | return nil, err |
||
252 | } |
||
253 | |||
254 | credential = fromCredentialsProvider("sts", provider) |
||
255 | case "ecs_ram_role": |
||
256 | runtime := &utils.Runtime{ |
||
257 | Host: tea.StringValue(config.Host), |
||
258 | ReadTimeout: tea.IntValue(config.Timeout), |
||
259 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
260 | } |
||
261 | credential = newEcsRAMRoleCredentialWithEnableIMDSv2( |
||
262 | tea.StringValue(config.RoleName), |
||
263 | tea.BoolValue(config.EnableIMDSv2), |
||
264 | tea.IntValue(config.MetadataTokenDuration), |
||
265 | tea.Float64Value(config.InAdvanceScale), |
||
266 | runtime) |
||
267 | case "ram_role_arn": |
||
268 | err = checkRAMRoleArn(config) |
||
269 | if err != nil { |
||
270 | return |
||
271 | } |
||
272 | runtime := &utils.Runtime{ |
||
273 | Host: tea.StringValue(config.Host), |
||
274 | Proxy: tea.StringValue(config.Proxy), |
||
275 | ReadTimeout: tea.IntValue(config.Timeout), |
||
276 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
277 | STSEndpoint: tea.StringValue(config.STSEndpoint), |
||
278 | } |
||
279 | credential = newRAMRoleArnl( |
||
280 | tea.StringValue(config.AccessKeyId), |
||
281 | tea.StringValue(config.AccessKeySecret), |
||
282 | tea.StringValue(config.SecurityToken), |
||
283 | tea.StringValue(config.RoleArn), |
||
284 | tea.StringValue(config.RoleSessionName), |
||
285 | tea.StringValue(config.Policy), |
||
286 | tea.IntValue(config.RoleSessionExpiration), |
||
287 | tea.StringValue(config.ExternalId), |
||
288 | runtime) |
||
289 | case "rsa_key_pair": |
||
290 | err = checkRSAKeyPair(config) |
||
291 | if err != nil { |
||
292 | return |
||
293 | } |
||
294 | file, err1 := os.Open(tea.StringValue(config.PrivateKeyFile)) |
||
295 | if err1 != nil { |
||
296 | err = fmt.Errorf("InvalidPath: Can not open PrivateKeyFile, err is %s", err1.Error()) |
||
297 | return |
||
298 | } |
||
299 | defer file.Close() |
||
300 | var privateKey string |
||
301 | scan := bufio.NewScanner(file) |
||
302 | for scan.Scan() { |
||
303 | if strings.HasPrefix(scan.Text(), "----") { |
||
304 | continue |
||
305 | } |
||
306 | privateKey += scan.Text() + "\n" |
||
307 | } |
||
308 | runtime := &utils.Runtime{ |
||
309 | Host: tea.StringValue(config.Host), |
||
310 | Proxy: tea.StringValue(config.Proxy), |
||
311 | ReadTimeout: tea.IntValue(config.Timeout), |
||
312 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
313 | STSEndpoint: tea.StringValue(config.STSEndpoint), |
||
314 | } |
||
315 | credential = newRsaKeyPairCredential( |
||
316 | privateKey, |
||
317 | tea.StringValue(config.PublicKeyId), |
||
318 | tea.IntValue(config.SessionExpiration), |
||
319 | runtime) |
||
320 | case "bearer": |
||
321 | if tea.StringValue(config.BearerToken) == "" { |
||
322 | err = errors.New("BearerToken cannot be empty") |
||
323 | return |
||
324 | } |
||
325 | credential = newBearerTokenCredential(tea.StringValue(config.BearerToken)) |
||
326 | default: |
||
327 | err = errors.New("invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair") |
||
328 | return |
||
329 | } |
||
330 | return credential, nil |
||
331 | } |
||
500 |