Skip to content

Commit

Permalink
credential-plugin: add Expiration to then token
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Nov 14, 2023
1 parent e794cf4 commit 2b6db5d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/ctl/credentialplugin/gettoken.go
Expand Up @@ -44,6 +44,12 @@ var getTokenCmd = &cobra.Command{

func newTokenExecCredential(token *ramauthenticator.Token) (*types.ExecCredential, error) {
version := getApiVersion(getCredentialOpts.apiVersion)
var exp *types.KubeTime
if !token.Expiration.IsZero() {
t := types.NewKubeTime(token.Expiration)
exp = &t
}

cred := &types.ExecCredential{
KubeTypeMeta: types.KubeTypeMeta{
Kind: kindExecCredential,
Expand All @@ -53,7 +59,8 @@ func newTokenExecCredential(token *ramauthenticator.Token) (*types.ExecCredentia
Interactive: false,
},
Status: &types.ExecCredentialStatus{
Token: token.String(),
ExpirationTimestamp: exp,
Token: token.String(),
},
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/ramauthenticator/token.go
Expand Up @@ -11,12 +11,15 @@ import (
"github.com/alibabacloud-go/tea/tea"
"github.com/aliyun/credentials-go/credentials"
"strings"
"time"
)

const (
tokenPrefixV1 = "k8s-ack-v2." // #nosec G101
)

var tokenExpiration = time.Minute * 15 // #nosec G101

var signParamsWhitelist = map[string]bool{
"x-acs-action": true,
"x-acs-version": true,
Expand All @@ -36,6 +39,8 @@ type Token struct {
Path string `json:"path"`
Query map[string]string `json:"query"`
Headers map[string]string `json:"headers"`

Expiration time.Time `json:"-"`
}

func GenerateToken(clusterId string, cred credentials.Credential) (*Token, error) {
Expand Down Expand Up @@ -80,6 +85,8 @@ func GenerateToken(clusterId string, cred credentials.Credential) (*Token, error
t.Query[k] = tea.StringValue(v)
}

t.Expiration = time.Now().Add(tokenExpiration - 5*time.Minute).UTC()

return t, nil
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/types/kube.go
Expand Up @@ -115,6 +115,10 @@ type KubeTime struct {
time.Time
}

func NewKubeTime(t time.Time) KubeTime {
return KubeTime{t}
}

func (t KubeTime) MarshalJSON() ([]byte, error) {
if t.IsZero() {
// Encode unset/nil objects as JSON's "null".
Expand Down

0 comments on commit 2b6db5d

Please sign in to comment.