Skip to content

Commit

Permalink
support new env ACK_RAM_TOOL_TOKEN_EXTRA_KEY_PREFIX
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Nov 18, 2024
1 parent c6c1bf8 commit defbbbd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
31 changes: 30 additions & 1 deletion pkg/ctl/credentialplugin/gettoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ import (
"github.com/AliyunContainerService/ack-ram-tool/pkg/ramauthenticator"
"github.com/AliyunContainerService/ack-ram-tool/pkg/types"
"github.com/spf13/cobra"
"os"
"strings"
)

const envTokenExtraQueryKeyPrefix = "ACK_RAM_TOOL_TOKEN_EXTRA_KEY_PREFIX"

type GetTokenOpts struct {
//clusterId string
privateIpAddress bool
Expand All @@ -30,7 +34,9 @@ var getTokenCmd = &cobra.Command{
clusterId := ctl.GlobalOption.ClusterId
getCredentialOpts.clusterId = clusterId

token, err := ramauthenticator.GenerateToken(clusterId, client.Credential())
generator := ramauthenticator.NewTokenGenerator(clusterId, client.Credential())
generator.SetExtraQuery(getExtraTokenQuery())
token, err := generator.NewToken()
common.ExitIfError(err)

cred, err := newTokenExecCredential(token)
Expand All @@ -42,6 +48,29 @@ var getTokenCmd = &cobra.Command{
},
}

func getExtraTokenQuery() map[string]string {
query := make(map[string]string)
prefix := os.Getenv(envTokenExtraQueryKeyPrefix)
if prefix == "" {
return query
}
for _, item := range os.Environ() {
before, after, found := strings.Cut(item, "=")
if !found {
continue
}
if after == "" {
continue
}
if !strings.HasPrefix(before, prefix) {
continue
}
k := strings.ToLower(strings.TrimPrefix(before, prefix))
query[k] = after
}
return query
}

func newTokenExecCredential(token *ramauthenticator.Token) (*types.ExecCredential, error) {
version := getApiVersion(getCredentialOpts.apiVersion)
var exp *types.KubeTime
Expand Down
33 changes: 33 additions & 0 deletions pkg/ctl/credentialplugin/gettoken_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package credentialplugin

import (
"os"
"reflect"
"testing"
)

func Test_getExtraTokenQuery(t *testing.T) {
os.Setenv(envTokenExtraQueryKeyPrefix, "FOO_")
defer os.Unsetenv(envTokenExtraQueryKeyPrefix)
os.Setenv("FOO_BAR", "test1")
os.Setenv("FOO_FUZZ", "test2")
tests := []struct {
name string
want map[string]string
}{
{
name: "test",
want: map[string]string{
"bar": "test1",
"fuzz": "test2",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getExtraTokenQuery(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getExtraTokenQuery() = %v, want %v", got, tt.want)
}
})
}
}
40 changes: 38 additions & 2 deletions pkg/ramauthenticator/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,46 @@ type Token struct {
Expiration time.Time `json:"-"`
}

func GenerateToken(clusterId string, cred credentials.Credential) (*Token, error) {
type extendOption func(q *openapi.OpenApiRequest)

type TokenGenerator struct {
clusterId string
cred credentials.Credential
extraQuery map[string]string
}

func NewTokenGenerator(clusterId string, cred credentials.Credential) *TokenGenerator {
return &TokenGenerator{
clusterId: clusterId,
cred: cred,
extraQuery: make(map[string]string),
}
}

func (g *TokenGenerator) NewToken() (*Token, error) {
return GenerateToken(g.clusterId, g.cred, g.extendRequest)
}

func (g *TokenGenerator) SetExtraQuery(extraQuery map[string]string) {
g.extraQuery = extraQuery
}

func (g *TokenGenerator) extendRequest(req *openapi.OpenApiRequest) {
for k, v := range g.extraQuery {
k = strings.ToLower(k)
req.Query[k] = tea.String(v)
}
}

func GenerateToken(clusterId string, cred credentials.Credential, options ...extendOption) (*Token, error) {
q := &openapi.OpenApiRequest{
Query: map[string]*string{
"ACKClusterId": tea.String(clusterId),
},
}
for _, f := range options {
f(q)
}
params := &openapi.Params{
Action: tea.String("GetCallerIdentity"),
Version: tea.String("2015-04-01"),
Expand Down Expand Up @@ -82,7 +116,9 @@ func GenerateToken(clusterId string, cred credentials.Credential) (*Token, error
}
for k, v := range req.Query {
if !signParamsWhitelist[strings.ToLower(k)] {
continue
if q.Query[k] == nil {
continue
}
}
t.Query[k] = tea.StringValue(v)
}
Expand Down

0 comments on commit defbbbd

Please sign in to comment.