Skip to content

Commit

Permalink
Add commands for managing Bucket CORS
Browse files Browse the repository at this point in the history
  • Loading branch information
marktheunissen committed Aug 22, 2024
1 parent aca5c18 commit 1e53784
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/auto-complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ var completeCmds = map[string]complete.Predictor{
"/quota/clear": aliasCompleter,
"/put": complete.PredictOr(s3Completer, fsCompleter),
"/get": complete.PredictOr(s3Completer, fsCompleter),

"/cors/set": s3Complete{deepLevel: 2},
"/cors/get": s3Complete{deepLevel: 2},
"/cors/remove": s3Complete{deepLevel: 2},
}

// flagsToCompleteFlags transforms a cli.Flag to complete.Flags
Expand Down
25 changes: 25 additions & 0 deletions cmd/client-fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/minio/mc/pkg/hookreader"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/cors"
"github.com/minio/minio-go/v7/pkg/encrypt"
"github.com/minio/minio-go/v7/pkg/lifecycle"
"github.com/minio/minio-go/v7/pkg/notification"
Expand Down Expand Up @@ -1507,3 +1508,27 @@ func (f *fsClient) GetPart(_ context.Context, _ int) (io.ReadCloser, *probe.Erro
APIType: "filesystem",
})
}

// GetBucketCors - not implemented
func (f *fsClient) GetBucketCors(_ context.Context) (*cors.Config, *probe.Error) {
return nil, probe.NewError(APINotImplemented{
API: "GetBucketCors",
APIType: "filesystem",
})
}

// SetBucketCors - not implemented
func (f *fsClient) SetBucketCors(_ context.Context, _ []byte) *probe.Error {
return probe.NewError(APINotImplemented{
API: "SetBucketCors",
APIType: "filesystem",
})
}

// DeleteBucketCors - not implemented
func (f *fsClient) DeleteBucketCors(_ context.Context) *probe.Error {
return probe.NewError(APINotImplemented{
API: "DeleteBucketCors",
APIType: "filesystem",
})
}
50 changes: 50 additions & 0 deletions cmd/client-s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"sync"
"time"

"github.com/minio/minio-go/v7/pkg/cors"
"github.com/minio/pkg/v3/env"

"github.com/minio/minio-go/v7"
Expand Down Expand Up @@ -3001,3 +3002,52 @@ func (c *S3Client) GetPart(ctx context.Context, part int) (io.ReadCloser, *probe
}
return reader, nil
}

// SetBucketCors - Set bucket cors configuration.
func (c *S3Client) SetBucketCors(ctx context.Context, corsXML []byte) *probe.Error {
bucketName, _ := c.url2BucketAndObject()
if bucketName == "" {
return probe.NewError(BucketNameEmpty{})
}

corsCfg, err := cors.ParseBucketCorsConfig(bytes.NewReader(corsXML))
if err != nil {
return probe.NewError(err)
}

err = c.api.SetBucketCors(ctx, bucketName, corsCfg)
if err != nil {
return probe.NewError(err)
}
return nil
}

// GetBucketCors - Get bucket cors configuration.
func (c *S3Client) GetBucketCors(ctx context.Context) (*cors.Config, *probe.Error) {
bucketName, _ := c.url2BucketAndObject()
if bucketName == "" {
return nil, probe.NewError(BucketNameEmpty{})
}

corsCfg, err := c.api.GetBucketCors(ctx, bucketName)
if err != nil {
return nil, probe.NewError(err)
}

return corsCfg, nil
}

// DeleteBucketCors - Delete bucket cors configuration.
func (c *S3Client) DeleteBucketCors(ctx context.Context) *probe.Error {
bucketName, _ := c.url2BucketAndObject()
if bucketName == "" {
return probe.NewError(BucketNameEmpty{})
}

err := c.api.SetBucketCors(ctx, bucketName, nil)
if err != nil {
return probe.NewError(err)
}

return nil
}
6 changes: 6 additions & 0 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/minio/mc/pkg/limiter"
"github.com/minio/mc/pkg/probe"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/cors"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/minio/minio-go/v7/pkg/encrypt"
"github.com/minio/minio-go/v7/pkg/lifecycle"
Expand Down Expand Up @@ -199,6 +200,11 @@ type Client interface {
// OD operations
GetPart(ctx context.Context, part int) (io.ReadCloser, *probe.Error)
PutPart(ctx context.Context, reader io.Reader, size int64, progress io.Reader, opts PutOptions) (n int64, err *probe.Error)

// Cors operations
GetBucketCors(ctx context.Context) (*cors.Config, *probe.Error)
SetBucketCors(ctx context.Context, corsXML []byte) *probe.Error
DeleteBucketCors(ctx context.Context) *probe.Error
}

// ClientContent - Content container for content metadata
Expand Down
84 changes: 84 additions & 0 deletions cmd/cors-get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/pkg/v3/console"
)

var corsGetCmd = cli.Command{
Name: "get",
Usage: "get a bucket CORS configuration",
Action: mainCorsGet,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: globalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} ALIAS/BUCKET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Get the CORS configuration for the bucket 'mybucket':
{{.Prompt}} {{.HelpName}} myminio/mybucket
`,
}

// checkCorsGetSyntax - validate all the passed arguments
func checkCorsGetSyntax(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
showCommandHelpAndExit(ctx, 1) // last argument is exit code
}
}

// mainCorsGet is the handle for "mc cors get" command.
func mainCorsGet(ctx *cli.Context) error {
checkCorsGetSyntax(ctx)

console.SetColor("CorsMessage", color.New(color.FgGreen))
console.SetColor("CorsNotFound", color.New(color.FgYellow))

// args[0] is the ALIAS/BUCKET argument.
args := ctx.Args()
urlStr := args.Get(0)

client, err := newClient(urlStr)
fatalIf(err.Trace(urlStr), "Unable to initialize client for "+urlStr)

corsCfg, err := client.GetBucketCors(globalContext)
fatalIf(err.Trace(urlStr), "Unable to get bucket CORS configuration for "+urlStr)

status := "success"
if corsCfg == nil {
status = "not found"
}

printMsg(corsMessage{
op: "get",
Status: status,
CorsCfg: corsCfg,
})

return nil
}
40 changes: 40 additions & 0 deletions cmd/cors-main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import "github.com/minio/cli"

var corsSubcommands = []cli.Command{
corsSetCmd,
corsGetCmd,
corsRemoveCmd,
}

var corsCmd = cli.Command{
Name: "cors",
Usage: "manage bucket CORS configuration",
Action: mainCors,
Before: setGlobalsFromContext,
Flags: globalFlags,
Subcommands: corsSubcommands,
}

func mainCors(ctx *cli.Context) error {
commandNotFound(ctx, corsSubcommands)
return nil
}
77 changes: 77 additions & 0 deletions cmd/cors-remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2015-2024 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/pkg/v3/console"
)

var corsRemoveCmd = cli.Command{
Name: "remove",
Usage: "remove a bucket CORS configuration",
Action: mainCorsRemove,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: globalFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} ALIAS/BUCKET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Remove the CORS configuration for the bucket 'mybucket':
{{.Prompt}} {{.HelpName}} myminio/mybucket
`,
}

// checkCorsRemoveSyntax - validate all the passed arguments
func checkCorsRemoveSyntax(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
showCommandHelpAndExit(ctx, 1) // last argument is exit code
}
}

// mainCorsRemove is the handle for "mc cors remove" command.
func mainCorsRemove(ctx *cli.Context) error {
checkCorsRemoveSyntax(ctx)

console.SetColor("CorsMessage", color.New(color.FgGreen))

// args[0] is the ALIAS/BUCKET argument.
args := ctx.Args()
urlStr := args.Get(0)

client, err := newClient(urlStr)
fatalIf(err.Trace(urlStr), "Unable to initialize client for "+urlStr)

err = client.DeleteBucketCors(globalContext)
fatalIf(err.Trace(urlStr), "Unable to remove bucket CORS configuration for "+urlStr)

printMsg(corsMessage{
op: "remove",
Status: "success",
})

return nil
}
Loading

0 comments on commit 1e53784

Please sign in to comment.