| Conditions | 25 |
| Total Lines | 153 |
| Code Lines | 125 |
| 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 |
||
| 227 | func NewCredential(config *Config) (credential Credential, err error) { |
||
| 228 | if config == nil { |
||
| 229 | provider := providers.NewDefaultCredentialsProvider() |
||
| 230 | credential = FromCredentialsProvider("default", provider) |
||
| 231 | return |
||
| 232 | } |
||
| 233 | switch tea.StringValue(config.Type) { |
||
| 234 | case "credentials_uri": |
||
| 235 | provider, err := providers.NewURLCredentialsProviderBuilder(). |
||
| 236 | WithUrl(tea.StringValue(config.Url)). |
||
| 237 | WithHttpOptions(&providers.HttpOptions{ |
||
| 238 | Proxy: tea.StringValue(config.Proxy), |
||
| 239 | ReadTimeout: tea.IntValue(config.Timeout), |
||
| 240 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
| 241 | }). |
||
| 242 | Build() |
||
| 243 | |||
| 244 | if err != nil { |
||
| 245 | return nil, err |
||
| 246 | } |
||
| 247 | credential = FromCredentialsProvider("credentials_uri", provider) |
||
| 248 | case "oidc_role_arn": |
||
| 249 | provider, err := providers.NewOIDCCredentialsProviderBuilder(). |
||
| 250 | WithRoleArn(tea.StringValue(config.RoleArn)). |
||
| 251 | WithOIDCTokenFilePath(tea.StringValue(config.OIDCTokenFilePath)). |
||
| 252 | WithOIDCProviderARN(tea.StringValue(config.OIDCProviderArn)). |
||
| 253 | WithDurationSeconds(tea.IntValue(config.RoleSessionExpiration)). |
||
| 254 | WithPolicy(tea.StringValue(config.Policy)). |
||
| 255 | WithRoleSessionName(tea.StringValue(config.RoleSessionName)). |
||
| 256 | WithSTSEndpoint(tea.StringValue(config.STSEndpoint)). |
||
| 257 | WithHttpOptions(&providers.HttpOptions{ |
||
| 258 | Proxy: tea.StringValue(config.Proxy), |
||
| 259 | ReadTimeout: tea.IntValue(config.Timeout), |
||
| 260 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
| 261 | }). |
||
| 262 | Build() |
||
| 263 | |||
| 264 | if err != nil { |
||
| 265 | return nil, err |
||
| 266 | } |
||
| 267 | credential = FromCredentialsProvider("oidc_role_arn", provider) |
||
| 268 | case "access_key": |
||
| 269 | provider, err := providers.NewStaticAKCredentialsProviderBuilder(). |
||
| 270 | WithAccessKeyId(tea.StringValue(config.AccessKeyId)). |
||
| 271 | WithAccessKeySecret(tea.StringValue(config.AccessKeySecret)). |
||
| 272 | Build() |
||
| 273 | if err != nil { |
||
| 274 | return nil, err |
||
| 275 | } |
||
| 276 | |||
| 277 | credential = FromCredentialsProvider("access_key", provider) |
||
| 278 | case "sts": |
||
| 279 | provider, err := providers.NewStaticSTSCredentialsProviderBuilder(). |
||
| 280 | WithAccessKeyId(tea.StringValue(config.AccessKeyId)). |
||
| 281 | WithAccessKeySecret(tea.StringValue(config.AccessKeySecret)). |
||
| 282 | WithSecurityToken(tea.StringValue(config.SecurityToken)). |
||
| 283 | Build() |
||
| 284 | if err != nil { |
||
| 285 | return nil, err |
||
| 286 | } |
||
| 287 | |||
| 288 | credential = FromCredentialsProvider("sts", provider) |
||
| 289 | case "ecs_ram_role": |
||
| 290 | provider, err := providers.NewECSRAMRoleCredentialsProviderBuilder(). |
||
| 291 | WithRoleName(tea.StringValue(config.RoleName)). |
||
| 292 | WithDisableIMDSv1(tea.BoolValue(config.DisableIMDSv1)). |
||
| 293 | Build() |
||
| 294 | |||
| 295 | if err != nil { |
||
| 296 | return nil, err |
||
| 297 | } |
||
| 298 | |||
| 299 | credential = FromCredentialsProvider("ecs_ram_role", provider) |
||
| 300 | case "ram_role_arn": |
||
| 301 | var credentialsProvider providers.CredentialsProvider |
||
| 302 | if config.SecurityToken != nil && *config.SecurityToken != "" { |
||
| 303 | credentialsProvider, err = providers.NewStaticSTSCredentialsProviderBuilder(). |
||
| 304 | WithAccessKeyId(tea.StringValue(config.AccessKeyId)). |
||
| 305 | WithAccessKeySecret(tea.StringValue(config.AccessKeySecret)). |
||
| 306 | WithSecurityToken(tea.StringValue(config.SecurityToken)). |
||
| 307 | Build() |
||
| 308 | } else { |
||
| 309 | credentialsProvider, err = providers.NewStaticAKCredentialsProviderBuilder(). |
||
| 310 | WithAccessKeyId(tea.StringValue(config.AccessKeyId)). |
||
| 311 | WithAccessKeySecret(tea.StringValue(config.AccessKeySecret)). |
||
| 312 | Build() |
||
| 313 | } |
||
| 314 | |||
| 315 | if err != nil { |
||
| 316 | return nil, err |
||
| 317 | } |
||
| 318 | |||
| 319 | provider, err := providers.NewRAMRoleARNCredentialsProviderBuilder(). |
||
| 320 | WithCredentialsProvider(credentialsProvider). |
||
| 321 | WithRoleArn(tea.StringValue(config.RoleArn)). |
||
| 322 | WithRoleSessionName(tea.StringValue(config.RoleSessionName)). |
||
| 323 | WithPolicy(tea.StringValue(config.Policy)). |
||
| 324 | WithDurationSeconds(tea.IntValue(config.RoleSessionExpiration)). |
||
| 325 | WithExternalId(tea.StringValue(config.ExternalId)). |
||
| 326 | WithStsEndpoint(tea.StringValue(config.STSEndpoint)). |
||
| 327 | WithHttpOptions(&providers.HttpOptions{ |
||
| 328 | Proxy: tea.StringValue(config.Proxy), |
||
| 329 | ReadTimeout: tea.IntValue(config.Timeout), |
||
| 330 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
| 331 | }). |
||
| 332 | Build() |
||
| 333 | if err != nil { |
||
| 334 | return nil, err |
||
| 335 | } |
||
| 336 | |||
| 337 | credential = FromCredentialsProvider("ram_role_arn", provider) |
||
| 338 | case "rsa_key_pair": |
||
| 339 | err = checkRSAKeyPair(config) |
||
| 340 | if err != nil { |
||
| 341 | return |
||
| 342 | } |
||
| 343 | file, err1 := os.Open(tea.StringValue(config.PrivateKeyFile)) |
||
| 344 | if err1 != nil { |
||
| 345 | err = fmt.Errorf("InvalidPath: Can not open PrivateKeyFile, err is %s", err1.Error()) |
||
| 346 | return |
||
| 347 | } |
||
| 348 | defer file.Close() |
||
| 349 | var privateKey string |
||
| 350 | scan := bufio.NewScanner(file) |
||
| 351 | for scan.Scan() { |
||
| 352 | if strings.HasPrefix(scan.Text(), "----") { |
||
| 353 | continue |
||
| 354 | } |
||
| 355 | privateKey += scan.Text() + "\n" |
||
| 356 | } |
||
| 357 | runtime := &utils.Runtime{ |
||
| 358 | Host: tea.StringValue(config.Host), |
||
| 359 | Proxy: tea.StringValue(config.Proxy), |
||
| 360 | ReadTimeout: tea.IntValue(config.Timeout), |
||
| 361 | ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
||
| 362 | STSEndpoint: tea.StringValue(config.STSEndpoint), |
||
| 363 | } |
||
| 364 | credential = newRsaKeyPairCredential( |
||
| 365 | privateKey, |
||
| 366 | tea.StringValue(config.PublicKeyId), |
||
| 367 | tea.IntValue(config.SessionExpiration), |
||
| 368 | runtime) |
||
| 369 | case "bearer": |
||
| 370 | if tea.StringValue(config.BearerToken) == "" { |
||
| 371 | err = errors.New("BearerToken cannot be empty") |
||
| 372 | return |
||
| 373 | } |
||
| 374 | credential = newBearerTokenCredential(tea.StringValue(config.BearerToken)) |
||
| 375 | default: |
||
| 376 | err = errors.New("invalid type option, support: access_key, sts, bearer, ecs_ram_role, ram_role_arn, rsa_key_pair, oidc_role_arn, credentials_uri") |
||
| 377 | return |
||
| 378 | } |
||
| 379 | return credential, nil |
||
| 380 | } |
||
| 519 |