Selaa lähdekoodia

add new interface

归邪 7 vuotta sitten
vanhempi
commit
1ea9fe5cd6

+ 7 - 0
sdk/auth/credentials/base_credential.go

@@ -4,3 +4,10 @@ type BaseCredential struct {
 	AccessKeyId     string
 	AccessKeySecret string
 }
+
+func NewBaseCredential(AccessKeyId, AccessKeySecret string) *BaseCredential {
+	return &BaseCredential{
+		AccessKeyId:     AccessKeyId,
+		AccessKeySecret: AccessKeySecret,
+	}
+}

+ 8 - 0
sdk/auth/credentials/rsa_key_pair_credential.go

@@ -5,3 +5,11 @@ type RsaKeyPairCredential struct {
 	PublicKeyId       string
 	SessionExpiration int
 }
+
+func NewRsaKeyPairCredential(PrivateKey, PublicKeyId string, SessionExpiration int) *RsaKeyPairCredential {
+	return &RsaKeyPairCredential{
+		PrivateKey:        PrivateKey,
+		PublicKeyId:       PublicKeyId,
+		SessionExpiration: SessionExpiration,
+	}
+}

+ 8 - 0
sdk/auth/credentials/sts_credential.go

@@ -5,3 +5,11 @@ type StsCredential struct {
 	AccessKeySecret   string
 	AccessKeyStsToken string
 }
+
+func NewStsCredential(AccessKeyId, AccessKeySecret, AccessKeyStsToken string) *StsCredential {
+	return &StsCredential{
+		AccessKeyId:       AccessKeyId,
+		AccessKeySecret:   AccessKeySecret,
+		AccessKeyStsToken: AccessKeyStsToken,
+	}
+}

+ 10 - 0
sdk/auth/credentials/sts_role_arn_credential.go

@@ -7,3 +7,13 @@ type StsRoleArnCredential struct {
 	RoleSessionName       string
 	RoleSessionExpiration int
 }
+
+func NewStsRoleArnCredential(AccessKeyId, AccessKeySecret, RoleArn, RoleSessionName string, RoleSessionExpiration int) *StsRoleArnCredential {
+	return &StsRoleArnCredential{
+		AccessKeyId:           AccessKeyId,
+		AccessKeySecret:       AccessKeySecret,
+		RoleArn:               RoleArn,
+		RoleSessionName:       RoleSessionName,
+		RoleSessionExpiration: RoleSessionExpiration,
+	}
+}

+ 6 - 0
sdk/auth/credentials/sts_role_name_on_ecs_credential.go

@@ -3,3 +3,9 @@ package credentials
 type StsRoleNameOnEcsCredential struct {
 	RoleName string
 }
+
+func NewStsRoleNameOnEcsCredential(RoleName string) *StsRoleNameOnEcsCredential {
+	return &StsRoleNameOnEcsCredential{
+		RoleName: RoleName,
+	}
+}

+ 1 - 1
sdk/auth/signers/signer_sts_assume_role.go

@@ -64,7 +64,7 @@ func NewSignerStsAssumeRole(credential *credentials.StsRoleArnCredential, common
 		signer.roleSessionName = "aliyun-go-sdk-" + strconv.FormatInt(time.Now().UnixNano()/1000, 10)
 	}
 	if credential.RoleSessionExpiration > 0 {
-		if credential.RoleSessionExpiration > 900 && credential.RoleSessionExpiration < 3600 {
+		if credential.RoleSessionExpiration >= 900 && credential.RoleSessionExpiration <= 3600 {
 			signer.credentialExpiration = credential.RoleSessionExpiration
 		} else {
 			err = errors.NewClientError(errors.InvalidParamCode, "Assume Role session duration should be in the range of 15min - 1Hr", nil)

+ 19 - 6
sdk/client.go

@@ -92,6 +92,16 @@ func (client *Client) InitWithAccessKey(regionId, accessKeyId, accessKeySecret s
 	return client.InitWithOptions(regionId, config, credential)
 }
 
+func (client *Client) InitWithSecurityToken(regionId, accessKeyId, accessKeySecret, securityToken string) (err error) {
+	config := client.InitClientConfig()
+	credential := &credentials.StsCredential{
+		AccessKeyId:       accessKeyId,
+		AccessKeySecret:   accessKeySecret,
+		AccessKeyStsToken: securityToken,
+	}
+	return client.InitWithOptions(regionId, config, credential)
+}
+
 func (client *Client) InitWithStsRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (err error) {
 	config := client.InitClientConfig()
 	credential := &credentials.StsRoleArnCredential{
@@ -247,23 +257,26 @@ func NewClientWithAccessKey(regionId, accessKeyId, accessKeySecret string) (clie
 	return
 }
 
-func NewClientWithRsaKeyPair(regionId string, config *Config, publicKeyId, privateKey string, sessionExpiration int) (client *Client, err error) {
+func NewClientWithSecurityToken(regionId, accessKeyId, accessKeySecret, securityToken string) (client *Client, err error) {
+	client = &Client{}
+	err = client.InitWithSecurityToken(regionId, accessKeyId, accessKeySecret, accessKeySecret)
+	return
+}
+
+func NewClientWithRsaKeyPair(regionId string, publicKeyId, privateKey string, sessionExpiration int) (client *Client, err error) {
 	client = &Client{}
-	client.config = config
 	err = client.InitWithRsaKeyPair(regionId, publicKeyId, privateKey, sessionExpiration)
 	return
 }
 
-func NewClientWithStsRoleNameOnEcs(regionId string, config *Config, roleName string) (client *Client, err error) {
+func NewClientWithStsRoleNameOnEcs(regionId string, roleName string) (client *Client, err error) {
 	client = &Client{}
-	client.config = config
 	err = client.InitWithStsRoleNameOnEcs(regionId, roleName)
 	return
 }
 
-func NewClientWithStsRoleArn(regionId string, config *Config, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (client *Client, err error) {
+func NewClientWithStsRoleArn(regionId string, accessKeyId, accessKeySecret, roleArn, roleSessionName string) (client *Client, err error) {
 	client = &Client{}
-	client.config = config
 	err = client.InitWithStsRoleArn(regionId, accessKeyId, accessKeySecret, roleArn, roleSessionName)
 	return
 }

+ 17 - 5
sdk/client_test.go

@@ -28,7 +28,7 @@ import (
 	"testing"
 )
 
-var client, clientKeyPair, clientEcs, clientRoleArn *Client
+var client, clientKeyPair, clientEcs, clientRoleArn, clientSts *Client
 
 type TestConfig struct {
 	AccessKeyId     string
@@ -36,6 +36,7 @@ type TestConfig struct {
 	PublicKeyId     string
 	PrivateKey      string
 	RoleArn         string
+	StsToken        string
 	ChildAK         string
 	ChildSecret     string
 }
@@ -113,16 +114,27 @@ func testSetup() {
 	if err != nil {
 		panic(err)
 	}
-	clientKeyPair, err = NewClientWithRsaKeyPair("cn-hangzhou", clientConfig, testConfig.PublicKeyId, testConfig.PrivateKey, 3600)
-	clientKeyPair.config = clientConfig
+
+	rsaKeypairCredential := credentials.NewRsaKeyPairCredential(testConfig.PrivateKey, testConfig.PublicKeyId, 3600)
+	clientKeyPair, err = NewClientWithOptions("cn-hangzhou", clientConfig, rsaKeypairCredential)
 	if err != nil {
 		panic(err)
 	}
-	clientEcs, err = NewClientWithStsRoleNameOnEcs("cn-hangzhou", clientConfig, "conan")
+
+	roleNameOnEcsCredential := credentials.NewStsRoleNameOnEcsCredential("conan")
+	clientEcs, err = NewClientWithOptions("cn-hangzhou", clientConfig, roleNameOnEcsCredential)
 	if err != nil {
 		panic(err)
 	}
-	clientRoleArn, err = NewClientWithStsRoleArn("cn-hangzhou", clientConfig, testConfig.ChildAK, testConfig.ChildSecret, testConfig.RoleArn, "clientTest")
+
+	stsRoleArnCredential := credentials.NewStsRoleArnCredential(testConfig.ChildAK, testConfig.ChildSecret, testConfig.RoleArn, "clientTest", 3600)
+	clientRoleArn, err = NewClientWithOptions("cn-hangzhou", clientConfig, stsRoleArnCredential)
+	if err != nil {
+		panic(err)
+	}
+
+	stsCredential := credentials.NewStsCredential(testConfig.AccessKeyId, testConfig.AccessKeySecret, testConfig.StsToken)
+	client, err = NewClientWithOptions("cn-hangzhou", clientConfig, stsCredential)
 	if err != nil {
 		panic(err)
 	}