云上操作日志同步

This commit is contained in:
ioito
2019-11-07 16:38:17 +08:00
committed by Qu Xuan
parent e7491733d7
commit 4036d34656
36 changed files with 1821 additions and 27 deletions
+158
View File
@@ -0,0 +1,158 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package qcloud
import (
"fmt"
"strings"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/onecloud/pkg/cloudprovider"
)
type SEvent struct {
region *SRegion
AccountID int64
CloudAuditEvent string
ErrorCode int
EventId string
EventName string
EventNameCn string
EventRegion string
EventSource string
EventTime time.Time
RequestID string
ResourceRegion string
ResourceTypeCn string
Resources map[string]string
SecretId string
SourceIPAddress string
Username string
}
func (event *SEvent) GetName() string {
if resourceName, ok := event.Resources["ResourceName"]; ok && len(resourceName) > 0 {
return resourceName
}
return event.EventName
}
func (event *SEvent) GetService() string {
return event.ResourceTypeCn
}
func (event *SEvent) GetAction() string {
return event.EventName
}
func (event *SEvent) GetResourceType() string {
if resourceType, ok := event.Resources["ResourceType"]; ok {
return resourceType
}
return ""
}
func (event *SEvent) GetRequest() jsonutils.JSONObject {
return jsonutils.Marshal(event)
}
func (event *SEvent) GetRequestId() string {
return event.RequestID
}
func (event *SEvent) GetAccount() string {
if len(event.SecretId) > 0 {
return event.SecretId
}
return event.Username
}
func (event *SEvent) IsSuccess() bool {
return event.ErrorCode > 0
}
func (event *SEvent) GetCreatedAt() time.Time {
return event.EventTime
}
func (region *SRegion) GetICloudEvents(start time.Time, end time.Time, withReadEvent bool) ([]cloudprovider.ICloudEvent, error) {
events, err := region.GetEvents(start, end)
if err != nil {
return nil, err
}
iEvents := []cloudprovider.ICloudEvent{}
for i := range events {
if withReadEvent || !strings.Contains(events[i].CloudAuditEvent, `"Read"`) {
iEvents = append(iEvents, &events[i])
}
}
return iEvents, nil
}
func (region *SRegion) GetEvents(start time.Time, end time.Time) ([]SEvent, error) {
var (
events []SEvent
nextToken string
err error
_events []SEvent
)
for {
_events, nextToken, err = region.getEvents(start, end, nextToken)
if err != nil {
return nil, err
}
if len(nextToken) == 0 {
break
}
events = append(events, _events...)
}
return events, nil
}
func (region *SRegion) getEvents(start time.Time, end time.Time, nextToken string) ([]SEvent, string, error) {
params := map[string]string{}
if start.IsZero() {
start = time.Now().AddDate(0, 0, -7)
}
if end.IsZero() {
end = time.Now()
}
params["StartTime"] = fmt.Sprintf("%d", start.Unix())
params["EndTime"] = fmt.Sprintf("%d", end.Unix())
params["MaxResults"] = "50"
if len(nextToken) > 0 {
params["NextToken"] = nextToken
}
body, err := region.auditRequest("LookUpEvents", params)
if err != nil {
return nil, "", err
}
events := []SEvent{}
err = body.Unmarshal(&events, "Events")
if err != nil {
return nil, "", errors.Wrap(err, "body.Unmarshal")
}
nextToken, _ = body.GetString("NextToken")
if over, _ := body.Bool("ListOver"); over {
nextToken = ""
}
return events, nextToken, nil
}
@@ -40,6 +40,18 @@ func (self *SQcloudProviderFactory) GetName() string {
return qcloud.CLOUD_PROVIDER_QCLOUD_CN
}
func (self *SQcloudProviderFactory) IsCloudeventRegional() bool {
return true
}
func (self *SQcloudProviderFactory) GetMaxCloudEventSyncDays() int {
return 7
}
func (self *SQcloudProviderFactory) GetMaxCloudEventKeepDays() int {
return 30
}
func (self *SQcloudProviderFactory) ValidateChangeBandwidth(instanceId string, bandwidth int64) error {
if len(instanceId) == 0 {
return fmt.Errorf("Only changes to the binding machine's EIP bandwidth are supported")
+17
View File
@@ -49,6 +49,7 @@ const (
QCLOUD_API_VERSION = "2017-03-12"
QCLOUD_CLB_API_VERSION = "2018-03-17"
QCLOUD_BILLING_API_VERSION = "2018-07-09"
QCLOUD_AUDIT_API_VERSION = "2019-03-19"
)
type SQcloudClient struct {
@@ -116,6 +117,11 @@ func vpcRequest(client *common.Client, apiName string, params map[string]string,
return _jsonRequest(client, domain, QCLOUD_API_VERSION, apiName, params, debug, true)
}
func auditRequest(client *common.Client, apiName string, params map[string]string, debug bool) (jsonutils.JSONObject, error) {
domain := apiDomain("cloudaudit", params)
return _jsonRequest(client, domain, QCLOUD_AUDIT_API_VERSION, apiName, params, debug, true)
}
func cbsRequest(client *common.Client, apiName string, params map[string]string, debug bool) (jsonutils.JSONObject, error) {
domain := apiDomain("cbs", params)
return _jsonRequest(client, domain, QCLOUD_API_VERSION, apiName, params, debug, true)
@@ -361,6 +367,9 @@ func _baseJsonRequest(client *common.Client, req tchttp.Request, resp qcloudResp
if strings.Contains(err.Error(), "Code=ResourceNotFound") {
return nil, cloudprovider.ErrNotFound
}
if strings.Contains(err.Error(), "Code=UnsupportedRegion") {
return nil, cloudprovider.ErrNotSupported
}
if needRetry {
log.Errorf("request url %s\nparams: %s\nerror: %v\ntry after %d seconds", req.GetDomain(), jsonutils.Marshal(req.GetParams()).PrettyString(), err, i*10)
time.Sleep(time.Second * time.Duration(i*10))
@@ -400,6 +409,14 @@ func (client *SQcloudClient) vpcRequest(apiName string, params map[string]string
return vpcRequest(cli, apiName, params, client.Debug)
}
func (client *SQcloudClient) auditRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
cli, err := client.getDefaultClient()
if err != nil {
return nil, err
}
return auditRequest(cli, apiName, params, client.Debug)
}
func (client *SQcloudClient) cbsRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
cli, err := client.getDefaultClient()
if err != nil {
+5
View File
@@ -632,6 +632,11 @@ func (self *SRegion) vpcRequest(apiName string, params map[string]string) (jsonu
return self.client.vpcRequest(apiName, params)
}
func (self *SRegion) auditRequest(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
params["Region"] = self.Region
return self.client.auditRequest(apiName, params)
}
func (self *SRegion) vpc2017Request(apiName string, params map[string]string) (jsonutils.JSONObject, error) {
params["Region"] = self.Region
return self.client.vpc2017Request(apiName, params)
+40
View File
@@ -0,0 +1,40 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shell
import (
"time"
"yunion.io/x/onecloud/pkg/multicloud/qcloud"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type EventListOptions struct {
Start time.Time
End time.Time
Offset int `help:"List offset"`
Limit int `help:"List limit"`
}
shellutils.R(&EventListOptions{}, "event-list", "List events", func(cli *qcloud.SRegion, args *EventListOptions) error {
events, err := cli.GetEvents(args.Start, args.End)
if err != nil {
return err
}
printList(events, 0, args.Offset, args.Limit, []string{})
return nil
})
}