Conditions | 14 |
Total Lines | 62 |
Code Lines | 49 |
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 credentials.*OIDCCredentialsProvider.updateCredential 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 credentials |
||
144 | func (r *OIDCCredentialsProvider) updateCredential() (err error) { |
||
145 | if r.runtime == nil { |
||
146 | r.runtime = new(utils.Runtime) |
||
147 | } |
||
148 | request := request.NewCommonRequest() |
||
149 | request.Domain = "sts.aliyuncs.com" |
||
150 | if r.runtime.STSEndpoint != "" { |
||
151 | request.Domain = r.runtime.STSEndpoint |
||
152 | } |
||
153 | request.Scheme = "HTTPS" |
||
154 | request.Method = "POST" |
||
155 | request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
156 | request.QueryParams["Action"] = "AssumeRoleWithOIDC" |
||
157 | request.QueryParams["Format"] = "JSON" |
||
158 | request.BodyParams["RoleArn"] = r.RoleArn |
||
159 | request.BodyParams["OIDCProviderArn"] = r.OIDCProviderArn |
||
160 | token, err := getFileContent(r.OIDCTokenFilePath) |
||
161 | if err != nil { |
||
162 | return fmt.Errorf("read oidc token file failed: %s", err.Error()) |
||
163 | } |
||
164 | |||
165 | request.BodyParams["OIDCToken"] = token |
||
166 | if r.Policy != "" { |
||
167 | request.QueryParams["Policy"] = r.Policy |
||
168 | } |
||
169 | if r.RoleSessionExpiration > 0 { |
||
170 | request.QueryParams["DurationSeconds"] = strconv.Itoa(r.RoleSessionExpiration) |
||
171 | } |
||
172 | request.QueryParams["RoleSessionName"] = r.RoleSessionName |
||
173 | request.QueryParams["Version"] = "2015-04-01" |
||
174 | request.QueryParams["SignatureNonce"] = utils.GetUUID() |
||
175 | request.Headers["Host"] = request.Domain |
||
176 | request.Headers["Accept-Encoding"] = "identity" |
||
177 | request.Headers["content-type"] = "application/x-www-form-urlencoded" |
||
178 | request.URL = request.BuildURL() |
||
179 | content, err := doAction(request, r.runtime) |
||
180 | if err != nil { |
||
181 | return fmt.Errorf("get sts token failed with: %s", err.Error()) |
||
182 | } |
||
183 | var resp *OIDCResponse |
||
184 | err = json.Unmarshal(content, &resp) |
||
185 | if err != nil { |
||
186 | return fmt.Errorf("get sts token failed with: Json.Unmarshal fail: %s", err.Error()) |
||
187 | } |
||
188 | if resp == nil || resp.Credentials == nil { |
||
189 | return fmt.Errorf("get sts token failed with: credentials is empty") |
||
190 | } |
||
191 | respCredentials := resp.Credentials |
||
192 | if respCredentials.AccessKeyId == "" || respCredentials.AccessKeySecret == "" || respCredentials.SecurityToken == "" || respCredentials.Expiration == "" { |
||
193 | return fmt.Errorf("get sts token failed with: AccessKeyId: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", respCredentials.AccessKeyId, respCredentials.AccessKeySecret, respCredentials.SecurityToken, respCredentials.Expiration) |
||
194 | } |
||
195 | |||
196 | expirationTime, err := time.Parse("2006-01-02T15:04:05Z", respCredentials.Expiration) |
||
197 | r.lastUpdateTimestamp = time.Now().Unix() |
||
198 | r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix()) |
||
199 | r.sessionCredential = &sessionCredential{ |
||
200 | AccessKeyId: respCredentials.AccessKeyId, |
||
201 | AccessKeySecret: respCredentials.AccessKeySecret, |
||
202 | SecurityToken: respCredentials.SecurityToken, |
||
203 | } |
||
204 | |||
205 | return |
||
206 | } |
||
207 |