signer_key_pair.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/errors"
  20. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  21. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
  22. "github.com/jmespath/go-jmespath"
  23. "net/http"
  24. "strconv"
  25. )
  26. type SignerKeyPair struct {
  27. *credentialUpdater
  28. sessionCredential *SessionAkCredential
  29. credential *credentials.RsaKeyPairCredential
  30. commonApi func(request *requests.CommonRequest, signer interface{}) (response *responses.CommonResponse, err error)
  31. }
  32. type SessionAkCredential struct {
  33. accessKeyId string
  34. accessKeySecret string
  35. }
  36. func NewSignerKeyPair(credential *credentials.RsaKeyPairCredential, commonApi func(*requests.CommonRequest, interface{}) (response *responses.CommonResponse, err error)) (signer *SignerKeyPair, err error) {
  37. signer = &SignerKeyPair{
  38. credential: credential,
  39. commonApi: commonApi,
  40. }
  41. signer.credentialUpdater = &credentialUpdater{
  42. credentialExpiration: credential.SessionExpiration,
  43. buildRequestMethod: signer.buildCommonRequest,
  44. responseCallBack: signer.refreshCredential,
  45. refreshApi: signer.refreshApi,
  46. }
  47. if credential.SessionExpiration > 0 {
  48. if credential.SessionExpiration >= 900 && credential.SessionExpiration <= 3600 {
  49. signer.credentialExpiration = credential.SessionExpiration
  50. } else {
  51. err = errors.NewClientError(errors.InvalidParamErrorCode, "Key Pair session duration should be in the range of 15min - 1Hr", nil)
  52. }
  53. } else {
  54. signer.credentialExpiration = defaultDurationSeconds
  55. }
  56. return
  57. }
  58. func (*SignerKeyPair) GetName() string {
  59. return "HMAC-SHA1"
  60. }
  61. func (*SignerKeyPair) GetType() string {
  62. return ""
  63. }
  64. func (*SignerKeyPair) GetVersion() string {
  65. return "1.0"
  66. }
  67. func (signer *SignerKeyPair) GetAccessKeyId() string {
  68. if signer.sessionCredential == nil || signer.needUpdateCredential() {
  69. signer.updateCredential()
  70. }
  71. if signer.sessionCredential == nil || len(signer.sessionCredential.accessKeyId) <= 0 {
  72. return ""
  73. }
  74. return signer.sessionCredential.accessKeyId
  75. }
  76. func (signer *SignerKeyPair) GetExtraParam() map[string]string {
  77. if signer.sessionCredential == nil || signer.needUpdateCredential() {
  78. signer.updateCredential()
  79. }
  80. if signer.sessionCredential == nil || len(signer.sessionCredential.accessKeyId) <= 0 {
  81. return make(map[string]string)
  82. }
  83. return make(map[string]string)
  84. }
  85. func (signer *SignerKeyPair) Sign(stringToSign, secretSuffix string) string {
  86. secret := signer.sessionCredential.accessKeySecret + secretSuffix
  87. return ShaHmac1(stringToSign, secret)
  88. }
  89. func (signer *SignerKeyPair) buildCommonRequest() (request *requests.CommonRequest, err error) {
  90. request = requests.NewCommonRequest()
  91. request.Product = "Sts"
  92. request.Version = "2015-04-01"
  93. request.ApiName = "GenerateSessionAccessKey"
  94. request.Scheme = requests.HTTPS
  95. request.QueryParams["PublicKeyId"] = signer.credential.PublicKeyId
  96. request.QueryParams["DurationSeconds"] = strconv.Itoa(signer.credentialExpiration)
  97. return
  98. }
  99. func (signerKeyPair *SignerKeyPair) refreshApi(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
  100. signerV2, err := NewSignerV2(signerKeyPair.credential)
  101. return signerKeyPair.commonApi(request, signerV2)
  102. }
  103. func (signer *SignerKeyPair) refreshCredential(response *responses.CommonResponse) (err error) {
  104. if response.GetHttpStatus() != http.StatusOK {
  105. message := "refresh session AccessKey failed, message = " + response.GetHttpContentString()
  106. err = errors.NewServerError(response.GetHttpStatus(), response.GetOriginHttpResponse().Status, message)
  107. if signer.sessionCredential == nil {
  108. panic(err)
  109. }
  110. return
  111. }
  112. var data interface{}
  113. err = json.Unmarshal(response.GetHttpContentBytes(), &data)
  114. if err != nil {
  115. fmt.Println("refresh KeyPair err, json.Unmarshal fail", err)
  116. return
  117. }
  118. accessKeyId, err := jmespath.Search("SessionAccessKey.SessionAccessKeyId", data)
  119. if err != nil {
  120. fmt.Println("refresh KeyPair err, fail to get SessionAccessKeyId", err)
  121. return
  122. }
  123. accessKeySecret, err := jmespath.Search("SessionAccessKey.SessionAccessKeySecret", data)
  124. if err != nil {
  125. fmt.Println("refresh KeyPair err, fail to get SessionAccessKeySecret", err)
  126. return
  127. }
  128. if accessKeyId == nil || accessKeySecret == nil {
  129. if signer.sessionCredential == nil {
  130. panic("refresh KeyPair, accessKeyId or accessKeySecret is null")
  131. }
  132. }
  133. signer.sessionCredential = &SessionAkCredential{
  134. accessKeyId: accessKeyId.(string),
  135. accessKeySecret: accessKeySecret.(string),
  136. }
  137. return
  138. }
  139. func (signer *SignerKeyPair) Shutdown() {
  140. }