diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 47e14076..707ebfa1 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -251,6 +251,10 @@ var completeCmds = map[string]complete.Predictor{ "/event/list": s3Complete{deepLevel: 2}, "/event/remove": s3Complete{deepLevel: 2}, + "/encrypt/set": s3Complete{deepLevel: 2}, + "/encrypt/info": s3Complete{deepLevel: 2}, + "/encrypt/clear": s3Complete{deepLevel: 2}, + "/tag/list": s3Completer, "/tag/remove": s3Completer, "/tag/set": s3Completer, diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 2c41e41f..79b84636 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -1170,3 +1170,27 @@ func (f *fsClient) RemoveReplication(ctx context.Context) *probe.Error { }) } + +// Get encryption info for a bucket, not implemented. +func (f *fsClient) GetEncryption(ctx context.Context) (string, string, *probe.Error) { + return "", "", probe.NewError(APINotImplemented{ + API: "GetEncryption", + APIType: "filesystem", + }) +} + +// SetEncryption - Set encryption configuration on a bucket, not implemented +func (f *fsClient) SetEncryption(ctx context.Context, algorithm, keyID string) *probe.Error { + return probe.NewError(APINotImplemented{ + API: "SetEncryption", + APIType: "filesystem", + }) +} + +// DeleteEncryption - removes encryption configuration on a bucket, not implemented +func (f *fsClient) DeleteEncryption(ctx context.Context) *probe.Error { + return probe.NewError(APINotImplemented{ + API: "DeleteEncryption", + APIType: "filesystem", + }) +} diff --git a/cmd/client-s3.go b/cmd/client-s3.go index ec8bce70..1b42b627 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -45,6 +45,8 @@ import ( "github.com/minio/minio-go/v7/pkg/notification" "github.com/minio/minio-go/v7/pkg/policy" "github.com/minio/minio-go/v7/pkg/replication" + "github.com/minio/minio-go/v7/pkg/sse" + "github.com/minio/minio-go/v7/pkg/s3utils" "github.com/minio/minio-go/v7/pkg/tags" "github.com/minio/minio/pkg/mimedb" @@ -2446,3 +2448,57 @@ func (c *S3Client) SetReplication(ctx context.Context, cfg *replication.Config) } return nil } + +// GetEncryption - gets bucket encryption info. +func (c *S3Client) GetEncryption(ctx context.Context) (algorithm, keyID string, err *probe.Error) { + bucket, _ := c.url2BucketAndObject() + if bucket == "" { + return "", "", probe.NewError(BucketNameEmpty{}) + } + + config, e := c.api.GetBucketEncryption(ctx, bucket) + if e != nil { + return "", "", probe.NewError(e) + } + for _, rule := range config.Rules { + algorithm = rule.Apply.SSEAlgorithm + if rule.Apply.KmsMasterKeyID != "" { + keyID = rule.Apply.KmsMasterKeyID + break + } + } + return algorithm, keyID, nil +} + +// SetEncryption - Set encryption configuration on a bucket +func (c *S3Client) SetEncryption(ctx context.Context, encType string, kmsKeyID string) *probe.Error { + bucket, _ := c.url2BucketAndObject() + if bucket == "" { + return probe.NewError(BucketNameEmpty{}) + } + var config *sse.Configuration + switch strings.ToLower(encType) { + case "sse-kms": + config = sse.NewConfigurationSSEKMS(kmsKeyID) + case "sse-s3": + config = sse.NewConfigurationSSES3() + default: + return probe.NewError(fmt.Errorf("Invalid encryption algorithm %s", encType)) + } + if err := c.api.SetBucketEncryption(ctx, bucket, config); err != nil { + return probe.NewError(err) + } + return nil +} + +// DeleteEncryption - removes encryption configuration on a bucket +func (c *S3Client) DeleteEncryption(ctx context.Context) *probe.Error { + bucket, _ := c.url2BucketAndObject() + if bucket == "" { + return probe.NewError(BucketNameEmpty{}) + } + if err := c.api.RemoveBucketEncryption(ctx, bucket); err != nil { + return probe.NewError(err) + } + return nil +} diff --git a/cmd/client.go b/cmd/client.go index 568d116b..121f3015 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -143,6 +143,10 @@ type Client interface { GetReplication(ctx context.Context) (replication.Config, *probe.Error) SetReplication(ctx context.Context, cfg *replication.Config) *probe.Error RemoveReplication(ctx context.Context) *probe.Error + // Encryption operations + GetEncryption(ctx context.Context) (string, string, *probe.Error) + SetEncryption(ctx context.Context, algorithm, kmsKeyID string) *probe.Error + DeleteEncryption(ctx context.Context) *probe.Error } // ClientContent - Content container for content metadata diff --git a/cmd/encrypt-clear.go b/cmd/encrypt-clear.go new file mode 100644 index 00000000..67ab0c97 --- /dev/null +++ b/cmd/encrypt-clear.go @@ -0,0 +1,96 @@ +/* + * MinIO Client (C) 2020 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 ( + "context" + "fmt" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio/pkg/console" +) + +var encryptClearCmd = cli.Command{ + Name: "clear", + Usage: "Clear encryption config", + Action: mainEncryptClear, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Remove auto encryption config on bucket "mybucket" for alias "myminio". + {{.Prompt}} {{.HelpName}} myminio/mybucket +`, +} + +// checkEncryptClearSyntax - validate all the passed arguments +func checkEncryptClearSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "clear", 1) // last argument is exit code + } +} + +type encryptClearMessage struct { + Op string `json:"op"` + Status string `json:"status"` + URL string `json:"url"` +} + +func (v encryptClearMessage) JSON() string { + v.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(v, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func (v encryptClearMessage) String() string { + return console.Colorize("encryptClearMessage", fmt.Sprintf("Auto encryption configuration has been cleared successfully for %s", v.URL)) +} + +func mainEncryptClear(cliCtx *cli.Context) error { + ctx, cancelencryptClear := context.WithCancel(globalContext) + defer cancelencryptClear() + + console.SetColor("encryptClearMessage", color.New(color.FgGreen)) + + checkEncryptClearSyntax(cliCtx) + + // Get the alias parameter from cli + args := cliCtx.Args() + aliasedURL := args.Get(0) + // Create a new Client + client, err := newClient(aliasedURL) + fatalIf(err, "Unable to initialize connection.") + fatalIf(client.DeleteEncryption(ctx), "Cannot clear auto encryption configuration") + printMsg(encryptClearMessage{ + Op: "clear", + Status: "success", + URL: aliasedURL, + }) + return nil +} diff --git a/cmd/encrypt-info.go b/cmd/encrypt-info.go new file mode 100644 index 00000000..a0059582 --- /dev/null +++ b/cmd/encrypt-info.go @@ -0,0 +1,115 @@ +/* + * MinIO Client (C) 2020 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 ( + "context" + "fmt" + "strings" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio/pkg/console" +) + +var encryptInfoCmd = cli.Command{ + Name: "info", + Usage: "Show bucket encryption status", + Action: mainEncryptInfo, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Display bucket encryption status for bucket "mybucket". + {{.Prompt}} {{.HelpName}} myminio/mybucket +`, +} + +// checkversionInfoSyntax - validate all the passed arguments +func checkEncryptInfoSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "info", 1) // last argument is exit code + } +} + +type encryptInfoMessage struct { + Op string `json:"op"` + Status string `json:"status"` + URL string `json:"url"` + Encryption struct { + Algorithm string `json:"algorithm,omitempty"` + KeyID string `json:"keyId,omitempty"` + } `json:"encryption,omitempty"` +} + +func (v encryptInfoMessage) JSON() string { + v.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(v, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func (v encryptInfoMessage) String() string { + msg := "" + switch v.Encryption.Algorithm { + case "": + msg = fmt.Sprintf("Auto encryption is not enabled for %s ", v.URL) + default: + msg = fmt.Sprintf("Auto encryption configuration:\n\tAlgorithm: %s ", strings.ToUpper(v.Encryption.Algorithm)) + } + if v.Encryption.KeyID != "" { + msg += fmt.Sprintf("\n\tKey ID : %s", v.Encryption.KeyID) + } + return console.Colorize("encryptInfoMessage", msg) +} + +func mainEncryptInfo(cliCtx *cli.Context) error { + ctx, cancelEncryptInfo := context.WithCancel(globalContext) + defer cancelEncryptInfo() + + console.SetColor("encryptInfoMessage", color.New(color.FgGreen)) + + checkEncryptInfoSyntax(cliCtx) + + // Get the alias parameter from cli + args := cliCtx.Args() + aliasedURL := args.Get(0) + // Create a new Client + client, err := newClient(aliasedURL) + fatalIf(err, "Unable to initialize connection.") + algorithm, keyID, e := client.GetEncryption(ctx) + fatalIf(e, "Cannot get encryption info") + msg := encryptInfoMessage{ + Op: "info", + Status: "success", + URL: aliasedURL, + } + msg.Encryption.Algorithm = algorithm + msg.Encryption.KeyID = keyID + printMsg(msg) + return nil +} diff --git a/cmd/encrypt-main.go b/cmd/encrypt-main.go new file mode 100644 index 00000000..ee26f933 --- /dev/null +++ b/cmd/encrypt-main.go @@ -0,0 +1,40 @@ +/* + * MinIO Client (C) 2020 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 "github.com/minio/cli" + +var encryptCmd = cli.Command{ + Name: "encrypt", + Usage: "manage bucket encryption config", + HideHelpCommand: true, + Action: mainEncrypt, + Before: setGlobalsFromContext, + Flags: globalFlags, + Subcommands: []cli.Command{ + encryptSetCmd, + encryptClearCmd, + encryptInfoCmd, + }, +} + +// mainEncrypt is the handle for "mc encrypt" command. +func mainEncrypt(ctx *cli.Context) error { + cli.ShowCommandHelp(ctx, ctx.Args().First()) + return nil + // Sub-commands like "info", "set", "clear" have their own main. +} diff --git a/cmd/encrypt-set.go b/cmd/encrypt-set.go new file mode 100644 index 00000000..82f69f9e --- /dev/null +++ b/cmd/encrypt-set.go @@ -0,0 +1,117 @@ +/* + * MinIO Client (C) 2020 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 ( + "context" + "fmt" + "strings" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/mc/pkg/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/minio/pkg/console" +) + +var encryptSetCmd = cli.Command{ + Name: "set", + Usage: "Set encryption config", + Action: mainEncryptSet, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Enable SSE-S3 auto encryption on bucket "mybucket" for alias "myminio". + {{.Prompt}} {{.HelpName}} set sse-s3 myminio/mybucket + + 2. Enable SSE-KMS auto encryption with kms key on bucket "mybucket" for alias "s3". + {{.Prompt}} {{.HelpName}} set sse-kms arn:aws:kms:us-east-1:xxx:key/xxx myminio/mybucket +`, +} + +// checkEncryptSetSyntax - validate all the passed arguments +func checkEncryptSetSyntax(ctx *cli.Context) { + if len(ctx.Args()) < 2 || len(ctx.Args()) > 3 { + cli.ShowCommandHelpAndExit(ctx, "set", 1) // last argument is exit code + } +} + +type encryptSetMessage struct { + Op string `json:"op"` + Status string `json:"status"` + URL string `json:"url"` + Encryption struct { + Algorithm string `json:"algorithm,omitempty"` + KeyID string `json:"keyId,omitempty"` + } `json:"encryption,omitempty"` +} + +func (v encryptSetMessage) JSON() string { + v.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(v, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + return string(jsonMessageBytes) +} + +func (v encryptSetMessage) String() string { + return console.Colorize("encryptSetMessage", fmt.Sprintf("Auto encryption configuration has been set successfully for %s", v.URL)) +} + +func mainEncryptSet(cliCtx *cli.Context) error { + ctx, cancelencryptSet := context.WithCancel(globalContext) + defer cancelencryptSet() + + console.SetColor("encryptSetMessage", color.New(color.FgGreen)) + + checkEncryptSetSyntax(cliCtx) + + // Get the alias parameter from cli + args := cliCtx.Args() + aliasedURL := args.Get(len(args) - 1) + // Create a new Client + client, err := newClient(aliasedURL) + fatalIf(err, "Unable to initialize connection.") + var algorithm, keyID string + switch len(args) { + case 3: + algorithm = strings.ToLower(args[0]) + keyID = args[1] + case 2: + algorithm = strings.ToLower(args[0]) + } + if algorithm != "sse-s3" && algorithm != "sse-kms" { + fatalIf(probe.NewError(fmt.Errorf("Unknown argument `%s` passed", algorithm)), "Invalid encryption algorithm") + } + fatalIf(client.SetEncryption(ctx, algorithm, keyID), "Cannot enable auto encryption") + msg := encryptSetMessage{ + Op: "set", + Status: "success", + URL: aliasedURL, + } + msg.Encryption.Algorithm = algorithm + printMsg(msg) + return nil +} diff --git a/cmd/main.go b/cmd/main.go index 3db86f4f..98f0f95f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -340,6 +340,7 @@ var appCmds = []cli.Command{ diffCmd, rmCmd, eventCmd, + encryptCmd, watchCmd, policyCmd, tagCmd, diff --git a/docs/minio-client-complete-guide.md b/docs/minio-client-complete-guide.md index c7ecca97..e20be93e 100644 --- a/docs/minio-client-complete-guide.md +++ b/docs/minio-client-complete-guide.md @@ -26,9 +26,11 @@ diff list differences in object name, size, and date between two buckets rm remove objects event manage object notifications bucket manage bucket configuration +encrypt manage bucket encryption config watch listen for object notification events policy manage anonymous access to buckets and objects tag manage tags for bucket(s) and object(s) +bucket manage bucket(s) admin manage MinIO servers update update mc to latest release ``` @@ -304,7 +306,7 @@ mc version RELEASE.2020-04-25T00-43-23Z | [**share** - generate URL for temporary access to an object](#share) | [**rm** - remove objects](#rm) | [**find** - find files and objects](#find) | | | [**diff** - list differences in object name, size, and date between two buckets](#diff) | [**mirror** - synchronize object(s) to a remote site](#mirror) | [**ilm** - manage bucket lifecycle](#ilm) | | | [**alias** - manage aliases](#alias) | [**policy** - set public policy on bucket or prefix](#policy) | [**event** - manage events on your buckets](#event) |[**replicate** - manage bucket replication](#replicate) | -| [**update** - manage software updates](#update) | [**watch** - watch for events](#watch) | [**stat** - stat contents of objects and folders](#stat) | | +| [**update** - manage software updates](#update) | [**watch** - watch for events](#watch) | [**stat** - stat contents of objects and folders](#stat) | [**encrypt** - manage bucket encryption](#encrypt) | | [**head** - display first 'n' lines of an object](#head) | [**lock** - manage default bucket object lock configuration](#lock) | [**retention** - set retention for object(s)](#retention) | | | [**mv** - move objects](#mv) | [**sql** - run sql queries on objects](#sql) | [**legalhold** - set legal hold for object(s)](#legalhold) | | @@ -1609,3 +1611,49 @@ myminio/mybucket versioning status is enabled mc bucket version myminio/mybucket suspend myminio/mybucket versioning is suspended ``` + +### Command `encrypt` +`encrypt` manages bucket encryption config + +``` +NAME: + mc encrypt - manage bucket encryption config + +USAGE: + mc encrypt COMMAND [COMMAND FLAGS | -h] [ARGUMENTS...] + +COMMANDS: + set Set encryption config + clear Clear encryption config + info Show bucket encryption status + +FLAGS: + --help, -h show help +``` + +*Example: Display bucket encryption status for bucket `mybucket`* + +``` +mc encrypt info myminio/mybucket +Algorithm: AES256 +``` + +*Example: Set SSE-S3 auto encryption for bucket `mybucket` on alias `myminio`* + +``` +mc encrypt set sse-s3 myminio/mybucket +Auto encryption has been set successfully for myminio/source +``` +*Example: Set SSE-KMS auto encryption for bucket `mybucket` on alias `myminio` with KMS Key Id "arn:aws:kms:us-east-1:xxx:key/xxx"* + +``` +mc encrypt set sse-kms "arn:aws:kms:us-east-1:xxx:key/xxx" myminio/mybucket +Auto encryption has been set successfully for myminio/source +``` + +*Example: Clear auto encryption config for bucket `mybucket` on alias `myminio`* + +``` +mc encrypt clear myminio/mybucket +Auto encryption configuration has been cleared successfully. +``` \ No newline at end of file