|
| 1 | +package cam |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/404tk/cloudtoolkit/utils/logger" |
| 9 | + cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116" |
| 10 | + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" |
| 11 | +) |
| 12 | + |
| 13 | +var policy_infos map[string]string |
| 14 | + |
| 15 | +func listAttachedUserAllPolicies(client *cam.Client, uin *uint64) string { |
| 16 | + request := cam.NewListAttachedUserAllPoliciesRequest() |
| 17 | + request.TargetUin = uin |
| 18 | + request.Rp = common.Uint64Ptr(20) |
| 19 | + request.Page = common.Uint64Ptr(1) |
| 20 | + request.AttachType = common.Uint64Ptr(0) |
| 21 | + |
| 22 | + resp, err := client.ListAttachedUserAllPolicies(request) |
| 23 | + if err != nil { |
| 24 | + return "" |
| 25 | + } |
| 26 | + policies := []string{} |
| 27 | + for _, p := range resp.Response.PolicyList { |
| 28 | + policies = append(policies, *p.PolicyName) |
| 29 | + if *p.StrategyType == "1" { |
| 30 | + if _, ok := policy_infos[*p.PolicyName]; !ok { |
| 31 | + details := getPolicy(client, *p.PolicyId) |
| 32 | + policy_infos[*p.PolicyName] = details |
| 33 | + msg := fmt.Sprintf("Found Custom Policy %s: %s", *p.PolicyName, details) |
| 34 | + logger.Warning(msg) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + return strings.Join(policies, "\n") |
| 39 | +} |
| 40 | + |
| 41 | +func getPolicy(client *cam.Client, pid string) string { |
| 42 | + request := cam.NewGetPolicyRequest() |
| 43 | + pid_int, _ := strconv.Atoi(pid) |
| 44 | + request.PolicyId = common.Uint64Ptr(uint64(pid_int)) |
| 45 | + |
| 46 | + response, err := client.GetPolicy(request) |
| 47 | + if err != nil { |
| 48 | + return err.Error() |
| 49 | + } |
| 50 | + return *response.Response.PolicyDocument |
| 51 | +} |
0 commit comments