svcacct: Validate policy document and error out if needed (#3873)

This commit is contained in:
Anis Elleuch
2021-11-27 14:47:48 -08:00
committed by GitHub
parent 4793c40bf6
commit a064265d0c
+11 -3
View File
@@ -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,