Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add col db and resource #67

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions api/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"

"github.com/gin-gonic/gin"
"github.com/taosdata/taoskeeper/db"
"github.com/taosdata/taoskeeper/infrastructure/config"
"github.com/taosdata/taoskeeper/infrastructure/log"
"net/http"
"strings"
"time"
)

var auditLogger = log.GetLogger("audit")

const MAX_DETAIL_LEN = 50000

type Audit struct {
username string
password string
Expand All @@ -32,6 +35,8 @@ type AuditInfo struct {
ClusterID string `json:"cluster_id"`
User string `json:"user"`
Operation string `json:"operation"`
Db string `json:"db"`
Resource string `json:"resource"`
ClientAdd string `json:"client_add"` // client address
Details string `json:"details"`
}
Expand Down Expand Up @@ -106,14 +111,18 @@ func parseSql(audit AuditInfo) string {
if strings.Contains(details, "\"") {
details = strings.ReplaceAll(details, "\"", "\\\"")
}
if len(details) > MAX_DETAIL_LEN {
details = details[:MAX_DETAIL_LEN]
}

ts := time.UnixMilli(audit.Timestamp).Format(time.RFC3339)
return fmt.Sprintf(
"insert into %s using operations tags ('%s') values ('%s', '%s', '%s', '%s', '%s')",
getTableName(audit), audit.ClusterID, ts, audit.User, audit.Operation, audit.ClientAdd, details)
"insert into %s using operations_v2 tags ('%s') values ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
getTableName(audit), audit.ClusterID, ts, audit.User, audit.Operation, audit.Db, audit.Resource, audit.ClientAdd, details)
}

func getTableName(audit AuditInfo) string {
return fmt.Sprintf("t_operations_%s", audit.ClusterID)
return fmt.Sprintf("t_operations_v2_%s", audit.ClusterID)
}

func (a *Audit) initConnect() error {
Expand Down Expand Up @@ -163,8 +172,8 @@ func (a *Audit) createDBSql() string {
return buf.String()
}

var createTableSql = "create stable if not exists operations " +
"(ts timestamp, user_name varchar(25), operation varchar(20), client_address varchar(25), details varchar(65444)) " +
var createTableSql = "create stable if not exists operations_v2 " +
"(ts timestamp, user_name varchar(25), operation varchar(20), db varchar(65), resource varchar(193), client_address varchar(25), details varchar(50000)) " +
"tags (cluster_id varchar(64))"

func (a *Audit) createSTables() error {
Expand All @@ -174,6 +183,8 @@ func (a *Audit) createSTables() error {
_, err := a.conn.Exec(context.Background(), createTableSql)
if err != nil {
auditLogger.Error("## create stable error ", "error", err)
return err
}
return err

return nil
}
16 changes: 12 additions & 4 deletions api/audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func TestAudit(t *testing.T) {
err = a.Init(router)
assert.NoError(t, err)

longDetails := strings.Repeat("0123456789", 5000)

cases := []struct {
name string
ts int64
Expand All @@ -43,21 +45,27 @@ func TestAudit(t *testing.T) {
{
name: "1",
ts: 1692840000000,
data: `{"timestamp": 1692840000000, "cluster_id": "cluster_id", "user": "user", "operation": "operation", "client_add": "localhost:30000", "details": "detail"}`,
data: `{"timestamp": 1692840000000, "cluster_id": "cluster_id", "user": "user", "operation": "operation", "db":"dbnamea", "resource":"resourcenamea", "client_add": "localhost:30000", "details": "detail"}`,
expect: "detail",
},
{
name: "2",
ts: 1692850000000,
data: "{\"timestamp\": 1692850000000, \"cluster_id\": \"cluster_id\", \"user\": \"user\", \"operation\": \"operation\", \"client_add\": \"localhost:30000\", \"details\": \"create database `meter` buffer 32 cachemodel 'none' duration 50d keep 3650d single_stable 0 wal_retention_period 3600 precision 'ms'\"}",
data: `{"timestamp": 1692850000000, "cluster_id": "cluster_id", "user": "user", "operation": "operation", "db":"dbnamea", "resource":"resourcenamea", "client_add": "localhost:30000", "details": "` + longDetails + `"}`,
expect: longDetails[:50000],
},
{
name: "3",
ts: 1692860000000,
data: "{\"timestamp\": 1692860000000, \"cluster_id\": \"cluster_id\", \"user\": \"user\", \"operation\": \"operation\", \"db\":\"dbnameb\", \"resource\":\"resourcenameb\", \"client_add\": \"localhost:30000\", \"details\": \"create database `meter` buffer 32 cachemodel 'none' duration 50d keep 3650d single_stable 0 wal_retention_period 3600 precision 'ms'\"}",
expect: "create database `meter` buffer 32 cachemodel 'none' duration 50d keep 3650d single_stable 0 wal_retention_period 3600 precision 'ms'",
},
}

conn, err := db.NewConnectorWithDb(c.TDengine.Username, c.TDengine.Password, c.TDengine.Host, c.TDengine.Port, c.Audit.Database.Name)
assert.NoError(t, err)
defer func() {
_, _ = conn.Query(context.Background(), "drop database if exists audit.operations")
_, _ = conn.Query(context.Background(), "drop stable if exists audit.operations_v2")
}()

for _, c := range cases {
Expand All @@ -68,7 +76,7 @@ func TestAudit(t *testing.T) {
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)

data, err := conn.Query(context.Background(), fmt.Sprintf("select ts, details from audit.operations where ts=%d", c.ts))
data, err := conn.Query(context.Background(), fmt.Sprintf("select ts, details from audit.operations_v2 where ts=%d", c.ts))
assert.NoError(t, err)
assert.Equal(t, 1, len(data.Data))
assert.Equal(t, c.expect, data.Data[0][1])
Expand Down
Loading