Conditions | 14 |
Total Lines | 98 |
Code Lines | 71 |
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 |
||
161 | func (provider *RAMRoleARNCredentialsProvider) getCredentials(cc *Credentials) (session *sessionCredentials, err error) { |
||
162 | method := "POST" |
||
163 | req := &httputil.Request{ |
||
164 | Method: method, |
||
165 | Protocol: "https", |
||
166 | Host: provider.stsEndpoint, |
||
167 | Headers: map[string]string{}, |
||
168 | } |
||
169 | |||
170 | queries := make(map[string]string) |
||
171 | queries["Version"] = "2015-04-01" |
||
172 | queries["Action"] = "AssumeRole" |
||
173 | queries["Format"] = "JSON" |
||
174 | queries["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
175 | queries["SignatureMethod"] = "HMAC-SHA1" |
||
176 | queries["SignatureVersion"] = "1.0" |
||
177 | queries["SignatureNonce"] = utils.GetNonce() |
||
178 | queries["AccessKeyId"] = cc.AccessKeyId |
||
179 | |||
180 | if cc.SecurityToken != "" { |
||
181 | queries["SecurityToken"] = cc.SecurityToken |
||
182 | } |
||
183 | |||
184 | bodyForm := make(map[string]string) |
||
185 | bodyForm["RoleArn"] = provider.roleArn |
||
186 | if provider.policy != "" { |
||
187 | bodyForm["Policy"] = provider.policy |
||
188 | } |
||
189 | if provider.externalId != "" { |
||
190 | bodyForm["ExternalId"] = provider.externalId |
||
191 | } |
||
192 | bodyForm["RoleSessionName"] = provider.roleSessionName |
||
193 | bodyForm["DurationSeconds"] = strconv.Itoa(provider.durationSeconds) |
||
194 | req.Form = bodyForm |
||
195 | |||
196 | // caculate signature |
||
197 | signParams := make(map[string]string) |
||
198 | for key, value := range queries { |
||
199 | signParams[key] = value |
||
200 | } |
||
201 | for key, value := range bodyForm { |
||
202 | signParams[key] = value |
||
203 | } |
||
204 | |||
205 | stringToSign := utils.GetURLFormedMap(signParams) |
||
206 | stringToSign = strings.Replace(stringToSign, "+", "%20", -1) |
||
207 | stringToSign = strings.Replace(stringToSign, "*", "%2A", -1) |
||
208 | stringToSign = strings.Replace(stringToSign, "%7E", "~", -1) |
||
209 | stringToSign = url.QueryEscape(stringToSign) |
||
210 | stringToSign = method + "&%2F&" + stringToSign |
||
211 | secret := cc.AccessKeySecret + "&" |
||
212 | queries["Signature"] = utils.ShaHmac1(stringToSign, secret) |
||
213 | |||
214 | req.Queries = queries |
||
215 | |||
216 | // set headers |
||
217 | req.Headers["Accept-Encoding"] = "identity" |
||
218 | req.Headers["Content-Type"] = "application/x-www-form-urlencoded" |
||
219 | req.Headers["x-acs-credentials-provider"] = cc.ProviderName |
||
220 | |||
221 | if provider.httpOptions != nil { |
||
222 | req.ConnectTimeout = time.Duration(provider.httpOptions.ConnectTimeout) * time.Second |
||
223 | req.ReadTimeout = time.Duration(provider.httpOptions.ReadTimeout) * time.Second |
||
224 | req.Proxy = provider.httpOptions.Proxy |
||
225 | } |
||
226 | |||
227 | res, err := httpDo(req) |
||
228 | if err != nil { |
||
229 | return |
||
230 | } |
||
231 | |||
232 | if res.StatusCode != http.StatusOK { |
||
233 | err = errors.New("refresh session token failed: " + string(res.Body)) |
||
234 | return |
||
235 | } |
||
236 | var data assumeRoleResponse |
||
237 | err = json.Unmarshal(res.Body, &data) |
||
238 | if err != nil { |
||
239 | err = fmt.Errorf("refresh RoleArn sts token err, json.Unmarshal fail: %s", err.Error()) |
||
240 | return |
||
241 | } |
||
242 | if data.Credentials == nil { |
||
243 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
244 | return |
||
245 | } |
||
246 | |||
247 | if data.Credentials.AccessKeyId == nil || data.Credentials.AccessKeySecret == nil || data.Credentials.SecurityToken == nil { |
||
248 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
249 | return |
||
250 | } |
||
251 | |||
252 | session = &sessionCredentials{ |
||
253 | AccessKeyId: *data.Credentials.AccessKeyId, |
||
254 | AccessKeySecret: *data.Credentials.AccessKeySecret, |
||
255 | SecurityToken: *data.Credentials.SecurityToken, |
||
256 | Expiration: *data.Credentials.Expiration, |
||
257 | } |
||
258 | return |
||
259 | } |
||
303 |