signer_ecs_instance.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 signers
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
  19. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  20. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  21. "github.com/jmespath/go-jmespath"
  22. "net/http"
  23. "strings"
  24. "time"
  25. )
  26. type SignerEcsInstance struct {
  27. *credentialUpdater
  28. sessionCredential *sessionCredential
  29. credential *credentials.StsRoleNameOnEcsCredential
  30. commonApi func(request *requests.CommonRequest, signer interface{}) (response *responses.CommonResponse, err error)
  31. }
  32. func NewSignereEcsInstance(credential *credentials.StsRoleNameOnEcsCredential, commonApi func(*requests.CommonRequest, interface{}) (response *responses.CommonResponse, err error)) (signer *SignerEcsInstance, err error) {
  33. signer = &SignerEcsInstance{
  34. credential: credential,
  35. commonApi: commonApi,
  36. }
  37. signer.credentialUpdater = &credentialUpdater{
  38. credentialExpiration: defaultDurationSeconds / 60,
  39. buildRequestMethod: signer.buildCommonRequest,
  40. responseCallBack: signer.refreshCredential,
  41. refreshApi: signer.refreshApi,
  42. }
  43. return
  44. }
  45. func (*SignerEcsInstance) GetName() string {
  46. return "HMAC-SHA1"
  47. }
  48. func (*SignerEcsInstance) GetType() string {
  49. return ""
  50. }
  51. func (*SignerEcsInstance) GetVersion() string {
  52. return "1.0"
  53. }
  54. func (signer *SignerEcsInstance) GetAccessKeyId() string {
  55. if signer.sessionCredential == nil || signer.needUpdateCredential() {
  56. signer.updateCredential()
  57. }
  58. if len(signer.sessionCredential.accessKeyId) <= 0 {
  59. return ""
  60. }
  61. return signer.sessionCredential.accessKeyId
  62. }
  63. func (signer *SignerEcsInstance) GetExtraParam() map[string]string {
  64. if signer.sessionCredential == nil {
  65. return make(map[string]string)
  66. }
  67. if len(signer.sessionCredential.securityToken) <= 0 {
  68. return make(map[string]string)
  69. }
  70. return map[string]string{"SecurityToken": signer.sessionCredential.securityToken}
  71. }
  72. func (signer *SignerEcsInstance) Sign(stringToSign, secretSuffix string) string {
  73. secret := signer.sessionCredential.accessKeySecret + secretSuffix
  74. return ShaHmac1(stringToSign, secret)
  75. }
  76. func (signer *SignerEcsInstance) buildCommonRequest() (request *requests.CommonRequest, err error) {
  77. request = requests.NewCommonRequest()
  78. return
  79. }
  80. func (signer *SignerEcsInstance) refreshApi(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
  81. requestUrl := "http://100.100.100.200/latest/meta-data/ram/security-credentials/" + signer.credential.RoleName
  82. httpRequest, err := http.NewRequest(requests.GET, requestUrl, strings.NewReader(""))
  83. if err != nil {
  84. fmt.Println("refresh Ecs sts token err", err)
  85. return
  86. }
  87. httpClient := &http.Client{}
  88. httpResponse, err := httpClient.Do(httpRequest)
  89. if err != nil {
  90. fmt.Println("refresh Ecs sts token err", err)
  91. return
  92. }
  93. response = responses.NewCommonResponse()
  94. err = responses.Unmarshal(response, httpResponse, "")
  95. return
  96. }
  97. func (signer *SignerEcsInstance) refreshCredential(response *responses.CommonResponse) (err error) {
  98. if response.GetHttpStatus() != http.StatusOK {
  99. fmt.Println("refresh Ecs sts token err, httpStatus: " + string(response.GetHttpStatus()) + ", message = " + response.GetHttpContentString())
  100. return
  101. }
  102. var data interface{}
  103. err = json.Unmarshal(response.GetHttpContentBytes(), &data)
  104. if err != nil {
  105. fmt.Println("refresh Ecs sts token err, json.Unmarshal fail", err)
  106. return
  107. }
  108. code, err := jmespath.Search("Code", data)
  109. if err != nil {
  110. fmt.Println("refresh Ecs sts token err, fail to get Code", err)
  111. return
  112. }
  113. if code.(string) != "Success" {
  114. fmt.Println("refresh Ecs sts token err, Code is not Success", err)
  115. return
  116. }
  117. accessKeyId, err := jmespath.Search("AccessKeyId", data)
  118. if err != nil {
  119. fmt.Println("refresh Ecs sts token err, fail to get AccessKeyId", err)
  120. return
  121. }
  122. accessKeySecret, err := jmespath.Search("AccessKeySecret", data)
  123. if err != nil {
  124. fmt.Println("refresh Ecs sts token err, fail to get AccessKeySecret", err)
  125. return
  126. }
  127. securityToken, err := jmespath.Search("SecurityToken", data)
  128. if err != nil {
  129. fmt.Println("refresh Ecs sts token err, fail to get SecurityToken", err)
  130. return
  131. }
  132. expiration, err := jmespath.Search("Expiration", data)
  133. if err != nil {
  134. fmt.Println("refresh Ecs sts token err, fail to get Expiration", err)
  135. return
  136. }
  137. if accessKeyId == nil || accessKeySecret == nil || securityToken == nil {
  138. if signer.sessionCredential == nil {
  139. panic("refresh Ecs sts token failed, accessKeyId, accessKeySecret or securityToken is null")
  140. }
  141. }
  142. expirationTime, err := time.Parse("2006-01-02T15:04:05Z", expiration.(string))
  143. signer.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
  144. signer.sessionCredential = &sessionCredential{
  145. accessKeyId: accessKeyId.(string),
  146. accessKeySecret: accessKeySecret.(string),
  147. securityToken: securityToken.(string),
  148. }
  149. return
  150. }
  151. func (signer *SignerEcsInstance) Shutdown() {
  152. }