add lock command (#2880)
`lock` command enables to set, get and clear object lock configuration of given bucket.
This commit is contained in:
+2
-1
@@ -987,10 +987,11 @@ func (f *fsClient) listRecursiveInRoutine(contentCh chan *clientContent) {
|
||||
}
|
||||
|
||||
// MakeBucket - create a new bucket.
|
||||
func (f *fsClient) MakeBucket(region string, ignoreExisting bool) *probe.Error {
|
||||
func (f *fsClient) MakeBucket(region string, ignoreExisting, withLock bool) *probe.Error {
|
||||
// TODO: ignoreExisting has no effect currently. In the future, we want
|
||||
// to call os.Mkdir() when ignoredExisting is disabled and os.MkdirAll()
|
||||
// otherwise.
|
||||
// NOTE: withLock=true has no meaning here.
|
||||
e := os.MkdirAll(f.PathURL.Path, 0777)
|
||||
if e != nil {
|
||||
return probe.NewError(e)
|
||||
|
||||
@@ -194,7 +194,7 @@ func (s *TestSuite) TestPutBucket(c *C) {
|
||||
bucketPath := filepath.Join(root, "bucket")
|
||||
fsClient, err := fsNew(bucketPath)
|
||||
c.Assert(err, IsNil)
|
||||
err = fsClient.MakeBucket("us-east-1", true)
|
||||
err = fsClient.MakeBucket("us-east-1", true, false)
|
||||
c.Assert(err, IsNil)
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ func (s *TestSuite) TestStatBucket(c *C) {
|
||||
|
||||
fsClient, err := fsNew(bucketPath)
|
||||
c.Assert(err, IsNil)
|
||||
err = fsClient.MakeBucket("us-east-1", true)
|
||||
err = fsClient.MakeBucket("us-east-1", true, false)
|
||||
c.Assert(err, IsNil)
|
||||
_, err = fsClient.Stat(false, false, false, nil)
|
||||
c.Assert(err, IsNil)
|
||||
@@ -223,7 +223,7 @@ func (s *TestSuite) TestBucketACLFails(c *C) {
|
||||
bucketPath := filepath.Join(root, "bucket")
|
||||
fsClient, err := fsNew(bucketPath)
|
||||
c.Assert(err, IsNil)
|
||||
err = fsClient.MakeBucket("us-east-1", true)
|
||||
err = fsClient.MakeBucket("us-east-1", true, false)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
// On windows setting permissions is not supported.
|
||||
|
||||
+31
-2
@@ -977,7 +977,7 @@ func (c *s3Client) Remove(isIncomplete, isRemoveBucket bool, contentCh <-chan *c
|
||||
}
|
||||
|
||||
// MakeBucket - make a new bucket.
|
||||
func (c *s3Client) MakeBucket(region string, ignoreExisting bool) *probe.Error {
|
||||
func (c *s3Client) MakeBucket(region string, ignoreExisting, withLock bool) *probe.Error {
|
||||
bucket, object := c.url2BucketAndObject()
|
||||
if bucket == "" {
|
||||
return probe.NewError(BucketNameEmpty{})
|
||||
@@ -1001,7 +1001,12 @@ func (c *s3Client) MakeBucket(region string, ignoreExisting bool) *probe.Error {
|
||||
return probe.NewError(BucketNameTopLevel{})
|
||||
}
|
||||
|
||||
e := c.api.MakeBucket(bucket, region)
|
||||
var e error
|
||||
if withLock {
|
||||
e = c.api.MakeBucketWithObjectLock(bucket, region)
|
||||
} else {
|
||||
e = c.api.MakeBucket(bucket, region)
|
||||
}
|
||||
if e != nil {
|
||||
// Ignore bucket already existing error when ignoreExisting flag is enabled
|
||||
if ignoreExisting {
|
||||
@@ -1959,3 +1964,27 @@ func (c *s3Client) ShareUpload(isRecursive bool, expires time.Duration, contentT
|
||||
}
|
||||
return u.String(), m, nil
|
||||
}
|
||||
|
||||
// Set object lock configurataion of bucket.
|
||||
func (c *s3Client) SetObjectLockConfig(mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit) *probe.Error {
|
||||
bucket, _ := c.url2BucketAndObject()
|
||||
|
||||
err := c.api.SetBucketObjectLockConfig(bucket, mode, validity, unit)
|
||||
if err != nil {
|
||||
return probe.NewError(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get object lock configuration of bucket.
|
||||
func (c *s3Client) GetObjectLockConfig() (mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, perr *probe.Error) {
|
||||
bucket, _ := c.url2BucketAndObject()
|
||||
|
||||
mode, validity, unit, err := c.api.GetBucketObjectLockConfig(bucket)
|
||||
if err != nil {
|
||||
return nil, nil, nil, probe.NewError(err)
|
||||
}
|
||||
|
||||
return mode, validity, unit, nil
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ func (s *TestSuite) TestBucketOperations(c *C) {
|
||||
s3c, err := s3New(conf)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
err = s3c.MakeBucket("us-east-1", true)
|
||||
err = s3c.MakeBucket("us-east-1", true, false)
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
conf.HostURL = server.URL + string(s3c.GetURL().Separator)
|
||||
|
||||
+2
-2
@@ -23,7 +23,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
"github.com/minio/minio-go/v6"
|
||||
minio "github.com/minio/minio-go/v6"
|
||||
"github.com/minio/minio-go/v6/pkg/encrypt"
|
||||
)
|
||||
|
||||
@@ -49,7 +49,7 @@ type Client interface {
|
||||
List(isRecursive, isIncomplete bool, showDir DirOpt) <-chan *clientContent
|
||||
|
||||
// Bucket operations
|
||||
MakeBucket(region string, ignoreExisting bool) *probe.Error
|
||||
MakeBucket(region string, ignoreExisting, withLock bool) *probe.Error
|
||||
|
||||
// Access policy operations.
|
||||
GetAccess() (access string, policyJSON string, error *probe.Error)
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* MinIO Client (C) 2019 MinIO, 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,
|
||||
* 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 cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
json "github.com/minio/mc/pkg/colorjson"
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
minio "github.com/minio/minio-go/v6"
|
||||
)
|
||||
|
||||
var (
|
||||
lockFlags = []cli.Flag{
|
||||
cli.BoolFlag{
|
||||
Name: "clear, c",
|
||||
Usage: "clears previously stored object lock configuration",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
var lockCmd = cli.Command{
|
||||
Name: "lock",
|
||||
Usage: "set and get object lock configuration",
|
||||
Action: mainLock,
|
||||
Before: setGlobalsFromContext,
|
||||
Flags: append(lockFlags, globalFlags...),
|
||||
CustomHelpTemplate: `NAME:
|
||||
{{.HelpName}} - {{.Usage}}
|
||||
|
||||
USAGE:
|
||||
{{.HelpName}} [FLAGS] TARGET [GOVERNANCE | COMPLIANCE] [VALIDITY]
|
||||
|
||||
FLAGS:
|
||||
{{range .VisibleFlags}}{{.}}
|
||||
{{end}}
|
||||
VALIDITY:
|
||||
This argument must be formatted like Nd or Ny where 'd' denotes days and 'y' denotes years e.g. 10d, 3y.
|
||||
|
||||
EXAMPLES:
|
||||
1. Set object lock configuration
|
||||
$ {{.HelpName}} myminio/mybucket GOVERNANCE 30d
|
||||
|
||||
2. Get object lock configuration
|
||||
$ {{.HelpName}} myminio/mybucket
|
||||
|
||||
3. Clear object lock configuration
|
||||
$ {{.HelpName}} --clear myminio/mybucket
|
||||
`,
|
||||
}
|
||||
|
||||
// Structured message depending on the type of console.
|
||||
type lockCmdMessage struct {
|
||||
Enabled string `json:"enabled"`
|
||||
Mode *minio.RetentionMode `json:"mode"`
|
||||
Validity *string `json:"validity"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// Colorized message for console printing.
|
||||
func (m lockCmdMessage) String() string {
|
||||
if m.Mode == nil {
|
||||
return fmt.Sprintf("No mode is enabled")
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s mode is enabled for %s", console.Colorize("Mode", *m.Mode), console.Colorize("Validity", *m.Validity))
|
||||
}
|
||||
|
||||
// JSON'ified message for scripting.
|
||||
func (m lockCmdMessage) JSON() string {
|
||||
msgBytes, e := json.MarshalIndent(m, "", " ")
|
||||
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
|
||||
return string(msgBytes)
|
||||
}
|
||||
|
||||
// lock - set/get object lock configuration.
|
||||
func lock(urlStr string, mode *minio.RetentionMode, validity *uint, unit *minio.ValidityUnit, clearLock bool) error {
|
||||
client, err := newClient(urlStr)
|
||||
if err != nil {
|
||||
fatalIf(err.Trace(), "Cannot parse the provided url.")
|
||||
}
|
||||
|
||||
s3Client, ok := client.(*s3Client)
|
||||
if !ok {
|
||||
fatalIf(errDummy().Trace(), "The provided url doesn't point to a S3 server.")
|
||||
}
|
||||
|
||||
validityStr := func() *string {
|
||||
if validity == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
unitStr := "d"
|
||||
if *unit == minio.Years {
|
||||
unitStr = "y"
|
||||
}
|
||||
s := fmt.Sprint(*validity, unitStr)
|
||||
return &s
|
||||
}
|
||||
|
||||
if clearLock || mode != nil {
|
||||
err = s3Client.SetObjectLockConfig(mode, validity, unit)
|
||||
fatalIf(err, "Cannot enable object lock configuration on the specified bucket.")
|
||||
} else {
|
||||
mode, validity, unit, err = s3Client.GetObjectLockConfig()
|
||||
fatalIf(err, "Cannot get object lock configuration on the specified bucket.")
|
||||
}
|
||||
|
||||
printMsg(lockCmdMessage{
|
||||
Enabled: "Enabled",
|
||||
Mode: mode,
|
||||
Validity: validityStr(),
|
||||
Status: "success",
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// main for lock command.
|
||||
func mainLock(ctx *cli.Context) error {
|
||||
console.SetColor("Mode", color.New(color.FgCyan, color.Bold))
|
||||
console.SetColor("Validity", color.New(color.FgYellow))
|
||||
|
||||
// Parse encryption keys per command.
|
||||
_, err := getEncKeys(ctx)
|
||||
fatalIf(err, "Unable to parse encryption keys.")
|
||||
|
||||
// lock specific flags.
|
||||
clearLock := ctx.Bool("clear")
|
||||
|
||||
args := ctx.Args()
|
||||
|
||||
var urlStr string
|
||||
var mode *minio.RetentionMode
|
||||
var validity *uint
|
||||
var unit *minio.ValidityUnit
|
||||
|
||||
switch l := len(args); l {
|
||||
case 1:
|
||||
urlStr = args[0]
|
||||
|
||||
case 3:
|
||||
urlStr = args[0]
|
||||
if clearLock {
|
||||
fatalIf(probe.NewError(errors.New("invalid argument")), "clear flag must be passed with target alone")
|
||||
}
|
||||
|
||||
m := minio.RetentionMode(args[1])
|
||||
if !m.IsValid() {
|
||||
fatalIf(probe.NewError(errors.New("invalid argument")), "invalid retention mode '%v'", m)
|
||||
}
|
||||
|
||||
mode = &m
|
||||
|
||||
validityStr := args[2]
|
||||
unitStr := string(validityStr[len(validityStr)-1])
|
||||
|
||||
validityStr = validityStr[:len(validityStr)-1]
|
||||
ui64, err := strconv.ParseUint(validityStr, 10, 64)
|
||||
if err != nil {
|
||||
fatalIf(probe.NewError(errors.New("invalid argument")), "invalid validity '%v'", args[2])
|
||||
}
|
||||
u := uint(ui64)
|
||||
validity = &u
|
||||
|
||||
switch unitStr {
|
||||
case "d", "D":
|
||||
d := minio.Days
|
||||
unit = &d
|
||||
case "y", "Y":
|
||||
y := minio.Years
|
||||
unit = &y
|
||||
default:
|
||||
fatalIf(probe.NewError(errors.New("invalid argument")), "invalid validity format '%v'", args[2])
|
||||
}
|
||||
default:
|
||||
cli.ShowCommandHelpAndExit(ctx, "lock", 1)
|
||||
}
|
||||
|
||||
return lock(urlStr, mode, validity, unit, clearLock)
|
||||
}
|
||||
@@ -322,6 +322,7 @@ var appCmds = []cli.Command{
|
||||
statCmd,
|
||||
treeCmd,
|
||||
duCmd,
|
||||
lockCmd,
|
||||
diffCmd,
|
||||
rmCmd,
|
||||
eventCmd,
|
||||
|
||||
+9
-1
@@ -35,6 +35,10 @@ var (
|
||||
Name: "ignore-existing, p",
|
||||
Usage: "ignore if bucket/directory already exists",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "with-lock, l",
|
||||
Usage: "enable object lock",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -72,6 +76,9 @@ EXAMPLES:
|
||||
|
||||
6. Ignore if bucket/directory already exists.
|
||||
{{.Prompt}} {{.HelpName}} --ignore-existing myminio/mynewbucket
|
||||
|
||||
7. Create a new bucket on Amazon S3 cloud storage in region 'us-west-2' with object lock enabled.
|
||||
{{.Prompt}} {{.HelpName}} --with-lock --region=us-west-2 s3/myregionbucket
|
||||
`,
|
||||
}
|
||||
|
||||
@@ -114,6 +121,7 @@ func mainMakeBucket(ctx *cli.Context) error {
|
||||
// Save region.
|
||||
region := ctx.String("region")
|
||||
ignoreExisting := ctx.Bool("p")
|
||||
withLock := ctx.Bool("l")
|
||||
|
||||
var cErr error
|
||||
for _, targetURL := range ctx.Args() {
|
||||
@@ -126,7 +134,7 @@ func mainMakeBucket(ctx *cli.Context) error {
|
||||
}
|
||||
|
||||
// Make bucket.
|
||||
err = clnt.MakeBucket(region, ignoreExisting)
|
||||
err = clnt.MakeBucket(region, ignoreExisting, withLock)
|
||||
if err != nil {
|
||||
switch err.ToGoError().(type) {
|
||||
case BucketNameEmpty:
|
||||
|
||||
+1
-1
@@ -715,7 +715,7 @@ func runMirror(srcURL, dstURL string, ctx *cli.Context, encKeyDB map[string][]pr
|
||||
|
||||
if d.Diff == differInFirst {
|
||||
// Bucket only exists in the source, create the same bucket in the destination
|
||||
if err := newDstClt.MakeBucket(ctx.String("region"), false); err != nil {
|
||||
if err := newDstClt.MakeBucket(ctx.String("region"), false, false); err != nil {
|
||||
errorIf(err, "Cannot created bucket in `"+newTgtURL+"`.")
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -325,6 +325,7 @@ USAGE:
|
||||
FLAGS:
|
||||
--region value specify bucket region; defaults to 'us-east-1' (default: "us-east-1")
|
||||
--ignore-existing, -p ignore if bucket/directory already exists
|
||||
--with-lock, -l enable object lock
|
||||
--help, -h show help
|
||||
|
||||
```
|
||||
|
||||
@@ -16,7 +16,7 @@ require (
|
||||
github.com/mattn/go-runewidth v0.0.5 // indirect
|
||||
github.com/minio/cli v1.22.0
|
||||
github.com/minio/minio v0.0.0-20191105141826-1e457dda7eed
|
||||
github.com/minio/minio-go/v6 v6.0.40
|
||||
github.com/minio/minio-go/v6 v6.0.41
|
||||
github.com/minio/sha256-simd v0.1.1
|
||||
github.com/mitchellh/go-homedir v1.1.0
|
||||
github.com/pkg/profile v1.3.0
|
||||
|
||||
Reference in New Issue
Block a user