Conditions | 19 |
Total Lines | 107 |
Code Lines | 77 |
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 providers.*RAMRoleARNCredentialsProvider.getCredentials 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 providers |
||
223 | func (provider *RAMRoleARNCredentialsProvider) getCredentials(cc *Credentials) (session *sessionCredentials, err error) { |
||
224 | method := "POST" |
||
225 | req := &httputil.Request{ |
||
226 | Method: method, |
||
227 | Protocol: "https", |
||
228 | Host: provider.stsEndpoint, |
||
229 | Headers: map[string]string{}, |
||
230 | } |
||
231 | |||
232 | queries := make(map[string]string) |
||
233 | queries["Version"] = "2015-04-01" |
||
234 | queries["Action"] = "AssumeRole" |
||
235 | queries["Format"] = "JSON" |
||
236 | queries["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
237 | queries["SignatureMethod"] = "HMAC-SHA1" |
||
238 | queries["SignatureVersion"] = "1.0" |
||
239 | queries["SignatureNonce"] = utils.GetNonce() |
||
240 | queries["AccessKeyId"] = cc.AccessKeyId |
||
241 | |||
242 | if cc.SecurityToken != "" { |
||
243 | queries["SecurityToken"] = cc.SecurityToken |
||
244 | } |
||
245 | |||
246 | bodyForm := make(map[string]string) |
||
247 | bodyForm["RoleArn"] = provider.roleArn |
||
248 | if provider.policy != "" { |
||
249 | bodyForm["Policy"] = provider.policy |
||
250 | } |
||
251 | if provider.externalId != "" { |
||
252 | bodyForm["ExternalId"] = provider.externalId |
||
253 | } |
||
254 | bodyForm["RoleSessionName"] = provider.roleSessionName |
||
255 | bodyForm["DurationSeconds"] = strconv.Itoa(provider.durationSeconds) |
||
256 | req.Form = bodyForm |
||
257 | |||
258 | // caculate signature |
||
259 | signParams := make(map[string]string) |
||
260 | for key, value := range queries { |
||
261 | signParams[key] = value |
||
262 | } |
||
263 | for key, value := range bodyForm { |
||
264 | signParams[key] = value |
||
265 | } |
||
266 | |||
267 | stringToSign := utils.GetURLFormedMap(signParams) |
||
268 | stringToSign = strings.Replace(stringToSign, "+", "%20", -1) |
||
269 | stringToSign = strings.Replace(stringToSign, "*", "%2A", -1) |
||
270 | stringToSign = strings.Replace(stringToSign, "%7E", "~", -1) |
||
271 | stringToSign = url.QueryEscape(stringToSign) |
||
272 | stringToSign = method + "&%2F&" + stringToSign |
||
273 | secret := cc.AccessKeySecret + "&" |
||
274 | queries["Signature"] = utils.ShaHmac1(stringToSign, secret) |
||
275 | |||
276 | req.Queries = queries |
||
277 | |||
278 | // set headers |
||
279 | req.Headers["Accept-Encoding"] = "identity" |
||
280 | req.Headers["Content-Type"] = "application/x-www-form-urlencoded" |
||
281 | req.Headers["x-acs-credentials-provider"] = cc.ProviderName |
||
282 | |||
283 | connectTimeout := 5 * time.Second |
||
284 | readTimeout := 10 * time.Second |
||
285 | |||
286 | if provider.httpOptions != nil && provider.httpOptions.ConnectTimeout > 0 { |
||
287 | connectTimeout = time.Duration(provider.httpOptions.ConnectTimeout) * time.Millisecond |
||
288 | } |
||
289 | if provider.httpOptions != nil && provider.httpOptions.ReadTimeout > 0 { |
||
290 | readTimeout = time.Duration(provider.httpOptions.ReadTimeout) * time.Millisecond |
||
291 | } |
||
292 | if provider.httpOptions != nil && provider.httpOptions.Proxy != "" { |
||
293 | req.Proxy = provider.httpOptions.Proxy |
||
294 | } |
||
295 | req.ConnectTimeout = connectTimeout |
||
296 | req.ReadTimeout = readTimeout |
||
297 | |||
298 | res, err := httpDo(req) |
||
299 | if err != nil { |
||
300 | return |
||
301 | } |
||
302 | |||
303 | if res.StatusCode != http.StatusOK { |
||
304 | err = errors.New("refresh session token failed: " + string(res.Body)) |
||
305 | return |
||
306 | } |
||
307 | var data assumeRoleResponse |
||
308 | err = json.Unmarshal(res.Body, &data) |
||
309 | if err != nil { |
||
310 | err = fmt.Errorf("refresh RoleArn sts token err, json.Unmarshal fail: %s", err.Error()) |
||
311 | return |
||
312 | } |
||
313 | if data.Credentials == nil { |
||
314 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
315 | return |
||
316 | } |
||
317 | |||
318 | if data.Credentials.AccessKeyId == nil || data.Credentials.AccessKeySecret == nil || data.Credentials.SecurityToken == nil { |
||
319 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
320 | return |
||
321 | } |
||
322 | |||
323 | session = &sessionCredentials{ |
||
324 | AccessKeyId: *data.Credentials.AccessKeyId, |
||
325 | AccessKeySecret: *data.Credentials.AccessKeySecret, |
||
326 | SecurityToken: *data.Credentials.SecurityToken, |
||
327 | Expiration: *data.Credentials.Expiration, |
||
328 | } |
||
329 | return |
||
330 | } |
||
374 |