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