Skip to content

Commit

Permalink
feat: 🎸 support bash cmd line (#2891)
Browse files Browse the repository at this point in the history
* feat: 🎸 support bash cmd line

Signed-off-by: bugaolengdeyuxiaoer <937426472@qq.com>

* refactor: 💡 change audit limit condition

* feat: 🎸 support alt + backspace
  • Loading branch information
bugaolengdeyuxiaoer authored Nov 8, 2021
1 parent 853d7c4 commit d41f632
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 156 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.16
require (
bou.ke/monkey v1.0.2
github.com/360EntSecGroup-Skylar/excelize/v2 v2.3.2
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/GoogleCloudPlatform/spark-on-k8s-operator v0.0.0-20201215015655-2e8b733f5ad0
github.com/Masterminds/semver v1.5.0
Expand All @@ -23,6 +22,7 @@ require (
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/bluele/gcache v0.0.2
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869
github.com/bugaolengdeyuxiaoer/go-ansiterm v0.0.0-20211108054157-9835e04f3693 // indirect
github.com/buger/jsonparser v1.1.1
github.com/c2h5oh/datasize v0.0.0-20200112174442-28bbd4740fee
github.com/caarlos0/env v0.0.0-20180521112546-3e0f30cbf50b
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ github.com/Azure/azure-sdk-for-go v32.5.0+incompatible/go.mod h1:9XXNKU+eRnpl9mo
github.com/Azure/azure-sdk-for-go v43.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Azure/go-autorest v10.8.1+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
github.com/Azure/go-autorest/autorest v0.9.0/go.mod h1:xyHB1BMZT0cuDHU7I0+g046+BFDTQ8rEZB0s4Yfa6bI=
Expand Down Expand Up @@ -246,6 +247,10 @@ github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx2
github.com/brancz/gojsontoyaml v0.0.0-20190425155809-e8bd32d46b3d/go.mod h1:IyUJYN1gvWjtLF5ZuygmxbnsAyP3aJS6cHzIuZY50B0=
github.com/bshuster-repo/logrus-logstash-hook v0.4.1 h1:pgAtgj+A31JBVtEHu2uHuEx0n+2ukqUJnS2vVe5pQNA=
github.com/bshuster-repo/logrus-logstash-hook v0.4.1/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
github.com/bugaolengdeyuxiaoer/go-ansiterm v0.0.0-20211105082458-a2d01f0f130b h1:9cMzzuEqaP6RhGdV3DvIh6rm/fHUdez8uMIwMZJblyQ=
github.com/bugaolengdeyuxiaoer/go-ansiterm v0.0.0-20211105082458-a2d01f0f130b/go.mod h1:zoCZuwF7bNiQYEVqIzNMPsWunfW6fAYiD606Td3x88Y=
github.com/bugaolengdeyuxiaoer/go-ansiterm v0.0.0-20211108054157-9835e04f3693 h1:bxbrP2gHOdx2T8cNFFcgpfbqcj8BLjs9mGOgsig+SjE=
github.com/bugaolengdeyuxiaoer/go-ansiterm v0.0.0-20211108054157-9835e04f3693/go.mod h1:zoCZuwF7bNiQYEVqIzNMPsWunfW6fAYiD606Td3x88Y=
github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd h1:rFt+Y/IK1aEZkEHchZRSq9OQbsSzIT/OrI8YFFmRIng=
Expand Down Expand Up @@ -2105,6 +2110,7 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164 h1:7ZDGnxgHAMw7thfC5bEos0RDAccZKxioiWBhfIe+tvw=
golang.org/x/sys v0.0.0-20210915083310-ed5796bab164/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
85 changes: 40 additions & 45 deletions modules/cmp/steve/middleware/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ import (
"strings"
"time"

"github.com/Azure/go-ansiterm"
"github.com/bugaolengdeyuxiaoer/go-ansiterm"
"github.com/sirupsen/logrus"
"golang.org/x/time/rate"

"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
Expand Down Expand Up @@ -63,7 +62,7 @@ const (
AuditTargetLabel = "targetLabel"
AuditCommands = "commands"

maxAuditLength = 40000
maxAuditLength = 1 << 16
)

type Auditor struct {
Expand Down Expand Up @@ -263,48 +262,42 @@ func (w *wrapWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
ctx: w.ctx,
}
go func(w *wrapWriter) {
limiter := rate.NewLimiter(1, 1)
for {
select {
case <-w.ctx.Done():
return
case <-closeChan:
return
default:
err = limiter.Wait(w.ctx)
if err != nil {
logrus.Errorf("audit with websocket limiter error, %v", err)
return
}
auditStr := ""
ticker := time.NewTicker(1 * time.Second)
LOOP:
for {
select {
case <-ticker.C:
auditStr := ""
LOOP:
for {
select {
case cmd := <-d.auditReqChan:
auditStr += fmt.Sprintf("%s: %s\n", cmd.start.Format(time.RFC3339), cmd.cmd)
if len(auditStr) > maxAuditLength {
break LOOP
case cmd := <-d.auditReqChan:
auditStr += fmt.Sprintf("%s: %s\n", cmd.start.Format(time.RFC3339), cmd.cmd)
if len(auditStr) > 1024 {
break LOOP
}
default:
}
}
if len(auditStr) > 0 {
w.auditReq.Context[AuditCommands] = auditStr
if err := w.bdl.CreateAuditEvent(&w.auditReq); err != nil {
logrus.Errorf("faild to audit in steve audit, %v", err)
} else {
logrus.Infof("send audit message with websocket : %s", auditStr)
}
case <-w.ctx.Done():
w.sendWebsocketAudit(auditStr)
return
case <-closeChan:
w.sendWebsocketAudit(auditStr)
return
}
w.sendWebsocketAudit(auditStr)
}

}
}(w)
return wc, rw, nil
}

func (w *wrapWriter) sendWebsocketAudit(auditStr string) {
if len(auditStr) > 0 {
w.auditReq.Context[AuditCommands] = auditStr
if err := w.bdl.CreateAuditEvent(&w.auditReq); err != nil {
logrus.Errorf("faild to audit in steve audit, %v", err)
} else {
logrus.Infof("send audit message with websocket ,length : %d,", len(auditStr))
}
}
}

type wrapConn struct {
net.Conn
parser *ansiterm.AnsiParser
Expand All @@ -326,18 +319,20 @@ func (w *wrapConn) Read(p []byte) (n int, err error) {
logrus.Error(r)
}
}()
data := websocket.DecodeFrame(p)
if len(data) <= 1 || data[0] != '0' {
return
}
for _, d := range strings.Split(string(data), "\n") {
decoded, _ := base64.StdEncoding.DecodeString(d[1:])
if err != nil || len(decoded) == 0 {
data := websocket.DecodeFrames(p[:n])
for _, datum := range data {
if datum[0] != '0' {
return
}
_, err = w.parser.Parse(decoded)
if err != nil {
return 0, err
for _, d := range strings.Split(string(datum), "\n") {
decoded, _ := base64.StdEncoding.DecodeString(d[1:])
if len(decoded) == 0 {
continue
}
_, err = w.parser.Parse(decoded)
if err != nil {
logrus.Errorf("audit message parse err :%v", err)
}
}
}
return
Expand Down
Loading

0 comments on commit d41f632

Please sign in to comment.