This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
Add minimal privileges test for backup using dynamic privs #418
Open
morgo
wants to merge
5
commits into
pingcap:master
Choose a base branch
from
morgo:dynamicprivs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6c85312
Add minimal privileges test for backup using dynamic privs
morgo 510f452
Merge branch 'master' into dynamicprivs
morgo d060b1d
switch to ioutil package for tempdir
morgo b13bfb5
remove the need to set experimental flag
morgo 5956cda
Add restore_admin test
morgo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,5 +70,9 @@ | |
[ | ||
'/bin/tll', | ||
], | ||
'dynamicprivs'(args={}):: | ||
[ | ||
'/bin/dynamicprivs', | ||
], | ||
// +tipocket:scaffold:case_decls | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
_config+:: { | ||
case_name: 'dynamicprivs', | ||
image_name: 'hub.pingcap.net/qa/tipocket', | ||
args+: { | ||
// k8s configurations | ||
// 'storage-class': 'local-storage', | ||
}, | ||
command: {}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
GOARCH := $(if $(GOARCH),$(GOARCH),amd64) | ||
GO=GO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GOOS=$(GOOS) GOARCH=$(GOARCH) GO111MODULE=on go | ||
GOTEST=GO15VENDOREXPERIMENT="1" CGO_ENABLED=1 GO111MODULE=on go test # go race detector requires cgo | ||
VERSION := $(if $(VERSION),$(VERSION),latest) | ||
|
||
PACKAGES := go list ./...| grep -vE 'vendor' | ||
|
||
LDFLAGS += -s -w | ||
LDFLAGS += -X "github.com/pingcap/tipocket/pkg/test-infra/fixture.BuildTS=$(shell date -u '+%Y-%m-%d %I:%M:%S')" | ||
LDFLAGS += -X "github.com/pingcap/tipocket/pkg/test-infra/fixture.BuildHash=$(shell git rev-parse HEAD)" | ||
|
||
GOBUILD=$(GO) build -ldflags '$(LDFLAGS)' | ||
|
||
DOCKER_REGISTRY_PREFIX := $(if $(DOCKER_REGISTRY),$(DOCKER_REGISTRY)/,) | ||
|
||
default: tidy fmt lint build | ||
|
||
build: mod-sum dynamicprivs | ||
|
||
dynamicprivs: | ||
$(GOBUILD) $(GOMOD) -o bin/dynamicprivs cmd/*.go | ||
|
||
fmt: groupimports | ||
go fmt ./... | ||
|
||
mod-sum: | ||
$(GO) mod tidy | ||
|
||
tidy: | ||
@echo "go mod tidy" | ||
GO111MODULE=on go mod tidy | ||
@git diff --exit-code -- go.mod | ||
|
||
lint: revive | ||
@echo "linting" | ||
revive -formatter friendly -config revive.toml $$($(PACKAGES)) | ||
|
||
revive: | ||
ifeq (,$(shell which revive)) | ||
@echo "installing revive" | ||
$(GO) get github.com/mgechev/[email protected] | ||
endif | ||
|
||
groupimports: install-goimports | ||
goimports -w -l -local github.com/pingcap/tipocket . | ||
|
||
install-goimports: | ||
ifeq (,$(shell which goimports)) | ||
@echo "installing goimports" | ||
go get golang.org/x/tools/cmd/goimports | ||
endif | ||
|
||
clean: | ||
@rm -rf bin/* | ||
|
||
test: | ||
$(GOTEST) ./... | ||
|
||
.PHONY: all clean build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// Copyright 2021 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 dynamicprivs | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/ngaut/log" | ||
"github.com/pingcap/tipocket/pkg/cluster" | ||
"github.com/pingcap/tipocket/pkg/core" | ||
"github.com/pingcap/tipocket/pkg/test-infra/fixture" | ||
"github.com/pingcap/tipocket/util" | ||
) | ||
|
||
// CaseCreator is a creator of test client | ||
type CaseCreator struct{} | ||
|
||
// Create creates a test client | ||
func (c CaseCreator) Create(node cluster.ClientNode) core.Client { | ||
return &Client{} | ||
} | ||
|
||
// Client defines how our test case works | ||
type Client struct { | ||
db *sql.DB | ||
tempdir string | ||
} | ||
|
||
// SetUp implements the core.Client interface. | ||
func (c *Client) SetUp(ctx context.Context, _ []cluster.Node, clientNodes []cluster.ClientNode, idx int) error { | ||
log.Info("start to setup client...") | ||
node := clientNodes[idx] | ||
dsn := fmt.Sprintf("root@tcp(%s:%d)/test", node.IP, node.Port) | ||
util.SetMySQLProxy(fixture.Context.MySQLProxy) | ||
db, err := util.OpenDB(dsn, 1) | ||
if err != nil { | ||
log.Fatalf("open db error: %v", err) | ||
} | ||
|
||
util.MustExec(db, "drop table if exists t") | ||
util.MustExec(db, "create table t(id int)") | ||
util.MustExec(db, "insert into t(id) values(1)") | ||
|
||
// Create a user that only has the privileges to backup | ||
// and nothing else. Make sure the test confirms these | ||
// are the minimal privileges. | ||
|
||
util.MustExec(db, "DROP USER IF EXISTS backupadmin") | ||
util.MustExec(db, "CREATE USER backupadmin") | ||
util.MustExec(db, "GRANT BACKUP_ADMIN, RESTORE_ADMIN ON *.* TO backupadmin") | ||
|
||
if err := db.Close(); err != nil { | ||
log.Fatalf("could not close initial connection due to: %v", err) | ||
} | ||
|
||
dsn = fmt.Sprintf("backupadmin@tcp(%s:%d)/information_schema", node.IP, node.Port) | ||
db, err = util.OpenDB(dsn, 1) | ||
if err != nil { | ||
log.Fatalf("open db error: %v", err) | ||
} | ||
|
||
// Create a tempdir. | ||
c.tempdir, err = ioutil.TempDir("", "dynamicprivstest") | ||
if err != nil { | ||
log.Fatalf("failed to setup temp dir: %v", err) | ||
} | ||
|
||
c.db = db | ||
return nil | ||
} | ||
|
||
// TearDown implements the core.Client interface. | ||
func (c *Client) TearDown(ctx context.Context, nodes []cluster.ClientNode, idx int) error { | ||
// delete the tempdir. | ||
os.RemoveAll(c.tempdir) | ||
return nil | ||
} | ||
|
||
// Start implements the core.StandardClientExtensions interface. | ||
func (c *Client) Start(ctx context.Context, cfg interface{}, clientNodes []cluster.ClientNode) error { | ||
|
||
expectedLocation := fmt.Sprintf("local://%s", c.tempdir) | ||
backupStmt := fmt.Sprintf("BACKUP DATABASE * TO '%s'", expectedLocation) | ||
log.Info("creating a backup in: " + c.tempdir) | ||
rows, err := c.db.QueryContext(ctx, backupStmt) | ||
if err != nil { | ||
return err | ||
} | ||
var ( | ||
location string | ||
size string | ||
backupTs string | ||
queueTime string | ||
executionTime string | ||
) | ||
if rows.Next() { | ||
if err := rows.Scan(&location, &size, &backupTs, &queueTime, &executionTime); err != nil { | ||
return err | ||
} | ||
} | ||
if location != expectedLocation { | ||
return fmt.Errorf("expected backup to %s, but got %s", expectedLocation, location) | ||
} | ||
rows.Close() | ||
log.Info("backup complete") | ||
|
||
// In future we should try restoring as well, but it currently won't work due to | ||
// https://github.com/pingcap/tidb/issues/24912 | ||
restoreStmt := fmt.Sprintf("RESTORE DATABASE * FROM '%s'", expectedLocation) | ||
log.Info("restoring database from: ", expectedLocation) | ||
rows, err = c.db.QueryContext(ctx, restoreStmt) | ||
if err != nil { | ||
return err | ||
} | ||
defer rows.Close() | ||
log.Info("restore completed") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2021 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 main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
|
||
// use mysql | ||
_ "github.com/go-sql-driver/mysql" | ||
|
||
"github.com/pingcap/tipocket/cmd/util" | ||
logs "github.com/pingcap/tipocket/logsearch/pkg/logs" | ||
"github.com/pingcap/tipocket/pkg/cluster" | ||
"github.com/pingcap/tipocket/pkg/control" | ||
test_infra "github.com/pingcap/tipocket/pkg/test-infra" | ||
"github.com/pingcap/tipocket/pkg/test-infra/fixture" | ||
|
||
testcase "github.com/pingcap/tipocket/testcase/dynamicprivs" | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
cfg := control.Config{ | ||
Mode: control.ModeStandard, | ||
ClientCount: 1, | ||
RunTime: fixture.Context.RunTime, | ||
} | ||
c := fixture.Context | ||
suit := util.Suit{ | ||
Config: &cfg, | ||
Provider: cluster.NewDefaultClusterProvider(), | ||
ClientCreator: testcase.CaseCreator{}, | ||
NemesisGens: util.ParseNemesisGenerators(fixture.Context.Nemesis), | ||
ClusterDefs: test_infra.NewDefaultCluster(c.Namespace, c.ClusterName, c.TiDBClusterConfig), | ||
LogsClient: logs.NewDiagnosticLogClient(), | ||
} | ||
suit.Run(context.Background()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,48 @@ | ||||||
module github.com/pingcap/tipocket/testcase/dynamicprivs | ||||||
|
||||||
go 1.15 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Please bump the version to 1.16 |
||||||
|
||||||
require ( | ||||||
github.com/go-sql-driver/mysql v1.5.0 | ||||||
github.com/ngaut/log v0.0.0-20180314031856-b8e36e7ba5ac | ||||||
github.com/pingcap/tipocket v1.0.0 | ||||||
github.com/pingcap/tipocket/logsearch v1.0.0 | ||||||
) | ||||||
|
||||||
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0 | ||||||
|
||||||
replace github.com/prometheus/prometheus => github.com/prometheus/prometheus v1.8.2-0.20200213233353-b90be6f32a33 | ||||||
|
||||||
replace github.com/uber-go/atomic => go.uber.org/atomic v1.5.0 | ||||||
|
||||||
replace ( | ||||||
k8s.io/api => k8s.io/api v0.17.0 | ||||||
k8s.io/apiextensions-apiserver => k8s.io/apiextensions-apiserver v0.17.0 | ||||||
k8s.io/apimachinery => k8s.io/apimachinery v0.17.0 | ||||||
k8s.io/apiserver => k8s.io/apiserver v0.17.0 | ||||||
k8s.io/cli-runtime => k8s.io/cli-runtime v0.17.0 | ||||||
k8s.io/client-go => k8s.io/client-go v0.17.0 | ||||||
k8s.io/cloud-provider => k8s.io/cloud-provider v0.17.0 | ||||||
k8s.io/cluster-bootstrap => k8s.io/cluster-bootstrap v0.17.0 | ||||||
k8s.io/code-generator => k8s.io/code-generator v0.17.0 | ||||||
k8s.io/component-base => k8s.io/component-base v0.17.0 | ||||||
k8s.io/cri-api => k8s.io/cri-api v0.17.0 | ||||||
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.17.0 | ||||||
k8s.io/kube-aggregator => k8s.io/kube-aggregator v0.17.0 | ||||||
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.17.0 | ||||||
k8s.io/kube-proxy => k8s.io/kube-proxy v0.17.0 | ||||||
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.17.0 | ||||||
k8s.io/kubectl => k8s.io/kubectl v0.17.0 | ||||||
k8s.io/kubelet => k8s.io/kubelet v0.17.0 | ||||||
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.17.0 | ||||||
k8s.io/metrics => k8s.io/metrics v0.17.0 | ||||||
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.17.0 | ||||||
) | ||||||
|
||||||
replace github.com/Azure/go-autorest => github.com/Azure/go-autorest v12.2.0+incompatible | ||||||
|
||||||
replace golang.org/x/net v0.0.0-20190813000000-74dc4d7220e7 => golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 | ||||||
|
||||||
replace github.com/pingcap/tipocket => ../../. | ||||||
|
||||||
replace github.com/pingcap/tipocket/logsearch => ../../logsearch |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove the blank line.