@@ -18,8 +18,10 @@ import (
1818 "errors"
1919 "fmt"
2020 "os"
21+ "os/exec"
2122 "regexp"
2223 "strconv"
24+ "strings"
2325 "time"
2426
2527 "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
@@ -39,6 +41,7 @@ const (
3941 EcsRamRole = AuthenticateMode ("EcsRamRole" )
4042 RsaKeyPair = AuthenticateMode ("RsaKeyPair" )
4143 RamRoleArnWithEcs = AuthenticateMode ("RamRoleArnWithRoleName" )
44+ External = AuthenticateMode ("External" )
4245)
4346
4447type Profile struct {
@@ -61,6 +64,7 @@ type Profile struct {
6164 ReadTimeout int `json:"retry_timeout"`
6265 ConnectTimeout int `json:"connect_timeout"`
6366 RetryCount int `json:"retry_count"`
67+ ProcessCommand string `json:"process_command"`
6468 parent * Configuration //`json:"-"`
6569}
6670
@@ -117,10 +121,13 @@ func (cp *Profile) Validate() error {
117121 if cp .KeyPairName == "" {
118122 return fmt .Errorf ("invailed key_pair_name" )
119123 }
124+ case External :
125+ if cp .ProcessCommand == "" {
126+ return fmt .Errorf ("invailed process_command" )
127+ }
120128 default :
121129 return fmt .Errorf ("invailed mode: %s" , cp .Mode )
122130 }
123-
124131 return nil
125132}
126133
@@ -144,6 +151,7 @@ func (cp *Profile) OverwriteWithFlags(ctx *cli.Context) {
144151 cp .ConnectTimeout = ConnectTimeoutFlag (ctx .Flags ()).GetIntegerOrDefault (cp .ConnectTimeout )
145152 cp .RetryCount = RetryCountFlag (ctx .Flags ()).GetIntegerOrDefault (cp .RetryCount )
146153 cp .ExpiredSeconds = ExpiredSecondsFlag (ctx .Flags ()).GetIntegerOrDefault (cp .ExpiredSeconds )
154+ cp .ProcessCommand = ProcessCommandFlag (ctx .Flags ()).GetStringOrDefault (cp .ProcessCommand )
147155
148156 if cp .AccessKeyId == "" {
149157 switch {
@@ -199,6 +207,8 @@ func AutoModeRecognition(cp *Profile) {
199207 cp .Mode = RsaKeyPair
200208 } else if cp .RamRoleName != "" {
201209 cp .Mode = EcsRamRole
210+ } else if cp .ProcessCommand != "" {
211+ cp .Mode = External
202212 }
203213}
204214
@@ -233,6 +243,8 @@ func (cp *Profile) GetClient(ctx *cli.Context) (*sdk.Client, error) {
233243 client , err = cp .GetClientByPrivateKey (config )
234244 case RamRoleArnWithEcs :
235245 client , err = cp .GetClientByRamRoleArnWithEcs (config )
246+ case External :
247+ return cp .GetClientByExternal (config , ctx )
236248 default :
237249 client , err = nil , fmt .Errorf ("unexcepted certificate mode: %s" , cp .Mode )
238250 }
@@ -336,6 +348,22 @@ func (cp *Profile) GetClientByPrivateKey(config *sdk.Config) (*sdk.Client, error
336348 return client , err
337349}
338350
351+ func (cp * Profile ) GetClientByExternal (config * sdk.Config , ctx * cli.Context ) (* sdk.Client , error ) {
352+ args := strings .Fields (cp .ProcessCommand )
353+ cmd := exec .Command (args [0 ], args [1 :]... )
354+ buf , err := cmd .CombinedOutput ()
355+ if err != nil {
356+ return nil , err
357+ }
358+ err = json .Unmarshal (buf , cp )
359+ if err != nil {
360+ fmt .Println (cp .ProcessCommand )
361+ fmt .Println (string (buf ))
362+ return nil , err
363+ }
364+ return cp .GetClient (ctx )
365+ }
366+
339367func IsRegion (region string ) bool {
340368 if match , _ := regexp .MatchString ("^[a-zA-Z0-9-]*$" , region ); ! match {
341369 return false
0 commit comments