signer_key_pair.go 4.7 KB

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