Conditions | 11 |
Total Lines | 73 |
Code Lines | 53 |
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 |
||
140 | func (provider *OIDCCredentialsProvider) getCredentials() (session *sessionCredentials, err error) { |
||
141 | req := &httputil.Request{ |
||
142 | Method: "POST", |
||
143 | Protocol: "https", |
||
144 | Host: provider.stsEndpoint, |
||
145 | Headers: map[string]string{}, |
||
146 | } |
||
147 | |||
148 | if provider.httpOptions != nil { |
||
149 | req.ConnectTimeout = time.Duration(provider.httpOptions.ConnectTimeout) * time.Second |
||
150 | req.ReadTimeout = time.Duration(provider.httpOptions.ReadTimeout) * time.Second |
||
151 | req.Proxy = provider.httpOptions.Proxy |
||
152 | } |
||
153 | |||
154 | queries := make(map[string]string) |
||
155 | queries["Version"] = "2015-04-01" |
||
156 | queries["Action"] = "AssumeRoleWithOIDC" |
||
157 | queries["Format"] = "JSON" |
||
158 | queries["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
159 | req.Queries = queries |
||
160 | |||
161 | bodyForm := make(map[string]string) |
||
162 | bodyForm["RoleArn"] = provider.roleArn |
||
163 | bodyForm["OIDCProviderArn"] = provider.oidcProviderARN |
||
164 | token, err := ioutil.ReadFile(provider.oidcTokenFilePath) |
||
165 | if err != nil { |
||
166 | return |
||
167 | } |
||
168 | |||
169 | bodyForm["OIDCToken"] = string(token) |
||
170 | if provider.policy != "" { |
||
171 | bodyForm["Policy"] = provider.policy |
||
172 | } |
||
173 | |||
174 | bodyForm["RoleSessionName"] = provider.roleSessionName |
||
175 | bodyForm["DurationSeconds"] = strconv.Itoa(provider.durationSeconds) |
||
176 | req.Form = bodyForm |
||
177 | |||
178 | // set headers |
||
179 | req.Headers["Accept-Encoding"] = "identity" |
||
180 | res, err := httpDo(req) |
||
181 | if err != nil { |
||
182 | return |
||
183 | } |
||
184 | |||
185 | if res.StatusCode != http.StatusOK { |
||
186 | message := "get session token failed: " |
||
187 | err = errors.New(message + string(res.Body)) |
||
188 | return |
||
189 | } |
||
190 | var data assumeRoleResponse |
||
191 | err = json.Unmarshal(res.Body, &data) |
||
192 | if err != nil { |
||
193 | err = fmt.Errorf("get oidc sts token err, json.Unmarshal fail: %s", err.Error()) |
||
194 | return |
||
195 | } |
||
196 | if data.Credentials == nil { |
||
197 | err = fmt.Errorf("get oidc sts token err, fail to get credentials") |
||
198 | return |
||
199 | } |
||
200 | |||
201 | if data.Credentials.AccessKeyId == nil || data.Credentials.AccessKeySecret == nil || data.Credentials.SecurityToken == nil { |
||
202 | err = fmt.Errorf("refresh RoleArn sts token err, fail to get credentials") |
||
203 | return |
||
204 | } |
||
205 | |||
206 | session = &sessionCredentials{ |
||
207 | AccessKeyId: *data.Credentials.AccessKeyId, |
||
208 | AccessKeySecret: *data.Credentials.AccessKeySecret, |
||
209 | SecurityToken: *data.Credentials.SecurityToken, |
||
210 | Expiration: *data.Credentials.Expiration, |
||
211 | } |
||
212 | return |
||
213 | } |
||
252 |