Ver código fonte

Merge pull request #59 from aliyun/pre-release

Pre release
gaort 7 anos atrás
pai
commit
85333571f2

+ 7 - 0
sdk/auth/signers/session_credential.go

@@ -0,0 +1,7 @@
+package signers
+
+type SessionCredential struct {
+	AccessKeyId     string
+	AccessKeySecret string
+	StsToken        string
+}

+ 14 - 10
sdk/auth/signers/signer_ecs_ram_role.go

@@ -28,7 +28,7 @@ import (
 
 type EcsRamRoleSigner struct {
 	*credentialUpdater
-	sessionCredential *sessionCredential
+	sessionCredential *SessionCredential
 	credential        *credentials.EcsRamRoleCredential
 	commonApi         func(request *requests.CommonRequest, signer interface{}) (response *responses.CommonResponse, err error)
 }
@@ -65,24 +65,24 @@ func (signer *EcsRamRoleSigner) GetAccessKeyId() (accessKeyId string, err error)
 	if signer.sessionCredential == nil || signer.needUpdateCredential() {
 		err = signer.updateCredential()
 	}
-	if err != nil && (signer.sessionCredential == nil || len(signer.sessionCredential.accessKeyId) <= 0) {
+	if err != nil && (signer.sessionCredential == nil || len(signer.sessionCredential.AccessKeyId) <= 0) {
 		return "", err
 	}
-	return signer.sessionCredential.accessKeyId, nil
+	return signer.sessionCredential.AccessKeyId, nil
 }
 
 func (signer *EcsRamRoleSigner) GetExtraParam() map[string]string {
 	if signer.sessionCredential == nil {
 		return make(map[string]string)
 	}
-	if len(signer.sessionCredential.securityToken) <= 0 {
+	if len(signer.sessionCredential.StsToken) <= 0 {
 		return make(map[string]string)
 	}
-	return map[string]string{"SecurityToken": signer.sessionCredential.securityToken}
+	return map[string]string{"SecurityToken": signer.sessionCredential.StsToken}
 }
 
 func (signer *EcsRamRoleSigner) Sign(stringToSign, secretSuffix string) string {
-	secret := signer.sessionCredential.accessKeySecret + secretSuffix
+	secret := signer.sessionCredential.AccessKeyId + secretSuffix
 	return ShaHmac1(stringToSign, secret)
 }
 
@@ -157,15 +157,19 @@ func (signer *EcsRamRoleSigner) refreshCredential(response *responses.CommonResp
 
 	expirationTime, err := time.Parse("2006-01-02T15:04:05Z", expiration.(string))
 	signer.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix())
-	signer.sessionCredential = &sessionCredential{
-		accessKeyId:     accessKeyId.(string),
-		accessKeySecret: accessKeySecret.(string),
-		securityToken:   securityToken.(string),
+	signer.sessionCredential = &SessionCredential{
+		AccessKeyId:     accessKeyId.(string),
+		AccessKeySecret: accessKeySecret.(string),
+		StsToken:        securityToken.(string),
 	}
 
 	return
 }
 
+func (signer *EcsRamRoleSigner) GetSessionCredential() *SessionCredential {
+	return signer.sessionCredential
+}
+
 func (signer *EcsRamRoleSigner) Shutdown() {
 
 }

+ 8 - 13
sdk/auth/signers/signer_key_pair.go

@@ -28,16 +28,11 @@ import (
 
 type SignerKeyPair struct {
 	*credentialUpdater
-	sessionCredential *SessionAkCredential
+	sessionCredential *SessionCredential
 	credential        *credentials.RsaKeyPairCredential
 	commonApi         func(request *requests.CommonRequest, signer interface{}) (response *responses.CommonResponse, err error)
 }
 
-type SessionAkCredential struct {
-	accessKeyId     string
-	accessKeySecret string
-}
-
 func NewSignerKeyPair(credential *credentials.RsaKeyPairCredential, commonApi func(*requests.CommonRequest, interface{}) (response *responses.CommonResponse, err error)) (signer *SignerKeyPair, err error) {
 	signer = &SignerKeyPair{
 		credential: credential,
@@ -79,24 +74,24 @@ func (signer *SignerKeyPair) GetAccessKeyId() (accessKeyId string, err error) {
 	if signer.sessionCredential == nil || signer.needUpdateCredential() {
 		err = signer.updateCredential()
 	}
-	if err != nil && (signer.sessionCredential == nil || len(signer.sessionCredential.accessKeyId) <= 0) {
+	if err != nil && (signer.sessionCredential == nil || len(signer.sessionCredential.AccessKeyId) <= 0) {
 		return "", err
 	}
-	return signer.sessionCredential.accessKeyId, err
+	return signer.sessionCredential.AccessKeyId, err
 }
 
 func (signer *SignerKeyPair) GetExtraParam() map[string]string {
 	if signer.sessionCredential == nil || signer.needUpdateCredential() {
 		signer.updateCredential()
 	}
-	if signer.sessionCredential == nil || len(signer.sessionCredential.accessKeyId) <= 0 {
+	if signer.sessionCredential == nil || len(signer.sessionCredential.AccessKeyId) <= 0 {
 		return make(map[string]string)
 	}
 	return make(map[string]string)
 }
 
 func (signer *SignerKeyPair) Sign(stringToSign, secretSuffix string) string {
-	secret := signer.sessionCredential.accessKeySecret + secretSuffix
+	secret := signer.sessionCredential.AccessKeyId + secretSuffix
 	return ShaHmac1(stringToSign, secret)
 }
 
@@ -141,9 +136,9 @@ func (signer *SignerKeyPair) refreshCredential(response *responses.CommonRespons
 	if accessKeyId == nil || accessKeySecret == nil {
 		return
 	}
-	signer.sessionCredential = &SessionAkCredential{
-		accessKeyId:     accessKeyId.(string),
-		accessKeySecret: accessKeySecret.(string),
+	signer.sessionCredential = &SessionCredential{
+		AccessKeyId:     accessKeyId.(string),
+		AccessKeySecret: accessKeySecret.(string),
 	}
 	return
 }

+ 18 - 20
sdk/auth/signers/signer_ram_role_arn.go

@@ -34,17 +34,11 @@ const (
 type RamRoleArnSigner struct {
 	*credentialUpdater
 	roleSessionName   string
-	sessionCredential *sessionCredential
+	sessionCredential *SessionCredential
 	credential        *credentials.RamRoleArnCredential
 	commonApi         func(request *requests.CommonRequest, signer interface{}) (response *responses.CommonResponse, err error)
 }
 
-type sessionCredential struct {
-	accessKeyId     string
-	accessKeySecret string
-	securityToken   string
-}
-
 func NewRamRoleArnSigner(credential *credentials.RamRoleArnCredential, commonApi func(request *requests.CommonRequest, signer interface{}) (response *responses.CommonResponse, err error)) (signer *RamRoleArnSigner, err error) {
 	signer = &RamRoleArnSigner{
 		credential: credential,
@@ -91,24 +85,24 @@ func (signer *RamRoleArnSigner) GetAccessKeyId() (accessKeyId string, err error)
 	if signer.sessionCredential == nil || signer.needUpdateCredential() {
 		err = signer.updateCredential()
 	}
-	if err != nil && (signer.sessionCredential == nil || len(signer.sessionCredential.accessKeyId) <= 0) {
+	if err != nil && (signer.sessionCredential == nil || len(signer.sessionCredential.AccessKeyId) <= 0) {
 		return "", err
 	}
-	return signer.sessionCredential.accessKeyId, nil
+	return signer.sessionCredential.AccessKeyId, nil
 }
 
 func (signer *RamRoleArnSigner) GetExtraParam() map[string]string {
 	if signer.sessionCredential == nil || signer.needUpdateCredential() {
 		signer.updateCredential()
 	}
-	if signer.sessionCredential == nil || len(signer.sessionCredential.securityToken) <= 0 {
+	if signer.sessionCredential == nil || len(signer.sessionCredential.StsToken) <= 0 {
 		return make(map[string]string)
 	}
-	return map[string]string{"SecurityToken": signer.sessionCredential.securityToken}
+	return map[string]string{"SecurityToken": signer.sessionCredential.StsToken}
 }
 
 func (signer *RamRoleArnSigner) Sign(stringToSign, secretSuffix string) string {
-	secret := signer.sessionCredential.accessKeySecret + secretSuffix
+	secret := signer.sessionCredential.AccessKeySecret + secretSuffix
 	return ShaHmac1(stringToSign, secret)
 }
 
@@ -124,13 +118,13 @@ func (signer *RamRoleArnSigner) buildCommonRequest() (request *requests.CommonRe
 	return
 }
 
-func (signerStsAssumeRole *RamRoleArnSigner) refreshApi(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
+func (signer *RamRoleArnSigner) refreshApi(request *requests.CommonRequest) (response *responses.CommonResponse, err error) {
 	credential := &credentials.AccessKeyCredential{
-		AccessKeyId:     signerStsAssumeRole.credential.AccessKeyId,
-		AccessKeySecret: signerStsAssumeRole.credential.AccessKeySecret,
+		AccessKeyId:     signer.credential.AccessKeyId,
+		AccessKeySecret: signer.credential.AccessKeySecret,
 	}
 	signerV1, err := NewAccessKeySigner(credential)
-	return signerStsAssumeRole.commonApi(request, signerV1)
+	return signer.commonApi(request, signerV1)
 }
 
 func (signer *RamRoleArnSigner) refreshCredential(response *responses.CommonResponse) (err error) {
@@ -163,14 +157,18 @@ func (signer *RamRoleArnSigner) refreshCredential(response *responses.CommonResp
 	if accessKeyId == nil || accessKeySecret == nil || securityToken == nil {
 		return
 	}
-	signer.sessionCredential = &sessionCredential{
-		accessKeyId:     accessKeyId.(string),
-		accessKeySecret: accessKeySecret.(string),
-		securityToken:   securityToken.(string),
+	signer.sessionCredential = &SessionCredential{
+		AccessKeyId:     accessKeyId.(string),
+		AccessKeySecret: accessKeySecret.(string),
+		StsToken:        securityToken.(string),
 	}
 	return
 }
 
+func (signer *RamRoleArnSigner) GetSessionCredential() *SessionCredential {
+	return signer.sessionCredential
+}
+
 func (signer *RamRoleArnSigner) Shutdown() {
 
 }