1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
// Copyright (c) 2015-2021 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 policy
import (
"bytes"
"encoding/json"
"path"
"strings"
"github.com/minio/pkg/v3/policy/condition"
"github.com/minio/pkg/v3/wildcard"
)
const (
// ResourceARNPrefix - resource S3 ARN prefix as per S3 specification.
ResourceARNPrefix = "arn:aws:s3:::"
// ResourceARNKMSPrefix is for KMS key resources. MinIO specific API.
ResourceARNKMSPrefix = "arn:minio:kms:::"
)
// ResourceARNType - ARN prefix type
type ResourceARNType uint32
const (
// Zero value for detecting errors
unknownARN ResourceARNType = iota
// ResourceARNS3 is the ARN prefix type for S3 resources.
ResourceARNS3
// ResourceARNKMS is the ARN prefix type for MinIO KMS resources.
ResourceARNKMS
// ResourceARNAll is the ARN '*'
ResourceARNAll
)
// ARNTypeToPrefix maps the type to prefix string
var ARNTypeToPrefix = map[ResourceARNType]string{
ResourceARNS3: ResourceARNPrefix,
ResourceARNKMS: ResourceARNKMSPrefix,
ResourceARNAll: "*",
}
// ARNPrefixToType maps prefix to types.
var ARNPrefixToType map[string]ResourceARNType
func init() {
ARNPrefixToType = make(map[string]ResourceARNType)
for k, v := range ARNTypeToPrefix {
ARNPrefixToType[v] = k
}
}
func (a ResourceARNType) String() string {
return ARNTypeToPrefix[a]
}
// Resource - resource in policy statement.
type Resource struct {
Pattern string
Type ResourceARNType
}
func (r Resource) isKMS() bool {
return r.Type == ResourceARNKMS || r.Type == ResourceARNAll
}
func (r Resource) isS3() bool {
return r.Type == ResourceARNS3 || r.Type == ResourceARNAll
}
func (r Resource) isBucketPattern() bool {
return !strings.Contains(r.Pattern, "/") || r.Pattern == "*"
}
func (r Resource) isObjectPattern() bool {
return strings.Contains(r.Pattern, "/") || strings.Contains(r.Pattern, "*")
}
// IsValid - checks whether Resource is valid or not.
func (r Resource) IsValid() bool {
if r.Type == unknownARN {
return false
}
if r.isS3() {
if strings.HasPrefix(r.Pattern, "/") {
return false
}
}
if r.isKMS() {
if strings.IndexFunc(r.Pattern, func(c rune) bool {
return c == '/' || c == '\\' || c == '.'
}) >= 0 {
return false
}
}
return r.Pattern != ""
}
// MatchResource matches object name with resource pattern only.
func (r Resource) MatchResource(resource string) bool {
return r.Match(resource, nil)
}
// Match - matches object name with resource pattern, including specific conditionals.
func (r Resource) Match(resource string, conditionValues map[string][]string) bool {
// Happy path, with no replacements
idx := strings.IndexByte(r.Pattern, '$')
if idx < 0 {
if cp := path.Clean(resource); cp != "." && cp == r.Pattern {
return true
}
return wildcard.Match(r.Pattern, resource)
}
// Use a small buffer
pat := smallBufPool.Get().(*bytes.Buffer)
defer smallBufPool.Put(pat)
pat.Reset()
// Do replacement of known keys.
pat.WriteString(r.Pattern[:idx])
remain := r.Pattern[idx:]
for len(remain) > 0 {
val := remain[0]
if val != '$' || len(remain) < 3 {
pat.WriteByte(val)
remain = remain[1:]
continue
}
keyEnds := strings.IndexByte(remain, '}')
// If no curly brackets, emit as-is.
if remain[1] != '{' || keyEnds < 0 {
pat.WriteByte('$')
remain = remain[1:]
continue
}
ckey := condition.KeyName(remain[2:keyEnds])
// Only replace keys we know
if rvalues, ok := conditionValues[ckey.Name()]; condition.CommonKeysMap[ckey] && ok && rvalues[0] != "" {
pat.WriteString(rvalues[0])
} else {
// Write without replacing...
pat.WriteString("${")
pat.WriteString(string(ckey))
pat.WriteString("}")
}
remain = remain[keyEnds+1:]
}
pattern := pat.String()
if cp := path.Clean(resource); cp != "." && cp == pattern {
return true
}
return wildcard.Match(pattern, resource)
}
// MarshalJSON - encodes Resource to JSON data.
func (r Resource) MarshalJSON() ([]byte, error) {
if !r.IsValid() {
return nil, Errorf("invalid resource %v", r)
}
return json.Marshal(r.String())
}
func (r Resource) String() string {
return r.Type.String() + r.Pattern
}
// UnmarshalJSON - decodes JSON data to Resource.
func (r *Resource) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
parsedResource, err := parseResource(s)
if err != nil {
return err
}
*r = parsedResource
return nil
}
// Validate - validates Resource.
func (r Resource) Validate() error {
if !r.IsValid() {
return Errorf("invalid resource")
}
return nil
}
// ValidateBucket - validates that given bucketName is matched by Resource.
func (r Resource) ValidateBucket(bucketName string) error {
if !r.IsValid() {
return Errorf("invalid resource")
}
// For the resource to match the bucket, there are two cases:
//
// 1. the whole resource pattern must match the bucket name (e.g.
// `example*a` matches bucket 'example-east-a'), or
//
// 2. bucket name followed by '/' must match as a prefix of the resource
// pattern (e.g. `example*a` includes resources in a bucket 'example22'
// for example the object `example22/2023/a` is matched by this resource).
if !wildcard.Match(r.Pattern, bucketName) &&
!wildcard.MatchAsPatternPrefix(r.Pattern, bucketName+"/") {
return Errorf("bucket name does not match")
}
return nil
}
// parseResource - parses string to Resource.
func parseResource(s string) (Resource, error) {
r := Resource{}
for k, v := range ARNPrefixToType {
if s == k {
// all pattern
r.Type = ResourceARNAll
r.Pattern = k
continue
}
if rem, ok := strings.CutPrefix(s, k); ok {
r.Type = v
r.Pattern = rem
break
}
}
if r.Type == unknownARN {
return r, Errorf("invalid resource '%v'", s)
}
if strings.HasPrefix(r.Pattern, "/") {
return r, Errorf("invalid resource '%v' - starts with '/' will not match a bucket", s)
}
return r, nil
}
// NewResource - creates new resource with the default ARN type of S3.
func NewResource(pattern string) Resource {
return Resource{
Pattern: pattern,
Type: ResourceARNS3,
}
}
// NewKMSResource - creates new resource with type KMS
func NewKMSResource(pattern string) Resource {
return Resource{
Pattern: pattern,
Type: ResourceARNKMS,
}
}
|