Conditions | 13 |
Total Lines | 58 |
Code Lines | 48 |
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.*OIDCCredential.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 |
||
120 | func (r *OIDCCredential) updateCredential() (err error) { |
||
121 | if r.runtime == nil { |
||
122 | r.runtime = new(utils.Runtime) |
||
123 | } |
||
124 | request := request.NewCommonRequest() |
||
125 | request.Domain = "sts.aliyuncs.com" |
||
126 | request.Scheme = "HTTPS" |
||
127 | request.Method = "POST" |
||
128 | request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601() |
||
129 | request.QueryParams["Action"] = "AssumeRoleWithOIDC" |
||
130 | request.QueryParams["Format"] = "JSON" |
||
131 | request.BodyParams["RoleArn"] = r.RoleArn |
||
132 | request.BodyParams["OIDCProviderArn"] = r.OIDCProviderArn |
||
133 | token := r.GetOIDCToken(r.OIDCTokenFilePath) |
||
134 | request.BodyParams["OIDCToken"] = tea.StringValue(token) |
||
135 | if r.Policy != "" { |
||
136 | request.QueryParams["Policy"] = r.Policy |
||
137 | } |
||
138 | request.QueryParams["RoleSessionName"] = r.RoleSessionName |
||
139 | request.QueryParams["Version"] = "2015-04-01" |
||
140 | request.QueryParams["SignatureNonce"] = utils.GetUUID() |
||
141 | if r.AccessKeyId != "" && r.AccessKeySecret != "" { |
||
142 | signature := utils.ShaHmac1(request.BuildStringToSign(), r.AccessKeySecret+"&") |
||
143 | request.QueryParams["Signature"] = signature |
||
144 | request.QueryParams["AccessKeyId"] = r.AccessKeyId |
||
145 | request.QueryParams["AccessKeySecret"] = r.AccessKeySecret |
||
146 | } |
||
147 | request.Headers["Host"] = request.Domain |
||
148 | request.Headers["Accept-Encoding"] = "identity" |
||
149 | request.Headers["content-type"] = "application/x-www-form-urlencoded" |
||
150 | request.URL = request.BuildURL() |
||
151 | content, err := doAction(request, r.runtime) |
||
152 | if err != nil { |
||
153 | return fmt.Errorf("refresh RoleArn sts token err: %s", err.Error()) |
||
154 | } |
||
155 | var resp *OIDCResponse |
||
156 | err = json.Unmarshal(content, &resp) |
||
157 | if err != nil { |
||
158 | return fmt.Errorf("refresh RoleArn sts token err: Json.Unmarshal fail: %s", err.Error()) |
||
159 | } |
||
160 | if resp == nil || resp.Credentials == nil { |
||
161 | return fmt.Errorf("refresh RoleArn sts token err: Credentials is empty") |
||
162 | } |
||
163 | respCredentials := resp.Credentials |
||
164 | if respCredentials.AccessKeyId == "" || respCredentials.AccessKeySecret == "" || respCredentials.SecurityToken == "" || respCredentials.Expiration == "" { |
||
165 | return fmt.Errorf("refresh RoleArn sts token err: AccessKeyId: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", respCredentials.AccessKeyId, respCredentials.AccessKeySecret, respCredentials.SecurityToken, respCredentials.Expiration) |
||
166 | } |
||
167 | |||
168 | expirationTime, err := time.Parse("2006-01-02T15:04:05Z", respCredentials.Expiration) |
||
169 | r.lastUpdateTimestamp = time.Now().Unix() |
||
170 | r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix()) |
||
171 | r.sessionCredential = &sessionCredential{ |
||
172 | AccessKeyId: respCredentials.AccessKeyId, |
||
173 | AccessKeySecret: respCredentials.AccessKeySecret, |
||
174 | SecurityToken: respCredentials.SecurityToken, |
||
175 | } |
||
176 | |||
177 | return |
||
178 | } |
||
179 |