Skip to content

Commit

Permalink
chore: support change endpoint via env
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Jul 18, 2024
1 parent ffda29f commit efa1ea2
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/ctl/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type ClientConfig struct {
roleArn string
stsEndpoint string
sessionName string

endpoints openapi.Endpoints
}

func NewClient(config ClientConfig) (*openapi.Client, error) {
Expand Down Expand Up @@ -63,11 +65,11 @@ func NewClient(config ClientConfig) (*openapi.Client, error) {
regionId = "cn-hangzhou"
}

return openapi.NewClient(&client.Config{
return openapi.NewClientWithEndpoints(&client.Config{
RegionId: tea.String(regionId),
Credential: cred,
UserAgent: tea.String(version.UserAgent()),
})
}, config.endpoints)
}

type getCredentialOption struct {
Expand Down Expand Up @@ -170,6 +172,7 @@ func GetClientOrDie() *openapi.Client {
ignoreAliyuncli: ctl.GlobalOption.GetIgnoreAliyuncliConfig(),
roleArn: ctl.GlobalOption.GetRoleArn(),
stsEndpoint: ctl.GlobalOption.GetSTSEndpoint(),
endpoints: ctl.GlobalOption.GetEndpoints(),
},
)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/ctl/global.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ctl

import (
"github.com/AliyunContainerService/ack-ram-tool/pkg/openapi"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -138,6 +139,13 @@ func (g *globalOption) GetRoleArn() string {
return g.FinalAssumeRoleAnotherRoleArn
}

func (g *globalOption) GetEndpoints() openapi.Endpoints {
region := g.GetRegion()
endpoints := openapi.NewEndpoints(region, g.UseVPCEndpoint)
endpoints.STS = g.GetSTSEndpoint()
return endpoints
}

func (g *globalOption) GetSTSEndpoint() string {
region := g.GetRegion()
return provider.GetSTSEndpoint(region, g.UseVPCEndpoint)
Expand Down
84 changes: 84 additions & 0 deletions pkg/openapi/client.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package openapi

import (
"fmt"
"github.com/AliyunContainerService/ack-ram-tool/pkg/credentials/provider"
cs "github.com/alibabacloud-go/cs-20151215/v3/client"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
ram "github.com/alibabacloud-go/ram-20150501/client"
sts "github.com/alibabacloud-go/sts-20150401/client"
"github.com/alibabacloud-go/tea/tea"
"github.com/aliyun/credentials-go/credentials"
"os"
"strings"
// "github.com/aliyun/credentials-go/credentials"
)

Expand All @@ -16,6 +20,12 @@ var (
defaultStsApiEndpoint = "sts.aliyuncs.com"
)

var endpointsTpl = map[string][2]string{
"cs": {"cs.%s.aliyuncs.com", "cs-anony-vpc.%s.aliyuncs.com"},
"ram": {"ram.aliyuncs.com", "ram.vpc-proxy.aliyuncs.com"},
"sts": {"sts.%s.aliyuncs.com", "sts-vpc.%s.aliyuncs.com"},
}

type ClientInterface interface {
RamClientInterface
CSClientInterface
Expand All @@ -28,6 +38,30 @@ type Client struct {
csClient *cs.Client
}

type Endpoints struct {
RAM string
STS string
CS string
}

func NewClientWithEndpoints(config *openapi.Config, endpoints Endpoints) (*Client, error) {
client, err := NewClient(config)
if err != nil {
return nil, err
}
if endpoints.STS != "" {
client.stsClient.Endpoint = tea.String(endpoints.STS)
}
if endpoints.RAM != "" {
client.ramClient.Endpoint = tea.String(endpoints.RAM)
}
if endpoints.CS != "" {
client.csClient.Endpoint = tea.String(endpoints.CS)
}

return client, nil
}

func NewClient(config *openapi.Config) (*Client, error) {
csClient, err := cs.NewClient(config)
if err != nil {
Expand Down Expand Up @@ -57,3 +91,53 @@ func NewClient(config *openapi.Config) (*Client, error) {
func (c *Client) Credential() credentials.Credential {
return c.csClient.Credential
}

func NewEndpoints(region string, vpc bool) Endpoints {
endpoints := Endpoints{
RAM: os.Getenv("RAM_ENDPOINT"),
STS: provider.GetSTSEndpoint(region, vpc),
CS: os.Getenv("CS_ENDPOINT"),
}
index := 0
if vpc {
index = 1
}
if endpoints.RAM == "" {
tpl := endpointsTpl["ram"][index]
if strings.Contains(tpl, "%") {
if region == "" {
endpoints.RAM = defaultRamApiEndpoint
} else {
endpoints.RAM = fmt.Sprintf(tpl, region)
}
} else {
endpoints.RAM = tpl
}
}
if endpoints.STS == "" {
tpl := endpointsTpl["sts"][index]
if strings.Contains(tpl, "%") {
if region == "" {
endpoints.STS = defaultStsApiEndpoint
} else {
endpoints.STS = fmt.Sprintf(tpl, region)
}
} else {
endpoints.STS = tpl
}
}
if endpoints.CS == "" {
tpl := endpointsTpl["cs"][index]
if strings.Contains(tpl, "%") {
if region == "" {
endpoints.CS = defaultCsApiEndpoint
} else {
endpoints.CS = fmt.Sprintf(tpl, region)
}
} else {
endpoints.CS = tpl
}
}

return endpoints
}
50 changes: 50 additions & 0 deletions pkg/openapi/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package openapi

import (
"reflect"
"testing"
)

func TestNewEndpoints(t *testing.T) {
type args struct {
region string
vpc bool
}
tests := []struct {
name string
args args
want Endpoints
}{
{
name: "cn-hangzhou",
args: args{
region: "cn-hangzhou",
vpc: false,
},
want: Endpoints{
RAM: "ram.aliyuncs.com",
STS: "sts.cn-hangzhou.aliyuncs.com",
CS: "cs.cn-hangzhou.aliyuncs.com",
},
},
{
name: "cn-hangzhou with vpc",
args: args{
region: "cn-hangzhou",
vpc: true,
},
want: Endpoints{
RAM: "ram.vpc-proxy.aliyuncs.com",
STS: "sts-vpc.cn-hangzhou.aliyuncs.com",
CS: "cs-anony-vpc.cn-hangzhou.aliyuncs.com",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewEndpoints(tt.args.region, tt.args.vpc); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewEndpoints() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit efa1ea2

Please sign in to comment.