2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
package condition
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Key - conditional key which is used to fetch values for any condition.
|
|
|
|
// Refer https://docs.aws.amazon.com/IAM/latest/UserGuide/list_s3.html
|
|
|
|
// for more information about available condition keys.
|
|
|
|
type Key string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// S3XAmzCopySource - key representing x-amz-copy-source HTTP header applicable to PutObject API only.
|
|
|
|
S3XAmzCopySource Key = "s3:x-amz-copy-source"
|
|
|
|
|
|
|
|
// S3XAmzServerSideEncryption - key representing x-amz-server-side-encryption HTTP header applicable
|
|
|
|
// to PutObject API only.
|
2019-01-20 23:57:14 -05:00
|
|
|
S3XAmzServerSideEncryption Key = "s3:x-amz-server-side-encryption"
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
// S3XAmzServerSideEncryptionCustomerAlgorithm - key representing
|
|
|
|
// x-amz-server-side-encryption-customer-algorithm HTTP header applicable to PutObject API only.
|
2019-01-20 23:57:14 -05:00
|
|
|
S3XAmzServerSideEncryptionCustomerAlgorithm Key = "s3:x-amz-server-side-encryption-customer-algorithm"
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
// S3XAmzMetadataDirective - key representing x-amz-metadata-directive HTTP header applicable to
|
|
|
|
// PutObject API only.
|
2019-01-20 23:57:14 -05:00
|
|
|
S3XAmzMetadataDirective Key = "s3:x-amz-metadata-directive"
|
2018-04-24 18:53:30 -04:00
|
|
|
|
2020-04-01 03:04:25 -04:00
|
|
|
// S3XAmzContentSha256 - set a static content-sha256 for all calls for a given action.
|
|
|
|
S3XAmzContentSha256 = "s3:x-amz-content-sha256"
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
// S3XAmzStorageClass - key representing x-amz-storage-class HTTP header applicable to PutObject API
|
|
|
|
// only.
|
2019-01-20 23:57:14 -05:00
|
|
|
S3XAmzStorageClass Key = "s3:x-amz-storage-class"
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
// S3LocationConstraint - key representing LocationConstraint XML tag of CreateBucket API only.
|
2019-01-20 23:57:14 -05:00
|
|
|
S3LocationConstraint Key = "s3:LocationConstraint"
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
// S3Prefix - key representing prefix query parameter of ListBucket API only.
|
2019-01-20 23:57:14 -05:00
|
|
|
S3Prefix Key = "s3:prefix"
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
// S3Delimiter - key representing delimiter query parameter of ListBucket API only.
|
2019-01-20 23:57:14 -05:00
|
|
|
S3Delimiter Key = "s3:delimiter"
|
2018-04-24 18:53:30 -04:00
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// S3VersionID - Enables you to limit the permission for the
|
|
|
|
// s3:PutObjectVersionTagging action to a specific object version.
|
|
|
|
S3VersionID Key = "s3:versionid"
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
// S3MaxKeys - key representing max-keys query parameter of ListBucket API only.
|
2019-01-20 23:57:14 -05:00
|
|
|
S3MaxKeys Key = "s3:max-keys"
|
2018-04-24 18:53:30 -04:00
|
|
|
|
2020-04-01 03:04:25 -04:00
|
|
|
// S3ObjectLockRemainingRetentionDays - key representing object-lock-remaining-retention-days
|
|
|
|
// Enables enforcement of an object relative to the remaining retention days, you can set
|
|
|
|
// minimum and maximum allowable retention periods for a bucket using a bucket policy.
|
|
|
|
// This key are specific for s3:PutObjectRetention API.
|
|
|
|
S3ObjectLockRemainingRetentionDays Key = "s3:object-lock-remaining-retention-days"
|
|
|
|
|
|
|
|
// S3ObjectLockMode - key representing object-lock-mode
|
|
|
|
// Enables enforcement of the specified object retention mode
|
|
|
|
S3ObjectLockMode Key = "s3:object-lock-mode"
|
|
|
|
|
|
|
|
// S3ObjectLockRetainUntilDate - key representing object-lock-retain-util-date
|
|
|
|
// Enables enforcement of a specific retain-until-date
|
|
|
|
S3ObjectLockRetainUntilDate Key = "s3:object-lock-retain-until-date"
|
|
|
|
|
|
|
|
// S3ObjectLockLegalHold - key representing object-local-legal-hold
|
|
|
|
// Enables enforcement of the specified object legal hold status
|
|
|
|
S3ObjectLockLegalHold Key = "s3:object-lock-legal-hold"
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
// AWSReferer - key representing Referer header of any API.
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSReferer Key = "aws:Referer"
|
2018-04-24 18:53:30 -04:00
|
|
|
|
|
|
|
// AWSSourceIP - key representing client's IP address (not intermittent proxies) of any API.
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSSourceIP Key = "aws:SourceIp"
|
2018-12-26 20:39:30 -05:00
|
|
|
|
|
|
|
// AWSUserAgent - key representing UserAgent header for any API.
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSUserAgent Key = "aws:UserAgent"
|
2018-12-26 20:39:30 -05:00
|
|
|
|
|
|
|
// AWSSecureTransport - key representing if the clients request is authenticated or not.
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSSecureTransport Key = "aws:SecureTransport"
|
2018-12-28 15:18:58 -05:00
|
|
|
|
|
|
|
// AWSCurrentTime - key representing the current time.
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSCurrentTime Key = "aws:CurrentTime"
|
2018-12-28 15:18:58 -05:00
|
|
|
|
|
|
|
// AWSEpochTime - key representing the current epoch time.
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSEpochTime Key = "aws:EpochTime"
|
|
|
|
|
|
|
|
// AWSPrincipalType - user principal type currently supported values are "User" and "Anonymous".
|
|
|
|
AWSPrincipalType Key = "aws:principaltype"
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// AWSUserID - user unique ID, in MinIO this value is same as your user Access Key.
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSUserID Key = "aws:userid"
|
|
|
|
|
2019-04-09 14:39:42 -04:00
|
|
|
// AWSUsername - user friendly name, in MinIO this value is same as your user Access Key.
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSUsername Key = "aws:username"
|
2021-04-02 12:34:15 -04:00
|
|
|
|
|
|
|
// S3SignatureVersion - identifies the version of AWS Signature that you want to support for authenticated requests.
|
|
|
|
S3SignatureVersion = "s3:signatureversion"
|
|
|
|
|
|
|
|
// S3AuthType - optionally use this condition key to restrict incoming requests to use a specific authentication method.
|
|
|
|
S3AuthType = "s3:authType"
|
2018-04-24 18:53:30 -04:00
|
|
|
)
|
|
|
|
|
2018-12-26 20:39:30 -05:00
|
|
|
// AllSupportedKeys - is list of all all supported keys.
|
2020-03-05 08:05:36 -05:00
|
|
|
var AllSupportedKeys = append([]Key{
|
2021-04-02 12:34:15 -04:00
|
|
|
S3SignatureVersion,
|
|
|
|
S3AuthType,
|
2018-12-26 20:39:30 -05:00
|
|
|
S3XAmzCopySource,
|
|
|
|
S3XAmzServerSideEncryption,
|
2018-12-28 15:18:58 -05:00
|
|
|
S3XAmzServerSideEncryptionCustomerAlgorithm,
|
2018-12-26 20:39:30 -05:00
|
|
|
S3XAmzMetadataDirective,
|
|
|
|
S3XAmzStorageClass,
|
2020-04-01 03:04:25 -04:00
|
|
|
S3XAmzContentSha256,
|
2018-12-26 20:39:30 -05:00
|
|
|
S3LocationConstraint,
|
|
|
|
S3Prefix,
|
|
|
|
S3Delimiter,
|
|
|
|
S3MaxKeys,
|
2021-02-13 02:05:09 -05:00
|
|
|
S3VersionID,
|
2020-04-01 03:04:25 -04:00
|
|
|
S3ObjectLockRemainingRetentionDays,
|
|
|
|
S3ObjectLockMode,
|
|
|
|
S3ObjectLockLegalHold,
|
|
|
|
S3ObjectLockRetainUntilDate,
|
2018-12-26 20:39:30 -05:00
|
|
|
AWSReferer,
|
|
|
|
AWSSourceIP,
|
|
|
|
AWSUserAgent,
|
|
|
|
AWSSecureTransport,
|
2018-12-28 15:18:58 -05:00
|
|
|
AWSCurrentTime,
|
|
|
|
AWSEpochTime,
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSPrincipalType,
|
|
|
|
AWSUserID,
|
|
|
|
AWSUsername,
|
2020-08-17 20:39:55 -04:00
|
|
|
LDAPUser,
|
2018-12-26 20:39:30 -05:00
|
|
|
// Add new supported condition keys.
|
2020-03-05 08:05:36 -05:00
|
|
|
}, JWTKeys...)
|
2018-12-26 20:39:30 -05:00
|
|
|
|
2018-12-28 15:18:58 -05:00
|
|
|
// CommonKeys - is list of all common condition keys.
|
2020-03-05 08:05:36 -05:00
|
|
|
var CommonKeys = append([]Key{
|
2021-04-02 12:34:15 -04:00
|
|
|
S3SignatureVersion,
|
|
|
|
S3AuthType,
|
2021-02-13 02:05:09 -05:00
|
|
|
S3XAmzContentSha256,
|
|
|
|
S3LocationConstraint,
|
2018-12-28 15:18:58 -05:00
|
|
|
AWSReferer,
|
|
|
|
AWSSourceIP,
|
|
|
|
AWSUserAgent,
|
|
|
|
AWSSecureTransport,
|
|
|
|
AWSCurrentTime,
|
|
|
|
AWSEpochTime,
|
2019-01-20 23:57:14 -05:00
|
|
|
AWSPrincipalType,
|
|
|
|
AWSUserID,
|
|
|
|
AWSUsername,
|
2020-08-17 20:39:55 -04:00
|
|
|
LDAPUser,
|
2020-03-05 08:05:36 -05:00
|
|
|
}, JWTKeys...)
|
2019-01-20 23:57:14 -05:00
|
|
|
|
|
|
|
func substFuncFromValues(values map[string][]string) func(string) string {
|
|
|
|
return func(v string) string {
|
|
|
|
for _, key := range CommonKeys {
|
|
|
|
// Empty values are not supported for policy variables.
|
|
|
|
if rvalues, ok := values[key.Name()]; ok && rvalues[0] != "" {
|
|
|
|
v = strings.Replace(v, key.VarName(), rvalues[0], -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2018-12-28 15:18:58 -05:00
|
|
|
}
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
// IsValid - checks if key is valid or not.
|
|
|
|
func (key Key) IsValid() bool {
|
2018-12-26 20:39:30 -05:00
|
|
|
for _, supKey := range AllSupportedKeys {
|
|
|
|
if supKey == key {
|
|
|
|
return true
|
|
|
|
}
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON - encodes Key to JSON data.
|
|
|
|
func (key Key) MarshalJSON() ([]byte, error) {
|
|
|
|
if !key.IsValid() {
|
|
|
|
return nil, fmt.Errorf("unknown key %v", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(string(key))
|
|
|
|
}
|
|
|
|
|
2019-01-20 23:57:14 -05:00
|
|
|
// VarName - returns variable key name, such as "${aws:username}"
|
|
|
|
func (key Key) VarName() string {
|
|
|
|
return fmt.Sprintf("${%s}", key)
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
// Name - returns key name which is stripped value of prefixes "aws:" and "s3:"
|
|
|
|
func (key Key) Name() string {
|
|
|
|
keyString := string(key)
|
|
|
|
|
|
|
|
if strings.HasPrefix(keyString, "aws:") {
|
|
|
|
return strings.TrimPrefix(keyString, "aws:")
|
2019-10-16 11:59:59 -04:00
|
|
|
} else if strings.HasPrefix(keyString, "jwt:") {
|
|
|
|
return strings.TrimPrefix(keyString, "jwt:")
|
2020-08-17 20:39:55 -04:00
|
|
|
} else if strings.HasPrefix(keyString, "ldap:") {
|
|
|
|
return strings.TrimPrefix(keyString, "ldap:")
|
2018-04-24 18:53:30 -04:00
|
|
|
}
|
|
|
|
return strings.TrimPrefix(keyString, "s3:")
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON - decodes JSON data to Key.
|
|
|
|
func (key *Key) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedKey, err := parseKey(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*key = parsedKey
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseKey(s string) (Key, error) {
|
|
|
|
key := Key(s)
|
|
|
|
|
|
|
|
if key.IsValid() {
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, fmt.Errorf("invalid condition key '%v'", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// KeySet - set representation of slice of keys.
|
|
|
|
type KeySet map[Key]struct{}
|
|
|
|
|
|
|
|
// Add - add a key to key set.
|
|
|
|
func (set KeySet) Add(key Key) {
|
|
|
|
set[key] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2021-02-13 02:05:09 -05:00
|
|
|
// Merge merges two key sets, duplicates are overwritten
|
|
|
|
func (set KeySet) Merge(mset KeySet) {
|
|
|
|
for k, v := range mset {
|
|
|
|
set[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 18:53:30 -04:00
|
|
|
// Difference - returns a key set contains difference of two keys.
|
|
|
|
// Example:
|
|
|
|
// keySet1 := ["one", "two", "three"]
|
|
|
|
// keySet2 := ["two", "four", "three"]
|
|
|
|
// keySet1.Difference(keySet2) == ["one"]
|
|
|
|
func (set KeySet) Difference(sset KeySet) KeySet {
|
|
|
|
nset := make(KeySet)
|
|
|
|
|
|
|
|
for k := range set {
|
|
|
|
if _, ok := sset[k]; !ok {
|
|
|
|
nset.Add(k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nset
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty - returns whether key set is empty or not.
|
|
|
|
func (set KeySet) IsEmpty() bool {
|
|
|
|
return len(set) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (set KeySet) String() string {
|
|
|
|
return fmt.Sprintf("%v", set.ToSlice())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToSlice - returns slice of keys.
|
|
|
|
func (set KeySet) ToSlice() []Key {
|
|
|
|
keys := []Key{}
|
|
|
|
|
|
|
|
for key := range set {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeySet - returns new KeySet contains given keys.
|
|
|
|
func NewKeySet(keys ...Key) KeySet {
|
|
|
|
set := make(KeySet)
|
|
|
|
for _, key := range keys {
|
|
|
|
set.Add(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|
2019-11-19 05:03:18 -05:00
|
|
|
|
|
|
|
// AllSupportedAdminKeys - is list of all admin supported keys.
|
|
|
|
var AllSupportedAdminKeys = []Key{
|
|
|
|
AWSReferer,
|
|
|
|
AWSSourceIP,
|
|
|
|
AWSUserAgent,
|
|
|
|
AWSSecureTransport,
|
|
|
|
AWSCurrentTime,
|
|
|
|
AWSEpochTime,
|
|
|
|
// Add new supported condition keys.
|
|
|
|
}
|