| Conditions | 16 |
| Total Lines | 107 |
| Code Lines | 76 |
| 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 |
||
| 129 | func (provider *RAMRoleARNCredentialsProvider) getCredentials(cc *Credentials) (session *sessionCredentials, err error) { |
||
| 130 | method := "POST" |
||
| 131 | var host string |
||
| 132 | if provider.stsRegion != "" { |
||
| 133 | host = fmt.Sprintf("sts.%s.aliyuncs.com", provider.stsRegion) |
||
| 134 | } else { |
||
| 135 | host = "sts.aliyuncs.com" |
||
| 136 | } |
||
| 137 | |||
| 138 | queries := make(map[string]string) |
||
| 139 | queries["Version"] = "2015-04-01" |
||
| 140 | queries["Action"] = "AssumeRole" |
||
| 141 | queries["Format"] = "JSON" |
||
| 142 | queries["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
| 143 | queries["SignatureMethod"] = "HMAC-SHA1" |
||
| 144 | queries["SignatureVersion"] = "1.0" |
||
| 145 | queries["SignatureNonce"] = utils.GetNonce() |
||
| 146 | queries["AccessKeyId"] = cc.AccessKeyId |
||
| 147 | if cc.SecurityToken != "" { |
||
| 148 | queries["SecurityToken"] = cc.SecurityToken |
||
| 149 | } |
||
| 150 | |||
| 151 | bodyForm := make(map[string]string) |
||
| 152 | bodyForm["RoleArn"] = provider.roleArn |
||
| 153 | if provider.policy != "" { |
||
| 154 | bodyForm["Policy"] = provider.policy |
||
| 155 | } |
||
| 156 | if provider.externalId != "" { |
||
| 157 | bodyForm["ExternalId"] = provider.externalId |
||
| 158 | } |
||
| 159 | bodyForm["RoleSessionName"] = provider.roleSessionName |
||
| 160 | bodyForm["DurationSeconds"] = strconv.Itoa(provider.durationSeconds) |
||
| 161 | |||
| 162 | // caculate signature |
||
| 163 | signParams := make(map[string]string) |
||
| 164 | for key, value := range queries { |
||
| 165 | signParams[key] = value |
||
| 166 | } |
||
| 167 | for key, value := range bodyForm { |
||
| 168 | signParams[key] = value |
||
| 169 | } |
||
| 170 | |||
| 171 | stringToSign := utils.GetURLFormedMap(signParams) |
||
| 172 | stringToSign = strings.Replace(stringToSign, "+", "%20", -1) |
||
| 173 | stringToSign = strings.Replace(stringToSign, "*", "%2A", -1) |
||
| 174 | stringToSign = strings.Replace(stringToSign, "%7E", "~", -1) |
||
| 175 | stringToSign = url.QueryEscape(stringToSign) |
||
| 176 | stringToSign = method + "&%2F&" + stringToSign |
||
| 177 | secret := cc.AccessKeySecret + "&" |
||
| 178 | queries["Signature"] = utils.ShaHmac1(stringToSign, secret) |
||
| 179 | |||
| 180 | querystring := utils.GetURLFormedMap(queries) |
||
| 181 | // do request |
||
| 182 | httpUrl := fmt.Sprintf("https://%s/?%s", host, querystring) |
||
| 183 | |||
| 184 | body := utils.GetURLFormedMap(bodyForm) |
||
| 185 | |||
| 186 | httpRequest, err := hookNewRequest(http.NewRequest)(method, httpUrl, strings.NewReader(body)) |
||
| 187 | if err != nil { |
||
| 188 | return |
||
| 189 | } |
||
| 190 | |||
| 191 | // set headers |
||
| 192 | httpRequest.Header["Accept-Encoding"] = []string{"identity"} |
||
| 193 | httpRequest.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"} |
||
| 194 | httpRequest.Header["x-credentials-provider"] = []string{cc.ProviderName} |
||
| 195 | httpClient := &http.Client{} |
||
| 196 | |||
| 197 | httpResponse, err := hookDo(httpClient.Do)(httpRequest) |
||
| 198 | if err != nil { |
||
| 199 | return |
||
| 200 | } |
||
| 201 | |||
| 202 | defer httpResponse.Body.Close() |
||
| 203 | |||
| 204 | responseBody, err := ioutil.ReadAll(httpResponse.Body) |
||
| 205 | if err != nil { |
||
| 206 | return |
||
| 207 | } |
||
| 208 | |||
| 209 | if httpResponse.StatusCode != http.StatusOK { |
||
| 210 | err = errors.New("refresh session token failed: " + string(responseBody)) |
||
| 211 | return |
||
| 212 | } |
||
| 213 | var data assumeRoleResponse |
||
| 214 | err = json.Unmarshal(responseBody, &data) |
||
| 215 | if err != nil { |
||
| 216 | err = fmt.Errorf("refresh RoleArn sts token err, json.Unmarshal fail: %s", err.Error()) |
||
| 217 | return |
||
| 218 | } |
||
| 219 | if data.Credentials == nil { |
||
| 220 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
| 221 | return |
||
| 222 | } |
||
| 223 | |||
| 224 | if data.Credentials.AccessKeyId == nil || data.Credentials.AccessKeySecret == nil || data.Credentials.SecurityToken == nil { |
||
| 225 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
| 226 | return |
||
| 227 | } |
||
| 228 | |||
| 229 | session = &sessionCredentials{ |
||
| 230 | AccessKeyId: *data.Credentials.AccessKeyId, |
||
| 231 | AccessKeySecret: *data.Credentials.AccessKeySecret, |
||
| 232 | SecurityToken: *data.Credentials.SecurityToken, |
||
| 233 | Expiration: *data.Credentials.Expiration, |
||
| 234 | } |
||
| 235 | return |
||
| 236 | } |
||
| 275 |