Conditions | 20 |
Total Lines | 118 |
Code Lines | 84 |
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 |
||
158 | func (provider *RAMRoleARNCredentialsProvider) getCredentials(cc *Credentials) (session *sessionCredentials, err error) { |
||
159 | method := "POST" |
||
160 | host := provider.stsEndpoint |
||
161 | queries := make(map[string]string) |
||
162 | queries["Version"] = "2015-04-01" |
||
163 | queries["Action"] = "AssumeRole" |
||
164 | queries["Format"] = "JSON" |
||
165 | queries["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
166 | queries["SignatureMethod"] = "HMAC-SHA1" |
||
167 | queries["SignatureVersion"] = "1.0" |
||
168 | queries["SignatureNonce"] = utils.GetNonce() |
||
169 | queries["AccessKeyId"] = cc.AccessKeyId |
||
170 | if cc.SecurityToken != "" { |
||
171 | queries["SecurityToken"] = cc.SecurityToken |
||
172 | } |
||
173 | |||
174 | bodyForm := make(map[string]string) |
||
175 | bodyForm["RoleArn"] = provider.roleArn |
||
176 | if provider.policy != "" { |
||
177 | bodyForm["Policy"] = provider.policy |
||
178 | } |
||
179 | if provider.externalId != "" { |
||
180 | bodyForm["ExternalId"] = provider.externalId |
||
181 | } |
||
182 | bodyForm["RoleSessionName"] = provider.roleSessionName |
||
183 | bodyForm["DurationSeconds"] = strconv.Itoa(provider.durationSeconds) |
||
184 | |||
185 | // caculate signature |
||
186 | signParams := make(map[string]string) |
||
187 | for key, value := range queries { |
||
188 | signParams[key] = value |
||
189 | } |
||
190 | for key, value := range bodyForm { |
||
191 | signParams[key] = value |
||
192 | } |
||
193 | |||
194 | stringToSign := utils.GetURLFormedMap(signParams) |
||
195 | stringToSign = strings.Replace(stringToSign, "+", "%20", -1) |
||
196 | stringToSign = strings.Replace(stringToSign, "*", "%2A", -1) |
||
197 | stringToSign = strings.Replace(stringToSign, "%7E", "~", -1) |
||
198 | stringToSign = url.QueryEscape(stringToSign) |
||
199 | stringToSign = method + "&%2F&" + stringToSign |
||
200 | secret := cc.AccessKeySecret + "&" |
||
201 | queries["Signature"] = utils.ShaHmac1(stringToSign, secret) |
||
202 | |||
203 | querystring := utils.GetURLFormedMap(queries) |
||
204 | // do request |
||
205 | httpUrl := fmt.Sprintf("https://%s/?%s", host, querystring) |
||
206 | |||
207 | body := utils.GetURLFormedMap(bodyForm) |
||
208 | |||
209 | httpRequest, err := hookNewRequest(http.NewRequest)(method, httpUrl, strings.NewReader(body)) |
||
210 | if err != nil { |
||
211 | return |
||
212 | } |
||
213 | |||
214 | // set headers |
||
215 | httpRequest.Header["Accept-Encoding"] = []string{"identity"} |
||
216 | httpRequest.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"} |
||
217 | httpRequest.Header["x-credentials-provider"] = []string{cc.ProviderName} |
||
218 | httpClient := &http.Client{} |
||
219 | |||
220 | if provider.httpOptions != nil { |
||
221 | httpClient.Timeout = time.Duration(provider.httpOptions.ReadTimeout) * time.Second |
||
222 | proxy := &url.URL{} |
||
223 | if provider.httpOptions.Proxy != "" { |
||
224 | proxy, err = url.Parse(provider.httpOptions.Proxy) |
||
225 | if err != nil { |
||
226 | return |
||
227 | } |
||
228 | } |
||
229 | trans := &http.Transport{} |
||
230 | if proxy != nil && provider.httpOptions.Proxy != "" { |
||
231 | trans.Proxy = http.ProxyURL(proxy) |
||
232 | } |
||
233 | trans.DialContext = utils.Timeout(time.Duration(provider.httpOptions.ConnectTimeout) * time.Second) |
||
234 | httpClient.Transport = trans |
||
235 | } |
||
236 | |||
237 | httpResponse, err := hookDo(httpClient.Do)(httpRequest) |
||
238 | if err != nil { |
||
239 | return |
||
240 | } |
||
241 | |||
242 | defer httpResponse.Body.Close() |
||
243 | |||
244 | responseBody, err := ioutil.ReadAll(httpResponse.Body) |
||
245 | if err != nil { |
||
246 | return |
||
247 | } |
||
248 | |||
249 | if httpResponse.StatusCode != http.StatusOK { |
||
250 | err = errors.New("refresh session token failed: " + string(responseBody)) |
||
251 | return |
||
252 | } |
||
253 | var data assumeRoleResponse |
||
254 | err = json.Unmarshal(responseBody, &data) |
||
255 | if err != nil { |
||
256 | err = fmt.Errorf("refresh RoleArn sts token err, json.Unmarshal fail: %s", err.Error()) |
||
257 | return |
||
258 | } |
||
259 | if data.Credentials == nil { |
||
260 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
261 | return |
||
262 | } |
||
263 | |||
264 | if data.Credentials.AccessKeyId == nil || data.Credentials.AccessKeySecret == nil || data.Credentials.SecurityToken == nil { |
||
265 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
266 | return |
||
267 | } |
||
268 | |||
269 | session = &sessionCredentials{ |
||
270 | AccessKeyId: *data.Credentials.AccessKeyId, |
||
271 | AccessKeySecret: *data.Credentials.AccessKeySecret, |
||
272 | SecurityToken: *data.Credentials.SecurityToken, |
||
273 | Expiration: *data.Credentials.Expiration, |
||
274 | } |
||
275 | return |
||
276 | } |
||
320 |