Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set-json to fail on custom policies without .json #4745

Merged
merged 8 commits into from
Nov 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions cmd/access-perms.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

package cmd

import "path/filepath"
import (
"os"
"path/filepath"

json "github.com/minio/colorjson"
)

// isValidAccessPERM - is provided access perm string supported.
func (b accessPerms) isValidAccessPERM() bool {
Expand All @@ -28,12 +33,50 @@ func (b accessPerms) isValidAccessPERM() bool {
return false
}

type PolicyDocument struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a new struct - can you not use pkg/policy ?

Version string `json:"Version"`
Statement []struct {
Effect string `json:"Effect"`
Action []string `json:"Action"`
Resource []string `json:"Resource"`
Condition Condition `json:"Condition"`
} `json:"Statement"`
}

type Condition map[string]map[string]interface{}

func (b accessPerms) isValidAccessFile() bool {
res := filepath.Ext(string(b)) == ".json"
if !res {

if filepath.Ext(string(b)) != ".json" {
fatalIf(errDummy().Trace(), "Invalid access file extension. Only .json files are supported.")
return false
}

file, err := os.Open(string(b))
if err != nil {
fatalIf(errDummy().Trace(), "Unable to open access file.")
return false
}
defer file.Close()

var policy PolicyDocument
if json.NewDecoder(file).Decode(&policy) != nil {
fatalIf(errDummy().Trace(), "Unable to parse access file.")
return false
}

if policy.Version != "2012-10-17" {
fatalIf(errDummy().Trace(), "Invalid policy version. Only 2012-10-17 is supported.")
return false
}

for _, statement := range policy.Statement {
if statement.Effect != "Allow" && statement.Effect != "Deny" {
fatalIf(errDummy().Trace(), "Invalid policy effect. Only Allow and Deny are supported.")
return false
}
}

return true
}

Expand Down