-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrole_entry.go
92 lines (71 loc) · 1.85 KB
/
role_entry.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
package plugin
import (
"strconv"
"strings"
"time"
"github.com/hashicorp/go-sockaddr"
"github.com/hashicorp/vault-plugin-auth-tencentcloud/sdk"
"github.com/hashicorp/vault/sdk/helper/tokenutil"
)
type roleEntry struct {
tokenutil.TokenParams
Arn Arn `json:"arn"`
Policies []string `json:"policies"`
TTL time.Duration `json:"ttl"`
MaxTTL time.Duration `json:"max_ttl"`
Period time.Duration `json:"period"`
BoundCIDRs []*sockaddr.SockAddrMarshaler `json:"bound_cidrs"`
}
func (r *roleEntry) ResponseData() map[string]interface{} {
d := map[string]interface{}{
"arn": r.Arn,
}
r.PopulateTokenData(d)
if len(r.Policies) > 0 {
d["policies"] = d["token_policies"]
}
if len(r.BoundCIDRs) > 0 {
d["bound_cidrs"] = d["token_bound_cidrs"]
}
if r.TTL > 0 {
d["ttl"] = int64(r.TTL.Seconds())
}
if r.MaxTTL > 0 {
d["max_ttl"] = int64(r.MaxTTL.Seconds())
}
if r.Period > 0 {
d["period"] = int64(r.Period.Seconds())
}
return d
}
type Arn string
func GetRoleName(client *sdk.Client, userId string) (string, error) {
if strings.HasPrefix(userId, "roleSessionName") {
roleId := userId[:strings.Index(userId, ":roleSessionName")]
role, err := client.GetRole(roleId)
if err != nil {
return "", err
}
return *role.RoleName, nil
}
var uin int64
if strings.HasSuffix(userId, "federatedUserName") {
uinStr := userId[:strings.Index(userId, ":federatedUserName")]
var err error
uin, err = strconv.ParseInt(uinStr, 10, 64)
if err != nil {
return "", err
}
} else {
var err error
uin, err = strconv.ParseInt(userId, 10, 64)
if err != nil {
return "", err
}
}
userBasicInfo, err := client.GetUserBasicInfo(uin)
if err != nil {
return "", err
}
return *userBasicInfo.Response.Nickname, nil
}