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