/
token.go
169 lines (148 loc) · 5.11 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ramauthenticator
import (
"encoding/base64"
"encoding/json"
"errors"
"github.com/AliyunContainerService/ack-ram-tool/pkg/version"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
openapiutil "github.com/alibabacloud-go/openapi-util/service"
util "github.com/alibabacloud-go/tea-utils/service"
"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,
"authorization": true,
"x-acs-signature-nonce": true,
"x-acs-date": true,
"x-acs-content-sha256": true,
"x-acs-content-sm3": true,
"x-acs-security-token": true,
"ackclusterid": true,
}
type Token struct {
ClusterId string `json:"clusterId"`
Method string `json:"method"`
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) {
q := &openapi.OpenApiRequest{
Query: map[string]*string{
"ACKClusterId": tea.String(clusterId),
},
}
params := &openapi.Params{
Action: tea.String("GetCallerIdentity"),
Version: tea.String("2015-04-01"),
Protocol: tea.String("HTTPS"),
Pathname: tea.String("/"),
Method: tea.String("POST"),
AuthType: tea.String("AK"),
Style: tea.String("RPC"),
ReqBodyType: tea.String("formData"),
BodyType: tea.String("json"),
}
req, err := generatePreSignedReq(q, params, cred)
if err != nil {
return nil, err
}
t := &Token{
ClusterId: clusterId,
Method: tea.StringValue(params.Method),
Path: tea.StringValue(req.Pathname),
Headers: map[string]string{},
Query: map[string]string{},
}
for k, v := range req.Headers {
if !signParamsWhitelist[strings.ToLower(k)] {
continue
}
t.Headers[k] = tea.StringValue(v)
}
for k, v := range req.Query {
if !signParamsWhitelist[strings.ToLower(k)] {
continue
}
t.Query[k] = tea.StringValue(v)
}
t.Expiration = time.Now().Add(tokenExpiration - 5*time.Minute).UTC()
return t, nil
}
func (t *Token) String() string {
req, _ := json.Marshal(t)
return tokenPrefixV1 + base64.StdEncoding.EncodeToString(req)
}
func generatePreSignedReq(request *openapi.OpenApiRequest, params *openapi.Params, cred credentials.Credential) (*tea.Request, error) {
newReq := tea.NewRequest()
newReq.Protocol = util.DefaultString(nil, params.Protocol)
newReq.Method = params.Method
newReq.Pathname = params.Pathname
newReq.Query = tea.Merge(request.Query)
// endpoint is setted in product client
newReq.Headers = tea.Merge(map[string]*string{
//"host": client.Endpoint,
"x-acs-version": params.Version,
"x-acs-action": params.Action,
"user-agent": tea.String(version.UserAgent()),
"x-acs-date": openapiutil.GetTimestamp(),
//"x-acs-signature-nonce": util.GetNonce(),
"accept": tea.String("application/json"),
}, request.Headers)
signatureAlgorithm := util.DefaultString(nil, tea.String("ACS3-HMAC-SHA256"))
hashedRequestPayload := openapiutil.HexEncode(openapiutil.Hash(util.ToBytes(tea.String("")), signatureAlgorithm))
if !tea.BoolValue(util.IsUnset(request.Body)) {
if tea.BoolValue(util.EqualString(params.ReqBodyType, tea.String("json"))) {
jsonObj := util.ToJSONString(request.Body)
hashedRequestPayload = openapiutil.HexEncode(openapiutil.Hash(util.ToBytes(jsonObj), signatureAlgorithm))
newReq.Body = tea.ToReader(jsonObj)
newReq.Headers["content-type"] = tea.String("application/json; charset=utf-8")
} else {
m := util.AssertAsMap(request.Body)
formObj := openapiutil.ToForm(m)
hashedRequestPayload = openapiutil.HexEncode(openapiutil.Hash(util.ToBytes(formObj), signatureAlgorithm))
newReq.Body = tea.ToReader(formObj)
newReq.Headers["content-type"] = tea.String("application/x-www-form-urlencoded")
}
}
newReq.Headers["x-acs-content-sha256"] = hashedRequestPayload
if !tea.BoolValue(util.EqualString(params.AuthType, tea.String("Anonymous"))) {
authType := cred.GetType()
if tea.BoolValue(util.EqualString(authType, tea.String("bearer"))) {
bearerToken := cred.GetBearerToken()
if tea.StringValue(bearerToken) == "" {
return nil, errors.New("GetBearerToken failed")
}
newReq.Headers["x-acs-bearer-token"] = bearerToken
} else {
accessKeyId, _err := cred.GetAccessKeyId()
if _err != nil {
return nil, _err
}
accessKeySecret, _err := cred.GetAccessKeySecret()
if _err != nil {
return nil, _err
}
securityToken, _err := cred.GetSecurityToken()
if _err != nil {
return nil, _err
}
if !tea.BoolValue(util.Empty(securityToken)) {
newReq.Headers["x-acs-accesskey-id"] = accessKeyId
newReq.Headers["x-acs-security-token"] = securityToken
}
newReq.Headers["Authorization"] = openapiutil.GetAuthorization(
newReq, signatureAlgorithm, hashedRequestPayload, accessKeyId, accessKeySecret)
}
}
return newReq, nil
}