client.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package sdk
  15. import (
  16. "fmt"
  17. "net/http"
  18. "strconv"
  19. "strings"
  20. "sync"
  21. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth"
  22. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  23. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/endpoints"
  24. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
  25. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  26. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  27. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
  28. )
  29. var debug utils.Debug
  30. func init() {
  31. debug = utils.Init("sdk")
  32. }
  33. // Version this value will be replaced while build: -ldflags="-X sdk.version=x.x.x"
  34. var Version = "0.0.1"
  35. var hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
  36. return fn
  37. }
  38. // Client the type Client
  39. type Client struct {
  40. regionId string
  41. config *Config
  42. signer auth.Signer
  43. httpClient *http.Client
  44. asyncTaskQueue chan func()
  45. debug bool
  46. isRunning bool
  47. // void "panic(write to close channel)" cause of addAsync() after Shutdown()
  48. asyncChanLock *sync.RWMutex
  49. }
  50. func (client *Client) Init() (err error) {
  51. panic("not support yet")
  52. }
  53. func (client *Client) InitWithOptions(regionId string, config *Config, credential auth.Credential) (err error) {
  54. client.isRunning = true
  55. client.asyncChanLock = new(sync.RWMutex)
  56. client.regionId = regionId
  57. client.config = config
  58. client.httpClient = &http.Client{}
  59. if config.HttpTransport != nil {
  60. client.httpClient.Transport = config.HttpTransport
  61. }
  62. if config.Timeout > 0 {
  63. client.httpClient.Timeout = config.Timeout
  64. }
  65. if config.EnableAsync {
  66. client.EnableAsync(config.GoRoutinePoolSize, config.MaxTaskQueueSize)
  67. }
  68. client.signer, err = auth.NewSignerWithCredential(credential, client.ProcessCommonRequestWithSigner)
  69. return
  70. }
  71. // EnableAsync enable the async task queue
  72. func (client *Client) EnableAsync(routinePoolSize, maxTaskQueueSize int) {
  73. client.asyncTaskQueue = make(chan func(), maxTaskQueueSize)
  74. for i := 0; i < routinePoolSize; i++ {
  75. go func() {
  76. for client.isRunning {
  77. select {
  78. case task, notClosed := <-client.asyncTaskQueue:
  79. if notClosed {
  80. task()
  81. }
  82. }
  83. }
  84. }()
  85. }
  86. }
  87. func (client *Client) InitWithAccessKey(regionId, accessKeyId, accessKeySecret string) (err error) {
  88. config := client.InitClientConfig()
  89. credential := &credentials.BaseCredential{
  90. AccessKeyId: accessKeyId,
  91. AccessKeySecret: accessKeySecret,
  92. }
  93. return client.InitWithOptions(regionId, config, credential)
  94. }
  95. func (client *Client) InitWithStsToken(regionId, accessKeyId, accessKeySecret, securityToken string) (err error) {
  96. config := client.InitClientConfig()
  97. credential := &credentials.StsTokenCredential{
  98. AccessKeyId: accessKeyId,
  99. AccessKeySecret: accessKeySecret,
  100. AccessKeyStsToken: securityToken,
  101. }
  102. return client.InitWithOptions(regionId, config, credential)
  103. }
  104. func (client *Client) InitWithRamRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (err error) {
  105. config := client.InitClientConfig()
  106. credential := &credentials.RamRoleArnCredential{
  107. AccessKeyId: accessKeyId,
  108. AccessKeySecret: accessKeySecret,
  109. RoleArn: roleArn,
  110. RoleSessionName: roleSessionName,
  111. }
  112. return client.InitWithOptions(regionId, config, credential)
  113. }
  114. func (client *Client) InitWithRsaKeyPair(regionId, publicKeyId, privateKey string, sessionExpiration int) (err error) {
  115. config := client.InitClientConfig()
  116. credential := &credentials.RsaKeyPairCredential{
  117. PrivateKey: privateKey,
  118. PublicKeyId: publicKeyId,
  119. SessionExpiration: sessionExpiration,
  120. }
  121. return client.InitWithOptions(regionId, config, credential)
  122. }
  123. func (client *Client) InitWithEcsRamRole(regionId, roleName string) (err error) {
  124. config := client.InitClientConfig()
  125. credential := &credentials.EcsRamRoleCredential{
  126. RoleName: roleName,
  127. }
  128. return client.InitWithOptions(regionId, config, credential)
  129. }
  130. func (client *Client) InitClientConfig() (config *Config) {
  131. if client.config != nil {
  132. return client.config
  133. } else {
  134. return NewConfig()
  135. }
  136. }
  137. func (client *Client) DoAction(request requests.AcsRequest, response responses.AcsResponse) (err error) {
  138. return client.DoActionWithSigner(request, response, nil)
  139. }
  140. func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *http.Request, err error) {
  141. // add clientVersion
  142. request.GetHeaders()["x-sdk-core-version"] = Version
  143. regionId := client.regionId
  144. if len(request.GetRegionId()) > 0 {
  145. regionId = request.GetRegionId()
  146. }
  147. // resolve endpoint
  148. resolveParam := &endpoints.ResolveParam{
  149. Domain: request.GetDomain(),
  150. Product: request.GetProduct(),
  151. RegionId: regionId,
  152. LocationProduct: request.GetLocationServiceCode(),
  153. LocationEndpointType: request.GetLocationEndpointType(),
  154. CommonApi: client.ProcessCommonRequest,
  155. }
  156. endpoint, err := endpoints.Resolve(resolveParam)
  157. if err != nil {
  158. return
  159. }
  160. request.SetDomain(endpoint)
  161. if request.GetScheme() == "" {
  162. request.SetScheme(client.config.Scheme)
  163. }
  164. // init request params
  165. err = requests.InitParams(request)
  166. if err != nil {
  167. return
  168. }
  169. // signature
  170. var finalSigner auth.Signer
  171. if signer != nil {
  172. finalSigner = signer
  173. } else {
  174. finalSigner = client.signer
  175. }
  176. httpRequest, err = buildHttpRequest(request, finalSigner, regionId)
  177. if client.config.UserAgent != "" {
  178. httpRequest.Header.Set("User-Agent", client.config.UserAgent)
  179. }
  180. return
  181. }
  182. func (client *Client) BuildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (err error) {
  183. _, err = client.buildRequestWithSigner(request, signer)
  184. return
  185. }
  186. func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) {
  187. httpRequest, err := client.buildRequestWithSigner(request, signer)
  188. if err != nil {
  189. return
  190. }
  191. var httpResponse *http.Response
  192. for retryTimes := 0; retryTimes <= client.config.MaxRetryTime; retryTimes++ {
  193. debug("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto)
  194. debug("> Host: %s", httpRequest.Host)
  195. for key, value := range httpRequest.Header {
  196. debug("> %s: %v", key, strings.Join(value, ""))
  197. }
  198. debug(">")
  199. httpResponse, err = hookDo(client.httpClient.Do)(httpRequest)
  200. if err == nil {
  201. debug("< %s %s", httpResponse.Proto, httpResponse.Status)
  202. for key, value := range httpResponse.Header {
  203. debug("< %s: %v", key, strings.Join(value, ""))
  204. }
  205. }
  206. debug("<")
  207. // receive error
  208. if err != nil {
  209. if !client.config.AutoRetry {
  210. return
  211. } else if retryTimes >= client.config.MaxRetryTime {
  212. // timeout but reached the max retry times, return
  213. timeoutErrorMsg := fmt.Sprintf(errors.TimeoutErrorMessage, strconv.Itoa(retryTimes+1), strconv.Itoa(retryTimes+1))
  214. err = errors.NewClientError(errors.TimeoutErrorCode, timeoutErrorMsg, err)
  215. return
  216. }
  217. }
  218. // if status code >= 500 or timeout, will trigger retry
  219. if client.config.AutoRetry && (err != nil || isServerError(httpResponse)) {
  220. // rewrite signatureNonce and signature
  221. httpRequest, err = client.buildRequestWithSigner(request, signer)
  222. // buildHttpRequest(request, finalSigner, regionId)
  223. if err != nil {
  224. return
  225. }
  226. continue
  227. }
  228. break
  229. }
  230. err = responses.Unmarshal(response, httpResponse, request.GetAcceptFormat())
  231. // wrap server errors
  232. if serverErr, ok := err.(*errors.ServerError); ok {
  233. var wrapInfo = map[string]string{}
  234. wrapInfo["StringToSign"] = request.GetStringToSign()
  235. err = errors.WrapServerError(serverErr, wrapInfo)
  236. }
  237. return
  238. }
  239. func buildHttpRequest(request requests.AcsRequest, singer auth.Signer, regionId string) (httpRequest *http.Request, err error) {
  240. err = auth.Sign(request, singer, regionId)
  241. if err != nil {
  242. return
  243. }
  244. requestMethod := request.GetMethod()
  245. requestUrl := request.BuildUrl()
  246. body := request.GetBodyReader()
  247. httpRequest, err = http.NewRequest(requestMethod, requestUrl, body)
  248. if err != nil {
  249. return
  250. }
  251. for key, value := range request.GetHeaders() {
  252. httpRequest.Header[key] = []string{value}
  253. }
  254. // host is a special case
  255. if host, containsHost := request.GetHeaders()["Host"]; containsHost {
  256. httpRequest.Host = host
  257. }
  258. return
  259. }
  260. func isServerError(httpResponse *http.Response) bool {
  261. return httpResponse.StatusCode >= http.StatusInternalServerError
  262. }
  263. /**
  264. only block when any one of the following occurs:
  265. 1. the asyncTaskQueue is full, increase the queue size to avoid this
  266. 2. Shutdown() in progressing, the client is being closed
  267. **/
  268. func (client *Client) AddAsyncTask(task func()) (err error) {
  269. if client.asyncTaskQueue != nil {
  270. client.asyncChanLock.RLock()
  271. defer client.asyncChanLock.RUnlock()
  272. if client.isRunning {
  273. client.asyncTaskQueue <- task
  274. }
  275. } else {
  276. err = errors.NewClientError(errors.AsyncFunctionNotEnabledCode, errors.AsyncFunctionNotEnabledMessage, nil)
  277. }
  278. return
  279. }
  280. func (client *Client) GetConfig() *Config {
  281. return client.config
  282. }
  283. func NewClient() (client *Client, err error) {
  284. client = &Client{}
  285. err = client.Init()
  286. return
  287. }
  288. func NewClientWithOptions(regionId string, config *Config, credential auth.Credential) (client *Client, err error) {
  289. client = &Client{}
  290. err = client.InitWithOptions(regionId, config, credential)
  291. return
  292. }
  293. func NewClientWithAccessKey(regionId, accessKeyId, accessKeySecret string) (client *Client, err error) {
  294. client = &Client{}
  295. err = client.InitWithAccessKey(regionId, accessKeyId, accessKeySecret)
  296. return
  297. }
  298. func NewClientWithStsToken(regionId, stsAccessKeyId, stsAccessKeySecret, stsToken string) (client *Client, err error) {
  299. client = &Client{}
  300. err = client.InitWithStsToken(regionId, stsAccessKeyId, stsAccessKeySecret, stsToken)
  301. return
  302. }
  303. func NewClientWithRamRoleArn(regionId string, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (client *Client, err error) {
  304. client = &Client{}
  305. err = client.InitWithRamRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName)
  306. return
  307. }
  308. func NewClientWithEcsRamRole(regionId string, roleName string) (client *Client, err error) {
  309. client = &Client{}
  310. err = client.InitWithEcsRamRole(regionId, roleName)
  311. return
  312. }
  313. func NewClientWithRsaKeyPair(regionId string, publicKeyId, privateKey string, sessionExpiration int) (client *Client, err error) {
  314. client = &Client{}
  315. err = client.InitWithRsaKeyPair(regionId, publicKeyId, privateKey, sessionExpiration)
  316. return
  317. }
  318. // Deprecated: Use NewClientWithRamRoleArn in this package instead.
  319. func NewClientWithStsRoleArn(regionId string, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (client *Client, err error) {
  320. return NewClientWithRamRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName)
  321. }
  322. // Deprecated: Use NewClientWithEcsRamRole in this package instead.
  323. func NewClientWithStsRoleNameOnEcs(regionId string, roleName string) (client *Client, err error) {
  324. return NewClientWithEcsRamRole(regionId, roleName)
  325. }
  326. func (client *Client) ProcessCommonRequest(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
  327. request.TransToAcsRequest()
  328. response = responses.NewCommonResponse()
  329. err = client.DoAction(request, response)
  330. return
  331. }
  332. func (client *Client) ProcessCommonRequestWithSigner(request *requests.CommonRequest, signerInterface interface{}) (response *responses.CommonResponse, err error) {
  333. if signer, isSigner := signerInterface.(auth.Signer); isSigner {
  334. request.TransToAcsRequest()
  335. response = responses.NewCommonResponse()
  336. err = client.DoActionWithSigner(request, response, signer)
  337. return
  338. }
  339. panic("should not be here")
  340. }
  341. func (client *Client) Shutdown() {
  342. // lock the addAsync()
  343. client.asyncChanLock.Lock()
  344. defer client.asyncChanLock.Unlock()
  345. if client.asyncTaskQueue != nil {
  346. close(client.asyncTaskQueue)
  347. }
  348. client.isRunning = false
  349. }