From a064265d0c47ef090bfa100bffccc7cb3780ee75 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Sat, 27 Nov 2021 23:47:48 +0100 Subject: [PATCH] svcacct: Validate policy document and error out if needed (#3873) --- cmd/admin-user-svcacct-add.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cmd/admin-user-svcacct-add.go b/cmd/admin-user-svcacct-add.go index 27307244..efadc725 100644 --- a/cmd/admin-user-svcacct-add.go +++ b/cmd/admin-user-svcacct-add.go @@ -18,6 +18,7 @@ package cmd import ( + "bytes" "fmt" "io/ioutil" "strings" @@ -28,6 +29,7 @@ import ( "github.com/minio/madmin-go" "github.com/minio/mc/pkg/probe" "github.com/minio/pkg/console" + iampolicy "github.com/minio/pkg/iam/policy" ) var adminUserSvcAcctAddFlags = []cli.Flag{ @@ -158,15 +160,21 @@ func mainAdminUserSvcAcctAdd(ctx *cli.Context) error { client, err := newAdminClient(aliasedURL) fatalIf(err, "Unable to initialize admin connection.") - var buf []byte + var policyBytes []byte if policyPath != "" { + // Validate the policy document and ensure it has at least when statement var e error - buf, e = ioutil.ReadFile(policyPath) + policyBytes, e = ioutil.ReadFile(policyPath) fatalIf(probe.NewError(e), "Unable to open the policy document.") + p, e := iampolicy.ParseConfig(bytes.NewReader(policyBytes)) + fatalIf(probe.NewError(e), "Unable to parse the policy document.") + if p.IsEmpty() { + fatalIf(errInvalidArgument(), "Empty policy documents are not allowed.") + } } opts := madmin.AddServiceAccountReq{ - Policy: buf, + Policy: policyBytes, AccessKey: accessKey, SecretKey: secretKey, TargetUser: user,