From 0490231b34a1468ddeaaeb622acd7cd69fcc6d99 Mon Sep 17 00:00:00 2001 From: poornas Date: Mon, 18 Mar 2019 20:41:16 -0700 Subject: [PATCH] For non-canned policies, show policy status as custom (#2716) --- cmd/access-perms.go | 1 + cmd/client-fs.go | 16 ++++++++-------- cmd/client-fs_test.go | 2 +- cmd/client-s3.go | 16 ++++++++++------ cmd/client.go | 2 +- cmd/mirror-main.go | 2 +- cmd/policy-main.go | 34 +++++++++++++++++++++++----------- cmd/sql-main.go | 2 +- 8 files changed, 46 insertions(+), 29 deletions(-) diff --git a/cmd/access-perms.go b/cmd/access-perms.go index 94e16cbc..e759f0f7 100644 --- a/cmd/access-perms.go +++ b/cmd/access-perms.go @@ -40,4 +40,5 @@ const ( accessDownload = accessPerms("download") accessUpload = accessPerms("upload") accessPublic = accessPerms("public") + accessCustom = accessPerms("custom") ) diff --git a/cmd/client-fs.go b/cmd/client-fs.go index 0bf48108..1a5d2342 100644 --- a/cmd/client-fs.go +++ b/cmd/client-fs.go @@ -893,28 +893,28 @@ func (f *fsClient) GetAccessRules() (map[string]string, *probe.Error) { } // GetAccess - get access policy permissions. -func (f *fsClient) GetAccess() (access string, err *probe.Error) { +func (f *fsClient) GetAccess() (access string, policyJSON string, err *probe.Error) { // For windows this feature is not implemented. if runtime.GOOS == "windows" { - return "", probe.NewError(APINotImplemented{API: "GetAccess", APIType: "filesystem"}) + return "", "", probe.NewError(APINotImplemented{API: "GetAccess", APIType: "filesystem"}) } st, err := f.fsStat(false) if err != nil { - return "", err.Trace(f.PathURL.String()) + return "", "", err.Trace(f.PathURL.String()) } if !st.Mode().IsDir() { - return "", probe.NewError(APINotImplemented{API: "GetAccess", APIType: "filesystem"}) + return "", "", probe.NewError(APINotImplemented{API: "GetAccess", APIType: "filesystem"}) } // Mask with os.ModePerm to get only inode permissions switch st.Mode() & os.ModePerm { case os.FileMode(0777): - return "readwrite", nil + return "readwrite", "", nil case os.FileMode(0555): - return "readonly", nil + return "readonly", "", nil case os.FileMode(0333): - return "writeonly", nil + return "writeonly", "", nil } - return "none", nil + return "none", "", nil } // SetAccess - set access policy permissions. diff --git a/cmd/client-fs_test.go b/cmd/client-fs_test.go index afdcf36a..ca87c8e6 100644 --- a/cmd/client-fs_test.go +++ b/cmd/client-fs_test.go @@ -231,7 +231,7 @@ func (s *TestSuite) TestBucketACLFails(c *C) { err = fsClient.SetAccess("readonly", false) c.Assert(err, IsNil) - _, err = fsClient.GetAccess() + _, _, err = fsClient.GetAccess() c.Assert(err, IsNil) } } diff --git a/cmd/client-s3.go b/cmd/client-s3.go index 2efff331..9a713521 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -1050,23 +1050,27 @@ func (c *s3Client) GetAccessRules() (map[string]string, *probe.Error) { } // GetAccess get access policy permissions. -func (c *s3Client) GetAccess() (string, *probe.Error) { +func (c *s3Client) GetAccess() (string, string, *probe.Error) { bucket, object := c.url2BucketAndObject() if bucket == "" { - return "", probe.NewError(BucketNameEmpty{}) + return "", "", probe.NewError(BucketNameEmpty{}) } policyStr, e := c.api.GetBucketPolicy(bucket) if e != nil { - return "", probe.NewError(e) + return "", "", probe.NewError(e) } if policyStr == "" { - return string(policy.BucketPolicyNone), nil + return string(policy.BucketPolicyNone), policyStr, nil } var p policy.BucketAccessPolicy if e = json.Unmarshal([]byte(policyStr), &p); e != nil { - return "", probe.NewError(e) + return "", "", probe.NewError(e) } - return string(policy.GetPolicy(p.Statements, bucket, object)), nil + pType := string(policy.GetPolicy(p.Statements, bucket, object)) + if pType == string(policy.BucketPolicyNone) && policyStr != "" { + pType = "custom" + } + return pType, policyStr, nil } // SetAccess set access policy permissions. diff --git a/cmd/client.go b/cmd/client.go index 83a88546..efb5eca3 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -52,7 +52,7 @@ type Client interface { MakeBucket(region string, ignoreExisting bool) *probe.Error // Access policy operations. - GetAccess() (access string, error *probe.Error) + GetAccess() (access string, policyJSON string, error *probe.Error) GetAccessRules() (policyRules map[string]string, error *probe.Error) SetAccess(access string, isJSON bool) *probe.Error diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 2b9350d4..0494424b 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -618,7 +618,7 @@ func copyBucketPolicies(srcClt, dstClt Client, isOverwrite bool) *probe.Error { } // Set found rules to target bucket if permitted for _, r := range rules { - originalRule, err := dstClt.GetAccess() + originalRule, _, err := dstClt.GetAccess() if err != nil { return err } diff --git a/cmd/policy-main.go b/cmd/policy-main.go index 818bb26d..102b3977 100644 --- a/cmd/policy-main.go +++ b/cmd/policy-main.go @@ -113,10 +113,11 @@ func (s policyRules) JSON() string { // policyMessage is container for policy command on bucket success and failure messages. type policyMessage struct { - Operation string `json:"operation"` - Status string `json:"status"` - Bucket string `json:"bucket"` - Perms accessPerms `json:"permission"` + Operation string `json:"operation"` + Status string `json:"status"` + Bucket string `json:"bucket"` + Perms accessPerms `json:"permission"` + Policy map[string]interface{} `json:"policy,omitempty"` } // String colorized access message. @@ -216,6 +217,8 @@ func accessPermToString(perm accessPerms) string { policy = "writeonly" case accessPublic: policy = "readwrite" + case accessCustom: + policy = "custom" } return policy } @@ -275,21 +278,23 @@ func stringToAccessPerm(perm string) accessPerms { policy = accessUpload case "readwrite": policy = accessPublic + case "custom": + policy = accessCustom } return policy } // doGetAccess do get access. -func doGetAccess(targetURL string) (perms accessPerms, err *probe.Error) { +func doGetAccess(targetURL string) (perms accessPerms, policyStr string, err *probe.Error) { clnt, err := newClient(targetURL) if err != nil { - return "", err.Trace(targetURL) + return "", "", err.Trace(targetURL) } - perm, err := clnt.GetAccess() + perm, policyJSON, err := clnt.GetAccess() if err != nil { - return "", err.Trace(targetURL) + return "", "", err.Trace(targetURL) } - return stringToAccessPerm(perm), nil + return stringToAccessPerm(perm), policyJSON, nil } // doGetAccessRules do get access rules. @@ -388,7 +393,7 @@ func runPolicyLinksCmd(ctx *cli.Context) { // Run policy cmd to fetch set permission func runPolicyCmd(ctx *cli.Context) { - var operation string + var operation, policyStr string var probeErr *probe.Error perms := accessPerms(ctx.Args().First()) targetURL := ctx.Args().Last() @@ -400,7 +405,7 @@ func runPolicyCmd(ctx *cli.Context) { operation = "setJSON" } else { targetURL = ctx.Args().First() - perms, probeErr = doGetAccess(targetURL) + perms, policyStr, probeErr = doGetAccess(targetURL) operation = "get" } // Upon error exit. @@ -413,11 +418,18 @@ func runPolicyCmd(ctx *cli.Context) { "Unable to "+operation+" policy `"+string(perms)+"` for `"+targetURL+"`.") } } + policyJSON := map[string]interface{}{} + if policyStr != "" { + e := json.Unmarshal([]byte(policyStr), &policyJSON) + fatalIf(probe.NewError(e), "Cannot unmarshal custom policy file.") + } + printMsg(policyMessage{ Status: "success", Operation: operation, Bucket: targetURL, Perms: perms, + Policy: policyJSON, }) } diff --git a/cmd/sql-main.go b/cmd/sql-main.go index abe59fa3..725b7eea 100644 --- a/cmd/sql-main.go +++ b/cmd/sql-main.go @@ -91,7 +91,7 @@ ENVIRONMENT VARIABLES: MC_ENCRYPT_KEY: list of comma delimited prefix=secret values SERIALIZATION OPTIONS: - For query serialization options, refer to https://docs.minio.io/docs/minio-client-complete-guide#sql + For query serialization options, refer to https://docs.minio.io/docs/minio-client-complete-guide#sql EXAMPLES: 1. Run a query on a set of objects recursively on AWS S3.