| Conditions | 19 |
| Total Lines | 107 |
| Code Lines | 73 |
| 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 |
||
| 141 | func (provider *OIDCCredentialsProvider) getCredentials() (session *sessionCredentials, err error) { |
||
| 142 | method := "POST" |
||
| 143 | host := provider.stsEndpoint |
||
| 144 | queries := make(map[string]string) |
||
| 145 | queries["Version"] = "2015-04-01" |
||
| 146 | queries["Action"] = "AssumeRoleWithOIDC" |
||
| 147 | queries["Format"] = "JSON" |
||
| 148 | queries["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
| 149 | |||
| 150 | bodyForm := make(map[string]string) |
||
| 151 | bodyForm["RoleArn"] = provider.roleArn |
||
| 152 | bodyForm["OIDCProviderArn"] = provider.oidcProviderARN |
||
| 153 | token, err := ioutil.ReadFile(provider.oidcTokenFilePath) |
||
| 154 | if err != nil { |
||
| 155 | return |
||
| 156 | } |
||
| 157 | |||
| 158 | bodyForm["OIDCToken"] = string(token) |
||
| 159 | if provider.policy != "" { |
||
| 160 | bodyForm["Policy"] = provider.policy |
||
| 161 | } |
||
| 162 | |||
| 163 | bodyForm["RoleSessionName"] = provider.roleSessionName |
||
| 164 | bodyForm["DurationSeconds"] = strconv.Itoa(provider.durationSeconds) |
||
| 165 | |||
| 166 | // caculate signature |
||
| 167 | signParams := make(map[string]string) |
||
| 168 | for key, value := range queries { |
||
| 169 | signParams[key] = value |
||
| 170 | } |
||
| 171 | for key, value := range bodyForm { |
||
| 172 | signParams[key] = value |
||
| 173 | } |
||
| 174 | |||
| 175 | querystring := utils.GetURLFormedMap(queries) |
||
| 176 | // do request |
||
| 177 | httpUrl := fmt.Sprintf("https://%s/?%s", host, querystring) |
||
| 178 | |||
| 179 | body := utils.GetURLFormedMap(bodyForm) |
||
| 180 | |||
| 181 | httpRequest, err := hookNewRequest(http.NewRequest)(method, httpUrl, strings.NewReader(body)) |
||
| 182 | if err != nil { |
||
| 183 | return |
||
| 184 | } |
||
| 185 | |||
| 186 | // set headers |
||
| 187 | httpRequest.Header["Accept-Encoding"] = []string{"identity"} |
||
| 188 | httpRequest.Header["Content-Type"] = []string{"application/x-www-form-urlencoded"} |
||
| 189 | httpClient := &http.Client{} |
||
| 190 | |||
| 191 | if provider.httpOptions != nil { |
||
| 192 | httpClient.Timeout = time.Duration(provider.httpOptions.ReadTimeout) * time.Second |
||
| 193 | proxy := &url.URL{} |
||
| 194 | if provider.httpOptions.Proxy != "" { |
||
| 195 | proxy, err = url.Parse(provider.httpOptions.Proxy) |
||
| 196 | if err != nil { |
||
| 197 | return |
||
| 198 | } |
||
| 199 | } |
||
| 200 | trans := &http.Transport{} |
||
| 201 | if proxy != nil && provider.httpOptions.Proxy != "" { |
||
| 202 | trans.Proxy = http.ProxyURL(proxy) |
||
| 203 | } |
||
| 204 | trans.DialContext = utils.Timeout(time.Duration(provider.httpOptions.ConnectTimeout) * time.Second) |
||
| 205 | httpClient.Transport = trans |
||
| 206 | } |
||
| 207 | |||
| 208 | httpResponse, err := hookDo(httpClient.Do)(httpRequest) |
||
| 209 | if err != nil { |
||
| 210 | return |
||
| 211 | } |
||
| 212 | |||
| 213 | defer httpResponse.Body.Close() |
||
| 214 | |||
| 215 | responseBody, err := ioutil.ReadAll(httpResponse.Body) |
||
| 216 | if err != nil { |
||
| 217 | return |
||
| 218 | } |
||
| 219 | |||
| 220 | if httpResponse.StatusCode != http.StatusOK { |
||
| 221 | message := "get session token failed: " |
||
| 222 | err = errors.New(message + string(responseBody)) |
||
| 223 | return |
||
| 224 | } |
||
| 225 | var data assumeRoleResponse |
||
| 226 | err = json.Unmarshal(responseBody, &data) |
||
| 227 | if err != nil { |
||
| 228 | err = fmt.Errorf("get oidc sts token err, json.Unmarshal fail: %s", err.Error()) |
||
| 229 | return |
||
| 230 | } |
||
| 231 | if data.Credentials == nil { |
||
| 232 | err = fmt.Errorf("get oidc sts token err, fail to get credentials") |
||
| 233 | return |
||
| 234 | } |
||
| 235 | |||
| 236 | if data.Credentials.AccessKeyId == nil || data.Credentials.AccessKeySecret == nil || data.Credentials.SecurityToken == nil { |
||
| 237 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
| 238 | return |
||
| 239 | } |
||
| 240 | |||
| 241 | session = &sessionCredentials{ |
||
| 242 | AccessKeyId: *data.Credentials.AccessKeyId, |
||
| 243 | AccessKeySecret: *data.Credentials.AccessKeySecret, |
||
| 244 | SecurityToken: *data.Credentials.SecurityToken, |
||
| 245 | Expiration: *data.Credentials.Expiration, |
||
| 246 | } |
||
| 247 | return |
||
| 248 | } |
||
| 287 |