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

cluster/audit: audit log in seconds maybe leads to file overwriten #879

Merged
merged 2 commits into from
Nov 3, 2020
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
6 changes: 3 additions & 3 deletions pkg/cluster/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func ShowAuditList(dir string) error {
if err != nil {
continue
}
t := time.Unix(ts, 0)
t := time.Unix(ts/1e9, 0)
cmd, err := firstLine(fi.Name())
if err != nil {
continue
Expand All @@ -82,7 +82,7 @@ func ShowAuditList(dir string) error {

// OutputAuditLog outputs audit log.
func OutputAuditLog(dir string, data []byte) error {
fname := filepath.Join(dir, base52.Encode(time.Now().Unix()))
fname := filepath.Join(dir, base52.Encode(time.Now().UnixNano()))
return ioutil.WriteFile(fname, data, 0644)
}

Expand All @@ -103,7 +103,7 @@ func ShowAuditLog(dir string, auditID string) error {
return errors.Trace(err)
}

t := time.Unix(ts, 0)
t := time.Unix(ts/1e9, 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not compatible with the existing audit files ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my careless, @9547 can your please open another PR to fix this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was inconsiderate, I'll fix in another PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #882 @lucklove @july2993 PTAL

hint := fmt.Sprintf("- OPERATION TIME: %s -", t.Format("2006-01-02T15:04:05"))
line := strings.Repeat("-", len(hint))
_, _ = os.Stdout.WriteString(color.MagentaString("%s\n%s\n%s\n", line, hint, line))
Expand Down
65 changes: 65 additions & 0 deletions pkg/cluster/audit/audit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package audit

import (
"os"
"path"
"path/filepath"
"runtime"

. "github.com/pingcap/check"
"golang.org/x/sync/errgroup"
)

var _ = Suite(&testAuditSuite{})

type testAuditSuite struct{}

func currentDir() string {
_, file, _, _ := runtime.Caller(0)
return filepath.Dir(file)
}

func auditDir() string {
return path.Join(currentDir(), "testdata", "audit")
}

func (s *testAuditSuite) SetUpSuite(c *C) {
_ = os.RemoveAll(auditDir())
_ = os.MkdirAll(auditDir(), 0777)
}

func (s *testAuditSuite) TearDownSuite(c *C) {
_ = os.RemoveAll(auditDir())
}

func (s *testAuditSuite) TestOutputAuditLog(c *C) {
dir := auditDir()
var g errgroup.Group
for i := 0; i < 20; i++ {
g.Go(func() error { return OutputAuditLog(dir, []byte("audit log")) })
}
err := g.Wait()
c.Assert(err, IsNil)

var paths []string
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
// simply filter the not relate files.
paths = append(paths, path)
return nil
})
c.Assert(err, IsNil)
c.Assert(len(paths), Equals, 20)
}
2 changes: 1 addition & 1 deletion tests/tiup-dm/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function tiup-dm() {
mkdir -p ~/.tiup/bin && cp -f ./root.json ~/.tiup/bin/
# echo "in function"
if [ -f "./bin/tiup-dm.test" ]; then
./bin/tiup-dm.test -test.coverprofile=./cover/cov.itest-$(date +'%s')-$RANDOM.out __DEVEL--i-heard-you-like-tests "$@"
./bin/tiup-dm.test -test.coverprofile=./cover/cov.itest-$(date +'%s')-$RANDOM.out __DEVEL--i-heard-you-like-tests "$@"
else
../../bin/tiup-dm "$@"
fi
Expand Down