| Conditions | 16 | 
| Total Lines | 82 | 
| Code Lines | 59 | 
| 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.*OIDCCredentialsProvider.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 *OIDCCredentialsProvider) getCredentials() (session *sessionCredentials, err error) { | ||
| 159 | 	req := &httputil.Request{ | ||
| 160 | Method: "POST", | ||
| 161 | Protocol: "https", | ||
| 162 | Host: provider.stsEndpoint, | ||
| 163 | 		Headers:  map[string]string{}, | ||
| 164 | } | ||
| 165 | |||
| 166 | connectTimeout := 5 * time.Second | ||
| 167 | readTimeout := 10 * time.Second | ||
| 168 | |||
| 169 | 	if provider.httpOptions != nil && provider.httpOptions.ConnectTimeout > 0 { | ||
| 170 | connectTimeout = time.Duration(provider.httpOptions.ConnectTimeout) * time.Millisecond | ||
| 171 | } | ||
| 172 | 	if provider.httpOptions != nil && provider.httpOptions.ReadTimeout > 0 { | ||
| 173 | readTimeout = time.Duration(provider.httpOptions.ReadTimeout) * time.Millisecond | ||
| 174 | } | ||
| 175 | 	if provider.httpOptions != nil && provider.httpOptions.Proxy != "" { | ||
| 176 | req.Proxy = provider.httpOptions.Proxy | ||
| 177 | } | ||
| 178 | req.ConnectTimeout = connectTimeout | ||
| 179 | req.ReadTimeout = readTimeout | ||
| 180 | |||
| 181 | queries := make(map[string]string) | ||
| 182 | queries["Version"] = "2015-04-01" | ||
| 183 | queries["Action"] = "AssumeRoleWithOIDC" | ||
| 184 | queries["Format"] = "JSON" | ||
| 185 | queries["Timestamp"] = utils.GetTimeInFormatISO8601() | ||
| 186 | req.Queries = queries | ||
| 187 | |||
| 188 | bodyForm := make(map[string]string) | ||
| 189 | bodyForm["RoleArn"] = provider.roleArn | ||
| 190 | bodyForm["OIDCProviderArn"] = provider.oidcProviderARN | ||
| 191 | token, err := ioutil.ReadFile(provider.oidcTokenFilePath) | ||
| 192 | 	if err != nil { | ||
| 193 | return | ||
| 194 | } | ||
| 195 | |||
| 196 | bodyForm["OIDCToken"] = string(token) | ||
| 197 | 	if provider.policy != "" { | ||
| 198 | bodyForm["Policy"] = provider.policy | ||
| 199 | } | ||
| 200 | |||
| 201 | bodyForm["RoleSessionName"] = provider.roleSessionName | ||
| 202 | bodyForm["DurationSeconds"] = strconv.Itoa(provider.durationSeconds) | ||
| 203 | req.Form = bodyForm | ||
| 204 | |||
| 205 | // set headers | ||
| 206 | req.Headers["Accept-Encoding"] = "identity" | ||
| 207 | res, err := httpDo(req) | ||
| 208 | 	if err != nil { | ||
| 209 | return | ||
| 210 | } | ||
| 211 | |||
| 212 | 	if res.StatusCode != http.StatusOK { | ||
| 213 | message := "get session token failed: " | ||
| 214 | err = errors.New(message + string(res.Body)) | ||
| 215 | return | ||
| 216 | } | ||
| 217 | var data assumeRoleResponse | ||
| 218 | err = json.Unmarshal(res.Body, &data) | ||
| 219 | 	if err != nil { | ||
| 220 | 		err = fmt.Errorf("get oidc sts token err, json.Unmarshal fail: %s", err.Error()) | ||
| 221 | return | ||
| 222 | } | ||
| 223 | 	if data.Credentials == nil { | ||
| 224 | 		err = fmt.Errorf("get oidc sts token err, fail to get credentials") | ||
| 225 | return | ||
| 226 | } | ||
| 227 | |||
| 228 | 	if data.Credentials.AccessKeyId == nil || data.Credentials.AccessKeySecret == nil || data.Credentials.SecurityToken == nil { | ||
| 229 | 		err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") | ||
| 230 | return | ||
| 231 | } | ||
| 232 | |||
| 233 | 	session = &sessionCredentials{ | ||
| 234 | AccessKeyId: *data.Credentials.AccessKeyId, | ||
| 235 | AccessKeySecret: *data.Credentials.AccessKeySecret, | ||
| 236 | SecurityToken: *data.Credentials.SecurityToken, | ||
| 237 | Expiration: *data.Credentials.Expiration, | ||
| 238 | } | ||
| 239 | return | ||
| 240 | } | ||
| 279 |