Unified · Split
| @@ -25,7 +25,7 @@ import ( · 24 unmodified lines | ||
| 25 | 25 | "github.com/gorilla/mux" |
| 26 | 26 | xhttp "github.com/minio/minio/cmd/http" |
| 27 | 27 | "github.com/minio/minio/cmd/logger" |
| 28 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 28 | + "github.com/minio/pkg/bucket/policy" | |
| 29 | 29 | ) |
| 30 | 30 | |
| 31 | 31 | // Data types used for returning dummy access control |
| @@ -27,7 +27,7 @@ import ( · 26 unmodified lines | ||
| 27 | 27 | jsoniter "github.com/json-iterator/go" |
| 28 | 28 | "github.com/minio/madmin-go" |
| 29 | 29 | "github.com/minio/minio/cmd/logger" |
| 30 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 30 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 31 | 31 | ) |
| 32 | 32 | |
| 33 | 33 | const ( |
| @@ -37,7 +37,7 @@ import ( · 36 unmodified lines | ||
| 37 | 37 | "github.com/minio/minio/cmd/config/storageclass" |
| 38 | 38 | "github.com/minio/minio/cmd/logger" |
| 39 | 39 | "github.com/minio/minio/pkg/auth" |
| 40 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 40 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 41 | 41 | ) |
| 42 | 42 | |
| 43 | 43 | func validateAdminReqConfigKV(ctx context.Context, w http.ResponseWriter, r *http.Request) (auth.Credentials, ObjectLayer) { |
| @@ -32,7 +32,7 @@ import ( · 31 unmodified lines | ||
| 32 | 32 | "github.com/minio/minio/cmd/config/dns" |
| 33 | 33 | "github.com/minio/minio/cmd/logger" |
| 34 | 34 | "github.com/minio/minio/pkg/auth" |
| 35 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 35 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 36 | 36 | ) |
| 37 | 37 | |
| 38 | 38 | func validateAdminUsersReq(ctx context.Context, w http.ResponseWriter, r *http.Request, action iampolicy.AdminAction) (ObjectLayer, auth.Credentials) { |
| @@ -46,9 +46,9 @@ import ( · 45 unmodified lines | ||
| 46 | 46 | "github.com/minio/minio/pkg/auth" |
| 47 | 47 | "github.com/minio/minio/pkg/dsync" |
| 48 | 48 | "github.com/minio/minio/pkg/handlers" |
| 49 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 50 | 49 | "github.com/minio/minio/pkg/kms" |
| 51 | 50 | xnet "github.com/minio/minio/pkg/net" |
| 51 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 52 | 52 | ) |
| 53 | 53 | |
| 54 | 54 | const ( |
| @@ -38,10 +38,10 @@ import ( · 37 unmodified lines | ||
| 38 | 38 | "github.com/minio/minio/pkg/bucket/replication" |
| 39 | 39 | |
| 40 | 40 | objectlock "github.com/minio/minio/pkg/bucket/object/lock" |
| 41 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 42 | 41 | "github.com/minio/minio/pkg/bucket/versioning" |
| 43 | 42 | "github.com/minio/minio/pkg/event" |
| 44 | 43 | "github.com/minio/minio/pkg/hash" |
| 44 | + "github.com/minio/pkg/bucket/policy" | |
| 45 | 45 | ) |
| 46 | 46 | |
| 47 | 47 | // APIError structure |
| @@ -37,10 +37,10 @@ import ( · 36 unmodified lines | ||
| 37 | 37 | "github.com/minio/minio/cmd/logger" |
| 38 | 38 | "github.com/minio/minio/pkg/auth" |
| 39 | 39 | objectlock "github.com/minio/minio/pkg/bucket/object/lock" |
| 40 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 41 | 40 | "github.com/minio/minio/pkg/etag" |
| 42 | 41 | "github.com/minio/minio/pkg/hash" |
| 43 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 42 | + "github.com/minio/pkg/bucket/policy" | |
| 43 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 44 | 44 | ) |
| 45 | 45 | |
| 46 | 46 | // Verify if request has JWT. |
| @@ -29,7 +29,7 @@ import ( · 28 unmodified lines | ||
| 29 | 29 | "time" |
| 30 | 30 | |
| 31 | 31 | "github.com/minio/minio/pkg/auth" |
| 32 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 32 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 33 | 33 | ) |
| 34 | 34 | |
| 35 | 35 | // Test get request auth type. |
| @@ -25,7 +25,7 @@ import ( · 24 unmodified lines | ||
| 25 | 25 | |
| 26 | 26 | "github.com/gorilla/mux" |
| 27 | 27 | "github.com/minio/minio/cmd/logger" |
| 28 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 28 | + "github.com/minio/pkg/bucket/policy" | |
| 29 | 29 | ) |
| 30 | 30 | |
| 31 | 31 | const ( |
| @@ -44,14 +44,14 @@ import ( · 43 unmodified lines | ||
| 44 | 44 | xhttp "github.com/minio/minio/cmd/http" |
| 45 | 45 | "github.com/minio/minio/cmd/logger" |
| 46 | 46 | objectlock "github.com/minio/minio/pkg/bucket/object/lock" |
| 47 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 48 | 47 | "github.com/minio/minio/pkg/bucket/replication" |
| 49 | 48 | "github.com/minio/minio/pkg/event" |
| 50 | 49 | "github.com/minio/minio/pkg/handlers" |
| 51 | 50 | "github.com/minio/minio/pkg/hash" |
| 52 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 53 | 51 | "github.com/minio/minio/pkg/kms" |
| 54 | 52 | "github.com/minio/minio/pkg/sync/errgroup" |
| 53 | + "github.com/minio/pkg/bucket/policy" | |
| 54 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 55 | 55 | ) |
| 56 | 56 | |
| 57 | 57 | const ( |
| @@ -26,7 +26,7 @@ import ( · 25 unmodified lines | ||
| 26 | 26 | xhttp "github.com/minio/minio/cmd/http" |
| 27 | 27 | "github.com/minio/minio/cmd/logger" |
| 28 | 28 | "github.com/minio/minio/pkg/bucket/lifecycle" |
| 29 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 29 | + "github.com/minio/pkg/bucket/policy" | |
| 30 | 30 | ) |
| 31 | 31 | |
| 32 | 32 | const ( |
| @@ -26,8 +26,8 @@ import ( · 25 unmodified lines | ||
| 26 | 26 | "github.com/gorilla/mux" |
| 27 | 27 | "github.com/minio/minio/cmd/logger" |
| 28 | 28 | |
| 29 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 30 | 29 | "github.com/minio/minio/pkg/sync/errgroup" |
| 30 | + "github.com/minio/pkg/bucket/policy" | |
| 31 | 31 | ) |
| 32 | 32 | |
| 33 | 33 | func concurrentDecryptETag(ctx context.Context, objects []ObjectInfo) { |
| @@ -30,12 +30,12 @@ import ( · 29 unmodified lines | ||
| 30 | 30 | bucketsse "github.com/minio/minio/pkg/bucket/encryption" |
| 31 | 31 | "github.com/minio/minio/pkg/bucket/lifecycle" |
| 32 | 32 | objectlock "github.com/minio/minio/pkg/bucket/object/lock" |
| 33 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 34 | 33 | "github.com/minio/minio/pkg/bucket/replication" |
| 35 | 34 | "github.com/minio/minio/pkg/bucket/versioning" |
| 36 | 35 | "github.com/minio/minio/pkg/event" |
| 37 | 36 | "github.com/minio/minio/pkg/kms" |
| 38 | 37 | "github.com/minio/minio/pkg/sync/errgroup" |
| 38 | + "github.com/minio/pkg/bucket/policy" | |
| 39 | 39 | ) |
| 40 | 40 | |
| 41 | 41 | // BucketMetadataSys captures all bucket metadata for a given cluster. |
| @@ -36,12 +36,12 @@ import ( · 35 unmodified lines | ||
| 36 | 36 | bucketsse "github.com/minio/minio/pkg/bucket/encryption" |
| 37 | 37 | "github.com/minio/minio/pkg/bucket/lifecycle" |
| 38 | 38 | objectlock "github.com/minio/minio/pkg/bucket/object/lock" |
| 39 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 40 | 39 | "github.com/minio/minio/pkg/bucket/replication" |
| 41 | 40 | "github.com/minio/minio/pkg/bucket/versioning" |
| 42 | 41 | "github.com/minio/minio/pkg/event" |
| 43 | 42 | "github.com/minio/minio/pkg/fips" |
| 44 | 43 | "github.com/minio/minio/pkg/kms" |
| 44 | + "github.com/minio/pkg/bucket/policy" | |
| 45 | 45 | "github.com/minio/sio" |
| 46 | 46 | ) |
| 47 | 47 | |
| @@ -25,8 +25,8 @@ import ( · 24 unmodified lines | ||
| 25 | 25 | |
| 26 | 26 | "github.com/gorilla/mux" |
| 27 | 27 | "github.com/minio/minio/cmd/logger" |
| 28 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 29 | 28 | "github.com/minio/minio/pkg/event" |
| 29 | + "github.com/minio/pkg/bucket/policy" | |
| 30 | 30 | ) |
| 31 | 31 | |
| 32 | 32 | const ( |
| @@ -26,8 +26,8 @@ import ( · 25 unmodified lines | ||
| 26 | 26 | "github.com/minio/minio/cmd/logger" |
| 27 | 27 | "github.com/minio/minio/pkg/auth" |
| 28 | 28 | objectlock "github.com/minio/minio/pkg/bucket/object/lock" |
| 29 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 30 | 29 | "github.com/minio/minio/pkg/bucket/replication" |
| 30 | + "github.com/minio/pkg/bucket/policy" | |
| 31 | 31 | ) |
| 32 | 32 | |
| 33 | 33 | // BucketObjectLockSys - map of bucket and retention configuration. |
| @@ -25,7 +25,7 @@ import ( · 24 unmodified lines | ||
| 25 | 25 | humanize "github.com/dustin/go-humanize" |
| 26 | 26 | "github.com/gorilla/mux" |
| 27 | 27 | "github.com/minio/minio/cmd/logger" |
| 28 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 28 | + "github.com/minio/pkg/bucket/policy" | |
| 29 | 29 | ) |
| 30 | 30 | |
| 31 | 31 | const ( |
| @@ -29,8 +29,8 @@ import ( · 28 unmodified lines | ||
| 29 | 29 | "testing" |
| 30 | 30 | |
| 31 | 31 | "github.com/minio/minio/pkg/auth" |
| 32 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 33 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 32 | + "github.com/minio/pkg/bucket/policy" | |
| 33 | + "github.com/minio/pkg/bucket/policy/condition" | |
| 34 | 34 | ) |
| 35 | 35 | |
| 36 | 36 | func getAnonReadOnlyBucketPolicy(bucketName string) *policy.Policy { |
| @@ -29,8 +29,8 @@ import ( · 28 unmodified lines | ||
| 29 | 29 | miniogopolicy "github.com/minio/minio-go/v7/pkg/policy" |
| 30 | 30 | xhttp "github.com/minio/minio/cmd/http" |
| 31 | 31 | "github.com/minio/minio/cmd/logger" |
| 32 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 33 | 32 | "github.com/minio/minio/pkg/handlers" |
| 33 | + "github.com/minio/pkg/bucket/policy" | |
| 34 | 34 | ) |
| 35 | 35 | |
| 36 | 36 | // PolicySys - policy subsystem. |
| @@ -38,7 +38,7 @@ import ( · 37 unmodified lines | ||
| 38 | 38 | "github.com/minio/minio/pkg/bucket/bandwidth" |
| 39 | 39 | "github.com/minio/minio/pkg/bucket/replication" |
| 40 | 40 | "github.com/minio/minio/pkg/event" |
| 41 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 41 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 42 | 42 | ) |
| 43 | 43 | |
| 44 | 44 | // gets replication config associated to a given bucket name. |
| @@ -25,8 +25,8 @@ import ( · 24 unmodified lines | ||
| 25 | 25 | humanize "github.com/dustin/go-humanize" |
| 26 | 26 | "github.com/gorilla/mux" |
| 27 | 27 | "github.com/minio/minio/cmd/logger" |
| 28 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 29 | 28 | "github.com/minio/minio/pkg/bucket/versioning" |
| 29 | + "github.com/minio/pkg/bucket/policy" | |
| 30 | 30 | ) |
| 31 | 31 | |
| 32 | 32 | const ( |
| @@ -32,9 +32,9 @@ import ( · 31 unmodified lines | ||
| 32 | 32 | jwtgo "github.com/dgrijalva/jwt-go" |
| 33 | 33 | "github.com/minio/minio/cmd/config" |
| 34 | 34 | "github.com/minio/minio/pkg/auth" |
| 35 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 36 | 35 | xnet "github.com/minio/minio/pkg/net" |
| 37 | 36 | "github.com/minio/pkg/env" |
| 37 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 38 | 38 | ) |
| 39 | 39 | |
| 40 | 40 | // Config - OpenID Config |
| @@ -25,9 +25,9 @@ import ( · 24 unmodified lines | ||
| 25 | 25 | "net/http" |
| 26 | 26 | |
| 27 | 27 | "github.com/minio/minio/cmd/config" |
| 28 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 29 | 28 | xnet "github.com/minio/minio/pkg/net" |
| 30 | 29 | "github.com/minio/pkg/env" |
| 30 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 31 | 31 | ) |
| 32 | 32 | |
| 33 | 33 | // Env IAM OPA URL |
| @@ -22,7 +22,7 @@ import ( · 21 unmodified lines | ||
| 22 | 22 | |
| 23 | 23 | "github.com/gorilla/mux" |
| 24 | 24 | "github.com/minio/minio/cmd/logger" |
| 25 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 25 | + "github.com/minio/pkg/bucket/policy" | |
| 26 | 26 | ) |
| 27 | 27 | |
| 28 | 28 | // Data types used for returning dummy tagging XML. |
| @@ -40,11 +40,11 @@ import ( · 39 unmodified lines | ||
| 40 | 40 | "github.com/minio/minio/cmd/config" |
| 41 | 41 | xhttp "github.com/minio/minio/cmd/http" |
| 42 | 42 | "github.com/minio/minio/cmd/logger" |
| 43 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 44 | 43 | "github.com/minio/minio/pkg/color" |
| 45 | 44 | xioutil "github.com/minio/minio/pkg/ioutil" |
| 46 | 45 | "github.com/minio/minio/pkg/lock" |
| 47 | 46 | "github.com/minio/minio/pkg/mountinfo" |
| 47 | + "github.com/minio/pkg/bucket/policy" | |
| 48 | 48 | "github.com/minio/pkg/mimedb" |
| 49 | 49 | ) |
| 50 | 50 | |
| @@ -26,8 +26,8 @@ import ( · 25 unmodified lines | ||
| 26 | 26 | "github.com/minio/minio-go/v7/pkg/tags" |
| 27 | 27 | bucketsse "github.com/minio/minio/pkg/bucket/encryption" |
| 28 | 28 | "github.com/minio/minio/pkg/bucket/lifecycle" |
| 29 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 30 | 29 | "github.com/minio/minio/pkg/bucket/versioning" |
| 30 | + "github.com/minio/pkg/bucket/policy" | |
| 31 | 31 | |
| 32 | 32 | "github.com/minio/madmin-go" |
| 33 | 33 | ) |
| @@ -45,8 +45,8 @@ import ( · 44 unmodified lines | ||
| 45 | 45 | minio "github.com/minio/minio/cmd" |
| 46 | 46 | "github.com/minio/minio/cmd/logger" |
| 47 | 47 | "github.com/minio/minio/pkg/auth" |
| 48 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 49 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 48 | + "github.com/minio/pkg/bucket/policy" | |
| 49 | + "github.com/minio/pkg/bucket/policy/condition" | |
| 50 | 50 | "github.com/minio/pkg/env" |
| 51 | 51 | ) |
| 52 | 52 | |
| @@ -42,8 +42,8 @@ import ( · 41 unmodified lines | ||
| 42 | 42 | minio "github.com/minio/minio/cmd" |
| 43 | 43 | "github.com/minio/minio/cmd/logger" |
| 44 | 44 | "github.com/minio/minio/pkg/auth" |
| 45 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 46 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 45 | + "github.com/minio/pkg/bucket/policy" | |
| 46 | + "github.com/minio/pkg/bucket/policy/condition" | |
| 47 | 47 | "github.com/minio/pkg/env" |
| 48 | 48 | "google.golang.org/api/googleapi" |
| 49 | 49 | "google.golang.org/api/iterator" |
| @@ -37,7 +37,7 @@ import ( · 36 unmodified lines | ||
| 37 | 37 | xhttp "github.com/minio/minio/cmd/http" |
| 38 | 38 | "github.com/minio/minio/cmd/logger" |
| 39 | 39 | "github.com/minio/minio/pkg/auth" |
| 40 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 40 | + "github.com/minio/pkg/bucket/policy" | |
| 41 | 41 | ) |
| 42 | 42 | |
| 43 | 43 | func init() { |
| @@ -33,8 +33,8 @@ import ( · 32 unmodified lines | ||
| 33 | 33 | "github.com/minio/minio/cmd/config" |
| 34 | 34 | "github.com/minio/minio/cmd/logger" |
| 35 | 35 | "github.com/minio/minio/pkg/auth" |
| 36 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 37 | 36 | "github.com/minio/minio/pkg/kms" |
| 37 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 38 | 38 | "go.etcd.io/etcd/api/v3/mvccpb" |
| 39 | 39 | etcd "go.etcd.io/etcd/client/v3" |
| 40 | 40 | ) |
| @@ -29,8 +29,8 @@ import ( · 28 unmodified lines | ||
| 29 | 29 | "github.com/minio/minio/cmd/config" |
| 30 | 30 | "github.com/minio/minio/cmd/logger" |
| 31 | 31 | "github.com/minio/minio/pkg/auth" |
| 32 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 33 | 32 | "github.com/minio/minio/pkg/kms" |
| 33 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 34 | 34 | ) |
| 35 | 35 | |
| 36 | 36 | // IAMObjectStore implements IAMStorageAPI |
| @@ -34,7 +34,7 @@ import ( · 33 unmodified lines | ||
| 34 | 34 | "github.com/minio/minio-go/v7/pkg/set" |
| 35 | 35 | "github.com/minio/minio/cmd/logger" |
| 36 | 36 | "github.com/minio/minio/pkg/auth" |
| 37 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 37 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 38 | 38 | ) |
| 39 | 39 | |
| 40 | 40 | // UsersSysType - defines the type of users and groups system that is |
| @@ -24,8 +24,8 @@ import ( · 23 unmodified lines | ||
| 24 | 24 | |
| 25 | 25 | "github.com/gorilla/mux" |
| 26 | 26 | "github.com/minio/minio/cmd/logger" |
| 27 | − policy "github.com/minio/minio/pkg/bucket/policy" | |
| 28 | 27 | "github.com/minio/minio/pkg/event" |
| 28 | + policy "github.com/minio/pkg/bucket/policy" | |
| 29 | 29 | ) |
| 30 | 30 | |
| 31 | 31 | func (api objectAPIHandlers) ListenNotificationHandler(w http.ResponseWriter, r *http.Request) { |
| @@ -25,7 +25,7 @@ import ( · 24 unmodified lines | ||
| 25 | 25 | |
| 26 | 26 | "github.com/minio/madmin-go" |
| 27 | 27 | "github.com/minio/minio/cmd/logger" |
| 28 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 28 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 29 | 29 | "github.com/prometheus/client_golang/prometheus" |
| 30 | 30 | "github.com/prometheus/client_golang/prometheus/promhttp" |
| 31 | 31 | ) |
| @@ -40,10 +40,10 @@ import ( · 39 unmodified lines | ||
| 40 | 40 | xhttp "github.com/minio/minio/cmd/http" |
| 41 | 41 | "github.com/minio/minio/cmd/logger" |
| 42 | 42 | bucketBandwidth "github.com/minio/minio/pkg/bucket/bandwidth" |
| 43 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 44 | 43 | "github.com/minio/minio/pkg/event" |
| 45 | 44 | xnet "github.com/minio/minio/pkg/net" |
| 46 | 45 | "github.com/minio/minio/pkg/sync/errgroup" |
| 46 | + "github.com/minio/pkg/bucket/policy" | |
| 47 | 47 | ) |
| 48 | 48 | |
| 49 | 49 | // NotificationSys - notification system. |
| @@ -26,7 +26,7 @@ import ( · 25 unmodified lines | ||
| 26 | 26 | "github.com/minio/madmin-go" |
| 27 | 27 | "github.com/minio/minio-go/v7/pkg/encrypt" |
| 28 | 28 | "github.com/minio/minio-go/v7/pkg/tags" |
| 29 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 29 | + "github.com/minio/pkg/bucket/policy" | |
| 30 | 30 | ) |
| 31 | 31 | |
| 32 | 32 | // CheckPreconditionFn returns true if precondition check failed. |
| @@ -47,18 +47,18 @@ import ( · 46 unmodified lines | ||
| 47 | 47 | "github.com/minio/minio/cmd/logger" |
| 48 | 48 | "github.com/minio/minio/pkg/bucket/lifecycle" |
| 49 | 49 | objectlock "github.com/minio/minio/pkg/bucket/object/lock" |
| 50 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 51 | 50 | "github.com/minio/minio/pkg/bucket/replication" |
| 52 | 51 | "github.com/minio/minio/pkg/etag" |
| 53 | 52 | "github.com/minio/minio/pkg/event" |
| 54 | 53 | "github.com/minio/minio/pkg/fips" |
| 55 | 54 | "github.com/minio/minio/pkg/handlers" |
| 56 | 55 | "github.com/minio/minio/pkg/hash" |
| 57 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 58 | 56 | "github.com/minio/minio/pkg/ioutil" |
| 59 | 57 | "github.com/minio/minio/pkg/kms" |
| 60 | 58 | xnet "github.com/minio/minio/pkg/net" |
| 61 | 59 | "github.com/minio/minio/pkg/s3select" |
| 60 | + "github.com/minio/pkg/bucket/policy" | |
| 61 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 62 | 62 | "github.com/minio/sio" |
| 63 | 63 | ) |
| 64 | 64 | |
| @@ -23,8 +23,8 @@ import ( · 22 unmodified lines | ||
| 23 | 23 | |
| 24 | 24 | miniogopolicy "github.com/minio/minio-go/v7/pkg/policy" |
| 25 | 25 | "github.com/minio/minio-go/v7/pkg/set" |
| 26 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 27 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 26 | + "github.com/minio/pkg/bucket/policy" | |
| 27 | + "github.com/minio/pkg/bucket/policy/condition" | |
| 28 | 28 | ) |
| 29 | 29 | |
| 30 | 30 | func TestPolicySysIsAllowed(t *testing.T) { |
| @@ -35,7 +35,7 @@ import ( · 34 unmodified lines | ||
| 35 | 35 | humanize "github.com/dustin/go-humanize" |
| 36 | 36 | "github.com/minio/minio-go/v7/pkg/set" |
| 37 | 37 | xhttp "github.com/minio/minio/cmd/http" |
| 38 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 38 | + "github.com/minio/pkg/bucket/policy" | |
| 39 | 39 | ) |
| 40 | 40 | |
| 41 | 41 | // API suite container common to both FS and Erasure. |
| @@ -31,7 +31,7 @@ import ( · 30 unmodified lines | ||
| 31 | 31 | xhttp "github.com/minio/minio/cmd/http" |
| 32 | 32 | "github.com/minio/minio/cmd/logger" |
| 33 | 33 | "github.com/minio/minio/pkg/auth" |
| 34 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 34 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 35 | 35 | "github.com/minio/pkg/wildcard" |
| 36 | 36 | ) |
| 37 | 37 | |
| @@ -65,8 +65,8 @@ import ( · 64 unmodified lines | ||
| 65 | 65 | "github.com/minio/minio/cmd/logger" |
| 66 | 66 | "github.com/minio/minio/cmd/rest" |
| 67 | 67 | "github.com/minio/minio/pkg/auth" |
| 68 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 69 | 68 | "github.com/minio/minio/pkg/hash" |
| 69 | + "github.com/minio/pkg/bucket/policy" | |
| 70 | 70 | ) |
| 71 | 71 | |
| 72 | 72 | // TestMain to set up global env. |
| @@ -26,7 +26,7 @@ import ( · 25 unmodified lines | ||
| 26 | 26 | jsoniter "github.com/json-iterator/go" |
| 27 | 27 | "github.com/minio/madmin-go" |
| 28 | 28 | "github.com/minio/minio/cmd/logger" |
| 29 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 29 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 30 | 30 | ) |
| 31 | 31 | |
| 32 | 32 | var ( |
| @@ -48,14 +48,14 @@ import ( · 47 unmodified lines | ||
| 48 | 48 | "github.com/minio/minio/cmd/logger" |
| 49 | 49 | "github.com/minio/minio/pkg/auth" |
| 50 | 50 | objectlock "github.com/minio/minio/pkg/bucket/object/lock" |
| 51 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 52 | 51 | "github.com/minio/minio/pkg/bucket/replication" |
| 53 | 52 | "github.com/minio/minio/pkg/etag" |
| 54 | 53 | "github.com/minio/minio/pkg/event" |
| 55 | 54 | "github.com/minio/minio/pkg/handlers" |
| 56 | 55 | "github.com/minio/minio/pkg/hash" |
| 57 | − iampolicy "github.com/minio/minio/pkg/iam/policy" | |
| 58 | 56 | "github.com/minio/minio/pkg/ioutil" |
| 57 | + "github.com/minio/pkg/bucket/policy" | |
| 58 | + iampolicy "github.com/minio/pkg/iam/policy" | |
| 59 | 59 | "github.com/minio/rpc/json2" |
| 60 | 60 | ) |
| 61 | 61 | |
| @@ -47,7 +47,7 @@ require ( · 46 unmodified lines | ||
| 47 | 47 | github.com/minio/madmin-go v1.0.2 |
| 48 | 48 | github.com/minio/minio-go/v7 v7.0.11-0.20210302210017-6ae69c73ce78 |
| 49 | 49 | github.com/minio/parquet-go v1.0.0 |
| 50 | − github.com/minio/pkg v1.0.2 | |
| 50 | + github.com/minio/pkg v1.0.3 | |
| 51 | 51 | github.com/minio/rpc v1.0.0 |
| 52 | 52 | github.com/minio/selfupdate v0.3.1 |
| 53 | 53 | github.com/minio/sha256-simd v1.0.0 |
| @@ -504,8 +504,8 @@ github.com/minio/minio-go/v7 v7.0.11-0.20210302210017-6ae69c73ce78 h1:v7OMbUnWky · 503 unmodified lines | ||
| 504 | 504 | github.com/minio/minio-go/v7 v7.0.11-0.20210302210017-6ae69c73ce78/go.mod h1:mTh2uJuAbEqdhMVl6CMIIZLUeiMiWtJR4JB8/5g2skw= |
| 505 | 505 | github.com/minio/parquet-go v1.0.0 h1:fcWsEvub04Nsl/4hiRBDWlbqd6jhacQieV07a+nhiIk= |
| 506 | 506 | github.com/minio/parquet-go v1.0.0/go.mod h1:aQlkSOfOq2AtQKkuou3mosNVMwNokd+faTacxxk/oHA= |
| 507 | −github.com/minio/pkg v1.0.2 h1:vUlNMJbOgP/Hi/ekN+tl1xTOm3Q39gPr5XurDVOgvBA= | |
| 508 | −github.com/minio/pkg v1.0.2/go.mod h1:e9WOU0bav8jd8AzloFjCTSiXSNqnXqxbzGwlH+2rQnI= | |
| 507 | +github.com/minio/pkg v1.0.3 h1:tUhM6lG/BdNB0+5f2RbE4ifCAYwMs6cRJnZ/AY0WIeQ= | |
| 508 | +github.com/minio/pkg v1.0.3/go.mod h1:obU54TZ9QlMv0TRaDgQ/JTzf11ZSXxnSfLrm4tMtBP8= | |
| 509 | 509 | github.com/minio/rpc v1.0.0 h1:tJCHyLfQF6k6HlMQFpKy2FO/7lc2WP8gLDGMZp18E70= |
| 510 | 510 | github.com/minio/rpc v1.0.0/go.mod h1:b9xqF7J0xeMXr0cM4pnBlP7Te7PDsG5JrRxl5dG6Ldk= |
| 511 | 511 | github.com/minio/selfupdate v0.3.1 h1:BWEFSNnrZVMUWXbXIgLDNDjbejkmpAmZvy/nCz1HlEs= |
| @@ -1,426 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − | |
| 23 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 24 | −) | |
| 25 | − | |
| 26 | −// Action - policy action. | |
| 27 | −// Refer https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazons3.html | |
| 28 | −// for more information about available actions. | |
| 29 | −type Action string | |
| 30 | − | |
| 31 | −const ( | |
| 32 | − // AbortMultipartUploadAction - AbortMultipartUpload Rest API action. | |
| 33 | − AbortMultipartUploadAction Action = "s3:AbortMultipartUpload" | |
| 34 | − | |
| 35 | − // CreateBucketAction - CreateBucket Rest API action. | |
| 36 | − CreateBucketAction = "s3:CreateBucket" | |
| 37 | − | |
| 38 | − // DeleteBucketAction - DeleteBucket Rest API action. | |
| 39 | − DeleteBucketAction = "s3:DeleteBucket" | |
| 40 | − | |
| 41 | − // ForceDeleteBucketAction - DeleteBucket Rest API action when x-minio-force-delete flag | |
| 42 | − // is specified. | |
| 43 | − ForceDeleteBucketAction = "s3:ForceDeleteBucket" | |
| 44 | − | |
| 45 | − // DeleteBucketPolicyAction - DeleteBucketPolicy Rest API action. | |
| 46 | − DeleteBucketPolicyAction = "s3:DeleteBucketPolicy" | |
| 47 | − | |
| 48 | − // DeleteObjectAction - DeleteObject Rest API action. | |
| 49 | − DeleteObjectAction = "s3:DeleteObject" | |
| 50 | − | |
| 51 | − // GetBucketLocationAction - GetBucketLocation Rest API action. | |
| 52 | − GetBucketLocationAction = "s3:GetBucketLocation" | |
| 53 | − | |
| 54 | − // GetBucketNotificationAction - GetBucketNotification Rest API action. | |
| 55 | − GetBucketNotificationAction = "s3:GetBucketNotification" | |
| 56 | − | |
| 57 | − // GetBucketPolicyAction - GetBucketPolicy Rest API action. | |
| 58 | − GetBucketPolicyAction = "s3:GetBucketPolicy" | |
| 59 | − | |
| 60 | − // GetObjectAction - GetObject Rest API action. | |
| 61 | − GetObjectAction = "s3:GetObject" | |
| 62 | − | |
| 63 | − // HeadBucketAction - HeadBucket Rest API action. This action is unused in minio. | |
| 64 | − HeadBucketAction = "s3:HeadBucket" | |
| 65 | − | |
| 66 | − // ListAllMyBucketsAction - ListAllMyBuckets (List buckets) Rest API action. | |
| 67 | − ListAllMyBucketsAction = "s3:ListAllMyBuckets" | |
| 68 | − | |
| 69 | − // ListBucketAction - ListBucket Rest API action. | |
| 70 | − ListBucketAction = "s3:ListBucket" | |
| 71 | − | |
| 72 | − // GetBucketPolicyStatusAction - Retrieves the policy status for a bucket. | |
| 73 | − GetBucketPolicyStatusAction = "s3:GetBucketPolicyStatus" | |
| 74 | − | |
| 75 | − // ListBucketMultipartUploadsAction - ListMultipartUploads Rest API action. | |
| 76 | − ListBucketMultipartUploadsAction = "s3:ListBucketMultipartUploads" | |
| 77 | − | |
| 78 | − // ListBucketVersionsAction - ListBucket versions Rest API action. | |
| 79 | − ListBucketVersionsAction = "s3:ListBucketVersions" | |
| 80 | − | |
| 81 | − // ListenNotificationAction - ListenNotification Rest API action. | |
| 82 | − // This is MinIO extension. | |
| 83 | − ListenNotificationAction = "s3:ListenNotification" | |
| 84 | − | |
| 85 | − // ListenBucketNotificationAction - ListenBucketNotification Rest API action. | |
| 86 | − // This is MinIO extension. | |
| 87 | − ListenBucketNotificationAction = "s3:ListenBucketNotification" | |
| 88 | − | |
| 89 | − // ListMultipartUploadPartsAction - ListParts Rest API action. | |
| 90 | − ListMultipartUploadPartsAction = "s3:ListMultipartUploadParts" | |
| 91 | − | |
| 92 | − // PutBucketNotificationAction - PutObjectNotification Rest API action. | |
| 93 | − PutBucketNotificationAction = "s3:PutBucketNotification" | |
| 94 | − | |
| 95 | − // PutBucketPolicyAction - PutBucketPolicy Rest API action. | |
| 96 | − PutBucketPolicyAction = "s3:PutBucketPolicy" | |
| 97 | − | |
| 98 | − // PutObjectAction - PutObject Rest API action. | |
| 99 | − PutObjectAction = "s3:PutObject" | |
| 100 | − | |
| 101 | − // PutBucketLifecycleAction - PutBucketLifecycle Rest API action. | |
| 102 | − PutBucketLifecycleAction = "s3:PutLifecycleConfiguration" | |
| 103 | − | |
| 104 | − // GetBucketLifecycleAction - GetBucketLifecycle Rest API action. | |
| 105 | − GetBucketLifecycleAction = "s3:GetLifecycleConfiguration" | |
| 106 | − | |
| 107 | − // BypassGovernanceRetentionAction - bypass governance retention for PutObjectRetention, PutObject and DeleteObject Rest API action. | |
| 108 | − BypassGovernanceRetentionAction = "s3:BypassGovernanceRetention" | |
| 109 | − // PutObjectRetentionAction - PutObjectRetention Rest API action. | |
| 110 | − PutObjectRetentionAction = "s3:PutObjectRetention" | |
| 111 | − | |
| 112 | − // GetObjectRetentionAction - GetObjectRetention, GetObject, HeadObject Rest API action. | |
| 113 | − GetObjectRetentionAction = "s3:GetObjectRetention" | |
| 114 | − // GetObjectLegalHoldAction - GetObjectLegalHold, GetObject Rest API action. | |
| 115 | − GetObjectLegalHoldAction = "s3:GetObjectLegalHold" | |
| 116 | − // PutObjectLegalHoldAction - PutObjectLegalHold, PutObject Rest API action. | |
| 117 | − PutObjectLegalHoldAction = "s3:PutObjectLegalHold" | |
| 118 | − // GetBucketObjectLockConfigurationAction - GetObjectLockConfiguration Rest API action | |
| 119 | − GetBucketObjectLockConfigurationAction = "s3:GetBucketObjectLockConfiguration" | |
| 120 | − // PutBucketObjectLockConfigurationAction - PutObjectLockConfiguration Rest API action | |
| 121 | − PutBucketObjectLockConfigurationAction = "s3:PutBucketObjectLockConfiguration" | |
| 122 | − | |
| 123 | − // GetBucketTaggingAction - GetTagging Rest API action | |
| 124 | − GetBucketTaggingAction = "s3:GetBucketTagging" | |
| 125 | − // PutBucketTaggingAction - PutTagging Rest API action | |
| 126 | − PutBucketTaggingAction = "s3:PutBucketTagging" | |
| 127 | − | |
| 128 | − // GetObjectTaggingAction - Get Object Tags API action | |
| 129 | − GetObjectTaggingAction = "s3:GetObjectTagging" | |
| 130 | − // PutObjectTaggingAction - Put Object Tags API action | |
| 131 | − PutObjectTaggingAction = "s3:PutObjectTagging" | |
| 132 | − // DeleteObjectTaggingAction - Delete Object Tags API action | |
| 133 | − DeleteObjectTaggingAction = "s3:DeleteObjectTagging" | |
| 134 | − | |
| 135 | − // PutBucketEncryptionAction - PutBucketEncryption REST API action | |
| 136 | − PutBucketEncryptionAction = "s3:PutEncryptionConfiguration" | |
| 137 | − // GetBucketEncryptionAction - GetBucketEncryption REST API action | |
| 138 | − GetBucketEncryptionAction = "s3:GetEncryptionConfiguration" | |
| 139 | − | |
| 140 | − // PutBucketVersioningAction - PutBucketVersioning REST API action | |
| 141 | − PutBucketVersioningAction = "s3:PutBucketVersioning" | |
| 142 | − // GetBucketVersioningAction - GetBucketVersioning REST API action | |
| 143 | − GetBucketVersioningAction = "s3:GetBucketVersioning" | |
| 144 | − | |
| 145 | − // DeleteObjectVersionAction - DeleteObjectVersion Rest API action. | |
| 146 | − DeleteObjectVersionAction = "s3:DeleteObjectVersion" | |
| 147 | − | |
| 148 | − // DeleteObjectVersionTaggingAction - DeleteObjectVersionTagging Rest API action. | |
| 149 | − DeleteObjectVersionTaggingAction = "s3:DeleteObjectVersionTagging" | |
| 150 | − | |
| 151 | − // GetObjectVersionAction - GetObjectVersionAction Rest API action. | |
| 152 | − GetObjectVersionAction = "s3:GetObjectVersion" | |
| 153 | − | |
| 154 | − // GetObjectVersionTaggingAction - GetObjectVersionTagging Rest API action. | |
| 155 | − GetObjectVersionTaggingAction = "s3:GetObjectVersionTagging" | |
| 156 | − | |
| 157 | − // PutObjectVersionTaggingAction - PutObjectVersionTagging Rest API action. | |
| 158 | − PutObjectVersionTaggingAction = "s3:PutObjectVersionTagging" | |
| 159 | − | |
| 160 | − // GetReplicationConfigurationAction - GetReplicationConfiguration REST API action | |
| 161 | − GetReplicationConfigurationAction = "s3:GetReplicationConfiguration" | |
| 162 | − // PutReplicationConfigurationAction - PutReplicationConfiguration REST API action | |
| 163 | − PutReplicationConfigurationAction = "s3:PutReplicationConfiguration" | |
| 164 | − | |
| 165 | − // ReplicateObjectAction - ReplicateObject REST API action | |
| 166 | − ReplicateObjectAction = "s3:ReplicateObject" | |
| 167 | − | |
| 168 | − // ReplicateDeleteAction - ReplicateDelete REST API action | |
| 169 | − ReplicateDeleteAction = "s3:ReplicateDelete" | |
| 170 | − | |
| 171 | − // ReplicateTagsAction - ReplicateTags REST API action | |
| 172 | − ReplicateTagsAction = "s3:ReplicateTags" | |
| 173 | − | |
| 174 | − // GetObjectVersionForReplicationAction - GetObjectVersionForReplication REST API action | |
| 175 | − GetObjectVersionForReplicationAction = "s3:GetObjectVersionForReplication" | |
| 176 | − | |
| 177 | − // RestoreObjectAction - RestoreObject REST API action | |
| 178 | − RestoreObjectAction = "s3:RestoreObject" | |
| 179 | −) | |
| 180 | − | |
| 181 | −// List of all supported object actions. | |
| 182 | −var supportedObjectActions = map[Action]struct{}{ | |
| 183 | − AbortMultipartUploadAction: {}, | |
| 184 | − DeleteObjectAction: {}, | |
| 185 | − GetObjectAction: {}, | |
| 186 | − ListMultipartUploadPartsAction: {}, | |
| 187 | − PutObjectAction: {}, | |
| 188 | − BypassGovernanceRetentionAction: {}, | |
| 189 | − PutObjectRetentionAction: {}, | |
| 190 | − GetObjectRetentionAction: {}, | |
| 191 | − PutObjectLegalHoldAction: {}, | |
| 192 | − GetObjectLegalHoldAction: {}, | |
| 193 | − GetObjectTaggingAction: {}, | |
| 194 | − PutObjectTaggingAction: {}, | |
| 195 | − DeleteObjectTaggingAction: {}, | |
| 196 | − GetObjectVersionAction: {}, | |
| 197 | − GetObjectVersionTaggingAction: {}, | |
| 198 | − DeleteObjectVersionAction: {}, | |
| 199 | − DeleteObjectVersionTaggingAction: {}, | |
| 200 | − PutObjectVersionTaggingAction: {}, | |
| 201 | − ReplicateObjectAction: {}, | |
| 202 | − ReplicateDeleteAction: {}, | |
| 203 | − ReplicateTagsAction: {}, | |
| 204 | − GetObjectVersionForReplicationAction: {}, | |
| 205 | − RestoreObjectAction: {}, | |
| 206 | −} | |
| 207 | − | |
| 208 | −// isObjectAction - returns whether action is object type or not. | |
| 209 | −func (action Action) isObjectAction() bool { | |
| 210 | − _, ok := supportedObjectActions[action] | |
| 211 | − return ok | |
| 212 | −} | |
| 213 | − | |
| 214 | −// List of all supported actions. | |
| 215 | −var supportedActions = map[Action]struct{}{ | |
| 216 | − AbortMultipartUploadAction: {}, | |
| 217 | − CreateBucketAction: {}, | |
| 218 | − DeleteBucketAction: {}, | |
| 219 | − ForceDeleteBucketAction: {}, | |
| 220 | − DeleteBucketPolicyAction: {}, | |
| 221 | − DeleteObjectAction: {}, | |
| 222 | − GetBucketLocationAction: {}, | |
| 223 | − GetBucketNotificationAction: {}, | |
| 224 | − GetBucketPolicyAction: {}, | |
| 225 | − GetObjectAction: {}, | |
| 226 | − HeadBucketAction: {}, | |
| 227 | − ListAllMyBucketsAction: {}, | |
| 228 | − ListBucketAction: {}, | |
| 229 | − GetBucketPolicyStatusAction: {}, | |
| 230 | − ListBucketVersionsAction: {}, | |
| 231 | − ListBucketMultipartUploadsAction: {}, | |
| 232 | − ListenNotificationAction: {}, | |
| 233 | − ListenBucketNotificationAction: {}, | |
| 234 | − ListMultipartUploadPartsAction: {}, | |
| 235 | − PutBucketNotificationAction: {}, | |
| 236 | − PutBucketPolicyAction: {}, | |
| 237 | − PutObjectAction: {}, | |
| 238 | − GetBucketLifecycleAction: {}, | |
| 239 | − PutBucketLifecycleAction: {}, | |
| 240 | − PutObjectRetentionAction: {}, | |
| 241 | − GetObjectRetentionAction: {}, | |
| 242 | − GetObjectLegalHoldAction: {}, | |
| 243 | − PutObjectLegalHoldAction: {}, | |
| 244 | − PutBucketObjectLockConfigurationAction: {}, | |
| 245 | − GetBucketObjectLockConfigurationAction: {}, | |
| 246 | − PutBucketTaggingAction: {}, | |
| 247 | − GetBucketTaggingAction: {}, | |
| 248 | − GetObjectVersionAction: {}, | |
| 249 | − GetObjectVersionTaggingAction: {}, | |
| 250 | − DeleteObjectVersionAction: {}, | |
| 251 | − DeleteObjectVersionTaggingAction: {}, | |
| 252 | − PutObjectVersionTaggingAction: {}, | |
| 253 | − BypassGovernanceRetentionAction: {}, | |
| 254 | − GetObjectTaggingAction: {}, | |
| 255 | − PutObjectTaggingAction: {}, | |
| 256 | − DeleteObjectTaggingAction: {}, | |
| 257 | − PutBucketEncryptionAction: {}, | |
| 258 | − GetBucketEncryptionAction: {}, | |
| 259 | − PutBucketVersioningAction: {}, | |
| 260 | − GetBucketVersioningAction: {}, | |
| 261 | − GetReplicationConfigurationAction: {}, | |
| 262 | − PutReplicationConfigurationAction: {}, | |
| 263 | − ReplicateObjectAction: {}, | |
| 264 | − ReplicateDeleteAction: {}, | |
| 265 | − ReplicateTagsAction: {}, | |
| 266 | − GetObjectVersionForReplicationAction: {}, | |
| 267 | − RestoreObjectAction: {}, | |
| 268 | −} | |
| 269 | − | |
| 270 | −// IsValid - checks if action is valid or not. | |
| 271 | −func (action Action) IsValid() bool { | |
| 272 | − _, ok := supportedActions[action] | |
| 273 | − return ok | |
| 274 | −} | |
| 275 | − | |
| 276 | −// MarshalJSON - encodes Action to JSON data. | |
| 277 | −func (action Action) MarshalJSON() ([]byte, error) { | |
| 278 | − if action.IsValid() { | |
| 279 | − return json.Marshal(string(action)) | |
| 280 | − } | |
| 281 | − | |
| 282 | − return nil, Errorf("invalid action '%v'", action) | |
| 283 | −} | |
| 284 | − | |
| 285 | −// UnmarshalJSON - decodes JSON data to Action. | |
| 286 | −func (action *Action) UnmarshalJSON(data []byte) error { | |
| 287 | − var s string | |
| 288 | − | |
| 289 | − if err := json.Unmarshal(data, &s); err != nil { | |
| 290 | − return err | |
| 291 | − } | |
| 292 | − | |
| 293 | − a := Action(s) | |
| 294 | − if !a.IsValid() { | |
| 295 | − return Errorf("invalid action '%v'", s) | |
| 296 | − } | |
| 297 | − | |
| 298 | − *action = a | |
| 299 | − | |
| 300 | − return nil | |
| 301 | −} | |
| 302 | − | |
| 303 | −func parseAction(s string) (Action, error) { | |
| 304 | − action := Action(s) | |
| 305 | − | |
| 306 | − if action.IsValid() { | |
| 307 | − return action, nil | |
| 308 | − } | |
| 309 | − | |
| 310 | − return action, Errorf("unsupported action '%v'", s) | |
| 311 | −} | |
| 312 | − | |
| 313 | −// actionConditionKeyMap - holds mapping of supported condition key for an action. | |
| 314 | −var actionConditionKeyMap = map[Action]condition.KeySet{ | |
| 315 | − AbortMultipartUploadAction: condition.NewKeySet(condition.CommonKeys...), | |
| 316 | − | |
| 317 | − CreateBucketAction: condition.NewKeySet(condition.CommonKeys...), | |
| 318 | − | |
| 319 | − DeleteObjectAction: condition.NewKeySet(condition.CommonKeys...), | |
| 320 | − | |
| 321 | − GetBucketLocationAction: condition.NewKeySet(condition.CommonKeys...), | |
| 322 | − | |
| 323 | − GetBucketPolicyStatusAction: condition.NewKeySet(condition.CommonKeys...), | |
| 324 | − | |
| 325 | − GetObjectAction: condition.NewKeySet( | |
| 326 | − append([]condition.Key{ | |
| 327 | − condition.S3XAmzServerSideEncryption, | |
| 328 | − condition.S3XAmzServerSideEncryptionCustomerAlgorithm, | |
| 329 | − }, condition.CommonKeys...)...), | |
| 330 | − | |
| 331 | − HeadBucketAction: condition.NewKeySet(condition.CommonKeys...), | |
| 332 | − | |
| 333 | − ListAllMyBucketsAction: condition.NewKeySet(condition.CommonKeys...), | |
| 334 | − | |
| 335 | − ListBucketAction: condition.NewKeySet( | |
| 336 | − append([]condition.Key{ | |
| 337 | − condition.S3Prefix, | |
| 338 | − condition.S3Delimiter, | |
| 339 | − condition.S3MaxKeys, | |
| 340 | − }, condition.CommonKeys...)...), | |
| 341 | − | |
| 342 | − ListBucketVersionsAction: condition.NewKeySet( | |
| 343 | − append([]condition.Key{ | |
| 344 | − condition.S3Prefix, | |
| 345 | − condition.S3Delimiter, | |
| 346 | − condition.S3MaxKeys, | |
| 347 | − }, condition.CommonKeys...)...), | |
| 348 | − | |
| 349 | − ListBucketMultipartUploadsAction: condition.NewKeySet(condition.CommonKeys...), | |
| 350 | − | |
| 351 | − ListenNotificationAction: condition.NewKeySet(condition.CommonKeys...), | |
| 352 | − | |
| 353 | − ListenBucketNotificationAction: condition.NewKeySet(condition.CommonKeys...), | |
| 354 | − | |
| 355 | − ListMultipartUploadPartsAction: condition.NewKeySet(condition.CommonKeys...), | |
| 356 | − | |
| 357 | − PutObjectAction: condition.NewKeySet( | |
| 358 | − append([]condition.Key{ | |
| 359 | − condition.S3XAmzCopySource, | |
| 360 | − condition.S3XAmzServerSideEncryption, | |
| 361 | − condition.S3XAmzServerSideEncryptionCustomerAlgorithm, | |
| 362 | − condition.S3XAmzMetadataDirective, | |
| 363 | − condition.S3XAmzStorageClass, | |
| 364 | − condition.S3ObjectLockRetainUntilDate, | |
| 365 | − condition.S3ObjectLockMode, | |
| 366 | − condition.S3ObjectLockLegalHold, | |
| 367 | − }, condition.CommonKeys...)...), | |
| 368 | − | |
| 369 | − // https://docs.aws.amazon.com/AmazonS3/latest/dev/list_amazons3.html | |
| 370 | − // LockLegalHold is not supported with PutObjectRetentionAction | |
| 371 | − PutObjectRetentionAction: condition.NewKeySet( | |
| 372 | − append([]condition.Key{ | |
| 373 | − condition.S3ObjectLockRemainingRetentionDays, | |
| 374 | − condition.S3ObjectLockRetainUntilDate, | |
| 375 | − condition.S3ObjectLockMode, | |
| 376 | − }, condition.CommonKeys...)...), | |
| 377 | − | |
| 378 | − GetObjectRetentionAction: condition.NewKeySet(condition.CommonKeys...), | |
| 379 | − PutObjectLegalHoldAction: condition.NewKeySet( | |
| 380 | − append([]condition.Key{ | |
| 381 | − condition.S3ObjectLockLegalHold, | |
| 382 | − }, condition.CommonKeys...)...), | |
| 383 | − GetObjectLegalHoldAction: condition.NewKeySet(condition.CommonKeys...), | |
| 384 | − | |
| 385 | − // https://docs.aws.amazon.com/AmazonS3/latest/dev/list_amazons3.html | |
| 386 | − BypassGovernanceRetentionAction: condition.NewKeySet( | |
| 387 | − append([]condition.Key{ | |
| 388 | − condition.S3ObjectLockRemainingRetentionDays, | |
| 389 | − condition.S3ObjectLockRetainUntilDate, | |
| 390 | − condition.S3ObjectLockMode, | |
| 391 | − condition.S3ObjectLockLegalHold, | |
| 392 | − }, condition.CommonKeys...)...), | |
| 393 | − | |
| 394 | − GetBucketObjectLockConfigurationAction: condition.NewKeySet(condition.CommonKeys...), | |
| 395 | − PutBucketObjectLockConfigurationAction: condition.NewKeySet(condition.CommonKeys...), | |
| 396 | − GetBucketTaggingAction: condition.NewKeySet(condition.CommonKeys...), | |
| 397 | − PutBucketTaggingAction: condition.NewKeySet(condition.CommonKeys...), | |
| 398 | − PutObjectTaggingAction: condition.NewKeySet(condition.CommonKeys...), | |
| 399 | − GetObjectTaggingAction: condition.NewKeySet(condition.CommonKeys...), | |
| 400 | − DeleteObjectTaggingAction: condition.NewKeySet(condition.CommonKeys...), | |
| 401 | − | |
| 402 | − PutObjectVersionTaggingAction: condition.NewKeySet(condition.CommonKeys...), | |
| 403 | − GetObjectVersionAction: condition.NewKeySet( | |
| 404 | − append([]condition.Key{ | |
| 405 | − condition.S3VersionID, | |
| 406 | − }, condition.CommonKeys...)...), | |
| 407 | − GetObjectVersionTaggingAction: condition.NewKeySet( | |
| 408 | − append([]condition.Key{ | |
| 409 | − condition.S3VersionID, | |
| 410 | − }, condition.CommonKeys...)...), | |
| 411 | − DeleteObjectVersionAction: condition.NewKeySet( | |
| 412 | − append([]condition.Key{ | |
| 413 | − condition.S3VersionID, | |
| 414 | − }, condition.CommonKeys...)...), | |
| 415 | − DeleteObjectVersionTaggingAction: condition.NewKeySet( | |
| 416 | − append([]condition.Key{ | |
| 417 | − condition.S3VersionID, | |
| 418 | − }, condition.CommonKeys...)...), | |
| 419 | − GetReplicationConfigurationAction: condition.NewKeySet(condition.CommonKeys...), | |
| 420 | − PutReplicationConfigurationAction: condition.NewKeySet(condition.CommonKeys...), | |
| 421 | − ReplicateObjectAction: condition.NewKeySet(condition.CommonKeys...), | |
| 422 | − ReplicateDeleteAction: condition.NewKeySet(condition.CommonKeys...), | |
| 423 | − ReplicateTagsAction: condition.NewKeySet(condition.CommonKeys...), | |
| 424 | − GetObjectVersionForReplicationAction: condition.NewKeySet(condition.CommonKeys...), | |
| 425 | − RestoreObjectAction: condition.NewKeySet(condition.CommonKeys...), | |
| 426 | −} | |
| @@ -1,117 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestActionIsObjectAction(t *testing.T) { | |
| 27 | − testCases := []struct { | |
| 28 | − action Action | |
| 29 | − expectedResult bool | |
| 30 | − }{ | |
| 31 | − {AbortMultipartUploadAction, true}, | |
| 32 | − {DeleteObjectAction, true}, | |
| 33 | − {GetObjectAction, true}, | |
| 34 | − {ListMultipartUploadPartsAction, true}, | |
| 35 | − {PutObjectAction, true}, | |
| 36 | − {CreateBucketAction, false}, | |
| 37 | − } | |
| 38 | − | |
| 39 | − for i, testCase := range testCases { | |
| 40 | − result := testCase.action.isObjectAction() | |
| 41 | − | |
| 42 | − if testCase.expectedResult != result { | |
| 43 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 44 | − } | |
| 45 | − } | |
| 46 | −} | |
| 47 | − | |
| 48 | −func TestActionIsValid(t *testing.T) { | |
| 49 | − testCases := []struct { | |
| 50 | − action Action | |
| 51 | − expectedResult bool | |
| 52 | − }{ | |
| 53 | − {AbortMultipartUploadAction, true}, | |
| 54 | − {Action("foo"), false}, | |
| 55 | − } | |
| 56 | − | |
| 57 | − for i, testCase := range testCases { | |
| 58 | − result := testCase.action.IsValid() | |
| 59 | − | |
| 60 | − if testCase.expectedResult != result { | |
| 61 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 62 | − } | |
| 63 | − } | |
| 64 | −} | |
| 65 | − | |
| 66 | −func TestActionMarshalJSON(t *testing.T) { | |
| 67 | − testCases := []struct { | |
| 68 | − action Action | |
| 69 | − expectedResult []byte | |
| 70 | − expectErr bool | |
| 71 | − }{ | |
| 72 | − {PutObjectAction, []byte(`"s3:PutObject"`), false}, | |
| 73 | − {Action("foo"), nil, true}, | |
| 74 | − } | |
| 75 | − | |
| 76 | − for i, testCase := range testCases { | |
| 77 | − result, err := json.Marshal(testCase.action) | |
| 78 | − expectErr := (err != nil) | |
| 79 | − | |
| 80 | − if testCase.expectErr != expectErr { | |
| 81 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 82 | − } | |
| 83 | − | |
| 84 | − if !testCase.expectErr { | |
| 85 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 86 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 87 | − } | |
| 88 | − } | |
| 89 | − } | |
| 90 | −} | |
| 91 | − | |
| 92 | −func TestActionUnmarshalJSON(t *testing.T) { | |
| 93 | − testCases := []struct { | |
| 94 | − data []byte | |
| 95 | − expectedResult Action | |
| 96 | − expectErr bool | |
| 97 | − }{ | |
| 98 | − {[]byte(`"s3:PutObject"`), PutObjectAction, false}, | |
| 99 | − {[]byte(`"foo"`), Action(""), true}, | |
| 100 | − } | |
| 101 | − | |
| 102 | − for i, testCase := range testCases { | |
| 103 | − var result Action | |
| 104 | − err := json.Unmarshal(testCase.data, &result) | |
| 105 | − expectErr := (err != nil) | |
| 106 | − | |
| 107 | − if testCase.expectErr != expectErr { | |
| 108 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 109 | − } | |
| 110 | − | |
| 111 | − if !testCase.expectErr { | |
| 112 | − if testCase.expectedResult != result { | |
| 113 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 114 | − } | |
| 115 | − } | |
| 116 | − } | |
| 117 | −} | |
| @@ -1,137 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | − "sort" | |
| 24 | − | |
| 25 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 26 | −) | |
| 27 | − | |
| 28 | −// ActionSet - set of actions. | |
| 29 | −type ActionSet map[Action]struct{} | |
| 30 | − | |
| 31 | −// Add - add action to the set. | |
| 32 | −func (actionSet ActionSet) Add(action Action) { | |
| 33 | − actionSet[action] = struct{}{} | |
| 34 | −} | |
| 35 | − | |
| 36 | −// Contains - checks given action exists in the action set. | |
| 37 | −func (actionSet ActionSet) Contains(action Action) bool { | |
| 38 | − _, found := actionSet[action] | |
| 39 | − return found | |
| 40 | −} | |
| 41 | − | |
| 42 | −// Equals - checks whether given action set is equal to current action set or not. | |
| 43 | −func (actionSet ActionSet) Equals(sactionSet ActionSet) bool { | |
| 44 | − // If length of set is not equal to length of given set, the | |
| 45 | − // set is not equal to given set. | |
| 46 | − if len(actionSet) != len(sactionSet) { | |
| 47 | − return false | |
| 48 | − } | |
| 49 | − | |
| 50 | − // As both sets are equal in length, check each elements are equal. | |
| 51 | − for k := range actionSet { | |
| 52 | − if _, ok := sactionSet[k]; !ok { | |
| 53 | − return false | |
| 54 | − } | |
| 55 | − } | |
| 56 | − | |
| 57 | − return true | |
| 58 | −} | |
| 59 | − | |
| 60 | −// Intersection - returns actions available in both ActionSet. | |
| 61 | −func (actionSet ActionSet) Intersection(sset ActionSet) ActionSet { | |
| 62 | − nset := NewActionSet() | |
| 63 | − for k := range actionSet { | |
| 64 | − if _, ok := sset[k]; ok { | |
| 65 | − nset.Add(k) | |
| 66 | − } | |
| 67 | − } | |
| 68 | − | |
| 69 | − return nset | |
| 70 | −} | |
| 71 | − | |
| 72 | −// MarshalJSON - encodes ActionSet to JSON data. | |
| 73 | −func (actionSet ActionSet) MarshalJSON() ([]byte, error) { | |
| 74 | − if len(actionSet) == 0 { | |
| 75 | − return nil, Errorf("empty actions not allowed") | |
| 76 | − } | |
| 77 | − | |
| 78 | − return json.Marshal(actionSet.ToSlice()) | |
| 79 | −} | |
| 80 | − | |
| 81 | −func (actionSet ActionSet) String() string { | |
| 82 | − actions := []string{} | |
| 83 | − for action := range actionSet { | |
| 84 | − actions = append(actions, string(action)) | |
| 85 | − } | |
| 86 | − sort.Strings(actions) | |
| 87 | − | |
| 88 | − return fmt.Sprintf("%v", actions) | |
| 89 | −} | |
| 90 | − | |
| 91 | −// ToSlice - returns slice of actions from the action set. | |
| 92 | −func (actionSet ActionSet) ToSlice() []Action { | |
| 93 | − actions := []Action{} | |
| 94 | − for action := range actionSet { | |
| 95 | − actions = append(actions, action) | |
| 96 | − } | |
| 97 | − return actions | |
| 98 | −} | |
| 99 | − | |
| 100 | −// Clone clones ActionSet structure | |
| 101 | −func (actionSet ActionSet) Clone() ActionSet { | |
| 102 | − return NewActionSet(actionSet.ToSlice()...) | |
| 103 | −} | |
| 104 | − | |
| 105 | −// UnmarshalJSON - decodes JSON data to ActionSet. | |
| 106 | −func (actionSet *ActionSet) UnmarshalJSON(data []byte) error { | |
| 107 | − var sset set.StringSet | |
| 108 | − if err := json.Unmarshal(data, &sset); err != nil { | |
| 109 | − return err | |
| 110 | − } | |
| 111 | − | |
| 112 | − if len(sset) == 0 { | |
| 113 | − return Errorf("empty actions not allowed") | |
| 114 | − } | |
| 115 | − | |
| 116 | − *actionSet = make(ActionSet) | |
| 117 | − for _, s := range sset.ToSlice() { | |
| 118 | − action, err := parseAction(s) | |
| 119 | − if err != nil { | |
| 120 | − return err | |
| 121 | − } | |
| 122 | − | |
| 123 | − actionSet.Add(action) | |
| 124 | − } | |
| 125 | − | |
| 126 | − return nil | |
| 127 | −} | |
| 128 | − | |
| 129 | −// NewActionSet - creates new action set. | |
| 130 | −func NewActionSet(actions ...Action) ActionSet { | |
| 131 | − actionSet := make(ActionSet) | |
| 132 | − for _, action := range actions { | |
| 133 | − actionSet.Add(action) | |
| 134 | − } | |
| 135 | − | |
| 136 | − return actionSet | |
| 137 | −} | |
| @@ -1,159 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestActionSetAdd(t *testing.T) { | |
| 27 | − testCases := []struct { | |
| 28 | − set ActionSet | |
| 29 | − action Action | |
| 30 | − expectedResult ActionSet | |
| 31 | − }{ | |
| 32 | − {NewActionSet(), PutObjectAction, NewActionSet(PutObjectAction)}, | |
| 33 | − {NewActionSet(PutObjectAction), PutObjectAction, NewActionSet(PutObjectAction)}, | |
| 34 | − } | |
| 35 | − | |
| 36 | − for i, testCase := range testCases { | |
| 37 | − testCase.set.Add(testCase.action) | |
| 38 | − | |
| 39 | − if !reflect.DeepEqual(testCase.expectedResult, testCase.set) { | |
| 40 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set) | |
| 41 | − } | |
| 42 | − } | |
| 43 | −} | |
| 44 | − | |
| 45 | −func TestActionSetContains(t *testing.T) { | |
| 46 | − testCases := []struct { | |
| 47 | − set ActionSet | |
| 48 | − action Action | |
| 49 | − expectedResult bool | |
| 50 | − }{ | |
| 51 | − {NewActionSet(PutObjectAction), PutObjectAction, true}, | |
| 52 | − {NewActionSet(PutObjectAction, GetObjectAction), PutObjectAction, true}, | |
| 53 | − {NewActionSet(PutObjectAction, GetObjectAction), AbortMultipartUploadAction, false}, | |
| 54 | − } | |
| 55 | − | |
| 56 | − for i, testCase := range testCases { | |
| 57 | − result := testCase.set.Contains(testCase.action) | |
| 58 | − | |
| 59 | − if result != testCase.expectedResult { | |
| 60 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 61 | − } | |
| 62 | − } | |
| 63 | −} | |
| 64 | − | |
| 65 | −func TestActionSetIntersection(t *testing.T) { | |
| 66 | − testCases := []struct { | |
| 67 | − set ActionSet | |
| 68 | − setToIntersect ActionSet | |
| 69 | − expectedResult ActionSet | |
| 70 | − }{ | |
| 71 | − {NewActionSet(), NewActionSet(PutObjectAction), NewActionSet()}, | |
| 72 | − {NewActionSet(PutObjectAction), NewActionSet(), NewActionSet()}, | |
| 73 | − {NewActionSet(PutObjectAction), NewActionSet(PutObjectAction, GetObjectAction), NewActionSet(PutObjectAction)}, | |
| 74 | − } | |
| 75 | − | |
| 76 | − for i, testCase := range testCases { | |
| 77 | − result := testCase.set.Intersection(testCase.setToIntersect) | |
| 78 | − | |
| 79 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 80 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set) | |
| 81 | − } | |
| 82 | − } | |
| 83 | −} | |
| 84 | − | |
| 85 | −func TestActionSetMarshalJSON(t *testing.T) { | |
| 86 | − testCases := []struct { | |
| 87 | − actionSet ActionSet | |
| 88 | − expectedResult []byte | |
| 89 | − expectErr bool | |
| 90 | − }{ | |
| 91 | − {NewActionSet(PutObjectAction), []byte(`["s3:PutObject"]`), false}, | |
| 92 | − {NewActionSet(), nil, true}, | |
| 93 | − } | |
| 94 | − | |
| 95 | − for i, testCase := range testCases { | |
| 96 | − result, err := json.Marshal(testCase.actionSet) | |
| 97 | − expectErr := (err != nil) | |
| 98 | − | |
| 99 | − if expectErr != testCase.expectErr { | |
| 100 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 101 | − } | |
| 102 | − | |
| 103 | − if !testCase.expectErr { | |
| 104 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 105 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, string(testCase.expectedResult), string(result)) | |
| 106 | − } | |
| 107 | − } | |
| 108 | − } | |
| 109 | −} | |
| 110 | − | |
| 111 | −func TestActionSetToSlice(t *testing.T) { | |
| 112 | − testCases := []struct { | |
| 113 | − actionSet ActionSet | |
| 114 | − expectedResult []Action | |
| 115 | − }{ | |
| 116 | − {NewActionSet(PutObjectAction), []Action{PutObjectAction}}, | |
| 117 | − {NewActionSet(), []Action{}}, | |
| 118 | − } | |
| 119 | − | |
| 120 | − for i, testCase := range testCases { | |
| 121 | − result := testCase.actionSet.ToSlice() | |
| 122 | − | |
| 123 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 124 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 125 | − } | |
| 126 | − } | |
| 127 | −} | |
| 128 | − | |
| 129 | −func TestActionSetUnmarshalJSON(t *testing.T) { | |
| 130 | − testCases := []struct { | |
| 131 | − data []byte | |
| 132 | − expectedResult ActionSet | |
| 133 | − expectErr bool | |
| 134 | − }{ | |
| 135 | − {[]byte(`"s3:PutObject"`), NewActionSet(PutObjectAction), false}, | |
| 136 | − {[]byte(`["s3:PutObject"]`), NewActionSet(PutObjectAction), false}, | |
| 137 | − {[]byte(`["s3:PutObject", "s3:GetObject"]`), NewActionSet(PutObjectAction, GetObjectAction), false}, | |
| 138 | − {[]byte(`["s3:PutObject", "s3:GetObject", "s3:PutObject"]`), NewActionSet(PutObjectAction, GetObjectAction), false}, | |
| 139 | − {[]byte(`[]`), NewActionSet(), true}, // Empty array. | |
| 140 | − {[]byte(`"foo"`), nil, true}, // Invalid action. | |
| 141 | − {[]byte(`["s3:PutObject", "foo"]`), nil, true}, // Invalid action. | |
| 142 | − } | |
| 143 | − | |
| 144 | − for i, testCase := range testCases { | |
| 145 | − result := make(ActionSet) | |
| 146 | − err := json.Unmarshal(testCase.data, &result) | |
| 147 | − expectErr := (err != nil) | |
| 148 | − | |
| 149 | − if expectErr != testCase.expectErr { | |
| 150 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 151 | − } | |
| 152 | − | |
| 153 | − if !testCase.expectErr { | |
| 154 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 155 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 156 | − } | |
| 157 | − } | |
| 158 | − } | |
| 159 | −} | |
| @@ -1,144 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/base64" | |
| 22 | − "fmt" | |
| 23 | − "net/http" | |
| 24 | − "sort" | |
| 25 | − | |
| 26 | − "github.com/minio/minio-go/v7/pkg/s3utils" | |
| 27 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 28 | −) | |
| 29 | − | |
| 30 | −func toBinaryEqualsFuncString(n name, key Key, values set.StringSet) string { | |
| 31 | − valueStrings := values.ToSlice() | |
| 32 | − sort.Strings(valueStrings) | |
| 33 | − | |
| 34 | − return fmt.Sprintf("%v:%v:%v", n, key, valueStrings) | |
| 35 | −} | |
| 36 | − | |
| 37 | −// binaryEqualsFunc - String equals function. It checks whether value by Key in given | |
| 38 | −// values map is in condition values. | |
| 39 | −// For example, | |
| 40 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 41 | −// in value map for Key is in values. | |
| 42 | −type binaryEqualsFunc struct { | |
| 43 | − k Key | |
| 44 | − values set.StringSet | |
| 45 | −} | |
| 46 | − | |
| 47 | −// evaluate() - evaluates to check whether value by Key in given values is in | |
| 48 | −// condition values. | |
| 49 | −func (f binaryEqualsFunc) evaluate(values map[string][]string) bool { | |
| 50 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 51 | − if !ok { | |
| 52 | − requestValue = values[f.k.Name()] | |
| 53 | − } | |
| 54 | − | |
| 55 | − fvalues := f.values.ApplyFunc(substFuncFromValues(values)) | |
| 56 | − return !fvalues.Intersection(set.CreateStringSet(requestValue...)).IsEmpty() | |
| 57 | −} | |
| 58 | − | |
| 59 | −// key() - returns condition key which is used by this condition function. | |
| 60 | −func (f binaryEqualsFunc) key() Key { | |
| 61 | − return f.k | |
| 62 | −} | |
| 63 | − | |
| 64 | −// name() - returns "BinaryEquals" condition name. | |
| 65 | −func (f binaryEqualsFunc) name() name { | |
| 66 | − return binaryEquals | |
| 67 | −} | |
| 68 | − | |
| 69 | −func (f binaryEqualsFunc) String() string { | |
| 70 | − return toBinaryEqualsFuncString(binaryEquals, f.k, f.values) | |
| 71 | −} | |
| 72 | − | |
| 73 | −// toMap - returns map representation of this function. | |
| 74 | −func (f binaryEqualsFunc) toMap() map[Key]ValueSet { | |
| 75 | − if !f.k.IsValid() { | |
| 76 | − return nil | |
| 77 | − } | |
| 78 | − | |
| 79 | − values := NewValueSet() | |
| 80 | − for _, value := range f.values.ToSlice() { | |
| 81 | − values.Add(NewStringValue(base64.StdEncoding.EncodeToString([]byte(value)))) | |
| 82 | − } | |
| 83 | − | |
| 84 | − return map[Key]ValueSet{ | |
| 85 | − f.k: values, | |
| 86 | − } | |
| 87 | −} | |
| 88 | − | |
| 89 | −func validateBinaryEqualsValues(n name, key Key, values set.StringSet) error { | |
| 90 | − vslice := values.ToSlice() | |
| 91 | − for _, s := range vslice { | |
| 92 | − sbytes, err := base64.StdEncoding.DecodeString(s) | |
| 93 | − if err != nil { | |
| 94 | − return err | |
| 95 | − } | |
| 96 | − values.Remove(s) | |
| 97 | − s = string(sbytes) | |
| 98 | − switch key { | |
| 99 | − case S3XAmzCopySource: | |
| 100 | − bucket, object := path2BucketAndObject(s) | |
| 101 | − if object == "" { | |
| 102 | − return fmt.Errorf("invalid value '%v' for '%v' for %v condition", s, S3XAmzCopySource, n) | |
| 103 | − } | |
| 104 | − if err = s3utils.CheckValidBucketName(bucket); err != nil { | |
| 105 | − return err | |
| 106 | − } | |
| 107 | − case S3XAmzServerSideEncryption, S3XAmzServerSideEncryptionCustomerAlgorithm: | |
| 108 | − if s != "AES256" { | |
| 109 | − return fmt.Errorf("invalid value '%v' for '%v' for %v condition", s, S3XAmzServerSideEncryption, n) | |
| 110 | − } | |
| 111 | − case S3XAmzMetadataDirective: | |
| 112 | − if s != "COPY" && s != "REPLACE" { | |
| 113 | − return fmt.Errorf("invalid value '%v' for '%v' for %v condition", s, S3XAmzMetadataDirective, n) | |
| 114 | − } | |
| 115 | − case S3XAmzContentSha256: | |
| 116 | − if s == "" { | |
| 117 | − return fmt.Errorf("invalid empty value for '%v' for %v condition", S3XAmzContentSha256, n) | |
| 118 | − } | |
| 119 | − } | |
| 120 | − values.Add(s) | |
| 121 | − } | |
| 122 | − | |
| 123 | − return nil | |
| 124 | −} | |
| 125 | − | |
| 126 | −// newBinaryEqualsFunc - returns new BinaryEquals function. | |
| 127 | −func newBinaryEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 128 | − valueStrings, err := valuesToStringSlice(binaryEquals, values) | |
| 129 | − if err != nil { | |
| 130 | − return nil, err | |
| 131 | − } | |
| 132 | − | |
| 133 | − return NewBinaryEqualsFunc(key, valueStrings...) | |
| 134 | −} | |
| 135 | − | |
| 136 | −// NewBinaryEqualsFunc - returns new BinaryEquals function. | |
| 137 | −func NewBinaryEqualsFunc(key Key, values ...string) (Function, error) { | |
| 138 | − sset := set.CreateStringSet(values...) | |
| 139 | − if err := validateBinaryEqualsValues(binaryEquals, key, sset); err != nil { | |
| 140 | − return nil, err | |
| 141 | − } | |
| 142 | − | |
| 143 | − return &binaryEqualsFunc{key, sset}, nil | |
| 144 | −} | |
| @@ -1,383 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/base64" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestBinaryEqualsFuncEvaluate(t *testing.T) { | |
| 27 | − case1Function, err := newBinaryEqualsFunc(S3XAmzCopySource, | |
| 28 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject"))))) | |
| 29 | − if err != nil { | |
| 30 | − t.Fatalf("unexpected error. %v\n", err) | |
| 31 | − } | |
| 32 | − | |
| 33 | − case2Function, err := newBinaryEqualsFunc(S3XAmzServerSideEncryption, | |
| 34 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256"))))) | |
| 35 | − if err != nil { | |
| 36 | − t.Fatalf("unexpected error. %v\n", err) | |
| 37 | − } | |
| 38 | − | |
| 39 | − case3Function, err := newBinaryEqualsFunc(S3XAmzMetadataDirective, | |
| 40 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE"))))) | |
| 41 | − if err != nil { | |
| 42 | − t.Fatalf("unexpected error. %v\n", err) | |
| 43 | − } | |
| 44 | − | |
| 45 | − case4Function, err := newBinaryEqualsFunc(S3LocationConstraint, | |
| 46 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1"))))) | |
| 47 | − if err != nil { | |
| 48 | − t.Fatalf("unexpected error. %v\n", err) | |
| 49 | − } | |
| 50 | − | |
| 51 | − testCases := []struct { | |
| 52 | − function Function | |
| 53 | − values map[string][]string | |
| 54 | − expectedResult bool | |
| 55 | − }{ | |
| 56 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, true}, | |
| 57 | − {case1Function, map[string][]string{"x-amz-copy-source": {"yourbucket/myobject"}}, false}, | |
| 58 | − {case1Function, map[string][]string{}, false}, | |
| 59 | − {case1Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 60 | − | |
| 61 | − {case2Function, map[string][]string{"x-amz-server-side-encryption": {"AES256"}}, true}, | |
| 62 | − {case2Function, map[string][]string{}, false}, | |
| 63 | − {case2Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 64 | − | |
| 65 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE"}}, true}, | |
| 66 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"COPY"}}, false}, | |
| 67 | − {case3Function, map[string][]string{}, false}, | |
| 68 | − {case3Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 69 | − | |
| 70 | − {case4Function, map[string][]string{"LocationConstraint": {"eu-west-1"}}, true}, | |
| 71 | − {case4Function, map[string][]string{"LocationConstraint": {"us-east-1"}}, false}, | |
| 72 | − {case4Function, map[string][]string{}, false}, | |
| 73 | − {case4Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 74 | − } | |
| 75 | − | |
| 76 | − for i, testCase := range testCases { | |
| 77 | − result := testCase.function.evaluate(testCase.values) | |
| 78 | − | |
| 79 | − if result != testCase.expectedResult { | |
| 80 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 81 | − } | |
| 82 | − } | |
| 83 | −} | |
| 84 | − | |
| 85 | −func TestBinaryEqualsFuncKey(t *testing.T) { | |
| 86 | − case1Function, err := newBinaryEqualsFunc(S3XAmzCopySource, | |
| 87 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject"))))) | |
| 88 | − if err != nil { | |
| 89 | − t.Fatalf("unexpected error. %v\n", err) | |
| 90 | − } | |
| 91 | − | |
| 92 | − case2Function, err := newBinaryEqualsFunc(S3XAmzServerSideEncryption, | |
| 93 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256"))))) | |
| 94 | − if err != nil { | |
| 95 | − t.Fatalf("unexpected error. %v\n", err) | |
| 96 | − } | |
| 97 | − | |
| 98 | − case3Function, err := newBinaryEqualsFunc(S3XAmzMetadataDirective, | |
| 99 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE"))))) | |
| 100 | − if err != nil { | |
| 101 | − t.Fatalf("unexpected error. %v\n", err) | |
| 102 | − } | |
| 103 | − | |
| 104 | − case4Function, err := newBinaryEqualsFunc(S3LocationConstraint, | |
| 105 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1"))))) | |
| 106 | − if err != nil { | |
| 107 | − t.Fatalf("unexpected error. %v\n", err) | |
| 108 | − } | |
| 109 | − | |
| 110 | − testCases := []struct { | |
| 111 | − function Function | |
| 112 | − expectedResult Key | |
| 113 | − }{ | |
| 114 | − {case1Function, S3XAmzCopySource}, | |
| 115 | − {case2Function, S3XAmzServerSideEncryption}, | |
| 116 | − {case3Function, S3XAmzMetadataDirective}, | |
| 117 | − {case4Function, S3LocationConstraint}, | |
| 118 | − } | |
| 119 | − | |
| 120 | − for i, testCase := range testCases { | |
| 121 | − result := testCase.function.key() | |
| 122 | − | |
| 123 | − if result != testCase.expectedResult { | |
| 124 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 125 | − } | |
| 126 | − } | |
| 127 | −} | |
| 128 | − | |
| 129 | −func TestBinaryEqualsFuncToMap(t *testing.T) { | |
| 130 | − case1Function, err := newBinaryEqualsFunc(S3XAmzCopySource, | |
| 131 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject"))))) | |
| 132 | − if err != nil { | |
| 133 | − t.Fatalf("unexpected error. %v\n", err) | |
| 134 | − } | |
| 135 | − | |
| 136 | − case1Result := map[Key]ValueSet{ | |
| 137 | − S3XAmzCopySource: NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject")))), | |
| 138 | − } | |
| 139 | − | |
| 140 | − case2Function, err := newBinaryEqualsFunc(S3XAmzCopySource, | |
| 141 | − NewValueSet( | |
| 142 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject"))), | |
| 143 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("yourbucket/myobject"))), | |
| 144 | − ), | |
| 145 | − ) | |
| 146 | − if err != nil { | |
| 147 | − t.Fatalf("unexpected error. %v\n", err) | |
| 148 | − } | |
| 149 | − | |
| 150 | − case2Result := map[Key]ValueSet{ | |
| 151 | − S3XAmzCopySource: NewValueSet( | |
| 152 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject"))), | |
| 153 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("yourbucket/myobject"))), | |
| 154 | − ), | |
| 155 | − } | |
| 156 | − | |
| 157 | − case3Function, err := newBinaryEqualsFunc(S3XAmzServerSideEncryption, | |
| 158 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256"))))) | |
| 159 | − if err != nil { | |
| 160 | − t.Fatalf("unexpected error. %v\n", err) | |
| 161 | − } | |
| 162 | − | |
| 163 | − case3Result := map[Key]ValueSet{ | |
| 164 | − S3XAmzServerSideEncryption: NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256")))), | |
| 165 | − } | |
| 166 | − | |
| 167 | − case4Function, err := newBinaryEqualsFunc(S3XAmzServerSideEncryption, | |
| 168 | − NewValueSet( | |
| 169 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256"))), | |
| 170 | − ), | |
| 171 | − ) | |
| 172 | − if err != nil { | |
| 173 | − t.Fatalf("unexpected error. %v\n", err) | |
| 174 | − } | |
| 175 | − | |
| 176 | − case4Result := map[Key]ValueSet{ | |
| 177 | − S3XAmzServerSideEncryption: NewValueSet( | |
| 178 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256"))), | |
| 179 | − ), | |
| 180 | − } | |
| 181 | − | |
| 182 | − case5Function, err := newBinaryEqualsFunc(S3XAmzMetadataDirective, | |
| 183 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE"))))) | |
| 184 | − if err != nil { | |
| 185 | − t.Fatalf("unexpected error. %v\n", err) | |
| 186 | − } | |
| 187 | − | |
| 188 | − case5Result := map[Key]ValueSet{ | |
| 189 | − S3XAmzMetadataDirective: NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE")))), | |
| 190 | − } | |
| 191 | − | |
| 192 | − case6Function, err := newBinaryEqualsFunc(S3XAmzMetadataDirective, | |
| 193 | − NewValueSet( | |
| 194 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE"))), | |
| 195 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("COPY"))), | |
| 196 | − ), | |
| 197 | − ) | |
| 198 | − if err != nil { | |
| 199 | − t.Fatalf("unexpected error. %v\n", err) | |
| 200 | − } | |
| 201 | − | |
| 202 | − case6Result := map[Key]ValueSet{ | |
| 203 | − S3XAmzMetadataDirective: NewValueSet( | |
| 204 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE"))), | |
| 205 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("COPY"))), | |
| 206 | − ), | |
| 207 | − } | |
| 208 | − | |
| 209 | − case7Function, err := newBinaryEqualsFunc(S3LocationConstraint, | |
| 210 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1"))))) | |
| 211 | − if err != nil { | |
| 212 | − t.Fatalf("unexpected error. %v\n", err) | |
| 213 | − } | |
| 214 | − | |
| 215 | − case7Result := map[Key]ValueSet{ | |
| 216 | − S3LocationConstraint: NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1")))), | |
| 217 | − } | |
| 218 | − | |
| 219 | − case8Function, err := newBinaryEqualsFunc(S3LocationConstraint, | |
| 220 | − NewValueSet( | |
| 221 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1"))), | |
| 222 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("us-west-1"))), | |
| 223 | − ), | |
| 224 | − ) | |
| 225 | − if err != nil { | |
| 226 | − t.Fatalf("unexpected error. %v\n", err) | |
| 227 | − } | |
| 228 | − | |
| 229 | − case8Result := map[Key]ValueSet{ | |
| 230 | − S3LocationConstraint: NewValueSet( | |
| 231 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1"))), | |
| 232 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("us-west-1"))), | |
| 233 | − ), | |
| 234 | − } | |
| 235 | − | |
| 236 | − testCases := []struct { | |
| 237 | − f Function | |
| 238 | − expectedResult map[Key]ValueSet | |
| 239 | − }{ | |
| 240 | − {case1Function, case1Result}, | |
| 241 | − {case2Function, case2Result}, | |
| 242 | − {case3Function, case3Result}, | |
| 243 | − {case4Function, case4Result}, | |
| 244 | − {case5Function, case5Result}, | |
| 245 | − {case6Function, case6Result}, | |
| 246 | − {case7Function, case7Result}, | |
| 247 | − {case8Function, case8Result}, | |
| 248 | − {&binaryEqualsFunc{}, nil}, | |
| 249 | − } | |
| 250 | − | |
| 251 | − for i, testCase := range testCases { | |
| 252 | − result := testCase.f.toMap() | |
| 253 | − | |
| 254 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 255 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 256 | − } | |
| 257 | − } | |
| 258 | −} | |
| 259 | − | |
| 260 | −func TestNewBinaryEqualsFunc(t *testing.T) { | |
| 261 | − case1Function, err := newBinaryEqualsFunc(S3XAmzCopySource, | |
| 262 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject"))))) | |
| 263 | − if err != nil { | |
| 264 | − t.Fatalf("unexpected error. %v\n", err) | |
| 265 | − } | |
| 266 | − | |
| 267 | − case2Function, err := newBinaryEqualsFunc(S3XAmzCopySource, | |
| 268 | − NewValueSet( | |
| 269 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject"))), | |
| 270 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("yourbucket/myobject"))), | |
| 271 | − ), | |
| 272 | − ) | |
| 273 | − if err != nil { | |
| 274 | − t.Fatalf("unexpected error. %v\n", err) | |
| 275 | − } | |
| 276 | − | |
| 277 | − case3Function, err := newBinaryEqualsFunc(S3XAmzServerSideEncryption, | |
| 278 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256"))))) | |
| 279 | − if err != nil { | |
| 280 | − t.Fatalf("unexpected error. %v\n", err) | |
| 281 | − } | |
| 282 | − | |
| 283 | − case4Function, err := newBinaryEqualsFunc(S3XAmzServerSideEncryption, | |
| 284 | − NewValueSet( | |
| 285 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256"))), | |
| 286 | − ), | |
| 287 | − ) | |
| 288 | − if err != nil { | |
| 289 | − t.Fatalf("unexpected error. %v\n", err) | |
| 290 | − } | |
| 291 | − | |
| 292 | − case5Function, err := newBinaryEqualsFunc(S3XAmzMetadataDirective, | |
| 293 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE"))))) | |
| 294 | − if err != nil { | |
| 295 | − t.Fatalf("unexpected error. %v\n", err) | |
| 296 | − } | |
| 297 | − | |
| 298 | − case6Function, err := newBinaryEqualsFunc(S3XAmzMetadataDirective, | |
| 299 | − NewValueSet( | |
| 300 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE"))), | |
| 301 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("COPY"))), | |
| 302 | − ), | |
| 303 | − ) | |
| 304 | − if err != nil { | |
| 305 | − t.Fatalf("unexpected error. %v\n", err) | |
| 306 | − } | |
| 307 | − | |
| 308 | − case7Function, err := newBinaryEqualsFunc(S3LocationConstraint, | |
| 309 | − NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1"))))) | |
| 310 | − if err != nil { | |
| 311 | − t.Fatalf("unexpected error. %v\n", err) | |
| 312 | − } | |
| 313 | − | |
| 314 | − case8Function, err := newBinaryEqualsFunc(S3LocationConstraint, | |
| 315 | − NewValueSet( | |
| 316 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1"))), | |
| 317 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("us-west-1"))), | |
| 318 | − ), | |
| 319 | − ) | |
| 320 | − if err != nil { | |
| 321 | − t.Fatalf("unexpected error. %v\n", err) | |
| 322 | − } | |
| 323 | − | |
| 324 | − testCases := []struct { | |
| 325 | − key Key | |
| 326 | − values ValueSet | |
| 327 | − expectedResult Function | |
| 328 | − expectErr bool | |
| 329 | − }{ | |
| 330 | − {S3XAmzCopySource, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject")))), case1Function, false}, | |
| 331 | − {S3XAmzCopySource, | |
| 332 | − NewValueSet( | |
| 333 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject"))), | |
| 334 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("yourbucket/myobject"))), | |
| 335 | − ), case2Function, false}, | |
| 336 | − | |
| 337 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256")))), case3Function, false}, | |
| 338 | − {S3XAmzServerSideEncryption, | |
| 339 | − NewValueSet( | |
| 340 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256"))), | |
| 341 | − ), case4Function, false}, | |
| 342 | − | |
| 343 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE")))), case5Function, false}, | |
| 344 | − {S3XAmzMetadataDirective, | |
| 345 | − NewValueSet( | |
| 346 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE"))), | |
| 347 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("COPY"))), | |
| 348 | − ), case6Function, false}, | |
| 349 | − | |
| 350 | − {S3LocationConstraint, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1")))), case7Function, false}, | |
| 351 | − {S3LocationConstraint, | |
| 352 | − NewValueSet( | |
| 353 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1"))), | |
| 354 | − NewStringValue(base64.StdEncoding.EncodeToString([]byte("us-west-1"))), | |
| 355 | − ), case8Function, false}, | |
| 356 | − | |
| 357 | − // Unsupported value error. | |
| 358 | − {S3XAmzCopySource, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket/myobject"))), NewIntValue(7)), nil, true}, | |
| 359 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("AES256"))), NewIntValue(7)), nil, true}, | |
| 360 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("REPLACE"))), NewIntValue(7)), nil, true}, | |
| 361 | − {S3LocationConstraint, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("eu-west-1"))), NewIntValue(7)), nil, true}, | |
| 362 | − | |
| 363 | − // Invalid value error. | |
| 364 | − {S3XAmzCopySource, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("mybucket")))), nil, true}, | |
| 365 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("SSE-C")))), nil, true}, | |
| 366 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue(base64.StdEncoding.EncodeToString([]byte("DUPLICATE")))), nil, true}, | |
| 367 | − } | |
| 368 | − | |
| 369 | − for i, testCase := range testCases { | |
| 370 | − result, err := newBinaryEqualsFunc(testCase.key, testCase.values) | |
| 371 | − expectErr := (err != nil) | |
| 372 | − | |
| 373 | − if expectErr != testCase.expectErr { | |
| 374 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 375 | − } | |
| 376 | − | |
| 377 | − if !testCase.expectErr { | |
| 378 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 379 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 380 | − } | |
| 381 | − } | |
| 382 | − } | |
| 383 | −} | |
| @@ -1,110 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "reflect" | |
| 24 | − "strconv" | |
| 25 | −) | |
| 26 | − | |
| 27 | −// booleanFunc - Bool condition function. It checks whether Key is true or false. | |
| 28 | −// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html#Conditions_Boolean | |
| 29 | −type booleanFunc struct { | |
| 30 | − k Key | |
| 31 | − value string | |
| 32 | −} | |
| 33 | − | |
| 34 | −// evaluate() - evaluates to check whether Key is present in given values or not. | |
| 35 | −// Depending on condition boolean value, this function returns true or false. | |
| 36 | −func (f booleanFunc) evaluate(values map[string][]string) bool { | |
| 37 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 38 | − if !ok { | |
| 39 | − requestValue = values[f.k.Name()] | |
| 40 | − } | |
| 41 | − | |
| 42 | − if len(requestValue) == 0 { | |
| 43 | − return false | |
| 44 | − } | |
| 45 | − | |
| 46 | − return f.value == requestValue[0] | |
| 47 | −} | |
| 48 | − | |
| 49 | −// key() - returns condition key which is used by this condition function. | |
| 50 | −func (f booleanFunc) key() Key { | |
| 51 | − return f.k | |
| 52 | −} | |
| 53 | − | |
| 54 | −// name() - returns "Bool" condition name. | |
| 55 | −func (f booleanFunc) name() name { | |
| 56 | − return boolean | |
| 57 | −} | |
| 58 | − | |
| 59 | −func (f booleanFunc) String() string { | |
| 60 | − return fmt.Sprintf("%v:%v:%v", boolean, f.k, f.value) | |
| 61 | −} | |
| 62 | − | |
| 63 | −// toMap - returns map representation of this function. | |
| 64 | −func (f booleanFunc) toMap() map[Key]ValueSet { | |
| 65 | − if !f.k.IsValid() { | |
| 66 | − return nil | |
| 67 | − } | |
| 68 | − | |
| 69 | − return map[Key]ValueSet{ | |
| 70 | − f.k: NewValueSet(NewStringValue(f.value)), | |
| 71 | − } | |
| 72 | −} | |
| 73 | − | |
| 74 | −func newBooleanFunc(key Key, values ValueSet) (Function, error) { | |
| 75 | − if key != AWSSecureTransport { | |
| 76 | − return nil, fmt.Errorf("only %v key is allowed for %v condition", AWSSecureTransport, boolean) | |
| 77 | − } | |
| 78 | − | |
| 79 | − if len(values) != 1 { | |
| 80 | − return nil, fmt.Errorf("only one value is allowed for boolean condition") | |
| 81 | − } | |
| 82 | − | |
| 83 | − var value Value | |
| 84 | − for v := range values { | |
| 85 | − value = v | |
| 86 | − switch v.GetType() { | |
| 87 | − case reflect.Bool: | |
| 88 | − if _, err := v.GetBool(); err != nil { | |
| 89 | − return nil, err | |
| 90 | − } | |
| 91 | − case reflect.String: | |
| 92 | − s, err := v.GetString() | |
| 93 | − if err != nil { | |
| 94 | − return nil, err | |
| 95 | − } | |
| 96 | − if _, err = strconv.ParseBool(s); err != nil { | |
| 97 | − return nil, fmt.Errorf("value must be a boolean string for boolean condition") | |
| 98 | − } | |
| 99 | − default: | |
| 100 | − return nil, fmt.Errorf("value must be a boolean for boolean condition") | |
| 101 | − } | |
| 102 | − } | |
| 103 | − | |
| 104 | − return &booleanFunc{key, value.String()}, nil | |
| 105 | −} | |
| 106 | − | |
| 107 | −// NewBoolFunc - returns new Bool function. | |
| 108 | −func NewBoolFunc(key Key, value string) (Function, error) { | |
| 109 | − return &booleanFunc{key, value}, nil | |
| 110 | −} | |
| @@ -1,153 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "reflect" | |
| 22 | − "testing" | |
| 23 | −) | |
| 24 | − | |
| 25 | −func TestBooleanFuncEvaluate(t *testing.T) { | |
| 26 | − case1Function, err := newBooleanFunc(AWSSecureTransport, NewValueSet(NewBoolValue(true))) | |
| 27 | − if err != nil { | |
| 28 | − t.Fatalf("unexpected error. %v\n", err) | |
| 29 | − } | |
| 30 | − | |
| 31 | − case2Function, err := newBooleanFunc(AWSSecureTransport, NewValueSet(NewBoolValue(false))) | |
| 32 | − if err != nil { | |
| 33 | − t.Fatalf("unexpected error. %v\n", err) | |
| 34 | − } | |
| 35 | − | |
| 36 | − testCases := []struct { | |
| 37 | − function Function | |
| 38 | − values map[string][]string | |
| 39 | − expectedResult bool | |
| 40 | − }{ | |
| 41 | − {case1Function, map[string][]string{"SecureTransport": {"true"}}, true}, | |
| 42 | − {case2Function, map[string][]string{"SecureTransport": {"false"}}, true}, | |
| 43 | − } | |
| 44 | − | |
| 45 | − for i, testCase := range testCases { | |
| 46 | − result := testCase.function.evaluate(testCase.values) | |
| 47 | − | |
| 48 | − if result != testCase.expectedResult { | |
| 49 | − t.Errorf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 50 | − } | |
| 51 | − } | |
| 52 | −} | |
| 53 | − | |
| 54 | −func TestBooleanFuncKey(t *testing.T) { | |
| 55 | − case1Function, err := newBooleanFunc(AWSSecureTransport, NewValueSet(NewBoolValue(true))) | |
| 56 | − if err != nil { | |
| 57 | − t.Fatalf("unexpected error. %v\n", err) | |
| 58 | − } | |
| 59 | − | |
| 60 | − testCases := []struct { | |
| 61 | − function Function | |
| 62 | − expectedResult Key | |
| 63 | − }{ | |
| 64 | − {case1Function, AWSSecureTransport}, | |
| 65 | − } | |
| 66 | − | |
| 67 | − for i, testCase := range testCases { | |
| 68 | − result := testCase.function.key() | |
| 69 | − | |
| 70 | − if result != testCase.expectedResult { | |
| 71 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 72 | − } | |
| 73 | − } | |
| 74 | −} | |
| 75 | − | |
| 76 | −func TestBooleanFuncToMap(t *testing.T) { | |
| 77 | − case1Function, err := newBooleanFunc(AWSSecureTransport, NewValueSet(NewBoolValue(true))) | |
| 78 | − if err != nil { | |
| 79 | − t.Fatalf("unexpected error. %v\n", err) | |
| 80 | − } | |
| 81 | − | |
| 82 | − case1Result := map[Key]ValueSet{ | |
| 83 | − AWSSecureTransport: NewValueSet(NewStringValue("true")), | |
| 84 | − } | |
| 85 | − | |
| 86 | − case2Function, err := newBooleanFunc(AWSSecureTransport, NewValueSet(NewBoolValue(false))) | |
| 87 | − if err != nil { | |
| 88 | − t.Fatalf("unexpected error. %v\n", err) | |
| 89 | − } | |
| 90 | − | |
| 91 | − case2Result := map[Key]ValueSet{ | |
| 92 | − AWSSecureTransport: NewValueSet(NewStringValue("false")), | |
| 93 | − } | |
| 94 | − | |
| 95 | − testCases := []struct { | |
| 96 | − f Function | |
| 97 | − expectedResult map[Key]ValueSet | |
| 98 | − }{ | |
| 99 | − {case1Function, case1Result}, | |
| 100 | − {case2Function, case2Result}, | |
| 101 | − } | |
| 102 | − | |
| 103 | − for i, testCase := range testCases { | |
| 104 | − result := testCase.f.toMap() | |
| 105 | − | |
| 106 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 107 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 108 | − } | |
| 109 | − } | |
| 110 | −} | |
| 111 | − | |
| 112 | −func TestNewBooleanFunc(t *testing.T) { | |
| 113 | − case1Function, err := newBooleanFunc(AWSSecureTransport, NewValueSet(NewBoolValue(true))) | |
| 114 | − if err != nil { | |
| 115 | − t.Fatalf("unexpected error. %v\n", err) | |
| 116 | − } | |
| 117 | − | |
| 118 | − case2Function, err := newBooleanFunc(AWSSecureTransport, NewValueSet(NewBoolValue(false))) | |
| 119 | − if err != nil { | |
| 120 | − t.Fatalf("unexpected error. %v\n", err) | |
| 121 | − } | |
| 122 | − | |
| 123 | − testCases := []struct { | |
| 124 | − key Key | |
| 125 | − values ValueSet | |
| 126 | − expectedResult Function | |
| 127 | − expectErr bool | |
| 128 | − }{ | |
| 129 | − {AWSSecureTransport, NewValueSet(NewBoolValue(true)), case1Function, false}, | |
| 130 | − {AWSSecureTransport, NewValueSet(NewStringValue("false")), case2Function, false}, | |
| 131 | − // Multiple values error. | |
| 132 | − {AWSSecureTransport, NewValueSet(NewStringValue("true"), NewStringValue("false")), nil, true}, | |
| 133 | − // Invalid boolean string error. | |
| 134 | − {AWSSecureTransport, NewValueSet(NewStringValue("foo")), nil, true}, | |
| 135 | − // Invalid value error. | |
| 136 | − {AWSSecureTransport, NewValueSet(NewIntValue(7)), nil, true}, | |
| 137 | − } | |
| 138 | − | |
| 139 | − for i, testCase := range testCases { | |
| 140 | − result, err := newBooleanFunc(testCase.key, testCase.values) | |
| 141 | − expectErr := (err != nil) | |
| 142 | − | |
| 143 | − if expectErr != testCase.expectErr { | |
| 144 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 145 | − } | |
| 146 | − | |
| 147 | − if !testCase.expectErr { | |
| 148 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 149 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 150 | − } | |
| 151 | − } | |
| 152 | − } | |
| 153 | −} | |
| @@ -1,165 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "reflect" | |
| 24 | − "time" | |
| 25 | −) | |
| 26 | − | |
| 27 | −func toDateEqualsFuncString(n name, key Key, value time.Time) string { | |
| 28 | − return fmt.Sprintf("%v:%v:%v", n, key, value.Format(time.RFC3339)) | |
| 29 | −} | |
| 30 | − | |
| 31 | −// dateEqualsFunc - String equals function. It checks whether value by Key in given | |
| 32 | −// values map is in condition values. | |
| 33 | −// For example, | |
| 34 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 35 | −// in value map for Key is in values. | |
| 36 | −type dateEqualsFunc struct { | |
| 37 | − k Key | |
| 38 | − value time.Time | |
| 39 | −} | |
| 40 | − | |
| 41 | −// evaluate() - evaluates to check whether value by Key in given values is in | |
| 42 | −// condition values. | |
| 43 | −func (f dateEqualsFunc) evaluate(values map[string][]string) bool { | |
| 44 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 45 | − if !ok { | |
| 46 | − requestValue = values[f.k.Name()] | |
| 47 | − } | |
| 48 | − | |
| 49 | − if len(requestValue) == 0 { | |
| 50 | − return false | |
| 51 | − } | |
| 52 | − | |
| 53 | − t, err := time.Parse(time.RFC3339, requestValue[0]) | |
| 54 | − if err != nil { | |
| 55 | − return false | |
| 56 | − } | |
| 57 | − | |
| 58 | − return f.value.Equal(t) | |
| 59 | −} | |
| 60 | − | |
| 61 | −// key() - returns condition key which is used by this condition function. | |
| 62 | −func (f dateEqualsFunc) key() Key { | |
| 63 | − return f.k | |
| 64 | −} | |
| 65 | − | |
| 66 | −// name() - returns "DateEquals" condition name. | |
| 67 | −func (f dateEqualsFunc) name() name { | |
| 68 | − return dateEquals | |
| 69 | −} | |
| 70 | − | |
| 71 | −func (f dateEqualsFunc) String() string { | |
| 72 | − return toDateEqualsFuncString(dateEquals, f.k, f.value) | |
| 73 | −} | |
| 74 | − | |
| 75 | −// toMap - returns map representation of this function. | |
| 76 | −func (f dateEqualsFunc) toMap() map[Key]ValueSet { | |
| 77 | − if !f.k.IsValid() { | |
| 78 | − return nil | |
| 79 | − } | |
| 80 | − | |
| 81 | − values := NewValueSet() | |
| 82 | − values.Add(NewStringValue(f.value.Format(time.RFC3339))) | |
| 83 | − | |
| 84 | − return map[Key]ValueSet{ | |
| 85 | − f.k: values, | |
| 86 | − } | |
| 87 | −} | |
| 88 | − | |
| 89 | −// dateNotEqualsFunc - String not equals function. It checks whether value by Key in | |
| 90 | −// given values is NOT in condition values. | |
| 91 | −// For example, | |
| 92 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 93 | −// in value map for Key is NOT in values. | |
| 94 | −type dateNotEqualsFunc struct { | |
| 95 | − dateEqualsFunc | |
| 96 | −} | |
| 97 | − | |
| 98 | −// evaluate() - evaluates to check whether value by Key in given values is NOT in | |
| 99 | −// condition values. | |
| 100 | −func (f dateNotEqualsFunc) evaluate(values map[string][]string) bool { | |
| 101 | − return !f.dateEqualsFunc.evaluate(values) | |
| 102 | −} | |
| 103 | − | |
| 104 | −// name() - returns "DateNotEquals" condition name. | |
| 105 | −func (f dateNotEqualsFunc) name() name { | |
| 106 | − return dateNotEquals | |
| 107 | −} | |
| 108 | − | |
| 109 | −func (f dateNotEqualsFunc) String() string { | |
| 110 | − return toDateEqualsFuncString(dateNotEquals, f.dateEqualsFunc.k, f.dateEqualsFunc.value) | |
| 111 | −} | |
| 112 | − | |
| 113 | −func valueToTime(n name, values ValueSet) (v time.Time, err error) { | |
| 114 | − if len(values) != 1 { | |
| 115 | − return v, fmt.Errorf("only one value is allowed for %s condition", n) | |
| 116 | − } | |
| 117 | − | |
| 118 | − for vs := range values { | |
| 119 | − switch vs.GetType() { | |
| 120 | − case reflect.String: | |
| 121 | − s, err := vs.GetString() | |
| 122 | − if err != nil { | |
| 123 | − return v, err | |
| 124 | − } | |
| 125 | − if v, err = time.Parse(time.RFC3339, s); err != nil { | |
| 126 | − return v, fmt.Errorf("value %s must be a time.Time string for %s condition: %w", vs, n, err) | |
| 127 | − } | |
| 128 | − default: | |
| 129 | − return v, fmt.Errorf("value %s must be a time.Time for %s condition", vs, n) | |
| 130 | − } | |
| 131 | − } | |
| 132 | − | |
| 133 | − return v, nil | |
| 134 | − | |
| 135 | −} | |
| 136 | − | |
| 137 | −// newDateEqualsFunc - returns new DateEquals function. | |
| 138 | −func newDateEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 139 | − v, err := valueToTime(dateEquals, values) | |
| 140 | − if err != nil { | |
| 141 | − return nil, err | |
| 142 | − } | |
| 143 | − | |
| 144 | − return NewDateEqualsFunc(key, v) | |
| 145 | −} | |
| 146 | − | |
| 147 | −// NewDateEqualsFunc - returns new DateEquals function. | |
| 148 | −func NewDateEqualsFunc(key Key, value time.Time) (Function, error) { | |
| 149 | − return &dateEqualsFunc{key, value}, nil | |
| 150 | −} | |
| 151 | − | |
| 152 | −// newDateNotEqualsFunc - returns new DateNotEquals function. | |
| 153 | −func newDateNotEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 154 | − v, err := valueToTime(dateNotEquals, values) | |
| 155 | − if err != nil { | |
| 156 | − return nil, err | |
| 157 | − } | |
| 158 | − | |
| 159 | − return NewDateNotEqualsFunc(key, v) | |
| 160 | −} | |
| 161 | − | |
| 162 | −// NewDateNotEqualsFunc - returns new DateNotEquals function. | |
| 163 | −func NewDateNotEqualsFunc(key Key, value time.Time) (Function, error) { | |
| 164 | − return &dateNotEqualsFunc{dateEqualsFunc{key, value}}, nil | |
| 165 | −} | |
| @@ -1,154 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "time" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func toDateGreaterThanFuncString(n name, key Key, value time.Time) string { | |
| 27 | − return fmt.Sprintf("%v:%v:%v", n, key, value.Format(time.RFC3339)) | |
| 28 | −} | |
| 29 | − | |
| 30 | −// dateGreaterThanFunc - String equals function. It checks whether value by Key in given | |
| 31 | −// values map is in condition values. | |
| 32 | −// For example, | |
| 33 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 34 | −// in value map for Key is in values. | |
| 35 | −type dateGreaterThanFunc struct { | |
| 36 | − k Key | |
| 37 | − value time.Time | |
| 38 | −} | |
| 39 | − | |
| 40 | −// evaluate() - evaluates to check whether value by Key in given values is in | |
| 41 | −// condition values. | |
| 42 | −func (f dateGreaterThanFunc) evaluate(values map[string][]string) bool { | |
| 43 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 44 | − if !ok { | |
| 45 | − requestValue = values[f.k.Name()] | |
| 46 | − } | |
| 47 | − | |
| 48 | − if len(requestValue) == 0 { | |
| 49 | − return false | |
| 50 | − } | |
| 51 | − | |
| 52 | − t, err := time.Parse(time.RFC3339, requestValue[0]) | |
| 53 | − if err != nil { | |
| 54 | − return false | |
| 55 | − } | |
| 56 | − | |
| 57 | − return t.After(f.value) | |
| 58 | −} | |
| 59 | − | |
| 60 | −// key() - returns condition key which is used by this condition function. | |
| 61 | −func (f dateGreaterThanFunc) key() Key { | |
| 62 | − return f.k | |
| 63 | −} | |
| 64 | − | |
| 65 | −// name() - returns "DateGreaterThan" condition name. | |
| 66 | −func (f dateGreaterThanFunc) name() name { | |
| 67 | − return dateGreaterThan | |
| 68 | −} | |
| 69 | − | |
| 70 | −func (f dateGreaterThanFunc) String() string { | |
| 71 | − return toDateGreaterThanFuncString(dateGreaterThan, f.k, f.value) | |
| 72 | −} | |
| 73 | − | |
| 74 | −// toMap - returns map representation of this function. | |
| 75 | −func (f dateGreaterThanFunc) toMap() map[Key]ValueSet { | |
| 76 | − if !f.k.IsValid() { | |
| 77 | − return nil | |
| 78 | − } | |
| 79 | − | |
| 80 | − values := NewValueSet() | |
| 81 | − values.Add(NewStringValue(f.value.Format(time.RFC3339))) | |
| 82 | − | |
| 83 | − return map[Key]ValueSet{ | |
| 84 | − f.k: values, | |
| 85 | − } | |
| 86 | −} | |
| 87 | − | |
| 88 | −// dateNotEqualsFunc - String not equals function. It checks whether value by Key in | |
| 89 | −// given values is NOT in condition values. | |
| 90 | −// For example, | |
| 91 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 92 | −// in value map for Key is NOT in values. | |
| 93 | −type dateGreaterThanEqualsFunc struct { | |
| 94 | − dateGreaterThanFunc | |
| 95 | −} | |
| 96 | − | |
| 97 | −// evaluate() - evaluates to check whether value by Key in given values is NOT in | |
| 98 | −// condition values. | |
| 99 | −func (f dateGreaterThanEqualsFunc) evaluate(values map[string][]string) bool { | |
| 100 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 101 | − if !ok { | |
| 102 | − requestValue = values[f.k.Name()] | |
| 103 | − } | |
| 104 | − | |
| 105 | − if len(requestValue) == 0 { | |
| 106 | − return false | |
| 107 | − } | |
| 108 | − | |
| 109 | − t, err := time.Parse(time.RFC3339, requestValue[0]) | |
| 110 | − if err != nil { | |
| 111 | − return false | |
| 112 | − } | |
| 113 | − | |
| 114 | − return t.After(f.value) || t.Equal(f.value) | |
| 115 | −} | |
| 116 | − | |
| 117 | −// name() - returns "DateNotEquals" condition name. | |
| 118 | −func (f dateGreaterThanEqualsFunc) name() name { | |
| 119 | − return dateGreaterThanEquals | |
| 120 | −} | |
| 121 | − | |
| 122 | −func (f dateGreaterThanEqualsFunc) String() string { | |
| 123 | − return toDateGreaterThanFuncString(dateNotEquals, f.dateGreaterThanFunc.k, f.dateGreaterThanFunc.value) | |
| 124 | −} | |
| 125 | − | |
| 126 | −// newDateGreaterThanFunc - returns new DateGreaterThan function. | |
| 127 | −func newDateGreaterThanFunc(key Key, values ValueSet) (Function, error) { | |
| 128 | − v, err := valueToTime(dateGreaterThan, values) | |
| 129 | − if err != nil { | |
| 130 | − return nil, err | |
| 131 | − } | |
| 132 | − | |
| 133 | − return NewDateGreaterThanFunc(key, v) | |
| 134 | −} | |
| 135 | − | |
| 136 | −// NewDateGreaterThanFunc - returns new DateGreaterThan function. | |
| 137 | −func NewDateGreaterThanFunc(key Key, value time.Time) (Function, error) { | |
| 138 | − return &dateGreaterThanFunc{key, value}, nil | |
| 139 | −} | |
| 140 | − | |
| 141 | −// newDateNotEqualsFunc - returns new DateNotEquals function. | |
| 142 | −func newDateGreaterThanEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 143 | − v, err := valueToTime(dateNotEquals, values) | |
| 144 | − if err != nil { | |
| 145 | − return nil, err | |
| 146 | − } | |
| 147 | − | |
| 148 | − return NewDateGreaterThanEqualsFunc(key, v) | |
| 149 | −} | |
| 150 | − | |
| 151 | −// NewDateGreaterThanEqualsFunc - returns new DateNotEquals function. | |
| 152 | −func NewDateGreaterThanEqualsFunc(key Key, value time.Time) (Function, error) { | |
| 153 | − return &dateGreaterThanEqualsFunc{dateGreaterThanFunc{key, value}}, nil | |
| 154 | −} | |
| @@ -1,154 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "time" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func toDateLessThanFuncString(n name, key Key, value time.Time) string { | |
| 27 | − return fmt.Sprintf("%v:%v:%v", n, key, value.Format(time.RFC3339)) | |
| 28 | −} | |
| 29 | − | |
| 30 | −// dateLessThanFunc - String equals function. It checks whether value by Key in given | |
| 31 | −// values map is in condition values. | |
| 32 | −// For example, | |
| 33 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 34 | −// in value map for Key is in values. | |
| 35 | −type dateLessThanFunc struct { | |
| 36 | − k Key | |
| 37 | − value time.Time | |
| 38 | −} | |
| 39 | − | |
| 40 | −// evaluate() - evaluates to check whether value by Key in given values is in | |
| 41 | −// condition values. | |
| 42 | −func (f dateLessThanFunc) evaluate(values map[string][]string) bool { | |
| 43 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 44 | − if !ok { | |
| 45 | − requestValue = values[f.k.Name()] | |
| 46 | − } | |
| 47 | − | |
| 48 | − if len(requestValue) == 0 { | |
| 49 | − return false | |
| 50 | − } | |
| 51 | − | |
| 52 | − t, err := time.Parse(time.RFC3339, requestValue[0]) | |
| 53 | − if err != nil { | |
| 54 | − return false | |
| 55 | − } | |
| 56 | − | |
| 57 | − return t.Before(f.value) | |
| 58 | −} | |
| 59 | − | |
| 60 | −// key() - returns condition key which is used by this condition function. | |
| 61 | −func (f dateLessThanFunc) key() Key { | |
| 62 | − return f.k | |
| 63 | −} | |
| 64 | − | |
| 65 | −// name() - returns "DateLessThan" condition name. | |
| 66 | −func (f dateLessThanFunc) name() name { | |
| 67 | − return dateLessThan | |
| 68 | −} | |
| 69 | − | |
| 70 | −func (f dateLessThanFunc) String() string { | |
| 71 | − return toDateLessThanFuncString(dateLessThan, f.k, f.value) | |
| 72 | −} | |
| 73 | − | |
| 74 | −// toMap - returns map representation of this function. | |
| 75 | −func (f dateLessThanFunc) toMap() map[Key]ValueSet { | |
| 76 | − if !f.k.IsValid() { | |
| 77 | − return nil | |
| 78 | − } | |
| 79 | − | |
| 80 | − values := NewValueSet() | |
| 81 | − values.Add(NewStringValue(f.value.Format(time.RFC3339))) | |
| 82 | − | |
| 83 | − return map[Key]ValueSet{ | |
| 84 | − f.k: values, | |
| 85 | − } | |
| 86 | −} | |
| 87 | − | |
| 88 | −// dateNotEqualsFunc - String not equals function. It checks whether value by Key in | |
| 89 | −// given values is NOT in condition values. | |
| 90 | −// For example, | |
| 91 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 92 | −// in value map for Key is NOT in values. | |
| 93 | −type dateLessThanEqualsFunc struct { | |
| 94 | − dateLessThanFunc | |
| 95 | −} | |
| 96 | − | |
| 97 | −// evaluate() - evaluates to check whether value by Key in given values is NOT in | |
| 98 | −// condition values. | |
| 99 | −func (f dateLessThanEqualsFunc) evaluate(values map[string][]string) bool { | |
| 100 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 101 | − if !ok { | |
| 102 | − requestValue = values[f.k.Name()] | |
| 103 | − } | |
| 104 | − | |
| 105 | − if len(requestValue) == 0 { | |
| 106 | − return false | |
| 107 | − } | |
| 108 | − | |
| 109 | − t, err := time.Parse(time.RFC3339, requestValue[0]) | |
| 110 | − if err != nil { | |
| 111 | − return false | |
| 112 | − } | |
| 113 | − | |
| 114 | − return t.Before(f.value) || t.Equal(f.value) | |
| 115 | −} | |
| 116 | − | |
| 117 | −// name() - returns "DateNotEquals" condition name. | |
| 118 | −func (f dateLessThanEqualsFunc) name() name { | |
| 119 | − return dateLessThanEquals | |
| 120 | −} | |
| 121 | − | |
| 122 | −func (f dateLessThanEqualsFunc) String() string { | |
| 123 | − return toDateLessThanFuncString(dateNotEquals, f.dateLessThanFunc.k, f.dateLessThanFunc.value) | |
| 124 | −} | |
| 125 | − | |
| 126 | −// newDateLessThanFunc - returns new DateLessThan function. | |
| 127 | −func newDateLessThanFunc(key Key, values ValueSet) (Function, error) { | |
| 128 | − v, err := valueToTime(dateLessThan, values) | |
| 129 | − if err != nil { | |
| 130 | − return nil, err | |
| 131 | − } | |
| 132 | − | |
| 133 | − return NewDateLessThanFunc(key, v) | |
| 134 | −} | |
| 135 | − | |
| 136 | −// NewDateLessThanFunc - returns new DateLessThan function. | |
| 137 | −func NewDateLessThanFunc(key Key, value time.Time) (Function, error) { | |
| 138 | − return &dateLessThanFunc{key, value}, nil | |
| 139 | −} | |
| 140 | − | |
| 141 | −// newDateNotEqualsFunc - returns new DateNotEquals function. | |
| 142 | −func newDateLessThanEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 143 | − v, err := valueToTime(dateNotEquals, values) | |
| 144 | − if err != nil { | |
| 145 | − return nil, err | |
| 146 | − } | |
| 147 | − | |
| 148 | − return NewDateLessThanEqualsFunc(key, v) | |
| 149 | −} | |
| 150 | − | |
| 151 | −// NewDateLessThanEqualsFunc - returns new DateNotEquals function. | |
| 152 | −func NewDateLessThanEqualsFunc(key Key, value time.Time) (Function, error) { | |
| 153 | − return &dateLessThanEqualsFunc{dateLessThanFunc{key, value}}, nil | |
| 154 | −} | |
| @@ -1,224 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | − "sort" | |
| 24 | −) | |
| 25 | − | |
| 26 | −// Function - condition function interface. | |
| 27 | −type Function interface { | |
| 28 | − // evaluate() - evaluates this condition function with given values. | |
| 29 | − evaluate(values map[string][]string) bool | |
| 30 | − | |
| 31 | − // key() - returns condition key used in this function. | |
| 32 | − key() Key | |
| 33 | − | |
| 34 | − // name() - returns condition name of this function. | |
| 35 | − name() name | |
| 36 | − | |
| 37 | − // String() - returns string representation of function. | |
| 38 | − String() string | |
| 39 | − | |
| 40 | − // toMap - returns map representation of this function. | |
| 41 | − toMap() map[Key]ValueSet | |
| 42 | −} | |
| 43 | − | |
| 44 | −// Functions - list of functions. | |
| 45 | −type Functions []Function | |
| 46 | − | |
| 47 | −// Evaluate - evaluates all functions with given values map. Each function is evaluated | |
| 48 | −// sequencely and next function is called only if current function succeeds. | |
| 49 | −func (functions Functions) Evaluate(values map[string][]string) bool { | |
| 50 | − for _, f := range functions { | |
| 51 | − if !f.evaluate(values) { | |
| 52 | − return false | |
| 53 | − } | |
| 54 | − } | |
| 55 | − | |
| 56 | − return true | |
| 57 | −} | |
| 58 | − | |
| 59 | −// Keys - returns list of keys used in all functions. | |
| 60 | −func (functions Functions) Keys() KeySet { | |
| 61 | − keySet := NewKeySet() | |
| 62 | − | |
| 63 | − for _, f := range functions { | |
| 64 | − keySet.Add(f.key()) | |
| 65 | − } | |
| 66 | − | |
| 67 | − return keySet | |
| 68 | −} | |
| 69 | − | |
| 70 | −// Clone clones Functions structure | |
| 71 | −func (functions Functions) Clone() Functions { | |
| 72 | − funcs := []Function{} | |
| 73 | − | |
| 74 | − for _, f := range functions { | |
| 75 | − vfn := conditionFuncMap[f.name()] | |
| 76 | − for key, values := range f.toMap() { | |
| 77 | − function, _ := vfn(key, values.Clone()) | |
| 78 | − funcs = append(funcs, function) | |
| 79 | − } | |
| 80 | − } | |
| 81 | − | |
| 82 | − return funcs | |
| 83 | −} | |
| 84 | − | |
| 85 | −// Equals returns true if two Functions structures are equal | |
| 86 | −func (functions Functions) Equals(funcs Functions) bool { | |
| 87 | − if len(functions) != len(funcs) { | |
| 88 | − return false | |
| 89 | − } | |
| 90 | − for _, fi := range functions { | |
| 91 | − fistr := fi.String() | |
| 92 | − found := false | |
| 93 | − for _, fj := range funcs { | |
| 94 | − if fistr == fj.String() { | |
| 95 | − found = true | |
| 96 | − break | |
| 97 | − } | |
| 98 | − } | |
| 99 | − if !found { | |
| 100 | − return false | |
| 101 | − } | |
| 102 | − } | |
| 103 | − return true | |
| 104 | −} | |
| 105 | − | |
| 106 | −// MarshalJSON - encodes Functions to JSON data. | |
| 107 | −func (functions Functions) MarshalJSON() ([]byte, error) { | |
| 108 | − nm := make(map[name]map[Key]ValueSet) | |
| 109 | − | |
| 110 | − for _, f := range functions { | |
| 111 | − if _, ok := nm[f.name()]; ok { | |
| 112 | − for k, v := range f.toMap() { | |
| 113 | − nm[f.name()][k] = v | |
| 114 | − } | |
| 115 | − } else { | |
| 116 | − nm[f.name()] = f.toMap() | |
| 117 | − } | |
| 118 | − } | |
| 119 | − | |
| 120 | − return json.Marshal(nm) | |
| 121 | −} | |
| 122 | − | |
| 123 | −func (functions Functions) String() string { | |
| 124 | − funcStrings := []string{} | |
| 125 | − for _, f := range functions { | |
| 126 | − s := fmt.Sprintf("%v", f) | |
| 127 | − funcStrings = append(funcStrings, s) | |
| 128 | − } | |
| 129 | − sort.Strings(funcStrings) | |
| 130 | − | |
| 131 | − return fmt.Sprintf("%v", funcStrings) | |
| 132 | −} | |
| 133 | − | |
| 134 | −var conditionFuncMap = map[name]func(Key, ValueSet) (Function, error){ | |
| 135 | − stringEquals: newStringEqualsFunc, | |
| 136 | − stringNotEquals: newStringNotEqualsFunc, | |
| 137 | − stringEqualsIgnoreCase: newStringEqualsIgnoreCaseFunc, | |
| 138 | − stringNotEqualsIgnoreCase: newStringNotEqualsIgnoreCaseFunc, | |
| 139 | − binaryEquals: newBinaryEqualsFunc, | |
| 140 | − stringLike: newStringLikeFunc, | |
| 141 | − stringNotLike: newStringNotLikeFunc, | |
| 142 | − ipAddress: newIPAddressFunc, | |
| 143 | − notIPAddress: newNotIPAddressFunc, | |
| 144 | − null: newNullFunc, | |
| 145 | − boolean: newBooleanFunc, | |
| 146 | − numericEquals: newNumericEqualsFunc, | |
| 147 | − numericNotEquals: newNumericNotEqualsFunc, | |
| 148 | − numericLessThan: newNumericLessThanFunc, | |
| 149 | − numericLessThanEquals: newNumericLessThanEqualsFunc, | |
| 150 | − numericGreaterThan: newNumericGreaterThanFunc, | |
| 151 | − numericGreaterThanEquals: newNumericGreaterThanEqualsFunc, | |
| 152 | − dateEquals: newDateEqualsFunc, | |
| 153 | − dateNotEquals: newDateNotEqualsFunc, | |
| 154 | − dateLessThan: newDateLessThanFunc, | |
| 155 | − dateLessThanEquals: newDateLessThanEqualsFunc, | |
| 156 | − dateGreaterThan: newDateGreaterThanFunc, | |
| 157 | − dateGreaterThanEquals: newDateGreaterThanEqualsFunc, | |
| 158 | − // Add new conditions here. | |
| 159 | −} | |
| 160 | − | |
| 161 | −// UnmarshalJSON - decodes JSON data to Functions. | |
| 162 | −func (functions *Functions) UnmarshalJSON(data []byte) error { | |
| 163 | − // As string kind, int kind then json.Unmarshaler is checked at | |
| 164 | − // https://github.com/golang/go/blob/master/src/encoding/json/decode.go#L618 | |
| 165 | − // UnmarshalJSON() is not called for types extending string | |
| 166 | − // see https://play.golang.org/p/HrSsKksHvrS, better way to do is | |
| 167 | − // https://play.golang.org/p/y9ElWpBgVAB | |
| 168 | − // | |
| 169 | − // Due to this issue, name and Key types cannot be used as map keys below. | |
| 170 | − nm := make(map[string]map[string]ValueSet) | |
| 171 | − if err := json.Unmarshal(data, &nm); err != nil { | |
| 172 | − return err | |
| 173 | − } | |
| 174 | − | |
| 175 | − if len(nm) == 0 { | |
| 176 | − return fmt.Errorf("condition must not be empty") | |
| 177 | − } | |
| 178 | − | |
| 179 | − funcs := []Function{} | |
| 180 | − for nameString, args := range nm { | |
| 181 | − n, err := parseName(nameString) | |
| 182 | − if err != nil { | |
| 183 | − return err | |
| 184 | − } | |
| 185 | − | |
| 186 | − for keyString, values := range args { | |
| 187 | − key, err := parseKey(keyString) | |
| 188 | − if err != nil { | |
| 189 | − return err | |
| 190 | − } | |
| 191 | − | |
| 192 | − vfn, ok := conditionFuncMap[n] | |
| 193 | − if !ok { | |
| 194 | − return fmt.Errorf("condition %v is not handled", n) | |
| 195 | − } | |
| 196 | − | |
| 197 | − f, err := vfn(key, values) | |
| 198 | − if err != nil { | |
| 199 | − return err | |
| 200 | − } | |
| 201 | − | |
| 202 | − funcs = append(funcs, f) | |
| 203 | − } | |
| 204 | − } | |
| 205 | − | |
| 206 | − *functions = funcs | |
| 207 | − | |
| 208 | − return nil | |
| 209 | −} | |
| 210 | − | |
| 211 | −// GobEncode - encodes Functions to gob data. | |
| 212 | −func (functions Functions) GobEncode() ([]byte, error) { | |
| 213 | − return functions.MarshalJSON() | |
| 214 | −} | |
| 215 | − | |
| 216 | −// GobDecode - decodes gob data to Functions. | |
| 217 | −func (functions *Functions) GobDecode(data []byte) error { | |
| 218 | − return functions.UnmarshalJSON(data) | |
| 219 | −} | |
| 220 | − | |
| 221 | −// NewFunctions - returns new Functions with given function list. | |
| 222 | −func NewFunctions(functions ...Function) Functions { | |
| 223 | − return Functions(functions) | |
| 224 | −} | |
| @@ -1,354 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestFunctionsEvaluate(t *testing.T) { | |
| 27 | − func1, err := newNullFunc(S3XAmzCopySource, NewValueSet(NewBoolValue(true))) | |
| 28 | − if err != nil { | |
| 29 | − t.Fatalf("unexpected error. %v\n", err) | |
| 30 | − } | |
| 31 | − | |
| 32 | − func2, err := newIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 33 | − if err != nil { | |
| 34 | − t.Fatalf("unexpected error. %v\n", err) | |
| 35 | − } | |
| 36 | − | |
| 37 | − func3, err := newStringEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 38 | − if err != nil { | |
| 39 | − t.Fatalf("unexpected error. %v\n", err) | |
| 40 | − } | |
| 41 | − | |
| 42 | − func4, err := newStringLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject*"))) | |
| 43 | − if err != nil { | |
| 44 | − t.Fatalf("unexpected error. %v\n", err) | |
| 45 | − } | |
| 46 | − | |
| 47 | − case1Function := NewFunctions(func1, func2, func3, func4) | |
| 48 | − | |
| 49 | − testCases := []struct { | |
| 50 | − functions Functions | |
| 51 | − values map[string][]string | |
| 52 | − expectedResult bool | |
| 53 | − }{ | |
| 54 | − {case1Function, map[string][]string{ | |
| 55 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 56 | − "SourceIp": {"192.168.1.10"}, | |
| 57 | − }, false}, | |
| 58 | − {case1Function, map[string][]string{ | |
| 59 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 60 | − "SourceIp": {"192.168.1.10"}, | |
| 61 | − "Refer": {"http://example.org/"}, | |
| 62 | − }, false}, | |
| 63 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, false}, | |
| 64 | − {case1Function, map[string][]string{"SourceIp": {"192.168.1.10"}}, false}, | |
| 65 | − {case1Function, map[string][]string{ | |
| 66 | − "x-amz-copy-source": {"mybucket/yourobject"}, | |
| 67 | − "SourceIp": {"192.168.1.10"}, | |
| 68 | − }, false}, | |
| 69 | − {case1Function, map[string][]string{ | |
| 70 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 71 | − "SourceIp": {"192.168.2.10"}, | |
| 72 | − }, false}, | |
| 73 | − {case1Function, map[string][]string{ | |
| 74 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 75 | − "Refer": {"http://example.org/"}, | |
| 76 | − }, false}, | |
| 77 | − } | |
| 78 | − | |
| 79 | − for i, testCase := range testCases { | |
| 80 | − result := testCase.functions.Evaluate(testCase.values) | |
| 81 | − | |
| 82 | − if result != testCase.expectedResult { | |
| 83 | − t.Errorf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 84 | − } | |
| 85 | − } | |
| 86 | −} | |
| 87 | − | |
| 88 | −func TestFunctionsKeys(t *testing.T) { | |
| 89 | − func1, err := newNullFunc(S3XAmzCopySource, NewValueSet(NewBoolValue(true))) | |
| 90 | − if err != nil { | |
| 91 | − t.Fatalf("unexpected error. %v\n", err) | |
| 92 | − } | |
| 93 | − | |
| 94 | − func2, err := newIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 95 | − if err != nil { | |
| 96 | − t.Fatalf("unexpected error. %v\n", err) | |
| 97 | − } | |
| 98 | − | |
| 99 | − func3, err := newStringEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 100 | − if err != nil { | |
| 101 | − t.Fatalf("unexpected error. %v\n", err) | |
| 102 | − } | |
| 103 | − | |
| 104 | − func4, err := newStringLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject*"))) | |
| 105 | − if err != nil { | |
| 106 | − t.Fatalf("unexpected error. %v\n", err) | |
| 107 | − } | |
| 108 | − | |
| 109 | − testCases := []struct { | |
| 110 | − functions Functions | |
| 111 | − expectedResult KeySet | |
| 112 | − }{ | |
| 113 | − {NewFunctions(func1, func2, func3, func4), NewKeySet(S3XAmzCopySource, AWSSourceIP)}, | |
| 114 | − } | |
| 115 | − | |
| 116 | − for i, testCase := range testCases { | |
| 117 | − result := testCase.functions.Keys() | |
| 118 | − | |
| 119 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 120 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 121 | − } | |
| 122 | − } | |
| 123 | −} | |
| 124 | − | |
| 125 | −func TestFunctionsMarshalJSON(t *testing.T) { | |
| 126 | − func1, err := newStringLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*"))) | |
| 127 | − if err != nil { | |
| 128 | − t.Fatalf("unexpected error. %v\n", err) | |
| 129 | − } | |
| 130 | − | |
| 131 | − func2, err := newStringEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 132 | − if err != nil { | |
| 133 | − t.Fatalf("unexpected error. %v\n", err) | |
| 134 | − } | |
| 135 | − | |
| 136 | − func3, err := newStringNotEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 137 | − if err != nil { | |
| 138 | − t.Fatalf("unexpected error. %v\n", err) | |
| 139 | − } | |
| 140 | − | |
| 141 | − func4, err := newNotIPAddressFunc(AWSSourceIP, | |
| 142 | − NewValueSet(NewStringValue("10.1.10.0/24"))) | |
| 143 | − if err != nil { | |
| 144 | − t.Fatalf("unexpected error. %v\n", err) | |
| 145 | − } | |
| 146 | − | |
| 147 | − func5, err := newStringNotLikeFunc(S3XAmzStorageClass, NewValueSet(NewStringValue("STANDARD"))) | |
| 148 | − if err != nil { | |
| 149 | − t.Fatalf("unexpected error. %v\n", err) | |
| 150 | − } | |
| 151 | − | |
| 152 | − func6, err := newNullFunc(S3XAmzServerSideEncryptionCustomerAlgorithm, NewValueSet(NewBoolValue(true))) | |
| 153 | − if err != nil { | |
| 154 | − t.Fatalf("unexpected error. %v\n", err) | |
| 155 | − } | |
| 156 | − | |
| 157 | − func7, err := newIPAddressFunc(AWSSourceIP, | |
| 158 | − NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 159 | − if err != nil { | |
| 160 | − t.Fatalf("unexpected error. %v\n", err) | |
| 161 | − } | |
| 162 | − | |
| 163 | − case1Result := []byte(`{"IpAddress":{"aws:SourceIp":["192.168.1.0/24"]},"NotIpAddress":{"aws:SourceIp":["10.1.10.0/24"]},"Null":{"s3:x-amz-server-side-encryption-customer-algorithm":[true]},"StringEquals":{"s3:x-amz-copy-source":["mybucket/myobject"]},"StringLike":{"s3:x-amz-metadata-directive":["REPL*"]},"StringNotEquals":{"s3:x-amz-server-side-encryption":["AES256"]},"StringNotLike":{"s3:x-amz-storage-class":["STANDARD"]}}`) | |
| 164 | − | |
| 165 | − case2Result := []byte(`{"Null":{"s3:x-amz-server-side-encryption-customer-algorithm":[true]}}`) | |
| 166 | − | |
| 167 | − testCases := []struct { | |
| 168 | − functions Functions | |
| 169 | − expectedResult []byte | |
| 170 | − expectErr bool | |
| 171 | − }{ | |
| 172 | − {NewFunctions(func1, func2, func3, func4, func5, func6, func7), case1Result, false}, | |
| 173 | − {NewFunctions(func6), case2Result, false}, | |
| 174 | − {NewFunctions(), []byte(`{}`), false}, | |
| 175 | − {nil, []byte(`{}`), false}, | |
| 176 | − } | |
| 177 | − | |
| 178 | − for i, testCase := range testCases { | |
| 179 | − result, err := json.Marshal(testCase.functions) | |
| 180 | − expectErr := (err != nil) | |
| 181 | − | |
| 182 | − if testCase.expectErr != expectErr { | |
| 183 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 184 | − } | |
| 185 | − | |
| 186 | − if !testCase.expectErr { | |
| 187 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 188 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, string(testCase.expectedResult), string(result)) | |
| 189 | − } | |
| 190 | − } | |
| 191 | − } | |
| 192 | −} | |
| 193 | − | |
| 194 | −func TestFunctionsUnmarshalJSON(t *testing.T) { | |
| 195 | − case1Data := []byte(`{ | |
| 196 | − "StringLike": { | |
| 197 | − "s3:x-amz-metadata-directive": "REPL*" | |
| 198 | − }, | |
| 199 | − "StringEquals": { | |
| 200 | − "s3:x-amz-copy-source": "mybucket/myobject" | |
| 201 | − }, | |
| 202 | − "StringNotEquals": { | |
| 203 | − "s3:x-amz-server-side-encryption": "AES256" | |
| 204 | − }, | |
| 205 | − "NotIpAddress": { | |
| 206 | − "aws:SourceIp": [ | |
| 207 | − "10.1.10.0/24", | |
| 208 | − "10.10.1.0/24" | |
| 209 | − ] | |
| 210 | − }, | |
| 211 | − "StringNotLike": { | |
| 212 | − "s3:x-amz-storage-class": "STANDARD" | |
| 213 | − }, | |
| 214 | − "Null": { | |
| 215 | − "s3:x-amz-server-side-encryption-customer-algorithm": true | |
| 216 | − }, | |
| 217 | − "IpAddress": { | |
| 218 | − "aws:SourceIp": [ | |
| 219 | − "192.168.1.0/24", | |
| 220 | − "192.168.2.0/24" | |
| 221 | − ] | |
| 222 | − } | |
| 223 | −}`) | |
| 224 | − func1, err := newStringLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*"))) | |
| 225 | − if err != nil { | |
| 226 | − t.Fatalf("unexpected error. %v\n", err) | |
| 227 | − } | |
| 228 | − | |
| 229 | − func2, err := newStringEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 230 | − if err != nil { | |
| 231 | − t.Fatalf("unexpected error. %v\n", err) | |
| 232 | − } | |
| 233 | − | |
| 234 | − func3, err := newStringNotEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 235 | − if err != nil { | |
| 236 | − t.Fatalf("unexpected error. %v\n", err) | |
| 237 | − } | |
| 238 | − | |
| 239 | − func4, err := newNotIPAddressFunc(AWSSourceIP, | |
| 240 | − NewValueSet(NewStringValue("10.1.10.0/24"), NewStringValue("10.10.1.0/24"))) | |
| 241 | − if err != nil { | |
| 242 | − t.Fatalf("unexpected error. %v\n", err) | |
| 243 | − } | |
| 244 | − | |
| 245 | − func5, err := newStringNotLikeFunc(S3XAmzStorageClass, NewValueSet(NewStringValue("STANDARD"))) | |
| 246 | − if err != nil { | |
| 247 | − t.Fatalf("unexpected error. %v\n", err) | |
| 248 | − } | |
| 249 | − | |
| 250 | − func6, err := newNullFunc(S3XAmzServerSideEncryptionCustomerAlgorithm, NewValueSet(NewBoolValue(true))) | |
| 251 | − if err != nil { | |
| 252 | − t.Fatalf("unexpected error. %v\n", err) | |
| 253 | − } | |
| 254 | − | |
| 255 | − func7, err := newIPAddressFunc(AWSSourceIP, | |
| 256 | − NewValueSet(NewStringValue("192.168.1.0/24"), NewStringValue("192.168.2.0/24"))) | |
| 257 | − if err != nil { | |
| 258 | − t.Fatalf("unexpected error. %v\n", err) | |
| 259 | − } | |
| 260 | − | |
| 261 | − case2Data := []byte(`{ | |
| 262 | − "Null": { | |
| 263 | − "s3:x-amz-server-side-encryption-customer-algorithm": true | |
| 264 | − }, | |
| 265 | − "Null": { | |
| 266 | − "s3:x-amz-server-side-encryption-customer-algorithm": "true" | |
| 267 | − } | |
| 268 | −}`) | |
| 269 | − | |
| 270 | − case3Data := []byte(`{}`) | |
| 271 | − | |
| 272 | − case4Data := []byte(`{ | |
| 273 | − "StringLike": { | |
| 274 | − "s3:x-amz-metadata-directive": "REPL*" | |
| 275 | − }, | |
| 276 | − "StringEquals": { | |
| 277 | − "s3:x-amz-copy-source": "mybucket/myobject", | |
| 278 | − "s3:prefix": [ | |
| 279 | − "", | |
| 280 | − "home/" | |
| 281 | − ], | |
| 282 | − "s3:delimiter": [ | |
| 283 | − "/" | |
| 284 | − ] | |
| 285 | − }, | |
| 286 | − "StringNotEquals": { | |
| 287 | − "s3:x-amz-server-side-encryption": "AES256" | |
| 288 | − }, | |
| 289 | − "NotIpAddress": { | |
| 290 | − "aws:SourceIp": [ | |
| 291 | − "10.1.10.0/24", | |
| 292 | − "10.10.1.0/24" | |
| 293 | − ] | |
| 294 | − }, | |
| 295 | − "StringNotLike": { | |
| 296 | − "s3:x-amz-storage-class": "STANDARD" | |
| 297 | − }, | |
| 298 | − "Null": { | |
| 299 | − "s3:x-amz-server-side-encryption-customer-algorithm": true | |
| 300 | − }, | |
| 301 | − "IpAddress": { | |
| 302 | − "aws:SourceIp": [ | |
| 303 | − "192.168.1.0/24", | |
| 304 | − "192.168.2.0/24" | |
| 305 | − ] | |
| 306 | − } | |
| 307 | −}`) | |
| 308 | − | |
| 309 | − func2_1, err := newStringEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 310 | − if err != nil { | |
| 311 | − t.Fatalf("unexpected error. %v\n", err) | |
| 312 | − } | |
| 313 | − | |
| 314 | − func2_2, err := newStringEqualsFunc(S3Prefix, NewValueSet(NewStringValue(""), NewStringValue("home/"))) | |
| 315 | − if err != nil { | |
| 316 | − t.Fatalf("unexpected error. %v\n", err) | |
| 317 | − } | |
| 318 | − | |
| 319 | − func2_3, err := newStringEqualsFunc(S3Delimiter, NewValueSet(NewStringValue("/"))) | |
| 320 | − if err != nil { | |
| 321 | − t.Fatalf("unexpected error. %v\n", err) | |
| 322 | − } | |
| 323 | − | |
| 324 | − testCases := []struct { | |
| 325 | − data []byte | |
| 326 | − expectedResult Functions | |
| 327 | − expectErr bool | |
| 328 | − }{ | |
| 329 | − // Success case, basic conditions. | |
| 330 | − {case1Data, NewFunctions(func1, func2, func3, func4, func5, func6, func7), false}, | |
| 331 | − // Duplicate conditions, success case only one value is preserved. | |
| 332 | − {case2Data, NewFunctions(func6), false}, | |
| 333 | − // empty condition error. | |
| 334 | − {case3Data, nil, true}, | |
| 335 | − // Success case multiple keys, same condition. | |
| 336 | − {case4Data, NewFunctions(func1, func2_1, func2_2, func2_3, func3, func4, func5, func6, func7), false}, | |
| 337 | − } | |
| 338 | − | |
| 339 | − for i, testCase := range testCases { | |
| 340 | − result := new(Functions) | |
| 341 | − err := json.Unmarshal(testCase.data, result) | |
| 342 | − expectErr := (err != nil) | |
| 343 | − | |
| 344 | − if testCase.expectErr != expectErr { | |
| 345 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 346 | − } | |
| 347 | − | |
| 348 | − if !testCase.expectErr { | |
| 349 | − if (*result).String() != testCase.expectedResult.String() { | |
| 350 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, *result) | |
| 351 | − } | |
| 352 | − } | |
| 353 | − } | |
| 354 | −} | |
| @@ -1,187 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net" | |
| 23 | − "net/http" | |
| 24 | − "sort" | |
| 25 | −) | |
| 26 | − | |
| 27 | −func toIPAddressFuncString(n name, key Key, values []*net.IPNet) string { | |
| 28 | − valueStrings := []string{} | |
| 29 | − for _, value := range values { | |
| 30 | − valueStrings = append(valueStrings, value.String()) | |
| 31 | − } | |
| 32 | − sort.Strings(valueStrings) | |
| 33 | − | |
| 34 | − return fmt.Sprintf("%v:%v:%v", n, key, valueStrings) | |
| 35 | −} | |
| 36 | − | |
| 37 | −// ipAddressFunc - IP address function. It checks whether value by Key in given | |
| 38 | −// values is in IP network. Here Key must be AWSSourceIP. | |
| 39 | −// For example, | |
| 40 | −// - if values = [192.168.1.0/24], at evaluate() it returns whether IP address | |
| 41 | −// in value map for AWSSourceIP falls in the network 192.168.1.10/24. | |
| 42 | −type ipAddressFunc struct { | |
| 43 | − k Key | |
| 44 | − values []*net.IPNet | |
| 45 | −} | |
| 46 | − | |
| 47 | −// evaluate() - evaluates to check whether IP address in values map for AWSSourceIP | |
| 48 | −// falls in one of network or not. | |
| 49 | −func (f ipAddressFunc) evaluate(values map[string][]string) bool { | |
| 50 | − IPs := []net.IP{} | |
| 51 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 52 | − if !ok { | |
| 53 | − requestValue = values[f.k.Name()] | |
| 54 | − } | |
| 55 | − | |
| 56 | − for _, s := range requestValue { | |
| 57 | − IP := net.ParseIP(s) | |
| 58 | − if IP == nil { | |
| 59 | − panic(fmt.Errorf("invalid IP address '%v'", s)) | |
| 60 | − } | |
| 61 | − | |
| 62 | − IPs = append(IPs, IP) | |
| 63 | − } | |
| 64 | − | |
| 65 | − for _, IP := range IPs { | |
| 66 | − for _, IPNet := range f.values { | |
| 67 | − if IPNet.Contains(IP) { | |
| 68 | − return true | |
| 69 | − } | |
| 70 | − } | |
| 71 | − } | |
| 72 | − | |
| 73 | − return false | |
| 74 | −} | |
| 75 | − | |
| 76 | −// key() - returns condition key which is used by this condition function. | |
| 77 | −// Key is always AWSSourceIP. | |
| 78 | −func (f ipAddressFunc) key() Key { | |
| 79 | − return f.k | |
| 80 | −} | |
| 81 | − | |
| 82 | −// name() - returns "IpAddress" condition name. | |
| 83 | −func (f ipAddressFunc) name() name { | |
| 84 | − return ipAddress | |
| 85 | −} | |
| 86 | − | |
| 87 | −func (f ipAddressFunc) String() string { | |
| 88 | − return toIPAddressFuncString(ipAddress, f.k, f.values) | |
| 89 | −} | |
| 90 | − | |
| 91 | −// toMap - returns map representation of this function. | |
| 92 | −func (f ipAddressFunc) toMap() map[Key]ValueSet { | |
| 93 | − if !f.k.IsValid() { | |
| 94 | − return nil | |
| 95 | − } | |
| 96 | − | |
| 97 | − values := NewValueSet() | |
| 98 | − for _, value := range f.values { | |
| 99 | − values.Add(NewStringValue(value.String())) | |
| 100 | − } | |
| 101 | − | |
| 102 | − return map[Key]ValueSet{ | |
| 103 | − f.k: values, | |
| 104 | − } | |
| 105 | −} | |
| 106 | − | |
| 107 | −// notIPAddressFunc - Not IP address function. It checks whether value by Key in given | |
| 108 | −// values is NOT in IP network. Here Key must be AWSSourceIP. | |
| 109 | −// For example, | |
| 110 | −// - if values = [192.168.1.0/24], at evaluate() it returns whether IP address | |
| 111 | −// in value map for AWSSourceIP does not fall in the network 192.168.1.10/24. | |
| 112 | −type notIPAddressFunc struct { | |
| 113 | − ipAddressFunc | |
| 114 | −} | |
| 115 | − | |
| 116 | −// evaluate() - evaluates to check whether IP address in values map for AWSSourceIP | |
| 117 | −// does not fall in one of network. | |
| 118 | −func (f notIPAddressFunc) evaluate(values map[string][]string) bool { | |
| 119 | − return !f.ipAddressFunc.evaluate(values) | |
| 120 | −} | |
| 121 | − | |
| 122 | −// name() - returns "NotIpAddress" condition name. | |
| 123 | −func (f notIPAddressFunc) name() name { | |
| 124 | − return notIPAddress | |
| 125 | −} | |
| 126 | − | |
| 127 | −func (f notIPAddressFunc) String() string { | |
| 128 | − return toIPAddressFuncString(notIPAddress, f.ipAddressFunc.k, f.ipAddressFunc.values) | |
| 129 | −} | |
| 130 | − | |
| 131 | −func valuesToIPNets(n name, values ValueSet) ([]*net.IPNet, error) { | |
| 132 | − IPNets := []*net.IPNet{} | |
| 133 | − for v := range values { | |
| 134 | − s, err := v.GetString() | |
| 135 | − if err != nil { | |
| 136 | − return nil, fmt.Errorf("value %v must be string representation of CIDR for %v condition", v, n) | |
| 137 | − } | |
| 138 | − | |
| 139 | − var IPNet *net.IPNet | |
| 140 | − _, IPNet, err = net.ParseCIDR(s) | |
| 141 | − if err != nil { | |
| 142 | − return nil, fmt.Errorf("value %v must be CIDR string for %v condition", s, n) | |
| 143 | − } | |
| 144 | − | |
| 145 | − IPNets = append(IPNets, IPNet) | |
| 146 | − } | |
| 147 | − | |
| 148 | − return IPNets, nil | |
| 149 | −} | |
| 150 | − | |
| 151 | −// newIPAddressFunc - returns new IP address function. | |
| 152 | −func newIPAddressFunc(key Key, values ValueSet) (Function, error) { | |
| 153 | − IPNets, err := valuesToIPNets(ipAddress, values) | |
| 154 | − if err != nil { | |
| 155 | − return nil, err | |
| 156 | − } | |
| 157 | − | |
| 158 | − return NewIPAddressFunc(key, IPNets...) | |
| 159 | −} | |
| 160 | − | |
| 161 | −// NewIPAddressFunc - returns new IP address function. | |
| 162 | −func NewIPAddressFunc(key Key, IPNets ...*net.IPNet) (Function, error) { | |
| 163 | − if key != AWSSourceIP { | |
| 164 | − return nil, fmt.Errorf("only %v key is allowed for %v condition", AWSSourceIP, ipAddress) | |
| 165 | − } | |
| 166 | − | |
| 167 | − return &ipAddressFunc{key, IPNets}, nil | |
| 168 | −} | |
| 169 | − | |
| 170 | −// newNotIPAddressFunc - returns new Not IP address function. | |
| 171 | −func newNotIPAddressFunc(key Key, values ValueSet) (Function, error) { | |
| 172 | − IPNets, err := valuesToIPNets(notIPAddress, values) | |
| 173 | − if err != nil { | |
| 174 | − return nil, err | |
| 175 | − } | |
| 176 | − | |
| 177 | − return NewNotIPAddressFunc(key, IPNets...) | |
| 178 | −} | |
| 179 | − | |
| 180 | −// NewNotIPAddressFunc - returns new Not IP address function. | |
| 181 | −func NewNotIPAddressFunc(key Key, IPNets ...*net.IPNet) (Function, error) { | |
| 182 | − if key != AWSSourceIP { | |
| 183 | − return nil, fmt.Errorf("only %v key is allowed for %v condition", AWSSourceIP, notIPAddress) | |
| 184 | − } | |
| 185 | − | |
| 186 | − return ¬IPAddressFunc{ipAddressFunc{key, IPNets}}, nil | |
| 187 | −} | |
| @@ -1,279 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "reflect" | |
| 22 | − "testing" | |
| 23 | −) | |
| 24 | − | |
| 25 | −func TestIPAddressFuncEvaluate(t *testing.T) { | |
| 26 | − case1Function, err := newIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 27 | − if err != nil { | |
| 28 | − t.Fatalf("unexpected error. %v\n", err) | |
| 29 | − } | |
| 30 | − | |
| 31 | − testCases := []struct { | |
| 32 | − function Function | |
| 33 | − values map[string][]string | |
| 34 | − expectedResult bool | |
| 35 | − }{ | |
| 36 | − {case1Function, map[string][]string{"SourceIp": {"192.168.1.10"}}, true}, | |
| 37 | − {case1Function, map[string][]string{"SourceIp": {"192.168.2.10"}}, false}, | |
| 38 | − {case1Function, map[string][]string{}, false}, | |
| 39 | − {case1Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 40 | − } | |
| 41 | − | |
| 42 | − for i, testCase := range testCases { | |
| 43 | − result := testCase.function.evaluate(testCase.values) | |
| 44 | − | |
| 45 | − if result != testCase.expectedResult { | |
| 46 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 47 | − } | |
| 48 | − } | |
| 49 | −} | |
| 50 | − | |
| 51 | −func TestIPAddressFuncKey(t *testing.T) { | |
| 52 | − case1Function, err := newIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 53 | − if err != nil { | |
| 54 | − t.Fatalf("unexpected error. %v\n", err) | |
| 55 | − } | |
| 56 | − | |
| 57 | − testCases := []struct { | |
| 58 | − function Function | |
| 59 | − expectedResult Key | |
| 60 | − }{ | |
| 61 | − {case1Function, AWSSourceIP}, | |
| 62 | − } | |
| 63 | − | |
| 64 | − for i, testCase := range testCases { | |
| 65 | − result := testCase.function.key() | |
| 66 | − | |
| 67 | − if result != testCase.expectedResult { | |
| 68 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 69 | − } | |
| 70 | − } | |
| 71 | −} | |
| 72 | − | |
| 73 | −func TestIPAddressFuncToMap(t *testing.T) { | |
| 74 | − case1Function, err := newIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 75 | − if err != nil { | |
| 76 | − t.Fatalf("unexpected error. %v\n", err) | |
| 77 | − } | |
| 78 | − | |
| 79 | − case2Function, err := newIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"), NewStringValue("10.1.10.1/32"))) | |
| 80 | − if err != nil { | |
| 81 | − t.Fatalf("unexpected error. %v\n", err) | |
| 82 | − } | |
| 83 | − | |
| 84 | − case1Result := map[Key]ValueSet{ | |
| 85 | − AWSSourceIP: NewValueSet(NewStringValue("192.168.1.0/24")), | |
| 86 | − } | |
| 87 | − | |
| 88 | − case2Result := map[Key]ValueSet{ | |
| 89 | − AWSSourceIP: NewValueSet(NewStringValue("192.168.1.0/24"), NewStringValue("10.1.10.1/32")), | |
| 90 | − } | |
| 91 | − | |
| 92 | − testCases := []struct { | |
| 93 | − f Function | |
| 94 | − expectedResult map[Key]ValueSet | |
| 95 | − }{ | |
| 96 | − {case1Function, case1Result}, | |
| 97 | − {case2Function, case2Result}, | |
| 98 | − {&ipAddressFunc{}, nil}, | |
| 99 | − } | |
| 100 | − | |
| 101 | − for i, testCase := range testCases { | |
| 102 | − result := testCase.f.toMap() | |
| 103 | − | |
| 104 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 105 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 106 | − } | |
| 107 | − } | |
| 108 | −} | |
| 109 | − | |
| 110 | −func TestNotIPAddressFuncEvaluate(t *testing.T) { | |
| 111 | − case1Function, err := newNotIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 112 | − if err != nil { | |
| 113 | − t.Fatalf("unexpected error. %v\n", err) | |
| 114 | − } | |
| 115 | − | |
| 116 | − testCases := []struct { | |
| 117 | − function Function | |
| 118 | − values map[string][]string | |
| 119 | − expectedResult bool | |
| 120 | − }{ | |
| 121 | − {case1Function, map[string][]string{"SourceIp": {"192.168.2.10"}}, true}, | |
| 122 | − {case1Function, map[string][]string{}, true}, | |
| 123 | − {case1Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 124 | − {case1Function, map[string][]string{"SourceIp": {"192.168.1.10"}}, false}, | |
| 125 | − } | |
| 126 | − | |
| 127 | − for i, testCase := range testCases { | |
| 128 | − result := testCase.function.evaluate(testCase.values) | |
| 129 | − | |
| 130 | − if result != testCase.expectedResult { | |
| 131 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 132 | − } | |
| 133 | − } | |
| 134 | −} | |
| 135 | − | |
| 136 | −func TestNotIPAddressFuncKey(t *testing.T) { | |
| 137 | − case1Function, err := newNotIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 138 | − if err != nil { | |
| 139 | − t.Fatalf("unexpected error. %v\n", err) | |
| 140 | − } | |
| 141 | − | |
| 142 | − testCases := []struct { | |
| 143 | − function Function | |
| 144 | − expectedResult Key | |
| 145 | − }{ | |
| 146 | − {case1Function, AWSSourceIP}, | |
| 147 | − } | |
| 148 | − | |
| 149 | − for i, testCase := range testCases { | |
| 150 | − result := testCase.function.key() | |
| 151 | − | |
| 152 | − if result != testCase.expectedResult { | |
| 153 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 154 | − } | |
| 155 | − } | |
| 156 | −} | |
| 157 | − | |
| 158 | −func TestNotIPAddressFuncToMap(t *testing.T) { | |
| 159 | − case1Function, err := newNotIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 160 | − if err != nil { | |
| 161 | − t.Fatalf("unexpected error. %v\n", err) | |
| 162 | − } | |
| 163 | − | |
| 164 | − case2Function, err := newNotIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"), NewStringValue("10.1.10.1/32"))) | |
| 165 | − if err != nil { | |
| 166 | − t.Fatalf("unexpected error. %v\n", err) | |
| 167 | − } | |
| 168 | − | |
| 169 | − case1Result := map[Key]ValueSet{ | |
| 170 | − AWSSourceIP: NewValueSet(NewStringValue("192.168.1.0/24")), | |
| 171 | − } | |
| 172 | − | |
| 173 | − case2Result := map[Key]ValueSet{ | |
| 174 | − AWSSourceIP: NewValueSet(NewStringValue("192.168.1.0/24"), NewStringValue("10.1.10.1/32")), | |
| 175 | − } | |
| 176 | − | |
| 177 | − testCases := []struct { | |
| 178 | − f Function | |
| 179 | − expectedResult map[Key]ValueSet | |
| 180 | − }{ | |
| 181 | − {case1Function, case1Result}, | |
| 182 | − {case2Function, case2Result}, | |
| 183 | − {¬IPAddressFunc{}, nil}, | |
| 184 | − } | |
| 185 | − | |
| 186 | − for i, testCase := range testCases { | |
| 187 | − result := testCase.f.toMap() | |
| 188 | − | |
| 189 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 190 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 191 | − } | |
| 192 | − } | |
| 193 | −} | |
| 194 | − | |
| 195 | −func TestNewIPAddressFunc(t *testing.T) { | |
| 196 | − case1Function, err := newIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 197 | − if err != nil { | |
| 198 | − t.Fatalf("unexpected error. %v\n", err) | |
| 199 | − } | |
| 200 | − | |
| 201 | − case2Function, err := newIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"), NewStringValue("10.1.10.1/32"))) | |
| 202 | − if err != nil { | |
| 203 | − t.Fatalf("unexpected error. %v\n", err) | |
| 204 | − } | |
| 205 | − | |
| 206 | − testCases := []struct { | |
| 207 | − key Key | |
| 208 | − values ValueSet | |
| 209 | − expectedResult Function | |
| 210 | − expectErr bool | |
| 211 | − }{ | |
| 212 | − {AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24")), case1Function, false}, | |
| 213 | − {AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"), NewStringValue("10.1.10.1/32")), case2Function, false}, | |
| 214 | − // Unsupported key error. | |
| 215 | − {S3Prefix, NewValueSet(NewStringValue("192.168.1.0/24")), nil, true}, | |
| 216 | − // Invalid value error. | |
| 217 | − {AWSSourceIP, NewValueSet(NewStringValue("node1.example.org")), nil, true}, | |
| 218 | − // Invalid CIDR format error. | |
| 219 | − {AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0.0/24")), nil, true}, | |
| 220 | − } | |
| 221 | − | |
| 222 | − for i, testCase := range testCases { | |
| 223 | − result, err := newIPAddressFunc(testCase.key, testCase.values) | |
| 224 | − expectErr := (err != nil) | |
| 225 | − | |
| 226 | − if expectErr != testCase.expectErr { | |
| 227 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 228 | − } | |
| 229 | − | |
| 230 | − if !testCase.expectErr { | |
| 231 | − if result.String() != testCase.expectedResult.String() { | |
| 232 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 233 | − } | |
| 234 | − } | |
| 235 | − } | |
| 236 | −} | |
| 237 | − | |
| 238 | −func TestNewNotIPAddressFunc(t *testing.T) { | |
| 239 | − case1Function, err := newNotIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"))) | |
| 240 | − if err != nil { | |
| 241 | − t.Fatalf("unexpected error. %v\n", err) | |
| 242 | − } | |
| 243 | − | |
| 244 | − case2Function, err := newNotIPAddressFunc(AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"), NewStringValue("10.1.10.1/32"))) | |
| 245 | − if err != nil { | |
| 246 | − t.Fatalf("unexpected error. %v\n", err) | |
| 247 | − } | |
| 248 | − | |
| 249 | − testCases := []struct { | |
| 250 | − key Key | |
| 251 | − values ValueSet | |
| 252 | − expectedResult Function | |
| 253 | − expectErr bool | |
| 254 | − }{ | |
| 255 | − {AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24")), case1Function, false}, | |
| 256 | − {AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0/24"), NewStringValue("10.1.10.1/32")), case2Function, false}, | |
| 257 | − // Unsupported key error. | |
| 258 | − {S3Prefix, NewValueSet(NewStringValue("192.168.1.0/24")), nil, true}, | |
| 259 | − // Invalid value error. | |
| 260 | − {AWSSourceIP, NewValueSet(NewStringValue("node1.example.org")), nil, true}, | |
| 261 | − // Invalid CIDR format error. | |
| 262 | − {AWSSourceIP, NewValueSet(NewStringValue("192.168.1.0.0/24")), nil, true}, | |
| 263 | − } | |
| 264 | − | |
| 265 | − for i, testCase := range testCases { | |
| 266 | − result, err := newNotIPAddressFunc(testCase.key, testCase.values) | |
| 267 | − expectErr := (err != nil) | |
| 268 | − | |
| 269 | − if expectErr != testCase.expectErr { | |
| 270 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 271 | − } | |
| 272 | − | |
| 273 | − if !testCase.expectErr { | |
| 274 | − if result.String() != testCase.expectedResult.String() { | |
| 275 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 276 | − } | |
| 277 | − } | |
| 278 | − } | |
| 279 | −} | |
| @@ -1,80 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −// JWT claims supported substitutions. | |
| 21 | −// https://www.iana.org/assignments/jwt/jwt.xhtml#claims | |
| 22 | −const ( | |
| 23 | − // JWTSub - JWT subject claim substitution. | |
| 24 | − JWTSub Key = "jwt:sub" | |
| 25 | − | |
| 26 | − // JWTIss issuer claim substitution. | |
| 27 | − JWTIss Key = "jwt:iss" | |
| 28 | − | |
| 29 | − // JWTAud audience claim substitution. | |
| 30 | − JWTAud Key = "jwt:aud" | |
| 31 | − | |
| 32 | − // JWTJti JWT unique identifier claim substitution. | |
| 33 | − JWTJti Key = "jwt:jti" | |
| 34 | − | |
| 35 | − JWTUpn Key = "jwt:upn" | |
| 36 | − JWTName Key = "jwt:name" | |
| 37 | − JWTGroups Key = "jwt:groups" | |
| 38 | − JWTGivenName Key = "jwt:given_name" | |
| 39 | − JWTFamilyName Key = "jwt:family_name" | |
| 40 | − JWTMiddleName Key = "jwt:middle_name" | |
| 41 | − JWTNickName Key = "jwt:nickname" | |
| 42 | − JWTPrefUsername Key = "jwt:preferred_username" | |
| 43 | − JWTProfile Key = "jwt:profile" | |
| 44 | − JWTPicture Key = "jwt:picture" | |
| 45 | − JWTWebsite Key = "jwt:website" | |
| 46 | − JWTEmail Key = "jwt:email" | |
| 47 | − JWTGender Key = "jwt:gender" | |
| 48 | − JWTBirthdate Key = "jwt:birthdate" | |
| 49 | − JWTPhoneNumber Key = "jwt:phone_number" | |
| 50 | − JWTAddress Key = "jwt:address" | |
| 51 | − JWTScope Key = "jwt:scope" | |
| 52 | − JWTClientID Key = "jwt:client_id" | |
| 53 | −) | |
| 54 | − | |
| 55 | −// JWTKeys - Supported JWT keys, non-exhaustive list please | |
| 56 | −// expand as new claims are standardized. | |
| 57 | −var JWTKeys = []Key{ | |
| 58 | − JWTSub, | |
| 59 | − JWTIss, | |
| 60 | − JWTAud, | |
| 61 | − JWTJti, | |
| 62 | − JWTName, | |
| 63 | − JWTUpn, | |
| 64 | − JWTGroups, | |
| 65 | − JWTGivenName, | |
| 66 | − JWTFamilyName, | |
| 67 | − JWTMiddleName, | |
| 68 | − JWTNickName, | |
| 69 | − JWTPrefUsername, | |
| 70 | − JWTProfile, | |
| 71 | − JWTPicture, | |
| 72 | − JWTWebsite, | |
| 73 | − JWTEmail, | |
| 74 | − JWTGender, | |
| 75 | − JWTBirthdate, | |
| 76 | − JWTPhoneNumber, | |
| 77 | − JWTAddress, | |
| 78 | − JWTScope, | |
| 79 | − JWTClientID, | |
| 80 | −} | |
| @@ -1,322 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | − "strings" | |
| 24 | −) | |
| 25 | − | |
| 26 | −// Key - conditional key which is used to fetch values for any condition. | |
| 27 | −// Refer https://docs.aws.amazon.com/IAM/latest/UserGuide/list_s3.html | |
| 28 | −// for more information about available condition keys. | |
| 29 | −type Key string | |
| 30 | − | |
| 31 | −const ( | |
| 32 | − // S3XAmzCopySource - key representing x-amz-copy-source HTTP header applicable to PutObject API only. | |
| 33 | − S3XAmzCopySource Key = "s3:x-amz-copy-source" | |
| 34 | − | |
| 35 | − // S3XAmzServerSideEncryption - key representing x-amz-server-side-encryption HTTP header applicable | |
| 36 | − // to PutObject API only. | |
| 37 | − S3XAmzServerSideEncryption Key = "s3:x-amz-server-side-encryption" | |
| 38 | − | |
| 39 | − // S3XAmzServerSideEncryptionCustomerAlgorithm - key representing | |
| 40 | − // x-amz-server-side-encryption-customer-algorithm HTTP header applicable to PutObject API only. | |
| 41 | − S3XAmzServerSideEncryptionCustomerAlgorithm Key = "s3:x-amz-server-side-encryption-customer-algorithm" | |
| 42 | − | |
| 43 | − // S3XAmzMetadataDirective - key representing x-amz-metadata-directive HTTP header applicable to | |
| 44 | − // PutObject API only. | |
| 45 | − S3XAmzMetadataDirective Key = "s3:x-amz-metadata-directive" | |
| 46 | − | |
| 47 | − // S3XAmzContentSha256 - set a static content-sha256 for all calls for a given action. | |
| 48 | − S3XAmzContentSha256 = "s3:x-amz-content-sha256" | |
| 49 | − | |
| 50 | − // S3XAmzStorageClass - key representing x-amz-storage-class HTTP header applicable to PutObject API | |
| 51 | − // only. | |
| 52 | − S3XAmzStorageClass Key = "s3:x-amz-storage-class" | |
| 53 | − | |
| 54 | − // S3LocationConstraint - key representing LocationConstraint XML tag of CreateBucket API only. | |
| 55 | − S3LocationConstraint Key = "s3:LocationConstraint" | |
| 56 | − | |
| 57 | − // S3Prefix - key representing prefix query parameter of ListBucket API only. | |
| 58 | − S3Prefix Key = "s3:prefix" | |
| 59 | − | |
| 60 | − // S3Delimiter - key representing delimiter query parameter of ListBucket API only. | |
| 61 | − S3Delimiter Key = "s3:delimiter" | |
| 62 | − | |
| 63 | − // S3VersionID - Enables you to limit the permission for the | |
| 64 | − // s3:PutObjectVersionTagging action to a specific object version. | |
| 65 | − S3VersionID Key = "s3:versionid" | |
| 66 | − | |
| 67 | − // S3MaxKeys - key representing max-keys query parameter of ListBucket API only. | |
| 68 | − S3MaxKeys Key = "s3:max-keys" | |
| 69 | − | |
| 70 | − // S3ObjectLockRemainingRetentionDays - key representing object-lock-remaining-retention-days | |
| 71 | − // Enables enforcement of an object relative to the remaining retention days, you can set | |
| 72 | − // minimum and maximum allowable retention periods for a bucket using a bucket policy. | |
| 73 | − // This key are specific for s3:PutObjectRetention API. | |
| 74 | − S3ObjectLockRemainingRetentionDays Key = "s3:object-lock-remaining-retention-days" | |
| 75 | − | |
| 76 | − // S3ObjectLockMode - key representing object-lock-mode | |
| 77 | − // Enables enforcement of the specified object retention mode | |
| 78 | − S3ObjectLockMode Key = "s3:object-lock-mode" | |
| 79 | − | |
| 80 | − // S3ObjectLockRetainUntilDate - key representing object-lock-retain-util-date | |
| 81 | − // Enables enforcement of a specific retain-until-date | |
| 82 | − S3ObjectLockRetainUntilDate Key = "s3:object-lock-retain-until-date" | |
| 83 | − | |
| 84 | − // S3ObjectLockLegalHold - key representing object-local-legal-hold | |
| 85 | − // Enables enforcement of the specified object legal hold status | |
| 86 | − S3ObjectLockLegalHold Key = "s3:object-lock-legal-hold" | |
| 87 | − | |
| 88 | − // AWSReferer - key representing Referer header of any API. | |
| 89 | − AWSReferer Key = "aws:Referer" | |
| 90 | − | |
| 91 | − // AWSSourceIP - key representing client's IP address (not intermittent proxies) of any API. | |
| 92 | − AWSSourceIP Key = "aws:SourceIp" | |
| 93 | − | |
| 94 | − // AWSUserAgent - key representing UserAgent header for any API. | |
| 95 | − AWSUserAgent Key = "aws:UserAgent" | |
| 96 | − | |
| 97 | − // AWSSecureTransport - key representing if the clients request is authenticated or not. | |
| 98 | − AWSSecureTransport Key = "aws:SecureTransport" | |
| 99 | − | |
| 100 | − // AWSCurrentTime - key representing the current time. | |
| 101 | − AWSCurrentTime Key = "aws:CurrentTime" | |
| 102 | − | |
| 103 | − // AWSEpochTime - key representing the current epoch time. | |
| 104 | − AWSEpochTime Key = "aws:EpochTime" | |
| 105 | − | |
| 106 | − // AWSPrincipalType - user principal type currently supported values are "User" and "Anonymous". | |
| 107 | − AWSPrincipalType Key = "aws:principaltype" | |
| 108 | − | |
| 109 | − // AWSUserID - user unique ID, in MinIO this value is same as your user Access Key. | |
| 110 | − AWSUserID Key = "aws:userid" | |
| 111 | − | |
| 112 | − // AWSUsername - user friendly name, in MinIO this value is same as your user Access Key. | |
| 113 | − AWSUsername Key = "aws:username" | |
| 114 | − | |
| 115 | − // S3SignatureVersion - identifies the version of AWS Signature that you want to support for authenticated requests. | |
| 116 | − S3SignatureVersion = "s3:signatureversion" | |
| 117 | − | |
| 118 | − // S3AuthType - optionally use this condition key to restrict incoming requests to use a specific authentication method. | |
| 119 | − S3AuthType = "s3:authType" | |
| 120 | −) | |
| 121 | − | |
| 122 | −// AllSupportedKeys - is list of all all supported keys. | |
| 123 | −var AllSupportedKeys = append([]Key{ | |
| 124 | − S3SignatureVersion, | |
| 125 | − S3AuthType, | |
| 126 | − S3XAmzCopySource, | |
| 127 | − S3XAmzServerSideEncryption, | |
| 128 | − S3XAmzServerSideEncryptionCustomerAlgorithm, | |
| 129 | − S3XAmzMetadataDirective, | |
| 130 | − S3XAmzStorageClass, | |
| 131 | − S3XAmzContentSha256, | |
| 132 | − S3LocationConstraint, | |
| 133 | − S3Prefix, | |
| 134 | − S3Delimiter, | |
| 135 | − S3MaxKeys, | |
| 136 | − S3VersionID, | |
| 137 | − S3ObjectLockRemainingRetentionDays, | |
| 138 | − S3ObjectLockMode, | |
| 139 | − S3ObjectLockLegalHold, | |
| 140 | − S3ObjectLockRetainUntilDate, | |
| 141 | − AWSReferer, | |
| 142 | − AWSSourceIP, | |
| 143 | − AWSUserAgent, | |
| 144 | − AWSSecureTransport, | |
| 145 | − AWSCurrentTime, | |
| 146 | − AWSEpochTime, | |
| 147 | − AWSPrincipalType, | |
| 148 | − AWSUserID, | |
| 149 | − AWSUsername, | |
| 150 | − LDAPUser, | |
| 151 | − LDAPUsername, | |
| 152 | − // Add new supported condition keys. | |
| 153 | −}, JWTKeys...) | |
| 154 | − | |
| 155 | −// CommonKeys - is list of all common condition keys. | |
| 156 | −var CommonKeys = append([]Key{ | |
| 157 | − S3SignatureVersion, | |
| 158 | − S3AuthType, | |
| 159 | − S3XAmzContentSha256, | |
| 160 | − S3LocationConstraint, | |
| 161 | − AWSReferer, | |
| 162 | − AWSSourceIP, | |
| 163 | − AWSUserAgent, | |
| 164 | − AWSSecureTransport, | |
| 165 | − AWSCurrentTime, | |
| 166 | − AWSEpochTime, | |
| 167 | − AWSPrincipalType, | |
| 168 | − AWSUserID, | |
| 169 | − AWSUsername, | |
| 170 | − LDAPUser, | |
| 171 | − LDAPUsername, | |
| 172 | −}, JWTKeys...) | |
| 173 | − | |
| 174 | −func substFuncFromValues(values map[string][]string) func(string) string { | |
| 175 | − return func(v string) string { | |
| 176 | − for _, key := range CommonKeys { | |
| 177 | − // Empty values are not supported for policy variables. | |
| 178 | − if rvalues, ok := values[key.Name()]; ok && rvalues[0] != "" { | |
| 179 | − v = strings.Replace(v, key.VarName(), rvalues[0], -1) | |
| 180 | − } | |
| 181 | − } | |
| 182 | − return v | |
| 183 | − } | |
| 184 | −} | |
| 185 | − | |
| 186 | −// IsValid - checks if key is valid or not. | |
| 187 | −func (key Key) IsValid() bool { | |
| 188 | − for _, supKey := range AllSupportedKeys { | |
| 189 | − if supKey == key { | |
| 190 | − return true | |
| 191 | − } | |
| 192 | − } | |
| 193 | − | |
| 194 | − return false | |
| 195 | −} | |
| 196 | − | |
| 197 | −// MarshalJSON - encodes Key to JSON data. | |
| 198 | −func (key Key) MarshalJSON() ([]byte, error) { | |
| 199 | − if !key.IsValid() { | |
| 200 | − return nil, fmt.Errorf("unknown key %v", key) | |
| 201 | − } | |
| 202 | − | |
| 203 | − return json.Marshal(string(key)) | |
| 204 | −} | |
| 205 | − | |
| 206 | −// VarName - returns variable key name, such as "${aws:username}" | |
| 207 | −func (key Key) VarName() string { | |
| 208 | − return fmt.Sprintf("${%s}", key) | |
| 209 | −} | |
| 210 | − | |
| 211 | −// Name - returns key name which is stripped value of prefixes "aws:" and "s3:" | |
| 212 | −func (key Key) Name() string { | |
| 213 | − keyString := string(key) | |
| 214 | − | |
| 215 | − if strings.HasPrefix(keyString, "aws:") { | |
| 216 | − return strings.TrimPrefix(keyString, "aws:") | |
| 217 | − } else if strings.HasPrefix(keyString, "jwt:") { | |
| 218 | − return strings.TrimPrefix(keyString, "jwt:") | |
| 219 | − } else if strings.HasPrefix(keyString, "ldap:") { | |
| 220 | − return strings.TrimPrefix(keyString, "ldap:") | |
| 221 | − } | |
| 222 | − return strings.TrimPrefix(keyString, "s3:") | |
| 223 | −} | |
| 224 | − | |
| 225 | −// UnmarshalJSON - decodes JSON data to Key. | |
| 226 | −func (key *Key) UnmarshalJSON(data []byte) error { | |
| 227 | − var s string | |
| 228 | − if err := json.Unmarshal(data, &s); err != nil { | |
| 229 | − return err | |
| 230 | − } | |
| 231 | − | |
| 232 | − parsedKey, err := parseKey(s) | |
| 233 | − if err != nil { | |
| 234 | − return err | |
| 235 | − } | |
| 236 | − | |
| 237 | − *key = parsedKey | |
| 238 | − return nil | |
| 239 | −} | |
| 240 | − | |
| 241 | −func parseKey(s string) (Key, error) { | |
| 242 | − key := Key(s) | |
| 243 | − | |
| 244 | − if key.IsValid() { | |
| 245 | − return key, nil | |
| 246 | − } | |
| 247 | − | |
| 248 | − return key, fmt.Errorf("invalid condition key '%v'", s) | |
| 249 | −} | |
| 250 | − | |
| 251 | −// KeySet - set representation of slice of keys. | |
| 252 | −type KeySet map[Key]struct{} | |
| 253 | − | |
| 254 | −// Add - add a key to key set. | |
| 255 | −func (set KeySet) Add(key Key) { | |
| 256 | − set[key] = struct{}{} | |
| 257 | −} | |
| 258 | − | |
| 259 | −// Merge merges two key sets, duplicates are overwritten | |
| 260 | −func (set KeySet) Merge(mset KeySet) { | |
| 261 | − for k, v := range mset { | |
| 262 | − set[k] = v | |
| 263 | − } | |
| 264 | −} | |
| 265 | − | |
| 266 | −// Difference - returns a key set contains difference of two keys. | |
| 267 | −// Example: | |
| 268 | −// keySet1 := ["one", "two", "three"] | |
| 269 | −// keySet2 := ["two", "four", "three"] | |
| 270 | −// keySet1.Difference(keySet2) == ["one"] | |
| 271 | −func (set KeySet) Difference(sset KeySet) KeySet { | |
| 272 | − nset := make(KeySet) | |
| 273 | − | |
| 274 | − for k := range set { | |
| 275 | − if _, ok := sset[k]; !ok { | |
| 276 | − nset.Add(k) | |
| 277 | − } | |
| 278 | − } | |
| 279 | − | |
| 280 | − return nset | |
| 281 | −} | |
| 282 | − | |
| 283 | −// IsEmpty - returns whether key set is empty or not. | |
| 284 | −func (set KeySet) IsEmpty() bool { | |
| 285 | − return len(set) == 0 | |
| 286 | −} | |
| 287 | − | |
| 288 | −func (set KeySet) String() string { | |
| 289 | − return fmt.Sprintf("%v", set.ToSlice()) | |
| 290 | −} | |
| 291 | − | |
| 292 | −// ToSlice - returns slice of keys. | |
| 293 | −func (set KeySet) ToSlice() []Key { | |
| 294 | − keys := []Key{} | |
| 295 | − | |
| 296 | − for key := range set { | |
| 297 | − keys = append(keys, key) | |
| 298 | − } | |
| 299 | − | |
| 300 | − return keys | |
| 301 | −} | |
| 302 | − | |
| 303 | −// NewKeySet - returns new KeySet contains given keys. | |
| 304 | −func NewKeySet(keys ...Key) KeySet { | |
| 305 | − set := make(KeySet) | |
| 306 | − for _, key := range keys { | |
| 307 | − set.Add(key) | |
| 308 | − } | |
| 309 | − | |
| 310 | − return set | |
| 311 | −} | |
| 312 | − | |
| 313 | −// AllSupportedAdminKeys - is list of all admin supported keys. | |
| 314 | −var AllSupportedAdminKeys = []Key{ | |
| 315 | − AWSReferer, | |
| 316 | − AWSSourceIP, | |
| 317 | − AWSUserAgent, | |
| 318 | − AWSSecureTransport, | |
| 319 | − AWSCurrentTime, | |
| 320 | − AWSEpochTime, | |
| 321 | − // Add new supported condition keys. | |
| 322 | −} | |
| @@ -1,215 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestKeyIsValid(t *testing.T) { | |
| 27 | − testCases := []struct { | |
| 28 | − key Key | |
| 29 | − expectedResult bool | |
| 30 | − }{ | |
| 31 | − {S3XAmzCopySource, true}, | |
| 32 | − {S3XAmzServerSideEncryption, true}, | |
| 33 | − {S3XAmzServerSideEncryptionCustomerAlgorithm, true}, | |
| 34 | − {S3XAmzMetadataDirective, true}, | |
| 35 | − {S3XAmzStorageClass, true}, | |
| 36 | − {S3LocationConstraint, true}, | |
| 37 | − {S3Prefix, true}, | |
| 38 | − {S3Delimiter, true}, | |
| 39 | − {S3MaxKeys, true}, | |
| 40 | − {AWSReferer, true}, | |
| 41 | − {AWSSourceIP, true}, | |
| 42 | − {Key("foo"), false}, | |
| 43 | − } | |
| 44 | − | |
| 45 | − for i, testCase := range testCases { | |
| 46 | − result := testCase.key.IsValid() | |
| 47 | − | |
| 48 | − if testCase.expectedResult != result { | |
| 49 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 50 | − } | |
| 51 | − } | |
| 52 | −} | |
| 53 | − | |
| 54 | −func TestKeyMarshalJSON(t *testing.T) { | |
| 55 | − testCases := []struct { | |
| 56 | − key Key | |
| 57 | − expectedResult []byte | |
| 58 | − expectErr bool | |
| 59 | − }{ | |
| 60 | − {S3XAmzCopySource, []byte(`"s3:x-amz-copy-source"`), false}, | |
| 61 | − {Key("foo"), nil, true}, | |
| 62 | − } | |
| 63 | − | |
| 64 | − for i, testCase := range testCases { | |
| 65 | − result, err := json.Marshal(testCase.key) | |
| 66 | − expectErr := (err != nil) | |
| 67 | − | |
| 68 | − if testCase.expectErr != expectErr { | |
| 69 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 70 | − } | |
| 71 | − | |
| 72 | − if !testCase.expectErr { | |
| 73 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 74 | − t.Fatalf("case %v: key: expected: %v, got: %v\n", i+1, string(testCase.expectedResult), string(result)) | |
| 75 | − } | |
| 76 | − } | |
| 77 | − } | |
| 78 | −} | |
| 79 | − | |
| 80 | −func TestKeyName(t *testing.T) { | |
| 81 | − testCases := []struct { | |
| 82 | − key Key | |
| 83 | − expectedResult string | |
| 84 | − }{ | |
| 85 | − {S3XAmzCopySource, "x-amz-copy-source"}, | |
| 86 | − {AWSReferer, "Referer"}, | |
| 87 | − } | |
| 88 | − | |
| 89 | − for i, testCase := range testCases { | |
| 90 | − result := testCase.key.Name() | |
| 91 | − | |
| 92 | − if testCase.expectedResult != result { | |
| 93 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 94 | − } | |
| 95 | − } | |
| 96 | −} | |
| 97 | − | |
| 98 | −func TestKeyUnmarshalJSON(t *testing.T) { | |
| 99 | − testCases := []struct { | |
| 100 | − data []byte | |
| 101 | − expectedKey Key | |
| 102 | − expectErr bool | |
| 103 | − }{ | |
| 104 | − {[]byte(`"s3:x-amz-copy-source"`), S3XAmzCopySource, false}, | |
| 105 | − {[]byte(`"foo"`), Key(""), true}, | |
| 106 | − } | |
| 107 | − | |
| 108 | − for i, testCase := range testCases { | |
| 109 | − var key Key | |
| 110 | − err := json.Unmarshal(testCase.data, &key) | |
| 111 | − expectErr := (err != nil) | |
| 112 | − | |
| 113 | − if testCase.expectErr != expectErr { | |
| 114 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 115 | − } | |
| 116 | − | |
| 117 | − if !testCase.expectErr { | |
| 118 | − if testCase.expectedKey != key { | |
| 119 | − t.Fatalf("case %v: key: expected: %v, got: %v\n", i+1, testCase.expectedKey, key) | |
| 120 | − } | |
| 121 | − } | |
| 122 | − } | |
| 123 | −} | |
| 124 | − | |
| 125 | −func TestKeySetAdd(t *testing.T) { | |
| 126 | − testCases := []struct { | |
| 127 | − set KeySet | |
| 128 | − key Key | |
| 129 | − expectedResult KeySet | |
| 130 | − }{ | |
| 131 | − {NewKeySet(), S3XAmzCopySource, NewKeySet(S3XAmzCopySource)}, | |
| 132 | − {NewKeySet(S3XAmzCopySource), S3XAmzCopySource, NewKeySet(S3XAmzCopySource)}, | |
| 133 | − } | |
| 134 | − | |
| 135 | − for i, testCase := range testCases { | |
| 136 | − testCase.set.Add(testCase.key) | |
| 137 | − | |
| 138 | − if !reflect.DeepEqual(testCase.expectedResult, testCase.set) { | |
| 139 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set) | |
| 140 | − } | |
| 141 | − } | |
| 142 | −} | |
| 143 | − | |
| 144 | −func TestKeySetDifference(t *testing.T) { | |
| 145 | − testCases := []struct { | |
| 146 | − set KeySet | |
| 147 | − setToDiff KeySet | |
| 148 | − expectedResult KeySet | |
| 149 | − }{ | |
| 150 | − {NewKeySet(), NewKeySet(S3XAmzCopySource), NewKeySet()}, | |
| 151 | − {NewKeySet(S3Prefix, S3Delimiter, S3MaxKeys), NewKeySet(S3Delimiter, S3MaxKeys), NewKeySet(S3Prefix)}, | |
| 152 | − } | |
| 153 | − | |
| 154 | − for i, testCase := range testCases { | |
| 155 | − result := testCase.set.Difference(testCase.setToDiff) | |
| 156 | − | |
| 157 | − if !reflect.DeepEqual(testCase.expectedResult, result) { | |
| 158 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 159 | − } | |
| 160 | − } | |
| 161 | −} | |
| 162 | − | |
| 163 | −func TestKeySetIsEmpty(t *testing.T) { | |
| 164 | − testCases := []struct { | |
| 165 | − set KeySet | |
| 166 | − expectedResult bool | |
| 167 | − }{ | |
| 168 | − {NewKeySet(), true}, | |
| 169 | − {NewKeySet(S3Delimiter), false}, | |
| 170 | − } | |
| 171 | − | |
| 172 | − for i, testCase := range testCases { | |
| 173 | − result := testCase.set.IsEmpty() | |
| 174 | − | |
| 175 | − if testCase.expectedResult != result { | |
| 176 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 177 | − } | |
| 178 | − } | |
| 179 | −} | |
| 180 | − | |
| 181 | −func TestKeySetString(t *testing.T) { | |
| 182 | − testCases := []struct { | |
| 183 | − set KeySet | |
| 184 | − expectedResult string | |
| 185 | − }{ | |
| 186 | − {NewKeySet(), `[]`}, | |
| 187 | − {NewKeySet(S3Delimiter), `[s3:delimiter]`}, | |
| 188 | − } | |
| 189 | − | |
| 190 | − for i, testCase := range testCases { | |
| 191 | − result := testCase.set.String() | |
| 192 | − | |
| 193 | − if testCase.expectedResult != result { | |
| 194 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 195 | − } | |
| 196 | − } | |
| 197 | −} | |
| 198 | − | |
| 199 | −func TestKeySetToSlice(t *testing.T) { | |
| 200 | − testCases := []struct { | |
| 201 | − set KeySet | |
| 202 | − expectedResult []Key | |
| 203 | − }{ | |
| 204 | − {NewKeySet(), []Key{}}, | |
| 205 | − {NewKeySet(S3Delimiter), []Key{S3Delimiter}}, | |
| 206 | − } | |
| 207 | − | |
| 208 | − for i, testCase := range testCases { | |
| 209 | − result := testCase.set.ToSlice() | |
| 210 | − | |
| 211 | − if !reflect.DeepEqual(testCase.expectedResult, result) { | |
| 212 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 213 | − } | |
| 214 | − } | |
| 215 | −} | |
| @@ -1,26 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −const ( | |
| 21 | − // LDAPUser - LDAP user DN, in MinIO this value is equal to user DN of the authenticated user. | |
| 22 | − LDAPUser Key = "ldap:user" | |
| 23 | − | |
| 24 | − // LDAPUsername - LDAP username, in MinIO is the authenticated simply user. | |
| 25 | − LDAPUsername Key = "ldap:username" | |
| 26 | −) | |
| @@ -1,124 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | −) | |
| 24 | − | |
| 25 | −type name string | |
| 26 | − | |
| 27 | −const ( | |
| 28 | − stringEquals name = "StringEquals" | |
| 29 | − stringNotEquals = "StringNotEquals" | |
| 30 | − stringEqualsIgnoreCase = "StringEqualsIgnoreCase" | |
| 31 | − stringNotEqualsIgnoreCase = "StringNotEqualsIgnoreCase" | |
| 32 | − stringLike = "StringLike" | |
| 33 | − stringNotLike = "StringNotLike" | |
| 34 | − binaryEquals = "BinaryEquals" | |
| 35 | − ipAddress = "IpAddress" | |
| 36 | − notIPAddress = "NotIpAddress" | |
| 37 | − null = "Null" | |
| 38 | − boolean = "Bool" | |
| 39 | − numericEquals = "NumericEquals" | |
| 40 | − numericNotEquals = "NumericNotEquals" | |
| 41 | − numericLessThan = "NumericLessThan" | |
| 42 | − numericLessThanEquals = "NumericLessThanEquals" | |
| 43 | − numericGreaterThan = "NumericGreaterThan" | |
| 44 | − numericGreaterThanEquals = "NumericGreaterThanEquals" | |
| 45 | − dateEquals = "DateEquals" | |
| 46 | − dateNotEquals = "DateNotEquals" | |
| 47 | − dateLessThan = "DateLessThan" | |
| 48 | − dateLessThanEquals = "DateLessThanEquals" | |
| 49 | − dateGreaterThan = "DateGreaterThan" | |
| 50 | − dateGreaterThanEquals = "DateGreaterThanEquals" | |
| 51 | −) | |
| 52 | − | |
| 53 | −var supportedConditions = []name{ | |
| 54 | − stringEquals, | |
| 55 | − stringNotEquals, | |
| 56 | − stringEqualsIgnoreCase, | |
| 57 | − stringNotEqualsIgnoreCase, | |
| 58 | − binaryEquals, | |
| 59 | − stringLike, | |
| 60 | − stringNotLike, | |
| 61 | − ipAddress, | |
| 62 | − notIPAddress, | |
| 63 | − null, | |
| 64 | − boolean, | |
| 65 | − numericEquals, | |
| 66 | − numericNotEquals, | |
| 67 | − numericLessThan, | |
| 68 | − numericLessThanEquals, | |
| 69 | − numericGreaterThan, | |
| 70 | − numericGreaterThanEquals, | |
| 71 | − dateEquals, | |
| 72 | − dateNotEquals, | |
| 73 | − dateLessThan, | |
| 74 | − dateLessThanEquals, | |
| 75 | − dateGreaterThan, | |
| 76 | − dateGreaterThanEquals, | |
| 77 | − // Add new conditions here. | |
| 78 | −} | |
| 79 | − | |
| 80 | −// IsValid - checks if name is valid or not. | |
| 81 | −func (n name) IsValid() bool { | |
| 82 | − for _, supn := range supportedConditions { | |
| 83 | − if n == supn { | |
| 84 | − return true | |
| 85 | − } | |
| 86 | − } | |
| 87 | − | |
| 88 | − return false | |
| 89 | −} | |
| 90 | − | |
| 91 | −// MarshalJSON - encodes name to JSON data. | |
| 92 | −func (n name) MarshalJSON() ([]byte, error) { | |
| 93 | − if !n.IsValid() { | |
| 94 | − return nil, fmt.Errorf("invalid name %v", n) | |
| 95 | − } | |
| 96 | − | |
| 97 | − return json.Marshal(string(n)) | |
| 98 | −} | |
| 99 | − | |
| 100 | −// UnmarshalJSON - decodes JSON data to condition name. | |
| 101 | −func (n *name) UnmarshalJSON(data []byte) error { | |
| 102 | − var s string | |
| 103 | − if err := json.Unmarshal(data, &s); err != nil { | |
| 104 | − return err | |
| 105 | − } | |
| 106 | − | |
| 107 | − parsedName, err := parseName(s) | |
| 108 | − if err != nil { | |
| 109 | − return err | |
| 110 | − } | |
| 111 | − | |
| 112 | − *n = parsedName | |
| 113 | − return nil | |
| 114 | −} | |
| 115 | − | |
| 116 | −func parseName(s string) (name, error) { | |
| 117 | − n := name(s) | |
| 118 | − | |
| 119 | − if n.IsValid() { | |
| 120 | − return n, nil | |
| 121 | − } | |
| 122 | − | |
| 123 | − return n, fmt.Errorf("invalid condition name '%v'", s) | |
| 124 | −} | |
| @@ -1,107 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestNameIsValid(t *testing.T) { | |
| 27 | − testCases := []struct { | |
| 28 | − n name | |
| 29 | − expectedResult bool | |
| 30 | − }{ | |
| 31 | − {stringEquals, true}, | |
| 32 | − {stringNotEquals, true}, | |
| 33 | − {stringLike, true}, | |
| 34 | − {stringNotLike, true}, | |
| 35 | − {ipAddress, true}, | |
| 36 | − {notIPAddress, true}, | |
| 37 | − {null, true}, | |
| 38 | − {name("foo"), false}, | |
| 39 | − } | |
| 40 | − | |
| 41 | − for i, testCase := range testCases { | |
| 42 | − result := testCase.n.IsValid() | |
| 43 | − | |
| 44 | − if testCase.expectedResult != result { | |
| 45 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 46 | − } | |
| 47 | − } | |
| 48 | −} | |
| 49 | − | |
| 50 | −func TestNameMarshalJSON(t *testing.T) { | |
| 51 | − testCases := []struct { | |
| 52 | − n name | |
| 53 | − expectedResult []byte | |
| 54 | − expectErr bool | |
| 55 | − }{ | |
| 56 | − {stringEquals, []byte(`"StringEquals"`), false}, | |
| 57 | − {stringNotEquals, []byte(`"StringNotEquals"`), false}, | |
| 58 | − {stringLike, []byte(`"StringLike"`), false}, | |
| 59 | − {stringNotLike, []byte(`"StringNotLike"`), false}, | |
| 60 | − {ipAddress, []byte(`"IpAddress"`), false}, | |
| 61 | − {notIPAddress, []byte(`"NotIpAddress"`), false}, | |
| 62 | − {null, []byte(`"Null"`), false}, | |
| 63 | − {name("foo"), nil, true}, | |
| 64 | − } | |
| 65 | − | |
| 66 | − for i, testCase := range testCases { | |
| 67 | − result, err := json.Marshal(testCase.n) | |
| 68 | − expectErr := (err != nil) | |
| 69 | − | |
| 70 | − if testCase.expectErr != expectErr { | |
| 71 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 72 | − } | |
| 73 | − | |
| 74 | − if !testCase.expectErr { | |
| 75 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 76 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, string(testCase.expectedResult), string(result)) | |
| 77 | − } | |
| 78 | − } | |
| 79 | − } | |
| 80 | −} | |
| 81 | − | |
| 82 | −func TestNameUnmarshalJSON(t *testing.T) { | |
| 83 | − testCases := []struct { | |
| 84 | − data []byte | |
| 85 | − expectedResult name | |
| 86 | − expectErr bool | |
| 87 | − }{ | |
| 88 | − {[]byte(`"StringEquals"`), stringEquals, false}, | |
| 89 | − {[]byte(`"foo"`), name(""), true}, | |
| 90 | − } | |
| 91 | − | |
| 92 | − for i, testCase := range testCases { | |
| 93 | − var result name | |
| 94 | − err := json.Unmarshal(testCase.data, &result) | |
| 95 | − expectErr := (err != nil) | |
| 96 | − | |
| 97 | − if testCase.expectErr != expectErr { | |
| 98 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 99 | − } | |
| 100 | − | |
| 101 | − if !testCase.expectErr { | |
| 102 | − if testCase.expectedResult != result { | |
| 103 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 104 | − } | |
| 105 | − } | |
| 106 | − } | |
| 107 | −} | |
| @@ -1,107 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "reflect" | |
| 24 | − "strconv" | |
| 25 | −) | |
| 26 | − | |
| 27 | −// nullFunc - Null condition function. It checks whether Key is not present in given | |
| 28 | −// values or not. | |
| 29 | −// For example, | |
| 30 | −// 1. if Key = S3XAmzCopySource and Value = true, at evaluate() it returns whether | |
| 31 | −// S3XAmzCopySource is NOT in given value map or not. | |
| 32 | −// 2. if Key = S3XAmzCopySource and Value = false, at evaluate() it returns whether | |
| 33 | −// S3XAmzCopySource is in given value map or not. | |
| 34 | −// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition_operators.html#Conditions_Null | |
| 35 | −type nullFunc struct { | |
| 36 | − k Key | |
| 37 | − value bool | |
| 38 | −} | |
| 39 | − | |
| 40 | −// evaluate() - evaluates to check whether Key is present in given values or not. | |
| 41 | −// Depending on condition boolean value, this function returns true or false. | |
| 42 | −func (f nullFunc) evaluate(values map[string][]string) bool { | |
| 43 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 44 | − if !ok { | |
| 45 | − requestValue = values[f.k.Name()] | |
| 46 | − } | |
| 47 | − | |
| 48 | − if f.value { | |
| 49 | − return len(requestValue) == 0 | |
| 50 | − } | |
| 51 | − | |
| 52 | − return len(requestValue) != 0 | |
| 53 | −} | |
| 54 | − | |
| 55 | −// key() - returns condition key which is used by this condition function. | |
| 56 | −func (f nullFunc) key() Key { | |
| 57 | − return f.k | |
| 58 | −} | |
| 59 | − | |
| 60 | −// name() - returns "Null" condition name. | |
| 61 | −func (f nullFunc) name() name { | |
| 62 | − return null | |
| 63 | −} | |
| 64 | − | |
| 65 | −func (f nullFunc) String() string { | |
| 66 | − return fmt.Sprintf("%v:%v:%v", null, f.k, f.value) | |
| 67 | −} | |
| 68 | − | |
| 69 | −// toMap - returns map representation of this function. | |
| 70 | −func (f nullFunc) toMap() map[Key]ValueSet { | |
| 71 | − if !f.k.IsValid() { | |
| 72 | − return nil | |
| 73 | − } | |
| 74 | − | |
| 75 | − return map[Key]ValueSet{ | |
| 76 | − f.k: NewValueSet(NewBoolValue(f.value)), | |
| 77 | − } | |
| 78 | −} | |
| 79 | − | |
| 80 | −func newNullFunc(key Key, values ValueSet) (Function, error) { | |
| 81 | − if len(values) != 1 { | |
| 82 | − return nil, fmt.Errorf("only one value is allowed for Null condition") | |
| 83 | − } | |
| 84 | − | |
| 85 | − var value bool | |
| 86 | − for v := range values { | |
| 87 | − switch v.GetType() { | |
| 88 | − case reflect.Bool: | |
| 89 | − value, _ = v.GetBool() | |
| 90 | − case reflect.String: | |
| 91 | − var err error | |
| 92 | − s, _ := v.GetString() | |
| 93 | − if value, err = strconv.ParseBool(s); err != nil { | |
| 94 | − return nil, fmt.Errorf("value must be a boolean string for Null condition") | |
| 95 | − } | |
| 96 | − default: | |
| 97 | − return nil, fmt.Errorf("value must be a boolean for Null condition") | |
| 98 | − } | |
| 99 | − } | |
| 100 | − | |
| 101 | − return &nullFunc{key, value}, nil | |
| 102 | −} | |
| 103 | − | |
| 104 | −// NewNullFunc - returns new Null function. | |
| 105 | −func NewNullFunc(key Key, value bool) (Function, error) { | |
| 106 | − return &nullFunc{key, value}, nil | |
| 107 | −} | |
| @@ -1,162 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "reflect" | |
| 22 | − "testing" | |
| 23 | −) | |
| 24 | − | |
| 25 | −func TestNullFuncEvaluate(t *testing.T) { | |
| 26 | − case1Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(true))) | |
| 27 | − if err != nil { | |
| 28 | − t.Fatalf("unexpected error. %v\n", err) | |
| 29 | − } | |
| 30 | − | |
| 31 | − case2Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(false))) | |
| 32 | − if err != nil { | |
| 33 | − t.Fatalf("unexpected error. %v\n", err) | |
| 34 | − } | |
| 35 | − | |
| 36 | − testCases := []struct { | |
| 37 | − function Function | |
| 38 | − values map[string][]string | |
| 39 | − expectedResult bool | |
| 40 | − }{ | |
| 41 | − {case1Function, map[string][]string{"prefix": {"true"}}, false}, | |
| 42 | − {case1Function, map[string][]string{"prefix": {"false"}}, false}, | |
| 43 | − {case1Function, map[string][]string{"prefix": {"mybucket/foo"}}, false}, | |
| 44 | − {case1Function, map[string][]string{}, true}, | |
| 45 | − {case1Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 46 | − {case2Function, map[string][]string{"prefix": {"true"}}, true}, | |
| 47 | − {case2Function, map[string][]string{"prefix": {"false"}}, true}, | |
| 48 | − {case2Function, map[string][]string{"prefix": {"mybucket/foo"}}, true}, | |
| 49 | − {case2Function, map[string][]string{}, false}, | |
| 50 | − {case2Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 51 | − } | |
| 52 | − | |
| 53 | − for i, testCase := range testCases { | |
| 54 | − result := testCase.function.evaluate(testCase.values) | |
| 55 | − | |
| 56 | − if result != testCase.expectedResult { | |
| 57 | − t.Errorf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 58 | − } | |
| 59 | − } | |
| 60 | −} | |
| 61 | − | |
| 62 | −func TestNullFuncKey(t *testing.T) { | |
| 63 | − case1Function, err := newNullFunc(S3XAmzCopySource, NewValueSet(NewBoolValue(true))) | |
| 64 | − if err != nil { | |
| 65 | − t.Fatalf("unexpected error. %v\n", err) | |
| 66 | − } | |
| 67 | − | |
| 68 | − testCases := []struct { | |
| 69 | − function Function | |
| 70 | − expectedResult Key | |
| 71 | − }{ | |
| 72 | − {case1Function, S3XAmzCopySource}, | |
| 73 | − } | |
| 74 | − | |
| 75 | − for i, testCase := range testCases { | |
| 76 | − result := testCase.function.key() | |
| 77 | − | |
| 78 | − if result != testCase.expectedResult { | |
| 79 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 80 | − } | |
| 81 | − } | |
| 82 | −} | |
| 83 | − | |
| 84 | −func TestNullFuncToMap(t *testing.T) { | |
| 85 | − case1Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(true))) | |
| 86 | − if err != nil { | |
| 87 | − t.Fatalf("unexpected error. %v\n", err) | |
| 88 | − } | |
| 89 | − | |
| 90 | − case1Result := map[Key]ValueSet{ | |
| 91 | − S3Prefix: NewValueSet(NewBoolValue(true)), | |
| 92 | − } | |
| 93 | − | |
| 94 | − case2Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(false))) | |
| 95 | − if err != nil { | |
| 96 | − t.Fatalf("unexpected error. %v\n", err) | |
| 97 | − } | |
| 98 | − | |
| 99 | − case2Result := map[Key]ValueSet{ | |
| 100 | − S3Prefix: NewValueSet(NewBoolValue(false)), | |
| 101 | − } | |
| 102 | − | |
| 103 | − testCases := []struct { | |
| 104 | − f Function | |
| 105 | − expectedResult map[Key]ValueSet | |
| 106 | − }{ | |
| 107 | − {case1Function, case1Result}, | |
| 108 | − {case2Function, case2Result}, | |
| 109 | − {&nullFunc{}, nil}, | |
| 110 | − } | |
| 111 | − | |
| 112 | − for i, testCase := range testCases { | |
| 113 | − result := testCase.f.toMap() | |
| 114 | − | |
| 115 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 116 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 117 | − } | |
| 118 | − } | |
| 119 | −} | |
| 120 | − | |
| 121 | −func TestNewNullFunc(t *testing.T) { | |
| 122 | − case1Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(true))) | |
| 123 | − if err != nil { | |
| 124 | − t.Fatalf("unexpected error. %v\n", err) | |
| 125 | − } | |
| 126 | − | |
| 127 | − case2Function, err := newNullFunc(S3Prefix, NewValueSet(NewBoolValue(false))) | |
| 128 | − if err != nil { | |
| 129 | − t.Fatalf("unexpected error. %v\n", err) | |
| 130 | − } | |
| 131 | − | |
| 132 | − testCases := []struct { | |
| 133 | − key Key | |
| 134 | − values ValueSet | |
| 135 | − expectedResult Function | |
| 136 | − expectErr bool | |
| 137 | − }{ | |
| 138 | − {S3Prefix, NewValueSet(NewBoolValue(true)), case1Function, false}, | |
| 139 | − {S3Prefix, NewValueSet(NewStringValue("false")), case2Function, false}, | |
| 140 | − // Multiple values error. | |
| 141 | − {S3Prefix, NewValueSet(NewBoolValue(true), NewBoolValue(false)), nil, true}, | |
| 142 | − // Invalid boolean string error. | |
| 143 | − {S3Prefix, NewValueSet(NewStringValue("foo")), nil, true}, | |
| 144 | − // Invalid value error. | |
| 145 | − {S3Prefix, NewValueSet(NewIntValue(7)), nil, true}, | |
| 146 | − } | |
| 147 | − | |
| 148 | − for i, testCase := range testCases { | |
| 149 | − result, err := newNullFunc(testCase.key, testCase.values) | |
| 150 | − expectErr := (err != nil) | |
| 151 | − | |
| 152 | − if expectErr != testCase.expectErr { | |
| 153 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 154 | − } | |
| 155 | − | |
| 156 | − if !testCase.expectErr { | |
| 157 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 158 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 159 | − } | |
| 160 | − } | |
| 161 | − } | |
| 162 | −} | |
| @@ -1,169 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "reflect" | |
| 24 | − "strconv" | |
| 25 | −) | |
| 26 | − | |
| 27 | −func toNumericEqualsFuncString(n name, key Key, value int) string { | |
| 28 | − return fmt.Sprintf("%v:%v:%v", n, key, value) | |
| 29 | −} | |
| 30 | − | |
| 31 | −// numericEqualsFunc - String equals function. It checks whether value by Key in given | |
| 32 | −// values map is in condition values. | |
| 33 | −// For example, | |
| 34 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 35 | −// in value map for Key is in values. | |
| 36 | −type numericEqualsFunc struct { | |
| 37 | − k Key | |
| 38 | − value int | |
| 39 | −} | |
| 40 | − | |
| 41 | −// evaluate() - evaluates to check whether value by Key in given values is in | |
| 42 | −// condition values. | |
| 43 | −func (f numericEqualsFunc) evaluate(values map[string][]string) bool { | |
| 44 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 45 | − if !ok { | |
| 46 | − requestValue = values[f.k.Name()] | |
| 47 | − } | |
| 48 | − | |
| 49 | − if len(requestValue) == 0 { | |
| 50 | − return false | |
| 51 | − } | |
| 52 | − | |
| 53 | − rvInt, err := strconv.Atoi(requestValue[0]) | |
| 54 | − if err != nil { | |
| 55 | − return false | |
| 56 | − } | |
| 57 | − | |
| 58 | − return f.value == rvInt | |
| 59 | −} | |
| 60 | − | |
| 61 | −// key() - returns condition key which is used by this condition function. | |
| 62 | −func (f numericEqualsFunc) key() Key { | |
| 63 | − return f.k | |
| 64 | −} | |
| 65 | − | |
| 66 | −// name() - returns "NumericEquals" condition name. | |
| 67 | −func (f numericEqualsFunc) name() name { | |
| 68 | − return numericEquals | |
| 69 | −} | |
| 70 | − | |
| 71 | −func (f numericEqualsFunc) String() string { | |
| 72 | − return toNumericEqualsFuncString(numericEquals, f.k, f.value) | |
| 73 | −} | |
| 74 | − | |
| 75 | −// toMap - returns map representation of this function. | |
| 76 | −func (f numericEqualsFunc) toMap() map[Key]ValueSet { | |
| 77 | − if !f.k.IsValid() { | |
| 78 | − return nil | |
| 79 | − } | |
| 80 | − | |
| 81 | − values := NewValueSet() | |
| 82 | − values.Add(NewIntValue(f.value)) | |
| 83 | − | |
| 84 | − return map[Key]ValueSet{ | |
| 85 | − f.k: values, | |
| 86 | − } | |
| 87 | −} | |
| 88 | − | |
| 89 | −// numericNotEqualsFunc - String not equals function. It checks whether value by Key in | |
| 90 | −// given values is NOT in condition values. | |
| 91 | −// For example, | |
| 92 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 93 | −// in value map for Key is NOT in values. | |
| 94 | −type numericNotEqualsFunc struct { | |
| 95 | − numericEqualsFunc | |
| 96 | −} | |
| 97 | − | |
| 98 | −// evaluate() - evaluates to check whether value by Key in given values is NOT in | |
| 99 | −// condition values. | |
| 100 | −func (f numericNotEqualsFunc) evaluate(values map[string][]string) bool { | |
| 101 | − return !f.numericEqualsFunc.evaluate(values) | |
| 102 | −} | |
| 103 | − | |
| 104 | −// name() - returns "NumericNotEquals" condition name. | |
| 105 | −func (f numericNotEqualsFunc) name() name { | |
| 106 | − return numericNotEquals | |
| 107 | −} | |
| 108 | − | |
| 109 | −func (f numericNotEqualsFunc) String() string { | |
| 110 | − return toNumericEqualsFuncString(numericNotEquals, f.numericEqualsFunc.k, f.numericEqualsFunc.value) | |
| 111 | −} | |
| 112 | − | |
| 113 | −func valueToInt(n name, values ValueSet) (v int, err error) { | |
| 114 | − if len(values) != 1 { | |
| 115 | − return -1, fmt.Errorf("only one value is allowed for %s condition", n) | |
| 116 | − } | |
| 117 | − | |
| 118 | − for vs := range values { | |
| 119 | − switch vs.GetType() { | |
| 120 | − case reflect.Int: | |
| 121 | − if v, err = vs.GetInt(); err != nil { | |
| 122 | − return -1, err | |
| 123 | − } | |
| 124 | − case reflect.String: | |
| 125 | − s, err := vs.GetString() | |
| 126 | − if err != nil { | |
| 127 | − return -1, err | |
| 128 | − } | |
| 129 | − if v, err = strconv.Atoi(s); err != nil { | |
| 130 | − return -1, fmt.Errorf("value %s must be a int for %s condition: %w", vs, n, err) | |
| 131 | − } | |
| 132 | − default: | |
| 133 | − return -1, fmt.Errorf("value %s must be a int for %s condition", vs, n) | |
| 134 | − } | |
| 135 | − } | |
| 136 | − | |
| 137 | − return v, nil | |
| 138 | − | |
| 139 | −} | |
| 140 | − | |
| 141 | −// newNumericEqualsFunc - returns new NumericEquals function. | |
| 142 | −func newNumericEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 143 | − v, err := valueToInt(numericEquals, values) | |
| 144 | − if err != nil { | |
| 145 | − return nil, err | |
| 146 | − } | |
| 147 | − | |
| 148 | − return NewNumericEqualsFunc(key, v) | |
| 149 | −} | |
| 150 | − | |
| 151 | −// NewNumericEqualsFunc - returns new NumericEquals function. | |
| 152 | −func NewNumericEqualsFunc(key Key, value int) (Function, error) { | |
| 153 | − return &numericEqualsFunc{key, value}, nil | |
| 154 | −} | |
| 155 | − | |
| 156 | −// newNumericNotEqualsFunc - returns new NumericNotEquals function. | |
| 157 | −func newNumericNotEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 158 | − v, err := valueToInt(numericNotEquals, values) | |
| 159 | − if err != nil { | |
| 160 | − return nil, err | |
| 161 | − } | |
| 162 | − | |
| 163 | − return NewNumericNotEqualsFunc(key, v) | |
| 164 | −} | |
| 165 | − | |
| 166 | −// NewNumericNotEqualsFunc - returns new NumericNotEquals function. | |
| 167 | −func NewNumericNotEqualsFunc(key Key, value int) (Function, error) { | |
| 168 | − return &numericNotEqualsFunc{numericEqualsFunc{key, value}}, nil | |
| 169 | −} | |
| @@ -1,154 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "strconv" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func toNumericGreaterThanFuncString(n name, key Key, value int) string { | |
| 27 | − return fmt.Sprintf("%v:%v:%v", n, key, value) | |
| 28 | −} | |
| 29 | − | |
| 30 | −// numericGreaterThanFunc - String equals function. It checks whether value by Key in given | |
| 31 | −// values map is in condition values. | |
| 32 | −// For example, | |
| 33 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 34 | −// in value map for Key is in values. | |
| 35 | −type numericGreaterThanFunc struct { | |
| 36 | − k Key | |
| 37 | − value int | |
| 38 | −} | |
| 39 | − | |
| 40 | −// evaluate() - evaluates to check whether value by Key in given values is in | |
| 41 | −// condition values. | |
| 42 | −func (f numericGreaterThanFunc) evaluate(values map[string][]string) bool { | |
| 43 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 44 | − if !ok { | |
| 45 | − requestValue = values[f.k.Name()] | |
| 46 | − } | |
| 47 | − | |
| 48 | − if len(requestValue) == 0 { | |
| 49 | − return false | |
| 50 | − } | |
| 51 | − | |
| 52 | − rvInt, err := strconv.Atoi(requestValue[0]) | |
| 53 | − if err != nil { | |
| 54 | − return false | |
| 55 | − } | |
| 56 | − | |
| 57 | − return rvInt > f.value | |
| 58 | −} | |
| 59 | − | |
| 60 | −// key() - returns condition key which is used by this condition function. | |
| 61 | −func (f numericGreaterThanFunc) key() Key { | |
| 62 | − return f.k | |
| 63 | −} | |
| 64 | − | |
| 65 | −// name() - returns "NumericGreaterThan" condition name. | |
| 66 | −func (f numericGreaterThanFunc) name() name { | |
| 67 | − return numericGreaterThan | |
| 68 | −} | |
| 69 | − | |
| 70 | −func (f numericGreaterThanFunc) String() string { | |
| 71 | − return toNumericGreaterThanFuncString(numericGreaterThan, f.k, f.value) | |
| 72 | −} | |
| 73 | − | |
| 74 | −// toMap - returns map representation of this function. | |
| 75 | −func (f numericGreaterThanFunc) toMap() map[Key]ValueSet { | |
| 76 | − if !f.k.IsValid() { | |
| 77 | − return nil | |
| 78 | − } | |
| 79 | − | |
| 80 | − values := NewValueSet() | |
| 81 | − values.Add(NewIntValue(f.value)) | |
| 82 | − | |
| 83 | − return map[Key]ValueSet{ | |
| 84 | − f.k: values, | |
| 85 | − } | |
| 86 | −} | |
| 87 | − | |
| 88 | −// numericGreaterThanEqualsFunc - String not equals function. It checks whether value by Key in | |
| 89 | −// given values is NOT in condition values. | |
| 90 | −// For example, | |
| 91 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 92 | −// in value map for Key is NOT in values. | |
| 93 | −type numericGreaterThanEqualsFunc struct { | |
| 94 | − numericGreaterThanFunc | |
| 95 | −} | |
| 96 | − | |
| 97 | −// evaluate() - evaluates to check whether value by Key in given values is NOT in | |
| 98 | −// condition values. | |
| 99 | −func (f numericGreaterThanEqualsFunc) evaluate(values map[string][]string) bool { | |
| 100 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 101 | − if !ok { | |
| 102 | − requestValue = values[f.k.Name()] | |
| 103 | − } | |
| 104 | − | |
| 105 | − if len(requestValue) == 0 { | |
| 106 | − return false | |
| 107 | − } | |
| 108 | − | |
| 109 | − rvInt, err := strconv.Atoi(requestValue[0]) | |
| 110 | − if err != nil { | |
| 111 | − return false | |
| 112 | − } | |
| 113 | − | |
| 114 | − return rvInt >= f.value | |
| 115 | −} | |
| 116 | − | |
| 117 | −// name() - returns "NumericGreaterThanEquals" condition name. | |
| 118 | −func (f numericGreaterThanEqualsFunc) name() name { | |
| 119 | − return numericGreaterThanEquals | |
| 120 | −} | |
| 121 | − | |
| 122 | −func (f numericGreaterThanEqualsFunc) String() string { | |
| 123 | − return toNumericGreaterThanFuncString(numericGreaterThanEquals, f.numericGreaterThanFunc.k, f.numericGreaterThanFunc.value) | |
| 124 | −} | |
| 125 | − | |
| 126 | −// newNumericGreaterThanFunc - returns new NumericGreaterThan function. | |
| 127 | −func newNumericGreaterThanFunc(key Key, values ValueSet) (Function, error) { | |
| 128 | − v, err := valueToInt(numericGreaterThan, values) | |
| 129 | − if err != nil { | |
| 130 | − return nil, err | |
| 131 | − } | |
| 132 | − | |
| 133 | − return NewNumericGreaterThanFunc(key, v) | |
| 134 | −} | |
| 135 | − | |
| 136 | −// NewNumericGreaterThanFunc - returns new NumericGreaterThan function. | |
| 137 | −func NewNumericGreaterThanFunc(key Key, value int) (Function, error) { | |
| 138 | − return &numericGreaterThanFunc{key, value}, nil | |
| 139 | −} | |
| 140 | − | |
| 141 | −// newNumericGreaterThanEqualsFunc - returns new NumericGreaterThanEquals function. | |
| 142 | −func newNumericGreaterThanEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 143 | − v, err := valueToInt(numericGreaterThanEquals, values) | |
| 144 | − if err != nil { | |
| 145 | − return nil, err | |
| 146 | − } | |
| 147 | − | |
| 148 | − return NewNumericGreaterThanEqualsFunc(key, v) | |
| 149 | −} | |
| 150 | − | |
| 151 | −// NewNumericGreaterThanEqualsFunc - returns new NumericGreaterThanEquals function. | |
| 152 | −func NewNumericGreaterThanEqualsFunc(key Key, value int) (Function, error) { | |
| 153 | − return &numericGreaterThanEqualsFunc{numericGreaterThanFunc{key, value}}, nil | |
| 154 | −} | |
| @@ -1,154 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "strconv" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func toNumericLessThanFuncString(n name, key Key, value int) string { | |
| 27 | − return fmt.Sprintf("%v:%v:%v", n, key, value) | |
| 28 | −} | |
| 29 | − | |
| 30 | −// numericLessThanFunc - String equals function. It checks whether value by Key in given | |
| 31 | −// values map is in condition values. | |
| 32 | −// For example, | |
| 33 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 34 | −// in value map for Key is in values. | |
| 35 | −type numericLessThanFunc struct { | |
| 36 | − k Key | |
| 37 | − value int | |
| 38 | −} | |
| 39 | − | |
| 40 | −// evaluate() - evaluates to check whether value by Key in given values is in | |
| 41 | −// condition values. | |
| 42 | −func (f numericLessThanFunc) evaluate(values map[string][]string) bool { | |
| 43 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 44 | − if !ok { | |
| 45 | − requestValue = values[f.k.Name()] | |
| 46 | − } | |
| 47 | − | |
| 48 | − if len(requestValue) == 0 { | |
| 49 | − return false | |
| 50 | − } | |
| 51 | − | |
| 52 | − rvInt, err := strconv.Atoi(requestValue[0]) | |
| 53 | − if err != nil { | |
| 54 | − return false | |
| 55 | − } | |
| 56 | − | |
| 57 | − return rvInt < f.value | |
| 58 | −} | |
| 59 | − | |
| 60 | −// key() - returns condition key which is used by this condition function. | |
| 61 | −func (f numericLessThanFunc) key() Key { | |
| 62 | − return f.k | |
| 63 | −} | |
| 64 | − | |
| 65 | −// name() - returns "NumericLessThan" condition name. | |
| 66 | −func (f numericLessThanFunc) name() name { | |
| 67 | − return numericLessThan | |
| 68 | −} | |
| 69 | − | |
| 70 | −func (f numericLessThanFunc) String() string { | |
| 71 | − return toNumericLessThanFuncString(numericLessThan, f.k, f.value) | |
| 72 | −} | |
| 73 | − | |
| 74 | −// toMap - returns map representation of this function. | |
| 75 | −func (f numericLessThanFunc) toMap() map[Key]ValueSet { | |
| 76 | − if !f.k.IsValid() { | |
| 77 | − return nil | |
| 78 | − } | |
| 79 | − | |
| 80 | − values := NewValueSet() | |
| 81 | − values.Add(NewIntValue(f.value)) | |
| 82 | − | |
| 83 | − return map[Key]ValueSet{ | |
| 84 | − f.k: values, | |
| 85 | − } | |
| 86 | −} | |
| 87 | − | |
| 88 | −// numericLessThanEqualsFunc - String not equals function. It checks whether value by Key in | |
| 89 | −// given values is NOT in condition values. | |
| 90 | −// For example, | |
| 91 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 92 | −// in value map for Key is NOT in values. | |
| 93 | −type numericLessThanEqualsFunc struct { | |
| 94 | − numericLessThanFunc | |
| 95 | −} | |
| 96 | − | |
| 97 | −// evaluate() - evaluates to check whether value by Key in given values is NOT in | |
| 98 | −// condition values. | |
| 99 | −func (f numericLessThanEqualsFunc) evaluate(values map[string][]string) bool { | |
| 100 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 101 | − if !ok { | |
| 102 | − requestValue = values[f.k.Name()] | |
| 103 | − } | |
| 104 | − | |
| 105 | − if len(requestValue) == 0 { | |
| 106 | − return false | |
| 107 | − } | |
| 108 | − | |
| 109 | − rvInt, err := strconv.Atoi(requestValue[0]) | |
| 110 | − if err != nil { | |
| 111 | − return false | |
| 112 | − } | |
| 113 | − | |
| 114 | − return rvInt <= f.value | |
| 115 | −} | |
| 116 | − | |
| 117 | −// name() - returns "NumericLessThanEquals" condition name. | |
| 118 | −func (f numericLessThanEqualsFunc) name() name { | |
| 119 | − return numericLessThanEquals | |
| 120 | −} | |
| 121 | − | |
| 122 | −func (f numericLessThanEqualsFunc) String() string { | |
| 123 | − return toNumericLessThanFuncString(numericLessThanEquals, f.numericLessThanFunc.k, f.numericLessThanFunc.value) | |
| 124 | −} | |
| 125 | − | |
| 126 | −// newNumericLessThanFunc - returns new NumericLessThan function. | |
| 127 | −func newNumericLessThanFunc(key Key, values ValueSet) (Function, error) { | |
| 128 | − v, err := valueToInt(numericLessThan, values) | |
| 129 | − if err != nil { | |
| 130 | − return nil, err | |
| 131 | − } | |
| 132 | − | |
| 133 | − return NewNumericLessThanFunc(key, v) | |
| 134 | −} | |
| 135 | − | |
| 136 | −// NewNumericLessThanFunc - returns new NumericLessThan function. | |
| 137 | −func NewNumericLessThanFunc(key Key, value int) (Function, error) { | |
| 138 | − return &numericLessThanFunc{key, value}, nil | |
| 139 | −} | |
| 140 | − | |
| 141 | −// newNumericLessThanEqualsFunc - returns new NumericLessThanEquals function. | |
| 142 | −func newNumericLessThanEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 143 | − v, err := valueToInt(numericLessThanEquals, values) | |
| 144 | − if err != nil { | |
| 145 | − return nil, err | |
| 146 | − } | |
| 147 | − | |
| 148 | − return NewNumericLessThanEqualsFunc(key, v) | |
| 149 | −} | |
| 150 | − | |
| 151 | −// NewNumericLessThanEqualsFunc - returns new NumericLessThanEquals function. | |
| 152 | −func NewNumericLessThanEqualsFunc(key Key, value int) (Function, error) { | |
| 153 | − return &numericLessThanEqualsFunc{numericLessThanFunc{key, value}}, nil | |
| 154 | −} | |
| @@ -1,194 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "sort" | |
| 24 | − | |
| 25 | − "github.com/minio/minio-go/v7/pkg/s3utils" | |
| 26 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 27 | −) | |
| 28 | − | |
| 29 | −func toStringEqualsFuncString(n name, key Key, values set.StringSet) string { | |
| 30 | − valueStrings := values.ToSlice() | |
| 31 | − sort.Strings(valueStrings) | |
| 32 | − | |
| 33 | − return fmt.Sprintf("%v:%v:%v", n, key, valueStrings) | |
| 34 | −} | |
| 35 | − | |
| 36 | −// stringEqualsFunc - String equals function. It checks whether value by Key in given | |
| 37 | −// values map is in condition values. | |
| 38 | −// For example, | |
| 39 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 40 | −// in value map for Key is in values. | |
| 41 | −type stringEqualsFunc struct { | |
| 42 | − k Key | |
| 43 | − values set.StringSet | |
| 44 | −} | |
| 45 | − | |
| 46 | −// evaluate() - evaluates to check whether value by Key in given values is in | |
| 47 | −// condition values. | |
| 48 | −func (f stringEqualsFunc) evaluate(values map[string][]string) bool { | |
| 49 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 50 | − if !ok { | |
| 51 | − requestValue = values[f.k.Name()] | |
| 52 | − } | |
| 53 | − | |
| 54 | − fvalues := f.values.ApplyFunc(substFuncFromValues(values)) | |
| 55 | − return !fvalues.Intersection(set.CreateStringSet(requestValue...)).IsEmpty() | |
| 56 | −} | |
| 57 | − | |
| 58 | −// key() - returns condition key which is used by this condition function. | |
| 59 | −func (f stringEqualsFunc) key() Key { | |
| 60 | − return f.k | |
| 61 | −} | |
| 62 | − | |
| 63 | −// name() - returns "StringEquals" condition name. | |
| 64 | −func (f stringEqualsFunc) name() name { | |
| 65 | − return stringEquals | |
| 66 | −} | |
| 67 | − | |
| 68 | −func (f stringEqualsFunc) String() string { | |
| 69 | − return toStringEqualsFuncString(stringEquals, f.k, f.values) | |
| 70 | −} | |
| 71 | − | |
| 72 | −// toMap - returns map representation of this function. | |
| 73 | −func (f stringEqualsFunc) toMap() map[Key]ValueSet { | |
| 74 | − if !f.k.IsValid() { | |
| 75 | − return nil | |
| 76 | − } | |
| 77 | − | |
| 78 | − values := NewValueSet() | |
| 79 | − for _, value := range f.values.ToSlice() { | |
| 80 | − values.Add(NewStringValue(value)) | |
| 81 | − } | |
| 82 | − | |
| 83 | − return map[Key]ValueSet{ | |
| 84 | − f.k: values, | |
| 85 | − } | |
| 86 | −} | |
| 87 | − | |
| 88 | −// stringNotEqualsFunc - String not equals function. It checks whether value by Key in | |
| 89 | −// given values is NOT in condition values. | |
| 90 | −// For example, | |
| 91 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 92 | −// in value map for Key is NOT in values. | |
| 93 | −type stringNotEqualsFunc struct { | |
| 94 | − stringEqualsFunc | |
| 95 | −} | |
| 96 | − | |
| 97 | −// evaluate() - evaluates to check whether value by Key in given values is NOT in | |
| 98 | −// condition values. | |
| 99 | −func (f stringNotEqualsFunc) evaluate(values map[string][]string) bool { | |
| 100 | − return !f.stringEqualsFunc.evaluate(values) | |
| 101 | −} | |
| 102 | − | |
| 103 | −// name() - returns "StringNotEquals" condition name. | |
| 104 | −func (f stringNotEqualsFunc) name() name { | |
| 105 | − return stringNotEquals | |
| 106 | −} | |
| 107 | − | |
| 108 | −func (f stringNotEqualsFunc) String() string { | |
| 109 | − return toStringEqualsFuncString(stringNotEquals, f.stringEqualsFunc.k, f.stringEqualsFunc.values) | |
| 110 | −} | |
| 111 | − | |
| 112 | −func valuesToStringSlice(n name, values ValueSet) ([]string, error) { | |
| 113 | − valueStrings := []string{} | |
| 114 | − | |
| 115 | − for value := range values { | |
| 116 | − s, err := value.GetString() | |
| 117 | − if err != nil { | |
| 118 | − return nil, fmt.Errorf("value must be a string for %v condition", n) | |
| 119 | − } | |
| 120 | − | |
| 121 | − valueStrings = append(valueStrings, s) | |
| 122 | − } | |
| 123 | − | |
| 124 | − return valueStrings, nil | |
| 125 | −} | |
| 126 | − | |
| 127 | −func validateStringEqualsValues(n name, key Key, values set.StringSet) error { | |
| 128 | − for _, s := range values.ToSlice() { | |
| 129 | − switch key { | |
| 130 | − case S3XAmzCopySource: | |
| 131 | − bucket, object := path2BucketAndObject(s) | |
| 132 | − if object == "" { | |
| 133 | − return fmt.Errorf("invalid value '%v' for '%v' for %v condition", s, S3XAmzCopySource, n) | |
| 134 | − } | |
| 135 | − if err := s3utils.CheckValidBucketName(bucket); err != nil { | |
| 136 | − return err | |
| 137 | − } | |
| 138 | − case S3XAmzServerSideEncryption, S3XAmzServerSideEncryptionCustomerAlgorithm: | |
| 139 | − if s != "AES256" { | |
| 140 | − return fmt.Errorf("invalid value '%v' for '%v' for %v condition", s, S3XAmzServerSideEncryption, n) | |
| 141 | − } | |
| 142 | − case S3XAmzMetadataDirective: | |
| 143 | − if s != "COPY" && s != "REPLACE" { | |
| 144 | − return fmt.Errorf("invalid value '%v' for '%v' for %v condition", s, S3XAmzMetadataDirective, n) | |
| 145 | − } | |
| 146 | − case S3XAmzContentSha256: | |
| 147 | − if s == "" { | |
| 148 | − return fmt.Errorf("invalid empty value for '%v' for %v condition", S3XAmzContentSha256, n) | |
| 149 | − } | |
| 150 | − } | |
| 151 | − } | |
| 152 | − | |
| 153 | − return nil | |
| 154 | −} | |
| 155 | − | |
| 156 | −// newStringEqualsFunc - returns new StringEquals function. | |
| 157 | −func newStringEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 158 | − valueStrings, err := valuesToStringSlice(stringEquals, values) | |
| 159 | − if err != nil { | |
| 160 | − return nil, err | |
| 161 | − } | |
| 162 | − | |
| 163 | − return NewStringEqualsFunc(key, valueStrings...) | |
| 164 | −} | |
| 165 | − | |
| 166 | −// NewStringEqualsFunc - returns new StringEquals function. | |
| 167 | −func NewStringEqualsFunc(key Key, values ...string) (Function, error) { | |
| 168 | − sset := set.CreateStringSet(values...) | |
| 169 | − if err := validateStringEqualsValues(stringEquals, key, sset); err != nil { | |
| 170 | − return nil, err | |
| 171 | − } | |
| 172 | − | |
| 173 | − return &stringEqualsFunc{key, sset}, nil | |
| 174 | −} | |
| 175 | − | |
| 176 | −// newStringNotEqualsFunc - returns new StringNotEquals function. | |
| 177 | −func newStringNotEqualsFunc(key Key, values ValueSet) (Function, error) { | |
| 178 | − valueStrings, err := valuesToStringSlice(stringNotEquals, values) | |
| 179 | − if err != nil { | |
| 180 | − return nil, err | |
| 181 | − } | |
| 182 | − | |
| 183 | − return NewStringNotEqualsFunc(key, valueStrings...) | |
| 184 | −} | |
| 185 | − | |
| 186 | −// NewStringNotEqualsFunc - returns new StringNotEquals function. | |
| 187 | −func NewStringNotEqualsFunc(key Key, values ...string) (Function, error) { | |
| 188 | − sset := set.CreateStringSet(values...) | |
| 189 | − if err := validateStringEqualsValues(stringNotEquals, key, sset); err != nil { | |
| 190 | − return nil, err | |
| 191 | − } | |
| 192 | − | |
| 193 | − return &stringNotEqualsFunc{stringEqualsFunc{key, sset}}, nil | |
| 194 | −} | |
| @@ -1,709 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "reflect" | |
| 22 | − "testing" | |
| 23 | −) | |
| 24 | − | |
| 25 | −func TestStringEqualsFuncEvaluate(t *testing.T) { | |
| 26 | − case1Function, err := newStringEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 27 | − if err != nil { | |
| 28 | − t.Fatalf("unexpected error. %v\n", err) | |
| 29 | − } | |
| 30 | − | |
| 31 | − case2Function, err := newStringEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 32 | − if err != nil { | |
| 33 | − t.Fatalf("unexpected error. %v\n", err) | |
| 34 | − } | |
| 35 | − | |
| 36 | − case3Function, err := newStringEqualsFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 37 | − if err != nil { | |
| 38 | − t.Fatalf("unexpected error. %v\n", err) | |
| 39 | − } | |
| 40 | − | |
| 41 | − case4Function, err := newStringEqualsFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 42 | − if err != nil { | |
| 43 | − t.Fatalf("unexpected error. %v\n", err) | |
| 44 | − } | |
| 45 | − | |
| 46 | − testCases := []struct { | |
| 47 | − function Function | |
| 48 | − values map[string][]string | |
| 49 | − expectedResult bool | |
| 50 | − }{ | |
| 51 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, true}, | |
| 52 | − {case1Function, map[string][]string{"x-amz-copy-source": {"yourbucket/myobject"}}, false}, | |
| 53 | − {case1Function, map[string][]string{}, false}, | |
| 54 | − {case1Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 55 | − | |
| 56 | − {case2Function, map[string][]string{"x-amz-server-side-encryption": {"AES256"}}, true}, | |
| 57 | − {case2Function, map[string][]string{}, false}, | |
| 58 | − {case2Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 59 | − | |
| 60 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE"}}, true}, | |
| 61 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"COPY"}}, false}, | |
| 62 | − {case3Function, map[string][]string{}, false}, | |
| 63 | − {case3Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 64 | − | |
| 65 | − {case4Function, map[string][]string{"LocationConstraint": {"eu-west-1"}}, true}, | |
| 66 | − {case4Function, map[string][]string{"LocationConstraint": {"us-east-1"}}, false}, | |
| 67 | − {case4Function, map[string][]string{}, false}, | |
| 68 | − {case4Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 69 | − } | |
| 70 | − | |
| 71 | − for i, testCase := range testCases { | |
| 72 | − result := testCase.function.evaluate(testCase.values) | |
| 73 | − | |
| 74 | − if result != testCase.expectedResult { | |
| 75 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 76 | − } | |
| 77 | − } | |
| 78 | −} | |
| 79 | − | |
| 80 | −func TestStringEqualsFuncKey(t *testing.T) { | |
| 81 | − case1Function, err := newStringEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 82 | − if err != nil { | |
| 83 | − t.Fatalf("unexpected error. %v\n", err) | |
| 84 | − } | |
| 85 | − | |
| 86 | − case2Function, err := newStringEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 87 | − if err != nil { | |
| 88 | − t.Fatalf("unexpected error. %v\n", err) | |
| 89 | − } | |
| 90 | − | |
| 91 | − case3Function, err := newStringEqualsFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 92 | − if err != nil { | |
| 93 | − t.Fatalf("unexpected error. %v\n", err) | |
| 94 | − } | |
| 95 | − | |
| 96 | − case4Function, err := newStringEqualsFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 97 | − if err != nil { | |
| 98 | − t.Fatalf("unexpected error. %v\n", err) | |
| 99 | − } | |
| 100 | − | |
| 101 | − testCases := []struct { | |
| 102 | − function Function | |
| 103 | − expectedResult Key | |
| 104 | − }{ | |
| 105 | − {case1Function, S3XAmzCopySource}, | |
| 106 | − {case2Function, S3XAmzServerSideEncryption}, | |
| 107 | − {case3Function, S3XAmzMetadataDirective}, | |
| 108 | − {case4Function, S3LocationConstraint}, | |
| 109 | − } | |
| 110 | − | |
| 111 | − for i, testCase := range testCases { | |
| 112 | − result := testCase.function.key() | |
| 113 | − | |
| 114 | − if result != testCase.expectedResult { | |
| 115 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 116 | − } | |
| 117 | − } | |
| 118 | −} | |
| 119 | − | |
| 120 | −func TestStringEqualsFuncToMap(t *testing.T) { | |
| 121 | − case1Function, err := newStringEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 122 | − if err != nil { | |
| 123 | − t.Fatalf("unexpected error. %v\n", err) | |
| 124 | − } | |
| 125 | − | |
| 126 | − case1Result := map[Key]ValueSet{ | |
| 127 | − S3XAmzCopySource: NewValueSet(NewStringValue("mybucket/myobject")), | |
| 128 | − } | |
| 129 | − | |
| 130 | − case2Function, err := newStringEqualsFunc(S3XAmzCopySource, | |
| 131 | − NewValueSet( | |
| 132 | − NewStringValue("mybucket/myobject"), | |
| 133 | − NewStringValue("yourbucket/myobject"), | |
| 134 | − ), | |
| 135 | − ) | |
| 136 | − if err != nil { | |
| 137 | − t.Fatalf("unexpected error. %v\n", err) | |
| 138 | − } | |
| 139 | − | |
| 140 | − case2Result := map[Key]ValueSet{ | |
| 141 | − S3XAmzCopySource: NewValueSet( | |
| 142 | − NewStringValue("mybucket/myobject"), | |
| 143 | − NewStringValue("yourbucket/myobject"), | |
| 144 | − ), | |
| 145 | − } | |
| 146 | − | |
| 147 | − case3Function, err := newStringEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 148 | − if err != nil { | |
| 149 | − t.Fatalf("unexpected error. %v\n", err) | |
| 150 | − } | |
| 151 | − | |
| 152 | − case3Result := map[Key]ValueSet{ | |
| 153 | − S3XAmzServerSideEncryption: NewValueSet(NewStringValue("AES256")), | |
| 154 | − } | |
| 155 | − | |
| 156 | − case4Function, err := newStringEqualsFunc(S3XAmzServerSideEncryption, | |
| 157 | − NewValueSet( | |
| 158 | − NewStringValue("AES256"), | |
| 159 | − ), | |
| 160 | − ) | |
| 161 | − if err != nil { | |
| 162 | − t.Fatalf("unexpected error. %v\n", err) | |
| 163 | − } | |
| 164 | − | |
| 165 | − case4Result := map[Key]ValueSet{ | |
| 166 | − S3XAmzServerSideEncryption: NewValueSet( | |
| 167 | − NewStringValue("AES256"), | |
| 168 | − ), | |
| 169 | − } | |
| 170 | − | |
| 171 | − case5Function, err := newStringEqualsFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 172 | − if err != nil { | |
| 173 | − t.Fatalf("unexpected error. %v\n", err) | |
| 174 | − } | |
| 175 | − | |
| 176 | − case5Result := map[Key]ValueSet{ | |
| 177 | − S3XAmzMetadataDirective: NewValueSet(NewStringValue("REPLACE")), | |
| 178 | − } | |
| 179 | − | |
| 180 | − case6Function, err := newStringEqualsFunc(S3XAmzMetadataDirective, | |
| 181 | − NewValueSet( | |
| 182 | − NewStringValue("REPLACE"), | |
| 183 | − NewStringValue("COPY"), | |
| 184 | − ), | |
| 185 | − ) | |
| 186 | − if err != nil { | |
| 187 | − t.Fatalf("unexpected error. %v\n", err) | |
| 188 | − } | |
| 189 | − | |
| 190 | − case6Result := map[Key]ValueSet{ | |
| 191 | − S3XAmzMetadataDirective: NewValueSet( | |
| 192 | − NewStringValue("REPLACE"), | |
| 193 | − NewStringValue("COPY"), | |
| 194 | − ), | |
| 195 | − } | |
| 196 | − | |
| 197 | − case7Function, err := newStringEqualsFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 198 | − if err != nil { | |
| 199 | − t.Fatalf("unexpected error. %v\n", err) | |
| 200 | − } | |
| 201 | − | |
| 202 | − case7Result := map[Key]ValueSet{ | |
| 203 | − S3LocationConstraint: NewValueSet(NewStringValue("eu-west-1")), | |
| 204 | − } | |
| 205 | − | |
| 206 | − case8Function, err := newStringEqualsFunc(S3LocationConstraint, | |
| 207 | − NewValueSet( | |
| 208 | − NewStringValue("eu-west-1"), | |
| 209 | − NewStringValue("us-west-1"), | |
| 210 | − ), | |
| 211 | − ) | |
| 212 | − if err != nil { | |
| 213 | − t.Fatalf("unexpected error. %v\n", err) | |
| 214 | − } | |
| 215 | − | |
| 216 | − case8Result := map[Key]ValueSet{ | |
| 217 | − S3LocationConstraint: NewValueSet( | |
| 218 | − NewStringValue("eu-west-1"), | |
| 219 | − NewStringValue("us-west-1"), | |
| 220 | − ), | |
| 221 | − } | |
| 222 | − | |
| 223 | − testCases := []struct { | |
| 224 | − f Function | |
| 225 | − expectedResult map[Key]ValueSet | |
| 226 | − }{ | |
| 227 | − {case1Function, case1Result}, | |
| 228 | − {case2Function, case2Result}, | |
| 229 | − {case3Function, case3Result}, | |
| 230 | − {case4Function, case4Result}, | |
| 231 | − {case5Function, case5Result}, | |
| 232 | − {case6Function, case6Result}, | |
| 233 | − {case7Function, case7Result}, | |
| 234 | − {case8Function, case8Result}, | |
| 235 | − {&stringEqualsFunc{}, nil}, | |
| 236 | − } | |
| 237 | − | |
| 238 | − for i, testCase := range testCases { | |
| 239 | − result := testCase.f.toMap() | |
| 240 | − | |
| 241 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 242 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 243 | − } | |
| 244 | − } | |
| 245 | −} | |
| 246 | − | |
| 247 | −func TestStringNotEqualsFuncEvaluate(t *testing.T) { | |
| 248 | − case1Function, err := newStringNotEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 249 | − if err != nil { | |
| 250 | − t.Fatalf("unexpected error. %v\n", err) | |
| 251 | − } | |
| 252 | − | |
| 253 | − case2Function, err := newStringNotEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 254 | − if err != nil { | |
| 255 | − t.Fatalf("unexpected error. %v\n", err) | |
| 256 | − } | |
| 257 | − | |
| 258 | − case3Function, err := newStringNotEqualsFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 259 | − if err != nil { | |
| 260 | − t.Fatalf("unexpected error. %v\n", err) | |
| 261 | − } | |
| 262 | − | |
| 263 | − case4Function, err := newStringNotEqualsFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 264 | − if err != nil { | |
| 265 | − t.Fatalf("unexpected error. %v\n", err) | |
| 266 | − } | |
| 267 | − | |
| 268 | − testCases := []struct { | |
| 269 | − function Function | |
| 270 | − values map[string][]string | |
| 271 | − expectedResult bool | |
| 272 | − }{ | |
| 273 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, false}, | |
| 274 | − {case1Function, map[string][]string{"x-amz-copy-source": {"yourbucket/myobject"}}, true}, | |
| 275 | − {case1Function, map[string][]string{}, true}, | |
| 276 | − {case1Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 277 | − | |
| 278 | − {case2Function, map[string][]string{"x-amz-server-side-encryption": {"AES256"}}, false}, | |
| 279 | − {case2Function, map[string][]string{}, true}, | |
| 280 | − {case2Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 281 | − | |
| 282 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE"}}, false}, | |
| 283 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"COPY"}}, true}, | |
| 284 | − {case3Function, map[string][]string{}, true}, | |
| 285 | − {case3Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 286 | − | |
| 287 | − {case4Function, map[string][]string{"LocationConstraint": {"eu-west-1"}}, false}, | |
| 288 | − {case4Function, map[string][]string{"LocationConstraint": {"us-east-1"}}, true}, | |
| 289 | − {case4Function, map[string][]string{}, true}, | |
| 290 | − {case4Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 291 | − } | |
| 292 | − | |
| 293 | − for i, testCase := range testCases { | |
| 294 | − result := testCase.function.evaluate(testCase.values) | |
| 295 | − | |
| 296 | − if result != testCase.expectedResult { | |
| 297 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 298 | − } | |
| 299 | − } | |
| 300 | −} | |
| 301 | − | |
| 302 | −func TestStringNotEqualsFuncKey(t *testing.T) { | |
| 303 | − case1Function, err := newStringNotEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 304 | − if err != nil { | |
| 305 | − t.Fatalf("unexpected error. %v\n", err) | |
| 306 | − } | |
| 307 | − | |
| 308 | − case2Function, err := newStringNotEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 309 | − if err != nil { | |
| 310 | − t.Fatalf("unexpected error. %v\n", err) | |
| 311 | − } | |
| 312 | − | |
| 313 | − case3Function, err := newStringNotEqualsFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 314 | − if err != nil { | |
| 315 | − t.Fatalf("unexpected error. %v\n", err) | |
| 316 | − } | |
| 317 | − | |
| 318 | − case4Function, err := newStringNotEqualsFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 319 | − if err != nil { | |
| 320 | − t.Fatalf("unexpected error. %v\n", err) | |
| 321 | − } | |
| 322 | − | |
| 323 | − testCases := []struct { | |
| 324 | − function Function | |
| 325 | − expectedResult Key | |
| 326 | − }{ | |
| 327 | − {case1Function, S3XAmzCopySource}, | |
| 328 | − {case2Function, S3XAmzServerSideEncryption}, | |
| 329 | − {case3Function, S3XAmzMetadataDirective}, | |
| 330 | − {case4Function, S3LocationConstraint}, | |
| 331 | − } | |
| 332 | − | |
| 333 | − for i, testCase := range testCases { | |
| 334 | − result := testCase.function.key() | |
| 335 | − | |
| 336 | − if result != testCase.expectedResult { | |
| 337 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 338 | − } | |
| 339 | − } | |
| 340 | −} | |
| 341 | − | |
| 342 | −func TestStringNotEqualsFuncToMap(t *testing.T) { | |
| 343 | − case1Function, err := newStringNotEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 344 | − if err != nil { | |
| 345 | − t.Fatalf("unexpected error. %v\n", err) | |
| 346 | − } | |
| 347 | − | |
| 348 | − case1Result := map[Key]ValueSet{ | |
| 349 | − S3XAmzCopySource: NewValueSet(NewStringValue("mybucket/myobject")), | |
| 350 | − } | |
| 351 | − | |
| 352 | − case2Function, err := newStringNotEqualsFunc(S3XAmzCopySource, | |
| 353 | − NewValueSet( | |
| 354 | − NewStringValue("mybucket/myobject"), | |
| 355 | − NewStringValue("yourbucket/myobject"), | |
| 356 | − ), | |
| 357 | − ) | |
| 358 | − if err != nil { | |
| 359 | − t.Fatalf("unexpected error. %v\n", err) | |
| 360 | − } | |
| 361 | − | |
| 362 | − case2Result := map[Key]ValueSet{ | |
| 363 | − S3XAmzCopySource: NewValueSet( | |
| 364 | − NewStringValue("mybucket/myobject"), | |
| 365 | − NewStringValue("yourbucket/myobject"), | |
| 366 | − ), | |
| 367 | − } | |
| 368 | − | |
| 369 | − case3Function, err := newStringNotEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 370 | − if err != nil { | |
| 371 | − t.Fatalf("unexpected error. %v\n", err) | |
| 372 | − } | |
| 373 | − | |
| 374 | − case3Result := map[Key]ValueSet{ | |
| 375 | − S3XAmzServerSideEncryption: NewValueSet(NewStringValue("AES256")), | |
| 376 | − } | |
| 377 | − | |
| 378 | − case4Function, err := newStringNotEqualsFunc(S3XAmzServerSideEncryption, | |
| 379 | − NewValueSet( | |
| 380 | − NewStringValue("AES256"), | |
| 381 | − ), | |
| 382 | − ) | |
| 383 | − if err != nil { | |
| 384 | − t.Fatalf("unexpected error. %v\n", err) | |
| 385 | − } | |
| 386 | − | |
| 387 | − case4Result := map[Key]ValueSet{ | |
| 388 | − S3XAmzServerSideEncryption: NewValueSet( | |
| 389 | − NewStringValue("AES256"), | |
| 390 | − ), | |
| 391 | − } | |
| 392 | − | |
| 393 | − case5Function, err := newStringNotEqualsFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 394 | − if err != nil { | |
| 395 | − t.Fatalf("unexpected error. %v\n", err) | |
| 396 | − } | |
| 397 | − | |
| 398 | − case5Result := map[Key]ValueSet{ | |
| 399 | − S3XAmzMetadataDirective: NewValueSet(NewStringValue("REPLACE")), | |
| 400 | − } | |
| 401 | − | |
| 402 | − case6Function, err := newStringNotEqualsFunc(S3XAmzMetadataDirective, | |
| 403 | − NewValueSet( | |
| 404 | − NewStringValue("REPLACE"), | |
| 405 | − NewStringValue("COPY"), | |
| 406 | − ), | |
| 407 | − ) | |
| 408 | − if err != nil { | |
| 409 | − t.Fatalf("unexpected error. %v\n", err) | |
| 410 | − } | |
| 411 | − | |
| 412 | − case6Result := map[Key]ValueSet{ | |
| 413 | − S3XAmzMetadataDirective: NewValueSet( | |
| 414 | − NewStringValue("REPLACE"), | |
| 415 | − NewStringValue("COPY"), | |
| 416 | − ), | |
| 417 | − } | |
| 418 | − | |
| 419 | − case7Function, err := newStringNotEqualsFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 420 | − if err != nil { | |
| 421 | − t.Fatalf("unexpected error. %v\n", err) | |
| 422 | − } | |
| 423 | − | |
| 424 | − case7Result := map[Key]ValueSet{ | |
| 425 | − S3LocationConstraint: NewValueSet(NewStringValue("eu-west-1")), | |
| 426 | − } | |
| 427 | − | |
| 428 | − case8Function, err := newStringNotEqualsFunc(S3LocationConstraint, | |
| 429 | − NewValueSet( | |
| 430 | − NewStringValue("eu-west-1"), | |
| 431 | − NewStringValue("us-west-1"), | |
| 432 | − ), | |
| 433 | − ) | |
| 434 | − if err != nil { | |
| 435 | − t.Fatalf("unexpected error. %v\n", err) | |
| 436 | − } | |
| 437 | − | |
| 438 | − case8Result := map[Key]ValueSet{ | |
| 439 | − S3LocationConstraint: NewValueSet( | |
| 440 | − NewStringValue("eu-west-1"), | |
| 441 | − NewStringValue("us-west-1"), | |
| 442 | − ), | |
| 443 | − } | |
| 444 | − | |
| 445 | − testCases := []struct { | |
| 446 | − f Function | |
| 447 | − expectedResult map[Key]ValueSet | |
| 448 | − }{ | |
| 449 | − {case1Function, case1Result}, | |
| 450 | − {case2Function, case2Result}, | |
| 451 | − {case3Function, case3Result}, | |
| 452 | − {case4Function, case4Result}, | |
| 453 | − {case5Function, case5Result}, | |
| 454 | − {case6Function, case6Result}, | |
| 455 | − {case7Function, case7Result}, | |
| 456 | − {case8Function, case8Result}, | |
| 457 | − {&stringNotEqualsFunc{}, nil}, | |
| 458 | − } | |
| 459 | − | |
| 460 | − for i, testCase := range testCases { | |
| 461 | − result := testCase.f.toMap() | |
| 462 | − | |
| 463 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 464 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 465 | − } | |
| 466 | − } | |
| 467 | −} | |
| 468 | − | |
| 469 | −func TestNewStringEqualsFunc(t *testing.T) { | |
| 470 | − case1Function, err := newStringEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 471 | − if err != nil { | |
| 472 | − t.Fatalf("unexpected error. %v\n", err) | |
| 473 | − } | |
| 474 | − | |
| 475 | − case2Function, err := newStringEqualsFunc(S3XAmzCopySource, | |
| 476 | − NewValueSet( | |
| 477 | − NewStringValue("mybucket/myobject"), | |
| 478 | − NewStringValue("yourbucket/myobject"), | |
| 479 | − ), | |
| 480 | − ) | |
| 481 | − if err != nil { | |
| 482 | − t.Fatalf("unexpected error. %v\n", err) | |
| 483 | − } | |
| 484 | − | |
| 485 | − case3Function, err := newStringEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 486 | − if err != nil { | |
| 487 | − t.Fatalf("unexpected error. %v\n", err) | |
| 488 | − } | |
| 489 | − | |
| 490 | − case4Function, err := newStringEqualsFunc(S3XAmzServerSideEncryption, | |
| 491 | − NewValueSet( | |
| 492 | − NewStringValue("AES256"), | |
| 493 | − ), | |
| 494 | − ) | |
| 495 | − if err != nil { | |
| 496 | − t.Fatalf("unexpected error. %v\n", err) | |
| 497 | − } | |
| 498 | − | |
| 499 | − case5Function, err := newStringEqualsFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 500 | − if err != nil { | |
| 501 | − t.Fatalf("unexpected error. %v\n", err) | |
| 502 | − } | |
| 503 | − | |
| 504 | − case6Function, err := newStringEqualsFunc(S3XAmzMetadataDirective, | |
| 505 | − NewValueSet( | |
| 506 | − NewStringValue("REPLACE"), | |
| 507 | − NewStringValue("COPY"), | |
| 508 | − ), | |
| 509 | − ) | |
| 510 | − if err != nil { | |
| 511 | − t.Fatalf("unexpected error. %v\n", err) | |
| 512 | − } | |
| 513 | − | |
| 514 | − case7Function, err := newStringEqualsFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 515 | − if err != nil { | |
| 516 | − t.Fatalf("unexpected error. %v\n", err) | |
| 517 | − } | |
| 518 | − | |
| 519 | − case8Function, err := newStringEqualsFunc(S3LocationConstraint, | |
| 520 | − NewValueSet( | |
| 521 | − NewStringValue("eu-west-1"), | |
| 522 | − NewStringValue("us-west-1"), | |
| 523 | − ), | |
| 524 | − ) | |
| 525 | − if err != nil { | |
| 526 | − t.Fatalf("unexpected error. %v\n", err) | |
| 527 | − } | |
| 528 | − | |
| 529 | − testCases := []struct { | |
| 530 | − key Key | |
| 531 | − values ValueSet | |
| 532 | − expectedResult Function | |
| 533 | − expectErr bool | |
| 534 | − }{ | |
| 535 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject")), case1Function, false}, | |
| 536 | − {S3XAmzCopySource, | |
| 537 | − NewValueSet( | |
| 538 | − NewStringValue("mybucket/myobject"), | |
| 539 | − NewStringValue("yourbucket/myobject"), | |
| 540 | − ), case2Function, false}, | |
| 541 | − | |
| 542 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256")), case3Function, false}, | |
| 543 | − {S3XAmzServerSideEncryption, | |
| 544 | − NewValueSet( | |
| 545 | − NewStringValue("AES256"), | |
| 546 | − ), case4Function, false}, | |
| 547 | − | |
| 548 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE")), case5Function, false}, | |
| 549 | − {S3XAmzMetadataDirective, | |
| 550 | − NewValueSet( | |
| 551 | − NewStringValue("REPLACE"), | |
| 552 | − NewStringValue("COPY"), | |
| 553 | − ), case6Function, false}, | |
| 554 | − | |
| 555 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1")), case7Function, false}, | |
| 556 | − {S3LocationConstraint, | |
| 557 | − NewValueSet( | |
| 558 | − NewStringValue("eu-west-1"), | |
| 559 | − NewStringValue("us-west-1"), | |
| 560 | − ), case8Function, false}, | |
| 561 | − | |
| 562 | − // Unsupported value error. | |
| 563 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"), NewIntValue(7)), nil, true}, | |
| 564 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"), NewIntValue(7)), nil, true}, | |
| 565 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"), NewIntValue(7)), nil, true}, | |
| 566 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"), NewIntValue(7)), nil, true}, | |
| 567 | − | |
| 568 | − // Invalid value error. | |
| 569 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket")), nil, true}, | |
| 570 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("SSE-C")), nil, true}, | |
| 571 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("DUPLICATE")), nil, true}, | |
| 572 | − } | |
| 573 | − | |
| 574 | − for i, testCase := range testCases { | |
| 575 | − result, err := newStringEqualsFunc(testCase.key, testCase.values) | |
| 576 | − expectErr := (err != nil) | |
| 577 | − | |
| 578 | − if expectErr != testCase.expectErr { | |
| 579 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 580 | − } | |
| 581 | − | |
| 582 | − if !testCase.expectErr { | |
| 583 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 584 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 585 | − } | |
| 586 | − } | |
| 587 | − } | |
| 588 | −} | |
| 589 | − | |
| 590 | −func TestNewStringNotEqualsFunc(t *testing.T) { | |
| 591 | − case1Function, err := newStringNotEqualsFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 592 | − if err != nil { | |
| 593 | − t.Fatalf("unexpected error. %v\n", err) | |
| 594 | − } | |
| 595 | − | |
| 596 | − case2Function, err := newStringNotEqualsFunc(S3XAmzCopySource, | |
| 597 | − NewValueSet( | |
| 598 | − NewStringValue("mybucket/myobject"), | |
| 599 | − NewStringValue("yourbucket/myobject"), | |
| 600 | − ), | |
| 601 | − ) | |
| 602 | − if err != nil { | |
| 603 | − t.Fatalf("unexpected error. %v\n", err) | |
| 604 | − } | |
| 605 | − | |
| 606 | − case3Function, err := newStringNotEqualsFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 607 | − if err != nil { | |
| 608 | − t.Fatalf("unexpected error. %v\n", err) | |
| 609 | − } | |
| 610 | − | |
| 611 | − case4Function, err := newStringNotEqualsFunc(S3XAmzServerSideEncryption, | |
| 612 | − NewValueSet( | |
| 613 | − NewStringValue("AES256"), | |
| 614 | − ), | |
| 615 | − ) | |
| 616 | − if err != nil { | |
| 617 | − t.Fatalf("unexpected error. %v\n", err) | |
| 618 | − } | |
| 619 | − | |
| 620 | − case5Function, err := newStringNotEqualsFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 621 | − if err != nil { | |
| 622 | − t.Fatalf("unexpected error. %v\n", err) | |
| 623 | − } | |
| 624 | − | |
| 625 | − case6Function, err := newStringNotEqualsFunc(S3XAmzMetadataDirective, | |
| 626 | − NewValueSet( | |
| 627 | − NewStringValue("REPLACE"), | |
| 628 | − NewStringValue("COPY"), | |
| 629 | − ), | |
| 630 | − ) | |
| 631 | − if err != nil { | |
| 632 | − t.Fatalf("unexpected error. %v\n", err) | |
| 633 | − } | |
| 634 | − | |
| 635 | − case7Function, err := newStringNotEqualsFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 636 | − if err != nil { | |
| 637 | − t.Fatalf("unexpected error. %v\n", err) | |
| 638 | − } | |
| 639 | − | |
| 640 | − case8Function, err := newStringNotEqualsFunc(S3LocationConstraint, | |
| 641 | − NewValueSet( | |
| 642 | − NewStringValue("eu-west-1"), | |
| 643 | − NewStringValue("us-west-1"), | |
| 644 | − ), | |
| 645 | − ) | |
| 646 | − if err != nil { | |
| 647 | − t.Fatalf("unexpected error. %v\n", err) | |
| 648 | − } | |
| 649 | − | |
| 650 | − testCases := []struct { | |
| 651 | − key Key | |
| 652 | − values ValueSet | |
| 653 | − expectedResult Function | |
| 654 | − expectErr bool | |
| 655 | − }{ | |
| 656 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject")), case1Function, false}, | |
| 657 | − {S3XAmzCopySource, | |
| 658 | − NewValueSet( | |
| 659 | − NewStringValue("mybucket/myobject"), | |
| 660 | − NewStringValue("yourbucket/myobject"), | |
| 661 | − ), case2Function, false}, | |
| 662 | − | |
| 663 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256")), case3Function, false}, | |
| 664 | − {S3XAmzServerSideEncryption, | |
| 665 | − NewValueSet( | |
| 666 | − NewStringValue("AES256"), | |
| 667 | − ), case4Function, false}, | |
| 668 | − | |
| 669 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE")), case5Function, false}, | |
| 670 | − {S3XAmzMetadataDirective, | |
| 671 | − NewValueSet( | |
| 672 | − NewStringValue("REPLACE"), | |
| 673 | − NewStringValue("COPY"), | |
| 674 | − ), case6Function, false}, | |
| 675 | − | |
| 676 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1")), case7Function, false}, | |
| 677 | − {S3LocationConstraint, | |
| 678 | − NewValueSet( | |
| 679 | − NewStringValue("eu-west-1"), | |
| 680 | − NewStringValue("us-west-1"), | |
| 681 | − ), case8Function, false}, | |
| 682 | − | |
| 683 | − // Unsupported value error. | |
| 684 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"), NewIntValue(7)), nil, true}, | |
| 685 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"), NewIntValue(7)), nil, true}, | |
| 686 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"), NewIntValue(7)), nil, true}, | |
| 687 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"), NewIntValue(7)), nil, true}, | |
| 688 | − | |
| 689 | − // Invalid value error. | |
| 690 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket")), nil, true}, | |
| 691 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("SSE-C")), nil, true}, | |
| 692 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("DUPLICATE")), nil, true}, | |
| 693 | − } | |
| 694 | − | |
| 695 | − for i, testCase := range testCases { | |
| 696 | − result, err := newStringNotEqualsFunc(testCase.key, testCase.values) | |
| 697 | − expectErr := (err != nil) | |
| 698 | − | |
| 699 | − if expectErr != testCase.expectErr { | |
| 700 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 701 | − } | |
| 702 | − | |
| 703 | − if !testCase.expectErr { | |
| 704 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 705 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 706 | − } | |
| 707 | − } | |
| 708 | − } | |
| 709 | −} | |
| @@ -1,161 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "sort" | |
| 24 | − "strings" | |
| 25 | − | |
| 26 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 27 | −) | |
| 28 | − | |
| 29 | −func toStringEqualsIgnoreCaseFuncString(n name, key Key, values set.StringSet) string { | |
| 30 | − valueStrings := values.ToSlice() | |
| 31 | − sort.Strings(valueStrings) | |
| 32 | − | |
| 33 | − return fmt.Sprintf("%v:%v:%v", n, key, valueStrings) | |
| 34 | −} | |
| 35 | − | |
| 36 | −// stringEqualsIgnoreCaseFunc - String equals function. It checks whether value by Key in given | |
| 37 | −// values map is in condition values. | |
| 38 | −// For example, | |
| 39 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 40 | −// in value map for Key is in values. | |
| 41 | −type stringEqualsIgnoreCaseFunc struct { | |
| 42 | − k Key | |
| 43 | − values set.StringSet | |
| 44 | −} | |
| 45 | − | |
| 46 | −// evaluate() - evaluates to check whether value by Key in given values is in | |
| 47 | −// condition values, ignores case. | |
| 48 | −func (f stringEqualsIgnoreCaseFunc) evaluate(values map[string][]string) bool { | |
| 49 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 50 | − if !ok { | |
| 51 | − requestValue = values[f.k.Name()] | |
| 52 | − } | |
| 53 | − | |
| 54 | − fvalues := f.values.ApplyFunc(substFuncFromValues(values)) | |
| 55 | − | |
| 56 | − for _, v := range requestValue { | |
| 57 | − if !fvalues.FuncMatch(strings.EqualFold, v).IsEmpty() { | |
| 58 | − return true | |
| 59 | − } | |
| 60 | − } | |
| 61 | − | |
| 62 | − return false | |
| 63 | −} | |
| 64 | − | |
| 65 | −// key() - returns condition key which is used by this condition function. | |
| 66 | −func (f stringEqualsIgnoreCaseFunc) key() Key { | |
| 67 | − return f.k | |
| 68 | −} | |
| 69 | − | |
| 70 | −// name() - returns "StringEqualsIgnoreCase" condition name. | |
| 71 | −func (f stringEqualsIgnoreCaseFunc) name() name { | |
| 72 | − return stringEqualsIgnoreCase | |
| 73 | −} | |
| 74 | − | |
| 75 | −func (f stringEqualsIgnoreCaseFunc) String() string { | |
| 76 | − return toStringEqualsIgnoreCaseFuncString(stringEqualsIgnoreCase, f.k, f.values) | |
| 77 | −} | |
| 78 | − | |
| 79 | −// toMap - returns map representation of this function. | |
| 80 | −func (f stringEqualsIgnoreCaseFunc) toMap() map[Key]ValueSet { | |
| 81 | − if !f.k.IsValid() { | |
| 82 | − return nil | |
| 83 | − } | |
| 84 | − | |
| 85 | − values := NewValueSet() | |
| 86 | − for _, value := range f.values.ToSlice() { | |
| 87 | − values.Add(NewStringValue(value)) | |
| 88 | − } | |
| 89 | − | |
| 90 | − return map[Key]ValueSet{ | |
| 91 | − f.k: values, | |
| 92 | − } | |
| 93 | −} | |
| 94 | − | |
| 95 | −// stringNotEqualsIgnoreCaseFunc - String not equals function. It checks whether value by Key in | |
| 96 | −// given values is NOT in condition values. | |
| 97 | −// For example, | |
| 98 | −// - if values = ["mybucket/foo"], at evaluate() it returns whether string | |
| 99 | −// in value map for Key is NOT in values. | |
| 100 | −type stringNotEqualsIgnoreCaseFunc struct { | |
| 101 | − stringEqualsIgnoreCaseFunc | |
| 102 | −} | |
| 103 | − | |
| 104 | −// evaluate() - evaluates to check whether value by Key in given values is NOT in | |
| 105 | −// condition values. | |
| 106 | −func (f stringNotEqualsIgnoreCaseFunc) evaluate(values map[string][]string) bool { | |
| 107 | − return !f.stringEqualsIgnoreCaseFunc.evaluate(values) | |
| 108 | −} | |
| 109 | − | |
| 110 | −// name() - returns "StringNotEqualsIgnoreCase" condition name. | |
| 111 | −func (f stringNotEqualsIgnoreCaseFunc) name() name { | |
| 112 | − return stringNotEqualsIgnoreCase | |
| 113 | −} | |
| 114 | − | |
| 115 | −func (f stringNotEqualsIgnoreCaseFunc) String() string { | |
| 116 | − return toStringEqualsIgnoreCaseFuncString(stringNotEqualsIgnoreCase, f.stringEqualsIgnoreCaseFunc.k, f.stringEqualsIgnoreCaseFunc.values) | |
| 117 | −} | |
| 118 | − | |
| 119 | −func validateStringEqualsIgnoreCaseValues(n name, key Key, values set.StringSet) error { | |
| 120 | − return validateStringEqualsValues(n, key, values) | |
| 121 | −} | |
| 122 | − | |
| 123 | −// newStringEqualsIgnoreCaseFunc - returns new StringEqualsIgnoreCase function. | |
| 124 | −func newStringEqualsIgnoreCaseFunc(key Key, values ValueSet) (Function, error) { | |
| 125 | − valueStrings, err := valuesToStringSlice(stringEqualsIgnoreCase, values) | |
| 126 | − if err != nil { | |
| 127 | − return nil, err | |
| 128 | − } | |
| 129 | − | |
| 130 | − return NewStringEqualsIgnoreCaseFunc(key, valueStrings...) | |
| 131 | −} | |
| 132 | − | |
| 133 | −// NewStringEqualsIgnoreCaseFunc - returns new StringEqualsIgnoreCase function. | |
| 134 | −func NewStringEqualsIgnoreCaseFunc(key Key, values ...string) (Function, error) { | |
| 135 | − sset := set.CreateStringSet(values...) | |
| 136 | − if err := validateStringEqualsIgnoreCaseValues(stringEqualsIgnoreCase, key, sset); err != nil { | |
| 137 | − return nil, err | |
| 138 | − } | |
| 139 | − | |
| 140 | − return &stringEqualsIgnoreCaseFunc{key, sset}, nil | |
| 141 | −} | |
| 142 | − | |
| 143 | −// newStringNotEqualsIgnoreCaseFunc - returns new StringNotEqualsIgnoreCase function. | |
| 144 | −func newStringNotEqualsIgnoreCaseFunc(key Key, values ValueSet) (Function, error) { | |
| 145 | − valueStrings, err := valuesToStringSlice(stringNotEqualsIgnoreCase, values) | |
| 146 | − if err != nil { | |
| 147 | − return nil, err | |
| 148 | − } | |
| 149 | − | |
| 150 | − return NewStringNotEqualsIgnoreCaseFunc(key, valueStrings...) | |
| 151 | −} | |
| 152 | − | |
| 153 | −// NewStringNotEqualsIgnoreCaseFunc - returns new StringNotEqualsIgnoreCase function. | |
| 154 | −func NewStringNotEqualsIgnoreCaseFunc(key Key, values ...string) (Function, error) { | |
| 155 | − sset := set.CreateStringSet(values...) | |
| 156 | − if err := validateStringEqualsIgnoreCaseValues(stringNotEqualsIgnoreCase, key, sset); err != nil { | |
| 157 | − return nil, err | |
| 158 | − } | |
| 159 | − | |
| 160 | − return &stringNotEqualsIgnoreCaseFunc{stringEqualsIgnoreCaseFunc{key, sset}}, nil | |
| 161 | −} | |
| @@ -1,711 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "reflect" | |
| 22 | − "testing" | |
| 23 | −) | |
| 24 | − | |
| 25 | −func TestStringEqualsIgnoreCaseFuncEvaluate(t *testing.T) { | |
| 26 | − case1Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 27 | − if err != nil { | |
| 28 | − t.Fatalf("unexpected error. %v\n", err) | |
| 29 | − } | |
| 30 | − | |
| 31 | − case2Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 32 | − if err != nil { | |
| 33 | − t.Fatalf("unexpected error. %v\n", err) | |
| 34 | − } | |
| 35 | − | |
| 36 | − case3Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 37 | − if err != nil { | |
| 38 | − t.Fatalf("unexpected error. %v\n", err) | |
| 39 | − } | |
| 40 | − | |
| 41 | − case4Function, err := newStringEqualsIgnoreCaseFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 42 | − if err != nil { | |
| 43 | − t.Fatalf("unexpected error. %v\n", err) | |
| 44 | − } | |
| 45 | − | |
| 46 | − testCases := []struct { | |
| 47 | − function Function | |
| 48 | − values map[string][]string | |
| 49 | − expectedResult bool | |
| 50 | − }{ | |
| 51 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, true}, | |
| 52 | − {case1Function, map[string][]string{"x-amz-copy-source": {"yourbucket/myobject"}}, false}, | |
| 53 | − {case1Function, map[string][]string{}, false}, | |
| 54 | − {case1Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 55 | − | |
| 56 | − {case2Function, map[string][]string{"x-amz-server-side-encryption": {"AES256"}}, true}, | |
| 57 | − {case2Function, map[string][]string{"x-amz-server-side-encryption": {"aes256"}}, true}, | |
| 58 | − {case2Function, map[string][]string{}, false}, | |
| 59 | − {case2Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 60 | − | |
| 61 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE"}}, true}, | |
| 62 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"replace"}}, true}, | |
| 63 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"COPY"}}, false}, | |
| 64 | − {case3Function, map[string][]string{}, false}, | |
| 65 | − {case3Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 66 | − | |
| 67 | − {case4Function, map[string][]string{"LocationConstraint": {"eu-west-1"}}, true}, | |
| 68 | − {case4Function, map[string][]string{"LocationConstraint": {"us-east-1"}}, false}, | |
| 69 | − {case4Function, map[string][]string{}, false}, | |
| 70 | − {case4Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 71 | − } | |
| 72 | − | |
| 73 | − for i, testCase := range testCases { | |
| 74 | − result := testCase.function.evaluate(testCase.values) | |
| 75 | − | |
| 76 | − if result != testCase.expectedResult { | |
| 77 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 78 | − } | |
| 79 | − } | |
| 80 | −} | |
| 81 | − | |
| 82 | −func TestStringEqualsIgnoreCaseFuncKey(t *testing.T) { | |
| 83 | − case1Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 84 | − if err != nil { | |
| 85 | − t.Fatalf("unexpected error. %v\n", err) | |
| 86 | − } | |
| 87 | − | |
| 88 | − case2Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 89 | − if err != nil { | |
| 90 | − t.Fatalf("unexpected error. %v\n", err) | |
| 91 | − } | |
| 92 | − | |
| 93 | − case3Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 94 | − if err != nil { | |
| 95 | − t.Fatalf("unexpected error. %v\n", err) | |
| 96 | − } | |
| 97 | − | |
| 98 | − case4Function, err := newStringEqualsIgnoreCaseFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 99 | − if err != nil { | |
| 100 | − t.Fatalf("unexpected error. %v\n", err) | |
| 101 | − } | |
| 102 | − | |
| 103 | − testCases := []struct { | |
| 104 | − function Function | |
| 105 | − expectedResult Key | |
| 106 | − }{ | |
| 107 | − {case1Function, S3XAmzCopySource}, | |
| 108 | − {case2Function, S3XAmzServerSideEncryption}, | |
| 109 | − {case3Function, S3XAmzMetadataDirective}, | |
| 110 | − {case4Function, S3LocationConstraint}, | |
| 111 | − } | |
| 112 | − | |
| 113 | − for i, testCase := range testCases { | |
| 114 | − result := testCase.function.key() | |
| 115 | − | |
| 116 | − if result != testCase.expectedResult { | |
| 117 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 118 | − } | |
| 119 | − } | |
| 120 | −} | |
| 121 | − | |
| 122 | −func TestStringEqualsIgnoreCaseFuncToMap(t *testing.T) { | |
| 123 | − case1Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 124 | − if err != nil { | |
| 125 | − t.Fatalf("unexpected error. %v\n", err) | |
| 126 | − } | |
| 127 | − | |
| 128 | − case1Result := map[Key]ValueSet{ | |
| 129 | − S3XAmzCopySource: NewValueSet(NewStringValue("mybucket/myobject")), | |
| 130 | − } | |
| 131 | − | |
| 132 | − case2Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzCopySource, | |
| 133 | − NewValueSet( | |
| 134 | − NewStringValue("mybucket/myobject"), | |
| 135 | − NewStringValue("yourbucket/myobject"), | |
| 136 | − ), | |
| 137 | − ) | |
| 138 | − if err != nil { | |
| 139 | − t.Fatalf("unexpected error. %v\n", err) | |
| 140 | − } | |
| 141 | − | |
| 142 | − case2Result := map[Key]ValueSet{ | |
| 143 | − S3XAmzCopySource: NewValueSet( | |
| 144 | − NewStringValue("mybucket/myobject"), | |
| 145 | − NewStringValue("yourbucket/myobject"), | |
| 146 | − ), | |
| 147 | − } | |
| 148 | − | |
| 149 | − case3Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 150 | − if err != nil { | |
| 151 | − t.Fatalf("unexpected error. %v\n", err) | |
| 152 | − } | |
| 153 | − | |
| 154 | − case3Result := map[Key]ValueSet{ | |
| 155 | − S3XAmzServerSideEncryption: NewValueSet(NewStringValue("AES256")), | |
| 156 | − } | |
| 157 | − | |
| 158 | − case4Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, | |
| 159 | − NewValueSet( | |
| 160 | − NewStringValue("AES256"), | |
| 161 | − ), | |
| 162 | − ) | |
| 163 | − if err != nil { | |
| 164 | − t.Fatalf("unexpected error. %v\n", err) | |
| 165 | − } | |
| 166 | − | |
| 167 | − case4Result := map[Key]ValueSet{ | |
| 168 | − S3XAmzServerSideEncryption: NewValueSet( | |
| 169 | − NewStringValue("AES256"), | |
| 170 | − ), | |
| 171 | − } | |
| 172 | − | |
| 173 | − case5Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 174 | − if err != nil { | |
| 175 | − t.Fatalf("unexpected error. %v\n", err) | |
| 176 | − } | |
| 177 | − | |
| 178 | − case5Result := map[Key]ValueSet{ | |
| 179 | − S3XAmzMetadataDirective: NewValueSet(NewStringValue("REPLACE")), | |
| 180 | − } | |
| 181 | − | |
| 182 | − case6Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, | |
| 183 | − NewValueSet( | |
| 184 | − NewStringValue("REPLACE"), | |
| 185 | − NewStringValue("COPY"), | |
| 186 | − ), | |
| 187 | − ) | |
| 188 | − if err != nil { | |
| 189 | − t.Fatalf("unexpected error. %v\n", err) | |
| 190 | − } | |
| 191 | − | |
| 192 | − case6Result := map[Key]ValueSet{ | |
| 193 | − S3XAmzMetadataDirective: NewValueSet( | |
| 194 | − NewStringValue("REPLACE"), | |
| 195 | − NewStringValue("COPY"), | |
| 196 | − ), | |
| 197 | − } | |
| 198 | − | |
| 199 | − case7Function, err := newStringEqualsIgnoreCaseFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 200 | − if err != nil { | |
| 201 | − t.Fatalf("unexpected error. %v\n", err) | |
| 202 | − } | |
| 203 | − | |
| 204 | − case7Result := map[Key]ValueSet{ | |
| 205 | − S3LocationConstraint: NewValueSet(NewStringValue("eu-west-1")), | |
| 206 | − } | |
| 207 | − | |
| 208 | − case8Function, err := newStringEqualsIgnoreCaseFunc(S3LocationConstraint, | |
| 209 | − NewValueSet( | |
| 210 | − NewStringValue("eu-west-1"), | |
| 211 | − NewStringValue("us-west-1"), | |
| 212 | − ), | |
| 213 | − ) | |
| 214 | − if err != nil { | |
| 215 | − t.Fatalf("unexpected error. %v\n", err) | |
| 216 | − } | |
| 217 | − | |
| 218 | − case8Result := map[Key]ValueSet{ | |
| 219 | − S3LocationConstraint: NewValueSet( | |
| 220 | − NewStringValue("eu-west-1"), | |
| 221 | − NewStringValue("us-west-1"), | |
| 222 | − ), | |
| 223 | − } | |
| 224 | − | |
| 225 | − testCases := []struct { | |
| 226 | − f Function | |
| 227 | − expectedResult map[Key]ValueSet | |
| 228 | − }{ | |
| 229 | − {case1Function, case1Result}, | |
| 230 | − {case2Function, case2Result}, | |
| 231 | − {case3Function, case3Result}, | |
| 232 | − {case4Function, case4Result}, | |
| 233 | − {case5Function, case5Result}, | |
| 234 | − {case6Function, case6Result}, | |
| 235 | − {case7Function, case7Result}, | |
| 236 | − {case8Function, case8Result}, | |
| 237 | − {&stringEqualsFunc{}, nil}, | |
| 238 | − } | |
| 239 | − | |
| 240 | − for i, testCase := range testCases { | |
| 241 | − result := testCase.f.toMap() | |
| 242 | − | |
| 243 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 244 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 245 | − } | |
| 246 | − } | |
| 247 | −} | |
| 248 | − | |
| 249 | −func TestStringNotEqualsIgnoreCaseFuncEvaluate(t *testing.T) { | |
| 250 | − case1Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 251 | − if err != nil { | |
| 252 | − t.Fatalf("unexpected error. %v\n", err) | |
| 253 | − } | |
| 254 | − | |
| 255 | − case2Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 256 | − if err != nil { | |
| 257 | − t.Fatalf("unexpected error. %v\n", err) | |
| 258 | − } | |
| 259 | − | |
| 260 | − case3Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 261 | − if err != nil { | |
| 262 | − t.Fatalf("unexpected error. %v\n", err) | |
| 263 | − } | |
| 264 | − | |
| 265 | − case4Function, err := newStringNotEqualsIgnoreCaseFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 266 | − if err != nil { | |
| 267 | − t.Fatalf("unexpected error. %v\n", err) | |
| 268 | − } | |
| 269 | − | |
| 270 | − testCases := []struct { | |
| 271 | − function Function | |
| 272 | − values map[string][]string | |
| 273 | − expectedResult bool | |
| 274 | − }{ | |
| 275 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, false}, | |
| 276 | − {case1Function, map[string][]string{"x-amz-copy-source": {"yourbucket/myobject"}}, true}, | |
| 277 | − {case1Function, map[string][]string{}, true}, | |
| 278 | − {case1Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 279 | − | |
| 280 | − {case2Function, map[string][]string{"x-amz-server-side-encryption": {"AES256"}}, false}, | |
| 281 | − {case2Function, map[string][]string{}, true}, | |
| 282 | − {case2Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 283 | − | |
| 284 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE"}}, false}, | |
| 285 | − {case3Function, map[string][]string{"x-amz-metadata-directive": {"COPY"}}, true}, | |
| 286 | − {case3Function, map[string][]string{}, true}, | |
| 287 | − {case3Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 288 | − | |
| 289 | − {case4Function, map[string][]string{"LocationConstraint": {"eu-west-1"}}, false}, | |
| 290 | − {case4Function, map[string][]string{"LocationConstraint": {"us-east-1"}}, true}, | |
| 291 | − {case4Function, map[string][]string{}, true}, | |
| 292 | − {case4Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 293 | − } | |
| 294 | − | |
| 295 | − for i, testCase := range testCases { | |
| 296 | − result := testCase.function.evaluate(testCase.values) | |
| 297 | − | |
| 298 | − if result != testCase.expectedResult { | |
| 299 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 300 | − } | |
| 301 | − } | |
| 302 | −} | |
| 303 | − | |
| 304 | −func TestStringNotEqualsIgnoreCaseFuncKey(t *testing.T) { | |
| 305 | − case1Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 306 | − if err != nil { | |
| 307 | − t.Fatalf("unexpected error. %v\n", err) | |
| 308 | − } | |
| 309 | − | |
| 310 | − case2Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 311 | − if err != nil { | |
| 312 | − t.Fatalf("unexpected error. %v\n", err) | |
| 313 | − } | |
| 314 | − | |
| 315 | − case3Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 316 | − if err != nil { | |
| 317 | − t.Fatalf("unexpected error. %v\n", err) | |
| 318 | − } | |
| 319 | − | |
| 320 | − case4Function, err := newStringNotEqualsIgnoreCaseFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 321 | − if err != nil { | |
| 322 | − t.Fatalf("unexpected error. %v\n", err) | |
| 323 | − } | |
| 324 | − | |
| 325 | − testCases := []struct { | |
| 326 | − function Function | |
| 327 | − expectedResult Key | |
| 328 | − }{ | |
| 329 | − {case1Function, S3XAmzCopySource}, | |
| 330 | − {case2Function, S3XAmzServerSideEncryption}, | |
| 331 | − {case3Function, S3XAmzMetadataDirective}, | |
| 332 | − {case4Function, S3LocationConstraint}, | |
| 333 | − } | |
| 334 | − | |
| 335 | − for i, testCase := range testCases { | |
| 336 | − result := testCase.function.key() | |
| 337 | − | |
| 338 | − if result != testCase.expectedResult { | |
| 339 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 340 | − } | |
| 341 | − } | |
| 342 | −} | |
| 343 | − | |
| 344 | −func TestStringNotEqualsIgnoreCaseFuncToMap(t *testing.T) { | |
| 345 | − case1Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 346 | − if err != nil { | |
| 347 | − t.Fatalf("unexpected error. %v\n", err) | |
| 348 | − } | |
| 349 | − | |
| 350 | − case1Result := map[Key]ValueSet{ | |
| 351 | − S3XAmzCopySource: NewValueSet(NewStringValue("mybucket/myobject")), | |
| 352 | − } | |
| 353 | − | |
| 354 | − case2Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzCopySource, | |
| 355 | − NewValueSet( | |
| 356 | − NewStringValue("mybucket/myobject"), | |
| 357 | − NewStringValue("yourbucket/myobject"), | |
| 358 | − ), | |
| 359 | − ) | |
| 360 | − if err != nil { | |
| 361 | − t.Fatalf("unexpected error. %v\n", err) | |
| 362 | − } | |
| 363 | − | |
| 364 | − case2Result := map[Key]ValueSet{ | |
| 365 | − S3XAmzCopySource: NewValueSet( | |
| 366 | − NewStringValue("mybucket/myobject"), | |
| 367 | − NewStringValue("yourbucket/myobject"), | |
| 368 | − ), | |
| 369 | − } | |
| 370 | − | |
| 371 | − case3Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 372 | − if err != nil { | |
| 373 | − t.Fatalf("unexpected error. %v\n", err) | |
| 374 | − } | |
| 375 | − | |
| 376 | − case3Result := map[Key]ValueSet{ | |
| 377 | − S3XAmzServerSideEncryption: NewValueSet(NewStringValue("AES256")), | |
| 378 | − } | |
| 379 | − | |
| 380 | − case4Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, | |
| 381 | − NewValueSet( | |
| 382 | − NewStringValue("AES256"), | |
| 383 | − ), | |
| 384 | − ) | |
| 385 | − if err != nil { | |
| 386 | − t.Fatalf("unexpected error. %v\n", err) | |
| 387 | − } | |
| 388 | − | |
| 389 | − case4Result := map[Key]ValueSet{ | |
| 390 | − S3XAmzServerSideEncryption: NewValueSet( | |
| 391 | − NewStringValue("AES256"), | |
| 392 | − ), | |
| 393 | − } | |
| 394 | − | |
| 395 | − case5Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 396 | − if err != nil { | |
| 397 | − t.Fatalf("unexpected error. %v\n", err) | |
| 398 | − } | |
| 399 | − | |
| 400 | − case5Result := map[Key]ValueSet{ | |
| 401 | − S3XAmzMetadataDirective: NewValueSet(NewStringValue("REPLACE")), | |
| 402 | − } | |
| 403 | − | |
| 404 | − case6Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, | |
| 405 | − NewValueSet( | |
| 406 | − NewStringValue("REPLACE"), | |
| 407 | − NewStringValue("COPY"), | |
| 408 | − ), | |
| 409 | − ) | |
| 410 | − if err != nil { | |
| 411 | − t.Fatalf("unexpected error. %v\n", err) | |
| 412 | − } | |
| 413 | − | |
| 414 | − case6Result := map[Key]ValueSet{ | |
| 415 | − S3XAmzMetadataDirective: NewValueSet( | |
| 416 | − NewStringValue("REPLACE"), | |
| 417 | − NewStringValue("COPY"), | |
| 418 | − ), | |
| 419 | − } | |
| 420 | − | |
| 421 | − case7Function, err := newStringNotEqualsIgnoreCaseFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 422 | − if err != nil { | |
| 423 | − t.Fatalf("unexpected error. %v\n", err) | |
| 424 | − } | |
| 425 | − | |
| 426 | − case7Result := map[Key]ValueSet{ | |
| 427 | − S3LocationConstraint: NewValueSet(NewStringValue("eu-west-1")), | |
| 428 | − } | |
| 429 | − | |
| 430 | − case8Function, err := newStringNotEqualsIgnoreCaseFunc(S3LocationConstraint, | |
| 431 | − NewValueSet( | |
| 432 | − NewStringValue("eu-west-1"), | |
| 433 | − NewStringValue("us-west-1"), | |
| 434 | − ), | |
| 435 | − ) | |
| 436 | − if err != nil { | |
| 437 | − t.Fatalf("unexpected error. %v\n", err) | |
| 438 | − } | |
| 439 | − | |
| 440 | − case8Result := map[Key]ValueSet{ | |
| 441 | − S3LocationConstraint: NewValueSet( | |
| 442 | − NewStringValue("eu-west-1"), | |
| 443 | − NewStringValue("us-west-1"), | |
| 444 | − ), | |
| 445 | − } | |
| 446 | − | |
| 447 | − testCases := []struct { | |
| 448 | − f Function | |
| 449 | − expectedResult map[Key]ValueSet | |
| 450 | − }{ | |
| 451 | − {case1Function, case1Result}, | |
| 452 | − {case2Function, case2Result}, | |
| 453 | − {case3Function, case3Result}, | |
| 454 | − {case4Function, case4Result}, | |
| 455 | − {case5Function, case5Result}, | |
| 456 | − {case6Function, case6Result}, | |
| 457 | − {case7Function, case7Result}, | |
| 458 | − {case8Function, case8Result}, | |
| 459 | − {&stringNotEqualsFunc{}, nil}, | |
| 460 | − } | |
| 461 | − | |
| 462 | − for i, testCase := range testCases { | |
| 463 | − result := testCase.f.toMap() | |
| 464 | − | |
| 465 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 466 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 467 | − } | |
| 468 | − } | |
| 469 | −} | |
| 470 | − | |
| 471 | −func TestNewStringEqualsIgnoreCaseFunc(t *testing.T) { | |
| 472 | − case1Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 473 | − if err != nil { | |
| 474 | − t.Fatalf("unexpected error. %v\n", err) | |
| 475 | − } | |
| 476 | − | |
| 477 | − case2Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzCopySource, | |
| 478 | − NewValueSet( | |
| 479 | − NewStringValue("mybucket/myobject"), | |
| 480 | − NewStringValue("yourbucket/myobject"), | |
| 481 | − ), | |
| 482 | − ) | |
| 483 | − if err != nil { | |
| 484 | − t.Fatalf("unexpected error. %v\n", err) | |
| 485 | − } | |
| 486 | − | |
| 487 | − case3Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 488 | − if err != nil { | |
| 489 | − t.Fatalf("unexpected error. %v\n", err) | |
| 490 | − } | |
| 491 | − | |
| 492 | − case4Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, | |
| 493 | − NewValueSet( | |
| 494 | − NewStringValue("AES256"), | |
| 495 | − ), | |
| 496 | − ) | |
| 497 | − if err != nil { | |
| 498 | − t.Fatalf("unexpected error. %v\n", err) | |
| 499 | − } | |
| 500 | − | |
| 501 | − case5Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 502 | − if err != nil { | |
| 503 | − t.Fatalf("unexpected error. %v\n", err) | |
| 504 | − } | |
| 505 | − | |
| 506 | − case6Function, err := newStringEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, | |
| 507 | − NewValueSet( | |
| 508 | − NewStringValue("REPLACE"), | |
| 509 | − NewStringValue("COPY"), | |
| 510 | − ), | |
| 511 | − ) | |
| 512 | − if err != nil { | |
| 513 | − t.Fatalf("unexpected error. %v\n", err) | |
| 514 | − } | |
| 515 | − | |
| 516 | − case7Function, err := newStringEqualsIgnoreCaseFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 517 | − if err != nil { | |
| 518 | − t.Fatalf("unexpected error. %v\n", err) | |
| 519 | − } | |
| 520 | − | |
| 521 | − case8Function, err := newStringEqualsIgnoreCaseFunc(S3LocationConstraint, | |
| 522 | − NewValueSet( | |
| 523 | − NewStringValue("eu-west-1"), | |
| 524 | − NewStringValue("us-west-1"), | |
| 525 | − ), | |
| 526 | − ) | |
| 527 | − if err != nil { | |
| 528 | − t.Fatalf("unexpected error. %v\n", err) | |
| 529 | − } | |
| 530 | − | |
| 531 | − testCases := []struct { | |
| 532 | − key Key | |
| 533 | − values ValueSet | |
| 534 | − expectedResult Function | |
| 535 | − expectErr bool | |
| 536 | − }{ | |
| 537 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject")), case1Function, false}, | |
| 538 | − {S3XAmzCopySource, | |
| 539 | − NewValueSet( | |
| 540 | − NewStringValue("mybucket/myobject"), | |
| 541 | − NewStringValue("yourbucket/myobject"), | |
| 542 | − ), case2Function, false}, | |
| 543 | − | |
| 544 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256")), case3Function, false}, | |
| 545 | − {S3XAmzServerSideEncryption, | |
| 546 | − NewValueSet( | |
| 547 | − NewStringValue("AES256"), | |
| 548 | − ), case4Function, false}, | |
| 549 | − | |
| 550 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE")), case5Function, false}, | |
| 551 | − {S3XAmzMetadataDirective, | |
| 552 | − NewValueSet( | |
| 553 | − NewStringValue("REPLACE"), | |
| 554 | − NewStringValue("COPY"), | |
| 555 | − ), case6Function, false}, | |
| 556 | − | |
| 557 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1")), case7Function, false}, | |
| 558 | − {S3LocationConstraint, | |
| 559 | − NewValueSet( | |
| 560 | − NewStringValue("eu-west-1"), | |
| 561 | − NewStringValue("us-west-1"), | |
| 562 | − ), case8Function, false}, | |
| 563 | − | |
| 564 | − // Unsupported value error. | |
| 565 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"), NewIntValue(7)), nil, true}, | |
| 566 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"), NewIntValue(7)), nil, true}, | |
| 567 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"), NewIntValue(7)), nil, true}, | |
| 568 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"), NewIntValue(7)), nil, true}, | |
| 569 | − | |
| 570 | − // Invalid value error. | |
| 571 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket")), nil, true}, | |
| 572 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("SSE-C")), nil, true}, | |
| 573 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("DUPLICATE")), nil, true}, | |
| 574 | − } | |
| 575 | − | |
| 576 | − for i, testCase := range testCases { | |
| 577 | − result, err := newStringEqualsIgnoreCaseFunc(testCase.key, testCase.values) | |
| 578 | − expectErr := (err != nil) | |
| 579 | − | |
| 580 | − if expectErr != testCase.expectErr { | |
| 581 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 582 | − } | |
| 583 | − | |
| 584 | − if !testCase.expectErr { | |
| 585 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 586 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 587 | − } | |
| 588 | − } | |
| 589 | − } | |
| 590 | −} | |
| 591 | − | |
| 592 | −func TestNewStringNotEqualsIgnoreCaseFunc(t *testing.T) { | |
| 593 | − case1Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 594 | − if err != nil { | |
| 595 | − t.Fatalf("unexpected error. %v\n", err) | |
| 596 | − } | |
| 597 | − | |
| 598 | − case2Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzCopySource, | |
| 599 | − NewValueSet( | |
| 600 | − NewStringValue("mybucket/myobject"), | |
| 601 | − NewStringValue("yourbucket/myobject"), | |
| 602 | − ), | |
| 603 | − ) | |
| 604 | − if err != nil { | |
| 605 | − t.Fatalf("unexpected error. %v\n", err) | |
| 606 | − } | |
| 607 | − | |
| 608 | − case3Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 609 | − if err != nil { | |
| 610 | − t.Fatalf("unexpected error. %v\n", err) | |
| 611 | − } | |
| 612 | − | |
| 613 | − case4Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzServerSideEncryption, | |
| 614 | − NewValueSet( | |
| 615 | − NewStringValue("AES256"), | |
| 616 | − ), | |
| 617 | − ) | |
| 618 | − if err != nil { | |
| 619 | − t.Fatalf("unexpected error. %v\n", err) | |
| 620 | − } | |
| 621 | − | |
| 622 | − case5Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 623 | − if err != nil { | |
| 624 | − t.Fatalf("unexpected error. %v\n", err) | |
| 625 | − } | |
| 626 | − | |
| 627 | − case6Function, err := newStringNotEqualsIgnoreCaseFunc(S3XAmzMetadataDirective, | |
| 628 | − NewValueSet( | |
| 629 | − NewStringValue("REPLACE"), | |
| 630 | − NewStringValue("COPY"), | |
| 631 | − ), | |
| 632 | − ) | |
| 633 | − if err != nil { | |
| 634 | − t.Fatalf("unexpected error. %v\n", err) | |
| 635 | − } | |
| 636 | − | |
| 637 | − case7Function, err := newStringNotEqualsIgnoreCaseFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 638 | − if err != nil { | |
| 639 | − t.Fatalf("unexpected error. %v\n", err) | |
| 640 | − } | |
| 641 | − | |
| 642 | − case8Function, err := newStringNotEqualsIgnoreCaseFunc(S3LocationConstraint, | |
| 643 | − NewValueSet( | |
| 644 | − NewStringValue("eu-west-1"), | |
| 645 | − NewStringValue("us-west-1"), | |
| 646 | − ), | |
| 647 | − ) | |
| 648 | − if err != nil { | |
| 649 | − t.Fatalf("unexpected error. %v\n", err) | |
| 650 | − } | |
| 651 | − | |
| 652 | − testCases := []struct { | |
| 653 | − key Key | |
| 654 | − values ValueSet | |
| 655 | − expectedResult Function | |
| 656 | − expectErr bool | |
| 657 | − }{ | |
| 658 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject")), case1Function, false}, | |
| 659 | − {S3XAmzCopySource, | |
| 660 | − NewValueSet( | |
| 661 | − NewStringValue("mybucket/myobject"), | |
| 662 | − NewStringValue("yourbucket/myobject"), | |
| 663 | − ), case2Function, false}, | |
| 664 | − | |
| 665 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256")), case3Function, false}, | |
| 666 | − {S3XAmzServerSideEncryption, | |
| 667 | − NewValueSet( | |
| 668 | − NewStringValue("AES256"), | |
| 669 | − ), case4Function, false}, | |
| 670 | − | |
| 671 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE")), case5Function, false}, | |
| 672 | − {S3XAmzMetadataDirective, | |
| 673 | − NewValueSet( | |
| 674 | − NewStringValue("REPLACE"), | |
| 675 | − NewStringValue("COPY"), | |
| 676 | − ), case6Function, false}, | |
| 677 | − | |
| 678 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1")), case7Function, false}, | |
| 679 | − {S3LocationConstraint, | |
| 680 | − NewValueSet( | |
| 681 | − NewStringValue("eu-west-1"), | |
| 682 | − NewStringValue("us-west-1"), | |
| 683 | − ), case8Function, false}, | |
| 684 | − | |
| 685 | − // Unsupported value error. | |
| 686 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"), NewIntValue(7)), nil, true}, | |
| 687 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"), NewIntValue(7)), nil, true}, | |
| 688 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"), NewIntValue(7)), nil, true}, | |
| 689 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"), NewIntValue(7)), nil, true}, | |
| 690 | − | |
| 691 | − // Invalid value error. | |
| 692 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket")), nil, true}, | |
| 693 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("SSE-C")), nil, true}, | |
| 694 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("DUPLICATE")), nil, true}, | |
| 695 | − } | |
| 696 | − | |
| 697 | − for i, testCase := range testCases { | |
| 698 | − result, err := newStringNotEqualsIgnoreCaseFunc(testCase.key, testCase.values) | |
| 699 | − expectErr := (err != nil) | |
| 700 | − | |
| 701 | − if expectErr != testCase.expectErr { | |
| 702 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 703 | − } | |
| 704 | − | |
| 705 | − if !testCase.expectErr { | |
| 706 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 707 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 708 | − } | |
| 709 | − } | |
| 710 | − } | |
| 711 | −} | |
| @@ -1,175 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | − "net/http" | |
| 23 | − "sort" | |
| 24 | − | |
| 25 | − "github.com/minio/minio-go/v7/pkg/s3utils" | |
| 26 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 27 | − "github.com/minio/pkg/wildcard" | |
| 28 | −) | |
| 29 | − | |
| 30 | −func toStringLikeFuncString(n name, key Key, values set.StringSet) string { | |
| 31 | − valueStrings := values.ToSlice() | |
| 32 | − sort.Strings(valueStrings) | |
| 33 | − | |
| 34 | − return fmt.Sprintf("%v:%v:%v", n, key, valueStrings) | |
| 35 | −} | |
| 36 | − | |
| 37 | −// stringLikeFunc - String like function. It checks whether value by Key in given | |
| 38 | −// values map is widcard matching in condition values. | |
| 39 | −// For example, | |
| 40 | −// - if values = ["mybucket/foo*"], at evaluate() it returns whether string | |
| 41 | −// in value map for Key is wildcard matching in values. | |
| 42 | −type stringLikeFunc struct { | |
| 43 | − k Key | |
| 44 | − values set.StringSet | |
| 45 | −} | |
| 46 | − | |
| 47 | −// evaluate() - evaluates to check whether value by Key in given values is wildcard | |
| 48 | −// matching in condition values. | |
| 49 | −func (f stringLikeFunc) evaluate(values map[string][]string) bool { | |
| 50 | − requestValue, ok := values[http.CanonicalHeaderKey(f.k.Name())] | |
| 51 | − if !ok { | |
| 52 | − requestValue = values[f.k.Name()] | |
| 53 | − } | |
| 54 | − | |
| 55 | − fvalues := f.values.ApplyFunc(substFuncFromValues(values)) | |
| 56 | − | |
| 57 | − for _, v := range requestValue { | |
| 58 | − if !fvalues.FuncMatch(wildcard.Match, v).IsEmpty() { | |
| 59 | − return true | |
| 60 | − } | |
| 61 | − } | |
| 62 | − | |
| 63 | − return false | |
| 64 | −} | |
| 65 | − | |
| 66 | −// key() - returns condition key which is used by this condition function. | |
| 67 | −func (f stringLikeFunc) key() Key { | |
| 68 | − return f.k | |
| 69 | −} | |
| 70 | − | |
| 71 | −// name() - returns "StringLike" function name. | |
| 72 | −func (f stringLikeFunc) name() name { | |
| 73 | − return stringLike | |
| 74 | −} | |
| 75 | − | |
| 76 | −func (f stringLikeFunc) String() string { | |
| 77 | − return toStringLikeFuncString(stringLike, f.k, f.values) | |
| 78 | −} | |
| 79 | − | |
| 80 | −// toMap - returns map representation of this function. | |
| 81 | −func (f stringLikeFunc) toMap() map[Key]ValueSet { | |
| 82 | − if !f.k.IsValid() { | |
| 83 | − return nil | |
| 84 | − } | |
| 85 | − | |
| 86 | − values := NewValueSet() | |
| 87 | − for _, value := range f.values.ToSlice() { | |
| 88 | − values.Add(NewStringValue(value)) | |
| 89 | − } | |
| 90 | − | |
| 91 | − return map[Key]ValueSet{ | |
| 92 | − f.k: values, | |
| 93 | − } | |
| 94 | −} | |
| 95 | − | |
| 96 | −// stringNotLikeFunc - String not like function. It checks whether value by Key in given | |
| 97 | −// values map is NOT widcard matching in condition values. | |
| 98 | −// For example, | |
| 99 | −// - if values = ["mybucket/foo*"], at evaluate() it returns whether string | |
| 100 | −// in value map for Key is NOT wildcard matching in values. | |
| 101 | −type stringNotLikeFunc struct { | |
| 102 | − stringLikeFunc | |
| 103 | −} | |
| 104 | − | |
| 105 | −// evaluate() - evaluates to check whether value by Key in given values is NOT wildcard | |
| 106 | −// matching in condition values. | |
| 107 | −func (f stringNotLikeFunc) evaluate(values map[string][]string) bool { | |
| 108 | − return !f.stringLikeFunc.evaluate(values) | |
| 109 | −} | |
| 110 | − | |
| 111 | −// name() - returns "StringNotLike" function name. | |
| 112 | −func (f stringNotLikeFunc) name() name { | |
| 113 | − return stringNotLike | |
| 114 | −} | |
| 115 | − | |
| 116 | −func (f stringNotLikeFunc) String() string { | |
| 117 | − return toStringLikeFuncString(stringNotLike, f.stringLikeFunc.k, f.stringLikeFunc.values) | |
| 118 | −} | |
| 119 | − | |
| 120 | −func validateStringLikeValues(n name, key Key, values set.StringSet) error { | |
| 121 | − for _, s := range values.ToSlice() { | |
| 122 | − switch key { | |
| 123 | − case S3XAmzCopySource: | |
| 124 | − bucket, object := path2BucketAndObject(s) | |
| 125 | − if object == "" { | |
| 126 | − return fmt.Errorf("invalid value '%v' for '%v' for %v condition", s, S3XAmzCopySource, n) | |
| 127 | − } | |
| 128 | − if err := s3utils.CheckValidBucketName(bucket); err != nil { | |
| 129 | − return err | |
| 130 | − } | |
| 131 | − } | |
| 132 | − } | |
| 133 | − | |
| 134 | − return nil | |
| 135 | −} | |
| 136 | − | |
| 137 | −// newStringLikeFunc - returns new StringLike function. | |
| 138 | −func newStringLikeFunc(key Key, values ValueSet) (Function, error) { | |
| 139 | − valueStrings, err := valuesToStringSlice(stringLike, values) | |
| 140 | − if err != nil { | |
| 141 | − return nil, err | |
| 142 | − } | |
| 143 | − | |
| 144 | − return NewStringLikeFunc(key, valueStrings...) | |
| 145 | −} | |
| 146 | − | |
| 147 | −// NewStringLikeFunc - returns new StringLike function. | |
| 148 | −func NewStringLikeFunc(key Key, values ...string) (Function, error) { | |
| 149 | − sset := set.CreateStringSet(values...) | |
| 150 | − if err := validateStringLikeValues(stringLike, key, sset); err != nil { | |
| 151 | − return nil, err | |
| 152 | − } | |
| 153 | − | |
| 154 | − return &stringLikeFunc{key, sset}, nil | |
| 155 | −} | |
| 156 | − | |
| 157 | −// newStringNotLikeFunc - returns new StringNotLike function. | |
| 158 | −func newStringNotLikeFunc(key Key, values ValueSet) (Function, error) { | |
| 159 | − valueStrings, err := valuesToStringSlice(stringNotLike, values) | |
| 160 | − if err != nil { | |
| 161 | − return nil, err | |
| 162 | − } | |
| 163 | − | |
| 164 | − return NewStringNotLikeFunc(key, valueStrings...) | |
| 165 | −} | |
| 166 | − | |
| 167 | −// NewStringNotLikeFunc - returns new StringNotLike function. | |
| 168 | −func NewStringNotLikeFunc(key Key, values ...string) (Function, error) { | |
| 169 | − sset := set.CreateStringSet(values...) | |
| 170 | − if err := validateStringLikeValues(stringNotLike, key, sset); err != nil { | |
| 171 | − return nil, err | |
| 172 | − } | |
| 173 | − | |
| 174 | − return &stringNotLikeFunc{stringLikeFunc{key, sset}}, nil | |
| 175 | −} | |
| @@ -1,799 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "reflect" | |
| 22 | − "testing" | |
| 23 | −) | |
| 24 | − | |
| 25 | −func TestStringLikeFuncEvaluate(t *testing.T) { | |
| 26 | − case1Function, err := newStringLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject*"))) | |
| 27 | − if err != nil { | |
| 28 | − t.Fatalf("unexpected error. %v\n", err) | |
| 29 | − } | |
| 30 | − | |
| 31 | − case2Function, err := newStringLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 32 | − if err != nil { | |
| 33 | − t.Fatalf("unexpected error. %v\n", err) | |
| 34 | − } | |
| 35 | − | |
| 36 | − case3Function, err := newStringLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES*"))) | |
| 37 | − if err != nil { | |
| 38 | − t.Fatalf("unexpected error. %v\n", err) | |
| 39 | − } | |
| 40 | − | |
| 41 | − case4Function, err := newStringLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 42 | − if err != nil { | |
| 43 | − t.Fatalf("unexpected error. %v\n", err) | |
| 44 | − } | |
| 45 | − | |
| 46 | − case5Function, err := newStringLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*"))) | |
| 47 | − if err != nil { | |
| 48 | − t.Fatalf("unexpected error. %v\n", err) | |
| 49 | − } | |
| 50 | − | |
| 51 | − case6Function, err := newStringLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 52 | − if err != nil { | |
| 53 | − t.Fatalf("unexpected error. %v\n", err) | |
| 54 | − } | |
| 55 | − | |
| 56 | − case7Function, err := newStringLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-*"))) | |
| 57 | − if err != nil { | |
| 58 | − t.Fatalf("unexpected error. %v\n", err) | |
| 59 | − } | |
| 60 | − | |
| 61 | − case8Function, err := newStringLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 62 | − if err != nil { | |
| 63 | − t.Fatalf("unexpected error. %v\n", err) | |
| 64 | − } | |
| 65 | − | |
| 66 | − testCases := []struct { | |
| 67 | − function Function | |
| 68 | − values map[string][]string | |
| 69 | − expectedResult bool | |
| 70 | − }{ | |
| 71 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, true}, | |
| 72 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject.png"}}, true}, | |
| 73 | − {case1Function, map[string][]string{"x-amz-copy-source": {"yourbucket/myobject"}}, false}, | |
| 74 | − {case1Function, map[string][]string{}, false}, | |
| 75 | − {case1Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 76 | − | |
| 77 | − {case2Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, true}, | |
| 78 | − {case2Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject.png"}}, false}, | |
| 79 | − {case2Function, map[string][]string{"x-amz-copy-source": {"yourbucket/myobject"}}, false}, | |
| 80 | − {case2Function, map[string][]string{}, false}, | |
| 81 | − {case2Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 82 | − | |
| 83 | − {case3Function, map[string][]string{"x-amz-server-side-encryption": {"AES256"}}, true}, | |
| 84 | − {case3Function, map[string][]string{"x-amz-server-side-encryption": {"AES512"}}, true}, | |
| 85 | − {case3Function, map[string][]string{}, false}, | |
| 86 | − {case3Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 87 | − | |
| 88 | − {case4Function, map[string][]string{"x-amz-server-side-encryption": {"AES256"}}, true}, | |
| 89 | − {case4Function, map[string][]string{"x-amz-server-side-encryption": {"AES512"}}, false}, | |
| 90 | − {case4Function, map[string][]string{}, false}, | |
| 91 | − {case4Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 92 | − | |
| 93 | − {case5Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE"}}, true}, | |
| 94 | − {case5Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE/COPY"}}, true}, | |
| 95 | − {case5Function, map[string][]string{"x-amz-metadata-directive": {"COPY"}}, false}, | |
| 96 | − {case5Function, map[string][]string{}, false}, | |
| 97 | − {case5Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 98 | − | |
| 99 | − {case6Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE"}}, true}, | |
| 100 | − {case6Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE/COPY"}}, false}, | |
| 101 | − {case6Function, map[string][]string{"x-amz-metadata-directive": {"COPY"}}, false}, | |
| 102 | − {case6Function, map[string][]string{}, false}, | |
| 103 | − {case6Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 104 | − | |
| 105 | − {case7Function, map[string][]string{"LocationConstraint": {"eu-west-1"}}, true}, | |
| 106 | − {case7Function, map[string][]string{"LocationConstraint": {"eu-west-2"}}, true}, | |
| 107 | − {case7Function, map[string][]string{"LocationConstraint": {"us-east-1"}}, false}, | |
| 108 | − {case7Function, map[string][]string{}, false}, | |
| 109 | − {case7Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 110 | − | |
| 111 | − {case8Function, map[string][]string{"LocationConstraint": {"eu-west-1"}}, true}, | |
| 112 | − {case8Function, map[string][]string{"LocationConstraint": {"eu-west-2"}}, false}, | |
| 113 | − {case8Function, map[string][]string{"LocationConstraint": {"us-east-1"}}, false}, | |
| 114 | − {case8Function, map[string][]string{}, false}, | |
| 115 | − {case8Function, map[string][]string{"delimiter": {"/"}}, false}, | |
| 116 | − } | |
| 117 | − | |
| 118 | − for i, testCase := range testCases { | |
| 119 | − result := testCase.function.evaluate(testCase.values) | |
| 120 | − | |
| 121 | − if result != testCase.expectedResult { | |
| 122 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 123 | − } | |
| 124 | − } | |
| 125 | −} | |
| 126 | − | |
| 127 | −func TestStringLikeFuncKey(t *testing.T) { | |
| 128 | − case1Function, err := newStringLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 129 | − if err != nil { | |
| 130 | − t.Fatalf("unexpected error. %v\n", err) | |
| 131 | − } | |
| 132 | − | |
| 133 | − case2Function, err := newStringLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 134 | − if err != nil { | |
| 135 | − t.Fatalf("unexpected error. %v\n", err) | |
| 136 | − } | |
| 137 | − | |
| 138 | − case3Function, err := newStringLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 139 | − if err != nil { | |
| 140 | − t.Fatalf("unexpected error. %v\n", err) | |
| 141 | − } | |
| 142 | − | |
| 143 | − case4Function, err := newStringLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 144 | − if err != nil { | |
| 145 | − t.Fatalf("unexpected error. %v\n", err) | |
| 146 | − } | |
| 147 | − | |
| 148 | − testCases := []struct { | |
| 149 | − function Function | |
| 150 | − expectedResult Key | |
| 151 | − }{ | |
| 152 | − {case1Function, S3XAmzCopySource}, | |
| 153 | − {case2Function, S3XAmzServerSideEncryption}, | |
| 154 | − {case3Function, S3XAmzMetadataDirective}, | |
| 155 | − {case4Function, S3LocationConstraint}, | |
| 156 | − } | |
| 157 | − | |
| 158 | − for i, testCase := range testCases { | |
| 159 | − result := testCase.function.key() | |
| 160 | − | |
| 161 | − if result != testCase.expectedResult { | |
| 162 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 163 | − } | |
| 164 | − } | |
| 165 | −} | |
| 166 | − | |
| 167 | −func TestStringLikeFuncToMap(t *testing.T) { | |
| 168 | − case1Function, err := newStringLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/*"))) | |
| 169 | − if err != nil { | |
| 170 | − t.Fatalf("unexpected error. %v\n", err) | |
| 171 | − } | |
| 172 | − | |
| 173 | − case1Result := map[Key]ValueSet{ | |
| 174 | − S3XAmzCopySource: NewValueSet(NewStringValue("mybucket/*")), | |
| 175 | − } | |
| 176 | − | |
| 177 | − case2Function, err := newStringLikeFunc(S3XAmzCopySource, | |
| 178 | − NewValueSet( | |
| 179 | − NewStringValue("mybucket/*"), | |
| 180 | − NewStringValue("yourbucket/myobject*"), | |
| 181 | − ), | |
| 182 | − ) | |
| 183 | − if err != nil { | |
| 184 | − t.Fatalf("unexpected error. %v\n", err) | |
| 185 | − } | |
| 186 | − | |
| 187 | − case2Result := map[Key]ValueSet{ | |
| 188 | − S3XAmzCopySource: NewValueSet( | |
| 189 | − NewStringValue("mybucket/*"), | |
| 190 | − NewStringValue("yourbucket/myobject*"), | |
| 191 | − ), | |
| 192 | − } | |
| 193 | − | |
| 194 | − case3Function, err := newStringLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES*"))) | |
| 195 | − if err != nil { | |
| 196 | − t.Fatalf("unexpected error. %v\n", err) | |
| 197 | − } | |
| 198 | − | |
| 199 | − case3Result := map[Key]ValueSet{ | |
| 200 | − S3XAmzServerSideEncryption: NewValueSet(NewStringValue("AES*")), | |
| 201 | − } | |
| 202 | − | |
| 203 | − case4Function, err := newStringLikeFunc(S3XAmzServerSideEncryption, | |
| 204 | − NewValueSet( | |
| 205 | − NewStringValue("AES*"), | |
| 206 | − ), | |
| 207 | − ) | |
| 208 | − if err != nil { | |
| 209 | − t.Fatalf("unexpected error. %v\n", err) | |
| 210 | − } | |
| 211 | − | |
| 212 | − case4Result := map[Key]ValueSet{ | |
| 213 | − S3XAmzServerSideEncryption: NewValueSet( | |
| 214 | − NewStringValue("AES*"), | |
| 215 | − ), | |
| 216 | − } | |
| 217 | − | |
| 218 | − case5Function, err := newStringLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*"))) | |
| 219 | − if err != nil { | |
| 220 | − t.Fatalf("unexpected error. %v\n", err) | |
| 221 | − } | |
| 222 | − | |
| 223 | − case5Result := map[Key]ValueSet{ | |
| 224 | − S3XAmzMetadataDirective: NewValueSet(NewStringValue("REPL*")), | |
| 225 | − } | |
| 226 | − | |
| 227 | − case6Function, err := newStringLikeFunc(S3XAmzMetadataDirective, | |
| 228 | − NewValueSet( | |
| 229 | − NewStringValue("REPL*"), | |
| 230 | − NewStringValue("COPY*"), | |
| 231 | − ), | |
| 232 | − ) | |
| 233 | − if err != nil { | |
| 234 | − t.Fatalf("unexpected error. %v\n", err) | |
| 235 | − } | |
| 236 | − | |
| 237 | − case6Result := map[Key]ValueSet{ | |
| 238 | − S3XAmzMetadataDirective: NewValueSet( | |
| 239 | − NewStringValue("REPL*"), | |
| 240 | − NewStringValue("COPY*"), | |
| 241 | − ), | |
| 242 | − } | |
| 243 | − | |
| 244 | − case7Function, err := newStringLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-*"))) | |
| 245 | − if err != nil { | |
| 246 | − t.Fatalf("unexpected error. %v\n", err) | |
| 247 | − } | |
| 248 | − | |
| 249 | − case7Result := map[Key]ValueSet{ | |
| 250 | − S3LocationConstraint: NewValueSet(NewStringValue("eu-west-*")), | |
| 251 | − } | |
| 252 | − | |
| 253 | − case8Function, err := newStringLikeFunc(S3LocationConstraint, | |
| 254 | − NewValueSet( | |
| 255 | − NewStringValue("eu-west-*"), | |
| 256 | − NewStringValue("us-west-*"), | |
| 257 | − ), | |
| 258 | − ) | |
| 259 | − if err != nil { | |
| 260 | − t.Fatalf("unexpected error. %v\n", err) | |
| 261 | − } | |
| 262 | − | |
| 263 | − case8Result := map[Key]ValueSet{ | |
| 264 | − S3LocationConstraint: NewValueSet( | |
| 265 | − NewStringValue("eu-west-*"), | |
| 266 | − NewStringValue("us-west-*"), | |
| 267 | − ), | |
| 268 | − } | |
| 269 | − | |
| 270 | − testCases := []struct { | |
| 271 | − f Function | |
| 272 | − expectedResult map[Key]ValueSet | |
| 273 | − }{ | |
| 274 | − {case1Function, case1Result}, | |
| 275 | − {case2Function, case2Result}, | |
| 276 | − {case3Function, case3Result}, | |
| 277 | − {case4Function, case4Result}, | |
| 278 | − {case5Function, case5Result}, | |
| 279 | − {case6Function, case6Result}, | |
| 280 | − {case7Function, case7Result}, | |
| 281 | − {case8Function, case8Result}, | |
| 282 | − {&stringLikeFunc{}, nil}, | |
| 283 | − } | |
| 284 | − | |
| 285 | − for i, testCase := range testCases { | |
| 286 | − result := testCase.f.toMap() | |
| 287 | − | |
| 288 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 289 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 290 | − } | |
| 291 | − } | |
| 292 | −} | |
| 293 | − | |
| 294 | −func TestStringNotLikeFuncEvaluate(t *testing.T) { | |
| 295 | − case1Function, err := newStringNotLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject*"))) | |
| 296 | − if err != nil { | |
| 297 | − t.Fatalf("unexpected error. %v\n", err) | |
| 298 | − } | |
| 299 | − | |
| 300 | − case2Function, err := newStringNotLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 301 | − if err != nil { | |
| 302 | − t.Fatalf("unexpected error. %v\n", err) | |
| 303 | − } | |
| 304 | − | |
| 305 | − case3Function, err := newStringNotLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES*"))) | |
| 306 | − if err != nil { | |
| 307 | − t.Fatalf("unexpected error. %v\n", err) | |
| 308 | − } | |
| 309 | − | |
| 310 | − case4Function, err := newStringNotLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 311 | − if err != nil { | |
| 312 | − t.Fatalf("unexpected error. %v\n", err) | |
| 313 | − } | |
| 314 | − | |
| 315 | − case5Function, err := newStringNotLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*"))) | |
| 316 | − if err != nil { | |
| 317 | − t.Fatalf("unexpected error. %v\n", err) | |
| 318 | − } | |
| 319 | − | |
| 320 | − case6Function, err := newStringNotLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 321 | − if err != nil { | |
| 322 | − t.Fatalf("unexpected error. %v\n", err) | |
| 323 | − } | |
| 324 | − | |
| 325 | − case7Function, err := newStringNotLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-*"))) | |
| 326 | − if err != nil { | |
| 327 | − t.Fatalf("unexpected error. %v\n", err) | |
| 328 | − } | |
| 329 | − | |
| 330 | − case8Function, err := newStringNotLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 331 | − if err != nil { | |
| 332 | − t.Fatalf("unexpected error. %v\n", err) | |
| 333 | − } | |
| 334 | − | |
| 335 | − testCases := []struct { | |
| 336 | − function Function | |
| 337 | − values map[string][]string | |
| 338 | − expectedResult bool | |
| 339 | − }{ | |
| 340 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, false}, | |
| 341 | − {case1Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject.png"}}, false}, | |
| 342 | − {case1Function, map[string][]string{"x-amz-copy-source": {"yourbucket/myobject"}}, true}, | |
| 343 | − {case1Function, map[string][]string{}, true}, | |
| 344 | − {case1Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 345 | − | |
| 346 | − {case2Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject"}}, false}, | |
| 347 | − {case2Function, map[string][]string{"x-amz-copy-source": {"mybucket/myobject.png"}}, true}, | |
| 348 | − {case2Function, map[string][]string{"x-amz-copy-source": {"yourbucket/myobject"}}, true}, | |
| 349 | − {case2Function, map[string][]string{}, true}, | |
| 350 | − {case2Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 351 | − | |
| 352 | − {case3Function, map[string][]string{"x-amz-server-side-encryption": {"AES256"}}, false}, | |
| 353 | − {case3Function, map[string][]string{"x-amz-server-side-encryption": {"AES512"}}, false}, | |
| 354 | − {case3Function, map[string][]string{}, true}, | |
| 355 | − {case3Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 356 | − | |
| 357 | − {case4Function, map[string][]string{"x-amz-server-side-encryption": {"AES256"}}, false}, | |
| 358 | − {case4Function, map[string][]string{"x-amz-server-side-encryption": {"AES512"}}, true}, | |
| 359 | − {case4Function, map[string][]string{}, true}, | |
| 360 | − {case4Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 361 | − | |
| 362 | − {case5Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE"}}, false}, | |
| 363 | − {case5Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE/COPY"}}, false}, | |
| 364 | − {case5Function, map[string][]string{"x-amz-metadata-directive": {"COPY"}}, true}, | |
| 365 | − {case5Function, map[string][]string{}, true}, | |
| 366 | − {case5Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 367 | − | |
| 368 | − {case6Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE"}}, false}, | |
| 369 | − {case6Function, map[string][]string{"x-amz-metadata-directive": {"REPLACE/COPY"}}, true}, | |
| 370 | − {case6Function, map[string][]string{"x-amz-metadata-directive": {"COPY"}}, true}, | |
| 371 | − {case6Function, map[string][]string{}, true}, | |
| 372 | − {case6Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 373 | − | |
| 374 | − {case7Function, map[string][]string{"LocationConstraint": {"eu-west-1"}}, false}, | |
| 375 | − {case7Function, map[string][]string{"LocationConstraint": {"eu-west-2"}}, false}, | |
| 376 | − {case7Function, map[string][]string{"LocationConstraint": {"us-east-1"}}, true}, | |
| 377 | − {case7Function, map[string][]string{}, true}, | |
| 378 | − {case7Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 379 | − | |
| 380 | − {case8Function, map[string][]string{"LocationConstraint": {"eu-west-1"}}, false}, | |
| 381 | − {case8Function, map[string][]string{"LocationConstraint": {"eu-west-2"}}, true}, | |
| 382 | − {case8Function, map[string][]string{"LocationConstraint": {"us-east-1"}}, true}, | |
| 383 | − {case8Function, map[string][]string{}, true}, | |
| 384 | − {case8Function, map[string][]string{"delimiter": {"/"}}, true}, | |
| 385 | − } | |
| 386 | − | |
| 387 | − for i, testCase := range testCases { | |
| 388 | − result := testCase.function.evaluate(testCase.values) | |
| 389 | − | |
| 390 | − if result != testCase.expectedResult { | |
| 391 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 392 | − } | |
| 393 | − } | |
| 394 | −} | |
| 395 | − | |
| 396 | −func TestStringNotLikeFuncKey(t *testing.T) { | |
| 397 | − case1Function, err := newStringNotLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"))) | |
| 398 | − if err != nil { | |
| 399 | − t.Fatalf("unexpected error. %v\n", err) | |
| 400 | − } | |
| 401 | − | |
| 402 | − case2Function, err := newStringNotLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"))) | |
| 403 | − if err != nil { | |
| 404 | − t.Fatalf("unexpected error. %v\n", err) | |
| 405 | − } | |
| 406 | − | |
| 407 | − case3Function, err := newStringNotLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"))) | |
| 408 | − if err != nil { | |
| 409 | − t.Fatalf("unexpected error. %v\n", err) | |
| 410 | − } | |
| 411 | − | |
| 412 | − case4Function, err := newStringNotLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"))) | |
| 413 | − if err != nil { | |
| 414 | − t.Fatalf("unexpected error. %v\n", err) | |
| 415 | − } | |
| 416 | − | |
| 417 | − testCases := []struct { | |
| 418 | − function Function | |
| 419 | − expectedResult Key | |
| 420 | − }{ | |
| 421 | − {case1Function, S3XAmzCopySource}, | |
| 422 | − {case2Function, S3XAmzServerSideEncryption}, | |
| 423 | − {case3Function, S3XAmzMetadataDirective}, | |
| 424 | − {case4Function, S3LocationConstraint}, | |
| 425 | − } | |
| 426 | − | |
| 427 | − for i, testCase := range testCases { | |
| 428 | − result := testCase.function.key() | |
| 429 | − | |
| 430 | − if result != testCase.expectedResult { | |
| 431 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 432 | − } | |
| 433 | − } | |
| 434 | −} | |
| 435 | − | |
| 436 | −func TestStringNotLikeFuncToMap(t *testing.T) { | |
| 437 | − case1Function, err := newStringNotLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/*"))) | |
| 438 | − if err != nil { | |
| 439 | − t.Fatalf("unexpected error. %v\n", err) | |
| 440 | − } | |
| 441 | − | |
| 442 | − case1Result := map[Key]ValueSet{ | |
| 443 | − S3XAmzCopySource: NewValueSet(NewStringValue("mybucket/*")), | |
| 444 | − } | |
| 445 | − | |
| 446 | − case2Function, err := newStringNotLikeFunc(S3XAmzCopySource, | |
| 447 | − NewValueSet( | |
| 448 | − NewStringValue("mybucket/*"), | |
| 449 | − NewStringValue("yourbucket/myobject*"), | |
| 450 | − ), | |
| 451 | − ) | |
| 452 | − if err != nil { | |
| 453 | − t.Fatalf("unexpected error. %v\n", err) | |
| 454 | − } | |
| 455 | − | |
| 456 | − case2Result := map[Key]ValueSet{ | |
| 457 | − S3XAmzCopySource: NewValueSet( | |
| 458 | − NewStringValue("mybucket/*"), | |
| 459 | − NewStringValue("yourbucket/myobject*"), | |
| 460 | − ), | |
| 461 | − } | |
| 462 | − | |
| 463 | − case3Function, err := newStringNotLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES*"))) | |
| 464 | − if err != nil { | |
| 465 | − t.Fatalf("unexpected error. %v\n", err) | |
| 466 | − } | |
| 467 | − | |
| 468 | − case3Result := map[Key]ValueSet{ | |
| 469 | − S3XAmzServerSideEncryption: NewValueSet(NewStringValue("AES*")), | |
| 470 | − } | |
| 471 | − | |
| 472 | − case4Function, err := newStringNotLikeFunc(S3XAmzServerSideEncryption, | |
| 473 | − NewValueSet( | |
| 474 | − NewStringValue("AES*"), | |
| 475 | − ), | |
| 476 | − ) | |
| 477 | − if err != nil { | |
| 478 | − t.Fatalf("unexpected error. %v\n", err) | |
| 479 | − } | |
| 480 | − | |
| 481 | − case4Result := map[Key]ValueSet{ | |
| 482 | − S3XAmzServerSideEncryption: NewValueSet( | |
| 483 | − NewStringValue("AES*"), | |
| 484 | − ), | |
| 485 | − } | |
| 486 | − | |
| 487 | − case5Function, err := newStringNotLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*"))) | |
| 488 | − if err != nil { | |
| 489 | − t.Fatalf("unexpected error. %v\n", err) | |
| 490 | − } | |
| 491 | − | |
| 492 | − case5Result := map[Key]ValueSet{ | |
| 493 | − S3XAmzMetadataDirective: NewValueSet(NewStringValue("REPL*")), | |
| 494 | − } | |
| 495 | − | |
| 496 | − case6Function, err := newStringNotLikeFunc(S3XAmzMetadataDirective, | |
| 497 | − NewValueSet( | |
| 498 | − NewStringValue("REPL*"), | |
| 499 | − NewStringValue("COPY*"), | |
| 500 | − ), | |
| 501 | − ) | |
| 502 | − if err != nil { | |
| 503 | − t.Fatalf("unexpected error. %v\n", err) | |
| 504 | − } | |
| 505 | − | |
| 506 | − case6Result := map[Key]ValueSet{ | |
| 507 | − S3XAmzMetadataDirective: NewValueSet( | |
| 508 | − NewStringValue("REPL*"), | |
| 509 | − NewStringValue("COPY*"), | |
| 510 | − ), | |
| 511 | − } | |
| 512 | − | |
| 513 | − case7Function, err := newStringNotLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-*"))) | |
| 514 | − if err != nil { | |
| 515 | − t.Fatalf("unexpected error. %v\n", err) | |
| 516 | − } | |
| 517 | − | |
| 518 | − case7Result := map[Key]ValueSet{ | |
| 519 | − S3LocationConstraint: NewValueSet(NewStringValue("eu-west-*")), | |
| 520 | − } | |
| 521 | − | |
| 522 | − case8Function, err := newStringNotLikeFunc(S3LocationConstraint, | |
| 523 | − NewValueSet( | |
| 524 | − NewStringValue("eu-west-*"), | |
| 525 | − NewStringValue("us-west-*"), | |
| 526 | − ), | |
| 527 | − ) | |
| 528 | − if err != nil { | |
| 529 | − t.Fatalf("unexpected error. %v\n", err) | |
| 530 | − } | |
| 531 | − | |
| 532 | − case8Result := map[Key]ValueSet{ | |
| 533 | − S3LocationConstraint: NewValueSet( | |
| 534 | − NewStringValue("eu-west-*"), | |
| 535 | − NewStringValue("us-west-*"), | |
| 536 | − ), | |
| 537 | − } | |
| 538 | − | |
| 539 | − testCases := []struct { | |
| 540 | − f Function | |
| 541 | − expectedResult map[Key]ValueSet | |
| 542 | − }{ | |
| 543 | − {case1Function, case1Result}, | |
| 544 | − {case2Function, case2Result}, | |
| 545 | − {case3Function, case3Result}, | |
| 546 | − {case4Function, case4Result}, | |
| 547 | − {case5Function, case5Result}, | |
| 548 | − {case6Function, case6Result}, | |
| 549 | − {case7Function, case7Result}, | |
| 550 | − {case8Function, case8Result}, | |
| 551 | − {&stringNotLikeFunc{}, nil}, | |
| 552 | − } | |
| 553 | − | |
| 554 | − for i, testCase := range testCases { | |
| 555 | − result := testCase.f.toMap() | |
| 556 | − | |
| 557 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 558 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 559 | − } | |
| 560 | − } | |
| 561 | −} | |
| 562 | − | |
| 563 | −func TestNewStringLikeFunc(t *testing.T) { | |
| 564 | − case1Function, err := newStringLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/*"))) | |
| 565 | − if err != nil { | |
| 566 | − t.Fatalf("unexpected error. %v\n", err) | |
| 567 | − } | |
| 568 | − | |
| 569 | − case2Function, err := newStringLikeFunc(S3XAmzCopySource, | |
| 570 | − NewValueSet( | |
| 571 | − NewStringValue("mybucket/*"), | |
| 572 | − NewStringValue("yourbucket/myobject*"), | |
| 573 | − ), | |
| 574 | − ) | |
| 575 | − if err != nil { | |
| 576 | − t.Fatalf("unexpected error. %v\n", err) | |
| 577 | − } | |
| 578 | − | |
| 579 | − case3Function, err := newStringLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES*"))) | |
| 580 | − if err != nil { | |
| 581 | − t.Fatalf("unexpected error. %v\n", err) | |
| 582 | − } | |
| 583 | − | |
| 584 | − case4Function, err := newStringLikeFunc(S3XAmzServerSideEncryption, | |
| 585 | − NewValueSet( | |
| 586 | − NewStringValue("AES*"), | |
| 587 | − ), | |
| 588 | − ) | |
| 589 | − if err != nil { | |
| 590 | − t.Fatalf("unexpected error. %v\n", err) | |
| 591 | − } | |
| 592 | − | |
| 593 | − case5Function, err := newStringLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*"))) | |
| 594 | − if err != nil { | |
| 595 | − t.Fatalf("unexpected error. %v\n", err) | |
| 596 | − } | |
| 597 | − | |
| 598 | − case6Function, err := newStringLikeFunc(S3XAmzMetadataDirective, | |
| 599 | − NewValueSet( | |
| 600 | − NewStringValue("REPL*"), | |
| 601 | − NewStringValue("COPY*"), | |
| 602 | − ), | |
| 603 | − ) | |
| 604 | − if err != nil { | |
| 605 | − t.Fatalf("unexpected error. %v\n", err) | |
| 606 | − } | |
| 607 | − | |
| 608 | − case7Function, err := newStringLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-*"))) | |
| 609 | − if err != nil { | |
| 610 | − t.Fatalf("unexpected error. %v\n", err) | |
| 611 | − } | |
| 612 | − | |
| 613 | − case8Function, err := newStringLikeFunc(S3LocationConstraint, | |
| 614 | − NewValueSet( | |
| 615 | − NewStringValue("eu-west-*"), | |
| 616 | − NewStringValue("us-west-*"), | |
| 617 | − ), | |
| 618 | − ) | |
| 619 | − if err != nil { | |
| 620 | − t.Fatalf("unexpected error. %v\n", err) | |
| 621 | − } | |
| 622 | − | |
| 623 | − testCases := []struct { | |
| 624 | − key Key | |
| 625 | − values ValueSet | |
| 626 | − expectedResult Function | |
| 627 | − expectErr bool | |
| 628 | − }{ | |
| 629 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/*")), case1Function, false}, | |
| 630 | − {S3XAmzCopySource, | |
| 631 | − NewValueSet( | |
| 632 | − NewStringValue("mybucket/*"), | |
| 633 | − NewStringValue("yourbucket/myobject*"), | |
| 634 | − ), case2Function, false}, | |
| 635 | − | |
| 636 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES*")), case3Function, false}, | |
| 637 | − {S3XAmzServerSideEncryption, | |
| 638 | − NewValueSet( | |
| 639 | − NewStringValue("AES*"), | |
| 640 | − ), case4Function, false}, | |
| 641 | − | |
| 642 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*")), case5Function, false}, | |
| 643 | − {S3XAmzMetadataDirective, | |
| 644 | − NewValueSet( | |
| 645 | − NewStringValue("REPL*"), | |
| 646 | − NewStringValue("COPY*"), | |
| 647 | − ), case6Function, false}, | |
| 648 | − | |
| 649 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-*")), case7Function, false}, | |
| 650 | − {S3LocationConstraint, | |
| 651 | − NewValueSet( | |
| 652 | − NewStringValue("eu-west-*"), | |
| 653 | − NewStringValue("us-west-*"), | |
| 654 | − ), case8Function, false}, | |
| 655 | − | |
| 656 | − // Unsupported value error. | |
| 657 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"), NewIntValue(7)), nil, true}, | |
| 658 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"), NewIntValue(7)), nil, true}, | |
| 659 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"), NewIntValue(7)), nil, true}, | |
| 660 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"), NewIntValue(7)), nil, true}, | |
| 661 | − | |
| 662 | − // Invalid value error. | |
| 663 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket")), nil, true}, | |
| 664 | − } | |
| 665 | − | |
| 666 | − for i, testCase := range testCases { | |
| 667 | − result, err := newStringLikeFunc(testCase.key, testCase.values) | |
| 668 | − expectErr := (err != nil) | |
| 669 | − | |
| 670 | − if expectErr != testCase.expectErr { | |
| 671 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 672 | − } | |
| 673 | − | |
| 674 | − if !testCase.expectErr { | |
| 675 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 676 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 677 | − } | |
| 678 | − } | |
| 679 | − } | |
| 680 | −} | |
| 681 | − | |
| 682 | −func TestNewStringNotLikeFunc(t *testing.T) { | |
| 683 | − case1Function, err := newStringNotLikeFunc(S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/*"))) | |
| 684 | − if err != nil { | |
| 685 | − t.Fatalf("unexpected error. %v\n", err) | |
| 686 | − } | |
| 687 | − | |
| 688 | − case2Function, err := newStringNotLikeFunc(S3XAmzCopySource, | |
| 689 | − NewValueSet( | |
| 690 | − NewStringValue("mybucket/*"), | |
| 691 | − NewStringValue("yourbucket/myobject*"), | |
| 692 | − ), | |
| 693 | − ) | |
| 694 | − if err != nil { | |
| 695 | − t.Fatalf("unexpected error. %v\n", err) | |
| 696 | − } | |
| 697 | − | |
| 698 | − case3Function, err := newStringNotLikeFunc(S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES*"))) | |
| 699 | − if err != nil { | |
| 700 | − t.Fatalf("unexpected error. %v\n", err) | |
| 701 | − } | |
| 702 | − | |
| 703 | − case4Function, err := newStringNotLikeFunc(S3XAmzServerSideEncryption, | |
| 704 | − NewValueSet( | |
| 705 | − NewStringValue("AES*"), | |
| 706 | − ), | |
| 707 | − ) | |
| 708 | − if err != nil { | |
| 709 | − t.Fatalf("unexpected error. %v\n", err) | |
| 710 | − } | |
| 711 | − | |
| 712 | − case5Function, err := newStringNotLikeFunc(S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*"))) | |
| 713 | − if err != nil { | |
| 714 | − t.Fatalf("unexpected error. %v\n", err) | |
| 715 | − } | |
| 716 | − | |
| 717 | − case6Function, err := newStringNotLikeFunc(S3XAmzMetadataDirective, | |
| 718 | − NewValueSet( | |
| 719 | − NewStringValue("REPL*"), | |
| 720 | − NewStringValue("COPY*"), | |
| 721 | − ), | |
| 722 | − ) | |
| 723 | − if err != nil { | |
| 724 | − t.Fatalf("unexpected error. %v\n", err) | |
| 725 | − } | |
| 726 | − | |
| 727 | − case7Function, err := newStringNotLikeFunc(S3LocationConstraint, NewValueSet(NewStringValue("eu-west-*"))) | |
| 728 | − if err != nil { | |
| 729 | − t.Fatalf("unexpected error. %v\n", err) | |
| 730 | − } | |
| 731 | − | |
| 732 | − case8Function, err := newStringNotLikeFunc(S3LocationConstraint, | |
| 733 | − NewValueSet( | |
| 734 | − NewStringValue("eu-west-*"), | |
| 735 | − NewStringValue("us-west-*"), | |
| 736 | − ), | |
| 737 | − ) | |
| 738 | − if err != nil { | |
| 739 | − t.Fatalf("unexpected error. %v\n", err) | |
| 740 | − } | |
| 741 | − | |
| 742 | − testCases := []struct { | |
| 743 | − key Key | |
| 744 | − values ValueSet | |
| 745 | − expectedResult Function | |
| 746 | − expectErr bool | |
| 747 | − }{ | |
| 748 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/*")), case1Function, false}, | |
| 749 | − {S3XAmzCopySource, | |
| 750 | − NewValueSet( | |
| 751 | − NewStringValue("mybucket/*"), | |
| 752 | − NewStringValue("yourbucket/myobject*"), | |
| 753 | − ), case2Function, false}, | |
| 754 | − | |
| 755 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES*")), case3Function, false}, | |
| 756 | − {S3XAmzServerSideEncryption, | |
| 757 | − NewValueSet( | |
| 758 | − NewStringValue("AES*"), | |
| 759 | − ), case4Function, false}, | |
| 760 | − | |
| 761 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPL*")), case5Function, false}, | |
| 762 | − {S3XAmzMetadataDirective, | |
| 763 | − NewValueSet( | |
| 764 | − NewStringValue("REPL*"), | |
| 765 | − NewStringValue("COPY*"), | |
| 766 | − ), case6Function, false}, | |
| 767 | − | |
| 768 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-*")), case7Function, false}, | |
| 769 | − {S3LocationConstraint, | |
| 770 | − NewValueSet( | |
| 771 | − NewStringValue("eu-west-*"), | |
| 772 | − NewStringValue("us-west-*"), | |
| 773 | − ), case8Function, false}, | |
| 774 | − | |
| 775 | − // Unsupported value error. | |
| 776 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket/myobject"), NewIntValue(7)), nil, true}, | |
| 777 | − {S3XAmzServerSideEncryption, NewValueSet(NewStringValue("AES256"), NewIntValue(7)), nil, true}, | |
| 778 | − {S3XAmzMetadataDirective, NewValueSet(NewStringValue("REPLACE"), NewIntValue(7)), nil, true}, | |
| 779 | − {S3LocationConstraint, NewValueSet(NewStringValue("eu-west-1"), NewIntValue(7)), nil, true}, | |
| 780 | − | |
| 781 | − // Invalid value error. | |
| 782 | − {S3XAmzCopySource, NewValueSet(NewStringValue("mybucket")), nil, true}, | |
| 783 | − } | |
| 784 | − | |
| 785 | − for i, testCase := range testCases { | |
| 786 | − result, err := newStringNotLikeFunc(testCase.key, testCase.values) | |
| 787 | − expectErr := (err != nil) | |
| 788 | − | |
| 789 | − if expectErr != testCase.expectErr { | |
| 790 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 791 | − } | |
| 792 | − | |
| 793 | − if !testCase.expectErr { | |
| 794 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 795 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 796 | − } | |
| 797 | − } | |
| 798 | − } | |
| 799 | −} | |
| @@ -1,176 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | − "reflect" | |
| 24 | − "strconv" | |
| 25 | − "strings" | |
| 26 | −) | |
| 27 | − | |
| 28 | −// Splits an incoming path into bucket and object components. | |
| 29 | −func path2BucketAndObject(path string) (bucket, object string) { | |
| 30 | − // Skip the first element if it is '/', split the rest. | |
| 31 | − path = strings.TrimPrefix(path, "/") | |
| 32 | − pathComponents := strings.SplitN(path, "/", 2) | |
| 33 | − | |
| 34 | − // Save the bucket and object extracted from path. | |
| 35 | − switch len(pathComponents) { | |
| 36 | − case 1: | |
| 37 | − bucket = pathComponents[0] | |
| 38 | − case 2: | |
| 39 | − bucket = pathComponents[0] | |
| 40 | − object = pathComponents[1] | |
| 41 | − } | |
| 42 | − return bucket, object | |
| 43 | −} | |
| 44 | − | |
| 45 | −// Value - is enum type of string, int or bool. | |
| 46 | −type Value struct { | |
| 47 | − t reflect.Kind | |
| 48 | − s string | |
| 49 | − i int | |
| 50 | − b bool | |
| 51 | −} | |
| 52 | − | |
| 53 | −// GetBool - gets stored bool value. | |
| 54 | −func (v Value) GetBool() (bool, error) { | |
| 55 | − var err error | |
| 56 | − | |
| 57 | − if v.t != reflect.Bool { | |
| 58 | − err = fmt.Errorf("not a bool Value") | |
| 59 | − } | |
| 60 | − | |
| 61 | − return v.b, err | |
| 62 | −} | |
| 63 | − | |
| 64 | −// GetInt - gets stored int value. | |
| 65 | −func (v Value) GetInt() (int, error) { | |
| 66 | − var err error | |
| 67 | − | |
| 68 | − if v.t != reflect.Int { | |
| 69 | − err = fmt.Errorf("not a int Value") | |
| 70 | − } | |
| 71 | − | |
| 72 | − return v.i, err | |
| 73 | −} | |
| 74 | − | |
| 75 | −// GetString - gets stored string value. | |
| 76 | −func (v Value) GetString() (string, error) { | |
| 77 | − var err error | |
| 78 | − | |
| 79 | − if v.t != reflect.String { | |
| 80 | − err = fmt.Errorf("not a string Value") | |
| 81 | − } | |
| 82 | − | |
| 83 | − return v.s, err | |
| 84 | −} | |
| 85 | − | |
| 86 | −// GetType - gets enum type. | |
| 87 | −func (v Value) GetType() reflect.Kind { | |
| 88 | − return v.t | |
| 89 | −} | |
| 90 | − | |
| 91 | −// MarshalJSON - encodes Value to JSON data. | |
| 92 | −func (v Value) MarshalJSON() ([]byte, error) { | |
| 93 | − switch v.t { | |
| 94 | − case reflect.String: | |
| 95 | − return json.Marshal(v.s) | |
| 96 | − case reflect.Int: | |
| 97 | − return json.Marshal(v.i) | |
| 98 | − case reflect.Bool: | |
| 99 | − return json.Marshal(v.b) | |
| 100 | − } | |
| 101 | − | |
| 102 | − return nil, fmt.Errorf("unknown value kind %v", v.t) | |
| 103 | −} | |
| 104 | − | |
| 105 | −// StoreBool - stores bool value. | |
| 106 | −func (v *Value) StoreBool(b bool) { | |
| 107 | − *v = Value{t: reflect.Bool, b: b} | |
| 108 | −} | |
| 109 | − | |
| 110 | −// StoreInt - stores int value. | |
| 111 | −func (v *Value) StoreInt(i int) { | |
| 112 | − *v = Value{t: reflect.Int, i: i} | |
| 113 | −} | |
| 114 | − | |
| 115 | −// StoreString - stores string value. | |
| 116 | −func (v *Value) StoreString(s string) { | |
| 117 | − *v = Value{t: reflect.String, s: s} | |
| 118 | −} | |
| 119 | − | |
| 120 | −// String - returns string representation of value. | |
| 121 | −func (v Value) String() string { | |
| 122 | − switch v.t { | |
| 123 | − case reflect.String: | |
| 124 | − return v.s | |
| 125 | − case reflect.Int: | |
| 126 | − return strconv.Itoa(v.i) | |
| 127 | − case reflect.Bool: | |
| 128 | − return strconv.FormatBool(v.b) | |
| 129 | − } | |
| 130 | − | |
| 131 | − return "" | |
| 132 | −} | |
| 133 | − | |
| 134 | −// UnmarshalJSON - decodes JSON data. | |
| 135 | −func (v *Value) UnmarshalJSON(data []byte) error { | |
| 136 | − var b bool | |
| 137 | − if err := json.Unmarshal(data, &b); err == nil { | |
| 138 | − v.StoreBool(b) | |
| 139 | − return nil | |
| 140 | − } | |
| 141 | − | |
| 142 | − var i int | |
| 143 | − if err := json.Unmarshal(data, &i); err == nil { | |
| 144 | − v.StoreInt(i) | |
| 145 | − return nil | |
| 146 | − } | |
| 147 | − | |
| 148 | − var s string | |
| 149 | − if err := json.Unmarshal(data, &s); err == nil { | |
| 150 | − v.StoreString(s) | |
| 151 | − return nil | |
| 152 | − } | |
| 153 | − | |
| 154 | − return fmt.Errorf("unknown json data '%v'", data) | |
| 155 | −} | |
| 156 | − | |
| 157 | −// NewBoolValue - returns new bool value. | |
| 158 | −func NewBoolValue(b bool) Value { | |
| 159 | − value := &Value{} | |
| 160 | − value.StoreBool(b) | |
| 161 | − return *value | |
| 162 | −} | |
| 163 | − | |
| 164 | −// NewIntValue - returns new int value. | |
| 165 | −func NewIntValue(i int) Value { | |
| 166 | − value := &Value{} | |
| 167 | − value.StoreInt(i) | |
| 168 | − return *value | |
| 169 | −} | |
| 170 | − | |
| 171 | −// NewStringValue - returns new string value. | |
| 172 | −func NewStringValue(s string) Value { | |
| 173 | − value := &Value{} | |
| 174 | − value.StoreString(s) | |
| 175 | − return *value | |
| 176 | −} | |
| @@ -1,261 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestValueGetBool(t *testing.T) { | |
| 27 | − testCases := []struct { | |
| 28 | − value Value | |
| 29 | − expectedResult bool | |
| 30 | − expectErr bool | |
| 31 | − }{ | |
| 32 | − {NewBoolValue(true), true, false}, | |
| 33 | − {NewIntValue(7), false, true}, | |
| 34 | − {Value{}, false, true}, | |
| 35 | − } | |
| 36 | − | |
| 37 | − for i, testCase := range testCases { | |
| 38 | − result, err := testCase.value.GetBool() | |
| 39 | − expectErr := (err != nil) | |
| 40 | − | |
| 41 | − if expectErr != testCase.expectErr { | |
| 42 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 43 | − } | |
| 44 | − | |
| 45 | − if !testCase.expectErr { | |
| 46 | − if result != testCase.expectedResult { | |
| 47 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 48 | − } | |
| 49 | − } | |
| 50 | − } | |
| 51 | −} | |
| 52 | − | |
| 53 | −func TestValueGetInt(t *testing.T) { | |
| 54 | − testCases := []struct { | |
| 55 | − value Value | |
| 56 | − expectedResult int | |
| 57 | − expectErr bool | |
| 58 | − }{ | |
| 59 | − {NewIntValue(7), 7, false}, | |
| 60 | − {NewBoolValue(true), 0, true}, | |
| 61 | − {Value{}, 0, true}, | |
| 62 | − } | |
| 63 | − | |
| 64 | − for i, testCase := range testCases { | |
| 65 | − result, err := testCase.value.GetInt() | |
| 66 | − expectErr := (err != nil) | |
| 67 | − | |
| 68 | − if expectErr != testCase.expectErr { | |
| 69 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 70 | − } | |
| 71 | − | |
| 72 | − if !testCase.expectErr { | |
| 73 | − if result != testCase.expectedResult { | |
| 74 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 75 | − } | |
| 76 | − } | |
| 77 | − } | |
| 78 | −} | |
| 79 | − | |
| 80 | −func TestValueGetString(t *testing.T) { | |
| 81 | − testCases := []struct { | |
| 82 | − value Value | |
| 83 | − expectedResult string | |
| 84 | − expectErr bool | |
| 85 | − }{ | |
| 86 | − {NewStringValue("foo"), "foo", false}, | |
| 87 | − {NewBoolValue(true), "", true}, | |
| 88 | − {Value{}, "", true}, | |
| 89 | − } | |
| 90 | − | |
| 91 | − for i, testCase := range testCases { | |
| 92 | − result, err := testCase.value.GetString() | |
| 93 | − expectErr := (err != nil) | |
| 94 | − | |
| 95 | − if expectErr != testCase.expectErr { | |
| 96 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 97 | − } | |
| 98 | − | |
| 99 | − if !testCase.expectErr { | |
| 100 | − if result != testCase.expectedResult { | |
| 101 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 102 | − } | |
| 103 | − } | |
| 104 | − } | |
| 105 | −} | |
| 106 | − | |
| 107 | −func TestValueGetType(t *testing.T) { | |
| 108 | − testCases := []struct { | |
| 109 | − value Value | |
| 110 | − expectedResult reflect.Kind | |
| 111 | − }{ | |
| 112 | − {NewBoolValue(true), reflect.Bool}, | |
| 113 | − {NewIntValue(7), reflect.Int}, | |
| 114 | − {NewStringValue("foo"), reflect.String}, | |
| 115 | − {Value{}, reflect.Invalid}, | |
| 116 | − } | |
| 117 | − | |
| 118 | − for i, testCase := range testCases { | |
| 119 | − result := testCase.value.GetType() | |
| 120 | − | |
| 121 | − if result != testCase.expectedResult { | |
| 122 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 123 | − } | |
| 124 | − } | |
| 125 | −} | |
| 126 | − | |
| 127 | −func TestValueMarshalJSON(t *testing.T) { | |
| 128 | − testCases := []struct { | |
| 129 | − value Value | |
| 130 | − expectedResult []byte | |
| 131 | − expectErr bool | |
| 132 | − }{ | |
| 133 | − {NewBoolValue(true), []byte("true"), false}, | |
| 134 | − {NewIntValue(7), []byte("7"), false}, | |
| 135 | − {NewStringValue("foo"), []byte(`"foo"`), false}, | |
| 136 | − {Value{}, nil, true}, | |
| 137 | − } | |
| 138 | − | |
| 139 | − for i, testCase := range testCases { | |
| 140 | − result, err := json.Marshal(testCase.value) | |
| 141 | − expectErr := (err != nil) | |
| 142 | − | |
| 143 | − if expectErr != testCase.expectErr { | |
| 144 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 145 | − } | |
| 146 | − | |
| 147 | − if !testCase.expectErr { | |
| 148 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 149 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 150 | − } | |
| 151 | − } | |
| 152 | − } | |
| 153 | −} | |
| 154 | − | |
| 155 | −func TestValueStoreBool(t *testing.T) { | |
| 156 | − testCases := []struct { | |
| 157 | − value bool | |
| 158 | − expectedResult Value | |
| 159 | − }{ | |
| 160 | − {false, NewBoolValue(false)}, | |
| 161 | − {true, NewBoolValue(true)}, | |
| 162 | − } | |
| 163 | − | |
| 164 | − for i, testCase := range testCases { | |
| 165 | − var result Value | |
| 166 | − result.StoreBool(testCase.value) | |
| 167 | − | |
| 168 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 169 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 170 | − } | |
| 171 | − } | |
| 172 | −} | |
| 173 | − | |
| 174 | −func TestValueStoreInt(t *testing.T) { | |
| 175 | − testCases := []struct { | |
| 176 | − value int | |
| 177 | − expectedResult Value | |
| 178 | − }{ | |
| 179 | − {0, NewIntValue(0)}, | |
| 180 | − {7, NewIntValue(7)}, | |
| 181 | − } | |
| 182 | − | |
| 183 | − for i, testCase := range testCases { | |
| 184 | − var result Value | |
| 185 | − result.StoreInt(testCase.value) | |
| 186 | − | |
| 187 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 188 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 189 | − } | |
| 190 | − } | |
| 191 | −} | |
| 192 | − | |
| 193 | −func TestValueStoreString(t *testing.T) { | |
| 194 | − testCases := []struct { | |
| 195 | − value string | |
| 196 | − expectedResult Value | |
| 197 | − }{ | |
| 198 | − {"", NewStringValue("")}, | |
| 199 | − {"foo", NewStringValue("foo")}, | |
| 200 | − } | |
| 201 | − | |
| 202 | − for i, testCase := range testCases { | |
| 203 | − var result Value | |
| 204 | − result.StoreString(testCase.value) | |
| 205 | − | |
| 206 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 207 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 208 | − } | |
| 209 | − } | |
| 210 | −} | |
| 211 | − | |
| 212 | −func TestValueString(t *testing.T) { | |
| 213 | − testCases := []struct { | |
| 214 | − value Value | |
| 215 | − expectedResult string | |
| 216 | − }{ | |
| 217 | − {NewBoolValue(true), "true"}, | |
| 218 | − {NewIntValue(7), "7"}, | |
| 219 | − {NewStringValue("foo"), "foo"}, | |
| 220 | − {Value{}, ""}, | |
| 221 | − } | |
| 222 | − | |
| 223 | − for i, testCase := range testCases { | |
| 224 | − result := testCase.value.String() | |
| 225 | − | |
| 226 | − if result != testCase.expectedResult { | |
| 227 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 228 | − } | |
| 229 | − } | |
| 230 | −} | |
| 231 | − | |
| 232 | −func TestValueUnmarshalJSON(t *testing.T) { | |
| 233 | − testCases := []struct { | |
| 234 | − data []byte | |
| 235 | − expectedResult Value | |
| 236 | − expectErr bool | |
| 237 | − }{ | |
| 238 | − {[]byte("true"), NewBoolValue(true), false}, | |
| 239 | − {[]byte("7"), NewIntValue(7), false}, | |
| 240 | − {[]byte(`"foo"`), NewStringValue("foo"), false}, | |
| 241 | − {[]byte("True"), Value{}, true}, | |
| 242 | − {[]byte("7.1"), Value{}, true}, | |
| 243 | − {[]byte(`["foo"]`), Value{}, true}, | |
| 244 | − } | |
| 245 | − | |
| 246 | − for i, testCase := range testCases { | |
| 247 | − var result Value | |
| 248 | − err := json.Unmarshal(testCase.data, &result) | |
| 249 | − expectErr := (err != nil) | |
| 250 | − | |
| 251 | − if expectErr != testCase.expectErr { | |
| 252 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 253 | − } | |
| 254 | − | |
| 255 | − if !testCase.expectErr { | |
| 256 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 257 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 258 | − } | |
| 259 | − } | |
| 260 | − } | |
| 261 | −} | |
| @@ -1,100 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | −) | |
| 24 | − | |
| 25 | −// ValueSet - unique list of values. | |
| 26 | −type ValueSet map[Value]struct{} | |
| 27 | − | |
| 28 | −// Add - adds given value to value set. | |
| 29 | −func (set ValueSet) Add(value Value) { | |
| 30 | − set[value] = struct{}{} | |
| 31 | −} | |
| 32 | − | |
| 33 | −// ToSlice converts ValueSet to a slice of Value | |
| 34 | −func (set ValueSet) ToSlice() []Value { | |
| 35 | − var values []Value | |
| 36 | − for k := range set { | |
| 37 | − values = append(values, k) | |
| 38 | − } | |
| 39 | − return values | |
| 40 | −} | |
| 41 | − | |
| 42 | −// MarshalJSON - encodes ValueSet to JSON data. | |
| 43 | −func (set ValueSet) MarshalJSON() ([]byte, error) { | |
| 44 | − var values []Value | |
| 45 | − for k := range set { | |
| 46 | − values = append(values, k) | |
| 47 | − } | |
| 48 | − | |
| 49 | − if len(values) == 0 { | |
| 50 | − return nil, fmt.Errorf("invalid value set %v", set) | |
| 51 | − } | |
| 52 | − | |
| 53 | − return json.Marshal(values) | |
| 54 | −} | |
| 55 | − | |
| 56 | −// UnmarshalJSON - decodes JSON data. | |
| 57 | −func (set *ValueSet) UnmarshalJSON(data []byte) error { | |
| 58 | − var v Value | |
| 59 | − if err := json.Unmarshal(data, &v); err == nil { | |
| 60 | − *set = make(ValueSet) | |
| 61 | − set.Add(v) | |
| 62 | − return nil | |
| 63 | − } | |
| 64 | − | |
| 65 | − var values []Value | |
| 66 | − if err := json.Unmarshal(data, &values); err != nil { | |
| 67 | − return err | |
| 68 | − } | |
| 69 | − | |
| 70 | − if len(values) < 1 { | |
| 71 | − return fmt.Errorf("invalid value") | |
| 72 | − } | |
| 73 | − | |
| 74 | − *set = make(ValueSet) | |
| 75 | − for _, v = range values { | |
| 76 | − if _, found := (*set)[v]; found { | |
| 77 | − return fmt.Errorf("duplicate value found '%v'", v) | |
| 78 | − } | |
| 79 | − | |
| 80 | − set.Add(v) | |
| 81 | − } | |
| 82 | − | |
| 83 | − return nil | |
| 84 | −} | |
| 85 | − | |
| 86 | −// Clone clones ValueSet structure | |
| 87 | −func (set ValueSet) Clone() ValueSet { | |
| 88 | − return NewValueSet(set.ToSlice()...) | |
| 89 | −} | |
| 90 | − | |
| 91 | −// NewValueSet - returns new value set containing given values. | |
| 92 | −func NewValueSet(values ...Value) ValueSet { | |
| 93 | − set := make(ValueSet) | |
| 94 | − | |
| 95 | − for _, value := range values { | |
| 96 | − set.Add(value) | |
| 97 | − } | |
| 98 | − | |
| 99 | − return set | |
| 100 | −} | |
| @@ -1,119 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package condition | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestValueSetAdd(t *testing.T) { | |
| 27 | − testCases := []struct { | |
| 28 | − value Value | |
| 29 | − expectedResult ValueSet | |
| 30 | − }{ | |
| 31 | − {NewBoolValue(true), NewValueSet(NewBoolValue(true))}, | |
| 32 | − {NewIntValue(7), NewValueSet(NewIntValue(7))}, | |
| 33 | − {NewStringValue("foo"), NewValueSet(NewStringValue("foo"))}, | |
| 34 | − } | |
| 35 | − | |
| 36 | − for i, testCase := range testCases { | |
| 37 | − result := NewValueSet() | |
| 38 | − result.Add(testCase.value) | |
| 39 | − | |
| 40 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 41 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 42 | − } | |
| 43 | − } | |
| 44 | −} | |
| 45 | − | |
| 46 | −func TestValueSetMarshalJSON(t *testing.T) { | |
| 47 | − testCases := []struct { | |
| 48 | − set ValueSet | |
| 49 | − expectedResult string | |
| 50 | − expectErr bool | |
| 51 | − }{ | |
| 52 | − {NewValueSet(NewBoolValue(true)), `[true]`, false}, | |
| 53 | − {NewValueSet(NewIntValue(7)), `[7]`, false}, | |
| 54 | − {NewValueSet(NewStringValue("foo")), `["foo"]`, false}, | |
| 55 | − {NewValueSet(NewBoolValue(true)), `[true]`, false}, | |
| 56 | − {NewValueSet(NewStringValue("7")), `["7"]`, false}, | |
| 57 | − {NewValueSet(NewStringValue("foo")), `["foo"]`, false}, | |
| 58 | − {make(ValueSet), "", true}, | |
| 59 | − } | |
| 60 | − | |
| 61 | − for i, testCase := range testCases { | |
| 62 | − result, err := json.Marshal(testCase.set) | |
| 63 | − expectErr := (err != nil) | |
| 64 | − | |
| 65 | − if expectErr != testCase.expectErr { | |
| 66 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 67 | − } | |
| 68 | − | |
| 69 | − if !testCase.expectErr { | |
| 70 | − if string(result) != testCase.expectedResult { | |
| 71 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, string(result)) | |
| 72 | − } | |
| 73 | − } | |
| 74 | − } | |
| 75 | −} | |
| 76 | − | |
| 77 | −func TestValueSetUnmarshalJSON(t *testing.T) { | |
| 78 | − set1 := NewValueSet( | |
| 79 | − NewBoolValue(true), | |
| 80 | − NewStringValue("false"), | |
| 81 | − NewIntValue(7), | |
| 82 | − NewStringValue("7"), | |
| 83 | − NewStringValue("foo"), | |
| 84 | − NewStringValue("192.168.1.100/24"), | |
| 85 | − ) | |
| 86 | − | |
| 87 | − testCases := []struct { | |
| 88 | − data []byte | |
| 89 | − expectedResult ValueSet | |
| 90 | − expectErr bool | |
| 91 | − }{ | |
| 92 | − {[]byte(`true`), NewValueSet(NewBoolValue(true)), false}, | |
| 93 | − {[]byte(`7`), NewValueSet(NewIntValue(7)), false}, | |
| 94 | − {[]byte(`"foo"`), NewValueSet(NewStringValue("foo")), false}, | |
| 95 | − {[]byte(`[true]`), NewValueSet(NewBoolValue(true)), false}, | |
| 96 | − {[]byte(`[7]`), NewValueSet(NewIntValue(7)), false}, | |
| 97 | − {[]byte(`["foo"]`), NewValueSet(NewStringValue("foo")), false}, | |
| 98 | − {[]byte(`[true, "false", 7, "7", "foo", "192.168.1.100/24"]`), set1, false}, | |
| 99 | − {[]byte(`{}`), nil, true}, // Unsupported data. | |
| 100 | − {[]byte(`[]`), nil, true}, // Empty array. | |
| 101 | − {[]byte(`[7, 7, true]`), nil, true}, // Duplicate value. | |
| 102 | − } | |
| 103 | − | |
| 104 | − for i, testCase := range testCases { | |
| 105 | − result := make(ValueSet) | |
| 106 | − err := json.Unmarshal(testCase.data, &result) | |
| 107 | − expectErr := (err != nil) | |
| 108 | − | |
| 109 | − if expectErr != testCase.expectErr { | |
| 110 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 111 | − } | |
| 112 | − | |
| 113 | − if !testCase.expectErr { | |
| 114 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 115 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 116 | − } | |
| 117 | − } | |
| 118 | − } | |
| 119 | −} | |
| @@ -1,48 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −// Effect - policy statement effect Allow or Deny. | |
| 21 | −type Effect string | |
| 22 | − | |
| 23 | −const ( | |
| 24 | − // Allow - allow effect. | |
| 25 | − Allow Effect = "Allow" | |
| 26 | − | |
| 27 | − // Deny - deny effect. | |
| 28 | − Deny = "Deny" | |
| 29 | −) | |
| 30 | − | |
| 31 | −// IsAllowed - returns if given check is allowed or not. | |
| 32 | −func (effect Effect) IsAllowed(b bool) bool { | |
| 33 | − if effect == Allow { | |
| 34 | − return b | |
| 35 | − } | |
| 36 | − | |
| 37 | − return !b | |
| 38 | −} | |
| 39 | − | |
| 40 | −// IsValid - checks if Effect is valid or not | |
| 41 | −func (effect Effect) IsValid() bool { | |
| 42 | − switch effect { | |
| 43 | − case Allow, Deny: | |
| 44 | − return true | |
| 45 | − } | |
| 46 | − | |
| 47 | − return false | |
| 48 | −} | |
| @@ -1,64 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "testing" | |
| 22 | −) | |
| 23 | − | |
| 24 | −func TestEffectIsAllowed(t *testing.T) { | |
| 25 | − testCases := []struct { | |
| 26 | − effect Effect | |
| 27 | − check bool | |
| 28 | − expectedResult bool | |
| 29 | − }{ | |
| 30 | − {Allow, false, false}, | |
| 31 | − {Allow, true, true}, | |
| 32 | − {Deny, false, true}, | |
| 33 | − {Deny, true, false}, | |
| 34 | − } | |
| 35 | − | |
| 36 | − for i, testCase := range testCases { | |
| 37 | − result := testCase.effect.IsAllowed(testCase.check) | |
| 38 | − | |
| 39 | − if result != testCase.expectedResult { | |
| 40 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 41 | − } | |
| 42 | − } | |
| 43 | − | |
| 44 | −} | |
| 45 | − | |
| 46 | −func TestEffectIsValid(t *testing.T) { | |
| 47 | − testCases := []struct { | |
| 48 | − effect Effect | |
| 49 | − expectedResult bool | |
| 50 | − }{ | |
| 51 | − {Allow, true}, | |
| 52 | − {Deny, true}, | |
| 53 | − {Effect(""), false}, | |
| 54 | − {Effect("foo"), false}, | |
| 55 | − } | |
| 56 | − | |
| 57 | − for i, testCase := range testCases { | |
| 58 | − result := testCase.effect.IsValid() | |
| 59 | − | |
| 60 | − if result != testCase.expectedResult { | |
| 61 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 62 | − } | |
| 63 | − } | |
| 64 | −} | |
| @@ -1,45 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | −) | |
| 23 | − | |
| 24 | −// Error is the generic type for any error happening during policy | |
| 25 | −// parsing. | |
| 26 | −type Error struct { | |
| 27 | − err error | |
| 28 | −} | |
| 29 | − | |
| 30 | −// Errorf - formats according to a format specifier and returns | |
| 31 | −// the string as a value that satisfies error of type policy.Error | |
| 32 | −func Errorf(format string, a ...interface{}) error { | |
| 33 | − return Error{err: fmt.Errorf(format, a...)} | |
| 34 | −} | |
| 35 | − | |
| 36 | −// Unwrap the internal error. | |
| 37 | −func (e Error) Unwrap() error { return e.err } | |
| 38 | − | |
| 39 | −// Error 'error' compatible method. | |
| 40 | −func (e Error) Error() string { | |
| 41 | − if e.err == nil { | |
| 42 | − return "policy: cause <nil>" | |
| 43 | − } | |
| 44 | − return e.err.Error() | |
| 45 | −} | |
| @@ -1,30 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "unicode/utf8" | |
| 22 | −) | |
| 23 | − | |
| 24 | −// ID - policy ID. | |
| 25 | −type ID string | |
| 26 | − | |
| 27 | −// IsValid - checks if ID is valid or not. | |
| 28 | −func (id ID) IsValid() bool { | |
| 29 | − return utf8.ValidString(string(id)) | |
| 30 | −} | |
| @@ -1,41 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "testing" | |
| 22 | −) | |
| 23 | − | |
| 24 | −func TestIDIsValid(t *testing.T) { | |
| 25 | − testCases := []struct { | |
| 26 | − id ID | |
| 27 | − expectedResult bool | |
| 28 | − }{ | |
| 29 | − {ID("DenyEncryptionSt1"), true}, | |
| 30 | − {ID(""), true}, | |
| 31 | − {ID("aa\xe2"), false}, | |
| 32 | − } | |
| 33 | − | |
| 34 | − for i, testCase := range testCases { | |
| 35 | − result := testCase.id.IsValid() | |
| 36 | − | |
| 37 | − if result != testCase.expectedResult { | |
| 38 | − t.Errorf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 39 | − } | |
| 40 | − } | |
| 41 | −} | |
| @@ -1,185 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "io" | |
| 23 | −) | |
| 24 | − | |
| 25 | −// DefaultVersion - default policy version as per AWS S3 specification. | |
| 26 | −const DefaultVersion = "2012-10-17" | |
| 27 | − | |
| 28 | −// Args - arguments to policy to check whether it is allowed | |
| 29 | −type Args struct { | |
| 30 | − AccountName string `json:"account"` | |
| 31 | − Groups []string `json:"groups"` | |
| 32 | − Action Action `json:"action"` | |
| 33 | − BucketName string `json:"bucket"` | |
| 34 | − ConditionValues map[string][]string `json:"conditions"` | |
| 35 | − IsOwner bool `json:"owner"` | |
| 36 | − ObjectName string `json:"object"` | |
| 37 | −} | |
| 38 | − | |
| 39 | −// Policy - bucket policy. | |
| 40 | −type Policy struct { | |
| 41 | − ID ID `json:"ID,omitempty"` | |
| 42 | − Version string | |
| 43 | − Statements []Statement `json:"Statement"` | |
| 44 | −} | |
| 45 | − | |
| 46 | −// IsAllowed - checks given policy args is allowed to continue the Rest API. | |
| 47 | −func (policy Policy) IsAllowed(args Args) bool { | |
| 48 | − // Check all deny statements. If any one statement denies, return false. | |
| 49 | − for _, statement := range policy.Statements { | |
| 50 | − if statement.Effect == Deny { | |
| 51 | − if !statement.IsAllowed(args) { | |
| 52 | − return false | |
| 53 | − } | |
| 54 | − } | |
| 55 | − } | |
| 56 | − | |
| 57 | − // For owner, its allowed by default. | |
| 58 | − if args.IsOwner { | |
| 59 | − return true | |
| 60 | − } | |
| 61 | − | |
| 62 | − // Check all allow statements. If any one statement allows, return true. | |
| 63 | − for _, statement := range policy.Statements { | |
| 64 | − if statement.Effect == Allow { | |
| 65 | − if statement.IsAllowed(args) { | |
| 66 | − return true | |
| 67 | − } | |
| 68 | − } | |
| 69 | − } | |
| 70 | − | |
| 71 | − return false | |
| 72 | −} | |
| 73 | − | |
| 74 | −// IsEmpty - returns whether policy is empty or not. | |
| 75 | −func (policy Policy) IsEmpty() bool { | |
| 76 | − return len(policy.Statements) == 0 | |
| 77 | −} | |
| 78 | − | |
| 79 | −// isValid - checks if Policy is valid or not. | |
| 80 | −func (policy Policy) isValid() error { | |
| 81 | − if policy.Version != DefaultVersion && policy.Version != "" { | |
| 82 | − return Errorf("invalid version '%v'", policy.Version) | |
| 83 | − } | |
| 84 | − | |
| 85 | − for _, statement := range policy.Statements { | |
| 86 | − if err := statement.isValid(); err != nil { | |
| 87 | − return err | |
| 88 | − } | |
| 89 | − } | |
| 90 | − | |
| 91 | − return nil | |
| 92 | −} | |
| 93 | − | |
| 94 | −// MarshalJSON - encodes Policy to JSON data. | |
| 95 | −func (policy Policy) MarshalJSON() ([]byte, error) { | |
| 96 | − if err := policy.isValid(); err != nil { | |
| 97 | − return nil, err | |
| 98 | − } | |
| 99 | − | |
| 100 | − // subtype to avoid recursive call to MarshalJSON() | |
| 101 | − type subPolicy Policy | |
| 102 | − return json.Marshal(subPolicy(policy)) | |
| 103 | −} | |
| 104 | − | |
| 105 | −// Merge merges two policies documents and drop | |
| 106 | −// duplicate statements if any. | |
| 107 | −func (policy Policy) Merge(input Policy) Policy { | |
| 108 | − var mergedPolicy Policy | |
| 109 | − if policy.Version != "" { | |
| 110 | − mergedPolicy.Version = policy.Version | |
| 111 | − } else { | |
| 112 | − mergedPolicy.Version = input.Version | |
| 113 | − } | |
| 114 | − for _, st := range policy.Statements { | |
| 115 | − mergedPolicy.Statements = append(mergedPolicy.Statements, st.Clone()) | |
| 116 | − } | |
| 117 | − for _, st := range input.Statements { | |
| 118 | − mergedPolicy.Statements = append(mergedPolicy.Statements, st.Clone()) | |
| 119 | − } | |
| 120 | − mergedPolicy.dropDuplicateStatements() | |
| 121 | − return mergedPolicy | |
| 122 | −} | |
| 123 | − | |
| 124 | −func (policy *Policy) dropDuplicateStatements() { | |
| 125 | −redo: | |
| 126 | − for i := range policy.Statements { | |
| 127 | − for j, statement := range policy.Statements[i+1:] { | |
| 128 | − if !policy.Statements[i].Equals(statement) { | |
| 129 | − continue | |
| 130 | − } | |
| 131 | − policy.Statements = append(policy.Statements[:j], policy.Statements[j+1:]...) | |
| 132 | − goto redo | |
| 133 | − } | |
| 134 | − } | |
| 135 | −} | |
| 136 | − | |
| 137 | −// UnmarshalJSON - decodes JSON data to Policy. | |
| 138 | −func (policy *Policy) UnmarshalJSON(data []byte) error { | |
| 139 | − // subtype to avoid recursive call to UnmarshalJSON() | |
| 140 | − type subPolicy Policy | |
| 141 | − var sp subPolicy | |
| 142 | − if err := json.Unmarshal(data, &sp); err != nil { | |
| 143 | − return err | |
| 144 | − } | |
| 145 | − | |
| 146 | − p := Policy(sp) | |
| 147 | − if err := p.isValid(); err != nil { | |
| 148 | − return err | |
| 149 | − } | |
| 150 | − | |
| 151 | − p.dropDuplicateStatements() | |
| 152 | − | |
| 153 | − *policy = p | |
| 154 | − | |
| 155 | − return nil | |
| 156 | −} | |
| 157 | − | |
| 158 | −// Validate - validates all statements are for given bucket or not. | |
| 159 | −func (policy Policy) Validate(bucketName string) error { | |
| 160 | − if err := policy.isValid(); err != nil { | |
| 161 | − return err | |
| 162 | − } | |
| 163 | − | |
| 164 | − for _, statement := range policy.Statements { | |
| 165 | − if err := statement.Validate(bucketName); err != nil { | |
| 166 | − return err | |
| 167 | − } | |
| 168 | − } | |
| 169 | − | |
| 170 | − return nil | |
| 171 | −} | |
| 172 | − | |
| 173 | −// ParseConfig - parses data in given reader to Policy. | |
| 174 | −func ParseConfig(reader io.Reader, bucketName string) (*Policy, error) { | |
| 175 | − var policy Policy | |
| 176 | − | |
| 177 | − decoder := json.NewDecoder(reader) | |
| 178 | − decoder.DisallowUnknownFields() | |
| 179 | − if err := decoder.Decode(&policy); err != nil { | |
| 180 | − return nil, Errorf("%w", err) | |
| 181 | − } | |
| 182 | − | |
| 183 | − err := policy.Validate(bucketName) | |
| 184 | − return &policy, err | |
| 185 | −} | |
| @@ -1,1241 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "net" | |
| 23 | − "reflect" | |
| 24 | − "testing" | |
| 25 | − | |
| 26 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 27 | −) | |
| 28 | − | |
| 29 | −func TestPolicyIsAllowed(t *testing.T) { | |
| 30 | − case1Policy := Policy{ | |
| 31 | − Version: DefaultVersion, | |
| 32 | − Statements: []Statement{ | |
| 33 | − NewStatement( | |
| 34 | − Allow, | |
| 35 | − NewPrincipal("*"), | |
| 36 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 37 | − NewResourceSet(NewResource("*", "")), | |
| 38 | − condition.NewFunctions(), | |
| 39 | − )}, | |
| 40 | − } | |
| 41 | − | |
| 42 | − case2Policy := Policy{ | |
| 43 | − Version: DefaultVersion, | |
| 44 | − Statements: []Statement{ | |
| 45 | − NewStatement( | |
| 46 | − Allow, | |
| 47 | − NewPrincipal("*"), | |
| 48 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 49 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 50 | − condition.NewFunctions(), | |
| 51 | − )}, | |
| 52 | − } | |
| 53 | − | |
| 54 | − _, IPNet, err := net.ParseCIDR("192.168.1.0/24") | |
| 55 | − if err != nil { | |
| 56 | − t.Fatalf("unexpected error. %v\n", err) | |
| 57 | − } | |
| 58 | − func1, err := condition.NewIPAddressFunc( | |
| 59 | − condition.AWSSourceIP, | |
| 60 | − IPNet, | |
| 61 | − ) | |
| 62 | − if err != nil { | |
| 63 | − t.Fatalf("unexpected error. %v\n", err) | |
| 64 | − } | |
| 65 | − | |
| 66 | − case3Policy := Policy{ | |
| 67 | − Version: DefaultVersion, | |
| 68 | − Statements: []Statement{ | |
| 69 | − NewStatement( | |
| 70 | − Allow, | |
| 71 | − NewPrincipal("*"), | |
| 72 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 73 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 74 | − condition.NewFunctions(func1), | |
| 75 | − )}, | |
| 76 | − } | |
| 77 | − | |
| 78 | − case4Policy := Policy{ | |
| 79 | − Version: DefaultVersion, | |
| 80 | − Statements: []Statement{ | |
| 81 | − NewStatement( | |
| 82 | − Deny, | |
| 83 | − NewPrincipal("*"), | |
| 84 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 85 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 86 | − condition.NewFunctions(func1), | |
| 87 | − )}, | |
| 88 | − } | |
| 89 | − | |
| 90 | − anonGetBucketLocationArgs := Args{ | |
| 91 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 92 | − Action: GetBucketLocationAction, | |
| 93 | − BucketName: "mybucket", | |
| 94 | − ConditionValues: map[string][]string{}, | |
| 95 | − } | |
| 96 | − | |
| 97 | − anonPutObjectActionArgs := Args{ | |
| 98 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 99 | − Action: PutObjectAction, | |
| 100 | − BucketName: "mybucket", | |
| 101 | − ConditionValues: map[string][]string{ | |
| 102 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 103 | − "SourceIp": {"192.168.1.10"}, | |
| 104 | − }, | |
| 105 | − ObjectName: "myobject", | |
| 106 | − } | |
| 107 | − | |
| 108 | − anonGetObjectActionArgs := Args{ | |
| 109 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 110 | − Action: GetObjectAction, | |
| 111 | − BucketName: "mybucket", | |
| 112 | − ConditionValues: map[string][]string{}, | |
| 113 | − ObjectName: "myobject", | |
| 114 | − } | |
| 115 | − | |
| 116 | − getBucketLocationArgs := Args{ | |
| 117 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 118 | − Action: GetBucketLocationAction, | |
| 119 | − BucketName: "mybucket", | |
| 120 | − ConditionValues: map[string][]string{}, | |
| 121 | − IsOwner: true, | |
| 122 | − } | |
| 123 | − | |
| 124 | − putObjectActionArgs := Args{ | |
| 125 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 126 | − Action: PutObjectAction, | |
| 127 | − BucketName: "mybucket", | |
| 128 | − ConditionValues: map[string][]string{ | |
| 129 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 130 | − "SourceIp": {"192.168.1.10"}, | |
| 131 | − }, | |
| 132 | − IsOwner: true, | |
| 133 | − ObjectName: "myobject", | |
| 134 | − } | |
| 135 | − | |
| 136 | − getObjectActionArgs := Args{ | |
| 137 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 138 | − Action: GetObjectAction, | |
| 139 | − BucketName: "mybucket", | |
| 140 | − ConditionValues: map[string][]string{}, | |
| 141 | − IsOwner: true, | |
| 142 | − ObjectName: "myobject", | |
| 143 | − } | |
| 144 | − | |
| 145 | − testCases := []struct { | |
| 146 | − policy Policy | |
| 147 | − args Args | |
| 148 | − expectedResult bool | |
| 149 | − }{ | |
| 150 | − {case1Policy, anonGetBucketLocationArgs, true}, | |
| 151 | − {case1Policy, anonPutObjectActionArgs, true}, | |
| 152 | − {case1Policy, anonGetObjectActionArgs, false}, | |
| 153 | − {case1Policy, getBucketLocationArgs, true}, | |
| 154 | − {case1Policy, putObjectActionArgs, true}, | |
| 155 | − {case1Policy, getObjectActionArgs, true}, | |
| 156 | − | |
| 157 | − {case2Policy, anonGetBucketLocationArgs, false}, | |
| 158 | − {case2Policy, anonPutObjectActionArgs, true}, | |
| 159 | − {case2Policy, anonGetObjectActionArgs, true}, | |
| 160 | − {case2Policy, getBucketLocationArgs, true}, | |
| 161 | − {case2Policy, putObjectActionArgs, true}, | |
| 162 | − {case2Policy, getObjectActionArgs, true}, | |
| 163 | − | |
| 164 | − {case3Policy, anonGetBucketLocationArgs, false}, | |
| 165 | − {case3Policy, anonPutObjectActionArgs, true}, | |
| 166 | − {case3Policy, anonGetObjectActionArgs, false}, | |
| 167 | − {case3Policy, getBucketLocationArgs, true}, | |
| 168 | − {case3Policy, putObjectActionArgs, true}, | |
| 169 | − {case3Policy, getObjectActionArgs, true}, | |
| 170 | − | |
| 171 | − {case4Policy, anonGetBucketLocationArgs, false}, | |
| 172 | − {case4Policy, anonPutObjectActionArgs, false}, | |
| 173 | − {case4Policy, anonGetObjectActionArgs, false}, | |
| 174 | − {case4Policy, getBucketLocationArgs, true}, | |
| 175 | − {case4Policy, putObjectActionArgs, false}, | |
| 176 | − {case4Policy, getObjectActionArgs, true}, | |
| 177 | − } | |
| 178 | − | |
| 179 | − for i, testCase := range testCases { | |
| 180 | − result := testCase.policy.IsAllowed(testCase.args) | |
| 181 | − | |
| 182 | − if result != testCase.expectedResult { | |
| 183 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 184 | − } | |
| 185 | − } | |
| 186 | −} | |
| 187 | − | |
| 188 | −func TestPolicyIsEmpty(t *testing.T) { | |
| 189 | − case1Policy := Policy{ | |
| 190 | − Version: DefaultVersion, | |
| 191 | − Statements: []Statement{ | |
| 192 | − NewStatement( | |
| 193 | − Allow, | |
| 194 | − NewPrincipal("*"), | |
| 195 | − NewActionSet(PutObjectAction), | |
| 196 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 197 | − condition.NewFunctions(), | |
| 198 | − ), | |
| 199 | − }, | |
| 200 | − } | |
| 201 | − | |
| 202 | − case2Policy := Policy{ | |
| 203 | − ID: "MyPolicyForMyBucket", | |
| 204 | − Version: DefaultVersion, | |
| 205 | − } | |
| 206 | − | |
| 207 | − testCases := []struct { | |
| 208 | − policy Policy | |
| 209 | − expectedResult bool | |
| 210 | − }{ | |
| 211 | − {case1Policy, false}, | |
| 212 | − {case2Policy, true}, | |
| 213 | − } | |
| 214 | − | |
| 215 | − for i, testCase := range testCases { | |
| 216 | − result := testCase.policy.IsEmpty() | |
| 217 | − | |
| 218 | − if result != testCase.expectedResult { | |
| 219 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 220 | − } | |
| 221 | − } | |
| 222 | −} | |
| 223 | − | |
| 224 | −func TestPolicyIsValid(t *testing.T) { | |
| 225 | − case1Policy := Policy{ | |
| 226 | − Version: DefaultVersion, | |
| 227 | − Statements: []Statement{ | |
| 228 | − NewStatement( | |
| 229 | − Allow, | |
| 230 | − NewPrincipal("*"), | |
| 231 | − NewActionSet(PutObjectAction), | |
| 232 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 233 | − condition.NewFunctions(), | |
| 234 | − ), | |
| 235 | − }, | |
| 236 | − } | |
| 237 | − | |
| 238 | − case2Policy := Policy{ | |
| 239 | − Version: DefaultVersion, | |
| 240 | − Statements: []Statement{ | |
| 241 | − NewStatement( | |
| 242 | − Allow, | |
| 243 | − NewPrincipal("*"), | |
| 244 | − NewActionSet(PutObjectAction), | |
| 245 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 246 | − condition.NewFunctions(), | |
| 247 | − ), | |
| 248 | − NewStatement( | |
| 249 | − Deny, | |
| 250 | − NewPrincipal("*"), | |
| 251 | − NewActionSet(GetObjectAction), | |
| 252 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 253 | − condition.NewFunctions(), | |
| 254 | − ), | |
| 255 | − }, | |
| 256 | − } | |
| 257 | − | |
| 258 | − case3Policy := Policy{ | |
| 259 | − Version: DefaultVersion, | |
| 260 | − Statements: []Statement{ | |
| 261 | − NewStatement( | |
| 262 | − Allow, | |
| 263 | − NewPrincipal("*"), | |
| 264 | − NewActionSet(PutObjectAction), | |
| 265 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 266 | − condition.NewFunctions(), | |
| 267 | − ), | |
| 268 | − NewStatement( | |
| 269 | − Deny, | |
| 270 | − NewPrincipal("*"), | |
| 271 | − NewActionSet(PutObjectAction), | |
| 272 | − NewResourceSet(NewResource("mybucket", "/yourobject*")), | |
| 273 | − condition.NewFunctions(), | |
| 274 | − ), | |
| 275 | − }, | |
| 276 | − } | |
| 277 | − | |
| 278 | − func1, err := condition.NewNullFunc( | |
| 279 | − condition.S3XAmzCopySource, | |
| 280 | − true, | |
| 281 | − ) | |
| 282 | − if err != nil { | |
| 283 | − t.Fatalf("unexpected error. %v\n", err) | |
| 284 | − } | |
| 285 | − func2, err := condition.NewNullFunc( | |
| 286 | − condition.S3XAmzServerSideEncryption, | |
| 287 | − false, | |
| 288 | − ) | |
| 289 | − if err != nil { | |
| 290 | − t.Fatalf("unexpected error. %v\n", err) | |
| 291 | − } | |
| 292 | − | |
| 293 | − case4Policy := Policy{ | |
| 294 | − Version: DefaultVersion, | |
| 295 | − Statements: []Statement{ | |
| 296 | − NewStatement( | |
| 297 | − Allow, | |
| 298 | − NewPrincipal("*"), | |
| 299 | − NewActionSet(PutObjectAction), | |
| 300 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 301 | − condition.NewFunctions(func1), | |
| 302 | − ), | |
| 303 | − NewStatement( | |
| 304 | − Deny, | |
| 305 | − NewPrincipal("*"), | |
| 306 | − NewActionSet(PutObjectAction), | |
| 307 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 308 | − condition.NewFunctions(func2), | |
| 309 | − ), | |
| 310 | − }, | |
| 311 | − } | |
| 312 | − | |
| 313 | − case5Policy := Policy{ | |
| 314 | − Version: "17-10-2012", | |
| 315 | − Statements: []Statement{ | |
| 316 | − NewStatement( | |
| 317 | − Allow, | |
| 318 | − NewPrincipal("*"), | |
| 319 | − NewActionSet(PutObjectAction), | |
| 320 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 321 | − condition.NewFunctions(), | |
| 322 | − ), | |
| 323 | − }, | |
| 324 | − } | |
| 325 | − | |
| 326 | − case6Policy := Policy{ | |
| 327 | − ID: "MyPolicyForMyBucket1", | |
| 328 | − Version: DefaultVersion, | |
| 329 | − Statements: []Statement{ | |
| 330 | − NewStatement( | |
| 331 | − Allow, | |
| 332 | − NewPrincipal("*"), | |
| 333 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 334 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 335 | − condition.NewFunctions(func1, func2), | |
| 336 | − ), | |
| 337 | − }, | |
| 338 | − } | |
| 339 | − | |
| 340 | − case7Policy := Policy{ | |
| 341 | − Version: DefaultVersion, | |
| 342 | − Statements: []Statement{ | |
| 343 | − NewStatement( | |
| 344 | − Allow, | |
| 345 | − NewPrincipal("*"), | |
| 346 | − NewActionSet(PutObjectAction), | |
| 347 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 348 | − condition.NewFunctions(), | |
| 349 | − ), | |
| 350 | − NewStatement( | |
| 351 | − Deny, | |
| 352 | − NewPrincipal("*"), | |
| 353 | − NewActionSet(PutObjectAction), | |
| 354 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 355 | − condition.NewFunctions(), | |
| 356 | − ), | |
| 357 | − }, | |
| 358 | − } | |
| 359 | − | |
| 360 | − case8Policy := Policy{ | |
| 361 | − Version: DefaultVersion, | |
| 362 | − Statements: []Statement{ | |
| 363 | − NewStatement( | |
| 364 | − Allow, | |
| 365 | − NewPrincipal("*"), | |
| 366 | − NewActionSet(PutObjectAction), | |
| 367 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 368 | − condition.NewFunctions(), | |
| 369 | − ), | |
| 370 | − NewStatement( | |
| 371 | − Allow, | |
| 372 | − NewPrincipal("*"), | |
| 373 | − NewActionSet(PutObjectAction), | |
| 374 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 375 | − condition.NewFunctions(), | |
| 376 | − ), | |
| 377 | − }, | |
| 378 | − } | |
| 379 | − | |
| 380 | − testCases := []struct { | |
| 381 | − policy Policy | |
| 382 | − expectErr bool | |
| 383 | − }{ | |
| 384 | − {case1Policy, false}, | |
| 385 | − // allowed duplicate principal. | |
| 386 | − {case2Policy, false}, | |
| 387 | − // allowed duplicate principal and action. | |
| 388 | − {case3Policy, false}, | |
| 389 | − // allowed duplicate principal, action and resource. | |
| 390 | − {case4Policy, false}, | |
| 391 | − // Invalid version error. | |
| 392 | − {case5Policy, true}, | |
| 393 | − // Invalid statement error. | |
| 394 | − {case6Policy, true}, | |
| 395 | − // Duplicate statement success different effects. | |
| 396 | − {case7Policy, false}, | |
| 397 | − // Duplicate statement success, duplicate statement dropped. | |
| 398 | − {case8Policy, false}, | |
| 399 | − } | |
| 400 | − | |
| 401 | − for i, testCase := range testCases { | |
| 402 | − err := testCase.policy.isValid() | |
| 403 | − expectErr := (err != nil) | |
| 404 | − | |
| 405 | − if expectErr != testCase.expectErr { | |
| 406 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 407 | − } | |
| 408 | − } | |
| 409 | −} | |
| 410 | − | |
| 411 | −func TestPolicyMarshalJSON(t *testing.T) { | |
| 412 | − case1Policy := Policy{ | |
| 413 | − ID: "MyPolicyForMyBucket1", | |
| 414 | − Version: DefaultVersion, | |
| 415 | − Statements: []Statement{ | |
| 416 | − NewStatement( | |
| 417 | − Allow, | |
| 418 | − NewPrincipal("*"), | |
| 419 | − NewActionSet(PutObjectAction), | |
| 420 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 421 | − condition.NewFunctions(), | |
| 422 | − ), | |
| 423 | − }, | |
| 424 | − } | |
| 425 | − case1Policy.Statements[0].SID = "SomeId1" | |
| 426 | − case1Data := []byte(`{"ID":"MyPolicyForMyBucket1","Version":"2012-10-17","Statement":[{"Sid":"SomeId1","Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"]}]}`) | |
| 427 | − | |
| 428 | − _, IPNet1, err := net.ParseCIDR("192.168.1.0/24") | |
| 429 | − if err != nil { | |
| 430 | − t.Fatalf("unexpected error. %v\n", err) | |
| 431 | − } | |
| 432 | − func1, err := condition.NewIPAddressFunc( | |
| 433 | − condition.AWSSourceIP, | |
| 434 | − IPNet1, | |
| 435 | − ) | |
| 436 | − if err != nil { | |
| 437 | − t.Fatalf("unexpected error. %v\n", err) | |
| 438 | − } | |
| 439 | − | |
| 440 | − case2Policy := Policy{ | |
| 441 | − Version: DefaultVersion, | |
| 442 | − Statements: []Statement{ | |
| 443 | − NewStatement( | |
| 444 | − Allow, | |
| 445 | − NewPrincipal("*"), | |
| 446 | − NewActionSet(PutObjectAction), | |
| 447 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 448 | − condition.NewFunctions(), | |
| 449 | − ), | |
| 450 | − NewStatement( | |
| 451 | − Deny, | |
| 452 | − NewPrincipal("*"), | |
| 453 | − NewActionSet(GetObjectAction), | |
| 454 | − NewResourceSet(NewResource("mybucket", "/yourobject*")), | |
| 455 | − condition.NewFunctions(func1), | |
| 456 | − ), | |
| 457 | − }, | |
| 458 | − } | |
| 459 | − case2Data := []byte(`{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"]},{"Effect":"Deny","Principal":{"AWS":["*"]},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::mybucket/yourobject*"],"Condition":{"IpAddress":{"aws:SourceIp":["192.168.1.0/24"]}}}]}`) | |
| 460 | − | |
| 461 | − case3Policy := Policy{ | |
| 462 | − ID: "MyPolicyForMyBucket1", | |
| 463 | − Version: DefaultVersion, | |
| 464 | − Statements: []Statement{ | |
| 465 | − NewStatement( | |
| 466 | − Allow, | |
| 467 | − NewPrincipal("Q3AM3UQ867SPQQA43P2F"), | |
| 468 | − NewActionSet(PutObjectAction), | |
| 469 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 470 | − condition.NewFunctions(), | |
| 471 | − ), | |
| 472 | − NewStatement( | |
| 473 | − Allow, | |
| 474 | − NewPrincipal("*"), | |
| 475 | − NewActionSet(PutObjectAction), | |
| 476 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 477 | − condition.NewFunctions(), | |
| 478 | − ), | |
| 479 | − }, | |
| 480 | − } | |
| 481 | − case3Data := []byte(`{"ID":"MyPolicyForMyBucket1","Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["Q3AM3UQ867SPQQA43P2F"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"]},{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"]}]}`) | |
| 482 | − | |
| 483 | − case4Policy := Policy{ | |
| 484 | − ID: "MyPolicyForMyBucket1", | |
| 485 | − Version: DefaultVersion, | |
| 486 | − Statements: []Statement{ | |
| 487 | − NewStatement( | |
| 488 | − Allow, | |
| 489 | − NewPrincipal("*"), | |
| 490 | − NewActionSet(PutObjectAction), | |
| 491 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 492 | − condition.NewFunctions(), | |
| 493 | − ), | |
| 494 | − NewStatement( | |
| 495 | − Allow, | |
| 496 | − NewPrincipal("*"), | |
| 497 | − NewActionSet(GetObjectAction), | |
| 498 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 499 | − condition.NewFunctions(), | |
| 500 | − ), | |
| 501 | − }, | |
| 502 | − } | |
| 503 | − case4Data := []byte(`{"ID":"MyPolicyForMyBucket1","Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"]},{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"]}]}`) | |
| 504 | − | |
| 505 | − case5Policy := Policy{ | |
| 506 | − ID: "MyPolicyForMyBucket1", | |
| 507 | − Version: DefaultVersion, | |
| 508 | − Statements: []Statement{ | |
| 509 | − NewStatement( | |
| 510 | − Allow, | |
| 511 | − NewPrincipal("*"), | |
| 512 | − NewActionSet(PutObjectAction), | |
| 513 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 514 | − condition.NewFunctions(), | |
| 515 | − ), | |
| 516 | − NewStatement( | |
| 517 | − Allow, | |
| 518 | − NewPrincipal("*"), | |
| 519 | − NewActionSet(PutObjectAction), | |
| 520 | − NewResourceSet(NewResource("mybucket", "/yourobject*")), | |
| 521 | − condition.NewFunctions(), | |
| 522 | − ), | |
| 523 | − }, | |
| 524 | − } | |
| 525 | − case5Data := []byte(`{"ID":"MyPolicyForMyBucket1","Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"]},{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/yourobject*"]}]}`) | |
| 526 | − | |
| 527 | − _, IPNet2, err := net.ParseCIDR("192.168.2.0/24") | |
| 528 | − if err != nil { | |
| 529 | − t.Fatalf("unexpected error. %v\n", err) | |
| 530 | − } | |
| 531 | − func2, err := condition.NewIPAddressFunc( | |
| 532 | − condition.AWSSourceIP, | |
| 533 | − IPNet2, | |
| 534 | − ) | |
| 535 | − if err != nil { | |
| 536 | − t.Fatalf("unexpected error. %v\n", err) | |
| 537 | − } | |
| 538 | − | |
| 539 | − case6Policy := Policy{ | |
| 540 | − ID: "MyPolicyForMyBucket1", | |
| 541 | − Version: DefaultVersion, | |
| 542 | − Statements: []Statement{ | |
| 543 | − NewStatement( | |
| 544 | − Allow, | |
| 545 | − NewPrincipal("*"), | |
| 546 | − NewActionSet(PutObjectAction), | |
| 547 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 548 | − condition.NewFunctions(func1), | |
| 549 | − ), | |
| 550 | − NewStatement( | |
| 551 | − Allow, | |
| 552 | − NewPrincipal("*"), | |
| 553 | − NewActionSet(PutObjectAction), | |
| 554 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 555 | − condition.NewFunctions(func2), | |
| 556 | − ), | |
| 557 | − }, | |
| 558 | − } | |
| 559 | − case6Data := []byte(`{"ID":"MyPolicyForMyBucket1","Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"],"Condition":{"IpAddress":{"aws:SourceIp":["192.168.1.0/24"]}}},{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"],"Condition":{"IpAddress":{"aws:SourceIp":["192.168.2.0/24"]}}}]}`) | |
| 560 | − | |
| 561 | − case7Policy := Policy{ | |
| 562 | − ID: "MyPolicyForMyBucket1", | |
| 563 | − Version: DefaultVersion, | |
| 564 | − Statements: []Statement{ | |
| 565 | − NewStatement( | |
| 566 | − Allow, | |
| 567 | − NewPrincipal("*"), | |
| 568 | − NewActionSet(GetBucketLocationAction), | |
| 569 | − NewResourceSet(NewResource("mybucket", "")), | |
| 570 | − condition.NewFunctions(), | |
| 571 | − ), | |
| 572 | − }, | |
| 573 | − } | |
| 574 | − case7Data := []byte(`{"ID":"MyPolicyForMyBucket1","Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetBucketLocation"],"Resource":["arn:aws:s3:::mybucket"]}]}`) | |
| 575 | − | |
| 576 | − case8Policy := Policy{ | |
| 577 | − ID: "MyPolicyForMyBucket1", | |
| 578 | − Version: DefaultVersion, | |
| 579 | − Statements: []Statement{ | |
| 580 | − NewStatement( | |
| 581 | − Allow, | |
| 582 | − NewPrincipal("*"), | |
| 583 | − NewActionSet(GetBucketLocationAction), | |
| 584 | − NewResourceSet(NewResource("*", "")), | |
| 585 | − condition.NewFunctions(), | |
| 586 | − ), | |
| 587 | − }, | |
| 588 | − } | |
| 589 | − case8Data := []byte(`{"ID":"MyPolicyForMyBucket1","Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:GetBucketLocation"],"Resource":["arn:aws:s3:::*"]}]}`) | |
| 590 | − | |
| 591 | − func3, err := condition.NewNullFunc( | |
| 592 | − condition.S3XAmzCopySource, | |
| 593 | − true, | |
| 594 | − ) | |
| 595 | − if err != nil { | |
| 596 | − t.Fatalf("unexpected error. %v\n", err) | |
| 597 | − } | |
| 598 | − case9Policy := Policy{ | |
| 599 | − ID: "MyPolicyForMyBucket1", | |
| 600 | − Version: DefaultVersion, | |
| 601 | − Statements: []Statement{ | |
| 602 | − NewStatement( | |
| 603 | − Allow, | |
| 604 | − NewPrincipal("*"), | |
| 605 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 606 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 607 | − condition.NewFunctions(func1, func2, func3), | |
| 608 | − ), | |
| 609 | − }, | |
| 610 | − } | |
| 611 | − | |
| 612 | − testCases := []struct { | |
| 613 | − policy Policy | |
| 614 | − expectedResult []byte | |
| 615 | − expectErr bool | |
| 616 | − }{ | |
| 617 | − {case1Policy, case1Data, false}, | |
| 618 | − {case2Policy, case2Data, false}, | |
| 619 | − {case3Policy, case3Data, false}, | |
| 620 | − {case4Policy, case4Data, false}, | |
| 621 | − {case5Policy, case5Data, false}, | |
| 622 | − {case6Policy, case6Data, false}, | |
| 623 | − {case7Policy, case7Data, false}, | |
| 624 | − {case8Policy, case8Data, false}, | |
| 625 | − {case9Policy, nil, true}, | |
| 626 | − } | |
| 627 | − | |
| 628 | − for i, testCase := range testCases { | |
| 629 | − result, err := json.Marshal(testCase.policy) | |
| 630 | − expectErr := (err != nil) | |
| 631 | − | |
| 632 | − if expectErr != testCase.expectErr { | |
| 633 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 634 | − } | |
| 635 | − | |
| 636 | − if !testCase.expectErr { | |
| 637 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 638 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, string(testCase.expectedResult), string(result)) | |
| 639 | − } | |
| 640 | − } | |
| 641 | − } | |
| 642 | −} | |
| 643 | − | |
| 644 | −func TestPolicyUnmarshalJSON(t *testing.T) { | |
| 645 | − case1Data := []byte(`{ | |
| 646 | − "ID": "MyPolicyForMyBucket1", | |
| 647 | − "Version": "2012-10-17", | |
| 648 | − "Statement": [ | |
| 649 | − { | |
| 650 | − "Sid": "SomeId1", | |
| 651 | − "Effect": "Allow", | |
| 652 | − "Principal": "*", | |
| 653 | − "Action": "s3:PutObject", | |
| 654 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 655 | − } | |
| 656 | − ] | |
| 657 | −}`) | |
| 658 | − case1Policy := Policy{ | |
| 659 | − ID: "MyPolicyForMyBucket1", | |
| 660 | − Version: DefaultVersion, | |
| 661 | − Statements: []Statement{ | |
| 662 | − NewStatement( | |
| 663 | − Allow, | |
| 664 | − NewPrincipal("*"), | |
| 665 | − NewActionSet(PutObjectAction), | |
| 666 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 667 | − condition.NewFunctions(), | |
| 668 | − ), | |
| 669 | − }, | |
| 670 | − } | |
| 671 | − case1Policy.Statements[0].SID = "SomeId1" | |
| 672 | − | |
| 673 | − case2Data := []byte(`{ | |
| 674 | − "Version": "2012-10-17", | |
| 675 | − "Statement": [ | |
| 676 | − { | |
| 677 | − "Effect": "Allow", | |
| 678 | − "Principal": "*", | |
| 679 | − "Action": "s3:PutObject", | |
| 680 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 681 | − }, | |
| 682 | − { | |
| 683 | − "Effect": "Deny", | |
| 684 | − "Principal": "*", | |
| 685 | − "Action": "s3:GetObject", | |
| 686 | − "Resource": "arn:aws:s3:::mybucket/yourobject*", | |
| 687 | − "Condition": { | |
| 688 | − "IpAddress": { | |
| 689 | − "aws:SourceIp": "192.168.1.0/24" | |
| 690 | − } | |
| 691 | − } | |
| 692 | − } | |
| 693 | − ] | |
| 694 | −}`) | |
| 695 | − _, IPNet1, err := net.ParseCIDR("192.168.1.0/24") | |
| 696 | − if err != nil { | |
| 697 | − t.Fatalf("unexpected error. %v\n", err) | |
| 698 | − } | |
| 699 | − func1, err := condition.NewIPAddressFunc( | |
| 700 | − condition.AWSSourceIP, | |
| 701 | − IPNet1, | |
| 702 | − ) | |
| 703 | − if err != nil { | |
| 704 | − t.Fatalf("unexpected error. %v\n", err) | |
| 705 | − } | |
| 706 | − | |
| 707 | − case2Policy := Policy{ | |
| 708 | − Version: DefaultVersion, | |
| 709 | − Statements: []Statement{ | |
| 710 | − NewStatement( | |
| 711 | − Allow, | |
| 712 | − NewPrincipal("*"), | |
| 713 | − NewActionSet(PutObjectAction), | |
| 714 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 715 | − condition.NewFunctions(), | |
| 716 | − ), | |
| 717 | − NewStatement( | |
| 718 | − Deny, | |
| 719 | − NewPrincipal("*"), | |
| 720 | − NewActionSet(GetObjectAction), | |
| 721 | − NewResourceSet(NewResource("mybucket", "/yourobject*")), | |
| 722 | − condition.NewFunctions(func1), | |
| 723 | − ), | |
| 724 | − }, | |
| 725 | − } | |
| 726 | − | |
| 727 | − case3Data := []byte(`{ | |
| 728 | − "ID": "MyPolicyForMyBucket1", | |
| 729 | − "Version": "2012-10-17", | |
| 730 | − "Statement": [ | |
| 731 | − { | |
| 732 | − "Effect": "Allow", | |
| 733 | − "Principal": { | |
| 734 | − "AWS": [ | |
| 735 | − "Q3AM3UQ867SPQQA43P2F" | |
| 736 | − ] | |
| 737 | − }, | |
| 738 | − "Action": "s3:PutObject", | |
| 739 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 740 | − }, | |
| 741 | − { | |
| 742 | − "Effect": "Allow", | |
| 743 | − "Principal": "*", | |
| 744 | − "Action": "s3:PutObject", | |
| 745 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 746 | − } | |
| 747 | − ] | |
| 748 | −}`) | |
| 749 | − case3Policy := Policy{ | |
| 750 | − ID: "MyPolicyForMyBucket1", | |
| 751 | − Version: DefaultVersion, | |
| 752 | − Statements: []Statement{ | |
| 753 | − NewStatement( | |
| 754 | − Allow, | |
| 755 | − NewPrincipal("Q3AM3UQ867SPQQA43P2F"), | |
| 756 | − NewActionSet(PutObjectAction), | |
| 757 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 758 | − condition.NewFunctions(), | |
| 759 | − ), | |
| 760 | − NewStatement( | |
| 761 | − Allow, | |
| 762 | − NewPrincipal("*"), | |
| 763 | − NewActionSet(PutObjectAction), | |
| 764 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 765 | − condition.NewFunctions(), | |
| 766 | − ), | |
| 767 | − }, | |
| 768 | − } | |
| 769 | − | |
| 770 | − case4Data := []byte(`{ | |
| 771 | − "ID": "MyPolicyForMyBucket1", | |
| 772 | − "Version": "2012-10-17", | |
| 773 | − "Statement": [ | |
| 774 | − { | |
| 775 | − "Effect": "Allow", | |
| 776 | − "Principal": "*", | |
| 777 | − "Action": "s3:PutObject", | |
| 778 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 779 | − }, | |
| 780 | − { | |
| 781 | − "Effect": "Allow", | |
| 782 | − "Principal": "*", | |
| 783 | − "Action": "s3:GetObject", | |
| 784 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 785 | − } | |
| 786 | − ] | |
| 787 | −}`) | |
| 788 | − case4Policy := Policy{ | |
| 789 | − ID: "MyPolicyForMyBucket1", | |
| 790 | − Version: DefaultVersion, | |
| 791 | − Statements: []Statement{ | |
| 792 | − NewStatement( | |
| 793 | − Allow, | |
| 794 | − NewPrincipal("*"), | |
| 795 | − NewActionSet(PutObjectAction), | |
| 796 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 797 | − condition.NewFunctions(), | |
| 798 | − ), | |
| 799 | − NewStatement( | |
| 800 | − Allow, | |
| 801 | − NewPrincipal("*"), | |
| 802 | − NewActionSet(GetObjectAction), | |
| 803 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 804 | − condition.NewFunctions(), | |
| 805 | − ), | |
| 806 | − }, | |
| 807 | − } | |
| 808 | − | |
| 809 | − case5Data := []byte(`{ | |
| 810 | − "ID": "MyPolicyForMyBucket1", | |
| 811 | − "Version": "2012-10-17", | |
| 812 | − "Statement": [ | |
| 813 | − { | |
| 814 | − "Effect": "Allow", | |
| 815 | − "Principal": "*", | |
| 816 | − "Action": "s3:PutObject", | |
| 817 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 818 | − }, | |
| 819 | − { | |
| 820 | − "Effect": "Allow", | |
| 821 | − "Principal": "*", | |
| 822 | − "Action": "s3:PutObject", | |
| 823 | − "Resource": "arn:aws:s3:::mybucket/yourobject*" | |
| 824 | − } | |
| 825 | − ] | |
| 826 | −}`) | |
| 827 | − case5Policy := Policy{ | |
| 828 | − ID: "MyPolicyForMyBucket1", | |
| 829 | − Version: DefaultVersion, | |
| 830 | − Statements: []Statement{ | |
| 831 | − NewStatement( | |
| 832 | − Allow, | |
| 833 | − NewPrincipal("*"), | |
| 834 | − NewActionSet(PutObjectAction), | |
| 835 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 836 | − condition.NewFunctions(), | |
| 837 | − ), | |
| 838 | − NewStatement( | |
| 839 | − Allow, | |
| 840 | − NewPrincipal("*"), | |
| 841 | − NewActionSet(PutObjectAction), | |
| 842 | − NewResourceSet(NewResource("mybucket", "/yourobject*")), | |
| 843 | − condition.NewFunctions(), | |
| 844 | − ), | |
| 845 | − }, | |
| 846 | − } | |
| 847 | − | |
| 848 | − case6Data := []byte(`{ | |
| 849 | − "ID": "MyPolicyForMyBucket1", | |
| 850 | − "Version": "2012-10-17", | |
| 851 | − "Statement": [ | |
| 852 | − { | |
| 853 | − "Effect": "Allow", | |
| 854 | − "Principal": "*", | |
| 855 | − "Action": "s3:PutObject", | |
| 856 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 857 | − "Condition": { | |
| 858 | − "IpAddress": { | |
| 859 | − "aws:SourceIp": "192.168.1.0/24" | |
| 860 | − } | |
| 861 | − } | |
| 862 | − }, | |
| 863 | − { | |
| 864 | − "Effect": "Allow", | |
| 865 | − "Principal": "*", | |
| 866 | − "Action": "s3:PutObject", | |
| 867 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 868 | − "Condition": { | |
| 869 | − "IpAddress": { | |
| 870 | − "aws:SourceIp": "192.168.2.0/24" | |
| 871 | − } | |
| 872 | − } | |
| 873 | − } | |
| 874 | − ] | |
| 875 | −}`) | |
| 876 | − _, IPNet2, err := net.ParseCIDR("192.168.2.0/24") | |
| 877 | − if err != nil { | |
| 878 | − t.Fatalf("unexpected error. %v\n", err) | |
| 879 | − } | |
| 880 | − func2, err := condition.NewIPAddressFunc( | |
| 881 | − condition.AWSSourceIP, | |
| 882 | − IPNet2, | |
| 883 | − ) | |
| 884 | − if err != nil { | |
| 885 | − t.Fatalf("unexpected error. %v\n", err) | |
| 886 | − } | |
| 887 | − | |
| 888 | − case6Policy := Policy{ | |
| 889 | − ID: "MyPolicyForMyBucket1", | |
| 890 | − Version: DefaultVersion, | |
| 891 | − Statements: []Statement{ | |
| 892 | − NewStatement( | |
| 893 | − Allow, | |
| 894 | − NewPrincipal("*"), | |
| 895 | − NewActionSet(PutObjectAction), | |
| 896 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 897 | − condition.NewFunctions(func1), | |
| 898 | − ), | |
| 899 | − NewStatement( | |
| 900 | − Allow, | |
| 901 | − NewPrincipal("*"), | |
| 902 | − NewActionSet(PutObjectAction), | |
| 903 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 904 | − condition.NewFunctions(func2), | |
| 905 | − ), | |
| 906 | − }, | |
| 907 | − } | |
| 908 | − | |
| 909 | − case7Data := []byte(`{ | |
| 910 | − "ID": "MyPolicyForMyBucket1", | |
| 911 | − "Version": "2012-10-17", | |
| 912 | − "Statement": [ | |
| 913 | − { | |
| 914 | − "Effect": "Allow", | |
| 915 | − "Principal": "*", | |
| 916 | − "Action": "s3:GetBucketLocation", | |
| 917 | − "Resource": "arn:aws:s3:::mybucket" | |
| 918 | − } | |
| 919 | − ] | |
| 920 | −}`) | |
| 921 | − | |
| 922 | − case7Policy := Policy{ | |
| 923 | − ID: "MyPolicyForMyBucket1", | |
| 924 | − Version: DefaultVersion, | |
| 925 | − Statements: []Statement{ | |
| 926 | − NewStatement( | |
| 927 | − Allow, | |
| 928 | − NewPrincipal("*"), | |
| 929 | − NewActionSet(GetBucketLocationAction), | |
| 930 | − NewResourceSet(NewResource("mybucket", "")), | |
| 931 | − condition.NewFunctions(), | |
| 932 | − ), | |
| 933 | − }, | |
| 934 | − } | |
| 935 | − | |
| 936 | − case8Data := []byte(`{ | |
| 937 | − "ID": "MyPolicyForMyBucket1", | |
| 938 | − "Version": "2012-10-17", | |
| 939 | − "Statement": [ | |
| 940 | − { | |
| 941 | − "Effect": "Allow", | |
| 942 | − "Principal": "*", | |
| 943 | − "Action": "s3:GetBucketLocation", | |
| 944 | − "Resource": "arn:aws:s3:::*" | |
| 945 | − } | |
| 946 | − ] | |
| 947 | −}`) | |
| 948 | − | |
| 949 | − case8Policy := Policy{ | |
| 950 | − ID: "MyPolicyForMyBucket1", | |
| 951 | − Version: DefaultVersion, | |
| 952 | − Statements: []Statement{ | |
| 953 | − NewStatement( | |
| 954 | − Allow, | |
| 955 | − NewPrincipal("*"), | |
| 956 | − NewActionSet(GetBucketLocationAction), | |
| 957 | − NewResourceSet(NewResource("*", "")), | |
| 958 | − condition.NewFunctions(), | |
| 959 | − ), | |
| 960 | − }, | |
| 961 | − } | |
| 962 | − | |
| 963 | − case9Data := []byte(`{ | |
| 964 | − "ID": "MyPolicyForMyBucket1", | |
| 965 | − "Version": "17-10-2012", | |
| 966 | − "Statement": [ | |
| 967 | − { | |
| 968 | − "Effect": "Allow", | |
| 969 | − "Principal": "*", | |
| 970 | − "Action": "s3:PutObject", | |
| 971 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 972 | − } | |
| 973 | − ] | |
| 974 | −}`) | |
| 975 | − | |
| 976 | − case10Data := []byte(`{ | |
| 977 | − "ID": "MyPolicyForMyBucket1", | |
| 978 | − "Version": "2012-10-17", | |
| 979 | − "Statement": [ | |
| 980 | − { | |
| 981 | − "Effect": "Allow", | |
| 982 | − "Principal": "*", | |
| 983 | − "Action": "s3:PutObject", | |
| 984 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 985 | − }, | |
| 986 | − { | |
| 987 | − "Effect": "Allow", | |
| 988 | − "Principal": "*", | |
| 989 | − "Action": "s3:PutObject", | |
| 990 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 991 | − } | |
| 992 | − ] | |
| 993 | −}`) | |
| 994 | − | |
| 995 | − case10Policy := Policy{ | |
| 996 | − ID: "MyPolicyForMyBucket1", | |
| 997 | − Version: DefaultVersion, | |
| 998 | − Statements: []Statement{ | |
| 999 | − NewStatement( | |
| 1000 | − Allow, | |
| 1001 | − NewPrincipal("*"), | |
| 1002 | − NewActionSet(PutObjectAction), | |
| 1003 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 1004 | − condition.NewFunctions(), | |
| 1005 | − ), | |
| 1006 | − }, | |
| 1007 | − } | |
| 1008 | − | |
| 1009 | − case11Data := []byte(`{ | |
| 1010 | − "ID": "MyPolicyForMyBucket1", | |
| 1011 | − "Version": "2012-10-17", | |
| 1012 | − "Statement": [ | |
| 1013 | − { | |
| 1014 | − "Effect": "Allow", | |
| 1015 | − "Principal": "*", | |
| 1016 | − "Action": "s3:PutObject", | |
| 1017 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 1018 | − }, | |
| 1019 | − { | |
| 1020 | − "Effect": "Deny", | |
| 1021 | − "Principal": "*", | |
| 1022 | − "Action": "s3:PutObject", | |
| 1023 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 1024 | − } | |
| 1025 | − ] | |
| 1026 | −}`) | |
| 1027 | − | |
| 1028 | − case11Policy := Policy{ | |
| 1029 | − ID: "MyPolicyForMyBucket1", | |
| 1030 | − Version: DefaultVersion, | |
| 1031 | − Statements: []Statement{ | |
| 1032 | − NewStatement( | |
| 1033 | − Allow, | |
| 1034 | − NewPrincipal("*"), | |
| 1035 | − NewActionSet(PutObjectAction), | |
| 1036 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 1037 | − condition.NewFunctions(), | |
| 1038 | − ), | |
| 1039 | − NewStatement( | |
| 1040 | − Deny, | |
| 1041 | − NewPrincipal("*"), | |
| 1042 | − NewActionSet(PutObjectAction), | |
| 1043 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 1044 | − condition.NewFunctions(), | |
| 1045 | − ), | |
| 1046 | − }, | |
| 1047 | − } | |
| 1048 | − | |
| 1049 | − testCases := []struct { | |
| 1050 | − data []byte | |
| 1051 | − expectedResult Policy | |
| 1052 | − expectErr bool | |
| 1053 | − }{ | |
| 1054 | − {case1Data, case1Policy, false}, | |
| 1055 | − {case2Data, case2Policy, false}, | |
| 1056 | − {case3Data, case3Policy, false}, | |
| 1057 | − {case4Data, case4Policy, false}, | |
| 1058 | − {case5Data, case5Policy, false}, | |
| 1059 | − {case6Data, case6Policy, false}, | |
| 1060 | − {case7Data, case7Policy, false}, | |
| 1061 | − {case8Data, case8Policy, false}, | |
| 1062 | − // Invalid version error. | |
| 1063 | − {case9Data, Policy{}, true}, | |
| 1064 | − // Duplicate statement success, duplicate statement removed. | |
| 1065 | − {case10Data, case10Policy, false}, | |
| 1066 | − // Duplicate statement success (Effect differs). | |
| 1067 | − {case11Data, case11Policy, false}, | |
| 1068 | − } | |
| 1069 | − | |
| 1070 | − for i, testCase := range testCases { | |
| 1071 | − var result Policy | |
| 1072 | − err := json.Unmarshal(testCase.data, &result) | |
| 1073 | − expectErr := (err != nil) | |
| 1074 | − | |
| 1075 | − if expectErr != testCase.expectErr { | |
| 1076 | − t.Errorf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 1077 | − } | |
| 1078 | − | |
| 1079 | − if !testCase.expectErr { | |
| 1080 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 1081 | − t.Errorf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 1082 | − } | |
| 1083 | − } | |
| 1084 | − } | |
| 1085 | −} | |
| 1086 | − | |
| 1087 | −func TestPolicyValidate(t *testing.T) { | |
| 1088 | − case1Policy := Policy{ | |
| 1089 | − Version: DefaultVersion, | |
| 1090 | − Statements: []Statement{ | |
| 1091 | − NewStatement( | |
| 1092 | − Allow, | |
| 1093 | − NewPrincipal("*"), | |
| 1094 | − NewActionSet(PutObjectAction), | |
| 1095 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 1096 | − condition.NewFunctions(), | |
| 1097 | − ), | |
| 1098 | − }, | |
| 1099 | − } | |
| 1100 | − | |
| 1101 | − func1, err := condition.NewNullFunc( | |
| 1102 | − condition.S3XAmzCopySource, | |
| 1103 | − true, | |
| 1104 | − ) | |
| 1105 | − if err != nil { | |
| 1106 | − t.Fatalf("unexpected error. %v\n", err) | |
| 1107 | − } | |
| 1108 | − func2, err := condition.NewNullFunc( | |
| 1109 | − condition.S3XAmzServerSideEncryption, | |
| 1110 | − false, | |
| 1111 | − ) | |
| 1112 | − if err != nil { | |
| 1113 | − t.Fatalf("unexpected error. %v\n", err) | |
| 1114 | − } | |
| 1115 | − case2Policy := Policy{ | |
| 1116 | − ID: "MyPolicyForMyBucket1", | |
| 1117 | − Version: DefaultVersion, | |
| 1118 | − Statements: []Statement{ | |
| 1119 | − NewStatement( | |
| 1120 | − Allow, | |
| 1121 | − NewPrincipal("*"), | |
| 1122 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 1123 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 1124 | − condition.NewFunctions(func1, func2), | |
| 1125 | − ), | |
| 1126 | − }, | |
| 1127 | − } | |
| 1128 | − | |
| 1129 | − testCases := []struct { | |
| 1130 | − policy Policy | |
| 1131 | − bucketName string | |
| 1132 | − expectErr bool | |
| 1133 | − }{ | |
| 1134 | − {case1Policy, "mybucket", false}, | |
| 1135 | − {case2Policy, "yourbucket", true}, | |
| 1136 | − {case1Policy, "yourbucket", true}, | |
| 1137 | − } | |
| 1138 | − | |
| 1139 | − for i, testCase := range testCases { | |
| 1140 | − err := testCase.policy.Validate(testCase.bucketName) | |
| 1141 | − expectErr := (err != nil) | |
| 1142 | − | |
| 1143 | − if expectErr != testCase.expectErr { | |
| 1144 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 1145 | − } | |
| 1146 | − } | |
| 1147 | −} | |
| 1148 | − | |
| 1149 | −func TestPolicyMerge(t *testing.T) { | |
| 1150 | − testCases := []struct { | |
| 1151 | − policy string | |
| 1152 | − }{ | |
| 1153 | − {`{ | |
| 1154 | − "Version": "2012-10-17", | |
| 1155 | − "Id": "S3PolicyId1", | |
| 1156 | − "Statement": [ | |
| 1157 | − { | |
| 1158 | − "Sid": "statement1", | |
| 1159 | − "Effect": "Deny", | |
| 1160 | − "Principal": "*", | |
| 1161 | − "Action":["s3:GetObject", "s3:PutObject"], | |
| 1162 | − "Resource": "arn:aws:s3:::awsexamplebucket1/*" | |
| 1163 | − } | |
| 1164 | − ] | |
| 1165 | −}`}, | |
| 1166 | − {`{ | |
| 1167 | − "Version": "2012-10-17", | |
| 1168 | − "Id": "S3PolicyId1", | |
| 1169 | − "Statement": [ | |
| 1170 | − { | |
| 1171 | − "Sid": "statement1", | |
| 1172 | − "Effect": "Allow", | |
| 1173 | − "Principal": "*", | |
| 1174 | − "Action":"s3:GetObject", | |
| 1175 | − "Resource": "arn:aws:s3:::awsexamplebucket1/*", | |
| 1176 | − "Condition" : { | |
| 1177 | − "IpAddress" : { | |
| 1178 | − "aws:SourceIp": "192.0.2.0/24" | |
| 1179 | − }, | |
| 1180 | − "NotIpAddress" : { | |
| 1181 | − "aws:SourceIp": "192.0.2.188/32" | |
| 1182 | − } | |
| 1183 | − } | |
| 1184 | − } | |
| 1185 | − ] | |
| 1186 | −}`}, | |
| 1187 | − {`{ | |
| 1188 | − "Version": "2012-10-17", | |
| 1189 | − "Statement": [ | |
| 1190 | − { | |
| 1191 | − "Sid": "cross-account permission to user in your own account", | |
| 1192 | − "Effect": "Allow", | |
| 1193 | − "Principal": { | |
| 1194 | − "AWS": "arn:aws:iam::123456789012:user/Dave" | |
| 1195 | − }, | |
| 1196 | − "Action": "s3:PutObject", | |
| 1197 | − "Resource": "arn:aws:s3:::awsexamplebucket1/*" | |
| 1198 | − }, | |
| 1199 | − { | |
| 1200 | − "Sid": "Deny your user permission to upload object if copy source is not /bucket/folder", | |
| 1201 | − "Effect": "Deny", | |
| 1202 | − "Principal": { | |
| 1203 | − "AWS": "arn:aws:iam::123456789012:user/Dave" | |
| 1204 | − }, | |
| 1205 | − "Action": "s3:PutObject", | |
| 1206 | − "Resource": "arn:aws:s3:::awsexamplebucket1/*", | |
| 1207 | − "Condition": { | |
| 1208 | − "StringNotLike": { | |
| 1209 | − "s3:x-amz-copy-source": "awsexamplebucket1/public/*" | |
| 1210 | − } | |
| 1211 | − } | |
| 1212 | − } | |
| 1213 | − ] | |
| 1214 | −}`}, | |
| 1215 | − } | |
| 1216 | − | |
| 1217 | − for i, testCase := range testCases { | |
| 1218 | − var p Policy | |
| 1219 | − err := json.Unmarshal([]byte(testCase.policy), &p) | |
| 1220 | − if err != nil { | |
| 1221 | − t.Fatalf("case %v: unexpected error: %v", i+1, err) | |
| 1222 | − } | |
| 1223 | − | |
| 1224 | − var clonedPolicy Policy | |
| 1225 | − clonedPolicy = clonedPolicy.Merge(p) | |
| 1226 | − | |
| 1227 | − j, err := json.Marshal(clonedPolicy) | |
| 1228 | − if err != nil { | |
| 1229 | − t.Fatalf("case %v: unexpected error: %v", i+1, err) | |
| 1230 | − } | |
| 1231 | − | |
| 1232 | − err = json.Unmarshal(j, &clonedPolicy) | |
| 1233 | − if err != nil { | |
| 1234 | − t.Fatalf("case %v: unexpected error: %v", i+1, err) | |
| 1235 | − } | |
| 1236 | − | |
| 1237 | − if !clonedPolicy.Statements[0].Equals(p.Statements[0]) { | |
| 1238 | − t.Fatalf("case %v: different policy outcome", i+1) | |
| 1239 | − } | |
| 1240 | − } | |
| 1241 | −} | |
| @@ -1,103 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − | |
| 23 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 24 | − "github.com/minio/pkg/wildcard" | |
| 25 | −) | |
| 26 | − | |
| 27 | −// Principal - policy principal. | |
| 28 | −type Principal struct { | |
| 29 | − AWS set.StringSet | |
| 30 | −} | |
| 31 | − | |
| 32 | −// IsValid - checks whether Principal is valid or not. | |
| 33 | −func (p Principal) IsValid() bool { | |
| 34 | − return len(p.AWS) != 0 | |
| 35 | −} | |
| 36 | − | |
| 37 | −// Equals - returns true if principals are equal. | |
| 38 | −func (p Principal) Equals(pp Principal) bool { | |
| 39 | − return p.AWS.Equals(pp.AWS) | |
| 40 | −} | |
| 41 | − | |
| 42 | −// Intersection - returns principals available in both Principal. | |
| 43 | −func (p Principal) Intersection(principal Principal) set.StringSet { | |
| 44 | − return p.AWS.Intersection(principal.AWS) | |
| 45 | −} | |
| 46 | − | |
| 47 | −// MarshalJSON - encodes Principal to JSON data. | |
| 48 | −func (p Principal) MarshalJSON() ([]byte, error) { | |
| 49 | − if !p.IsValid() { | |
| 50 | − return nil, Errorf("invalid principal %v", p) | |
| 51 | − } | |
| 52 | − | |
| 53 | − // subtype to avoid recursive call to MarshalJSON() | |
| 54 | − type subPrincipal Principal | |
| 55 | − sp := subPrincipal(p) | |
| 56 | − return json.Marshal(sp) | |
| 57 | −} | |
| 58 | − | |
| 59 | −// Match - matches given principal is wildcard matching with Principal. | |
| 60 | −func (p Principal) Match(principal string) bool { | |
| 61 | − for _, pattern := range p.AWS.ToSlice() { | |
| 62 | − if wildcard.MatchSimple(pattern, principal) { | |
| 63 | − return true | |
| 64 | − } | |
| 65 | − } | |
| 66 | − | |
| 67 | − return false | |
| 68 | −} | |
| 69 | − | |
| 70 | −// UnmarshalJSON - decodes JSON data to Principal. | |
| 71 | −func (p *Principal) UnmarshalJSON(data []byte) error { | |
| 72 | − // subtype to avoid recursive call to UnmarshalJSON() | |
| 73 | − type subPrincipal Principal | |
| 74 | − var sp subPrincipal | |
| 75 | − | |
| 76 | − if err := json.Unmarshal(data, &sp); err != nil { | |
| 77 | − var s string | |
| 78 | − if err = json.Unmarshal(data, &s); err != nil { | |
| 79 | − return err | |
| 80 | − } | |
| 81 | − | |
| 82 | − if s != "*" { | |
| 83 | − return Errorf("invalid principal '%v'", s) | |
| 84 | − } | |
| 85 | − | |
| 86 | − sp.AWS = set.CreateStringSet("*") | |
| 87 | − } | |
| 88 | − | |
| 89 | − *p = Principal(sp) | |
| 90 | − | |
| 91 | − return nil | |
| 92 | −} | |
| 93 | − | |
| 94 | −// Clone clones Principal structure | |
| 95 | −func (p Principal) Clone() Principal { | |
| 96 | − return NewPrincipal(p.AWS.ToSlice()...) | |
| 97 | − | |
| 98 | −} | |
| 99 | − | |
| 100 | −// NewPrincipal - creates new Principal. | |
| 101 | −func NewPrincipal(principals ...string) Principal { | |
| 102 | − return Principal{AWS: set.CreateStringSet(principals...)} | |
| 103 | −} | |
| @@ -1,142 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | − | |
| 25 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 26 | −) | |
| 27 | − | |
| 28 | −func TestPrincipalIsValid(t *testing.T) { | |
| 29 | − testCases := []struct { | |
| 30 | − principal Principal | |
| 31 | − expectedResult bool | |
| 32 | − }{ | |
| 33 | − {NewPrincipal("*"), true}, | |
| 34 | − {NewPrincipal("arn:aws:iam::AccountNumber:root"), true}, | |
| 35 | − {NewPrincipal(), false}, | |
| 36 | − } | |
| 37 | − | |
| 38 | − for i, testCase := range testCases { | |
| 39 | − result := testCase.principal.IsValid() | |
| 40 | − | |
| 41 | − if result != testCase.expectedResult { | |
| 42 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 43 | − } | |
| 44 | − } | |
| 45 | −} | |
| 46 | − | |
| 47 | −func TestPrincipalIntersection(t *testing.T) { | |
| 48 | − testCases := []struct { | |
| 49 | − principal Principal | |
| 50 | − principalToIntersect Principal | |
| 51 | − expectedResult set.StringSet | |
| 52 | − }{ | |
| 53 | − {NewPrincipal("*"), NewPrincipal("*"), set.CreateStringSet("*")}, | |
| 54 | − {NewPrincipal("arn:aws:iam::AccountNumber:root"), NewPrincipal("arn:aws:iam::AccountNumber:myuser"), set.CreateStringSet()}, | |
| 55 | − {NewPrincipal(), NewPrincipal("*"), set.CreateStringSet()}, | |
| 56 | − } | |
| 57 | − | |
| 58 | − for i, testCase := range testCases { | |
| 59 | − result := testCase.principal.Intersection(testCase.principalToIntersect) | |
| 60 | − | |
| 61 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 62 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 63 | − } | |
| 64 | − } | |
| 65 | −} | |
| 66 | − | |
| 67 | −func TestPrincipalMarshalJSON(t *testing.T) { | |
| 68 | − testCases := []struct { | |
| 69 | − principal Principal | |
| 70 | − expectedResult []byte | |
| 71 | − expectErr bool | |
| 72 | − }{ | |
| 73 | − {NewPrincipal("*"), []byte(`{"AWS":["*"]}`), false}, | |
| 74 | − {NewPrincipal("arn:aws:iam::AccountNumber:*"), []byte(`{"AWS":["arn:aws:iam::AccountNumber:*"]}`), false}, | |
| 75 | − {NewPrincipal(), nil, true}, | |
| 76 | − } | |
| 77 | − | |
| 78 | − for i, testCase := range testCases { | |
| 79 | − result, err := json.Marshal(testCase.principal) | |
| 80 | − expectErr := (err != nil) | |
| 81 | − | |
| 82 | − if expectErr != testCase.expectErr { | |
| 83 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 84 | − } | |
| 85 | − | |
| 86 | − if !testCase.expectErr { | |
| 87 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 88 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, string(testCase.expectedResult), string(result)) | |
| 89 | − } | |
| 90 | − } | |
| 91 | − } | |
| 92 | −} | |
| 93 | − | |
| 94 | −func TestPrincipalMatch(t *testing.T) { | |
| 95 | − testCases := []struct { | |
| 96 | − principals Principal | |
| 97 | − principal string | |
| 98 | − expectedResult bool | |
| 99 | − }{ | |
| 100 | − {NewPrincipal("*"), "AccountNumber", true}, | |
| 101 | − {NewPrincipal("arn:aws:iam::*"), "arn:aws:iam::AccountNumber:root", true}, | |
| 102 | − {NewPrincipal("arn:aws:iam::AccountNumber:*"), "arn:aws:iam::TestAccountNumber:root", false}, | |
| 103 | − } | |
| 104 | − | |
| 105 | − for i, testCase := range testCases { | |
| 106 | − result := testCase.principals.Match(testCase.principal) | |
| 107 | − | |
| 108 | − if result != testCase.expectedResult { | |
| 109 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 110 | − } | |
| 111 | − } | |
| 112 | −} | |
| 113 | − | |
| 114 | −func TestPrincipalUnmarshalJSON(t *testing.T) { | |
| 115 | − testCases := []struct { | |
| 116 | − data []byte | |
| 117 | − expectedResult Principal | |
| 118 | − expectErr bool | |
| 119 | − }{ | |
| 120 | − {[]byte(`"*"`), NewPrincipal("*"), false}, | |
| 121 | − {[]byte(`{"AWS": "*"}`), NewPrincipal("*"), false}, | |
| 122 | − {[]byte(`{"AWS": "arn:aws:iam::AccountNumber:*"}`), NewPrincipal("arn:aws:iam::AccountNumber:*"), false}, | |
| 123 | − {[]byte(`"arn:aws:iam::AccountNumber:*"`), NewPrincipal(), true}, | |
| 124 | − {[]byte(`["arn:aws:iam::AccountNumber:*", "arn:aws:iam:AnotherAccount:*"]`), NewPrincipal(), true}, | |
| 125 | − } | |
| 126 | − | |
| 127 | − for i, testCase := range testCases { | |
| 128 | − var result Principal | |
| 129 | − err := json.Unmarshal(testCase.data, &result) | |
| 130 | − expectErr := (err != nil) | |
| 131 | − | |
| 132 | − if expectErr != testCase.expectErr { | |
| 133 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 134 | − } | |
| 135 | − | |
| 136 | − if !testCase.expectErr { | |
| 137 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 138 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 139 | − } | |
| 140 | − } | |
| 141 | − } | |
| 142 | −} | |
| @@ -1,140 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "strings" | |
| 23 | − | |
| 24 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 25 | − "github.com/minio/pkg/wildcard" | |
| 26 | −) | |
| 27 | − | |
| 28 | −// ResourceARNPrefix - resource ARN prefix as per AWS S3 specification. | |
| 29 | −const ResourceARNPrefix = "arn:aws:s3:::" | |
| 30 | − | |
| 31 | −// Resource - resource in policy statement. | |
| 32 | −type Resource struct { | |
| 33 | − BucketName string | |
| 34 | − Pattern string | |
| 35 | −} | |
| 36 | − | |
| 37 | −func (r Resource) isBucketPattern() bool { | |
| 38 | − return !strings.Contains(r.Pattern, "/") | |
| 39 | −} | |
| 40 | − | |
| 41 | −func (r Resource) isObjectPattern() bool { | |
| 42 | − return strings.Contains(r.Pattern, "/") || strings.Contains(r.BucketName, "*") | |
| 43 | −} | |
| 44 | − | |
| 45 | −// IsValid - checks whether Resource is valid or not. | |
| 46 | −func (r Resource) IsValid() bool { | |
| 47 | − return r.BucketName != "" && r.Pattern != "" | |
| 48 | −} | |
| 49 | − | |
| 50 | −// Match - matches object name with resource pattern. | |
| 51 | −func (r Resource) Match(resource string, conditionValues map[string][]string) bool { | |
| 52 | − pattern := r.Pattern | |
| 53 | − for _, key := range condition.CommonKeys { | |
| 54 | − // Empty values are not supported for policy variables. | |
| 55 | − if rvalues, ok := conditionValues[key.Name()]; ok && rvalues[0] != "" { | |
| 56 | − pattern = strings.Replace(pattern, key.VarName(), rvalues[0], -1) | |
| 57 | − } | |
| 58 | − } | |
| 59 | − | |
| 60 | − return wildcard.Match(pattern, resource) | |
| 61 | −} | |
| 62 | − | |
| 63 | −// MarshalJSON - encodes Resource to JSON data. | |
| 64 | −func (r Resource) MarshalJSON() ([]byte, error) { | |
| 65 | − if !r.IsValid() { | |
| 66 | − return nil, Errorf("invalid resource %v", r) | |
| 67 | − } | |
| 68 | − | |
| 69 | − return json.Marshal(r.String()) | |
| 70 | −} | |
| 71 | − | |
| 72 | −func (r Resource) String() string { | |
| 73 | − return ResourceARNPrefix + r.Pattern | |
| 74 | −} | |
| 75 | − | |
| 76 | −// UnmarshalJSON - decodes JSON data to Resource. | |
| 77 | −func (r *Resource) UnmarshalJSON(data []byte) error { | |
| 78 | − var s string | |
| 79 | − if err := json.Unmarshal(data, &s); err != nil { | |
| 80 | − return err | |
| 81 | − } | |
| 82 | − | |
| 83 | − parsedResource, err := parseResource(s) | |
| 84 | − if err != nil { | |
| 85 | − return err | |
| 86 | − } | |
| 87 | − | |
| 88 | − *r = parsedResource | |
| 89 | − | |
| 90 | − return nil | |
| 91 | −} | |
| 92 | − | |
| 93 | −// Validate - validates Resource is for given bucket or not. | |
| 94 | −func (r Resource) Validate(bucketName string) error { | |
| 95 | − if !r.IsValid() { | |
| 96 | − return Errorf("invalid resource") | |
| 97 | − } | |
| 98 | − | |
| 99 | − if !wildcard.Match(r.BucketName, bucketName) { | |
| 100 | − return Errorf("bucket name does not match") | |
| 101 | − } | |
| 102 | − | |
| 103 | − return nil | |
| 104 | −} | |
| 105 | − | |
| 106 | −// parseResource - parses string to Resource. | |
| 107 | −func parseResource(s string) (Resource, error) { | |
| 108 | − if !strings.HasPrefix(s, ResourceARNPrefix) { | |
| 109 | − return Resource{}, Errorf("invalid resource '%v'", s) | |
| 110 | − } | |
| 111 | − | |
| 112 | − pattern := strings.TrimPrefix(s, ResourceARNPrefix) | |
| 113 | − tokens := strings.SplitN(pattern, "/", 2) | |
| 114 | − bucketName := tokens[0] | |
| 115 | − if bucketName == "" { | |
| 116 | − return Resource{}, Errorf("invalid resource format '%v'", s) | |
| 117 | − } | |
| 118 | − | |
| 119 | − return Resource{ | |
| 120 | − BucketName: bucketName, | |
| 121 | − Pattern: pattern, | |
| 122 | − }, nil | |
| 123 | −} | |
| 124 | − | |
| 125 | −// NewResource - creates new resource. | |
| 126 | −func NewResource(bucketName, keyName string) Resource { | |
| 127 | − pattern := bucketName | |
| 128 | − if keyName != "" { | |
| 129 | − if !strings.HasPrefix(keyName, "/") { | |
| 130 | − pattern += "/" | |
| 131 | − } | |
| 132 | − | |
| 133 | − pattern += keyName | |
| 134 | − } | |
| 135 | − | |
| 136 | − return Resource{ | |
| 137 | − BucketName: bucketName, | |
| 138 | − Pattern: pattern, | |
| 139 | − } | |
| 140 | −} | |
| @@ -1,222 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestResourceIsBucketPattern(t *testing.T) { | |
| 27 | − testCases := []struct { | |
| 28 | − resource Resource | |
| 29 | − expectedResult bool | |
| 30 | − }{ | |
| 31 | − {NewResource("*", ""), true}, | |
| 32 | − {NewResource("mybucket", ""), true}, | |
| 33 | − {NewResource("mybucket*", ""), true}, | |
| 34 | − {NewResource("mybucket?0", ""), true}, | |
| 35 | − {NewResource("", "*"), false}, | |
| 36 | − {NewResource("*", "*"), false}, | |
| 37 | − {NewResource("mybucket", "*"), false}, | |
| 38 | − {NewResource("mybucket*", "/myobject"), false}, | |
| 39 | − {NewResource("mybucket?0", "/2010/photos/*"), false}, | |
| 40 | − } | |
| 41 | − | |
| 42 | − for i, testCase := range testCases { | |
| 43 | − result := testCase.resource.isBucketPattern() | |
| 44 | − | |
| 45 | − if result != testCase.expectedResult { | |
| 46 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 47 | − } | |
| 48 | − } | |
| 49 | −} | |
| 50 | − | |
| 51 | −func TestResourceIsObjectPattern(t *testing.T) { | |
| 52 | − testCases := []struct { | |
| 53 | − resource Resource | |
| 54 | − expectedResult bool | |
| 55 | − }{ | |
| 56 | − {NewResource("*", ""), true}, | |
| 57 | − {NewResource("mybucket*", ""), true}, | |
| 58 | − {NewResource("", "*"), true}, | |
| 59 | − {NewResource("*", "*"), true}, | |
| 60 | − {NewResource("mybucket", "*"), true}, | |
| 61 | − {NewResource("mybucket*", "/myobject"), true}, | |
| 62 | − {NewResource("mybucket?0", "/2010/photos/*"), true}, | |
| 63 | − {NewResource("mybucket", ""), false}, | |
| 64 | − {NewResource("mybucket?0", ""), false}, | |
| 65 | − } | |
| 66 | − | |
| 67 | − for i, testCase := range testCases { | |
| 68 | − result := testCase.resource.isObjectPattern() | |
| 69 | − | |
| 70 | − if result != testCase.expectedResult { | |
| 71 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 72 | − } | |
| 73 | − } | |
| 74 | −} | |
| 75 | − | |
| 76 | −func TestResourceIsValid(t *testing.T) { | |
| 77 | − testCases := []struct { | |
| 78 | − resource Resource | |
| 79 | − expectedResult bool | |
| 80 | − }{ | |
| 81 | − {NewResource("*", ""), true}, | |
| 82 | − {NewResource("mybucket*", ""), true}, | |
| 83 | − {NewResource("*", "*"), true}, | |
| 84 | − {NewResource("mybucket", "*"), true}, | |
| 85 | − {NewResource("mybucket*", "/myobject"), true}, | |
| 86 | − {NewResource("mybucket?0", "/2010/photos/*"), true}, | |
| 87 | − {NewResource("mybucket", ""), true}, | |
| 88 | − {NewResource("mybucket?0", ""), true}, | |
| 89 | − {NewResource("", ""), false}, | |
| 90 | − {NewResource("", "*"), false}, | |
| 91 | − } | |
| 92 | − | |
| 93 | − for i, testCase := range testCases { | |
| 94 | − result := testCase.resource.IsValid() | |
| 95 | − | |
| 96 | − if result != testCase.expectedResult { | |
| 97 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 98 | − } | |
| 99 | − } | |
| 100 | −} | |
| 101 | − | |
| 102 | −func TestResourceMatch(t *testing.T) { | |
| 103 | − testCases := []struct { | |
| 104 | − resource Resource | |
| 105 | − objectName string | |
| 106 | − expectedResult bool | |
| 107 | − }{ | |
| 108 | − {NewResource("*", ""), "mybucket", true}, | |
| 109 | − {NewResource("*", ""), "mybucket/myobject", true}, | |
| 110 | − {NewResource("mybucket*", ""), "mybucket", true}, | |
| 111 | − {NewResource("mybucket*", ""), "mybucket/myobject", true}, | |
| 112 | − {NewResource("", "*"), "/myobject", true}, | |
| 113 | − {NewResource("*", "*"), "mybucket/myobject", true}, | |
| 114 | − {NewResource("mybucket", "*"), "mybucket/myobject", true}, | |
| 115 | − {NewResource("mybucket*", "/myobject"), "mybucket/myobject", true}, | |
| 116 | − {NewResource("mybucket*", "/myobject"), "mybucket100/myobject", true}, | |
| 117 | − {NewResource("mybucket?0", "/2010/photos/*"), "mybucket20/2010/photos/1.jpg", true}, | |
| 118 | − {NewResource("mybucket", ""), "mybucket", true}, | |
| 119 | − {NewResource("mybucket?0", ""), "mybucket30", true}, | |
| 120 | − {NewResource("", "*"), "mybucket/myobject", false}, | |
| 121 | − {NewResource("*", "*"), "mybucket", false}, | |
| 122 | − {NewResource("mybucket", "*"), "mybucket10/myobject", false}, | |
| 123 | − {NewResource("mybucket?0", "/2010/photos/*"), "mybucket0/2010/photos/1.jpg", false}, | |
| 124 | − {NewResource("mybucket", ""), "mybucket/myobject", false}, | |
| 125 | − } | |
| 126 | − | |
| 127 | − for i, testCase := range testCases { | |
| 128 | − result := testCase.resource.Match(testCase.objectName, nil) | |
| 129 | − | |
| 130 | − if result != testCase.expectedResult { | |
| 131 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 132 | − } | |
| 133 | − } | |
| 134 | −} | |
| 135 | − | |
| 136 | −func TestResourceMarshalJSON(t *testing.T) { | |
| 137 | − testCases := []struct { | |
| 138 | − resource Resource | |
| 139 | − expectedResult []byte | |
| 140 | − expectErr bool | |
| 141 | − }{ | |
| 142 | − {NewResource("*", ""), []byte(`"arn:aws:s3:::*"`), false}, | |
| 143 | − {NewResource("mybucket*", ""), []byte(`"arn:aws:s3:::mybucket*"`), false}, | |
| 144 | − {NewResource("mybucket", ""), []byte(`"arn:aws:s3:::mybucket"`), false}, | |
| 145 | − {NewResource("*", "*"), []byte(`"arn:aws:s3:::*/*"`), false}, | |
| 146 | − {NewResource("mybucket", "*"), []byte(`"arn:aws:s3:::mybucket/*"`), false}, | |
| 147 | − {NewResource("mybucket*", "myobject"), []byte(`"arn:aws:s3:::mybucket*/myobject"`), false}, | |
| 148 | − {NewResource("mybucket?0", "/2010/photos/*"), []byte(`"arn:aws:s3:::mybucket?0/2010/photos/*"`), false}, | |
| 149 | − {Resource{}, nil, true}, | |
| 150 | − {NewResource("", "*"), nil, true}, | |
| 151 | − } | |
| 152 | − | |
| 153 | − for i, testCase := range testCases { | |
| 154 | − result, err := json.Marshal(testCase.resource) | |
| 155 | − expectErr := (err != nil) | |
| 156 | − | |
| 157 | − if expectErr != testCase.expectErr { | |
| 158 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 159 | − } | |
| 160 | − | |
| 161 | − if !testCase.expectErr { | |
| 162 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 163 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, string(testCase.expectedResult), string(result)) | |
| 164 | − } | |
| 165 | − } | |
| 166 | − } | |
| 167 | −} | |
| 168 | − | |
| 169 | −func TestResourceUnmarshalJSON(t *testing.T) { | |
| 170 | − testCases := []struct { | |
| 171 | − data []byte | |
| 172 | − expectedResult Resource | |
| 173 | − expectErr bool | |
| 174 | − }{ | |
| 175 | − {[]byte(`"arn:aws:s3:::*"`), NewResource("*", ""), false}, | |
| 176 | − {[]byte(`"arn:aws:s3:::mybucket*"`), NewResource("mybucket*", ""), false}, | |
| 177 | − {[]byte(`"arn:aws:s3:::mybucket"`), NewResource("mybucket", ""), false}, | |
| 178 | − {[]byte(`"arn:aws:s3:::*/*"`), NewResource("*", "*"), false}, | |
| 179 | − {[]byte(`"arn:aws:s3:::mybucket/*"`), NewResource("mybucket", "*"), false}, | |
| 180 | − {[]byte(`"arn:aws:s3:::mybucket*/myobject"`), NewResource("mybucket*", "myobject"), false}, | |
| 181 | − {[]byte(`"arn:aws:s3:::mybucket?0/2010/photos/*"`), NewResource("mybucket?0", "/2010/photos/*"), false}, | |
| 182 | − {[]byte(`"mybucket/myobject*"`), Resource{}, true}, | |
| 183 | − {[]byte(`"arn:aws:s3:::/*"`), Resource{}, true}, | |
| 184 | − } | |
| 185 | − | |
| 186 | − for i, testCase := range testCases { | |
| 187 | − var result Resource | |
| 188 | − err := json.Unmarshal(testCase.data, &result) | |
| 189 | − expectErr := (err != nil) | |
| 190 | − | |
| 191 | − if expectErr != testCase.expectErr { | |
| 192 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 193 | − } | |
| 194 | − | |
| 195 | − if !testCase.expectErr { | |
| 196 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 197 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 198 | − } | |
| 199 | − } | |
| 200 | − } | |
| 201 | −} | |
| 202 | − | |
| 203 | −func TestResourceValidate(t *testing.T) { | |
| 204 | − testCases := []struct { | |
| 205 | − resource Resource | |
| 206 | − bucketName string | |
| 207 | − expectErr bool | |
| 208 | − }{ | |
| 209 | − {NewResource("mybucket", "/myobject*"), "mybucket", false}, | |
| 210 | − {NewResource("", "/myobject*"), "yourbucket", true}, | |
| 211 | − {NewResource("mybucket", "/myobject*"), "yourbucket", true}, | |
| 212 | − } | |
| 213 | − | |
| 214 | − for i, testCase := range testCases { | |
| 215 | − err := testCase.resource.Validate(testCase.bucketName) | |
| 216 | − expectErr := (err != nil) | |
| 217 | − | |
| 218 | − if expectErr != testCase.expectErr { | |
| 219 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 220 | − } | |
| 221 | − } | |
| 222 | −} | |
| @@ -1,181 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | − "sort" | |
| 24 | − | |
| 25 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 26 | −) | |
| 27 | − | |
| 28 | −// ResourceSet - set of resources in policy statement. | |
| 29 | −type ResourceSet map[Resource]struct{} | |
| 30 | − | |
| 31 | −// bucketResourceExists - checks if at least one bucket resource exists in the set. | |
| 32 | −func (resourceSet ResourceSet) bucketResourceExists() bool { | |
| 33 | − for resource := range resourceSet { | |
| 34 | − if resource.isBucketPattern() { | |
| 35 | − return true | |
| 36 | − } | |
| 37 | − } | |
| 38 | − | |
| 39 | − return false | |
| 40 | −} | |
| 41 | − | |
| 42 | −// objectResourceExists - checks if at least one object resource exists in the set. | |
| 43 | −func (resourceSet ResourceSet) objectResourceExists() bool { | |
| 44 | − for resource := range resourceSet { | |
| 45 | − if resource.isObjectPattern() { | |
| 46 | − return true | |
| 47 | − } | |
| 48 | − } | |
| 49 | − | |
| 50 | − return false | |
| 51 | −} | |
| 52 | − | |
| 53 | −// Add - adds resource to resource set. | |
| 54 | −func (resourceSet ResourceSet) Add(resource Resource) { | |
| 55 | − resourceSet[resource] = struct{}{} | |
| 56 | −} | |
| 57 | − | |
| 58 | −// Equals - checks whether given resource set is equal to current resource set or not. | |
| 59 | −func (resourceSet ResourceSet) Equals(sresourceSet ResourceSet) bool { | |
| 60 | − // If length of set is not equal to length of given set, the | |
| 61 | − // set is not equal to given set. | |
| 62 | − if len(resourceSet) != len(sresourceSet) { | |
| 63 | − return false | |
| 64 | − } | |
| 65 | − | |
| 66 | − // As both sets are equal in length, check each elements are equal. | |
| 67 | − for k := range resourceSet { | |
| 68 | − if _, ok := sresourceSet[k]; !ok { | |
| 69 | − return false | |
| 70 | − } | |
| 71 | − } | |
| 72 | − | |
| 73 | − return true | |
| 74 | −} | |
| 75 | − | |
| 76 | −// Intersection - returns resouces available in both ResourcsSet. | |
| 77 | −func (resourceSet ResourceSet) Intersection(sset ResourceSet) ResourceSet { | |
| 78 | − nset := NewResourceSet() | |
| 79 | − for k := range resourceSet { | |
| 80 | − if _, ok := sset[k]; ok { | |
| 81 | − nset.Add(k) | |
| 82 | − } | |
| 83 | − } | |
| 84 | − | |
| 85 | − return nset | |
| 86 | −} | |
| 87 | − | |
| 88 | −// MarshalJSON - encodes ResourceSet to JSON data. | |
| 89 | −func (resourceSet ResourceSet) MarshalJSON() ([]byte, error) { | |
| 90 | − if len(resourceSet) == 0 { | |
| 91 | − return nil, Errorf("empty resources not allowed") | |
| 92 | − } | |
| 93 | − | |
| 94 | − resources := []Resource{} | |
| 95 | − for resource := range resourceSet { | |
| 96 | − resources = append(resources, resource) | |
| 97 | − } | |
| 98 | − | |
| 99 | − return json.Marshal(resources) | |
| 100 | −} | |
| 101 | − | |
| 102 | −// Match - matches object name with anyone of resource pattern in resource set. | |
| 103 | −func (resourceSet ResourceSet) Match(resource string, conditionValues map[string][]string) bool { | |
| 104 | − for r := range resourceSet { | |
| 105 | − if r.Match(resource, conditionValues) { | |
| 106 | − return true | |
| 107 | − } | |
| 108 | − } | |
| 109 | − | |
| 110 | − return false | |
| 111 | −} | |
| 112 | − | |
| 113 | −func (resourceSet ResourceSet) String() string { | |
| 114 | − resources := []string{} | |
| 115 | − for resource := range resourceSet { | |
| 116 | − resources = append(resources, resource.String()) | |
| 117 | − } | |
| 118 | − sort.Strings(resources) | |
| 119 | − | |
| 120 | − return fmt.Sprintf("%v", resources) | |
| 121 | −} | |
| 122 | − | |
| 123 | −// UnmarshalJSON - decodes JSON data to ResourceSet. | |
| 124 | −func (resourceSet *ResourceSet) UnmarshalJSON(data []byte) error { | |
| 125 | − var sset set.StringSet | |
| 126 | − if err := json.Unmarshal(data, &sset); err != nil { | |
| 127 | − return err | |
| 128 | − } | |
| 129 | − | |
| 130 | − *resourceSet = make(ResourceSet) | |
| 131 | − for _, s := range sset.ToSlice() { | |
| 132 | − resource, err := parseResource(s) | |
| 133 | − if err != nil { | |
| 134 | − return err | |
| 135 | − } | |
| 136 | − | |
| 137 | − if _, found := (*resourceSet)[resource]; found { | |
| 138 | − return Errorf("duplicate resource '%v' found", s) | |
| 139 | − } | |
| 140 | − | |
| 141 | − resourceSet.Add(resource) | |
| 142 | − } | |
| 143 | − | |
| 144 | − return nil | |
| 145 | −} | |
| 146 | − | |
| 147 | −// Validate - validates ResourceSet is for given bucket or not. | |
| 148 | −func (resourceSet ResourceSet) Validate(bucketName string) error { | |
| 149 | − for resource := range resourceSet { | |
| 150 | − if err := resource.Validate(bucketName); err != nil { | |
| 151 | − return err | |
| 152 | − } | |
| 153 | − } | |
| 154 | − | |
| 155 | − return nil | |
| 156 | −} | |
| 157 | − | |
| 158 | −// ToSlice - returns slice of resources from the resource set. | |
| 159 | −func (resourceSet ResourceSet) ToSlice() []Resource { | |
| 160 | − resources := []Resource{} | |
| 161 | − for resource := range resourceSet { | |
| 162 | − resources = append(resources, resource) | |
| 163 | − } | |
| 164 | − | |
| 165 | − return resources | |
| 166 | −} | |
| 167 | − | |
| 168 | −// Clone clones ResourceSet structure | |
| 169 | −func (resourceSet ResourceSet) Clone() ResourceSet { | |
| 170 | − return NewResourceSet(resourceSet.ToSlice()...) | |
| 171 | −} | |
| 172 | − | |
| 173 | −// NewResourceSet - creates new resource set. | |
| 174 | −func NewResourceSet(resources ...Resource) ResourceSet { | |
| 175 | − resourceSet := make(ResourceSet) | |
| 176 | − for _, resource := range resources { | |
| 177 | − resourceSet.Add(resource) | |
| 178 | − } | |
| 179 | − | |
| 180 | − return resourceSet | |
| 181 | −} | |
| @@ -1,241 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestResourceSetBucketResourceExists(t *testing.T) { | |
| 27 | − testCases := []struct { | |
| 28 | − resourceSet ResourceSet | |
| 29 | − expectedResult bool | |
| 30 | − }{ | |
| 31 | − {NewResourceSet(NewResource("*", "")), true}, | |
| 32 | − {NewResourceSet(NewResource("mybucket", "")), true}, | |
| 33 | − {NewResourceSet(NewResource("mybucket*", "")), true}, | |
| 34 | − {NewResourceSet(NewResource("mybucket?0", "")), true}, | |
| 35 | − {NewResourceSet(NewResource("mybucket", "/2010/photos/*"), NewResource("mybucket", "")), true}, | |
| 36 | − {NewResourceSet(NewResource("", "*")), false}, | |
| 37 | − {NewResourceSet(NewResource("*", "*")), false}, | |
| 38 | − {NewResourceSet(NewResource("mybucket", "*")), false}, | |
| 39 | − {NewResourceSet(NewResource("mybucket*", "/myobject")), false}, | |
| 40 | − {NewResourceSet(NewResource("mybucket?0", "/2010/photos/*")), false}, | |
| 41 | − } | |
| 42 | − | |
| 43 | − for i, testCase := range testCases { | |
| 44 | − result := testCase.resourceSet.bucketResourceExists() | |
| 45 | − | |
| 46 | − if result != testCase.expectedResult { | |
| 47 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 48 | − } | |
| 49 | − } | |
| 50 | −} | |
| 51 | − | |
| 52 | −func TestResourceSetObjectResourceExists(t *testing.T) { | |
| 53 | − testCases := []struct { | |
| 54 | − resourceSet ResourceSet | |
| 55 | − expectedResult bool | |
| 56 | − }{ | |
| 57 | − {NewResourceSet(NewResource("*", "")), true}, | |
| 58 | − {NewResourceSet(NewResource("mybucket*", "")), true}, | |
| 59 | − {NewResourceSet(NewResource("", "*")), true}, | |
| 60 | − {NewResourceSet(NewResource("*", "*")), true}, | |
| 61 | − {NewResourceSet(NewResource("mybucket", "*")), true}, | |
| 62 | − {NewResourceSet(NewResource("mybucket*", "/myobject")), true}, | |
| 63 | − {NewResourceSet(NewResource("mybucket?0", "/2010/photos/*")), true}, | |
| 64 | − {NewResourceSet(NewResource("mybucket", ""), NewResource("mybucket", "/2910/photos/*")), true}, | |
| 65 | − {NewResourceSet(NewResource("mybucket", "")), false}, | |
| 66 | − {NewResourceSet(NewResource("mybucket?0", "")), false}, | |
| 67 | − } | |
| 68 | − | |
| 69 | − for i, testCase := range testCases { | |
| 70 | − result := testCase.resourceSet.objectResourceExists() | |
| 71 | − | |
| 72 | − if result != testCase.expectedResult { | |
| 73 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 74 | − } | |
| 75 | − } | |
| 76 | −} | |
| 77 | − | |
| 78 | −func TestResourceSetAdd(t *testing.T) { | |
| 79 | − testCases := []struct { | |
| 80 | − resourceSet ResourceSet | |
| 81 | − resource Resource | |
| 82 | − expectedResult ResourceSet | |
| 83 | − }{ | |
| 84 | − {NewResourceSet(), NewResource("mybucket", "/myobject*"), | |
| 85 | − NewResourceSet(NewResource("mybucket", "/myobject*"))}, | |
| 86 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 87 | − NewResource("mybucket", "/yourobject*"), | |
| 88 | − NewResourceSet(NewResource("mybucket", "/myobject*"), | |
| 89 | − NewResource("mybucket", "/yourobject*"))}, | |
| 90 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 91 | − NewResource("mybucket", "/myobject*"), | |
| 92 | − NewResourceSet(NewResource("mybucket", "/myobject*"))}, | |
| 93 | − } | |
| 94 | − | |
| 95 | − for i, testCase := range testCases { | |
| 96 | − testCase.resourceSet.Add(testCase.resource) | |
| 97 | − | |
| 98 | − if !reflect.DeepEqual(testCase.resourceSet, testCase.expectedResult) { | |
| 99 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, testCase.resourceSet) | |
| 100 | − } | |
| 101 | − } | |
| 102 | −} | |
| 103 | − | |
| 104 | −func TestResourceSetIntersection(t *testing.T) { | |
| 105 | − testCases := []struct { | |
| 106 | − set ResourceSet | |
| 107 | − setToIntersect ResourceSet | |
| 108 | − expectedResult ResourceSet | |
| 109 | − }{ | |
| 110 | − {NewResourceSet(), NewResourceSet(NewResource("mybucket", "/myobject*")), NewResourceSet()}, | |
| 111 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), NewResourceSet(), NewResourceSet()}, | |
| 112 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 113 | − NewResourceSet(NewResource("mybucket", "/myobject*"), NewResource("mybucket", "/yourobject*")), | |
| 114 | − NewResourceSet(NewResource("mybucket", "/myobject*"))}, | |
| 115 | − } | |
| 116 | − | |
| 117 | − for i, testCase := range testCases { | |
| 118 | − result := testCase.set.Intersection(testCase.setToIntersect) | |
| 119 | − | |
| 120 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 121 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set) | |
| 122 | − } | |
| 123 | − } | |
| 124 | −} | |
| 125 | − | |
| 126 | −func TestResourceSetMarshalJSON(t *testing.T) { | |
| 127 | − testCases := []struct { | |
| 128 | − resoruceSet ResourceSet | |
| 129 | − expectedResult []byte | |
| 130 | − expectErr bool | |
| 131 | − }{ | |
| 132 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 133 | − []byte(`["arn:aws:s3:::mybucket/myobject*"]`), false}, | |
| 134 | − {NewResourceSet(NewResource("mybucket", "/photos/myobject*")), | |
| 135 | − []byte(`["arn:aws:s3:::mybucket/photos/myobject*"]`), false}, | |
| 136 | − {NewResourceSet(), nil, true}, | |
| 137 | − } | |
| 138 | − | |
| 139 | − for i, testCase := range testCases { | |
| 140 | − result, err := json.Marshal(testCase.resoruceSet) | |
| 141 | − expectErr := (err != nil) | |
| 142 | − | |
| 143 | − if expectErr != testCase.expectErr { | |
| 144 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 145 | − } | |
| 146 | − | |
| 147 | − if !testCase.expectErr { | |
| 148 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 149 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, string(testCase.expectedResult), string(result)) | |
| 150 | − } | |
| 151 | − } | |
| 152 | − } | |
| 153 | −} | |
| 154 | − | |
| 155 | −func TestResourceSetMatch(t *testing.T) { | |
| 156 | − testCases := []struct { | |
| 157 | − resourceSet ResourceSet | |
| 158 | − resource string | |
| 159 | − expectedResult bool | |
| 160 | − }{ | |
| 161 | − {NewResourceSet(NewResource("*", "")), "mybucket", true}, | |
| 162 | − {NewResourceSet(NewResource("*", "")), "mybucket/myobject", true}, | |
| 163 | − {NewResourceSet(NewResource("mybucket*", "")), "mybucket", true}, | |
| 164 | − {NewResourceSet(NewResource("mybucket*", "")), "mybucket/myobject", true}, | |
| 165 | − {NewResourceSet(NewResource("", "*")), "/myobject", true}, | |
| 166 | − {NewResourceSet(NewResource("*", "*")), "mybucket/myobject", true}, | |
| 167 | − {NewResourceSet(NewResource("mybucket", "*")), "mybucket/myobject", true}, | |
| 168 | − {NewResourceSet(NewResource("mybucket*", "/myobject")), "mybucket/myobject", true}, | |
| 169 | − {NewResourceSet(NewResource("mybucket*", "/myobject")), "mybucket100/myobject", true}, | |
| 170 | − {NewResourceSet(NewResource("mybucket?0", "/2010/photos/*")), "mybucket20/2010/photos/1.jpg", true}, | |
| 171 | − {NewResourceSet(NewResource("mybucket", "")), "mybucket", true}, | |
| 172 | − {NewResourceSet(NewResource("mybucket?0", "")), "mybucket30", true}, | |
| 173 | − {NewResourceSet(NewResource("mybucket?0", "/2010/photos/*"), | |
| 174 | − NewResource("mybucket", "/2010/photos/*")), "mybucket/2010/photos/1.jpg", true}, | |
| 175 | − {NewResourceSet(NewResource("", "*")), "mybucket/myobject", false}, | |
| 176 | − {NewResourceSet(NewResource("*", "*")), "mybucket", false}, | |
| 177 | − {NewResourceSet(NewResource("mybucket", "*")), "mybucket10/myobject", false}, | |
| 178 | − {NewResourceSet(NewResource("mybucket", "")), "mybucket/myobject", false}, | |
| 179 | − {NewResourceSet(), "mybucket/myobject", false}, | |
| 180 | − } | |
| 181 | − | |
| 182 | − for i, testCase := range testCases { | |
| 183 | − result := testCase.resourceSet.Match(testCase.resource, nil) | |
| 184 | − | |
| 185 | − if result != testCase.expectedResult { | |
| 186 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 187 | − } | |
| 188 | − } | |
| 189 | −} | |
| 190 | − | |
| 191 | −func TestResourceSetUnmarshalJSON(t *testing.T) { | |
| 192 | − testCases := []struct { | |
| 193 | − data []byte | |
| 194 | − expectedResult ResourceSet | |
| 195 | − expectErr bool | |
| 196 | − }{ | |
| 197 | − {[]byte(`"arn:aws:s3:::mybucket/myobject*"`), | |
| 198 | − NewResourceSet(NewResource("mybucket", "/myobject*")), false}, | |
| 199 | − {[]byte(`"arn:aws:s3:::mybucket/photos/myobject*"`), | |
| 200 | − NewResourceSet(NewResource("mybucket", "/photos/myobject*")), false}, | |
| 201 | − {[]byte(`"arn:aws:s3:::mybucket"`), NewResourceSet(NewResource("mybucket", "")), false}, | |
| 202 | − {[]byte(`"mybucket/myobject*"`), nil, true}, | |
| 203 | − } | |
| 204 | − | |
| 205 | − for i, testCase := range testCases { | |
| 206 | − var result ResourceSet | |
| 207 | − err := json.Unmarshal(testCase.data, &result) | |
| 208 | − expectErr := (err != nil) | |
| 209 | − | |
| 210 | − if expectErr != testCase.expectErr { | |
| 211 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 212 | − } | |
| 213 | − | |
| 214 | − if !testCase.expectErr { | |
| 215 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 216 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 217 | − } | |
| 218 | − } | |
| 219 | − } | |
| 220 | −} | |
| 221 | − | |
| 222 | −func TestResourceSetValidate(t *testing.T) { | |
| 223 | − testCases := []struct { | |
| 224 | − resourceSet ResourceSet | |
| 225 | − bucketName string | |
| 226 | − expectErr bool | |
| 227 | − }{ | |
| 228 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), "mybucket", false}, | |
| 229 | − {NewResourceSet(NewResource("", "/myobject*")), "yourbucket", true}, | |
| 230 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), "yourbucket", true}, | |
| 231 | − } | |
| 232 | − | |
| 233 | − for i, testCase := range testCases { | |
| 234 | − err := testCase.resourceSet.Validate(testCase.bucketName) | |
| 235 | − expectErr := (err != nil) | |
| 236 | − | |
| 237 | − if expectErr != testCase.expectErr { | |
| 238 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 239 | − } | |
| 240 | − } | |
| 241 | −} | |
| @@ -1,182 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "strings" | |
| 23 | − | |
| 24 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 25 | −) | |
| 26 | − | |
| 27 | −// Statement - policy statement. | |
| 28 | −type Statement struct { | |
| 29 | − SID ID `json:"Sid,omitempty"` | |
| 30 | − Effect Effect `json:"Effect"` | |
| 31 | − Principal Principal `json:"Principal"` | |
| 32 | − Actions ActionSet `json:"Action"` | |
| 33 | − Resources ResourceSet `json:"Resource"` | |
| 34 | − Conditions condition.Functions `json:"Condition,omitempty"` | |
| 35 | −} | |
| 36 | − | |
| 37 | −// Equals checks if two statements are equal | |
| 38 | −func (statement Statement) Equals(st Statement) bool { | |
| 39 | − if statement.Effect != st.Effect { | |
| 40 | − return false | |
| 41 | − } | |
| 42 | − if !statement.Principal.Equals(st.Principal) { | |
| 43 | − return false | |
| 44 | − } | |
| 45 | − if !statement.Actions.Equals(st.Actions) { | |
| 46 | − return false | |
| 47 | − } | |
| 48 | − if !statement.Resources.Equals(st.Resources) { | |
| 49 | − return false | |
| 50 | − } | |
| 51 | − if !statement.Conditions.Equals(st.Conditions) { | |
| 52 | − return false | |
| 53 | − } | |
| 54 | − return true | |
| 55 | −} | |
| 56 | − | |
| 57 | −// IsAllowed - checks given policy args is allowed to continue the Rest API. | |
| 58 | −func (statement Statement) IsAllowed(args Args) bool { | |
| 59 | − check := func() bool { | |
| 60 | − if !statement.Principal.Match(args.AccountName) { | |
| 61 | − return false | |
| 62 | − } | |
| 63 | − | |
| 64 | − if !statement.Actions.Contains(args.Action) { | |
| 65 | − return false | |
| 66 | − } | |
| 67 | − | |
| 68 | − resource := args.BucketName | |
| 69 | − if args.ObjectName != "" { | |
| 70 | − if !strings.HasPrefix(args.ObjectName, "/") { | |
| 71 | − resource += "/" | |
| 72 | − } | |
| 73 | − | |
| 74 | − resource += args.ObjectName | |
| 75 | − } | |
| 76 | − | |
| 77 | − if !statement.Resources.Match(resource, args.ConditionValues) { | |
| 78 | − return false | |
| 79 | − } | |
| 80 | − | |
| 81 | − return statement.Conditions.Evaluate(args.ConditionValues) | |
| 82 | − } | |
| 83 | − | |
| 84 | − return statement.Effect.IsAllowed(check()) | |
| 85 | −} | |
| 86 | − | |
| 87 | −// isValid - checks whether statement is valid or not. | |
| 88 | −func (statement Statement) isValid() error { | |
| 89 | − if !statement.Effect.IsValid() { | |
| 90 | − return Errorf("invalid Effect %v", statement.Effect) | |
| 91 | − } | |
| 92 | − | |
| 93 | − if !statement.Principal.IsValid() { | |
| 94 | − return Errorf("invalid Principal %v", statement.Principal) | |
| 95 | − } | |
| 96 | − | |
| 97 | − if len(statement.Actions) == 0 { | |
| 98 | − return Errorf("Action must not be empty") | |
| 99 | − } | |
| 100 | − | |
| 101 | − if len(statement.Resources) == 0 { | |
| 102 | − return Errorf("Resource must not be empty") | |
| 103 | − } | |
| 104 | − | |
| 105 | − for action := range statement.Actions { | |
| 106 | − if action.isObjectAction() { | |
| 107 | − if !statement.Resources.objectResourceExists() { | |
| 108 | − return Errorf("unsupported Resource found %v for action %v", statement.Resources, action) | |
| 109 | − } | |
| 110 | − } else { | |
| 111 | − if !statement.Resources.bucketResourceExists() { | |
| 112 | − return Errorf("unsupported Resource found %v for action %v", statement.Resources, action) | |
| 113 | − } | |
| 114 | − } | |
| 115 | − | |
| 116 | − keys := statement.Conditions.Keys() | |
| 117 | − keyDiff := keys.Difference(actionConditionKeyMap[action]) | |
| 118 | − if !keyDiff.IsEmpty() { | |
| 119 | − return Errorf("unsupported condition keys '%v' used for action '%v'", keyDiff, action) | |
| 120 | − } | |
| 121 | − } | |
| 122 | − | |
| 123 | − return nil | |
| 124 | −} | |
| 125 | − | |
| 126 | −// MarshalJSON - encodes JSON data to Statement. | |
| 127 | −func (statement Statement) MarshalJSON() ([]byte, error) { | |
| 128 | − if err := statement.isValid(); err != nil { | |
| 129 | − return nil, err | |
| 130 | − } | |
| 131 | − | |
| 132 | − // subtype to avoid recursive call to MarshalJSON() | |
| 133 | − type subStatement Statement | |
| 134 | − ss := subStatement(statement) | |
| 135 | − return json.Marshal(ss) | |
| 136 | −} | |
| 137 | − | |
| 138 | −// UnmarshalJSON - decodes JSON data to Statement. | |
| 139 | −func (statement *Statement) UnmarshalJSON(data []byte) error { | |
| 140 | − // subtype to avoid recursive call to UnmarshalJSON() | |
| 141 | − type subStatement Statement | |
| 142 | − var ss subStatement | |
| 143 | − | |
| 144 | − if err := json.Unmarshal(data, &ss); err != nil { | |
| 145 | − return err | |
| 146 | − } | |
| 147 | − | |
| 148 | − s := Statement(ss) | |
| 149 | − if err := s.isValid(); err != nil { | |
| 150 | − return err | |
| 151 | − } | |
| 152 | − | |
| 153 | − *statement = s | |
| 154 | − | |
| 155 | − return nil | |
| 156 | −} | |
| 157 | − | |
| 158 | −// Validate - validates Statement is for given bucket or not. | |
| 159 | −func (statement Statement) Validate(bucketName string) error { | |
| 160 | − if err := statement.isValid(); err != nil { | |
| 161 | − return err | |
| 162 | − } | |
| 163 | − | |
| 164 | − return statement.Resources.Validate(bucketName) | |
| 165 | −} | |
| 166 | − | |
| 167 | −// Clone clones Statement structure | |
| 168 | −func (statement Statement) Clone() Statement { | |
| 169 | − return NewStatement(statement.Effect, statement.Principal.Clone(), | |
| 170 | − statement.Actions.Clone(), statement.Resources.Clone(), statement.Conditions.Clone()) | |
| 171 | −} | |
| 172 | − | |
| 173 | −// NewStatement - creates new statement. | |
| 174 | −func NewStatement(effect Effect, principal Principal, actionSet ActionSet, resourceSet ResourceSet, conditions condition.Functions) Statement { | |
| 175 | − return Statement{ | |
| 176 | − Effect: effect, | |
| 177 | − Principal: principal, | |
| 178 | − Actions: actionSet, | |
| 179 | − Resources: resourceSet, | |
| 180 | − Conditions: conditions, | |
| 181 | − } | |
| 182 | −} | |
| @@ -1,572 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package policy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "net" | |
| 23 | − "reflect" | |
| 24 | − "testing" | |
| 25 | − | |
| 26 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 27 | −) | |
| 28 | − | |
| 29 | −func TestStatementIsAllowed(t *testing.T) { | |
| 30 | − case1Statement := NewStatement( | |
| 31 | − Allow, | |
| 32 | − NewPrincipal("*"), | |
| 33 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 34 | − NewResourceSet(NewResource("*", "")), | |
| 35 | − condition.NewFunctions(), | |
| 36 | − ) | |
| 37 | − | |
| 38 | − case2Statement := NewStatement( | |
| 39 | − Allow, | |
| 40 | − NewPrincipal("*"), | |
| 41 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 42 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 43 | − condition.NewFunctions(), | |
| 44 | − ) | |
| 45 | − | |
| 46 | − _, IPNet1, err := net.ParseCIDR("192.168.1.0/24") | |
| 47 | − if err != nil { | |
| 48 | − t.Fatalf("unexpected error. %v\n", err) | |
| 49 | − } | |
| 50 | − func1, err := condition.NewIPAddressFunc( | |
| 51 | − condition.AWSSourceIP, | |
| 52 | − IPNet1, | |
| 53 | − ) | |
| 54 | − if err != nil { | |
| 55 | − t.Fatalf("unexpected error. %v\n", err) | |
| 56 | − } | |
| 57 | − | |
| 58 | − case3Statement := NewStatement( | |
| 59 | − Allow, | |
| 60 | − NewPrincipal("*"), | |
| 61 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 62 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 63 | − condition.NewFunctions(func1), | |
| 64 | − ) | |
| 65 | − | |
| 66 | − case4Statement := NewStatement( | |
| 67 | − Deny, | |
| 68 | − NewPrincipal("*"), | |
| 69 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 70 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 71 | − condition.NewFunctions(func1), | |
| 72 | − ) | |
| 73 | − | |
| 74 | − anonGetBucketLocationArgs := Args{ | |
| 75 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 76 | − Action: GetBucketLocationAction, | |
| 77 | − BucketName: "mybucket", | |
| 78 | − ConditionValues: map[string][]string{}, | |
| 79 | − } | |
| 80 | − | |
| 81 | − anonPutObjectActionArgs := Args{ | |
| 82 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 83 | − Action: PutObjectAction, | |
| 84 | − BucketName: "mybucket", | |
| 85 | − ConditionValues: map[string][]string{ | |
| 86 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 87 | − "SourceIp": {"192.168.1.10"}, | |
| 88 | − }, | |
| 89 | − ObjectName: "myobject", | |
| 90 | − } | |
| 91 | − | |
| 92 | − anonGetObjectActionArgs := Args{ | |
| 93 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 94 | − Action: GetObjectAction, | |
| 95 | − BucketName: "mybucket", | |
| 96 | − ConditionValues: map[string][]string{}, | |
| 97 | − ObjectName: "myobject", | |
| 98 | − } | |
| 99 | − | |
| 100 | − getBucketLocationArgs := Args{ | |
| 101 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 102 | − Action: GetBucketLocationAction, | |
| 103 | − BucketName: "mybucket", | |
| 104 | − ConditionValues: map[string][]string{}, | |
| 105 | − IsOwner: true, | |
| 106 | − } | |
| 107 | − | |
| 108 | − putObjectActionArgs := Args{ | |
| 109 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 110 | − Action: PutObjectAction, | |
| 111 | − BucketName: "mybucket", | |
| 112 | − ConditionValues: map[string][]string{ | |
| 113 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 114 | − "SourceIp": {"192.168.1.10"}, | |
| 115 | − }, | |
| 116 | − IsOwner: true, | |
| 117 | − ObjectName: "myobject", | |
| 118 | − } | |
| 119 | − | |
| 120 | − getObjectActionArgs := Args{ | |
| 121 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 122 | − Action: GetObjectAction, | |
| 123 | − BucketName: "mybucket", | |
| 124 | − ConditionValues: map[string][]string{}, | |
| 125 | − IsOwner: true, | |
| 126 | − ObjectName: "myobject", | |
| 127 | − } | |
| 128 | − | |
| 129 | − testCases := []struct { | |
| 130 | − statement Statement | |
| 131 | − args Args | |
| 132 | − expectedResult bool | |
| 133 | − }{ | |
| 134 | − {case1Statement, anonGetBucketLocationArgs, true}, | |
| 135 | − {case1Statement, anonPutObjectActionArgs, true}, | |
| 136 | − {case1Statement, anonGetObjectActionArgs, false}, | |
| 137 | − {case1Statement, getBucketLocationArgs, true}, | |
| 138 | − {case1Statement, putObjectActionArgs, true}, | |
| 139 | − {case1Statement, getObjectActionArgs, false}, | |
| 140 | − | |
| 141 | − {case2Statement, anonGetBucketLocationArgs, false}, | |
| 142 | − {case2Statement, anonPutObjectActionArgs, true}, | |
| 143 | − {case2Statement, anonGetObjectActionArgs, true}, | |
| 144 | − {case2Statement, getBucketLocationArgs, false}, | |
| 145 | − {case2Statement, putObjectActionArgs, true}, | |
| 146 | − {case2Statement, getObjectActionArgs, true}, | |
| 147 | − | |
| 148 | − {case3Statement, anonGetBucketLocationArgs, false}, | |
| 149 | − {case3Statement, anonPutObjectActionArgs, true}, | |
| 150 | − {case3Statement, anonGetObjectActionArgs, false}, | |
| 151 | − {case3Statement, getBucketLocationArgs, false}, | |
| 152 | − {case3Statement, putObjectActionArgs, true}, | |
| 153 | − {case3Statement, getObjectActionArgs, false}, | |
| 154 | − | |
| 155 | − {case4Statement, anonGetBucketLocationArgs, true}, | |
| 156 | − {case4Statement, anonPutObjectActionArgs, false}, | |
| 157 | − {case4Statement, anonGetObjectActionArgs, true}, | |
| 158 | − {case4Statement, getBucketLocationArgs, true}, | |
| 159 | − {case4Statement, putObjectActionArgs, false}, | |
| 160 | − {case4Statement, getObjectActionArgs, true}, | |
| 161 | − } | |
| 162 | − | |
| 163 | − for i, testCase := range testCases { | |
| 164 | − result := testCase.statement.IsAllowed(testCase.args) | |
| 165 | − | |
| 166 | − if result != testCase.expectedResult { | |
| 167 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 168 | − } | |
| 169 | − } | |
| 170 | −} | |
| 171 | − | |
| 172 | −func TestStatementIsValid(t *testing.T) { | |
| 173 | − _, IPNet1, err := net.ParseCIDR("192.168.1.0/24") | |
| 174 | − if err != nil { | |
| 175 | − t.Fatalf("unexpected error. %v\n", err) | |
| 176 | − } | |
| 177 | − func1, err := condition.NewIPAddressFunc( | |
| 178 | − condition.AWSSourceIP, | |
| 179 | − IPNet1, | |
| 180 | − ) | |
| 181 | − if err != nil { | |
| 182 | − t.Fatalf("unexpected error. %v\n", err) | |
| 183 | − } | |
| 184 | − | |
| 185 | − func2, err := condition.NewStringEqualsFunc( | |
| 186 | − condition.S3XAmzCopySource, | |
| 187 | − "mybucket/myobject", | |
| 188 | − ) | |
| 189 | − if err != nil { | |
| 190 | − t.Fatalf("unexpected error. %v\n", err) | |
| 191 | − } | |
| 192 | − | |
| 193 | − testCases := []struct { | |
| 194 | − statement Statement | |
| 195 | − expectErr bool | |
| 196 | − }{ | |
| 197 | − // Invalid effect error. | |
| 198 | − {NewStatement( | |
| 199 | − Effect("foo"), | |
| 200 | − NewPrincipal("*"), | |
| 201 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 202 | − NewResourceSet(NewResource("*", "")), | |
| 203 | − condition.NewFunctions(), | |
| 204 | − ), true}, | |
| 205 | − // Invalid principal error. | |
| 206 | − {NewStatement( | |
| 207 | − Allow, | |
| 208 | − NewPrincipal(), | |
| 209 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 210 | − NewResourceSet(NewResource("*", "")), | |
| 211 | − condition.NewFunctions(), | |
| 212 | − ), true}, | |
| 213 | − // Empty actions error. | |
| 214 | − {NewStatement( | |
| 215 | − Allow, | |
| 216 | − NewPrincipal("*"), | |
| 217 | − NewActionSet(), | |
| 218 | − NewResourceSet(NewResource("*", "")), | |
| 219 | − condition.NewFunctions(), | |
| 220 | − ), true}, | |
| 221 | − // Empty resources error. | |
| 222 | − {NewStatement( | |
| 223 | − Allow, | |
| 224 | − NewPrincipal("*"), | |
| 225 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 226 | − NewResourceSet(), | |
| 227 | − condition.NewFunctions(), | |
| 228 | − ), true}, | |
| 229 | − // Unsupported resource found for object action. | |
| 230 | − {NewStatement( | |
| 231 | − Allow, | |
| 232 | − NewPrincipal("*"), | |
| 233 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 234 | − NewResourceSet(NewResource("mybucket", "")), | |
| 235 | − condition.NewFunctions(), | |
| 236 | − ), true}, | |
| 237 | − // Unsupported resource found for bucket action. | |
| 238 | − {NewStatement( | |
| 239 | − Allow, | |
| 240 | − NewPrincipal("*"), | |
| 241 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 242 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 243 | − condition.NewFunctions(), | |
| 244 | − ), true}, | |
| 245 | − // Unsupported condition key for action. | |
| 246 | − {NewStatement( | |
| 247 | − Allow, | |
| 248 | − NewPrincipal("*"), | |
| 249 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 250 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 251 | − condition.NewFunctions(func1, func2), | |
| 252 | − ), true}, | |
| 253 | − {NewStatement( | |
| 254 | − Deny, | |
| 255 | − NewPrincipal("*"), | |
| 256 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 257 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 258 | − condition.NewFunctions(func1), | |
| 259 | − ), false}, | |
| 260 | − } | |
| 261 | − | |
| 262 | − for i, testCase := range testCases { | |
| 263 | − err := testCase.statement.isValid() | |
| 264 | − expectErr := (err != nil) | |
| 265 | − | |
| 266 | − if expectErr != testCase.expectErr { | |
| 267 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 268 | − } | |
| 269 | − } | |
| 270 | −} | |
| 271 | − | |
| 272 | −func TestStatementMarshalJSON(t *testing.T) { | |
| 273 | − case1Statement := NewStatement( | |
| 274 | − Allow, | |
| 275 | − NewPrincipal("*"), | |
| 276 | − NewActionSet(PutObjectAction), | |
| 277 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 278 | − condition.NewFunctions(), | |
| 279 | − ) | |
| 280 | − case1Statement.SID = "SomeId1" | |
| 281 | − case1Data := []byte(`{"Sid":"SomeId1","Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"]}`) | |
| 282 | − | |
| 283 | − func1, err := condition.NewNullFunc( | |
| 284 | − condition.S3XAmzCopySource, | |
| 285 | − true, | |
| 286 | − ) | |
| 287 | − if err != nil { | |
| 288 | − t.Fatalf("unexpected error. %v\n", err) | |
| 289 | − } | |
| 290 | − case2Statement := NewStatement( | |
| 291 | − Allow, | |
| 292 | − NewPrincipal("*"), | |
| 293 | − NewActionSet(PutObjectAction), | |
| 294 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 295 | − condition.NewFunctions(func1), | |
| 296 | − ) | |
| 297 | − case2Data := []byte(`{"Effect":"Allow","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"],"Condition":{"Null":{"s3:x-amz-copy-source":[true]}}}`) | |
| 298 | − | |
| 299 | − func2, err := condition.NewNullFunc( | |
| 300 | − condition.S3XAmzServerSideEncryption, | |
| 301 | − false, | |
| 302 | − ) | |
| 303 | − if err != nil { | |
| 304 | − t.Fatalf("unexpected error. %v\n", err) | |
| 305 | − } | |
| 306 | − case3Statement := NewStatement( | |
| 307 | − Deny, | |
| 308 | − NewPrincipal("*"), | |
| 309 | − NewActionSet(PutObjectAction), | |
| 310 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 311 | − condition.NewFunctions(func2), | |
| 312 | − ) | |
| 313 | − case3Data := []byte(`{"Effect":"Deny","Principal":{"AWS":["*"]},"Action":["s3:PutObject"],"Resource":["arn:aws:s3:::mybucket/myobject*"],"Condition":{"Null":{"s3:x-amz-server-side-encryption":[false]}}}`) | |
| 314 | − | |
| 315 | − case4Statement := NewStatement( | |
| 316 | − Allow, | |
| 317 | − NewPrincipal("*"), | |
| 318 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 319 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 320 | − condition.NewFunctions(func1, func2), | |
| 321 | − ) | |
| 322 | − | |
| 323 | − testCases := []struct { | |
| 324 | − statement Statement | |
| 325 | − expectedResult []byte | |
| 326 | − expectErr bool | |
| 327 | − }{ | |
| 328 | − {case1Statement, case1Data, false}, | |
| 329 | − {case2Statement, case2Data, false}, | |
| 330 | − {case3Statement, case3Data, false}, | |
| 331 | − // Invalid statement error. | |
| 332 | − {case4Statement, nil, true}, | |
| 333 | − } | |
| 334 | − | |
| 335 | − for i, testCase := range testCases { | |
| 336 | − result, err := json.Marshal(testCase.statement) | |
| 337 | − expectErr := (err != nil) | |
| 338 | − | |
| 339 | − if expectErr != testCase.expectErr { | |
| 340 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 341 | − } | |
| 342 | − | |
| 343 | − if !testCase.expectErr { | |
| 344 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 345 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, string(testCase.expectedResult), string(result)) | |
| 346 | − } | |
| 347 | − } | |
| 348 | − } | |
| 349 | −} | |
| 350 | − | |
| 351 | −func TestStatementUnmarshalJSON(t *testing.T) { | |
| 352 | − case1Data := []byte(`{ | |
| 353 | − "Sid": "SomeId1", | |
| 354 | − "Effect": "Allow", | |
| 355 | − "Principal": "*", | |
| 356 | − "Action": "s3:PutObject", | |
| 357 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 358 | −}`) | |
| 359 | − case1Statement := NewStatement( | |
| 360 | − Allow, | |
| 361 | − NewPrincipal("*"), | |
| 362 | − NewActionSet(PutObjectAction), | |
| 363 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 364 | − condition.NewFunctions(), | |
| 365 | − ) | |
| 366 | − case1Statement.SID = "SomeId1" | |
| 367 | − | |
| 368 | − case2Data := []byte(`{ | |
| 369 | − "Effect": "Allow", | |
| 370 | − "Principal": "*", | |
| 371 | − "Action": "s3:PutObject", | |
| 372 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 373 | − "Condition": { | |
| 374 | − "Null": { | |
| 375 | − "s3:x-amz-copy-source": true | |
| 376 | − } | |
| 377 | − } | |
| 378 | −}`) | |
| 379 | − func1, err := condition.NewNullFunc( | |
| 380 | − condition.S3XAmzCopySource, | |
| 381 | − true, | |
| 382 | − ) | |
| 383 | − if err != nil { | |
| 384 | − t.Fatalf("unexpected error. %v\n", err) | |
| 385 | − } | |
| 386 | − case2Statement := NewStatement( | |
| 387 | − Allow, | |
| 388 | − NewPrincipal("*"), | |
| 389 | − NewActionSet(PutObjectAction), | |
| 390 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 391 | − condition.NewFunctions(func1), | |
| 392 | − ) | |
| 393 | − | |
| 394 | − case3Data := []byte(`{ | |
| 395 | − "Effect": "Deny", | |
| 396 | − "Principal": { | |
| 397 | − "AWS": "*" | |
| 398 | − }, | |
| 399 | − "Action": [ | |
| 400 | − "s3:PutObject", | |
| 401 | − "s3:GetObject" | |
| 402 | − ], | |
| 403 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 404 | − "Condition": { | |
| 405 | − "Null": { | |
| 406 | − "s3:x-amz-server-side-encryption": "false" | |
| 407 | − } | |
| 408 | − } | |
| 409 | −}`) | |
| 410 | − func2, err := condition.NewNullFunc( | |
| 411 | − condition.S3XAmzServerSideEncryption, | |
| 412 | − false, | |
| 413 | − ) | |
| 414 | − if err != nil { | |
| 415 | − t.Fatalf("unexpected error. %v\n", err) | |
| 416 | − } | |
| 417 | − case3Statement := NewStatement( | |
| 418 | − Deny, | |
| 419 | − NewPrincipal("*"), | |
| 420 | − NewActionSet(PutObjectAction, GetObjectAction), | |
| 421 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 422 | − condition.NewFunctions(func2), | |
| 423 | − ) | |
| 424 | − | |
| 425 | − case4Data := []byte(`{ | |
| 426 | − "Effect": "Allow", | |
| 427 | − "Principal": "Q3AM3UQ867SPQQA43P2F", | |
| 428 | − "Action": "s3:PutObject", | |
| 429 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 430 | −}`) | |
| 431 | − | |
| 432 | − case5Data := []byte(`{ | |
| 433 | − "Principal": "*", | |
| 434 | − "Action": "s3:PutObject", | |
| 435 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 436 | −}`) | |
| 437 | − | |
| 438 | − case6Data := []byte(`{ | |
| 439 | − "Effect": "Allow", | |
| 440 | − "Action": "s3:PutObject", | |
| 441 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 442 | −}`) | |
| 443 | − | |
| 444 | − case7Data := []byte(`{ | |
| 445 | − "Effect": "Allow", | |
| 446 | − "Principal": "*", | |
| 447 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 448 | −}`) | |
| 449 | − | |
| 450 | − case8Data := []byte(`{ | |
| 451 | − "Effect": "Allow", | |
| 452 | − "Principal": "*", | |
| 453 | − "Action": "s3:PutObject" | |
| 454 | −}`) | |
| 455 | − | |
| 456 | − case9Data := []byte(`{ | |
| 457 | − "Effect": "Allow", | |
| 458 | − "Principal": "*", | |
| 459 | − "Action": "s3:PutObject", | |
| 460 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 461 | − "Condition": { | |
| 462 | − } | |
| 463 | −}`) | |
| 464 | − | |
| 465 | − case10Data := []byte(`{ | |
| 466 | − "Effect": "Deny", | |
| 467 | − "Principal": { | |
| 468 | − "AWS": "*" | |
| 469 | − }, | |
| 470 | − "Action": [ | |
| 471 | − "s3:PutObject", | |
| 472 | − "s3:GetObject" | |
| 473 | − ], | |
| 474 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 475 | − "Condition": { | |
| 476 | − "StringEquals": { | |
| 477 | − "s3:x-amz-copy-source": "yourbucket/myobject*" | |
| 478 | − } | |
| 479 | − } | |
| 480 | −}`) | |
| 481 | − | |
| 482 | − testCases := []struct { | |
| 483 | − data []byte | |
| 484 | − expectedResult Statement | |
| 485 | − expectErr bool | |
| 486 | − }{ | |
| 487 | − {case1Data, case1Statement, false}, | |
| 488 | − {case2Data, case2Statement, false}, | |
| 489 | − {case3Data, case3Statement, false}, | |
| 490 | − // JSON unmarshaling error. | |
| 491 | − {case4Data, Statement{}, true}, | |
| 492 | − // Invalid effect error. | |
| 493 | − {case5Data, Statement{}, true}, | |
| 494 | − // empty principal error. | |
| 495 | − {case6Data, Statement{}, true}, | |
| 496 | − // Empty action error. | |
| 497 | − {case7Data, Statement{}, true}, | |
| 498 | − // Empty resource error. | |
| 499 | − {case8Data, Statement{}, true}, | |
| 500 | − // Empty condition error. | |
| 501 | − {case9Data, Statement{}, true}, | |
| 502 | − // Unsupported condition key error. | |
| 503 | − {case10Data, Statement{}, true}, | |
| 504 | − } | |
| 505 | − | |
| 506 | − for i, testCase := range testCases { | |
| 507 | − var result Statement | |
| 508 | − err := json.Unmarshal(testCase.data, &result) | |
| 509 | − expectErr := (err != nil) | |
| 510 | − | |
| 511 | − if expectErr != testCase.expectErr { | |
| 512 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 513 | − } | |
| 514 | − | |
| 515 | − if !testCase.expectErr { | |
| 516 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 517 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 518 | − } | |
| 519 | − } | |
| 520 | − } | |
| 521 | −} | |
| 522 | − | |
| 523 | −func TestStatementValidate(t *testing.T) { | |
| 524 | − case1Statement := NewStatement( | |
| 525 | − Allow, | |
| 526 | − NewPrincipal("*"), | |
| 527 | − NewActionSet(PutObjectAction), | |
| 528 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 529 | − condition.NewFunctions(), | |
| 530 | − ) | |
| 531 | − | |
| 532 | − func1, err := condition.NewNullFunc( | |
| 533 | − condition.S3XAmzCopySource, | |
| 534 | − true, | |
| 535 | − ) | |
| 536 | − if err != nil { | |
| 537 | − t.Fatalf("unexpected error. %v\n", err) | |
| 538 | − } | |
| 539 | − func2, err := condition.NewNullFunc( | |
| 540 | − condition.S3XAmzServerSideEncryption, | |
| 541 | − false, | |
| 542 | − ) | |
| 543 | − if err != nil { | |
| 544 | − t.Fatalf("unexpected error. %v\n", err) | |
| 545 | − } | |
| 546 | − case2Statement := NewStatement( | |
| 547 | − Allow, | |
| 548 | − NewPrincipal("*"), | |
| 549 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 550 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 551 | − condition.NewFunctions(func1, func2), | |
| 552 | − ) | |
| 553 | − | |
| 554 | − testCases := []struct { | |
| 555 | − statement Statement | |
| 556 | − bucketName string | |
| 557 | − expectErr bool | |
| 558 | − }{ | |
| 559 | − {case1Statement, "mybucket", false}, | |
| 560 | − {case2Statement, "mybucket", true}, | |
| 561 | − {case1Statement, "yourbucket", true}, | |
| 562 | − } | |
| 563 | − | |
| 564 | − for i, testCase := range testCases { | |
| 565 | − err := testCase.statement.Validate(testCase.bucketName) | |
| 566 | − expectErr := (err != nil) | |
| 567 | − | |
| 568 | − if expectErr != testCase.expectErr { | |
| 569 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 570 | − } | |
| 571 | − } | |
| 572 | −} | |
| @@ -1,439 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 22 | − "github.com/minio/pkg/wildcard" | |
| 23 | −) | |
| 24 | − | |
| 25 | −// Action - policy action. | |
| 26 | −// Refer https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazons3.html | |
| 27 | −// for more information about available actions. | |
| 28 | −type Action string | |
| 29 | − | |
| 30 | −const ( | |
| 31 | − // AbortMultipartUploadAction - AbortMultipartUpload Rest API action. | |
| 32 | − AbortMultipartUploadAction Action = "s3:AbortMultipartUpload" | |
| 33 | − | |
| 34 | − // CreateBucketAction - CreateBucket Rest API action. | |
| 35 | − CreateBucketAction = "s3:CreateBucket" | |
| 36 | − | |
| 37 | − // DeleteBucketAction - DeleteBucket Rest API action. | |
| 38 | − DeleteBucketAction = "s3:DeleteBucket" | |
| 39 | − | |
| 40 | − // ForceDeleteBucketAction - DeleteBucket Rest API action when x-minio-force-delete flag | |
| 41 | − // is specified. | |
| 42 | − ForceDeleteBucketAction = "s3:ForceDeleteBucket" | |
| 43 | − | |
| 44 | − // DeleteBucketPolicyAction - DeleteBucketPolicy Rest API action. | |
| 45 | − DeleteBucketPolicyAction = "s3:DeleteBucketPolicy" | |
| 46 | − | |
| 47 | − // DeleteObjectAction - DeleteObject Rest API action. | |
| 48 | − DeleteObjectAction = "s3:DeleteObject" | |
| 49 | − | |
| 50 | − // GetBucketLocationAction - GetBucketLocation Rest API action. | |
| 51 | − GetBucketLocationAction = "s3:GetBucketLocation" | |
| 52 | − | |
| 53 | − // GetBucketNotificationAction - GetBucketNotification Rest API action. | |
| 54 | − GetBucketNotificationAction = "s3:GetBucketNotification" | |
| 55 | − | |
| 56 | − // GetBucketPolicyAction - GetBucketPolicy Rest API action. | |
| 57 | − GetBucketPolicyAction = "s3:GetBucketPolicy" | |
| 58 | − | |
| 59 | − // GetObjectAction - GetObject Rest API action. | |
| 60 | − GetObjectAction = "s3:GetObject" | |
| 61 | − | |
| 62 | − // HeadBucketAction - HeadBucket Rest API action. This action is unused in minio. | |
| 63 | − HeadBucketAction = "s3:HeadBucket" | |
| 64 | − | |
| 65 | − // ListAllMyBucketsAction - ListAllMyBuckets (List buckets) Rest API action. | |
| 66 | − ListAllMyBucketsAction = "s3:ListAllMyBuckets" | |
| 67 | − | |
| 68 | − // ListBucketAction - ListBucket Rest API action. | |
| 69 | − ListBucketAction = "s3:ListBucket" | |
| 70 | − | |
| 71 | − // GetBucketPolicyStatusAction - Retrieves the policy status for a bucket. | |
| 72 | − GetBucketPolicyStatusAction = "s3:GetBucketPolicyStatus" | |
| 73 | − | |
| 74 | − // ListBucketVersionsAction - ListBucketVersions Rest API action. | |
| 75 | − ListBucketVersionsAction = "s3:ListBucketVersions" | |
| 76 | − | |
| 77 | − // ListBucketMultipartUploadsAction - ListMultipartUploads Rest API action. | |
| 78 | − ListBucketMultipartUploadsAction = "s3:ListBucketMultipartUploads" | |
| 79 | − | |
| 80 | − // ListenNotificationAction - ListenNotification Rest API action. | |
| 81 | − // This is MinIO extension. | |
| 82 | − ListenNotificationAction = "s3:ListenNotification" | |
| 83 | − | |
| 84 | − // ListenBucketNotificationAction - ListenBucketNotification Rest API action. | |
| 85 | − // This is MinIO extension. | |
| 86 | − ListenBucketNotificationAction = "s3:ListenBucketNotification" | |
| 87 | − | |
| 88 | − // ListMultipartUploadPartsAction - ListParts Rest API action. | |
| 89 | − ListMultipartUploadPartsAction = "s3:ListMultipartUploadParts" | |
| 90 | − | |
| 91 | − // PutBucketLifecycleAction - PutBucketLifecycle Rest API action. | |
| 92 | − PutBucketLifecycleAction = "s3:PutLifecycleConfiguration" | |
| 93 | − | |
| 94 | − // GetBucketLifecycleAction - GetBucketLifecycle Rest API action. | |
| 95 | − GetBucketLifecycleAction = "s3:GetLifecycleConfiguration" | |
| 96 | − | |
| 97 | − // PutBucketNotificationAction - PutObjectNotification Rest API action. | |
| 98 | − PutBucketNotificationAction = "s3:PutBucketNotification" | |
| 99 | − | |
| 100 | − // PutBucketPolicyAction - PutBucketPolicy Rest API action. | |
| 101 | − PutBucketPolicyAction = "s3:PutBucketPolicy" | |
| 102 | − | |
| 103 | − // PutObjectAction - PutObject Rest API action. | |
| 104 | − PutObjectAction = "s3:PutObject" | |
| 105 | − | |
| 106 | − // DeleteObjectVersionAction - DeleteObjectVersion Rest API action. | |
| 107 | − DeleteObjectVersionAction = "s3:DeleteObjectVersion" | |
| 108 | − | |
| 109 | − // DeleteObjectVersionTaggingAction - DeleteObjectVersionTagging Rest API action. | |
| 110 | − DeleteObjectVersionTaggingAction = "s3:DeleteObjectVersionTagging" | |
| 111 | − | |
| 112 | − // GetObjectVersionAction - GetObjectVersionAction Rest API action. | |
| 113 | − GetObjectVersionAction = "s3:GetObjectVersion" | |
| 114 | − | |
| 115 | − // GetObjectVersionTaggingAction - GetObjectVersionTagging Rest API action. | |
| 116 | − GetObjectVersionTaggingAction = "s3:GetObjectVersionTagging" | |
| 117 | − | |
| 118 | − // PutObjectVersionTaggingAction - PutObjectVersionTagging Rest API action. | |
| 119 | − PutObjectVersionTaggingAction = "s3:PutObjectVersionTagging" | |
| 120 | − | |
| 121 | − // BypassGovernanceRetentionAction - bypass governance retention for PutObjectRetention, PutObject and DeleteObject Rest API action. | |
| 122 | − BypassGovernanceRetentionAction = "s3:BypassGovernanceRetention" | |
| 123 | − | |
| 124 | − // PutObjectRetentionAction - PutObjectRetention Rest API action. | |
| 125 | − PutObjectRetentionAction = "s3:PutObjectRetention" | |
| 126 | − | |
| 127 | − // GetObjectRetentionAction - GetObjectRetention, GetObject, HeadObject Rest API action. | |
| 128 | − GetObjectRetentionAction = "s3:GetObjectRetention" | |
| 129 | − | |
| 130 | − // GetObjectLegalHoldAction - GetObjectLegalHold, GetObject Rest API action. | |
| 131 | − GetObjectLegalHoldAction = "s3:GetObjectLegalHold" | |
| 132 | − | |
| 133 | − // PutObjectLegalHoldAction - PutObjectLegalHold, PutObject Rest API action. | |
| 134 | − PutObjectLegalHoldAction = "s3:PutObjectLegalHold" | |
| 135 | − | |
| 136 | − // GetBucketObjectLockConfigurationAction - GetBucketObjectLockConfiguration Rest API action | |
| 137 | − GetBucketObjectLockConfigurationAction = "s3:GetBucketObjectLockConfiguration" | |
| 138 | − | |
| 139 | − // PutBucketObjectLockConfigurationAction - PutBucketObjectLockConfiguration Rest API action | |
| 140 | − PutBucketObjectLockConfigurationAction = "s3:PutBucketObjectLockConfiguration" | |
| 141 | − | |
| 142 | − // GetBucketTaggingAction - GetBucketTagging Rest API action | |
| 143 | − GetBucketTaggingAction = "s3:GetBucketTagging" | |
| 144 | − | |
| 145 | − // PutBucketTaggingAction - PutBucketTagging Rest API action | |
| 146 | − PutBucketTaggingAction = "s3:PutBucketTagging" | |
| 147 | − | |
| 148 | − // GetObjectTaggingAction - Get Object Tags API action | |
| 149 | − GetObjectTaggingAction = "s3:GetObjectTagging" | |
| 150 | − | |
| 151 | − // PutObjectTaggingAction - Put Object Tags API action | |
| 152 | − PutObjectTaggingAction = "s3:PutObjectTagging" | |
| 153 | − | |
| 154 | − // DeleteObjectTaggingAction - Delete Object Tags API action | |
| 155 | − DeleteObjectTaggingAction = "s3:DeleteObjectTagging" | |
| 156 | − | |
| 157 | − // PutBucketEncryptionAction - PutBucketEncryption REST API action | |
| 158 | − PutBucketEncryptionAction = "s3:PutEncryptionConfiguration" | |
| 159 | − | |
| 160 | − // GetBucketEncryptionAction - GetBucketEncryption REST API action | |
| 161 | − GetBucketEncryptionAction = "s3:GetEncryptionConfiguration" | |
| 162 | − | |
| 163 | − // PutBucketVersioningAction - PutBucketVersioning REST API action | |
| 164 | − PutBucketVersioningAction = "s3:PutBucketVersioning" | |
| 165 | − | |
| 166 | − // GetBucketVersioningAction - GetBucketVersioning REST API action | |
| 167 | − GetBucketVersioningAction = "s3:GetBucketVersioning" | |
| 168 | − // GetReplicationConfigurationAction - GetReplicationConfiguration REST API action | |
| 169 | − GetReplicationConfigurationAction = "s3:GetReplicationConfiguration" | |
| 170 | − // PutReplicationConfigurationAction - PutReplicationConfiguration REST API action | |
| 171 | − PutReplicationConfigurationAction = "s3:PutReplicationConfiguration" | |
| 172 | − | |
| 173 | − // ReplicateObjectAction - ReplicateObject REST API action | |
| 174 | − ReplicateObjectAction = "s3:ReplicateObject" | |
| 175 | − | |
| 176 | − // ReplicateDeleteAction - ReplicateDelete REST API action | |
| 177 | − ReplicateDeleteAction = "s3:ReplicateDelete" | |
| 178 | − | |
| 179 | − // ReplicateTagsAction - ReplicateTags REST API action | |
| 180 | − ReplicateTagsAction = "s3:ReplicateTags" | |
| 181 | − | |
| 182 | − // GetObjectVersionForReplicationAction - GetObjectVersionForReplication REST API action | |
| 183 | − GetObjectVersionForReplicationAction = "s3:GetObjectVersionForReplication" | |
| 184 | − | |
| 185 | − // AllActions - all API actions | |
| 186 | − AllActions = "s3:*" | |
| 187 | −) | |
| 188 | − | |
| 189 | −// List of all supported actions. | |
| 190 | −var supportedActions = map[Action]struct{}{ | |
| 191 | − AbortMultipartUploadAction: {}, | |
| 192 | − CreateBucketAction: {}, | |
| 193 | − DeleteBucketAction: {}, | |
| 194 | − ForceDeleteBucketAction: {}, | |
| 195 | − DeleteBucketPolicyAction: {}, | |
| 196 | − DeleteObjectAction: {}, | |
| 197 | − GetBucketLocationAction: {}, | |
| 198 | − GetBucketNotificationAction: {}, | |
| 199 | − GetBucketPolicyAction: {}, | |
| 200 | − GetObjectAction: {}, | |
| 201 | − HeadBucketAction: {}, | |
| 202 | − ListAllMyBucketsAction: {}, | |
| 203 | − ListBucketAction: {}, | |
| 204 | − GetBucketPolicyStatusAction: {}, | |
| 205 | − ListBucketVersionsAction: {}, | |
| 206 | − ListBucketMultipartUploadsAction: {}, | |
| 207 | − ListenNotificationAction: {}, | |
| 208 | − ListenBucketNotificationAction: {}, | |
| 209 | − ListMultipartUploadPartsAction: {}, | |
| 210 | − PutBucketLifecycleAction: {}, | |
| 211 | − GetBucketLifecycleAction: {}, | |
| 212 | − PutBucketNotificationAction: {}, | |
| 213 | − PutBucketPolicyAction: {}, | |
| 214 | − PutObjectAction: {}, | |
| 215 | − BypassGovernanceRetentionAction: {}, | |
| 216 | − PutObjectRetentionAction: {}, | |
| 217 | − GetObjectRetentionAction: {}, | |
| 218 | − GetObjectLegalHoldAction: {}, | |
| 219 | − PutObjectLegalHoldAction: {}, | |
| 220 | − GetBucketObjectLockConfigurationAction: {}, | |
| 221 | − PutBucketObjectLockConfigurationAction: {}, | |
| 222 | − GetBucketTaggingAction: {}, | |
| 223 | − PutBucketTaggingAction: {}, | |
| 224 | − GetObjectVersionAction: {}, | |
| 225 | − GetObjectVersionTaggingAction: {}, | |
| 226 | − DeleteObjectVersionAction: {}, | |
| 227 | − DeleteObjectVersionTaggingAction: {}, | |
| 228 | − PutObjectVersionTaggingAction: {}, | |
| 229 | − GetObjectTaggingAction: {}, | |
| 230 | − PutObjectTaggingAction: {}, | |
| 231 | − DeleteObjectTaggingAction: {}, | |
| 232 | − PutBucketEncryptionAction: {}, | |
| 233 | − GetBucketEncryptionAction: {}, | |
| 234 | − PutBucketVersioningAction: {}, | |
| 235 | − GetBucketVersioningAction: {}, | |
| 236 | − GetReplicationConfigurationAction: {}, | |
| 237 | − PutReplicationConfigurationAction: {}, | |
| 238 | − ReplicateObjectAction: {}, | |
| 239 | − ReplicateDeleteAction: {}, | |
| 240 | − ReplicateTagsAction: {}, | |
| 241 | − GetObjectVersionForReplicationAction: {}, | |
| 242 | − AllActions: {}, | |
| 243 | −} | |
| 244 | − | |
| 245 | −// List of all supported object actions. | |
| 246 | −var supportedObjectActions = map[Action]struct{}{ | |
| 247 | − AllActions: {}, | |
| 248 | − AbortMultipartUploadAction: {}, | |
| 249 | − DeleteObjectAction: {}, | |
| 250 | − GetObjectAction: {}, | |
| 251 | − ListMultipartUploadPartsAction: {}, | |
| 252 | − PutObjectAction: {}, | |
| 253 | − BypassGovernanceRetentionAction: {}, | |
| 254 | − PutObjectRetentionAction: {}, | |
| 255 | − GetObjectRetentionAction: {}, | |
| 256 | − PutObjectLegalHoldAction: {}, | |
| 257 | − GetObjectLegalHoldAction: {}, | |
| 258 | − GetObjectTaggingAction: {}, | |
| 259 | − PutObjectTaggingAction: {}, | |
| 260 | − DeleteObjectTaggingAction: {}, | |
| 261 | − GetObjectVersionAction: {}, | |
| 262 | − GetObjectVersionTaggingAction: {}, | |
| 263 | − DeleteObjectVersionAction: {}, | |
| 264 | − DeleteObjectVersionTaggingAction: {}, | |
| 265 | − PutObjectVersionTaggingAction: {}, | |
| 266 | − ReplicateObjectAction: {}, | |
| 267 | − ReplicateDeleteAction: {}, | |
| 268 | − ReplicateTagsAction: {}, | |
| 269 | − GetObjectVersionForReplicationAction: {}, | |
| 270 | −} | |
| 271 | − | |
| 272 | −// isObjectAction - returns whether action is object type or not. | |
| 273 | −func (action Action) isObjectAction() bool { | |
| 274 | − for supAction := range supportedObjectActions { | |
| 275 | − if action.Match(supAction) { | |
| 276 | − return true | |
| 277 | − } | |
| 278 | − } | |
| 279 | − return false | |
| 280 | −} | |
| 281 | − | |
| 282 | −// Match - matches action name with action patter. | |
| 283 | −func (action Action) Match(a Action) bool { | |
| 284 | − return wildcard.Match(string(action), string(a)) | |
| 285 | −} | |
| 286 | − | |
| 287 | −// IsValid - checks if action is valid or not. | |
| 288 | −func (action Action) IsValid() bool { | |
| 289 | − for supAction := range supportedActions { | |
| 290 | − if action.Match(supAction) { | |
| 291 | − return true | |
| 292 | − } | |
| 293 | − } | |
| 294 | − return false | |
| 295 | −} | |
| 296 | − | |
| 297 | −type actionConditionKeyMap map[Action]condition.KeySet | |
| 298 | − | |
| 299 | −func (a actionConditionKeyMap) Lookup(action Action) condition.KeySet { | |
| 300 | − var ckeysMerged = condition.NewKeySet(condition.CommonKeys...) | |
| 301 | − for act, ckey := range a { | |
| 302 | − if action.Match(act) { | |
| 303 | − ckeysMerged.Merge(ckey) | |
| 304 | − } | |
| 305 | − } | |
| 306 | − return ckeysMerged | |
| 307 | −} | |
| 308 | − | |
| 309 | −// iamActionConditionKeyMap - holds mapping of supported condition key for an action. | |
| 310 | −var iamActionConditionKeyMap = actionConditionKeyMap{ | |
| 311 | − AllActions: condition.NewKeySet(condition.AllSupportedKeys...), | |
| 312 | − | |
| 313 | − GetObjectAction: condition.NewKeySet( | |
| 314 | − append([]condition.Key{ | |
| 315 | − condition.S3XAmzServerSideEncryption, | |
| 316 | − condition.S3XAmzServerSideEncryptionCustomerAlgorithm, | |
| 317 | − condition.S3VersionID, | |
| 318 | − }, condition.CommonKeys...)...), | |
| 319 | − | |
| 320 | − ListBucketAction: condition.NewKeySet( | |
| 321 | − append([]condition.Key{ | |
| 322 | − condition.S3Prefix, | |
| 323 | − condition.S3Delimiter, | |
| 324 | − condition.S3MaxKeys, | |
| 325 | − }, condition.CommonKeys...)...), | |
| 326 | − | |
| 327 | − ListBucketVersionsAction: condition.NewKeySet( | |
| 328 | − append([]condition.Key{ | |
| 329 | − condition.S3Prefix, | |
| 330 | − condition.S3Delimiter, | |
| 331 | − condition.S3MaxKeys, | |
| 332 | − }, condition.CommonKeys...)...), | |
| 333 | − | |
| 334 | − DeleteObjectAction: condition.NewKeySet( | |
| 335 | − append([]condition.Key{ | |
| 336 | − condition.S3VersionID, | |
| 337 | − }, condition.CommonKeys...)...), | |
| 338 | − | |
| 339 | − PutObjectAction: condition.NewKeySet( | |
| 340 | − append([]condition.Key{ | |
| 341 | − condition.S3XAmzCopySource, | |
| 342 | − condition.S3XAmzServerSideEncryption, | |
| 343 | − condition.S3XAmzServerSideEncryptionCustomerAlgorithm, | |
| 344 | − condition.S3XAmzMetadataDirective, | |
| 345 | − condition.S3XAmzStorageClass, | |
| 346 | − condition.S3VersionID, | |
| 347 | − condition.S3ObjectLockRetainUntilDate, | |
| 348 | − condition.S3ObjectLockMode, | |
| 349 | − condition.S3ObjectLockLegalHold, | |
| 350 | − }, condition.CommonKeys...)...), | |
| 351 | − | |
| 352 | − // https://docs.aws.amazon.com/AmazonS3/latest/dev/list_amazons3.html | |
| 353 | − // LockLegalHold is not supported with PutObjectRetentionAction | |
| 354 | − PutObjectRetentionAction: condition.NewKeySet( | |
| 355 | − append([]condition.Key{ | |
| 356 | − condition.S3XAmzServerSideEncryption, | |
| 357 | − condition.S3XAmzServerSideEncryptionCustomerAlgorithm, | |
| 358 | − condition.S3ObjectLockRemainingRetentionDays, | |
| 359 | − condition.S3ObjectLockRetainUntilDate, | |
| 360 | − condition.S3ObjectLockMode, | |
| 361 | − condition.S3VersionID, | |
| 362 | − }, condition.CommonKeys...)...), | |
| 363 | − | |
| 364 | − GetObjectRetentionAction: condition.NewKeySet( | |
| 365 | − append([]condition.Key{ | |
| 366 | − condition.S3XAmzServerSideEncryption, | |
| 367 | − condition.S3XAmzServerSideEncryptionCustomerAlgorithm, | |
| 368 | − condition.S3VersionID, | |
| 369 | − }, condition.CommonKeys...)...), | |
| 370 | − | |
| 371 | − PutObjectLegalHoldAction: condition.NewKeySet( | |
| 372 | − append([]condition.Key{ | |
| 373 | − condition.S3XAmzServerSideEncryption, | |
| 374 | − condition.S3XAmzServerSideEncryptionCustomerAlgorithm, | |
| 375 | − condition.S3ObjectLockLegalHold, | |
| 376 | − condition.S3VersionID, | |
| 377 | − }, condition.CommonKeys...)...), | |
| 378 | − GetObjectLegalHoldAction: condition.NewKeySet(condition.CommonKeys...), | |
| 379 | − | |
| 380 | − // https://docs.aws.amazon.com/AmazonS3/latest/dev/list_amazons3.html | |
| 381 | − BypassGovernanceRetentionAction: condition.NewKeySet( | |
| 382 | − append([]condition.Key{ | |
| 383 | − condition.S3VersionID, | |
| 384 | − condition.S3ObjectLockRemainingRetentionDays, | |
| 385 | − condition.S3ObjectLockRetainUntilDate, | |
| 386 | − condition.S3ObjectLockMode, | |
| 387 | − condition.S3ObjectLockLegalHold, | |
| 388 | − }, condition.CommonKeys...)...), | |
| 389 | − | |
| 390 | − PutObjectTaggingAction: condition.NewKeySet( | |
| 391 | − append([]condition.Key{ | |
| 392 | − condition.S3VersionID, | |
| 393 | − }, condition.CommonKeys...)...), | |
| 394 | − GetObjectTaggingAction: condition.NewKeySet( | |
| 395 | − append([]condition.Key{ | |
| 396 | − condition.S3VersionID, | |
| 397 | − }, condition.CommonKeys...)...), | |
| 398 | − DeleteObjectTaggingAction: condition.NewKeySet( | |
| 399 | − append([]condition.Key{ | |
| 400 | − condition.S3VersionID, | |
| 401 | − }, condition.CommonKeys...)...), | |
| 402 | − | |
| 403 | − PutObjectVersionTaggingAction: condition.NewKeySet( | |
| 404 | − append([]condition.Key{ | |
| 405 | − condition.S3VersionID, | |
| 406 | − }, condition.CommonKeys...)...), | |
| 407 | − GetObjectVersionAction: condition.NewKeySet( | |
| 408 | − append([]condition.Key{ | |
| 409 | − condition.S3VersionID, | |
| 410 | − }, condition.CommonKeys...)...), | |
| 411 | − GetObjectVersionTaggingAction: condition.NewKeySet( | |
| 412 | − append([]condition.Key{ | |
| 413 | − condition.S3VersionID, | |
| 414 | − }, condition.CommonKeys...)...), | |
| 415 | − DeleteObjectVersionAction: condition.NewKeySet( | |
| 416 | − append([]condition.Key{ | |
| 417 | − condition.S3VersionID, | |
| 418 | − }, condition.CommonKeys...)...), | |
| 419 | − DeleteObjectVersionTaggingAction: condition.NewKeySet( | |
| 420 | − append([]condition.Key{ | |
| 421 | − condition.S3VersionID, | |
| 422 | − }, condition.CommonKeys...)...), | |
| 423 | − ReplicateObjectAction: condition.NewKeySet( | |
| 424 | − append([]condition.Key{ | |
| 425 | − condition.S3VersionID, | |
| 426 | − }, condition.CommonKeys...)...), | |
| 427 | − ReplicateDeleteAction: condition.NewKeySet( | |
| 428 | − append([]condition.Key{ | |
| 429 | − condition.S3VersionID, | |
| 430 | − }, condition.CommonKeys...)...), | |
| 431 | − ReplicateTagsAction: condition.NewKeySet( | |
| 432 | − append([]condition.Key{ | |
| 433 | − condition.S3VersionID, | |
| 434 | − }, condition.CommonKeys...)...), | |
| 435 | − GetObjectVersionForReplicationAction: condition.NewKeySet( | |
| 436 | − append([]condition.Key{ | |
| 437 | − condition.S3VersionID, | |
| 438 | − }, condition.CommonKeys...)...), | |
| 439 | −} | |
| @@ -1,63 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "testing" | |
| 22 | −) | |
| 23 | − | |
| 24 | −func TestActionIsObjectAction(t *testing.T) { | |
| 25 | − testCases := []struct { | |
| 26 | − action Action | |
| 27 | − expectedResult bool | |
| 28 | − }{ | |
| 29 | − {AbortMultipartUploadAction, true}, | |
| 30 | − {DeleteObjectAction, true}, | |
| 31 | − {GetObjectAction, true}, | |
| 32 | − {ListMultipartUploadPartsAction, true}, | |
| 33 | − {PutObjectAction, true}, | |
| 34 | − {CreateBucketAction, false}, | |
| 35 | − } | |
| 36 | − | |
| 37 | − for i, testCase := range testCases { | |
| 38 | − result := testCase.action.isObjectAction() | |
| 39 | − | |
| 40 | − if testCase.expectedResult != result { | |
| 41 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 42 | − } | |
| 43 | − } | |
| 44 | −} | |
| 45 | − | |
| 46 | −func TestActionIsValid(t *testing.T) { | |
| 47 | − testCases := []struct { | |
| 48 | − action Action | |
| 49 | − expectedResult bool | |
| 50 | − }{ | |
| 51 | − {PutObjectAction, true}, | |
| 52 | − {AbortMultipartUploadAction, true}, | |
| 53 | − {Action("foo"), false}, | |
| 54 | − } | |
| 55 | − | |
| 56 | − for i, testCase := range testCases { | |
| 57 | − result := testCase.action.IsValid() | |
| 58 | − | |
| 59 | − if testCase.expectedResult != result { | |
| 60 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 61 | − } | |
| 62 | − } | |
| 63 | −} | |
| @@ -1,182 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | − "sort" | |
| 24 | − | |
| 25 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 26 | −) | |
| 27 | − | |
| 28 | −// ActionSet - set of actions. | |
| 29 | −type ActionSet map[Action]struct{} | |
| 30 | − | |
| 31 | −// Clone clones ActionSet structure | |
| 32 | −func (actionSet ActionSet) Clone() ActionSet { | |
| 33 | − return NewActionSet(actionSet.ToSlice()...) | |
| 34 | −} | |
| 35 | − | |
| 36 | −// Add - add action to the set. | |
| 37 | −func (actionSet ActionSet) Add(action Action) { | |
| 38 | − actionSet[action] = struct{}{} | |
| 39 | −} | |
| 40 | − | |
| 41 | −// IsEmpty - returns if the current action set is empty | |
| 42 | −func (actionSet ActionSet) IsEmpty() bool { | |
| 43 | − return len(actionSet) == 0 | |
| 44 | −} | |
| 45 | − | |
| 46 | −// Match - matches object name with anyone of action pattern in action set. | |
| 47 | −func (actionSet ActionSet) Match(action Action) bool { | |
| 48 | − for r := range actionSet { | |
| 49 | − if r.Match(action) { | |
| 50 | − return true | |
| 51 | − } | |
| 52 | − | |
| 53 | − // This is a special case where GetObjectVersion | |
| 54 | − // means GetObject is enabled implicitly. | |
| 55 | − switch r { | |
| 56 | − case GetObjectVersionAction: | |
| 57 | − if action == GetObjectAction { | |
| 58 | − return true | |
| 59 | − } | |
| 60 | − } | |
| 61 | − } | |
| 62 | − | |
| 63 | − return false | |
| 64 | −} | |
| 65 | − | |
| 66 | −// Equals - checks whether given action set is equal to current action set or not. | |
| 67 | −func (actionSet ActionSet) Equals(sactionSet ActionSet) bool { | |
| 68 | − // If length of set is not equal to length of given set, the | |
| 69 | − // set is not equal to given set. | |
| 70 | − if len(actionSet) != len(sactionSet) { | |
| 71 | − return false | |
| 72 | − } | |
| 73 | − | |
| 74 | − // As both sets are equal in length, check each elements are equal. | |
| 75 | − for k := range actionSet { | |
| 76 | − if _, ok := sactionSet[k]; !ok { | |
| 77 | − return false | |
| 78 | − } | |
| 79 | − } | |
| 80 | − | |
| 81 | − return true | |
| 82 | −} | |
| 83 | − | |
| 84 | −// Intersection - returns actions available in both ActionSet. | |
| 85 | −func (actionSet ActionSet) Intersection(sset ActionSet) ActionSet { | |
| 86 | − nset := NewActionSet() | |
| 87 | − for k := range actionSet { | |
| 88 | − if _, ok := sset[k]; ok { | |
| 89 | − nset.Add(k) | |
| 90 | − } | |
| 91 | − } | |
| 92 | − | |
| 93 | − return nset | |
| 94 | −} | |
| 95 | − | |
| 96 | −// MarshalJSON - encodes ActionSet to JSON data. | |
| 97 | −func (actionSet ActionSet) MarshalJSON() ([]byte, error) { | |
| 98 | − if len(actionSet) == 0 { | |
| 99 | − return nil, Errorf("empty action set") | |
| 100 | − } | |
| 101 | − | |
| 102 | − return json.Marshal(actionSet.ToSlice()) | |
| 103 | −} | |
| 104 | − | |
| 105 | −func (actionSet ActionSet) String() string { | |
| 106 | − actions := []string{} | |
| 107 | − for action := range actionSet { | |
| 108 | − actions = append(actions, string(action)) | |
| 109 | − } | |
| 110 | − sort.Strings(actions) | |
| 111 | − | |
| 112 | − return fmt.Sprintf("%v", actions) | |
| 113 | −} | |
| 114 | − | |
| 115 | −// ToSlice - returns slice of actions from the action set. | |
| 116 | −func (actionSet ActionSet) ToSlice() []Action { | |
| 117 | − actions := []Action{} | |
| 118 | − for action := range actionSet { | |
| 119 | − actions = append(actions, action) | |
| 120 | − } | |
| 121 | − | |
| 122 | − return actions | |
| 123 | −} | |
| 124 | − | |
| 125 | −// ToAdminSlice - returns slice of admin actions from the action set. | |
| 126 | −func (actionSet ActionSet) ToAdminSlice() []AdminAction { | |
| 127 | − actions := []AdminAction{} | |
| 128 | − for action := range actionSet { | |
| 129 | − actions = append(actions, AdminAction(action)) | |
| 130 | − } | |
| 131 | − | |
| 132 | − return actions | |
| 133 | −} | |
| 134 | − | |
| 135 | −// UnmarshalJSON - decodes JSON data to ActionSet. | |
| 136 | −func (actionSet *ActionSet) UnmarshalJSON(data []byte) error { | |
| 137 | − var sset set.StringSet | |
| 138 | − if err := json.Unmarshal(data, &sset); err != nil { | |
| 139 | − return err | |
| 140 | − } | |
| 141 | − | |
| 142 | − if len(sset) == 0 { | |
| 143 | − return Errorf("empty action set") | |
| 144 | − } | |
| 145 | − | |
| 146 | − *actionSet = make(ActionSet) | |
| 147 | − for _, s := range sset.ToSlice() { | |
| 148 | − actionSet.Add(Action(s)) | |
| 149 | − } | |
| 150 | − | |
| 151 | − return nil | |
| 152 | −} | |
| 153 | − | |
| 154 | −// ValidateAdmin checks if all actions are valid Admin actions | |
| 155 | −func (actionSet ActionSet) ValidateAdmin() error { | |
| 156 | − for _, action := range actionSet.ToAdminSlice() { | |
| 157 | − if !action.IsValid() { | |
| 158 | − return Errorf("unsupported admin action '%v'", action) | |
| 159 | − } | |
| 160 | − } | |
| 161 | − return nil | |
| 162 | −} | |
| 163 | − | |
| 164 | −// Validate checks if all actions are valid | |
| 165 | −func (actionSet ActionSet) Validate() error { | |
| 166 | − for _, action := range actionSet.ToSlice() { | |
| 167 | − if !action.IsValid() { | |
| 168 | − return Errorf("unsupported action '%v'", action) | |
| 169 | − } | |
| 170 | − } | |
| 171 | − return nil | |
| 172 | −} | |
| 173 | − | |
| 174 | −// NewActionSet - creates new action set. | |
| 175 | −func NewActionSet(actions ...Action) ActionSet { | |
| 176 | − actionSet := make(ActionSet) | |
| 177 | − for _, action := range actions { | |
| 178 | − actionSet.Add(action) | |
| 179 | − } | |
| 180 | − | |
| 181 | − return actionSet | |
| 182 | −} | |
| @@ -1,167 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "reflect" | |
| 23 | − "testing" | |
| 24 | −) | |
| 25 | − | |
| 26 | −func TestActionSetAdd(t *testing.T) { | |
| 27 | − testCases := []struct { | |
| 28 | − set ActionSet | |
| 29 | − action Action | |
| 30 | − expectedResult ActionSet | |
| 31 | − }{ | |
| 32 | − {NewActionSet(), PutObjectAction, NewActionSet(PutObjectAction)}, | |
| 33 | − {NewActionSet(PutObjectAction), PutObjectAction, NewActionSet(PutObjectAction)}, | |
| 34 | − } | |
| 35 | − | |
| 36 | − for i, testCase := range testCases { | |
| 37 | − testCase.set.Add(testCase.action) | |
| 38 | − | |
| 39 | − if !reflect.DeepEqual(testCase.expectedResult, testCase.set) { | |
| 40 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set) | |
| 41 | − } | |
| 42 | − } | |
| 43 | −} | |
| 44 | − | |
| 45 | −func TestActionSetMatches(t *testing.T) { | |
| 46 | − testCases := []struct { | |
| 47 | − set ActionSet | |
| 48 | − action Action | |
| 49 | − expectedResult bool | |
| 50 | − }{ | |
| 51 | − {NewActionSet(AllActions), AbortMultipartUploadAction, true}, | |
| 52 | − {NewActionSet(PutObjectAction), PutObjectAction, true}, | |
| 53 | − {NewActionSet(PutObjectAction, GetObjectAction), PutObjectAction, true}, | |
| 54 | − {NewActionSet(PutObjectAction, GetObjectAction), AbortMultipartUploadAction, false}, | |
| 55 | − } | |
| 56 | − | |
| 57 | − for i, testCase := range testCases { | |
| 58 | − result := testCase.set.Match(testCase.action) | |
| 59 | − | |
| 60 | − if result != testCase.expectedResult { | |
| 61 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 62 | − } | |
| 63 | − } | |
| 64 | −} | |
| 65 | − | |
| 66 | −func TestActionSetIntersection(t *testing.T) { | |
| 67 | − testCases := []struct { | |
| 68 | − set ActionSet | |
| 69 | − setToIntersect ActionSet | |
| 70 | − expectedResult ActionSet | |
| 71 | − }{ | |
| 72 | − {NewActionSet(), NewActionSet(PutObjectAction), NewActionSet()}, | |
| 73 | − {NewActionSet(PutObjectAction), NewActionSet(), NewActionSet()}, | |
| 74 | − {NewActionSet(PutObjectAction), NewActionSet(PutObjectAction, GetObjectAction), NewActionSet(PutObjectAction)}, | |
| 75 | − } | |
| 76 | − | |
| 77 | − for i, testCase := range testCases { | |
| 78 | − result := testCase.set.Intersection(testCase.setToIntersect) | |
| 79 | − | |
| 80 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 81 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set) | |
| 82 | − } | |
| 83 | − } | |
| 84 | −} | |
| 85 | − | |
| 86 | −func TestActionSetMarshalJSON(t *testing.T) { | |
| 87 | − testCases := []struct { | |
| 88 | − actionSet ActionSet | |
| 89 | − expectedResult []byte | |
| 90 | − expectErr bool | |
| 91 | − }{ | |
| 92 | − {NewActionSet(PutObjectAction), []byte(`["s3:PutObject"]`), false}, | |
| 93 | − {NewActionSet(), nil, true}, | |
| 94 | − } | |
| 95 | − | |
| 96 | − for i, testCase := range testCases { | |
| 97 | − result, err := json.Marshal(testCase.actionSet) | |
| 98 | − expectErr := (err != nil) | |
| 99 | − | |
| 100 | − if expectErr != testCase.expectErr { | |
| 101 | − t.Fatalf("case %v: error: expected: %v, got: %v\n", i+1, testCase.expectErr, expectErr) | |
| 102 | − } | |
| 103 | − | |
| 104 | − if !testCase.expectErr { | |
| 105 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 106 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, string(testCase.expectedResult), string(result)) | |
| 107 | − } | |
| 108 | − } | |
| 109 | − } | |
| 110 | −} | |
| 111 | − | |
| 112 | −func TestActionSetToSlice(t *testing.T) { | |
| 113 | − testCases := []struct { | |
| 114 | − actionSet ActionSet | |
| 115 | − expectedResult []Action | |
| 116 | − }{ | |
| 117 | − {NewActionSet(PutObjectAction), []Action{PutObjectAction}}, | |
| 118 | − {NewActionSet(), []Action{}}, | |
| 119 | − } | |
| 120 | − | |
| 121 | − for i, testCase := range testCases { | |
| 122 | − result := testCase.actionSet.ToSlice() | |
| 123 | − | |
| 124 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 125 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 126 | − } | |
| 127 | − } | |
| 128 | −} | |
| 129 | − | |
| 130 | −func TestActionSetUnmarshalJSON(t *testing.T) { | |
| 131 | − testCases := []struct { | |
| 132 | − data []byte | |
| 133 | − expectedResult ActionSet | |
| 134 | − expectUnmarshalErr bool | |
| 135 | − expectValidateErr bool | |
| 136 | − }{ | |
| 137 | − {[]byte(`"s3:PutObject"`), NewActionSet(PutObjectAction), false, false}, | |
| 138 | − {[]byte(`["s3:PutObject"]`), NewActionSet(PutObjectAction), false, false}, | |
| 139 | − {[]byte(`["s3:PutObject", "s3:GetObject"]`), NewActionSet(PutObjectAction, GetObjectAction), false, false}, | |
| 140 | − {[]byte(`["s3:PutObject", "s3:GetObject", "s3:PutObject"]`), NewActionSet(PutObjectAction, GetObjectAction), false, false}, | |
| 141 | − {[]byte(`[]`), NewActionSet(), true, false}, // Empty array. | |
| 142 | − {[]byte(`"foo"`), nil, false, true}, // Invalid action. | |
| 143 | − {[]byte(`["s3:PutObject", "foo"]`), nil, false, true}, // Invalid action. | |
| 144 | − } | |
| 145 | − | |
| 146 | − for i, testCase := range testCases { | |
| 147 | − result := make(ActionSet) | |
| 148 | − err := json.Unmarshal(testCase.data, &result) | |
| 149 | − expectErr := (err != nil) | |
| 150 | − | |
| 151 | − if expectErr != testCase.expectUnmarshalErr { | |
| 152 | − t.Fatalf("case %v: error during unmarshal: expected: %v, got: %v\n", i+1, testCase.expectUnmarshalErr, expectErr) | |
| 153 | − } | |
| 154 | − | |
| 155 | − err = result.Validate() | |
| 156 | − expectErr = (err != nil) | |
| 157 | − if expectErr != testCase.expectValidateErr { | |
| 158 | − t.Fatalf("case %v: error during validation: expected: %v, got: %v\n", i+1, testCase.expectValidateErr, expectErr) | |
| 159 | − } | |
| 160 | − | |
| 161 | − if !testCase.expectUnmarshalErr && !testCase.expectValidateErr { | |
| 162 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 163 | − t.Fatalf("case %v: result: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 164 | − } | |
| 165 | − } | |
| 166 | − } | |
| 167 | −} | |
| @@ -1,246 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 22 | −) | |
| 23 | − | |
| 24 | −// AdminAction - admin policy action. | |
| 25 | −type AdminAction string | |
| 26 | − | |
| 27 | −const ( | |
| 28 | − // HealAdminAction - allows heal command | |
| 29 | − HealAdminAction = "admin:Heal" | |
| 30 | − | |
| 31 | − // Service Actions | |
| 32 | − | |
| 33 | − // StorageInfoAdminAction - allow listing server info | |
| 34 | − StorageInfoAdminAction = "admin:StorageInfo" | |
| 35 | − // PrometheusAdminAction - prometheus info action | |
| 36 | − PrometheusAdminAction = "admin:Prometheus" | |
| 37 | − // DataUsageInfoAdminAction - allow listing data usage info | |
| 38 | − DataUsageInfoAdminAction = "admin:DataUsageInfo" | |
| 39 | − // ForceUnlockAdminAction - allow force unlocking locks | |
| 40 | − ForceUnlockAdminAction = "admin:ForceUnlock" | |
| 41 | − // TopLocksAdminAction - allow listing top locks | |
| 42 | − TopLocksAdminAction = "admin:TopLocksInfo" | |
| 43 | − // ProfilingAdminAction - allow profiling | |
| 44 | − ProfilingAdminAction = "admin:Profiling" | |
| 45 | − // TraceAdminAction - allow listing server trace | |
| 46 | − TraceAdminAction = "admin:ServerTrace" | |
| 47 | − // ConsoleLogAdminAction - allow listing console logs on terminal | |
| 48 | − ConsoleLogAdminAction = "admin:ConsoleLog" | |
| 49 | − // KMSCreateKeyAdminAction - allow creating a new KMS master key | |
| 50 | − KMSCreateKeyAdminAction = "admin:KMSCreateKey" | |
| 51 | − // KMSKeyStatusAdminAction - allow getting KMS key status | |
| 52 | − KMSKeyStatusAdminAction = "admin:KMSKeyStatus" | |
| 53 | − // ServerInfoAdminAction - allow listing server info | |
| 54 | − ServerInfoAdminAction = "admin:ServerInfo" | |
| 55 | − // HealthInfoAdminAction - allow obtaining cluster health information | |
| 56 | − HealthInfoAdminAction = "admin:OBDInfo" | |
| 57 | − // BandwidthMonitorAction - allow monitoring bandwidth usage | |
| 58 | − BandwidthMonitorAction = "admin:BandwidthMonitor" | |
| 59 | − | |
| 60 | − // ServerUpdateAdminAction - allow MinIO binary update | |
| 61 | − ServerUpdateAdminAction = "admin:ServerUpdate" | |
| 62 | − // ServiceRestartAdminAction - allow restart of MinIO service. | |
| 63 | − ServiceRestartAdminAction = "admin:ServiceRestart" | |
| 64 | − // ServiceStopAdminAction - allow stopping MinIO service. | |
| 65 | − ServiceStopAdminAction = "admin:ServiceStop" | |
| 66 | − | |
| 67 | − // ConfigUpdateAdminAction - allow MinIO config management | |
| 68 | − ConfigUpdateAdminAction = "admin:ConfigUpdate" | |
| 69 | − | |
| 70 | − // CreateUserAdminAction - allow creating MinIO user | |
| 71 | − CreateUserAdminAction = "admin:CreateUser" | |
| 72 | − // DeleteUserAdminAction - allow deleting MinIO user | |
| 73 | − DeleteUserAdminAction = "admin:DeleteUser" | |
| 74 | − // ListUsersAdminAction - allow list users permission | |
| 75 | − ListUsersAdminAction = "admin:ListUsers" | |
| 76 | − // EnableUserAdminAction - allow enable user permission | |
| 77 | − EnableUserAdminAction = "admin:EnableUser" | |
| 78 | − // DisableUserAdminAction - allow disable user permission | |
| 79 | − DisableUserAdminAction = "admin:DisableUser" | |
| 80 | − // GetUserAdminAction - allows GET permission on user info | |
| 81 | − GetUserAdminAction = "admin:GetUser" | |
| 82 | − | |
| 83 | − // Service account Actions | |
| 84 | − | |
| 85 | − // CreateServiceAccountAdminAction - allow create a service account for a user | |
| 86 | − CreateServiceAccountAdminAction = "admin:CreateServiceAccount" | |
| 87 | − // UpdateServiceAccountAdminAction - allow updating a service account | |
| 88 | − UpdateServiceAccountAdminAction = "admin:UpdateServiceAccount" | |
| 89 | − // RemoveServiceAccountAdminAction - allow removing a service account | |
| 90 | − RemoveServiceAccountAdminAction = "admin:RemoveServiceAccount" | |
| 91 | − // ListServiceAccountsAdminAction - allow listing service accounts | |
| 92 | − ListServiceAccountsAdminAction = "admin:ListServiceAccounts" | |
| 93 | − | |
| 94 | − // Group Actions | |
| 95 | − | |
| 96 | − // AddUserToGroupAdminAction - allow adding user to group permission | |
| 97 | − AddUserToGroupAdminAction = "admin:AddUserToGroup" | |
| 98 | − // RemoveUserFromGroupAdminAction - allow removing user to group permission | |
| 99 | − RemoveUserFromGroupAdminAction = "admin:RemoveUserFromGroup" | |
| 100 | − // GetGroupAdminAction - allow getting group info | |
| 101 | − GetGroupAdminAction = "admin:GetGroup" | |
| 102 | − // ListGroupsAdminAction - allow list groups permission | |
| 103 | − ListGroupsAdminAction = "admin:ListGroups" | |
| 104 | − // EnableGroupAdminAction - allow enable group permission | |
| 105 | − EnableGroupAdminAction = "admin:EnableGroup" | |
| 106 | − // DisableGroupAdminAction - allow disable group permission | |
| 107 | − DisableGroupAdminAction = "admin:DisableGroup" | |
| 108 | − | |
| 109 | − // Policy Actions | |
| 110 | − | |
| 111 | − // CreatePolicyAdminAction - allow create policy permission | |
| 112 | − CreatePolicyAdminAction = "admin:CreatePolicy" | |
| 113 | − // DeletePolicyAdminAction - allow delete policy permission | |
| 114 | − DeletePolicyAdminAction = "admin:DeletePolicy" | |
| 115 | − // GetPolicyAdminAction - allow get policy permission | |
| 116 | − GetPolicyAdminAction = "admin:GetPolicy" | |
| 117 | − // AttachPolicyAdminAction - allows attaching a policy to a user/group | |
| 118 | − AttachPolicyAdminAction = "admin:AttachUserOrGroupPolicy" | |
| 119 | − // ListUserPoliciesAdminAction - allows listing user policies | |
| 120 | − ListUserPoliciesAdminAction = "admin:ListUserPolicies" | |
| 121 | − | |
| 122 | − // Bucket quota Actions | |
| 123 | − | |
| 124 | − // SetBucketQuotaAdminAction - allow setting bucket quota | |
| 125 | − SetBucketQuotaAdminAction = "admin:SetBucketQuota" | |
| 126 | − // GetBucketQuotaAdminAction - allow getting bucket quota | |
| 127 | − GetBucketQuotaAdminAction = "admin:GetBucketQuota" | |
| 128 | − | |
| 129 | − // Bucket Target admin Actions | |
| 130 | − | |
| 131 | − // SetBucketTargetAction - allow setting bucket target | |
| 132 | − SetBucketTargetAction = "admin:SetBucketTarget" | |
| 133 | − // GetBucketTargetAction - allow getting bucket targets | |
| 134 | − GetBucketTargetAction = "admin:GetBucketTarget" | |
| 135 | − | |
| 136 | − // Remote Tier admin Actions | |
| 137 | − | |
| 138 | − // SetTierAction - allow adding/editing a remote tier | |
| 139 | − SetTierAction = "admin:SetTier" | |
| 140 | − // ListTierAction - allow listing remote tiers | |
| 141 | − ListTierAction = "admin:ListTier" | |
| 142 | − | |
| 143 | − // AllAdminActions - provides all admin permissions | |
| 144 | − AllAdminActions = "admin:*" | |
| 145 | −) | |
| 146 | − | |
| 147 | −// List of all supported admin actions. | |
| 148 | −var supportedAdminActions = map[AdminAction]struct{}{ | |
| 149 | − HealAdminAction: {}, | |
| 150 | − StorageInfoAdminAction: {}, | |
| 151 | − DataUsageInfoAdminAction: {}, | |
| 152 | − TopLocksAdminAction: {}, | |
| 153 | − ProfilingAdminAction: {}, | |
| 154 | − PrometheusAdminAction: {}, | |
| 155 | − TraceAdminAction: {}, | |
| 156 | − ConsoleLogAdminAction: {}, | |
| 157 | − KMSKeyStatusAdminAction: {}, | |
| 158 | − ServerInfoAdminAction: {}, | |
| 159 | − HealthInfoAdminAction: {}, | |
| 160 | − BandwidthMonitorAction: {}, | |
| 161 | − ServerUpdateAdminAction: {}, | |
| 162 | − ServiceRestartAdminAction: {}, | |
| 163 | − ServiceStopAdminAction: {}, | |
| 164 | − ConfigUpdateAdminAction: {}, | |
| 165 | − CreateUserAdminAction: {}, | |
| 166 | − DeleteUserAdminAction: {}, | |
| 167 | − ListUsersAdminAction: {}, | |
| 168 | − EnableUserAdminAction: {}, | |
| 169 | − DisableUserAdminAction: {}, | |
| 170 | − GetUserAdminAction: {}, | |
| 171 | − AddUserToGroupAdminAction: {}, | |
| 172 | − RemoveUserFromGroupAdminAction: {}, | |
| 173 | − GetGroupAdminAction: {}, | |
| 174 | − ListGroupsAdminAction: {}, | |
| 175 | − EnableGroupAdminAction: {}, | |
| 176 | − DisableGroupAdminAction: {}, | |
| 177 | − CreateServiceAccountAdminAction: {}, | |
| 178 | − UpdateServiceAccountAdminAction: {}, | |
| 179 | − RemoveServiceAccountAdminAction: {}, | |
| 180 | − ListServiceAccountsAdminAction: {}, | |
| 181 | − CreatePolicyAdminAction: {}, | |
| 182 | − DeletePolicyAdminAction: {}, | |
| 183 | − GetPolicyAdminAction: {}, | |
| 184 | − AttachPolicyAdminAction: {}, | |
| 185 | − ListUserPoliciesAdminAction: {}, | |
| 186 | − SetBucketQuotaAdminAction: {}, | |
| 187 | − GetBucketQuotaAdminAction: {}, | |
| 188 | − SetBucketTargetAction: {}, | |
| 189 | − GetBucketTargetAction: {}, | |
| 190 | − SetTierAction: {}, | |
| 191 | − ListTierAction: {}, | |
| 192 | − AllAdminActions: {}, | |
| 193 | −} | |
| 194 | − | |
| 195 | −// IsValid - checks if action is valid or not. | |
| 196 | −func (action AdminAction) IsValid() bool { | |
| 197 | − _, ok := supportedAdminActions[action] | |
| 198 | − return ok | |
| 199 | −} | |
| 200 | − | |
| 201 | −// adminActionConditionKeyMap - holds mapping of supported condition key for an action. | |
| 202 | −var adminActionConditionKeyMap = map[Action]condition.KeySet{ | |
| 203 | − AllAdminActions: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 204 | − HealAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 205 | − StorageInfoAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 206 | − ServerInfoAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 207 | − DataUsageInfoAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 208 | − HealthInfoAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 209 | − BandwidthMonitorAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 210 | − TopLocksAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 211 | − ProfilingAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 212 | − TraceAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 213 | − ConsoleLogAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 214 | − KMSKeyStatusAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 215 | − ServerUpdateAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 216 | − ServiceRestartAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 217 | − ServiceStopAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 218 | − ConfigUpdateAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 219 | − CreateUserAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 220 | − DeleteUserAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 221 | − ListUsersAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 222 | − EnableUserAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 223 | − DisableUserAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 224 | − GetUserAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 225 | − AddUserToGroupAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 226 | − RemoveUserFromGroupAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 227 | − ListGroupsAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 228 | − EnableGroupAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 229 | − DisableGroupAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 230 | − CreateServiceAccountAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 231 | − UpdateServiceAccountAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 232 | − RemoveServiceAccountAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 233 | − ListServiceAccountsAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 234 | − | |
| 235 | − CreatePolicyAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 236 | − DeletePolicyAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 237 | − GetPolicyAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 238 | − AttachPolicyAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 239 | − ListUserPoliciesAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 240 | − SetBucketQuotaAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 241 | − GetBucketQuotaAdminAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 242 | − SetBucketTargetAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 243 | − GetBucketTargetAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 244 | − SetTierAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 245 | − ListTierAction: condition.NewKeySet(condition.AllSupportedAdminKeys...), | |
| 246 | −} | |
| @@ -1,107 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 22 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 23 | −) | |
| 24 | − | |
| 25 | −// Policy claim constants | |
| 26 | −const ( | |
| 27 | − PolicyName = "policy" | |
| 28 | − SessionPolicyName = "sessionPolicy" | |
| 29 | −) | |
| 30 | − | |
| 31 | −// ReadWrite - provides full access to all buckets and all objects | |
| 32 | −var ReadWrite = Policy{ | |
| 33 | − Version: DefaultVersion, | |
| 34 | − Statements: []Statement{ | |
| 35 | − { | |
| 36 | − SID: policy.ID(""), | |
| 37 | − Effect: policy.Allow, | |
| 38 | − Actions: NewActionSet(AllActions), | |
| 39 | − Resources: NewResourceSet(NewResource("*", "")), | |
| 40 | − }, | |
| 41 | − }, | |
| 42 | −} | |
| 43 | − | |
| 44 | −// ReadOnly - read only. | |
| 45 | −var ReadOnly = Policy{ | |
| 46 | − Version: DefaultVersion, | |
| 47 | − Statements: []Statement{ | |
| 48 | − { | |
| 49 | − SID: policy.ID(""), | |
| 50 | − Effect: policy.Allow, | |
| 51 | − Actions: NewActionSet(GetBucketLocationAction, GetObjectAction), | |
| 52 | − Resources: NewResourceSet(NewResource("*", "")), | |
| 53 | − }, | |
| 54 | − }, | |
| 55 | −} | |
| 56 | − | |
| 57 | −// WriteOnly - provides write access. | |
| 58 | −var WriteOnly = Policy{ | |
| 59 | − Version: DefaultVersion, | |
| 60 | − Statements: []Statement{ | |
| 61 | − { | |
| 62 | − SID: policy.ID(""), | |
| 63 | − Effect: policy.Allow, | |
| 64 | − Actions: NewActionSet(PutObjectAction), | |
| 65 | − Resources: NewResourceSet(NewResource("*", "")), | |
| 66 | − }, | |
| 67 | − }, | |
| 68 | −} | |
| 69 | − | |
| 70 | −// AdminDiagnostics - provides admin diagnostics access. | |
| 71 | −var AdminDiagnostics = Policy{ | |
| 72 | − Version: DefaultVersion, | |
| 73 | − Statements: []Statement{ | |
| 74 | − { | |
| 75 | − SID: policy.ID(""), | |
| 76 | − Effect: policy.Allow, | |
| 77 | − Actions: NewActionSet(ProfilingAdminAction, | |
| 78 | − TraceAdminAction, ConsoleLogAdminAction, | |
| 79 | − ServerInfoAdminAction, TopLocksAdminAction, | |
| 80 | − HealthInfoAdminAction, BandwidthMonitorAction, | |
| 81 | − PrometheusAdminAction, | |
| 82 | − ), | |
| 83 | − Resources: NewResourceSet(NewResource("*", "")), | |
| 84 | − }, | |
| 85 | − }, | |
| 86 | −} | |
| 87 | − | |
| 88 | −// Admin - provides admin all-access canned policy | |
| 89 | −var Admin = Policy{ | |
| 90 | − Version: DefaultVersion, | |
| 91 | − Statements: []Statement{ | |
| 92 | − { | |
| 93 | − SID: policy.ID(""), | |
| 94 | − Effect: policy.Allow, | |
| 95 | − Actions: NewActionSet(AllAdminActions), | |
| 96 | − Resources: NewResourceSet(), | |
| 97 | − Conditions: condition.NewFunctions(), | |
| 98 | − }, | |
| 99 | − { | |
| 100 | − SID: policy.ID(""), | |
| 101 | − Effect: policy.Allow, | |
| 102 | − Actions: NewActionSet(AllActions), | |
| 103 | − Resources: NewResourceSet(NewResource("*", "")), | |
| 104 | − Conditions: condition.NewFunctions(), | |
| 105 | − }, | |
| 106 | − }, | |
| 107 | −} | |
| @@ -1,45 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "fmt" | |
| 22 | −) | |
| 23 | − | |
| 24 | −// Error is the generic type for any error happening during policy | |
| 25 | −// parsing. | |
| 26 | −type Error struct { | |
| 27 | − err error | |
| 28 | −} | |
| 29 | − | |
| 30 | −// Errorf - formats according to a format specifier and returns | |
| 31 | −// the string as a value that satisfies error of type policy.Error | |
| 32 | −func Errorf(format string, a ...interface{}) error { | |
| 33 | − return Error{err: fmt.Errorf(format, a...)} | |
| 34 | −} | |
| 35 | − | |
| 36 | −// Unwrap the internal error. | |
| 37 | −func (e Error) Unwrap() error { return e.err } | |
| 38 | − | |
| 39 | −// Error 'error' compatible method. | |
| 40 | −func (e Error) Error() string { | |
| 41 | − if e.err == nil { | |
| 42 | − return "iam: cause <nil>" | |
| 43 | − } | |
| 44 | − return e.err.Error() | |
| 45 | −} | |
| @@ -1,229 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "io" | |
| 23 | − "strings" | |
| 24 | − | |
| 25 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 26 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 27 | −) | |
| 28 | − | |
| 29 | −// DefaultVersion - default policy version as per AWS S3 specification. | |
| 30 | −const DefaultVersion = "2012-10-17" | |
| 31 | − | |
| 32 | −// Args - arguments to policy to check whether it is allowed | |
| 33 | −type Args struct { | |
| 34 | − AccountName string `json:"account"` | |
| 35 | − Groups []string `json:"groups"` | |
| 36 | − Action Action `json:"action"` | |
| 37 | − BucketName string `json:"bucket"` | |
| 38 | − ConditionValues map[string][]string `json:"conditions"` | |
| 39 | − IsOwner bool `json:"owner"` | |
| 40 | − ObjectName string `json:"object"` | |
| 41 | − Claims map[string]interface{} `json:"claims"` | |
| 42 | − DenyOnly bool `json:"denyOnly"` // only applies deny | |
| 43 | −} | |
| 44 | − | |
| 45 | −// GetPoliciesFromClaims returns the list of policies to be applied for this | |
| 46 | −// incoming request, extracting the information from input JWT claims. | |
| 47 | −func GetPoliciesFromClaims(claims map[string]interface{}, policyClaimName string) (set.StringSet, bool) { | |
| 48 | − s := set.NewStringSet() | |
| 49 | − pname, ok := claims[policyClaimName] | |
| 50 | − if !ok { | |
| 51 | − return s, false | |
| 52 | − } | |
| 53 | − pnames, ok := pname.([]interface{}) | |
| 54 | − if !ok { | |
| 55 | − pnameStr, ok := pname.(string) | |
| 56 | − if ok { | |
| 57 | − for _, pname := range strings.Split(pnameStr, ",") { | |
| 58 | − pname = strings.TrimSpace(pname) | |
| 59 | − if pname == "" { | |
| 60 | − // ignore any empty strings, considerate | |
| 61 | − // towards some user errors. | |
| 62 | − continue | |
| 63 | − } | |
| 64 | − s.Add(pname) | |
| 65 | − } | |
| 66 | − return s, true | |
| 67 | − } | |
| 68 | − return s, false | |
| 69 | − } | |
| 70 | − for _, pname := range pnames { | |
| 71 | − pnameStr, ok := pname.(string) | |
| 72 | − if ok { | |
| 73 | − for _, pnameStr := range strings.Split(pnameStr, ",") { | |
| 74 | − pnameStr = strings.TrimSpace(pnameStr) | |
| 75 | − if pnameStr == "" { | |
| 76 | − // ignore any empty strings, considerate | |
| 77 | − // towards some user errors. | |
| 78 | − continue | |
| 79 | − } | |
| 80 | − s.Add(pnameStr) | |
| 81 | − } | |
| 82 | − } | |
| 83 | − } | |
| 84 | − return s, true | |
| 85 | −} | |
| 86 | − | |
| 87 | −// GetPolicies returns the list of policies to be applied for this | |
| 88 | −// incoming request, extracting the information from JWT claims. | |
| 89 | −func (a Args) GetPolicies(policyClaimName string) (set.StringSet, bool) { | |
| 90 | − return GetPoliciesFromClaims(a.Claims, policyClaimName) | |
| 91 | −} | |
| 92 | − | |
| 93 | −// Policy - iam bucket iamp. | |
| 94 | −type Policy struct { | |
| 95 | − ID policy.ID `json:"ID,omitempty"` | |
| 96 | − Version string | |
| 97 | − Statements []Statement `json:"Statement"` | |
| 98 | −} | |
| 99 | − | |
| 100 | −// MatchResource matches resource with match resource patterns | |
| 101 | −func (iamp Policy) MatchResource(resource string) bool { | |
| 102 | − for _, statement := range iamp.Statements { | |
| 103 | − if statement.Resources.MatchResource(resource) { | |
| 104 | − return true | |
| 105 | − } | |
| 106 | − } | |
| 107 | − return false | |
| 108 | −} | |
| 109 | − | |
| 110 | −// IsAllowed - checks given policy args is allowed to continue the Rest API. | |
| 111 | −func (iamp Policy) IsAllowed(args Args) bool { | |
| 112 | − // Check all deny statements. If any one statement denies, return false. | |
| 113 | − for _, statement := range iamp.Statements { | |
| 114 | − if statement.Effect == policy.Deny { | |
| 115 | − if !statement.IsAllowed(args) { | |
| 116 | − return false | |
| 117 | − } | |
| 118 | − } | |
| 119 | − } | |
| 120 | − | |
| 121 | − // Applied any 'Deny' only policies, if we have | |
| 122 | − // reached here it means that there were no 'Deny' | |
| 123 | − // policies - this function mainly used for | |
| 124 | − // specific scenarios where we only want to validate | |
| 125 | − // 'Deny' only policies. | |
| 126 | − if args.DenyOnly { | |
| 127 | − return true | |
| 128 | − } | |
| 129 | − | |
| 130 | − // For owner, its allowed by default. | |
| 131 | − if args.IsOwner { | |
| 132 | − return true | |
| 133 | − } | |
| 134 | − | |
| 135 | − // Check all allow statements. If any one statement allows, return true. | |
| 136 | − for _, statement := range iamp.Statements { | |
| 137 | − if statement.Effect == policy.Allow { | |
| 138 | − if statement.IsAllowed(args) { | |
| 139 | − return true | |
| 140 | − } | |
| 141 | − } | |
| 142 | − } | |
| 143 | − | |
| 144 | − return false | |
| 145 | −} | |
| 146 | − | |
| 147 | −// IsEmpty - returns whether policy is empty or not. | |
| 148 | −func (iamp Policy) IsEmpty() bool { | |
| 149 | − return len(iamp.Statements) == 0 | |
| 150 | −} | |
| 151 | − | |
| 152 | −// isValid - checks if Policy is valid or not. | |
| 153 | −func (iamp Policy) isValid() error { | |
| 154 | − if iamp.Version != DefaultVersion && iamp.Version != "" { | |
| 155 | − return Errorf("invalid version '%v'", iamp.Version) | |
| 156 | − } | |
| 157 | − | |
| 158 | − for _, statement := range iamp.Statements { | |
| 159 | − if err := statement.isValid(); err != nil { | |
| 160 | − return err | |
| 161 | − } | |
| 162 | − } | |
| 163 | − return nil | |
| 164 | −} | |
| 165 | − | |
| 166 | −// Merge merges two policies documents and drop | |
| 167 | −// duplicate statements if any. | |
| 168 | −func (iamp Policy) Merge(input Policy) Policy { | |
| 169 | − var mergedPolicy Policy | |
| 170 | − if iamp.Version != "" { | |
| 171 | − mergedPolicy.Version = iamp.Version | |
| 172 | − } else { | |
| 173 | − mergedPolicy.Version = input.Version | |
| 174 | − } | |
| 175 | − for _, st := range iamp.Statements { | |
| 176 | − mergedPolicy.Statements = append(mergedPolicy.Statements, st.Clone()) | |
| 177 | − } | |
| 178 | − for _, st := range input.Statements { | |
| 179 | − mergedPolicy.Statements = append(mergedPolicy.Statements, st.Clone()) | |
| 180 | − } | |
| 181 | − mergedPolicy.dropDuplicateStatements() | |
| 182 | − return mergedPolicy | |
| 183 | −} | |
| 184 | − | |
| 185 | −func (iamp *Policy) dropDuplicateStatements() { | |
| 186 | −redo: | |
| 187 | − for i := range iamp.Statements { | |
| 188 | − for j, statement := range iamp.Statements[i+1:] { | |
| 189 | − if !iamp.Statements[i].Equals(statement) { | |
| 190 | − continue | |
| 191 | − } | |
| 192 | − iamp.Statements = append(iamp.Statements[:j], iamp.Statements[j+1:]...) | |
| 193 | − goto redo | |
| 194 | − } | |
| 195 | − } | |
| 196 | −} | |
| 197 | − | |
| 198 | −// UnmarshalJSON - decodes JSON data to Iamp. | |
| 199 | −func (iamp *Policy) UnmarshalJSON(data []byte) error { | |
| 200 | − // subtype to avoid recursive call to UnmarshalJSON() | |
| 201 | − type subPolicy Policy | |
| 202 | − var sp subPolicy | |
| 203 | − if err := json.Unmarshal(data, &sp); err != nil { | |
| 204 | − return err | |
| 205 | − } | |
| 206 | − | |
| 207 | − p := Policy(sp) | |
| 208 | − p.dropDuplicateStatements() | |
| 209 | − *iamp = p | |
| 210 | − return nil | |
| 211 | −} | |
| 212 | − | |
| 213 | −// Validate - validates all statements are for given bucket or not. | |
| 214 | −func (iamp Policy) Validate() error { | |
| 215 | − return iamp.isValid() | |
| 216 | −} | |
| 217 | − | |
| 218 | −// ParseConfig - parses data in given reader to Iamp. | |
| 219 | −func ParseConfig(reader io.Reader) (*Policy, error) { | |
| 220 | − var iamp Policy | |
| 221 | − | |
| 222 | − decoder := json.NewDecoder(reader) | |
| 223 | − decoder.DisallowUnknownFields() | |
| 224 | − if err := decoder.Decode(&iamp); err != nil { | |
| 225 | − return nil, Errorf("%w", err) | |
| 226 | − } | |
| 227 | − | |
| 228 | − return &iamp, iamp.Validate() | |
| 229 | −} | |
| @@ -1,1173 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "net" | |
| 23 | − "reflect" | |
| 24 | − "strings" | |
| 25 | − "testing" | |
| 26 | − "time" | |
| 27 | − | |
| 28 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 29 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 30 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 31 | −) | |
| 32 | − | |
| 33 | −func TestGetPoliciesFromClaims(t *testing.T) { | |
| 34 | − attributesArray := `{ | |
| 35 | − "exp": 1594690452, | |
| 36 | − "iat": 1594689552, | |
| 37 | − "auth_time": 1594689552, | |
| 38 | − "jti": "18ed05c9-2c69-45d5-a33f-8c94aca99ad5", | |
| 39 | − "iss": "http://localhost:8080/auth/realms/minio", | |
| 40 | − "aud": "account", | |
| 41 | − "sub": "7e5e2f30-1c97-4616-8623-2eae14dee9b1", | |
| 42 | − "typ": "ID", | |
| 43 | − "azp": "account", | |
| 44 | − "nonce": "66ZoLzwJbjdkiedI", | |
| 45 | − "session_state": "3df7b526-5310-4038-9f35-50ecd295a31d", | |
| 46 | − "acr": "1", | |
| 47 | − "upn": "harsha", | |
| 48 | − "address": {}, | |
| 49 | − "email_verified": false, | |
| 50 | − "groups": [ | |
| 51 | − "offline_access" | |
| 52 | − ], | |
| 53 | − "preferred_username": "harsha", | |
| 54 | − "policy": [ | |
| 55 | − "readwrite", | |
| 56 | − "readwrite,readonly", | |
| 57 | − " readonly", | |
| 58 | − "" | |
| 59 | − ]}` | |
| 60 | − var m = make(map[string]interface{}) | |
| 61 | − if err := json.Unmarshal([]byte(attributesArray), &m); err != nil { | |
| 62 | − t.Fatal(err) | |
| 63 | − } | |
| 64 | − var expectedSet = set.CreateStringSet("readwrite", "readonly") | |
| 65 | − gotSet, ok := GetPoliciesFromClaims(m, "policy") | |
| 66 | − if !ok { | |
| 67 | − t.Fatal("no policy claim was found") | |
| 68 | − } | |
| 69 | − if gotSet.IsEmpty() { | |
| 70 | − t.Fatal("no policies were found in policy claim") | |
| 71 | − } | |
| 72 | − if !gotSet.Equals(expectedSet) { | |
| 73 | − t.Fatalf("Expected %v got %v", expectedSet, gotSet) | |
| 74 | − } | |
| 75 | −} | |
| 76 | − | |
| 77 | −func TestPolicyIsAllowed(t *testing.T) { | |
| 78 | − case1Policy := Policy{ | |
| 79 | − Version: DefaultVersion, | |
| 80 | − Statements: []Statement{ | |
| 81 | − NewStatement( | |
| 82 | − policy.Allow, | |
| 83 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 84 | − NewResourceSet(NewResource("*", "")), | |
| 85 | − condition.NewFunctions(), | |
| 86 | − )}, | |
| 87 | − } | |
| 88 | − | |
| 89 | − case2Policy := Policy{ | |
| 90 | − Version: DefaultVersion, | |
| 91 | − Statements: []Statement{ | |
| 92 | − NewStatement( | |
| 93 | − policy.Allow, | |
| 94 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 95 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 96 | − condition.NewFunctions(), | |
| 97 | − )}, | |
| 98 | − } | |
| 99 | − | |
| 100 | − _, IPNet, err := net.ParseCIDR("192.168.1.0/24") | |
| 101 | − if err != nil { | |
| 102 | − t.Fatalf("unexpected error. %v\n", err) | |
| 103 | − } | |
| 104 | − func1, err := condition.NewIPAddressFunc( | |
| 105 | − condition.AWSSourceIP, | |
| 106 | − IPNet, | |
| 107 | − ) | |
| 108 | − if err != nil { | |
| 109 | − t.Fatalf("unexpected error. %v\n", err) | |
| 110 | − } | |
| 111 | − | |
| 112 | − case3Policy := Policy{ | |
| 113 | − Version: DefaultVersion, | |
| 114 | − Statements: []Statement{ | |
| 115 | − NewStatement( | |
| 116 | − policy.Allow, | |
| 117 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 118 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 119 | − condition.NewFunctions(func1), | |
| 120 | − )}, | |
| 121 | − } | |
| 122 | − | |
| 123 | − case4Policy := Policy{ | |
| 124 | − Version: DefaultVersion, | |
| 125 | − Statements: []Statement{ | |
| 126 | − NewStatement( | |
| 127 | − policy.Deny, | |
| 128 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 129 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 130 | − condition.NewFunctions(func1), | |
| 131 | − )}, | |
| 132 | − } | |
| 133 | − | |
| 134 | − anonGetBucketLocationArgs := Args{ | |
| 135 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 136 | − Action: GetBucketLocationAction, | |
| 137 | − BucketName: "mybucket", | |
| 138 | − ConditionValues: map[string][]string{}, | |
| 139 | − } | |
| 140 | − | |
| 141 | − anonPutObjectActionArgs := Args{ | |
| 142 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 143 | − Action: PutObjectAction, | |
| 144 | − BucketName: "mybucket", | |
| 145 | − ConditionValues: map[string][]string{ | |
| 146 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 147 | − "SourceIp": {"192.168.1.10"}, | |
| 148 | − }, | |
| 149 | − ObjectName: "myobject", | |
| 150 | − } | |
| 151 | − | |
| 152 | − anonGetObjectActionArgs := Args{ | |
| 153 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 154 | − Action: GetObjectAction, | |
| 155 | − BucketName: "mybucket", | |
| 156 | − ConditionValues: map[string][]string{}, | |
| 157 | − ObjectName: "myobject", | |
| 158 | − } | |
| 159 | − | |
| 160 | − getBucketLocationArgs := Args{ | |
| 161 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 162 | − Action: GetBucketLocationAction, | |
| 163 | − BucketName: "mybucket", | |
| 164 | − ConditionValues: map[string][]string{}, | |
| 165 | − } | |
| 166 | − | |
| 167 | − putObjectActionArgs := Args{ | |
| 168 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 169 | − Action: PutObjectAction, | |
| 170 | − BucketName: "mybucket", | |
| 171 | − ConditionValues: map[string][]string{ | |
| 172 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 173 | − "SourceIp": {"192.168.1.10"}, | |
| 174 | − }, | |
| 175 | − ObjectName: "myobject", | |
| 176 | − } | |
| 177 | − | |
| 178 | − getObjectActionArgs := Args{ | |
| 179 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 180 | − Action: GetObjectAction, | |
| 181 | − BucketName: "mybucket", | |
| 182 | − ConditionValues: map[string][]string{}, | |
| 183 | − ObjectName: "myobject", | |
| 184 | − } | |
| 185 | − | |
| 186 | − testCases := []struct { | |
| 187 | − policy Policy | |
| 188 | − args Args | |
| 189 | − expectedResult bool | |
| 190 | − }{ | |
| 191 | − {case1Policy, anonGetBucketLocationArgs, true}, | |
| 192 | − {case1Policy, anonPutObjectActionArgs, true}, | |
| 193 | − {case1Policy, anonGetObjectActionArgs, false}, | |
| 194 | − {case1Policy, getBucketLocationArgs, true}, | |
| 195 | − {case1Policy, putObjectActionArgs, true}, | |
| 196 | − {case1Policy, getObjectActionArgs, false}, | |
| 197 | − | |
| 198 | − {case2Policy, anonGetBucketLocationArgs, false}, | |
| 199 | − {case2Policy, anonPutObjectActionArgs, true}, | |
| 200 | − {case2Policy, anonGetObjectActionArgs, true}, | |
| 201 | − {case2Policy, getBucketLocationArgs, false}, | |
| 202 | − {case2Policy, putObjectActionArgs, true}, | |
| 203 | − {case2Policy, getObjectActionArgs, true}, | |
| 204 | − | |
| 205 | − {case3Policy, anonGetBucketLocationArgs, false}, | |
| 206 | − {case3Policy, anonPutObjectActionArgs, true}, | |
| 207 | − {case3Policy, anonGetObjectActionArgs, false}, | |
| 208 | − {case3Policy, getBucketLocationArgs, false}, | |
| 209 | − {case3Policy, putObjectActionArgs, true}, | |
| 210 | − {case3Policy, getObjectActionArgs, false}, | |
| 211 | − | |
| 212 | − {case4Policy, anonGetBucketLocationArgs, false}, | |
| 213 | − {case4Policy, anonPutObjectActionArgs, false}, | |
| 214 | − {case4Policy, anonGetObjectActionArgs, false}, | |
| 215 | − {case4Policy, getBucketLocationArgs, false}, | |
| 216 | − {case4Policy, putObjectActionArgs, false}, | |
| 217 | − {case4Policy, getObjectActionArgs, false}, | |
| 218 | − } | |
| 219 | − | |
| 220 | − for i, testCase := range testCases { | |
| 221 | − result := testCase.policy.IsAllowed(testCase.args) | |
| 222 | − | |
| 223 | − if result != testCase.expectedResult { | |
| 224 | − t.Errorf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 225 | − } | |
| 226 | − } | |
| 227 | −} | |
| 228 | − | |
| 229 | −func TestPolicyIsEmpty(t *testing.T) { | |
| 230 | − case1Policy := Policy{ | |
| 231 | − Version: DefaultVersion, | |
| 232 | − Statements: []Statement{ | |
| 233 | − NewStatement( | |
| 234 | − policy.Allow, | |
| 235 | − NewActionSet(PutObjectAction), | |
| 236 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 237 | − condition.NewFunctions(), | |
| 238 | − ), | |
| 239 | − }, | |
| 240 | − } | |
| 241 | − | |
| 242 | − case2Policy := Policy{ | |
| 243 | − ID: "MyPolicyForMyBucket", | |
| 244 | − Version: DefaultVersion, | |
| 245 | − } | |
| 246 | − | |
| 247 | − testCases := []struct { | |
| 248 | − policy Policy | |
| 249 | − expectedResult bool | |
| 250 | − }{ | |
| 251 | − {case1Policy, false}, | |
| 252 | − {case2Policy, true}, | |
| 253 | − } | |
| 254 | − | |
| 255 | − for i, testCase := range testCases { | |
| 256 | − result := testCase.policy.IsEmpty() | |
| 257 | − | |
| 258 | − if result != testCase.expectedResult { | |
| 259 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 260 | − } | |
| 261 | − } | |
| 262 | −} | |
| 263 | − | |
| 264 | −func TestPolicyIsValid(t *testing.T) { | |
| 265 | − case1Policy := Policy{ | |
| 266 | − Version: DefaultVersion, | |
| 267 | − Statements: []Statement{ | |
| 268 | − NewStatement( | |
| 269 | − policy.Allow, | |
| 270 | − NewActionSet(PutObjectAction), | |
| 271 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 272 | − condition.NewFunctions(), | |
| 273 | − ), | |
| 274 | − }, | |
| 275 | − } | |
| 276 | − | |
| 277 | − case2Policy := Policy{ | |
| 278 | − Version: DefaultVersion, | |
| 279 | − Statements: []Statement{ | |
| 280 | − NewStatement( | |
| 281 | − policy.Allow, | |
| 282 | − NewActionSet(PutObjectAction), | |
| 283 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 284 | − condition.NewFunctions(), | |
| 285 | − ), | |
| 286 | − NewStatement( | |
| 287 | − policy.Deny, | |
| 288 | − NewActionSet(GetObjectAction), | |
| 289 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 290 | − condition.NewFunctions(), | |
| 291 | − ), | |
| 292 | − }, | |
| 293 | − } | |
| 294 | − | |
| 295 | − case3Policy := Policy{ | |
| 296 | − Version: DefaultVersion, | |
| 297 | − Statements: []Statement{ | |
| 298 | − NewStatement( | |
| 299 | − policy.Allow, | |
| 300 | − NewActionSet(PutObjectAction), | |
| 301 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 302 | − condition.NewFunctions(), | |
| 303 | − ), | |
| 304 | − NewStatement( | |
| 305 | − policy.Deny, | |
| 306 | − NewActionSet(PutObjectAction), | |
| 307 | − NewResourceSet(NewResource("mybucket", "/yourobject*")), | |
| 308 | − condition.NewFunctions(), | |
| 309 | − ), | |
| 310 | − }, | |
| 311 | − } | |
| 312 | − | |
| 313 | − func1, err := condition.NewNullFunc( | |
| 314 | − condition.S3XAmzCopySource, | |
| 315 | − true, | |
| 316 | − ) | |
| 317 | − if err != nil { | |
| 318 | − t.Fatalf("unexpected error. %v\n", err) | |
| 319 | − } | |
| 320 | − func2, err := condition.NewNullFunc( | |
| 321 | − condition.S3XAmzServerSideEncryption, | |
| 322 | − false, | |
| 323 | − ) | |
| 324 | − if err != nil { | |
| 325 | − t.Fatalf("unexpected error. %v\n", err) | |
| 326 | − } | |
| 327 | − | |
| 328 | − case4Policy := Policy{ | |
| 329 | − Version: DefaultVersion, | |
| 330 | − Statements: []Statement{ | |
| 331 | − NewStatement( | |
| 332 | − policy.Allow, | |
| 333 | − NewActionSet(PutObjectAction), | |
| 334 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 335 | − condition.NewFunctions(func1), | |
| 336 | − ), | |
| 337 | − NewStatement( | |
| 338 | − policy.Deny, | |
| 339 | − NewActionSet(PutObjectAction), | |
| 340 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 341 | − condition.NewFunctions(func2), | |
| 342 | − ), | |
| 343 | − }, | |
| 344 | − } | |
| 345 | − | |
| 346 | − case5Policy := Policy{ | |
| 347 | − Version: "17-10-2012", | |
| 348 | − Statements: []Statement{ | |
| 349 | − NewStatement( | |
| 350 | − policy.Allow, | |
| 351 | − NewActionSet(PutObjectAction), | |
| 352 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 353 | − condition.NewFunctions(), | |
| 354 | − ), | |
| 355 | − }, | |
| 356 | − } | |
| 357 | − | |
| 358 | − case6Policy := Policy{ | |
| 359 | − ID: "MyPolicyForMyBucket1", | |
| 360 | − Version: DefaultVersion, | |
| 361 | − Statements: []Statement{ | |
| 362 | − NewStatement( | |
| 363 | − policy.Allow, | |
| 364 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 365 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 366 | − condition.NewFunctions(func1, func2), | |
| 367 | − ), | |
| 368 | − }, | |
| 369 | − } | |
| 370 | − | |
| 371 | − case7Policy := Policy{ | |
| 372 | − Version: DefaultVersion, | |
| 373 | − Statements: []Statement{ | |
| 374 | − NewStatement( | |
| 375 | − policy.Allow, | |
| 376 | − NewActionSet(PutObjectAction), | |
| 377 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 378 | − condition.NewFunctions(), | |
| 379 | − ), | |
| 380 | − NewStatement( | |
| 381 | − policy.Deny, | |
| 382 | − NewActionSet(PutObjectAction), | |
| 383 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 384 | − condition.NewFunctions(), | |
| 385 | − ), | |
| 386 | − }, | |
| 387 | − } | |
| 388 | − | |
| 389 | − case8Policy := Policy{ | |
| 390 | − Version: DefaultVersion, | |
| 391 | − Statements: []Statement{ | |
| 392 | − NewStatement( | |
| 393 | − policy.Allow, | |
| 394 | − NewActionSet(PutObjectAction), | |
| 395 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 396 | − condition.NewFunctions(), | |
| 397 | − ), | |
| 398 | − NewStatement( | |
| 399 | − policy.Allow, | |
| 400 | − NewActionSet(PutObjectAction), | |
| 401 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 402 | − condition.NewFunctions(), | |
| 403 | − ), | |
| 404 | − }, | |
| 405 | − } | |
| 406 | − | |
| 407 | − testCases := []struct { | |
| 408 | − policy Policy | |
| 409 | − expectErr bool | |
| 410 | − }{ | |
| 411 | − {case1Policy, false}, | |
| 412 | − // allowed duplicate principal. | |
| 413 | − {case2Policy, false}, | |
| 414 | − // allowed duplicate principal and action. | |
| 415 | − {case3Policy, false}, | |
| 416 | − // allowed duplicate principal, action and resource. | |
| 417 | − {case4Policy, false}, | |
| 418 | − // Invalid version error. | |
| 419 | − {case5Policy, true}, | |
| 420 | − // Invalid statement error. | |
| 421 | − {case6Policy, true}, | |
| 422 | − // Duplicate statement different Effects. | |
| 423 | − {case7Policy, false}, | |
| 424 | − // Duplicate statement same Effects, duplicate effect will be removed. | |
| 425 | − {case8Policy, false}, | |
| 426 | − } | |
| 427 | − | |
| 428 | − for i, testCase := range testCases { | |
| 429 | − err := testCase.policy.isValid() | |
| 430 | − expectErr := (err != nil) | |
| 431 | − | |
| 432 | − if expectErr != testCase.expectErr { | |
| 433 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 434 | − } | |
| 435 | − } | |
| 436 | −} | |
| 437 | − | |
| 438 | −// Parse config with location constraints | |
| 439 | −func TestPolicyParseConfig(t *testing.T) { | |
| 440 | − policy1LocationConstraint := `{ | |
| 441 | − "Version":"2012-10-17", | |
| 442 | − "Statement":[ | |
| 443 | − { | |
| 444 | − "Sid":"statement1", | |
| 445 | − "Effect":"Allow", | |
| 446 | − "Action": "s3:CreateBucket", | |
| 447 | − "Resource": "arn:aws:s3:::*", | |
| 448 | − "Condition": { | |
| 449 | − "StringLike": { | |
| 450 | − "s3:LocationConstraint": "us-east-1" | |
| 451 | − } | |
| 452 | − } | |
| 453 | − }, | |
| 454 | − { | |
| 455 | − "Sid":"statement2", | |
| 456 | − "Effect":"Deny", | |
| 457 | − "Action": "s3:CreateBucket", | |
| 458 | − "Resource": "arn:aws:s3:::*", | |
| 459 | − "Condition": { | |
| 460 | − "StringNotLike": { | |
| 461 | − "s3:LocationConstraint": "us-east-1" | |
| 462 | − } | |
| 463 | − } | |
| 464 | − } | |
| 465 | − ] | |
| 466 | −}` | |
| 467 | − policy2Condition := `{ | |
| 468 | − "Version": "2012-10-17", | |
| 469 | − "Statement": [ | |
| 470 | − { | |
| 471 | − "Sid": "statement1", | |
| 472 | − "Effect": "Allow", | |
| 473 | − "Action": "s3:GetObjectVersion", | |
| 474 | − "Resource": "arn:aws:s3:::test/HappyFace.jpg" | |
| 475 | − }, | |
| 476 | − { | |
| 477 | − "Sid": "statement2", | |
| 478 | − "Effect": "Deny", | |
| 479 | − "Action": "s3:GetObjectVersion", | |
| 480 | − "Resource": "arn:aws:s3:::test/HappyFace.jpg", | |
| 481 | − "Condition": { | |
| 482 | − "StringNotEquals": { | |
| 483 | − "s3:versionid": "AaaHbAQitwiL_h47_44lRO2DDfLlBO5e" | |
| 484 | − } | |
| 485 | − } | |
| 486 | − } | |
| 487 | − ] | |
| 488 | −}` | |
| 489 | − | |
| 490 | − policy3ConditionActionRegex := `{ | |
| 491 | − "Version": "2012-10-17", | |
| 492 | − "Statement": [ | |
| 493 | − { | |
| 494 | − "Sid": "statement2", | |
| 495 | − "Effect": "Allow", | |
| 496 | − "Action": "s3:Get*", | |
| 497 | − "Resource": "arn:aws:s3:::test/HappyFace.jpg", | |
| 498 | − "Condition": { | |
| 499 | − "StringEquals": { | |
| 500 | − "s3:versionid": "AaaHbAQitwiL_h47_44lRO2DDfLlBO5e" | |
| 501 | − } | |
| 502 | − } | |
| 503 | − } | |
| 504 | − ] | |
| 505 | −}` | |
| 506 | − | |
| 507 | − policy4ConditionAction := `{ | |
| 508 | − "Version": "2012-10-17", | |
| 509 | − "Statement": [ | |
| 510 | − { | |
| 511 | − "Sid": "statement2", | |
| 512 | − "Effect": "Allow", | |
| 513 | − "Action": "s3:GetObject", | |
| 514 | − "Resource": "arn:aws:s3:::test/HappyFace.jpg", | |
| 515 | − "Condition": { | |
| 516 | − "StringEquals": { | |
| 517 | − "s3:versionid": "AaaHbAQitwiL_h47_44lRO2DDfLlBO5e" | |
| 518 | − } | |
| 519 | − } | |
| 520 | − } | |
| 521 | − ] | |
| 522 | −}` | |
| 523 | − | |
| 524 | − policy5ConditionCurrenTime := `{ | |
| 525 | − "Version": "2012-10-17", | |
| 526 | − "Statement": [ | |
| 527 | − { | |
| 528 | − "Effect": "Allow", | |
| 529 | − "Action": [ | |
| 530 | − "s3:Get*", | |
| 531 | − "s3:Put*" | |
| 532 | − ], | |
| 533 | − "Resource": [ | |
| 534 | − "arn:aws:s3:::test/*" | |
| 535 | − ], | |
| 536 | − "Condition": { | |
| 537 | − "DateGreaterThan": { | |
| 538 | − "aws:CurrentTime": [ | |
| 539 | − "2017-02-28T00:00:00Z" | |
| 540 | − ] | |
| 541 | − } | |
| 542 | − } | |
| 543 | − } | |
| 544 | − ] | |
| 545 | −}` | |
| 546 | − | |
| 547 | − policy5ConditionCurrenTimeLesser := `{ | |
| 548 | − "Version": "2012-10-17", | |
| 549 | − "Statement": [ | |
| 550 | − { | |
| 551 | − "Effect": "Allow", | |
| 552 | − "Action": [ | |
| 553 | − "s3:Get*", | |
| 554 | − "s3:Put*" | |
| 555 | − ], | |
| 556 | − "Resource": [ | |
| 557 | − "arn:aws:s3:::test/*" | |
| 558 | − ], | |
| 559 | − "Condition": { | |
| 560 | − "DateLessThan": { | |
| 561 | − "aws:CurrentTime": [ | |
| 562 | − "2017-02-28T00:00:00Z" | |
| 563 | − ] | |
| 564 | − } | |
| 565 | − } | |
| 566 | − } | |
| 567 | − ] | |
| 568 | −}` | |
| 569 | − | |
| 570 | − tests := []struct { | |
| 571 | − p string | |
| 572 | − args Args | |
| 573 | − allowed bool | |
| 574 | − }{ | |
| 575 | − { | |
| 576 | − p: policy1LocationConstraint, | |
| 577 | − allowed: true, | |
| 578 | − args: Args{ | |
| 579 | − AccountName: "allowed", | |
| 580 | − Action: CreateBucketAction, | |
| 581 | − BucketName: "test", | |
| 582 | − ConditionValues: map[string][]string{"LocationConstraint": {"us-east-1"}}, | |
| 583 | − }, | |
| 584 | − }, | |
| 585 | − { | |
| 586 | − p: policy1LocationConstraint, | |
| 587 | − allowed: false, | |
| 588 | − args: Args{ | |
| 589 | − AccountName: "disallowed", | |
| 590 | − Action: CreateBucketAction, | |
| 591 | − BucketName: "test", | |
| 592 | − ConditionValues: map[string][]string{"LocationConstraint": {"us-east-2"}}, | |
| 593 | − }, | |
| 594 | − }, | |
| 595 | − { | |
| 596 | − p: policy2Condition, | |
| 597 | − allowed: true, | |
| 598 | − args: Args{ | |
| 599 | − AccountName: "allowed", | |
| 600 | − Action: GetObjectAction, | |
| 601 | − BucketName: "test", | |
| 602 | − ObjectName: "HappyFace.jpg", | |
| 603 | − ConditionValues: map[string][]string{"versionid": {"AaaHbAQitwiL_h47_44lRO2DDfLlBO5e"}}, | |
| 604 | − }, | |
| 605 | − }, | |
| 606 | − { | |
| 607 | − p: policy2Condition, | |
| 608 | − allowed: false, | |
| 609 | − args: Args{ | |
| 610 | − AccountName: "disallowed", | |
| 611 | − Action: GetObjectAction, | |
| 612 | − BucketName: "test", | |
| 613 | − ObjectName: "HappyFace.jpg", | |
| 614 | − ConditionValues: map[string][]string{"versionid": {"AaaHbAQitwiL_h47_44lRO2DDfLlBO5f"}}, | |
| 615 | − }, | |
| 616 | − }, | |
| 617 | − { | |
| 618 | − p: policy3ConditionActionRegex, | |
| 619 | − allowed: true, | |
| 620 | − args: Args{ | |
| 621 | − AccountName: "allowed", | |
| 622 | − Action: GetObjectAction, | |
| 623 | − BucketName: "test", | |
| 624 | − ObjectName: "HappyFace.jpg", | |
| 625 | − ConditionValues: map[string][]string{"versionid": {"AaaHbAQitwiL_h47_44lRO2DDfLlBO5e"}}, | |
| 626 | − }, | |
| 627 | − }, | |
| 628 | − { | |
| 629 | − p: policy3ConditionActionRegex, | |
| 630 | − allowed: false, | |
| 631 | − args: Args{ | |
| 632 | − AccountName: "disallowed", | |
| 633 | − Action: GetObjectAction, | |
| 634 | − BucketName: "test", | |
| 635 | − ObjectName: "HappyFace.jpg", | |
| 636 | − ConditionValues: map[string][]string{"versionid": {"AaaHbAQitwiL_h47_44lRO2DDfLlBO5f"}}, | |
| 637 | − }, | |
| 638 | − }, | |
| 639 | − { | |
| 640 | − p: policy4ConditionAction, | |
| 641 | − allowed: true, | |
| 642 | − args: Args{ | |
| 643 | − AccountName: "allowed", | |
| 644 | − Action: GetObjectAction, | |
| 645 | − BucketName: "test", | |
| 646 | − ObjectName: "HappyFace.jpg", | |
| 647 | − ConditionValues: map[string][]string{"versionid": {"AaaHbAQitwiL_h47_44lRO2DDfLlBO5e"}}, | |
| 648 | − }, | |
| 649 | − }, | |
| 650 | − { | |
| 651 | − p: policy5ConditionCurrenTime, | |
| 652 | − allowed: true, | |
| 653 | − args: Args{ | |
| 654 | − AccountName: "allowed", | |
| 655 | − Action: GetObjectAction, | |
| 656 | − BucketName: "test", | |
| 657 | − ObjectName: "HappyFace.jpg", | |
| 658 | − ConditionValues: map[string][]string{ | |
| 659 | − "CurrentTime": {time.Now().Format(time.RFC3339)}, | |
| 660 | − }, | |
| 661 | − }, | |
| 662 | − }, | |
| 663 | − { | |
| 664 | − p: policy5ConditionCurrenTimeLesser, | |
| 665 | − allowed: false, | |
| 666 | − args: Args{ | |
| 667 | − AccountName: "disallowed", | |
| 668 | − Action: GetObjectAction, | |
| 669 | − BucketName: "test", | |
| 670 | − ObjectName: "HappyFace.jpg", | |
| 671 | − ConditionValues: map[string][]string{ | |
| 672 | − "CurrentTime": {time.Now().Format(time.RFC3339)}, | |
| 673 | − }, | |
| 674 | − }, | |
| 675 | − }, | |
| 676 | − } | |
| 677 | − for _, test := range tests { | |
| 678 | − test := test | |
| 679 | − t.Run(test.args.AccountName, func(t *testing.T) { | |
| 680 | − ip, err := ParseConfig(strings.NewReader(test.p)) | |
| 681 | − if err != nil { | |
| 682 | − t.Error(err) | |
| 683 | − } | |
| 684 | − if got := ip.IsAllowed(test.args); got != test.allowed { | |
| 685 | − t.Errorf("Expected %t, got %t", test.allowed, got) | |
| 686 | − } | |
| 687 | − }) | |
| 688 | − } | |
| 689 | −} | |
| 690 | − | |
| 691 | −func TestPolicyUnmarshalJSONAndValidate(t *testing.T) { | |
| 692 | − case1Data := []byte(`{ | |
| 693 | − "ID": "MyPolicyForMyBucket1", | |
| 694 | − "Version": "2012-10-17", | |
| 695 | − "Statement": [ | |
| 696 | − { | |
| 697 | − "Sid": "SomeId1", | |
| 698 | − "Effect": "Allow", | |
| 699 | − "Action": "s3:PutObject", | |
| 700 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 701 | − } | |
| 702 | − ] | |
| 703 | −}`) | |
| 704 | − case1Policy := Policy{ | |
| 705 | − ID: "MyPolicyForMyBucket1", | |
| 706 | − Version: DefaultVersion, | |
| 707 | − Statements: []Statement{ | |
| 708 | − NewStatement( | |
| 709 | − policy.Allow, | |
| 710 | − NewActionSet(PutObjectAction), | |
| 711 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 712 | − condition.NewFunctions(), | |
| 713 | − ), | |
| 714 | − }, | |
| 715 | − } | |
| 716 | − case1Policy.Statements[0].SID = "SomeId1" | |
| 717 | − | |
| 718 | − case2Data := []byte(`{ | |
| 719 | − "Version": "2012-10-17", | |
| 720 | − "Statement": [ | |
| 721 | − { | |
| 722 | − "Effect": "Allow", | |
| 723 | − "Action": "s3:PutObject", | |
| 724 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 725 | − }, | |
| 726 | − { | |
| 727 | − "Effect": "Deny", | |
| 728 | − "Action": "s3:GetObject", | |
| 729 | − "Resource": "arn:aws:s3:::mybucket/yourobject*", | |
| 730 | − "Condition": { | |
| 731 | − "IpAddress": { | |
| 732 | − "aws:SourceIp": "192.168.1.0/24" | |
| 733 | − } | |
| 734 | − } | |
| 735 | − } | |
| 736 | − ] | |
| 737 | −}`) | |
| 738 | − _, IPNet1, err := net.ParseCIDR("192.168.1.0/24") | |
| 739 | − if err != nil { | |
| 740 | − t.Fatalf("unexpected error. %v\n", err) | |
| 741 | − } | |
| 742 | − func1, err := condition.NewIPAddressFunc( | |
| 743 | − condition.AWSSourceIP, | |
| 744 | − IPNet1, | |
| 745 | − ) | |
| 746 | − if err != nil { | |
| 747 | − t.Fatalf("unexpected error. %v\n", err) | |
| 748 | − } | |
| 749 | − | |
| 750 | − case2Policy := Policy{ | |
| 751 | − Version: DefaultVersion, | |
| 752 | − Statements: []Statement{ | |
| 753 | − NewStatement( | |
| 754 | − policy.Allow, | |
| 755 | − NewActionSet(PutObjectAction), | |
| 756 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 757 | − condition.NewFunctions(), | |
| 758 | − ), | |
| 759 | − NewStatement( | |
| 760 | − policy.Deny, | |
| 761 | − NewActionSet(GetObjectAction), | |
| 762 | − NewResourceSet(NewResource("mybucket", "/yourobject*")), | |
| 763 | − condition.NewFunctions(func1), | |
| 764 | − ), | |
| 765 | − }, | |
| 766 | − } | |
| 767 | − | |
| 768 | − case3Data := []byte(`{ | |
| 769 | − "ID": "MyPolicyForMyBucket1", | |
| 770 | − "Version": "2012-10-17", | |
| 771 | − "Statement": [ | |
| 772 | − { | |
| 773 | − "Effect": "Allow", | |
| 774 | − "Action": "s3:GetObject", | |
| 775 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 776 | − }, | |
| 777 | − { | |
| 778 | − "Effect": "Allow", | |
| 779 | − "Action": "s3:PutObject", | |
| 780 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 781 | − } | |
| 782 | − ] | |
| 783 | −}`) | |
| 784 | − case3Policy := Policy{ | |
| 785 | − ID: "MyPolicyForMyBucket1", | |
| 786 | − Version: DefaultVersion, | |
| 787 | − Statements: []Statement{ | |
| 788 | − NewStatement( | |
| 789 | − policy.Allow, | |
| 790 | − NewActionSet(GetObjectAction), | |
| 791 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 792 | − condition.NewFunctions(), | |
| 793 | − ), | |
| 794 | − NewStatement( | |
| 795 | − policy.Allow, | |
| 796 | − NewActionSet(PutObjectAction), | |
| 797 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 798 | − condition.NewFunctions(), | |
| 799 | − ), | |
| 800 | − }, | |
| 801 | − } | |
| 802 | − | |
| 803 | − case4Data := []byte(`{ | |
| 804 | − "ID": "MyPolicyForMyBucket1", | |
| 805 | − "Version": "2012-10-17", | |
| 806 | − "Statement": [ | |
| 807 | − { | |
| 808 | − "Effect": "Allow", | |
| 809 | − "Action": "s3:PutObject", | |
| 810 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 811 | − }, | |
| 812 | − { | |
| 813 | − "Effect": "Allow", | |
| 814 | − "Action": "s3:GetObject", | |
| 815 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 816 | − } | |
| 817 | − ] | |
| 818 | −}`) | |
| 819 | − case4Policy := Policy{ | |
| 820 | − ID: "MyPolicyForMyBucket1", | |
| 821 | − Version: DefaultVersion, | |
| 822 | − Statements: []Statement{ | |
| 823 | − NewStatement( | |
| 824 | − policy.Allow, | |
| 825 | − NewActionSet(PutObjectAction), | |
| 826 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 827 | − condition.NewFunctions(), | |
| 828 | − ), | |
| 829 | − NewStatement( | |
| 830 | − policy.Allow, | |
| 831 | − NewActionSet(GetObjectAction), | |
| 832 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 833 | − condition.NewFunctions(), | |
| 834 | − ), | |
| 835 | − }, | |
| 836 | − } | |
| 837 | − | |
| 838 | − case5Data := []byte(`{ | |
| 839 | − "ID": "MyPolicyForMyBucket1", | |
| 840 | − "Version": "2012-10-17", | |
| 841 | − "Statement": [ | |
| 842 | − { | |
| 843 | − "Effect": "Allow", | |
| 844 | − "Action": "s3:PutObject", | |
| 845 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 846 | − }, | |
| 847 | − { | |
| 848 | − "Effect": "Allow", | |
| 849 | − "Action": "s3:PutObject", | |
| 850 | − "Resource": "arn:aws:s3:::mybucket/yourobject*" | |
| 851 | − } | |
| 852 | − ] | |
| 853 | −}`) | |
| 854 | − case5Policy := Policy{ | |
| 855 | − ID: "MyPolicyForMyBucket1", | |
| 856 | − Version: DefaultVersion, | |
| 857 | − Statements: []Statement{ | |
| 858 | − NewStatement( | |
| 859 | − policy.Allow, | |
| 860 | − NewActionSet(PutObjectAction), | |
| 861 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 862 | − condition.NewFunctions(), | |
| 863 | − ), | |
| 864 | − NewStatement( | |
| 865 | − policy.Allow, | |
| 866 | − NewActionSet(PutObjectAction), | |
| 867 | − NewResourceSet(NewResource("mybucket", "/yourobject*")), | |
| 868 | − condition.NewFunctions(), | |
| 869 | − ), | |
| 870 | − }, | |
| 871 | − } | |
| 872 | − | |
| 873 | − case6Data := []byte(`{ | |
| 874 | − "ID": "MyPolicyForMyBucket1", | |
| 875 | − "Version": "2012-10-17", | |
| 876 | − "Statement": [ | |
| 877 | − { | |
| 878 | − "Effect": "Allow", | |
| 879 | − "Action": "s3:PutObject", | |
| 880 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 881 | − "Condition": { | |
| 882 | − "IpAddress": { | |
| 883 | − "aws:SourceIp": "192.168.1.0/24" | |
| 884 | − } | |
| 885 | − } | |
| 886 | − }, | |
| 887 | − { | |
| 888 | − "Effect": "Allow", | |
| 889 | − "Action": "s3:PutObject", | |
| 890 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 891 | − "Condition": { | |
| 892 | − "IpAddress": { | |
| 893 | − "aws:SourceIp": "192.168.2.0/24" | |
| 894 | − } | |
| 895 | − } | |
| 896 | − } | |
| 897 | − ] | |
| 898 | −}`) | |
| 899 | − _, IPNet2, err := net.ParseCIDR("192.168.2.0/24") | |
| 900 | − if err != nil { | |
| 901 | − t.Fatalf("unexpected error. %v\n", err) | |
| 902 | − } | |
| 903 | − func2, err := condition.NewIPAddressFunc( | |
| 904 | − condition.AWSSourceIP, | |
| 905 | − IPNet2, | |
| 906 | − ) | |
| 907 | − if err != nil { | |
| 908 | − t.Fatalf("unexpected error. %v\n", err) | |
| 909 | − } | |
| 910 | − | |
| 911 | − case6Policy := Policy{ | |
| 912 | − ID: "MyPolicyForMyBucket1", | |
| 913 | − Version: DefaultVersion, | |
| 914 | − Statements: []Statement{ | |
| 915 | − NewStatement( | |
| 916 | − policy.Allow, | |
| 917 | − NewActionSet(PutObjectAction), | |
| 918 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 919 | − condition.NewFunctions(func1), | |
| 920 | − ), | |
| 921 | − NewStatement( | |
| 922 | − policy.Allow, | |
| 923 | − NewActionSet(PutObjectAction), | |
| 924 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 925 | − condition.NewFunctions(func2), | |
| 926 | − ), | |
| 927 | − }, | |
| 928 | − } | |
| 929 | − | |
| 930 | − case7Data := []byte(`{ | |
| 931 | − "ID": "MyPolicyForMyBucket1", | |
| 932 | − "Version": "2012-10-17", | |
| 933 | − "Statement": [ | |
| 934 | − { | |
| 935 | − "Effect": "Allow", | |
| 936 | − "Action": "s3:GetBucketLocation", | |
| 937 | − "Resource": "arn:aws:s3:::mybucket" | |
| 938 | − } | |
| 939 | − ] | |
| 940 | −}`) | |
| 941 | − | |
| 942 | − case7Policy := Policy{ | |
| 943 | − ID: "MyPolicyForMyBucket1", | |
| 944 | − Version: DefaultVersion, | |
| 945 | − Statements: []Statement{ | |
| 946 | − NewStatement( | |
| 947 | − policy.Allow, | |
| 948 | − NewActionSet(GetBucketLocationAction), | |
| 949 | − NewResourceSet(NewResource("mybucket", "")), | |
| 950 | − condition.NewFunctions(), | |
| 951 | − ), | |
| 952 | − }, | |
| 953 | − } | |
| 954 | − | |
| 955 | − case8Data := []byte(`{ | |
| 956 | − "ID": "MyPolicyForMyBucket1", | |
| 957 | − "Version": "2012-10-17", | |
| 958 | − "Statement": [ | |
| 959 | − { | |
| 960 | − "Effect": "Allow", | |
| 961 | − "Action": "s3:GetBucketLocation", | |
| 962 | − "Resource": "arn:aws:s3:::*" | |
| 963 | − } | |
| 964 | − ] | |
| 965 | −}`) | |
| 966 | − | |
| 967 | − case8Policy := Policy{ | |
| 968 | − ID: "MyPolicyForMyBucket1", | |
| 969 | − Version: DefaultVersion, | |
| 970 | − Statements: []Statement{ | |
| 971 | − NewStatement( | |
| 972 | − policy.Allow, | |
| 973 | − NewActionSet(GetBucketLocationAction), | |
| 974 | − NewResourceSet(NewResource("*", "")), | |
| 975 | − condition.NewFunctions(), | |
| 976 | − ), | |
| 977 | − }, | |
| 978 | − } | |
| 979 | − | |
| 980 | − case9Data := []byte(`{ | |
| 981 | − "ID": "MyPolicyForMyBucket1", | |
| 982 | − "Version": "17-10-2012", | |
| 983 | − "Statement": [ | |
| 984 | − { | |
| 985 | − "Effect": "Allow", | |
| 986 | − "Action": "s3:PutObject", | |
| 987 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 988 | − } | |
| 989 | − ] | |
| 990 | −}`) | |
| 991 | − | |
| 992 | − case10Data := []byte(`{ | |
| 993 | − "ID": "MyPolicyForMyBucket1", | |
| 994 | − "Version": "2012-10-17", | |
| 995 | − "Statement": [ | |
| 996 | − { | |
| 997 | − "Effect": "Allow", | |
| 998 | − "Action": "s3:PutObject", | |
| 999 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 1000 | − }, | |
| 1001 | − { | |
| 1002 | − "Effect": "Allow", | |
| 1003 | − "Action": "s3:PutObject", | |
| 1004 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 1005 | − } | |
| 1006 | − ] | |
| 1007 | −}`) | |
| 1008 | − case10Policy := Policy{ | |
| 1009 | − ID: "MyPolicyForMyBucket1", | |
| 1010 | − Version: DefaultVersion, | |
| 1011 | − Statements: []Statement{ | |
| 1012 | − NewStatement( | |
| 1013 | − policy.Allow, | |
| 1014 | − NewActionSet(PutObjectAction), | |
| 1015 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 1016 | − condition.NewFunctions(), | |
| 1017 | − ), | |
| 1018 | − }, | |
| 1019 | − } | |
| 1020 | − | |
| 1021 | − case11Data := []byte(`{ | |
| 1022 | − "ID": "MyPolicyForMyBucket1", | |
| 1023 | − "Version": "2012-10-17", | |
| 1024 | − "Statement": [ | |
| 1025 | − { | |
| 1026 | − "Effect": "Allow", | |
| 1027 | − "Action": "s3:PutObject", | |
| 1028 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 1029 | − }, | |
| 1030 | − { | |
| 1031 | − "Effect": "Deny", | |
| 1032 | − "Action": "s3:PutObject", | |
| 1033 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 1034 | − } | |
| 1035 | − ] | |
| 1036 | −}`) | |
| 1037 | − | |
| 1038 | − case11Policy := Policy{ | |
| 1039 | − ID: "MyPolicyForMyBucket1", | |
| 1040 | − Version: DefaultVersion, | |
| 1041 | − Statements: []Statement{ | |
| 1042 | − NewStatement( | |
| 1043 | − policy.Allow, | |
| 1044 | − NewActionSet(PutObjectAction), | |
| 1045 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 1046 | − condition.NewFunctions(), | |
| 1047 | − ), | |
| 1048 | − NewStatement( | |
| 1049 | − policy.Deny, | |
| 1050 | − NewActionSet(PutObjectAction), | |
| 1051 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 1052 | − condition.NewFunctions(), | |
| 1053 | − ), | |
| 1054 | − }, | |
| 1055 | − } | |
| 1056 | − | |
| 1057 | − testCases := []struct { | |
| 1058 | − data []byte | |
| 1059 | − expectedResult Policy | |
| 1060 | − expectUnmarshalErr bool | |
| 1061 | − expectValidationErr bool | |
| 1062 | − }{ | |
| 1063 | − {case1Data, case1Policy, false, false}, | |
| 1064 | − {case2Data, case2Policy, false, false}, | |
| 1065 | − {case3Data, case3Policy, false, false}, | |
| 1066 | − {case4Data, case4Policy, false, false}, | |
| 1067 | − {case5Data, case5Policy, false, false}, | |
| 1068 | − {case6Data, case6Policy, false, false}, | |
| 1069 | − {case7Data, case7Policy, false, false}, | |
| 1070 | − {case8Data, case8Policy, false, false}, | |
| 1071 | − // Invalid version error. | |
| 1072 | − {case9Data, Policy{}, false, true}, | |
| 1073 | − // Duplicate statement success, duplicate statement is removed. | |
| 1074 | − {case10Data, case10Policy, false, false}, | |
| 1075 | − // Duplicate statement success (Effect differs). | |
| 1076 | − {case11Data, case11Policy, false, false}, | |
| 1077 | − } | |
| 1078 | − | |
| 1079 | − for i, testCase := range testCases { | |
| 1080 | − var result Policy | |
| 1081 | − err := json.Unmarshal(testCase.data, &result) | |
| 1082 | − expectErr := (err != nil) | |
| 1083 | − | |
| 1084 | − if expectErr != testCase.expectUnmarshalErr { | |
| 1085 | − t.Errorf("case %v: error during unmarshal: expected: %v, got: %v", i+1, testCase.expectUnmarshalErr, expectErr) | |
| 1086 | − } | |
| 1087 | − | |
| 1088 | − err = result.Validate() | |
| 1089 | − expectErr = (err != nil) | |
| 1090 | − | |
| 1091 | − if expectErr != testCase.expectValidationErr { | |
| 1092 | − t.Errorf("case %v: error during validation: expected: %v, got: %v", i+1, testCase.expectValidationErr, expectErr) | |
| 1093 | − } | |
| 1094 | − | |
| 1095 | − if !testCase.expectUnmarshalErr && !testCase.expectValidationErr { | |
| 1096 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 1097 | − t.Errorf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 1098 | − } | |
| 1099 | − } | |
| 1100 | − } | |
| 1101 | −} | |
| 1102 | − | |
| 1103 | −func TestPolicyValidate(t *testing.T) { | |
| 1104 | − case1Policy := Policy{ | |
| 1105 | − Version: DefaultVersion, | |
| 1106 | − Statements: []Statement{ | |
| 1107 | − NewStatement( | |
| 1108 | − policy.Allow, | |
| 1109 | − NewActionSet(PutObjectAction), | |
| 1110 | − NewResourceSet(NewResource("", "")), | |
| 1111 | − condition.NewFunctions(), | |
| 1112 | − ), | |
| 1113 | − }, | |
| 1114 | − } | |
| 1115 | − | |
| 1116 | − func1, err := condition.NewNullFunc( | |
| 1117 | − condition.S3XAmzCopySource, | |
| 1118 | − true, | |
| 1119 | − ) | |
| 1120 | − if err != nil { | |
| 1121 | − t.Fatalf("unexpected error. %v\n", err) | |
| 1122 | − } | |
| 1123 | − func2, err := condition.NewNullFunc( | |
| 1124 | − condition.S3XAmzServerSideEncryption, | |
| 1125 | − false, | |
| 1126 | − ) | |
| 1127 | − if err != nil { | |
| 1128 | − t.Fatalf("unexpected error. %v\n", err) | |
| 1129 | − } | |
| 1130 | − case2Policy := Policy{ | |
| 1131 | − ID: "MyPolicyForMyBucket1", | |
| 1132 | − Version: DefaultVersion, | |
| 1133 | − Statements: []Statement{ | |
| 1134 | − NewStatement( | |
| 1135 | − policy.Allow, | |
| 1136 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 1137 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 1138 | − condition.NewFunctions(func1, func2), | |
| 1139 | − ), | |
| 1140 | − }, | |
| 1141 | − } | |
| 1142 | − | |
| 1143 | − case3Policy := Policy{ | |
| 1144 | − ID: "MyPolicyForMyBucket1", | |
| 1145 | − Version: DefaultVersion, | |
| 1146 | − Statements: []Statement{ | |
| 1147 | − NewStatement( | |
| 1148 | − policy.Allow, | |
| 1149 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 1150 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 1151 | − condition.NewFunctions(), | |
| 1152 | − ), | |
| 1153 | − }, | |
| 1154 | − } | |
| 1155 | − | |
| 1156 | − testCases := []struct { | |
| 1157 | − policy Policy | |
| 1158 | − expectErr bool | |
| 1159 | − }{ | |
| 1160 | − {case1Policy, true}, | |
| 1161 | − {case2Policy, true}, | |
| 1162 | − {case3Policy, false}, | |
| 1163 | − } | |
| 1164 | − | |
| 1165 | − for i, testCase := range testCases { | |
| 1166 | − err := testCase.policy.Validate() | |
| 1167 | − expectErr := (err != nil) | |
| 1168 | − | |
| 1169 | − if expectErr != testCase.expectErr { | |
| 1170 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 1171 | − } | |
| 1172 | − } | |
| 1173 | −} | |
| @@ -1,143 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "path" | |
| 23 | − "strings" | |
| 24 | − | |
| 25 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 26 | − "github.com/minio/pkg/wildcard" | |
| 27 | −) | |
| 28 | − | |
| 29 | −// ResourceARNPrefix - resource ARN prefix as per AWS S3 specification. | |
| 30 | −const ResourceARNPrefix = "arn:aws:s3:::" | |
| 31 | − | |
| 32 | −// Resource - resource in policy statement. | |
| 33 | −type Resource struct { | |
| 34 | − BucketName string | |
| 35 | − Pattern string | |
| 36 | −} | |
| 37 | − | |
| 38 | −func (r Resource) isBucketPattern() bool { | |
| 39 | − return !strings.Contains(r.Pattern, "/") || r.Pattern == "*" | |
| 40 | −} | |
| 41 | − | |
| 42 | −func (r Resource) isObjectPattern() bool { | |
| 43 | − return strings.Contains(r.Pattern, "/") || strings.Contains(r.BucketName, "*") || r.Pattern == "*/*" | |
| 44 | −} | |
| 45 | − | |
| 46 | −// IsValid - checks whether Resource is valid or not. | |
| 47 | −func (r Resource) IsValid() bool { | |
| 48 | − return r.Pattern != "" | |
| 49 | −} | |
| 50 | − | |
| 51 | −// MatchResource matches object name with resource pattern only. | |
| 52 | −func (r Resource) MatchResource(resource string) bool { | |
| 53 | − return r.Match(resource, nil) | |
| 54 | −} | |
| 55 | − | |
| 56 | −// Match - matches object name with resource pattern, including specific conditionals. | |
| 57 | −func (r Resource) Match(resource string, conditionValues map[string][]string) bool { | |
| 58 | − pattern := r.Pattern | |
| 59 | − for _, key := range condition.CommonKeys { | |
| 60 | − // Empty values are not supported for policy variables. | |
| 61 | − if rvalues, ok := conditionValues[key.Name()]; ok && rvalues[0] != "" { | |
| 62 | − pattern = strings.Replace(pattern, key.VarName(), rvalues[0], -1) | |
| 63 | − } | |
| 64 | − } | |
| 65 | − if cp := path.Clean(resource); cp != "." && cp == pattern { | |
| 66 | − return true | |
| 67 | − } | |
| 68 | − return wildcard.Match(pattern, resource) | |
| 69 | −} | |
| 70 | − | |
| 71 | −// MarshalJSON - encodes Resource to JSON data. | |
| 72 | −func (r Resource) MarshalJSON() ([]byte, error) { | |
| 73 | − if !r.IsValid() { | |
| 74 | − return nil, Errorf("invalid resource %v", r) | |
| 75 | − } | |
| 76 | − | |
| 77 | − return json.Marshal(r.String()) | |
| 78 | −} | |
| 79 | − | |
| 80 | −func (r Resource) String() string { | |
| 81 | − return ResourceARNPrefix + r.Pattern | |
| 82 | −} | |
| 83 | − | |
| 84 | −// UnmarshalJSON - decodes JSON data to Resource. | |
| 85 | −func (r *Resource) UnmarshalJSON(data []byte) error { | |
| 86 | − var s string | |
| 87 | − if err := json.Unmarshal(data, &s); err != nil { | |
| 88 | − return err | |
| 89 | − } | |
| 90 | − | |
| 91 | − parsedResource, err := parseResource(s) | |
| 92 | − if err != nil { | |
| 93 | − return err | |
| 94 | − } | |
| 95 | − | |
| 96 | − *r = parsedResource | |
| 97 | − | |
| 98 | − return nil | |
| 99 | −} | |
| 100 | − | |
| 101 | −// Validate - validates Resource is for given bucket or not. | |
| 102 | −func (r Resource) Validate() error { | |
| 103 | − if !r.IsValid() { | |
| 104 | − return Errorf("invalid resource") | |
| 105 | − } | |
| 106 | − return nil | |
| 107 | −} | |
| 108 | − | |
| 109 | −// parseResource - parses string to Resource. | |
| 110 | −func parseResource(s string) (Resource, error) { | |
| 111 | − if !strings.HasPrefix(s, ResourceARNPrefix) { | |
| 112 | − return Resource{}, Errorf("invalid resource '%v'", s) | |
| 113 | − } | |
| 114 | − | |
| 115 | − pattern := strings.TrimPrefix(s, ResourceARNPrefix) | |
| 116 | − tokens := strings.SplitN(pattern, "/", 2) | |
| 117 | − bucketName := tokens[0] | |
| 118 | − if bucketName == "" { | |
| 119 | − return Resource{}, Errorf("invalid resource format '%v'", s) | |
| 120 | − } | |
| 121 | − | |
| 122 | − return Resource{ | |
| 123 | − BucketName: bucketName, | |
| 124 | − Pattern: pattern, | |
| 125 | − }, nil | |
| 126 | −} | |
| 127 | − | |
| 128 | −// NewResource - creates new resource. | |
| 129 | −func NewResource(bucketName, keyName string) Resource { | |
| 130 | − pattern := bucketName | |
| 131 | − if keyName != "" { | |
| 132 | − if !strings.HasPrefix(keyName, "/") { | |
| 133 | − pattern += "/" | |
| 134 | − } | |
| 135 | − | |
| 136 | − pattern += keyName | |
| 137 | − } | |
| 138 | − | |
| 139 | − return Resource{ | |
| 140 | − BucketName: bucketName, | |
| 141 | − Pattern: pattern, | |
| 142 | − } | |
| 143 | −} | |
| @@ -1,224 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | − "reflect" | |
| 24 | − "testing" | |
| 25 | −) | |
| 26 | − | |
| 27 | −func TestResourceIsBucketPattern(t *testing.T) { | |
| 28 | − testCases := []struct { | |
| 29 | − resource Resource | |
| 30 | − expectedResult bool | |
| 31 | − }{ | |
| 32 | − {NewResource("*", ""), true}, | |
| 33 | − {NewResource("mybucket", ""), true}, | |
| 34 | − {NewResource("mybucket*", ""), true}, | |
| 35 | − {NewResource("mybucket?0", ""), true}, | |
| 36 | − {NewResource("", "*"), false}, | |
| 37 | − {NewResource("*", "*"), false}, | |
| 38 | − {NewResource("mybucket", "*"), false}, | |
| 39 | − {NewResource("mybucket*", "/myobject"), false}, | |
| 40 | − {NewResource("mybucket?0", "/2010/photos/*"), false}, | |
| 41 | − } | |
| 42 | − | |
| 43 | − for i, testCase := range testCases { | |
| 44 | − result := testCase.resource.isBucketPattern() | |
| 45 | − | |
| 46 | − if result != testCase.expectedResult { | |
| 47 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 48 | − } | |
| 49 | − } | |
| 50 | −} | |
| 51 | − | |
| 52 | −func TestResourceIsObjectPattern(t *testing.T) { | |
| 53 | − testCases := []struct { | |
| 54 | − resource Resource | |
| 55 | − expectedResult bool | |
| 56 | − }{ | |
| 57 | − {NewResource("*", ""), true}, | |
| 58 | − {NewResource("mybucket*", ""), true}, | |
| 59 | − {NewResource("", "*"), true}, | |
| 60 | − {NewResource("*", "*"), true}, | |
| 61 | − {NewResource("mybucket", "*"), true}, | |
| 62 | − {NewResource("mybucket*", "/myobject"), true}, | |
| 63 | − {NewResource("mybucket?0", "/2010/photos/*"), true}, | |
| 64 | − {NewResource("mybucket", ""), false}, | |
| 65 | − {NewResource("mybucket?0", ""), false}, | |
| 66 | − } | |
| 67 | − | |
| 68 | − for i, testCase := range testCases { | |
| 69 | − result := testCase.resource.isObjectPattern() | |
| 70 | − | |
| 71 | − if result != testCase.expectedResult { | |
| 72 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 73 | − } | |
| 74 | − } | |
| 75 | −} | |
| 76 | − | |
| 77 | −func TestResourceIsValid(t *testing.T) { | |
| 78 | − testCases := []struct { | |
| 79 | − resource Resource | |
| 80 | − expectedResult bool | |
| 81 | − }{ | |
| 82 | − {NewResource("*", ""), true}, | |
| 83 | − {NewResource("mybucket*", ""), true}, | |
| 84 | − {NewResource("*", "*"), true}, | |
| 85 | − {NewResource("mybucket", "*"), true}, | |
| 86 | − {NewResource("mybucket*", "/myobject"), true}, | |
| 87 | − {NewResource("mybucket?0", "/2010/photos/*"), true}, | |
| 88 | − {NewResource("mybucket", ""), true}, | |
| 89 | − {NewResource("mybucket?0", ""), true}, | |
| 90 | − {NewResource("", "*"), true}, | |
| 91 | − {NewResource("", ""), false}, | |
| 92 | − } | |
| 93 | − | |
| 94 | − for i, testCase := range testCases { | |
| 95 | − result := testCase.resource.IsValid() | |
| 96 | − | |
| 97 | − if result != testCase.expectedResult { | |
| 98 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 99 | − } | |
| 100 | − } | |
| 101 | −} | |
| 102 | − | |
| 103 | −func TestResourceMatch(t *testing.T) { | |
| 104 | − testCases := []struct { | |
| 105 | − resource Resource | |
| 106 | − objectName string | |
| 107 | − expectedResult bool | |
| 108 | − }{ | |
| 109 | − {NewResource("*", ""), "mybucket", true}, | |
| 110 | − {NewResource("*", ""), "mybucket/myobject", true}, | |
| 111 | − {NewResource("mybucket*", ""), "mybucket", true}, | |
| 112 | − {NewResource("mybucket*", ""), "mybucket/myobject", true}, | |
| 113 | − {NewResource("", "*"), "/myobject", true}, | |
| 114 | − {NewResource("*", "*"), "mybucket/myobject", true}, | |
| 115 | − {NewResource("mybucket", "*"), "mybucket/myobject", true}, | |
| 116 | − {NewResource("mybucket*", "/myobject"), "mybucket/myobject", true}, | |
| 117 | − {NewResource("mybucket*", "/myobject"), "mybucket100/myobject", true}, | |
| 118 | − {NewResource("mybucket?0", "/2010/photos/*"), "mybucket20/2010/photos/1.jpg", true}, | |
| 119 | − {NewResource("mybucket", ""), "mybucket", true}, | |
| 120 | − {NewResource("mybucket?0", ""), "mybucket30", true}, | |
| 121 | − {NewResource("", "*"), "mybucket/myobject", false}, | |
| 122 | − {NewResource("*", "*"), "mybucket", false}, | |
| 123 | − {NewResource("mybucket", "*"), "mybucket10/myobject", false}, | |
| 124 | − {NewResource("mybucket?0", "/2010/photos/*"), "mybucket0/2010/photos/1.jpg", false}, | |
| 125 | − {NewResource("mybucket", ""), "mybucket/myobject", false}, | |
| 126 | − } | |
| 127 | − | |
| 128 | − for i, testCase := range testCases { | |
| 129 | − testCase := testCase | |
| 130 | − t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) { | |
| 131 | − result := testCase.resource.Match(testCase.objectName, nil) | |
| 132 | − if result != testCase.expectedResult { | |
| 133 | − t.Errorf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 134 | − } | |
| 135 | − }) | |
| 136 | − } | |
| 137 | −} | |
| 138 | − | |
| 139 | −func TestResourceMarshalJSON(t *testing.T) { | |
| 140 | − testCases := []struct { | |
| 141 | − resource Resource | |
| 142 | − expectedResult []byte | |
| 143 | − expectErr bool | |
| 144 | − }{ | |
| 145 | − {NewResource("*", ""), []byte(`"arn:aws:s3:::*"`), false}, | |
| 146 | − {NewResource("mybucket*", ""), []byte(`"arn:aws:s3:::mybucket*"`), false}, | |
| 147 | − {NewResource("mybucket", ""), []byte(`"arn:aws:s3:::mybucket"`), false}, | |
| 148 | − {NewResource("*", "*"), []byte(`"arn:aws:s3:::*/*"`), false}, | |
| 149 | − {NewResource("", "*"), []byte(`"arn:aws:s3:::/*"`), false}, | |
| 150 | − {NewResource("mybucket", "*"), []byte(`"arn:aws:s3:::mybucket/*"`), false}, | |
| 151 | − {NewResource("mybucket*", "myobject"), []byte(`"arn:aws:s3:::mybucket*/myobject"`), false}, | |
| 152 | − {NewResource("mybucket?0", "/2010/photos/*"), []byte(`"arn:aws:s3:::mybucket?0/2010/photos/*"`), false}, | |
| 153 | − {Resource{}, nil, true}, | |
| 154 | − } | |
| 155 | − | |
| 156 | − for i, testCase := range testCases { | |
| 157 | − result, err := json.Marshal(testCase.resource) | |
| 158 | − expectErr := (err != nil) | |
| 159 | − | |
| 160 | − if expectErr != testCase.expectErr { | |
| 161 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 162 | − } | |
| 163 | − | |
| 164 | − if !testCase.expectErr { | |
| 165 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 166 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, string(testCase.expectedResult), string(result)) | |
| 167 | − } | |
| 168 | − } | |
| 169 | − } | |
| 170 | −} | |
| 171 | − | |
| 172 | −func TestResourceUnmarshalJSON(t *testing.T) { | |
| 173 | − testCases := []struct { | |
| 174 | − data []byte | |
| 175 | − expectedResult Resource | |
| 176 | − expectErr bool | |
| 177 | − }{ | |
| 178 | − {[]byte(`"arn:aws:s3:::*"`), NewResource("*", ""), false}, | |
| 179 | − {[]byte(`"arn:aws:s3:::mybucket*"`), NewResource("mybucket*", ""), false}, | |
| 180 | − {[]byte(`"arn:aws:s3:::mybucket"`), NewResource("mybucket", ""), false}, | |
| 181 | − {[]byte(`"arn:aws:s3:::*/*"`), NewResource("*", "*"), false}, | |
| 182 | − {[]byte(`"arn:aws:s3:::mybucket/*"`), NewResource("mybucket", "*"), false}, | |
| 183 | − {[]byte(`"arn:aws:s3:::mybucket*/myobject"`), NewResource("mybucket*", "myobject"), false}, | |
| 184 | − {[]byte(`"arn:aws:s3:::mybucket?0/2010/photos/*"`), NewResource("mybucket?0", "/2010/photos/*"), false}, | |
| 185 | − {[]byte(`"mybucket/myobject*"`), Resource{}, true}, | |
| 186 | − {[]byte(`"arn:aws:s3:::/*"`), Resource{}, true}, | |
| 187 | − } | |
| 188 | − | |
| 189 | − for i, testCase := range testCases { | |
| 190 | − var result Resource | |
| 191 | − err := json.Unmarshal(testCase.data, &result) | |
| 192 | − expectErr := (err != nil) | |
| 193 | − | |
| 194 | − if expectErr != testCase.expectErr { | |
| 195 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 196 | − } | |
| 197 | − | |
| 198 | − if !testCase.expectErr { | |
| 199 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 200 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 201 | − } | |
| 202 | − } | |
| 203 | − } | |
| 204 | −} | |
| 205 | − | |
| 206 | −func TestResourceValidate(t *testing.T) { | |
| 207 | − testCases := []struct { | |
| 208 | − resource Resource | |
| 209 | − expectErr bool | |
| 210 | − }{ | |
| 211 | − {NewResource("mybucket", "/myobject*"), false}, | |
| 212 | − {NewResource("", "/myobject*"), false}, | |
| 213 | − {NewResource("", ""), true}, | |
| 214 | − } | |
| 215 | − | |
| 216 | − for i, testCase := range testCases { | |
| 217 | − err := testCase.resource.Validate() | |
| 218 | − expectErr := (err != nil) | |
| 219 | − | |
| 220 | − if expectErr != testCase.expectErr { | |
| 221 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 222 | − } | |
| 223 | − } | |
| 224 | −} | |
| @@ -1,191 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | − "sort" | |
| 24 | − | |
| 25 | − "github.com/minio/minio-go/v7/pkg/set" | |
| 26 | −) | |
| 27 | − | |
| 28 | −// ResourceSet - set of resources in policy statement. | |
| 29 | −type ResourceSet map[Resource]struct{} | |
| 30 | − | |
| 31 | −// bucketResourceExists - checks if at least one bucket resource exists in the set. | |
| 32 | −func (resourceSet ResourceSet) bucketResourceExists() bool { | |
| 33 | − for resource := range resourceSet { | |
| 34 | − if resource.isBucketPattern() { | |
| 35 | − return true | |
| 36 | − } | |
| 37 | − } | |
| 38 | − | |
| 39 | − return false | |
| 40 | −} | |
| 41 | − | |
| 42 | −// objectResourceExists - checks if at least one object resource exists in the set. | |
| 43 | −func (resourceSet ResourceSet) objectResourceExists() bool { | |
| 44 | − for resource := range resourceSet { | |
| 45 | − if resource.isObjectPattern() { | |
| 46 | − return true | |
| 47 | − } | |
| 48 | − } | |
| 49 | − | |
| 50 | − return false | |
| 51 | −} | |
| 52 | − | |
| 53 | −// Add - adds resource to resource set. | |
| 54 | −func (resourceSet ResourceSet) Add(resource Resource) { | |
| 55 | − resourceSet[resource] = struct{}{} | |
| 56 | −} | |
| 57 | − | |
| 58 | −// Equals - checks whether given resource set is equal to current resource set or not. | |
| 59 | −func (resourceSet ResourceSet) Equals(sresourceSet ResourceSet) bool { | |
| 60 | − // If length of set is not equal to length of given set, the | |
| 61 | − // set is not equal to given set. | |
| 62 | − if len(resourceSet) != len(sresourceSet) { | |
| 63 | − return false | |
| 64 | − } | |
| 65 | − | |
| 66 | − // As both sets are equal in length, check each elements are equal. | |
| 67 | − for k := range resourceSet { | |
| 68 | − if _, ok := sresourceSet[k]; !ok { | |
| 69 | − return false | |
| 70 | − } | |
| 71 | − } | |
| 72 | − | |
| 73 | − return true | |
| 74 | −} | |
| 75 | − | |
| 76 | −// Intersection - returns resources available in both ResourceSet. | |
| 77 | −func (resourceSet ResourceSet) Intersection(sset ResourceSet) ResourceSet { | |
| 78 | − nset := NewResourceSet() | |
| 79 | − for k := range resourceSet { | |
| 80 | − if _, ok := sset[k]; ok { | |
| 81 | − nset.Add(k) | |
| 82 | − } | |
| 83 | − } | |
| 84 | − | |
| 85 | − return nset | |
| 86 | −} | |
| 87 | − | |
| 88 | −// MarshalJSON - encodes ResourceSet to JSON data. | |
| 89 | −func (resourceSet ResourceSet) MarshalJSON() ([]byte, error) { | |
| 90 | − if len(resourceSet) == 0 { | |
| 91 | − return nil, Errorf("empty resource set") | |
| 92 | − } | |
| 93 | − | |
| 94 | − resources := []Resource{} | |
| 95 | − for resource := range resourceSet { | |
| 96 | − resources = append(resources, resource) | |
| 97 | − } | |
| 98 | − | |
| 99 | − return json.Marshal(resources) | |
| 100 | −} | |
| 101 | − | |
| 102 | −// MatchResource matches object name with resource patterns only. | |
| 103 | −func (resourceSet ResourceSet) MatchResource(resource string) bool { | |
| 104 | − for r := range resourceSet { | |
| 105 | − if r.MatchResource(resource) { | |
| 106 | − return true | |
| 107 | − } | |
| 108 | − } | |
| 109 | − return false | |
| 110 | −} | |
| 111 | − | |
| 112 | −// Match - matches object name with anyone of resource pattern in resource set. | |
| 113 | −func (resourceSet ResourceSet) Match(resource string, conditionValues map[string][]string) bool { | |
| 114 | − for r := range resourceSet { | |
| 115 | − if r.Match(resource, conditionValues) { | |
| 116 | − return true | |
| 117 | − } | |
| 118 | − } | |
| 119 | − | |
| 120 | − return false | |
| 121 | −} | |
| 122 | − | |
| 123 | −func (resourceSet ResourceSet) String() string { | |
| 124 | − resources := []string{} | |
| 125 | − for resource := range resourceSet { | |
| 126 | − resources = append(resources, resource.String()) | |
| 127 | − } | |
| 128 | − sort.Strings(resources) | |
| 129 | − | |
| 130 | − return fmt.Sprintf("%v", resources) | |
| 131 | −} | |
| 132 | − | |
| 133 | −// UnmarshalJSON - decodes JSON data to ResourceSet. | |
| 134 | −func (resourceSet *ResourceSet) UnmarshalJSON(data []byte) error { | |
| 135 | − var sset set.StringSet | |
| 136 | − if err := json.Unmarshal(data, &sset); err != nil { | |
| 137 | − return err | |
| 138 | − } | |
| 139 | − | |
| 140 | − *resourceSet = make(ResourceSet) | |
| 141 | − for _, s := range sset.ToSlice() { | |
| 142 | − resource, err := parseResource(s) | |
| 143 | − if err != nil { | |
| 144 | − return err | |
| 145 | − } | |
| 146 | − | |
| 147 | − if _, found := (*resourceSet)[resource]; found { | |
| 148 | − return Errorf("duplicate resource '%v' found", s) | |
| 149 | − } | |
| 150 | − | |
| 151 | − resourceSet.Add(resource) | |
| 152 | − } | |
| 153 | − | |
| 154 | − return nil | |
| 155 | −} | |
| 156 | − | |
| 157 | −// Validate - validates ResourceSet. | |
| 158 | −func (resourceSet ResourceSet) Validate() error { | |
| 159 | − for resource := range resourceSet { | |
| 160 | − if err := resource.Validate(); err != nil { | |
| 161 | − return err | |
| 162 | − } | |
| 163 | − } | |
| 164 | − | |
| 165 | − return nil | |
| 166 | −} | |
| 167 | − | |
| 168 | −// ToSlice - returns slice of resources from the resource set. | |
| 169 | −func (resourceSet ResourceSet) ToSlice() []Resource { | |
| 170 | − resources := []Resource{} | |
| 171 | − for resource := range resourceSet { | |
| 172 | − resources = append(resources, resource) | |
| 173 | − } | |
| 174 | − | |
| 175 | − return resources | |
| 176 | −} | |
| 177 | − | |
| 178 | −// Clone clones ResourceSet structure | |
| 179 | −func (resourceSet ResourceSet) Clone() ResourceSet { | |
| 180 | − return NewResourceSet(resourceSet.ToSlice()...) | |
| 181 | −} | |
| 182 | − | |
| 183 | −// NewResourceSet - creates new resource set. | |
| 184 | −func NewResourceSet(resources ...Resource) ResourceSet { | |
| 185 | − resourceSet := make(ResourceSet) | |
| 186 | − for _, resource := range resources { | |
| 187 | − resourceSet.Add(resource) | |
| 188 | − } | |
| 189 | − | |
| 190 | − return resourceSet | |
| 191 | −} | |
| @@ -1,243 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "fmt" | |
| 23 | − "reflect" | |
| 24 | − "testing" | |
| 25 | −) | |
| 26 | − | |
| 27 | −func TestResourceSetBucketResourceExists(t *testing.T) { | |
| 28 | − testCases := []struct { | |
| 29 | − resourceSet ResourceSet | |
| 30 | − expectedResult bool | |
| 31 | − }{ | |
| 32 | − {NewResourceSet(NewResource("*", "")), true}, | |
| 33 | − {NewResourceSet(NewResource("mybucket", "")), true}, | |
| 34 | − {NewResourceSet(NewResource("mybucket*", "")), true}, | |
| 35 | − {NewResourceSet(NewResource("mybucket?0", "")), true}, | |
| 36 | − {NewResourceSet(NewResource("mybucket", "/2010/photos/*"), NewResource("mybucket", "")), true}, | |
| 37 | − {NewResourceSet(NewResource("", "*")), false}, | |
| 38 | − {NewResourceSet(NewResource("*", "*")), false}, | |
| 39 | − {NewResourceSet(NewResource("mybucket", "*")), false}, | |
| 40 | − {NewResourceSet(NewResource("mybucket*", "/myobject")), false}, | |
| 41 | − {NewResourceSet(NewResource("mybucket?0", "/2010/photos/*")), false}, | |
| 42 | − } | |
| 43 | − | |
| 44 | − for i, testCase := range testCases { | |
| 45 | − result := testCase.resourceSet.bucketResourceExists() | |
| 46 | − | |
| 47 | − if result != testCase.expectedResult { | |
| 48 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 49 | − } | |
| 50 | − } | |
| 51 | −} | |
| 52 | − | |
| 53 | −func TestResourceSetObjectResourceExists(t *testing.T) { | |
| 54 | − testCases := []struct { | |
| 55 | − resourceSet ResourceSet | |
| 56 | − expectedResult bool | |
| 57 | − }{ | |
| 58 | − {NewResourceSet(NewResource("*", "")), true}, | |
| 59 | − {NewResourceSet(NewResource("mybucket*", "")), true}, | |
| 60 | − {NewResourceSet(NewResource("", "*")), true}, | |
| 61 | − {NewResourceSet(NewResource("*", "*")), true}, | |
| 62 | − {NewResourceSet(NewResource("mybucket", "*")), true}, | |
| 63 | − {NewResourceSet(NewResource("mybucket*", "/myobject")), true}, | |
| 64 | − {NewResourceSet(NewResource("mybucket?0", "/2010/photos/*")), true}, | |
| 65 | − {NewResourceSet(NewResource("mybucket", ""), NewResource("mybucket", "/2910/photos/*")), true}, | |
| 66 | − {NewResourceSet(NewResource("mybucket", "")), false}, | |
| 67 | − {NewResourceSet(NewResource("mybucket?0", "")), false}, | |
| 68 | − } | |
| 69 | − | |
| 70 | − for i, testCase := range testCases { | |
| 71 | − result := testCase.resourceSet.objectResourceExists() | |
| 72 | − | |
| 73 | − if result != testCase.expectedResult { | |
| 74 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 75 | − } | |
| 76 | − } | |
| 77 | −} | |
| 78 | − | |
| 79 | −func TestResourceSetAdd(t *testing.T) { | |
| 80 | − testCases := []struct { | |
| 81 | − resourceSet ResourceSet | |
| 82 | − resource Resource | |
| 83 | − expectedResult ResourceSet | |
| 84 | − }{ | |
| 85 | − {NewResourceSet(), NewResource("mybucket", "/myobject*"), | |
| 86 | − NewResourceSet(NewResource("mybucket", "/myobject*"))}, | |
| 87 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 88 | − NewResource("mybucket", "/yourobject*"), | |
| 89 | − NewResourceSet(NewResource("mybucket", "/myobject*"), | |
| 90 | − NewResource("mybucket", "/yourobject*"))}, | |
| 91 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 92 | − NewResource("mybucket", "/myobject*"), | |
| 93 | − NewResourceSet(NewResource("mybucket", "/myobject*"))}, | |
| 94 | − } | |
| 95 | − | |
| 96 | − for i, testCase := range testCases { | |
| 97 | − testCase.resourceSet.Add(testCase.resource) | |
| 98 | − | |
| 99 | − if !reflect.DeepEqual(testCase.resourceSet, testCase.expectedResult) { | |
| 100 | − t.Fatalf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, testCase.resourceSet) | |
| 101 | − } | |
| 102 | − } | |
| 103 | −} | |
| 104 | − | |
| 105 | −func TestResourceSetIntersection(t *testing.T) { | |
| 106 | − testCases := []struct { | |
| 107 | − set ResourceSet | |
| 108 | − setToIntersect ResourceSet | |
| 109 | − expectedResult ResourceSet | |
| 110 | − }{ | |
| 111 | − {NewResourceSet(), NewResourceSet(NewResource("mybucket", "/myobject*")), NewResourceSet()}, | |
| 112 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), NewResourceSet(), NewResourceSet()}, | |
| 113 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 114 | − NewResourceSet(NewResource("mybucket", "/myobject*"), NewResource("mybucket", "/yourobject*")), | |
| 115 | − NewResourceSet(NewResource("mybucket", "/myobject*"))}, | |
| 116 | − } | |
| 117 | − | |
| 118 | − for i, testCase := range testCases { | |
| 119 | − result := testCase.set.Intersection(testCase.setToIntersect) | |
| 120 | − | |
| 121 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 122 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, testCase.set) | |
| 123 | − } | |
| 124 | − } | |
| 125 | −} | |
| 126 | − | |
| 127 | −func TestResourceSetMarshalJSON(t *testing.T) { | |
| 128 | − testCases := []struct { | |
| 129 | − resoruceSet ResourceSet | |
| 130 | − expectedResult []byte | |
| 131 | − expectErr bool | |
| 132 | − }{ | |
| 133 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 134 | − []byte(`["arn:aws:s3:::mybucket/myobject*"]`), false}, | |
| 135 | − {NewResourceSet(NewResource("mybucket", "/photos/myobject*")), | |
| 136 | − []byte(`["arn:aws:s3:::mybucket/photos/myobject*"]`), false}, | |
| 137 | − {NewResourceSet(), nil, true}, | |
| 138 | − } | |
| 139 | − | |
| 140 | − for i, testCase := range testCases { | |
| 141 | − result, err := json.Marshal(testCase.resoruceSet) | |
| 142 | − expectErr := (err != nil) | |
| 143 | − | |
| 144 | − if expectErr != testCase.expectErr { | |
| 145 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 146 | − } | |
| 147 | − | |
| 148 | − if !testCase.expectErr { | |
| 149 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 150 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, string(testCase.expectedResult), string(result)) | |
| 151 | − } | |
| 152 | − } | |
| 153 | − } | |
| 154 | −} | |
| 155 | − | |
| 156 | −func TestResourceSetMatch(t *testing.T) { | |
| 157 | − testCases := []struct { | |
| 158 | − resourceSet ResourceSet | |
| 159 | − resource string | |
| 160 | − expectedResult bool | |
| 161 | − }{ | |
| 162 | − {NewResourceSet(NewResource("*", "")), "mybucket", true}, | |
| 163 | − {NewResourceSet(NewResource("*", "")), "mybucket/myobject", true}, | |
| 164 | − {NewResourceSet(NewResource("mybucket*", "")), "mybucket", true}, | |
| 165 | − {NewResourceSet(NewResource("mybucket*", "")), "mybucket/myobject", true}, | |
| 166 | − {NewResourceSet(NewResource("", "*")), "/myobject", true}, | |
| 167 | − {NewResourceSet(NewResource("*", "*")), "mybucket/myobject", true}, | |
| 168 | − {NewResourceSet(NewResource("mybucket", "*")), "mybucket/myobject", true}, | |
| 169 | − {NewResourceSet(NewResource("mybucket*", "/myobject")), "mybucket/myobject", true}, | |
| 170 | − {NewResourceSet(NewResource("mybucket*", "/myobject")), "mybucket100/myobject", true}, | |
| 171 | − {NewResourceSet(NewResource("mybucket?0", "/2010/photos/*")), "mybucket20/2010/photos/1.jpg", true}, | |
| 172 | − {NewResourceSet(NewResource("mybucket", "")), "mybucket", true}, | |
| 173 | − {NewResourceSet(NewResource("mybucket?0", "")), "mybucket30", true}, | |
| 174 | − {NewResourceSet(NewResource("mybucket?0", "/2010/photos/*"), | |
| 175 | − NewResource("mybucket", "/2010/photos/*")), "mybucket/2010/photos/1.jpg", true}, | |
| 176 | − {NewResourceSet(NewResource("", "*")), "mybucket/myobject", false}, | |
| 177 | − {NewResourceSet(NewResource("*", "*")), "mybucket", false}, | |
| 178 | − {NewResourceSet(NewResource("mybucket", "*")), "mybucket10/myobject", false}, | |
| 179 | − {NewResourceSet(NewResource("mybucket", "")), "mybucket/myobject", false}, | |
| 180 | − {NewResourceSet(), "mybucket/myobject", false}, | |
| 181 | − } | |
| 182 | − | |
| 183 | − for i, testCase := range testCases { | |
| 184 | − testCase := testCase | |
| 185 | − t.Run(fmt.Sprintf("Test%d", i+1), func(t *testing.T) { | |
| 186 | − result := testCase.resourceSet.Match(testCase.resource, nil) | |
| 187 | − if result != testCase.expectedResult { | |
| 188 | − t.Errorf("case %v: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 189 | − } | |
| 190 | − }) | |
| 191 | − } | |
| 192 | −} | |
| 193 | − | |
| 194 | −func TestResourceSetUnmarshalJSON(t *testing.T) { | |
| 195 | − testCases := []struct { | |
| 196 | − data []byte | |
| 197 | − expectedResult ResourceSet | |
| 198 | − expectErr bool | |
| 199 | − }{ | |
| 200 | − {[]byte(`"arn:aws:s3:::mybucket/myobject*"`), | |
| 201 | − NewResourceSet(NewResource("mybucket", "/myobject*")), false}, | |
| 202 | − {[]byte(`"arn:aws:s3:::mybucket/photos/myobject*"`), | |
| 203 | − NewResourceSet(NewResource("mybucket", "/photos/myobject*")), false}, | |
| 204 | − {[]byte(`"arn:aws:s3:::mybucket"`), NewResourceSet(NewResource("mybucket", "")), false}, | |
| 205 | − {[]byte(`"mybucket/myobject*"`), nil, true}, | |
| 206 | − } | |
| 207 | − | |
| 208 | − for i, testCase := range testCases { | |
| 209 | − var result ResourceSet | |
| 210 | − err := json.Unmarshal(testCase.data, &result) | |
| 211 | − expectErr := (err != nil) | |
| 212 | − | |
| 213 | − if expectErr != testCase.expectErr { | |
| 214 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 215 | − } | |
| 216 | − | |
| 217 | − if !testCase.expectErr { | |
| 218 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 219 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 220 | − } | |
| 221 | − } | |
| 222 | − } | |
| 223 | −} | |
| 224 | − | |
| 225 | −func TestResourceSetValidate(t *testing.T) { | |
| 226 | − testCases := []struct { | |
| 227 | − resourceSet ResourceSet | |
| 228 | − expectErr bool | |
| 229 | − }{ | |
| 230 | − {NewResourceSet(NewResource("mybucket", "/myobject*")), false}, | |
| 231 | − {NewResourceSet(NewResource("", "/myobject*")), false}, | |
| 232 | − {NewResourceSet(NewResource("", "")), true}, | |
| 233 | − } | |
| 234 | − | |
| 235 | − for i, testCase := range testCases { | |
| 236 | − err := testCase.resourceSet.Validate() | |
| 237 | − expectErr := (err != nil) | |
| 238 | − | |
| 239 | − if expectErr != testCase.expectErr { | |
| 240 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 241 | − } | |
| 242 | − } | |
| 243 | −} | |
| @@ -1,164 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "strings" | |
| 22 | − | |
| 23 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 24 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 25 | −) | |
| 26 | − | |
| 27 | −// Statement - iam policy statement. | |
| 28 | −type Statement struct { | |
| 29 | − SID policy.ID `json:"Sid,omitempty"` | |
| 30 | − Effect policy.Effect `json:"Effect"` | |
| 31 | − Actions ActionSet `json:"Action"` | |
| 32 | − Resources ResourceSet `json:"Resource,omitempty"` | |
| 33 | − Conditions condition.Functions `json:"Condition,omitempty"` | |
| 34 | −} | |
| 35 | − | |
| 36 | −// IsAllowed - checks given policy args is allowed to continue the Rest API. | |
| 37 | −func (statement Statement) IsAllowed(args Args) bool { | |
| 38 | − check := func() bool { | |
| 39 | − if !statement.Actions.Match(args.Action) { | |
| 40 | − return false | |
| 41 | − } | |
| 42 | − | |
| 43 | − resource := args.BucketName | |
| 44 | − if args.ObjectName != "" { | |
| 45 | − if !strings.HasPrefix(args.ObjectName, "/") { | |
| 46 | − resource += "/" | |
| 47 | − } | |
| 48 | − | |
| 49 | − resource += args.ObjectName | |
| 50 | − } else { | |
| 51 | − resource += "/" | |
| 52 | − } | |
| 53 | − | |
| 54 | − // For admin statements, resource match can be ignored. | |
| 55 | − if !statement.Resources.Match(resource, args.ConditionValues) && !statement.isAdmin() { | |
| 56 | − return false | |
| 57 | − } | |
| 58 | − | |
| 59 | − return statement.Conditions.Evaluate(args.ConditionValues) | |
| 60 | − } | |
| 61 | − | |
| 62 | − return statement.Effect.IsAllowed(check()) | |
| 63 | −} | |
| 64 | −func (statement Statement) isAdmin() bool { | |
| 65 | − for action := range statement.Actions { | |
| 66 | − if AdminAction(action).IsValid() { | |
| 67 | − return true | |
| 68 | − } | |
| 69 | − } | |
| 70 | − return false | |
| 71 | −} | |
| 72 | − | |
| 73 | −// isValid - checks whether statement is valid or not. | |
| 74 | −func (statement Statement) isValid() error { | |
| 75 | − if !statement.Effect.IsValid() { | |
| 76 | − return Errorf("invalid Effect %v", statement.Effect) | |
| 77 | − } | |
| 78 | − | |
| 79 | − if len(statement.Actions) == 0 { | |
| 80 | − return Errorf("Action must not be empty") | |
| 81 | − } | |
| 82 | − | |
| 83 | − if statement.isAdmin() { | |
| 84 | − if err := statement.Actions.ValidateAdmin(); err != nil { | |
| 85 | − return err | |
| 86 | − } | |
| 87 | − for action := range statement.Actions { | |
| 88 | − keys := statement.Conditions.Keys() | |
| 89 | − keyDiff := keys.Difference(adminActionConditionKeyMap[action]) | |
| 90 | − if !keyDiff.IsEmpty() { | |
| 91 | − return Errorf("unsupported condition keys '%v' used for action '%v'", keyDiff, action) | |
| 92 | − } | |
| 93 | − } | |
| 94 | − return nil | |
| 95 | − } | |
| 96 | − | |
| 97 | − if !statement.SID.IsValid() { | |
| 98 | − return Errorf("invalid SID %v", statement.SID) | |
| 99 | − } | |
| 100 | − | |
| 101 | − if len(statement.Resources) == 0 { | |
| 102 | − return Errorf("Resource must not be empty") | |
| 103 | − } | |
| 104 | − | |
| 105 | − if err := statement.Resources.Validate(); err != nil { | |
| 106 | − return err | |
| 107 | − } | |
| 108 | − | |
| 109 | − if err := statement.Actions.Validate(); err != nil { | |
| 110 | − return err | |
| 111 | − } | |
| 112 | − | |
| 113 | − for action := range statement.Actions { | |
| 114 | − if !statement.Resources.objectResourceExists() && !statement.Resources.bucketResourceExists() { | |
| 115 | − return Errorf("unsupported Resource found %v for action %v", statement.Resources, action) | |
| 116 | − } | |
| 117 | − | |
| 118 | − keys := statement.Conditions.Keys() | |
| 119 | − keyDiff := keys.Difference(iamActionConditionKeyMap.Lookup(action)) | |
| 120 | − if !keyDiff.IsEmpty() { | |
| 121 | − return Errorf("unsupported condition keys '%v' used for action '%v'", keyDiff, action) | |
| 122 | − } | |
| 123 | − } | |
| 124 | − | |
| 125 | − return nil | |
| 126 | −} | |
| 127 | − | |
| 128 | −// Validate - validates Statement is for given bucket or not. | |
| 129 | −func (statement Statement) Validate() error { | |
| 130 | − return statement.isValid() | |
| 131 | −} | |
| 132 | − | |
| 133 | −// Equals checks if two statements are equal | |
| 134 | −func (statement Statement) Equals(st Statement) bool { | |
| 135 | − if statement.Effect != st.Effect { | |
| 136 | − return false | |
| 137 | − } | |
| 138 | − if !statement.Actions.Equals(st.Actions) { | |
| 139 | − return false | |
| 140 | − } | |
| 141 | − if !statement.Resources.Equals(st.Resources) { | |
| 142 | − return false | |
| 143 | − } | |
| 144 | − if !statement.Conditions.Equals(st.Conditions) { | |
| 145 | − return false | |
| 146 | − } | |
| 147 | − return true | |
| 148 | −} | |
| 149 | − | |
| 150 | −// Clone clones Statement structure | |
| 151 | −func (statement Statement) Clone() Statement { | |
| 152 | − return NewStatement(statement.Effect, statement.Actions.Clone(), | |
| 153 | − statement.Resources.Clone(), statement.Conditions.Clone()) | |
| 154 | −} | |
| 155 | − | |
| 156 | −// NewStatement - creates new statement. | |
| 157 | −func NewStatement(effect policy.Effect, actionSet ActionSet, resourceSet ResourceSet, conditions condition.Functions) Statement { | |
| 158 | − return Statement{ | |
| 159 | − Effect: effect, | |
| 160 | − Actions: actionSet, | |
| 161 | − Resources: resourceSet, | |
| 162 | − Conditions: conditions, | |
| 163 | − } | |
| 164 | −} | |
| @@ -1,467 +0,0 @@ | ||
| 1 | −// Copyright (c) 2015-2021 MinIO, Inc. | |
| 2 | −// | |
| 3 | −// This file is part of MinIO Object Storage stack | |
| 4 | −// | |
| 5 | −// This program is free software: you can redistribute it and/or modify | |
| 6 | −// it under the terms of the GNU Affero General Public License as published by | |
| 7 | −// the Free Software Foundation, either version 3 of the License, or | |
| 8 | −// (at your option) any later version. | |
| 9 | −// | |
| 10 | −// This program is distributed in the hope that it will be useful | |
| 11 | −// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | −// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | −// GNU Affero General Public License for more details. | |
| 14 | −// | |
| 15 | −// You should have received a copy of the GNU Affero General Public License | |
| 16 | −// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | − | |
| 18 | −package iampolicy | |
| 19 | − | |
| 20 | −import ( | |
| 21 | − "encoding/json" | |
| 22 | − "net" | |
| 23 | − "reflect" | |
| 24 | − "testing" | |
| 25 | − | |
| 26 | − "github.com/minio/minio/pkg/bucket/policy" | |
| 27 | − "github.com/minio/minio/pkg/bucket/policy/condition" | |
| 28 | −) | |
| 29 | − | |
| 30 | −func TestStatementIsAllowed(t *testing.T) { | |
| 31 | − case1Statement := NewStatement( | |
| 32 | − policy.Allow, | |
| 33 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 34 | − NewResourceSet(NewResource("*", "")), | |
| 35 | − condition.NewFunctions(), | |
| 36 | − ) | |
| 37 | − | |
| 38 | − case2Statement := NewStatement( | |
| 39 | − policy.Allow, | |
| 40 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 41 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 42 | − condition.NewFunctions(), | |
| 43 | − ) | |
| 44 | − | |
| 45 | − _, IPNet1, err := net.ParseCIDR("192.168.1.0/24") | |
| 46 | − if err != nil { | |
| 47 | − t.Fatalf("unexpected error. %v\n", err) | |
| 48 | − } | |
| 49 | − func1, err := condition.NewIPAddressFunc( | |
| 50 | − condition.AWSSourceIP, | |
| 51 | − IPNet1, | |
| 52 | − ) | |
| 53 | − if err != nil { | |
| 54 | − t.Fatalf("unexpected error. %v\n", err) | |
| 55 | − } | |
| 56 | − | |
| 57 | − case3Statement := NewStatement( | |
| 58 | − policy.Allow, | |
| 59 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 60 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 61 | − condition.NewFunctions(func1), | |
| 62 | − ) | |
| 63 | − | |
| 64 | − case4Statement := NewStatement( | |
| 65 | − policy.Deny, | |
| 66 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 67 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 68 | − condition.NewFunctions(func1), | |
| 69 | − ) | |
| 70 | − | |
| 71 | − anonGetBucketLocationArgs := Args{ | |
| 72 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 73 | − Action: GetBucketLocationAction, | |
| 74 | − BucketName: "mybucket", | |
| 75 | − ConditionValues: map[string][]string{}, | |
| 76 | − } | |
| 77 | − | |
| 78 | − anonPutObjectActionArgs := Args{ | |
| 79 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 80 | − Action: PutObjectAction, | |
| 81 | − BucketName: "mybucket", | |
| 82 | − ConditionValues: map[string][]string{ | |
| 83 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 84 | − "SourceIp": {"192.168.1.10"}, | |
| 85 | − }, | |
| 86 | − ObjectName: "myobject", | |
| 87 | − } | |
| 88 | − | |
| 89 | − anonGetObjectActionArgs := Args{ | |
| 90 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 91 | − Action: GetObjectAction, | |
| 92 | − BucketName: "mybucket", | |
| 93 | − ConditionValues: map[string][]string{}, | |
| 94 | − ObjectName: "myobject", | |
| 95 | − } | |
| 96 | − | |
| 97 | − getBucketLocationArgs := Args{ | |
| 98 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 99 | − Action: GetBucketLocationAction, | |
| 100 | − BucketName: "mybucket", | |
| 101 | − ConditionValues: map[string][]string{}, | |
| 102 | − } | |
| 103 | − | |
| 104 | − putObjectActionArgs := Args{ | |
| 105 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 106 | − Action: PutObjectAction, | |
| 107 | − BucketName: "mybucket", | |
| 108 | − ConditionValues: map[string][]string{ | |
| 109 | − "x-amz-copy-source": {"mybucket/myobject"}, | |
| 110 | − "SourceIp": {"192.168.1.10"}, | |
| 111 | − }, | |
| 112 | − ObjectName: "myobject", | |
| 113 | − } | |
| 114 | − | |
| 115 | − getObjectActionArgs := Args{ | |
| 116 | − AccountName: "Q3AM3UQ867SPQQA43P2F", | |
| 117 | − Action: GetObjectAction, | |
| 118 | − BucketName: "mybucket", | |
| 119 | − ConditionValues: map[string][]string{}, | |
| 120 | − ObjectName: "myobject", | |
| 121 | − } | |
| 122 | − | |
| 123 | − testCases := []struct { | |
| 124 | − statement Statement | |
| 125 | − args Args | |
| 126 | − expectedResult bool | |
| 127 | − }{ | |
| 128 | − {case1Statement, anonGetBucketLocationArgs, true}, | |
| 129 | − {case1Statement, anonPutObjectActionArgs, true}, | |
| 130 | − {case1Statement, anonGetObjectActionArgs, false}, | |
| 131 | − {case1Statement, getBucketLocationArgs, true}, | |
| 132 | − {case1Statement, putObjectActionArgs, true}, | |
| 133 | − {case1Statement, getObjectActionArgs, false}, | |
| 134 | − | |
| 135 | − {case2Statement, anonGetBucketLocationArgs, false}, | |
| 136 | − {case2Statement, anonPutObjectActionArgs, true}, | |
| 137 | − {case2Statement, anonGetObjectActionArgs, true}, | |
| 138 | − {case2Statement, getBucketLocationArgs, false}, | |
| 139 | − {case2Statement, putObjectActionArgs, true}, | |
| 140 | − {case2Statement, getObjectActionArgs, true}, | |
| 141 | − | |
| 142 | − {case3Statement, anonGetBucketLocationArgs, false}, | |
| 143 | − {case3Statement, anonPutObjectActionArgs, true}, | |
| 144 | − {case3Statement, anonGetObjectActionArgs, false}, | |
| 145 | − {case3Statement, getBucketLocationArgs, false}, | |
| 146 | − {case3Statement, putObjectActionArgs, true}, | |
| 147 | − {case3Statement, getObjectActionArgs, false}, | |
| 148 | − | |
| 149 | − {case4Statement, anonGetBucketLocationArgs, true}, | |
| 150 | − {case4Statement, anonPutObjectActionArgs, false}, | |
| 151 | − {case4Statement, anonGetObjectActionArgs, true}, | |
| 152 | − {case4Statement, getBucketLocationArgs, true}, | |
| 153 | − {case4Statement, putObjectActionArgs, false}, | |
| 154 | − {case4Statement, getObjectActionArgs, true}, | |
| 155 | − } | |
| 156 | − | |
| 157 | − for i, testCase := range testCases { | |
| 158 | − result := testCase.statement.IsAllowed(testCase.args) | |
| 159 | − | |
| 160 | − if result != testCase.expectedResult { | |
| 161 | − t.Fatalf("case %v: expected: %v, got: %v\n", i+1, testCase.expectedResult, result) | |
| 162 | − } | |
| 163 | − } | |
| 164 | −} | |
| 165 | − | |
| 166 | −func TestStatementIsValid(t *testing.T) { | |
| 167 | − _, IPNet1, err := net.ParseCIDR("192.168.1.0/24") | |
| 168 | − if err != nil { | |
| 169 | − t.Fatalf("unexpected error. %v\n", err) | |
| 170 | − } | |
| 171 | − func1, err := condition.NewIPAddressFunc( | |
| 172 | − condition.AWSSourceIP, | |
| 173 | − IPNet1, | |
| 174 | − ) | |
| 175 | − if err != nil { | |
| 176 | − t.Fatalf("unexpected error. %v\n", err) | |
| 177 | − } | |
| 178 | − | |
| 179 | − func2, err := condition.NewStringEqualsFunc( | |
| 180 | − condition.S3XAmzCopySource, | |
| 181 | − "mybucket/myobject", | |
| 182 | − ) | |
| 183 | − if err != nil { | |
| 184 | − t.Fatalf("unexpected error. %v\n", err) | |
| 185 | − } | |
| 186 | − | |
| 187 | − func3, err := condition.NewStringEqualsFunc( | |
| 188 | − condition.AWSUserAgent, | |
| 189 | − "NSPlayer", | |
| 190 | − ) | |
| 191 | − if err != nil { | |
| 192 | − t.Fatalf("unexpected error. %v\n", err) | |
| 193 | − } | |
| 194 | − | |
| 195 | − testCases := []struct { | |
| 196 | − statement Statement | |
| 197 | − expectErr bool | |
| 198 | − }{ | |
| 199 | − // Invalid effect error. | |
| 200 | − {NewStatement( | |
| 201 | − policy.Effect("foo"), | |
| 202 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 203 | − NewResourceSet(NewResource("*", "")), | |
| 204 | − condition.NewFunctions(), | |
| 205 | − ), true}, | |
| 206 | − // Empty actions error. | |
| 207 | − {NewStatement( | |
| 208 | − policy.Allow, | |
| 209 | − NewActionSet(), | |
| 210 | − NewResourceSet(NewResource("*", "")), | |
| 211 | − condition.NewFunctions(), | |
| 212 | − ), true}, | |
| 213 | − // Empty resources error. | |
| 214 | − {NewStatement( | |
| 215 | − policy.Allow, | |
| 216 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 217 | − NewResourceSet(), | |
| 218 | − condition.NewFunctions(), | |
| 219 | − ), true}, | |
| 220 | − // Unsupported conditions for GetObject | |
| 221 | − {NewStatement( | |
| 222 | − policy.Allow, | |
| 223 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 224 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 225 | − condition.NewFunctions(func1, func2), | |
| 226 | − ), true}, | |
| 227 | − {NewStatement( | |
| 228 | − policy.Allow, | |
| 229 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 230 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 231 | − condition.NewFunctions(), | |
| 232 | − ), false}, | |
| 233 | − {NewStatement( | |
| 234 | − policy.Allow, | |
| 235 | − NewActionSet(GetBucketLocationAction, PutObjectAction), | |
| 236 | − NewResourceSet(NewResource("mybucket", "")), | |
| 237 | − condition.NewFunctions(), | |
| 238 | − ), false}, | |
| 239 | − {NewStatement( | |
| 240 | − policy.Deny, | |
| 241 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 242 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 243 | − condition.NewFunctions(func1), | |
| 244 | − ), false}, | |
| 245 | − {NewStatement( | |
| 246 | − policy.Allow, | |
| 247 | − NewActionSet(CreateUserAdminAction, DeleteUserAdminAction), | |
| 248 | − nil, | |
| 249 | − condition.NewFunctions(func2, func3), | |
| 250 | − ), true}, | |
| 251 | − {NewStatement( | |
| 252 | − policy.Allow, | |
| 253 | − NewActionSet(CreateUserAdminAction, DeleteUserAdminAction), | |
| 254 | − nil, | |
| 255 | − condition.NewFunctions(), | |
| 256 | − ), false}, | |
| 257 | − } | |
| 258 | − | |
| 259 | − for i, testCase := range testCases { | |
| 260 | − err := testCase.statement.isValid() | |
| 261 | − expectErr := (err != nil) | |
| 262 | − | |
| 263 | − if expectErr != testCase.expectErr { | |
| 264 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 265 | − } | |
| 266 | − } | |
| 267 | −} | |
| 268 | − | |
| 269 | −func TestStatementUnmarshalJSONAndValidate(t *testing.T) { | |
| 270 | − case1Data := []byte(`{ | |
| 271 | − "Sid": "SomeId1", | |
| 272 | − "Effect": "Allow", | |
| 273 | − "Action": "s3:PutObject", | |
| 274 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 275 | −}`) | |
| 276 | − case1Statement := NewStatement( | |
| 277 | − policy.Allow, | |
| 278 | − NewActionSet(PutObjectAction), | |
| 279 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 280 | − condition.NewFunctions(), | |
| 281 | − ) | |
| 282 | − case1Statement.SID = "SomeId1" | |
| 283 | − | |
| 284 | − case2Data := []byte(`{ | |
| 285 | − "Effect": "Allow", | |
| 286 | − "Action": "s3:PutObject", | |
| 287 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 288 | − "Condition": { | |
| 289 | − "Null": { | |
| 290 | − "s3:x-amz-copy-source": true | |
| 291 | − } | |
| 292 | − } | |
| 293 | −}`) | |
| 294 | − func1, err := condition.NewNullFunc( | |
| 295 | − condition.S3XAmzCopySource, | |
| 296 | − true, | |
| 297 | − ) | |
| 298 | − if err != nil { | |
| 299 | − t.Fatalf("unexpected error. %v\n", err) | |
| 300 | − } | |
| 301 | − case2Statement := NewStatement( | |
| 302 | − policy.Allow, | |
| 303 | − NewActionSet(PutObjectAction), | |
| 304 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 305 | − condition.NewFunctions(func1), | |
| 306 | − ) | |
| 307 | − | |
| 308 | − case3Data := []byte(`{ | |
| 309 | − "Effect": "Deny", | |
| 310 | − "Action": [ | |
| 311 | − "s3:PutObject", | |
| 312 | − "s3:GetObject" | |
| 313 | − ], | |
| 314 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 315 | − "Condition": { | |
| 316 | − "Null": { | |
| 317 | − "s3:x-amz-server-side-encryption": "false" | |
| 318 | − } | |
| 319 | − } | |
| 320 | −}`) | |
| 321 | − func2, err := condition.NewNullFunc( | |
| 322 | − condition.S3XAmzServerSideEncryption, | |
| 323 | − false, | |
| 324 | − ) | |
| 325 | − if err != nil { | |
| 326 | − t.Fatalf("unexpected error. %v\n", err) | |
| 327 | − } | |
| 328 | − case3Statement := NewStatement( | |
| 329 | − policy.Deny, | |
| 330 | − NewActionSet(PutObjectAction, GetObjectAction), | |
| 331 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 332 | − condition.NewFunctions(func2), | |
| 333 | − ) | |
| 334 | − | |
| 335 | − case4Data := []byte(`{ | |
| 336 | − "Effect": "Allow", | |
| 337 | − "Action": "s3:PutObjec, | |
| 338 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 339 | −}`) | |
| 340 | − | |
| 341 | − case5Data := []byte(`{ | |
| 342 | − "Action": "s3:PutObject", | |
| 343 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 344 | −}`) | |
| 345 | − | |
| 346 | − case7Data := []byte(`{ | |
| 347 | − "Effect": "Allow", | |
| 348 | − "Resource": "arn:aws:s3:::mybucket/myobject*" | |
| 349 | −}`) | |
| 350 | − | |
| 351 | − case8Data := []byte(`{ | |
| 352 | − "Effect": "Allow", | |
| 353 | − "Action": "s3:PutObject" | |
| 354 | −}`) | |
| 355 | − | |
| 356 | − case9Data := []byte(`{ | |
| 357 | − "Effect": "Allow", | |
| 358 | − "Action": "s3:PutObject", | |
| 359 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 360 | − "Condition": { | |
| 361 | − } | |
| 362 | −}`) | |
| 363 | − | |
| 364 | − case10Data := []byte(`{ | |
| 365 | − "Effect": "Deny", | |
| 366 | − "Action": [ | |
| 367 | − "s3:PutObject", | |
| 368 | − "s3:GetObject" | |
| 369 | − ], | |
| 370 | − "Resource": "arn:aws:s3:::mybucket/myobject*", | |
| 371 | − "Condition": { | |
| 372 | − "StringEquals": { | |
| 373 | − "s3:x-amz-copy-source": "yourbucket/myobject*" | |
| 374 | − } | |
| 375 | − } | |
| 376 | −}`) | |
| 377 | − | |
| 378 | − testCases := []struct { | |
| 379 | − data []byte | |
| 380 | − expectedResult Statement | |
| 381 | − expectUnmarshalErr bool | |
| 382 | − expectValidationErr bool | |
| 383 | − }{ | |
| 384 | − {case1Data, case1Statement, false, false}, | |
| 385 | − {case2Data, case2Statement, false, false}, | |
| 386 | − {case3Data, case3Statement, false, false}, | |
| 387 | − // JSON unmarshaling error. | |
| 388 | − {case4Data, Statement{}, true, true}, | |
| 389 | − // Invalid effect error. | |
| 390 | − {case5Data, Statement{}, false, true}, | |
| 391 | − // Empty action error. | |
| 392 | − {case7Data, Statement{}, false, true}, | |
| 393 | − // Empty resource error. | |
| 394 | − {case8Data, Statement{}, false, true}, | |
| 395 | − // Empty condition error. | |
| 396 | − {case9Data, Statement{}, true, false}, | |
| 397 | − // Unsupported condition key error. | |
| 398 | − {case10Data, Statement{}, false, true}, | |
| 399 | − } | |
| 400 | − | |
| 401 | − for i, testCase := range testCases { | |
| 402 | − var result Statement | |
| 403 | − expectErr := (json.Unmarshal(testCase.data, &result) != nil) | |
| 404 | − | |
| 405 | − if expectErr != testCase.expectUnmarshalErr { | |
| 406 | − t.Fatalf("case %v: error during unmarshal: expected: %v, got: %v", i+1, testCase.expectUnmarshalErr, expectErr) | |
| 407 | − } | |
| 408 | − | |
| 409 | − expectErr = (result.Validate() != nil) | |
| 410 | − if expectErr != testCase.expectValidationErr { | |
| 411 | − t.Fatalf("case %v: error during validation: expected: %v, got: %v", i+1, testCase.expectValidationErr, expectErr) | |
| 412 | − } | |
| 413 | − | |
| 414 | − if !testCase.expectUnmarshalErr && !testCase.expectValidationErr { | |
| 415 | − if !reflect.DeepEqual(result, testCase.expectedResult) { | |
| 416 | − t.Fatalf("case %v: result: expected: %v, got: %v", i+1, testCase.expectedResult, result) | |
| 417 | − } | |
| 418 | − } | |
| 419 | − } | |
| 420 | −} | |
| 421 | − | |
| 422 | −func TestStatementValidate(t *testing.T) { | |
| 423 | − case1Statement := NewStatement( | |
| 424 | − policy.Allow, | |
| 425 | − NewActionSet(PutObjectAction), | |
| 426 | − NewResourceSet(NewResource("mybucket", "/myobject*")), | |
| 427 | − condition.NewFunctions(), | |
| 428 | − ) | |
| 429 | − | |
| 430 | − func1, err := condition.NewNullFunc( | |
| 431 | − condition.S3XAmzCopySource, | |
| 432 | − true, | |
| 433 | − ) | |
| 434 | − if err != nil { | |
| 435 | − t.Fatalf("unexpected error. %v\n", err) | |
| 436 | − } | |
| 437 | − func2, err := condition.NewNullFunc( | |
| 438 | − condition.S3XAmzServerSideEncryption, | |
| 439 | − false, | |
| 440 | − ) | |
| 441 | − if err != nil { | |
| 442 | − t.Fatalf("unexpected error. %v\n", err) | |
| 443 | − } | |
| 444 | − case2Statement := NewStatement( | |
| 445 | − policy.Allow, | |
| 446 | − NewActionSet(GetObjectAction, PutObjectAction), | |
| 447 | − NewResourceSet(NewResource("mybucket", "myobject*")), | |
| 448 | − condition.NewFunctions(func1, func2), | |
| 449 | − ) | |
| 450 | − | |
| 451 | − testCases := []struct { | |
| 452 | − statement Statement | |
| 453 | − expectErr bool | |
| 454 | − }{ | |
| 455 | − {case1Statement, false}, | |
| 456 | − {case2Statement, true}, | |
| 457 | − } | |
| 458 | − | |
| 459 | − for i, testCase := range testCases { | |
| 460 | − err := testCase.statement.Validate() | |
| 461 | − expectErr := (err != nil) | |
| 462 | − | |
| 463 | − if expectErr != testCase.expectErr { | |
| 464 | − t.Fatalf("case %v: error: expected: %v, got: %v", i+1, testCase.expectErr, expectErr) | |
| 465 | − } | |
| 466 | − } | |
| 467 | −} | |