|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + cls "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls/v20201016" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + cam "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cam/v20190116" |
| 12 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/connectivity" |
| 13 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + // DefaultSearchLogStartTimestamp sync logs start time 2023-11-07 16:41:00 |
| 18 | + DefaultSearchLogStartTimestamp = 1699346460000 |
| 19 | + |
| 20 | + DefaultTopicId = "aef50d54-b17d-4782-8618-a7873203ec29" |
| 21 | + |
| 22 | + QueryGrammarRule = " AND " |
| 23 | +) |
| 24 | + |
| 25 | +// ResourceAccountInfo 资源账户信息 |
| 26 | +type ResourceAccountInfo struct { |
| 27 | + ResourceType string // 资源类型 |
| 28 | + ResourceName string // 资源名称 |
| 29 | + AccountId string // 主账号ID |
| 30 | + PrincipalId string // 用户ID |
| 31 | + UserName string // 用户名 |
| 32 | +} |
| 33 | + |
| 34 | +// GetResourceCreatorAccountInfo get resource creator user info |
| 35 | +func GetResourceCreatorAccountInfo(client *connectivity.TencentCloudClient, resourceCreateAction string, resources []*ResourceInstance) map[string]*ResourceAccountInfo { |
| 36 | + resourceIdToSubAccountInfoMap := make(map[string]*ResourceAccountInfo) |
| 37 | + if resourceCreateAction == "" { |
| 38 | + return resourceIdToSubAccountInfoMap |
| 39 | + } |
| 40 | + |
| 41 | + request := cls.NewSearchLogRequest() |
| 42 | + request.From = helper.IntInt64(DefaultSearchLogStartTimestamp) |
| 43 | + request.To = helper.Int64(CurrentTimeMillisecond()) |
| 44 | + request.TopicId = helper.String(DefaultTopicId) |
| 45 | + |
| 46 | + for _, r := range resources { |
| 47 | + query := resourceCreateAction + QueryGrammarRule |
| 48 | + if r.Id != "" { |
| 49 | + query = query + r.Id |
| 50 | + } else if r.Name != "" { |
| 51 | + query = query + r.Name |
| 52 | + } else { |
| 53 | + continue |
| 54 | + } |
| 55 | + request.Query = helper.String(query) |
| 56 | + |
| 57 | + response, err := client.UseClsClient().SearchLog(request) |
| 58 | + if err != nil { |
| 59 | + log.Printf("[CRITAL] search resource[%v] log data error: %v", r.Id, err.Error()) |
| 60 | + return resourceIdToSubAccountInfoMap |
| 61 | + } |
| 62 | + if response == nil || response.Response == nil { |
| 63 | + log.Printf("[CRITAL] search resource[%v] log data response is nil", r.Id) |
| 64 | + return resourceIdToSubAccountInfoMap |
| 65 | + } |
| 66 | + if len(response.Response.Results) == 0 { |
| 67 | + log.Printf("[CRITAL] search resource[%v] log data response results is empty", r.Id) |
| 68 | + return resourceIdToSubAccountInfoMap |
| 69 | + } |
| 70 | + |
| 71 | + result := response.Response.Results[0] |
| 72 | + if result != nil { |
| 73 | + var jsonData string |
| 74 | + if len(*result.LogJson) > 2 { |
| 75 | + jsonData = *result.LogJson |
| 76 | + } else if len(*result.RawLog) > 2 { |
| 77 | + jsonData = *result.RawLog |
| 78 | + } else { |
| 79 | + continue |
| 80 | + } |
| 81 | + |
| 82 | + resourceAccountInfo := ParseLogJsonData(jsonData) |
| 83 | + if resourceAccountInfo.PrincipalId == resourceAccountInfo.UserName && |
| 84 | + resourceAccountInfo.PrincipalId != resourceAccountInfo.AccountId { |
| 85 | + userName := GetSubAccountUserName(client, resourceAccountInfo.PrincipalId) |
| 86 | + resourceAccountInfo.UserName = userName |
| 87 | + } |
| 88 | + resourceIdToSubAccountInfoMap[r.Id] = resourceAccountInfo |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return resourceIdToSubAccountInfoMap |
| 93 | +} |
| 94 | + |
| 95 | +// GetSubAccountUserName get sub account user name |
| 96 | +func GetSubAccountUserName(client *connectivity.TencentCloudClient, uin string) string { |
| 97 | + uinNum, err := strconv.ParseUint(uin, 10, 64) |
| 98 | + if err != nil { |
| 99 | + log.Printf("[CRITAL] parse uin[%v] to uint64 type error: %v", uin, err.Error()) |
| 100 | + return "" |
| 101 | + } |
| 102 | + |
| 103 | + request := cam.NewDescribeSubAccountsRequest() |
| 104 | + |
| 105 | + uinArray := []*uint64{helper.Uint64(uinNum)} |
| 106 | + request.FilterSubAccountUin = uinArray |
| 107 | + |
| 108 | + response, err := client.UseCamClient().DescribeSubAccounts(request) |
| 109 | + if err != nil { |
| 110 | + log.Printf("[CRITAL] get sub account[%v] data error: %v", uin, err.Error()) |
| 111 | + return "" |
| 112 | + } |
| 113 | + if response == nil || response.Response == nil { |
| 114 | + log.Printf("[CRITAL] get sub account[%v] data response is nil", uin) |
| 115 | + return "" |
| 116 | + } |
| 117 | + |
| 118 | + name := response.Response.SubAccounts[0].Name |
| 119 | + return *name |
| 120 | +} |
| 121 | + |
| 122 | +// CurrentTimeMillisecond get the current millisecond timestamp |
| 123 | +func CurrentTimeMillisecond() int64 { |
| 124 | + return time.Now().UnixNano() / int64(time.Millisecond) |
| 125 | +} |
| 126 | + |
| 127 | +func ParseLogJsonData(jsonData string) *ResourceAccountInfo { |
| 128 | + if jsonData == "" { |
| 129 | + return nil |
| 130 | + } |
| 131 | + |
| 132 | + var data map[string]interface{} |
| 133 | + err := json.Unmarshal([]byte(jsonData), &data) |
| 134 | + if err != nil { |
| 135 | + log.Printf("[CRITAL] parse log json data[%v] error: %v", jsonData, err.Error()) |
| 136 | + return nil |
| 137 | + } |
| 138 | + |
| 139 | + resourceType := "" |
| 140 | + if v, ok := data["resourceType"]; ok { |
| 141 | + resourceType = v.(string) |
| 142 | + } |
| 143 | + resourceName := "" |
| 144 | + if v, ok := data["resourceName"]; ok { |
| 145 | + resourceName = v.(string) |
| 146 | + if resourceName != "" { |
| 147 | + resourceName = strings.Split(resourceName, "/")[0] |
| 148 | + } |
| 149 | + } |
| 150 | + accountId, principalId, userName := parseUserIdentityFields(data) |
| 151 | + |
| 152 | + return &ResourceAccountInfo{ |
| 153 | + ResourceType: resourceType, |
| 154 | + ResourceName: resourceName, |
| 155 | + AccountId: accountId, |
| 156 | + PrincipalId: principalId, |
| 157 | + UserName: userName, |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func parseUserIdentityFields(data map[string]interface{}) (accountId, principalId, userName string) { |
| 162 | + if v, ok := data["userIdentity.accountId"]; ok { |
| 163 | + accountId = v.(string) |
| 164 | + } |
| 165 | + if v, ok := data["userIdentity.principalId"]; ok { |
| 166 | + principalId = v.(string) |
| 167 | + } |
| 168 | + if v, ok := data["userIdentity.userName"]; ok { |
| 169 | + userName = v.(string) |
| 170 | + } |
| 171 | + if v, ok := data["userIdentity"]; ok { |
| 172 | + switch v := v.(type) { |
| 173 | + case string: |
| 174 | + var userIdentity map[string]string |
| 175 | + err := json.Unmarshal([]byte(v), &userIdentity) |
| 176 | + if err == nil { |
| 177 | + accountId = userIdentity["accountId"] |
| 178 | + principalId = userIdentity["principalId"] |
| 179 | + userName = userIdentity["userName"] |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + return |
| 184 | +} |
0 commit comments