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