2023-10-19 13:15:21 +00:00
|
|
|
package iam
|
|
|
|
|
|
|
|
import (
|
2023-11-28 14:56:36 +00:00
|
|
|
"encoding/json"
|
2023-11-10 14:56:41 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2023-10-19 13:15:21 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine/inmemory"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/resource/testutil"
|
2024-04-10 08:14:57 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/common"
|
2023-11-10 14:56:41 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/native"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/s3"
|
2023-10-19 13:15:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
type mockUserResolver struct {
|
2024-01-26 09:26:20 +00:00
|
|
|
users map[string]string
|
|
|
|
containers map[string]string
|
|
|
|
namespace string
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 09:26:20 +00:00
|
|
|
func newMockUserResolver(accountUsers []string, buckets []string, namespace string) *mockUserResolver {
|
2023-11-10 14:56:41 +00:00
|
|
|
userMap := make(map[string]string, len(accountUsers))
|
|
|
|
for _, user := range accountUsers {
|
|
|
|
userMap[user] = user + "/resolvedValue"
|
|
|
|
}
|
|
|
|
|
2024-01-26 09:26:20 +00:00
|
|
|
containerMap := make(map[string]string, len(buckets))
|
2023-11-10 14:56:41 +00:00
|
|
|
for _, bkt := range buckets {
|
2024-01-26 09:26:20 +00:00
|
|
|
containerMap[bkt] = bkt + "/resolvedValues"
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 09:26:20 +00:00
|
|
|
return &mockUserResolver{users: userMap, containers: containerMap, namespace: namespace}
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockUserResolver) GetUserAddress(account, user string) (string, error) {
|
|
|
|
key, ok := m.users[account+"/"+user]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockUserResolver) GetUserKey(account, user string) (string, error) {
|
|
|
|
key, ok := m.users[account+"/"+user]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
2024-01-26 09:26:20 +00:00
|
|
|
func (m *mockUserResolver) GetBucketInfo(bkt string) (*BucketInfo, error) {
|
|
|
|
cnr, ok := m.containers[bkt]
|
2023-11-10 14:56:41 +00:00
|
|
|
if !ok {
|
2024-01-26 09:26:20 +00:00
|
|
|
return nil, errors.New("not found")
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 09:26:20 +00:00
|
|
|
return &BucketInfo{Container: cnr, Namespace: m.namespace}, nil
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 13:15:21 +00:00
|
|
|
func TestConverters(t *testing.T) {
|
2023-11-10 14:56:41 +00:00
|
|
|
namespace := "root"
|
|
|
|
userName := "JohnDoe"
|
|
|
|
user := namespace + "/" + userName
|
|
|
|
principal := "arn:aws:iam::" + namespace + ":user/" + userName
|
|
|
|
bktName := "DOC-EXAMPLE-BUCKET"
|
|
|
|
objName := "object-name"
|
2023-12-19 07:35:14 +00:00
|
|
|
resource := fmt.Sprintf(s3.ResourceFormatS3BucketObjects, bktName)
|
2024-02-01 13:57:07 +00:00
|
|
|
s3GetObjectAction := "s3:GetObject"
|
|
|
|
s3HeadObjectAction := "s3:HeadObject"
|
2023-11-10 14:56:41 +00:00
|
|
|
|
2024-01-26 09:26:20 +00:00
|
|
|
mockResolver := newMockUserResolver([]string{user}, []string{bktName}, namespace)
|
2023-11-10 14:56:41 +00:00
|
|
|
|
2023-10-19 13:15:21 +00:00
|
|
|
t.Run("valid policy", func(t *testing.T) {
|
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
2023-10-30 13:34:53 +00:00
|
|
|
Principal: map[PrincipalType][]string{
|
2023-11-10 14:56:41 +00:00
|
|
|
AWSPrincipalType: {principal},
|
2023-10-19 13:15:21 +00:00
|
|
|
},
|
|
|
|
Effect: AllowEffect,
|
2024-02-01 13:57:07 +00:00
|
|
|
Action: []string{s3GetObjectAction},
|
2023-12-19 07:35:14 +00:00
|
|
|
Resource: []string{resource},
|
2023-10-19 13:15:21 +00:00
|
|
|
Conditions: map[string]Condition{
|
|
|
|
CondStringEquals: {
|
|
|
|
"s3:RequestObjectTag/Department": {"Finance"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
expected := &chain.Chain{Rules: []chain.Rule{
|
2023-10-19 13:15:21 +00:00
|
|
|
{
|
2023-11-07 17:20:54 +00:00
|
|
|
Status: chain.Allow,
|
2024-02-01 13:57:07 +00:00
|
|
|
Actions: chain.Actions{Names: []string{s3GetObjectAction, s3HeadObjectAction}},
|
2023-11-10 14:56:41 +00:00
|
|
|
Resources: chain.Resources{Names: []string{resource}},
|
2023-11-07 17:20:54 +00:00
|
|
|
Condition: []chain.Condition{
|
2023-10-19 13:15:21 +00:00
|
|
|
{
|
2023-11-07 17:20:54 +00:00
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
2023-11-10 14:56:41 +00:00
|
|
|
Key: s3.PropertyKeyOwner,
|
|
|
|
Value: mockResolver.users[user],
|
2023-10-19 13:15:21 +00:00
|
|
|
},
|
|
|
|
{
|
2023-11-07 17:20:54 +00:00
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
2023-10-19 13:15:21 +00:00
|
|
|
Key: "s3:RequestObjectTag/Department",
|
|
|
|
Value: "Finance",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
s3Chain, err := ConvertToS3Chain(p, mockResolver)
|
|
|
|
require.NoError(t, err)
|
2024-03-29 12:47:49 +00:00
|
|
|
assertChainsEqual(t, expected, s3Chain)
|
2023-11-10 14:56:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("valid native policy", func(t *testing.T) {
|
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
|
|
|
Principal: map[PrincipalType][]string{
|
|
|
|
AWSPrincipalType: {principal},
|
|
|
|
},
|
|
|
|
Effect: AllowEffect,
|
|
|
|
Action: []string{"s3:PutObject"},
|
2023-12-19 07:35:14 +00:00
|
|
|
Resource: []string{resource},
|
2023-11-10 14:56:41 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &chain.Chain{Rules: []chain.Rule{
|
|
|
|
{
|
2024-03-29 12:47:49 +00:00
|
|
|
Status: chain.Allow,
|
|
|
|
Actions: chain.Actions{Names: []string{native.MethodGetContainer, native.MethodPutObject}},
|
|
|
|
Resources: chain.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(native.ResourceFormatNamespaceContainerObjects, namespace, mockResolver.containers[bktName]),
|
|
|
|
fmt.Sprintf(native.ResourceFormatNamespaceContainer, namespace, mockResolver.containers[bktName])},
|
|
|
|
},
|
2023-11-10 14:56:41 +00:00
|
|
|
Condition: []chain.Condition{
|
|
|
|
{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: native.PropertyKeyActorPublicKey,
|
|
|
|
Value: mockResolver.users[user],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
nativeChain, err := ConvertToNativeChain(p, mockResolver)
|
2023-10-19 13:15:21 +00:00
|
|
|
require.NoError(t, err)
|
2024-03-29 12:47:49 +00:00
|
|
|
assertChainsEqual(t, expected, nativeChain)
|
2023-10-19 13:15:21 +00:00
|
|
|
})
|
|
|
|
|
2023-10-30 13:34:53 +00:00
|
|
|
t.Run("valid inverted policy", func(t *testing.T) {
|
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
|
|
|
NotPrincipal: map[PrincipalType][]string{
|
2023-11-10 14:56:41 +00:00
|
|
|
AWSPrincipalType: {principal},
|
2023-10-30 13:34:53 +00:00
|
|
|
},
|
|
|
|
Effect: DenyEffect,
|
2024-02-01 13:57:07 +00:00
|
|
|
NotAction: []string{s3GetObjectAction},
|
2023-12-19 07:35:14 +00:00
|
|
|
NotResource: []string{resource},
|
2023-10-30 13:34:53 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2023-11-07 17:20:54 +00:00
|
|
|
expected := &chain.Chain{Rules: []chain.Rule{
|
2023-10-30 13:34:53 +00:00
|
|
|
{
|
2023-11-07 17:20:54 +00:00
|
|
|
Status: chain.AccessDenied,
|
2024-02-01 13:57:07 +00:00
|
|
|
Actions: chain.Actions{Inverted: true, Names: []string{s3GetObjectAction, s3HeadObjectAction}},
|
2023-11-10 14:56:41 +00:00
|
|
|
Resources: chain.Resources{Inverted: true, Names: []string{resource}},
|
2023-11-07 17:20:54 +00:00
|
|
|
Condition: []chain.Condition{
|
2023-10-30 13:34:53 +00:00
|
|
|
{
|
2023-11-07 17:20:54 +00:00
|
|
|
Op: chain.CondStringNotEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
2023-11-10 14:56:41 +00:00
|
|
|
Key: s3.PropertyKeyOwner,
|
|
|
|
Value: mockResolver.users[user],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s3Chain, err := ConvertToS3Chain(p, mockResolver)
|
|
|
|
require.NoError(t, err)
|
2024-03-29 12:47:49 +00:00
|
|
|
assertChainsEqual(t, expected, s3Chain)
|
2023-11-10 14:56:41 +00:00
|
|
|
})
|
|
|
|
|
2024-01-26 12:43:48 +00:00
|
|
|
t.Run("valid native policy map action", func(t *testing.T) {
|
2023-11-10 14:56:41 +00:00
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
|
|
|
Principal: map[PrincipalType][]string{
|
|
|
|
AWSPrincipalType: {principal},
|
|
|
|
},
|
2024-03-29 12:47:49 +00:00
|
|
|
Effect: AllowEffect,
|
2024-01-26 12:43:48 +00:00
|
|
|
Action: []string{"s3:DeleteObject", "s3:DeleteBucket"},
|
|
|
|
Resource: []string{
|
|
|
|
fmt.Sprintf(s3.ResourceFormatS3BucketObject, bktName, objName),
|
|
|
|
fmt.Sprintf(s3.ResourceFormatS3Bucket, bktName),
|
|
|
|
},
|
2023-11-10 14:56:41 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := &chain.Chain{Rules: []chain.Rule{
|
|
|
|
{
|
2024-03-29 12:47:49 +00:00
|
|
|
Status: chain.Allow,
|
2024-04-10 11:45:31 +00:00
|
|
|
Actions: chain.Actions{Names: []string{native.MethodGetContainer, native.MethodDeleteContainer, native.MethodSearchObject, native.MethodHeadObject, native.MethodDeleteObject, native.MethodPutObject}},
|
2024-01-26 12:43:48 +00:00
|
|
|
Resources: chain.Resources{Names: []string{
|
2024-01-26 09:26:20 +00:00
|
|
|
fmt.Sprintf(native.ResourceFormatNamespaceContainerObjects, namespace, mockResolver.containers[bktName]),
|
2024-03-29 12:47:49 +00:00
|
|
|
fmt.Sprintf(native.ResourceFormatNamespaceContainer, namespace, mockResolver.containers[bktName]),
|
2023-11-10 14:56:41 +00:00
|
|
|
}},
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: native.PropertyKeyActorPublicKey,
|
|
|
|
Value: mockResolver.users[user],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Op: chain.CondStringLike,
|
|
|
|
Object: chain.ObjectResource,
|
|
|
|
Key: PropertyKeyFilePath,
|
|
|
|
Value: objName,
|
2023-10-30 13:34:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2024-01-26 12:43:48 +00:00
|
|
|
{
|
2024-03-29 12:47:49 +00:00
|
|
|
Status: chain.Allow,
|
2024-04-10 11:45:31 +00:00
|
|
|
Actions: chain.Actions{Names: []string{native.MethodGetContainer, native.MethodDeleteContainer, native.MethodSearchObject, native.MethodHeadObject, native.MethodDeleteObject, native.MethodPutObject}},
|
2024-01-26 12:43:48 +00:00
|
|
|
Resources: chain.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(native.ResourceFormatNamespaceContainer, namespace, mockResolver.containers[bktName]),
|
|
|
|
}},
|
|
|
|
Condition: []chain.Condition{{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: native.PropertyKeyActorPublicKey,
|
|
|
|
Value: mockResolver.users[user],
|
|
|
|
}},
|
|
|
|
},
|
2023-10-30 13:34:53 +00:00
|
|
|
}}
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
nativeChain, err := ConvertToNativeChain(p, mockResolver)
|
2023-10-30 13:34:53 +00:00
|
|
|
require.NoError(t, err)
|
2024-03-29 12:47:49 +00:00
|
|
|
assertChainsEqual(t, expected, nativeChain)
|
2023-10-30 13:34:53 +00:00
|
|
|
})
|
|
|
|
|
2023-10-19 13:15:21 +00:00
|
|
|
t.Run("invalid policy (unsupported principal type)", func(t *testing.T) {
|
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
2023-10-30 13:34:53 +00:00
|
|
|
Principal: map[PrincipalType][]string{
|
2023-11-10 14:56:41 +00:00
|
|
|
"dummy": {principal},
|
2023-10-19 13:15:21 +00:00
|
|
|
},
|
|
|
|
Effect: AllowEffect,
|
|
|
|
Action: []string{"s3:PutObject"},
|
|
|
|
Resource: []string{"arn:aws:s3:::DOC-EXAMPLE-BUCKET/*"},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
_, err := ConvertToS3Chain(p, mockResolver)
|
2023-10-19 13:15:21 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
2023-10-30 13:34:53 +00:00
|
|
|
t.Run("invalid policy (missing resource)", func(t *testing.T) {
|
2023-10-19 13:15:21 +00:00
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
2023-10-30 13:34:53 +00:00
|
|
|
Principal: map[PrincipalType][]string{
|
2023-11-10 14:56:41 +00:00
|
|
|
AWSPrincipalType: {principal},
|
2023-10-30 13:34:53 +00:00
|
|
|
},
|
|
|
|
Effect: AllowEffect,
|
|
|
|
Action: []string{"s3:PutObject"},
|
2023-10-19 13:15:21 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
_, err := ConvertToS3Chain(p, mockResolver)
|
2023-10-19 13:15:21 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
t.Run("invalid policy (not applicable native actions)", func(t *testing.T) {
|
2023-10-19 13:15:21 +00:00
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
2023-11-10 14:56:41 +00:00
|
|
|
Principal: map[PrincipalType][]string{
|
|
|
|
AWSPrincipalType: {principal},
|
2023-10-19 13:15:21 +00:00
|
|
|
},
|
2023-11-10 14:56:41 +00:00
|
|
|
Effect: AllowEffect,
|
|
|
|
Action: []string{"s3:AbortMultipartUpload"},
|
|
|
|
Resource: []string{"arn:aws:s3:::" + resource},
|
2023-10-19 13:15:21 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
_, err := ConvertToNativeChain(p, mockResolver)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2023-12-18 14:00:31 +00:00
|
|
|
|
2024-01-26 08:16:12 +00:00
|
|
|
t.Run("invalid policy (missing s3 actions)", func(t *testing.T) {
|
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
|
|
|
Principal: map[PrincipalType][]string{
|
|
|
|
AWSPrincipalType: {principal},
|
|
|
|
},
|
|
|
|
Effect: AllowEffect,
|
|
|
|
Resource: []string{"arn:aws:s3:::" + resource},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := ConvertToS3Chain(p, mockResolver)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
2023-12-18 14:00:31 +00:00
|
|
|
t.Run("valid mixed iam/s3 actions", func(t *testing.T) {
|
|
|
|
p := Policy{
|
2024-03-11 10:30:28 +00:00
|
|
|
Version: "2012-10-17",
|
2023-12-18 14:00:31 +00:00
|
|
|
Statement: []Statement{{
|
|
|
|
Principal: map[PrincipalType][]string{AWSPrincipalType: {principal}},
|
|
|
|
Effect: AllowEffect,
|
2024-02-01 13:57:07 +00:00
|
|
|
Action: []string{"s3:DeleteObject", "iam:*"},
|
2023-12-18 14:00:31 +00:00
|
|
|
Resource: []string{"*"},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
s3Expected := &chain.Chain{Rules: []chain.Rule{{
|
|
|
|
Status: chain.Allow,
|
2024-02-01 13:57:07 +00:00
|
|
|
Actions: chain.Actions{Names: []string{"s3:DeleteObject", "s3:DeleteMultipleObjects", "iam:*"}},
|
2023-12-18 14:00:31 +00:00
|
|
|
Resources: chain.Resources{Names: []string{"*"}},
|
|
|
|
Condition: []chain.Condition{{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: s3.PropertyKeyOwner,
|
|
|
|
Value: mockResolver.users[user],
|
|
|
|
}},
|
|
|
|
}}}
|
|
|
|
|
|
|
|
s3Chain, err := ConvertToS3Chain(p, mockResolver)
|
|
|
|
require.NoError(t, err)
|
2024-03-29 12:47:49 +00:00
|
|
|
assertChainsEqual(t, s3Expected, s3Chain)
|
2023-12-18 14:00:31 +00:00
|
|
|
|
|
|
|
nativeExpected := &chain.Chain{Rules: []chain.Rule{{
|
|
|
|
Status: chain.Allow,
|
2024-04-10 11:45:31 +00:00
|
|
|
Actions: chain.Actions{Names: []string{native.MethodGetContainer, native.MethodDeleteObject, native.MethodPutObject, native.MethodHeadObject}},
|
2024-03-29 12:47:49 +00:00
|
|
|
Resources: chain.Resources{Names: []string{native.ResourceFormatAllObjects, native.ResourceFormatAllContainers}},
|
2023-12-18 14:00:31 +00:00
|
|
|
Condition: []chain.Condition{{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: native.PropertyKeyActorPublicKey,
|
|
|
|
Value: mockResolver.users[user],
|
|
|
|
}},
|
|
|
|
}}}
|
|
|
|
|
|
|
|
nativeChain, err := ConvertToNativeChain(p, mockResolver)
|
|
|
|
require.NoError(t, err)
|
2024-03-29 12:47:49 +00:00
|
|
|
assertChainsEqual(t, nativeExpected, nativeChain)
|
2023-12-18 14:00:31 +00:00
|
|
|
})
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConvertToChainCondition(t *testing.T) {
|
|
|
|
principal := "arn:aws:iam::namespace:user/userName"
|
|
|
|
|
|
|
|
conditions := Conditions{
|
|
|
|
CondStringEquals: {"key1": {"val0", "val1"}},
|
|
|
|
CondStringNotEquals: {"key2": {"val2"}},
|
|
|
|
CondStringEqualsIgnoreCase: {"key3": {"val3"}},
|
|
|
|
CondStringNotEqualsIgnoreCase: {"key4": {"val4"}},
|
|
|
|
CondStringLike: {"key5": {"val5"}},
|
|
|
|
CondStringNotLike: {"key6": {"val6"}},
|
|
|
|
CondDateEquals: {"key7": {"2006-01-02T15:04:05+07:00"}},
|
|
|
|
CondDateNotEquals: {"key8": {"2006-01-02T15:04:05Z"}},
|
|
|
|
CondDateLessThan: {"key9": {"2006-01-02T15:04:05+06:00"}},
|
|
|
|
CondDateLessThanEquals: {"key10": {"2006-01-02T15:04:05+03:00"}},
|
|
|
|
CondDateGreaterThan: {"key11": {"2006-01-02T15:04:05-01:00"}},
|
|
|
|
CondDateGreaterThanEquals: {"key12": {"2006-01-02T15:04:05-03:00"}},
|
|
|
|
CondBool: {"key13": {"True"}},
|
|
|
|
CondIPAddress: {"key14": {"val14"}},
|
|
|
|
CondNotIPAddress: {"key15": {"val15"}},
|
|
|
|
CondArnEquals: {"key16": {"val16"}},
|
|
|
|
CondArnLike: {condKeyAWSPrincipalARN: {principal}},
|
|
|
|
CondArnNotEquals: {"key18": {"val18"}},
|
|
|
|
CondArnNotLike: {"key19": {"val19"}},
|
2024-04-01 14:27:45 +00:00
|
|
|
CondNumericEquals: {"key20": {"-20"}},
|
|
|
|
CondNumericNotEquals: {"key21": {"+21"}},
|
|
|
|
CondNumericLessThan: {"key22": {"0"}},
|
|
|
|
CondNumericLessThanEquals: {"key23": {"23.23"}},
|
|
|
|
CondNumericGreaterThan: {"key24": {"-24.24"}},
|
|
|
|
CondNumericGreaterThanEquals: {"key25": {"+25.25"}},
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expectedCondition := []GroupedConditions{
|
|
|
|
{
|
|
|
|
Any: true,
|
|
|
|
Conditions: []chain.Condition{
|
|
|
|
{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key1",
|
|
|
|
Value: "val0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key1",
|
|
|
|
Value: "val1",
|
2023-10-19 13:15:21 +00:00
|
|
|
},
|
|
|
|
},
|
2023-11-10 14:56:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringNotEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key2",
|
|
|
|
Value: "val2",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringEqualsIgnoreCase,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key3",
|
|
|
|
Value: "val3",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringNotEqualsIgnoreCase,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key4",
|
|
|
|
Value: "val4",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringLike,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key5",
|
|
|
|
Value: "val5",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringNotLike,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key6",
|
|
|
|
Value: "val6",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key7",
|
|
|
|
Value: "1136189045",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringNotEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key8",
|
|
|
|
Value: "1136214245",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringLessThan,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key9",
|
|
|
|
Value: "1136192645",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringLessThanEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key10",
|
|
|
|
Value: "1136203445",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringGreaterThan,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key11",
|
|
|
|
Value: "1136217845",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringGreaterThanEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key12",
|
|
|
|
Value: "1136225045",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringEqualsIgnoreCase,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key13",
|
|
|
|
Value: "True",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringLike,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key14",
|
|
|
|
Value: "val14",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringNotLike,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key15",
|
|
|
|
Value: "val15",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key16",
|
|
|
|
Value: "val16",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringLike,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: condKeyAWSPrincipalARN,
|
|
|
|
Value: principal,
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringNotEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key18",
|
|
|
|
Value: "val18",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondStringNotLike,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key19",
|
|
|
|
Value: "val19",
|
|
|
|
}},
|
|
|
|
},
|
2024-04-01 14:27:45 +00:00
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondNumericEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key20",
|
|
|
|
Value: "-20",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondNumericNotEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key21",
|
|
|
|
Value: "+21",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondNumericLessThan,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key22",
|
|
|
|
Value: "0",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondNumericLessThanEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key23",
|
|
|
|
Value: "23.23",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondNumericGreaterThan,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key24",
|
|
|
|
Value: "-24.24",
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Conditions: []chain.Condition{{
|
|
|
|
Op: chain.CondNumericGreaterThanEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: "key25",
|
|
|
|
Value: "+25.25",
|
|
|
|
}},
|
|
|
|
},
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
2023-10-19 13:15:21 +00:00
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
actualCondition, err := convertToChainCondition(conditions)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, expectedCondition, actualCondition)
|
2024-04-01 14:27:45 +00:00
|
|
|
|
|
|
|
invalidConditions := []Condition{
|
|
|
|
{"key1": {"invalid"}},
|
|
|
|
{"key2": {"1 2"}},
|
|
|
|
{"key3": {"0x12f"}},
|
|
|
|
{"key4": {"0b1010"}},
|
|
|
|
{"key5": {"+Inf"}},
|
|
|
|
{"key6": {"-Inf"}},
|
|
|
|
{"key7": {"inf"}},
|
|
|
|
{"key8": {"NaN"}},
|
|
|
|
{"key9": {"nan"}},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cond := range invalidConditions {
|
|
|
|
_, err = convertToChainCondition(Conditions{CondNumericEquals: cond})
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsePrincipalARN(t *testing.T) {
|
|
|
|
for i, tc := range []struct {
|
|
|
|
principal string
|
|
|
|
account string
|
|
|
|
user string
|
|
|
|
error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam::root:user/user",
|
|
|
|
account: "root",
|
|
|
|
user: "user",
|
|
|
|
error: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam::root:user/path/user/user2",
|
|
|
|
account: "root",
|
|
|
|
user: "user2",
|
|
|
|
error: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam::root:user/",
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "root:user/name",
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam::root:user",
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam::root:name",
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam::root:user/path/user/",
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
|
|
|
account, user, err := parsePrincipalAsIAMUser(tc.principal)
|
|
|
|
if tc.error {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.account, account)
|
|
|
|
require.Equal(t, tc.user, user)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestComplexNativeConditions(t *testing.T) {
|
|
|
|
namespace := "root"
|
|
|
|
userName1, userName2 := "user1", "user2"
|
|
|
|
user1, user2 := namespace+"/"+userName1, namespace+"/"+userName2
|
|
|
|
principal1 := "arn:aws:iam::" + namespace + ":user/" + userName1
|
|
|
|
principal2 := "arn:aws:iam::" + namespace + ":user/" + userName2
|
|
|
|
bktName1, bktName2, bktName3 := "bktName", "bktName2", "bktName3"
|
|
|
|
objName1 := "objName1"
|
|
|
|
resource1 := bktName1 + "/" + objName1
|
|
|
|
resource2 := bktName2 + "/*"
|
|
|
|
resource3 := bktName3 + "/*"
|
|
|
|
action := "PutObject"
|
|
|
|
|
|
|
|
key1, key2 := "key1", "key2"
|
|
|
|
val0, val1, val2 := "val0", "val1", "val2"
|
|
|
|
|
2024-01-26 09:26:20 +00:00
|
|
|
mockResolver := newMockUserResolver([]string{user1, user2}, []string{bktName1, bktName2, bktName3}, "")
|
|
|
|
nativeResource1 := fmt.Sprintf(native.ResourceFormatRootContainerObjects, mockResolver.containers[bktName1])
|
2024-03-29 12:47:49 +00:00
|
|
|
nativeResource1cnr := fmt.Sprintf(native.ResourceFormatRootContainer, mockResolver.containers[bktName1])
|
2024-01-26 09:26:20 +00:00
|
|
|
nativeResource2 := fmt.Sprintf(native.ResourceFormatRootContainerObjects, mockResolver.containers[bktName2])
|
2024-03-29 12:47:49 +00:00
|
|
|
nativeResource2cnr := fmt.Sprintf(native.ResourceFormatRootContainer, mockResolver.containers[bktName2])
|
2024-01-26 09:26:20 +00:00
|
|
|
nativeResource3 := fmt.Sprintf(native.ResourceFormatRootContainerObjects, mockResolver.containers[bktName3])
|
2024-03-29 12:47:49 +00:00
|
|
|
nativeResource3cnr := fmt.Sprintf(native.ResourceFormatRootContainer, mockResolver.containers[bktName3])
|
2023-11-10 14:56:41 +00:00
|
|
|
|
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
|
|
|
Principal: map[PrincipalType][]string{
|
|
|
|
AWSPrincipalType: {principal1, principal2},
|
|
|
|
},
|
2024-03-29 12:47:49 +00:00
|
|
|
Effect: AllowEffect,
|
2023-11-10 14:56:41 +00:00
|
|
|
Action: []string{"s3:" + action},
|
|
|
|
Resource: []string{"arn:aws:s3:::" + resource1, "arn:aws:s3:::" + resource2, "arn:aws:s3:::" + resource3},
|
|
|
|
Conditions: map[string]Condition{
|
|
|
|
CondStringEquals: {key1: {val0, val1}},
|
|
|
|
CondStringLike: {key2: {val2}},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
2024-03-29 12:47:49 +00:00
|
|
|
expectedStatus := chain.Allow
|
|
|
|
expectedActions := chain.Actions{Names: actionToNativeOpMap["s3:"+action]}
|
|
|
|
expectedResource1 := chain.Resources{Names: []string{nativeResource1, nativeResource1cnr}}
|
|
|
|
expectedResource23 := chain.Resources{Names: []string{nativeResource2, nativeResource2cnr, nativeResource3, nativeResource3cnr}}
|
2023-11-10 14:56:41 +00:00
|
|
|
|
|
|
|
user1Condition := chain.Condition{Op: chain.CondStringEquals, Object: chain.ObjectRequest, Key: native.PropertyKeyActorPublicKey, Value: mockResolver.users[user1]}
|
|
|
|
user2Condition := chain.Condition{Op: chain.CondStringEquals, Object: chain.ObjectRequest, Key: native.PropertyKeyActorPublicKey, Value: mockResolver.users[user2]}
|
|
|
|
objectName1Condition := chain.Condition{Op: chain.CondStringLike, Object: chain.ObjectResource, Key: PropertyKeyFilePath, Value: objName1}
|
|
|
|
key1val0Condition := chain.Condition{Op: chain.CondStringEquals, Object: chain.ObjectRequest, Key: key1, Value: val0}
|
|
|
|
key1val1Condition := chain.Condition{Op: chain.CondStringEquals, Object: chain.ObjectRequest, Key: key1, Value: val1}
|
|
|
|
key2val2Condition := chain.Condition{Op: chain.CondStringLike, Object: chain.ObjectRequest, Key: key2, Value: val2}
|
|
|
|
|
|
|
|
expected := &chain.Chain{Rules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResource1,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user1Condition,
|
|
|
|
objectName1Condition,
|
|
|
|
key1val0Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResource1,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user1Condition,
|
|
|
|
objectName1Condition,
|
|
|
|
key1val1Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResource1,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user2Condition,
|
|
|
|
objectName1Condition,
|
|
|
|
key1val0Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResource1,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user2Condition,
|
|
|
|
objectName1Condition,
|
|
|
|
key1val1Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResource23,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user1Condition,
|
|
|
|
key1val0Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResource23,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user1Condition,
|
|
|
|
key1val1Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResource23,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user2Condition,
|
|
|
|
key1val0Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResource23,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user2Condition,
|
|
|
|
key1val1Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
nativeChain, err := ConvertToNativeChain(p, mockResolver)
|
|
|
|
require.NoError(t, err)
|
|
|
|
requireChainRulesMatch(t, expected.Rules, nativeChain.Rules)
|
2023-10-19 13:15:21 +00:00
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
s := inmemory.NewInMemory()
|
2023-11-29 13:36:54 +00:00
|
|
|
_, _, err = s.MorphRuleChainStorage().AddMorphRuleChain("name", engine.NamespaceTarget("ns"), nativeChain)
|
2023-11-10 14:56:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
action string
|
|
|
|
resource string
|
|
|
|
resourceMap map[string]string
|
|
|
|
requestMap map[string]string
|
|
|
|
status chain.Status
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "bucket resource1, all conditions matched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName2], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: "any-object-name",
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
2024-03-29 12:47:49 +00:00
|
|
|
status: chain.Allow,
|
2023-11-10 14:56:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket resource3, all conditions matched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName3], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: "any-object-name",
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
2024-03-29 12:47:49 +00:00
|
|
|
status: chain.Allow,
|
2023-11-10 14:56:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket resource, user condition mismatched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName2], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: "any-object-name",
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket resource, key2 condition mismatched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName3], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: "any-object-name",
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val0,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket resource, key1 condition mismatched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName2], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: "any-object-name",
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: mockResolver.users[user1],
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, all conditions matched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName1], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: objName1,
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
2024-03-29 12:47:49 +00:00
|
|
|
status: chain.Allow,
|
2023-11-10 14:56:41 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, user condition mismatched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName1], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: objName1,
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: "dummy",
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, key1 condition mismatched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName1], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: objName1,
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: "dummy",
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, key2 condition mismatched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName1], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: objName1,
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: "dummy",
|
|
|
|
key1: val0,
|
|
|
|
key2: val0,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, object filepath condition mismatched",
|
|
|
|
action: action,
|
2024-01-26 09:26:20 +00:00
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, mockResolver.containers[bktName1], "some-oid"),
|
2023-11-10 14:56:41 +00:00
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: "any-object-name",
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "resource mismatched",
|
|
|
|
action: action,
|
|
|
|
resource: fmt.Sprintf(native.ResourceFormatRootContainerObject, "some-cid", "some-oid"),
|
|
|
|
resourceMap: map[string]string{
|
|
|
|
PropertyKeyFilePath: objName1,
|
|
|
|
},
|
|
|
|
requestMap: map[string]string{
|
|
|
|
native.PropertyKeyActorPublicKey: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := testutil.NewRequest(tc.action, testutil.NewResource(tc.resource, tc.resourceMap), tc.requestMap)
|
2023-12-01 15:12:57 +00:00
|
|
|
status, _, err := s.IsAllowed("name", engine.NewRequestTargetWithNamespace("ns"), req)
|
2023-11-10 14:56:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.status.String(), status.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestComplexS3Conditions(t *testing.T) {
|
|
|
|
namespace := "root"
|
|
|
|
userName1, userName2 := "user1", "user2"
|
|
|
|
user1, user2 := namespace+"/"+userName1, namespace+"/"+userName2
|
|
|
|
principal1 := "arn:aws:iam::" + namespace + ":user/" + userName1
|
|
|
|
principal2 := "arn:aws:iam::" + namespace + ":user/" + userName2
|
|
|
|
bktName1, bktName2, bktName3 := "bktName", "bktName2", "bktName3"
|
|
|
|
objName1 := "objName1"
|
2023-12-19 07:35:14 +00:00
|
|
|
resource1 := fmt.Sprintf(s3.ResourceFormatS3BucketObject, bktName1, objName1)
|
|
|
|
resource2 := fmt.Sprintf(s3.ResourceFormatS3BucketObjects, bktName2)
|
|
|
|
resource3 := fmt.Sprintf(s3.ResourceFormatS3BucketObjects, bktName3)
|
2024-02-01 13:57:07 +00:00
|
|
|
action := "s3:DeleteObject"
|
|
|
|
action2 := "s3:DeleteMultipleObjects"
|
2023-11-10 14:56:41 +00:00
|
|
|
|
|
|
|
key1, key2 := "key1", "key2"
|
|
|
|
val0, val1, val2 := "val0", "val1", "val2"
|
|
|
|
|
2024-01-26 09:26:20 +00:00
|
|
|
mockResolver := newMockUserResolver([]string{user1, user2}, []string{bktName1, bktName2, bktName3}, "")
|
2023-11-10 14:56:41 +00:00
|
|
|
|
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{{
|
|
|
|
Principal: map[PrincipalType][]string{
|
|
|
|
AWSPrincipalType: {principal1, principal2},
|
|
|
|
},
|
|
|
|
Effect: DenyEffect,
|
2023-12-18 14:00:31 +00:00
|
|
|
Action: []string{action},
|
2023-12-19 07:35:14 +00:00
|
|
|
Resource: []string{resource1, resource2, resource3},
|
2023-11-10 14:56:41 +00:00
|
|
|
Conditions: map[string]Condition{
|
|
|
|
CondStringEquals: {key1: {val0, val1}},
|
|
|
|
CondStringLike: {key2: {val2}},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedStatus := chain.AccessDenied
|
2024-02-01 13:57:07 +00:00
|
|
|
expectedActions := chain.Actions{Names: []string{action, action2}}
|
2023-11-10 14:56:41 +00:00
|
|
|
expectedResources := chain.Resources{Names: []string{resource1, resource2, resource3}}
|
|
|
|
|
|
|
|
user1Condition := chain.Condition{Op: chain.CondStringEquals, Object: chain.ObjectRequest, Key: s3.PropertyKeyOwner, Value: mockResolver.users[user1]}
|
|
|
|
user2Condition := chain.Condition{Op: chain.CondStringEquals, Object: chain.ObjectRequest, Key: s3.PropertyKeyOwner, Value: mockResolver.users[user2]}
|
|
|
|
key1val0Condition := chain.Condition{Op: chain.CondStringEquals, Object: chain.ObjectRequest, Key: key1, Value: val0}
|
|
|
|
key1val1Condition := chain.Condition{Op: chain.CondStringEquals, Object: chain.ObjectRequest, Key: key1, Value: val1}
|
|
|
|
key2val2Condition := chain.Condition{Op: chain.CondStringLike, Object: chain.ObjectRequest, Key: key2, Value: val2}
|
|
|
|
|
|
|
|
expected := &chain.Chain{Rules: []chain.Rule{
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResources,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user1Condition,
|
|
|
|
key1val0Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResources,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user1Condition,
|
|
|
|
key1val1Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResources,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user2Condition,
|
|
|
|
key1val0Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Status: expectedStatus,
|
|
|
|
Actions: expectedActions,
|
|
|
|
Resources: expectedResources,
|
|
|
|
Condition: []chain.Condition{
|
|
|
|
user2Condition,
|
|
|
|
key1val1Condition,
|
|
|
|
key2val2Condition,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
|
|
|
|
s3Chain, err := ConvertToS3Chain(p, mockResolver)
|
|
|
|
require.NoError(t, err)
|
|
|
|
requireChainRulesMatch(t, expected.Rules, s3Chain.Rules)
|
|
|
|
|
|
|
|
s := inmemory.NewInMemory()
|
2023-11-29 13:36:54 +00:00
|
|
|
_, _, err = s.MorphRuleChainStorage().AddMorphRuleChain("name", engine.NamespaceTarget("ns"), s3Chain)
|
2023-11-10 14:56:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
action string
|
|
|
|
resource string
|
|
|
|
resourceMap map[string]string
|
|
|
|
requestMap map[string]string
|
|
|
|
status chain.Status
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "bucket resource1, all conditions matched",
|
|
|
|
action: action,
|
|
|
|
resource: resource1,
|
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.AccessDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket resource3, all conditions matched",
|
|
|
|
action: action,
|
2023-12-19 07:35:14 +00:00
|
|
|
resource: fmt.Sprintf(s3.ResourceFormatS3BucketObject, bktName3, "some-obj"),
|
2023-11-10 14:56:41 +00:00
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.AccessDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket resource, user condition mismatched",
|
|
|
|
action: action,
|
2023-12-19 07:35:14 +00:00
|
|
|
resource: fmt.Sprintf(s3.ResourceFormatS3BucketObject, bktName2, "some-obj"),
|
2023-11-10 14:56:41 +00:00
|
|
|
requestMap: map[string]string{
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket resource, key2 condition mismatched",
|
|
|
|
action: action,
|
2023-12-19 07:35:14 +00:00
|
|
|
resource: fmt.Sprintf(s3.ResourceFormatS3BucketObject, bktName3, "some-obj"),
|
2023-11-10 14:56:41 +00:00
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val0,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket resource, key1 condition mismatched",
|
|
|
|
action: action,
|
2023-12-19 07:35:14 +00:00
|
|
|
resource: fmt.Sprintf(s3.ResourceFormatS3BucketObject, bktName2, "some-obj"),
|
2023-11-10 14:56:41 +00:00
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: mockResolver.users[user1],
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, all conditions matched",
|
|
|
|
action: action,
|
|
|
|
resource: resource1,
|
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.AccessDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, resource mismatched",
|
|
|
|
action: action,
|
2023-12-19 07:35:14 +00:00
|
|
|
resource: fmt.Sprintf(s3.ResourceFormatS3BucketObject, bktName1, "some-obj"),
|
2023-11-10 14:56:41 +00:00
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, user condition mismatched",
|
|
|
|
action: action,
|
|
|
|
resource: resource1,
|
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: "dummy",
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, key1 condition mismatched",
|
|
|
|
action: action,
|
|
|
|
resource: resource1,
|
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: "dummy",
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bucket/object resource, key2 condition mismatched",
|
|
|
|
action: action,
|
|
|
|
resource: resource1,
|
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: "dummy",
|
|
|
|
key1: val0,
|
|
|
|
key2: val0,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "resource mismatched",
|
|
|
|
action: action,
|
2023-12-19 07:35:14 +00:00
|
|
|
resource: fmt.Sprintf(s3.ResourceFormatS3BucketObject, "some-bkt", "some-obj"),
|
2023-11-10 14:56:41 +00:00
|
|
|
requestMap: map[string]string{
|
|
|
|
s3.PropertyKeyOwner: mockResolver.users[user1],
|
|
|
|
key1: val0,
|
|
|
|
key2: val2,
|
|
|
|
},
|
|
|
|
status: chain.NoRuleFound,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
req := testutil.NewRequest(tc.action, testutil.NewResource(tc.resource, tc.resourceMap), tc.requestMap)
|
2023-12-01 15:12:57 +00:00
|
|
|
status, _, err := s.IsAllowed("name", engine.NewRequestTargetWithNamespace("ns"), req)
|
2023-11-10 14:56:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.status.String(), status.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:43:47 +00:00
|
|
|
func TestS3BucketResource(t *testing.T) {
|
|
|
|
namespace := "ns"
|
|
|
|
bktName1, bktName2 := "bucket1", "bucket2"
|
|
|
|
chainName := chain.Name("name")
|
|
|
|
|
2024-01-26 09:26:20 +00:00
|
|
|
mockResolver := newMockUserResolver([]string{}, []string{}, "")
|
2023-12-11 11:43:47 +00:00
|
|
|
|
|
|
|
p := Policy{
|
|
|
|
Version: "2012-10-17",
|
|
|
|
Statement: []Statement{
|
|
|
|
{
|
|
|
|
Principal: map[PrincipalType][]string{Wildcard: nil},
|
|
|
|
Effect: DenyEffect,
|
2024-03-29 12:47:49 +00:00
|
|
|
Action: []string{"s3:ListBucket"},
|
2023-12-19 07:35:14 +00:00
|
|
|
Resource: []string{fmt.Sprintf(s3.ResourceFormatS3Bucket, bktName1)},
|
2023-12-11 11:43:47 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Principal: map[PrincipalType][]string{Wildcard: nil},
|
|
|
|
Effect: AllowEffect,
|
|
|
|
Action: []string{"*"},
|
2023-12-19 07:35:14 +00:00
|
|
|
Resource: []string{s3.ResourceFormatS3All},
|
2023-12-11 11:43:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
s3Chain, err := ConvertToS3Chain(p, mockResolver)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s := inmemory.NewInMemory()
|
|
|
|
_, _, err = s.MorphRuleChainStorage().AddMorphRuleChain(chainName, engine.NamespaceTarget(namespace), s3Chain)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// check we match just "bucket1" resource
|
2023-12-19 07:35:14 +00:00
|
|
|
req := testutil.NewRequest("s3:HeadBucket", testutil.NewResource(fmt.Sprintf(s3.ResourceFormatS3Bucket, bktName1), nil), nil)
|
2023-12-11 11:43:47 +00:00
|
|
|
status, _, err := s.IsAllowed(chainName, engine.NewRequestTargetWithNamespace(namespace), req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, chain.AccessDenied.String(), status.String())
|
|
|
|
|
|
|
|
// check we match just "bucket2" resource
|
2023-12-19 07:35:14 +00:00
|
|
|
req = testutil.NewRequest("s3:HeadBucket", testutil.NewResource(fmt.Sprintf(s3.ResourceFormatS3Bucket, bktName2), nil), nil)
|
2023-12-11 11:43:47 +00:00
|
|
|
status, _, err = s.IsAllowed(chainName, engine.NewRequestTargetWithNamespace(namespace), req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, chain.Allow.String(), status.String())
|
|
|
|
|
|
|
|
// check we also match "bucket2/object" resource
|
2023-12-19 07:35:14 +00:00
|
|
|
req = testutil.NewRequest("s3:PutObject", testutil.NewResource(fmt.Sprintf(s3.ResourceFormatS3BucketObject, bktName2, "object"), nil), nil)
|
2023-12-11 11:43:47 +00:00
|
|
|
status, _, err = s.IsAllowed(chainName, engine.NewRequestTargetWithNamespace(namespace), req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, chain.Allow.String(), status.String())
|
|
|
|
}
|
|
|
|
|
2023-11-28 14:56:36 +00:00
|
|
|
func TestWildcardConverters(t *testing.T) {
|
2024-03-29 12:47:49 +00:00
|
|
|
policy := `{"Version":"2012-10-17","Statement":{"Effect":"Allow", "Principal": "*", "Action":"s3:*","Resource":"*"}}`
|
2023-11-28 14:56:36 +00:00
|
|
|
|
|
|
|
var p Policy
|
|
|
|
err := json.Unmarshal([]byte(policy), &p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-01-26 09:54:39 +00:00
|
|
|
s3Expected := &chain.Chain{
|
|
|
|
Rules: []chain.Rule{{
|
|
|
|
Status: chain.Allow,
|
2024-03-29 12:47:49 +00:00
|
|
|
Actions: chain.Actions{Names: []string{"s3:*"}},
|
2024-01-26 09:54:39 +00:00
|
|
|
Resources: chain.Resources{Names: []string{Wildcard}},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
s3Chain, err := ConvertToS3Chain(p, newMockUserResolver(nil, nil, ""))
|
2023-11-28 14:56:36 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 09:54:39 +00:00
|
|
|
require.Equal(t, s3Expected, s3Chain)
|
|
|
|
|
|
|
|
nativeExpected := &chain.Chain{
|
|
|
|
Rules: []chain.Rule{{
|
|
|
|
Status: chain.Allow,
|
|
|
|
Actions: chain.Actions{Names: []string{Wildcard}},
|
|
|
|
Resources: chain.Resources{Names: []string{native.ResourceFormatAllObjects, native.ResourceFormatAllContainers}},
|
|
|
|
}},
|
|
|
|
}
|
2023-11-28 14:56:36 +00:00
|
|
|
|
2024-01-26 09:54:39 +00:00
|
|
|
nativeChain, err := ConvertToNativeChain(p, newMockUserResolver(nil, nil, ""))
|
2023-11-28 14:56:36 +00:00
|
|
|
require.NoError(t, err)
|
2024-01-26 09:54:39 +00:00
|
|
|
require.Equal(t, nativeExpected, nativeChain)
|
2023-11-28 14:56:36 +00:00
|
|
|
}
|
|
|
|
|
2024-03-29 12:47:49 +00:00
|
|
|
func TestWildcardObjectsConverters(t *testing.T) {
|
|
|
|
policy := `{"Version":"2012-10-17","Statement":{"Effect":"Allow", "Principal": "*", "Action":"s3:*","Resource":"arn:aws:s3:::bucket/*"}}`
|
|
|
|
|
|
|
|
var p Policy
|
|
|
|
err := json.Unmarshal([]byte(policy), &p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s3Expected := &chain.Chain{
|
|
|
|
Rules: []chain.Rule{{
|
|
|
|
Status: chain.Allow,
|
|
|
|
Actions: chain.Actions{Names: []string{"s3:*"}},
|
|
|
|
Resources: chain.Resources{Names: []string{"arn:aws:s3:::bucket/*"}},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
s3Chain, err := ConvertToS3Chain(p, newMockUserResolver(nil, nil, ""))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, s3Expected, s3Chain)
|
|
|
|
|
|
|
|
mockResolver := newMockUserResolver(nil, []string{"bucket"}, "")
|
|
|
|
|
|
|
|
nativeExpected := &chain.Chain{
|
|
|
|
Rules: []chain.Rule{{
|
|
|
|
Status: chain.Allow,
|
|
|
|
Actions: chain.Actions{Names: []string{Wildcard}},
|
|
|
|
Resources: chain.Resources{Names: []string{
|
|
|
|
fmt.Sprintf(native.ResourceFormatRootContainer, mockResolver.containers["bucket"]),
|
|
|
|
fmt.Sprintf(native.ResourceFormatRootContainerObjects, mockResolver.containers["bucket"]),
|
|
|
|
}},
|
|
|
|
}},
|
2023-11-28 14:56:36 +00:00
|
|
|
}
|
2024-03-29 12:47:49 +00:00
|
|
|
|
|
|
|
nativeChain, err := ConvertToNativeChain(p, mockResolver)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assertChainsEqual(t, nativeExpected, nativeChain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisableNativeDeny(t *testing.T) {
|
|
|
|
policy := `
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect": "Deny",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:*",
|
|
|
|
"Resource": [ "arn:aws:s3:::test-bucket/*" ]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
var p Policy
|
|
|
|
err := json.Unmarshal([]byte(policy), &p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = ConvertToNativeChain(p, newMockUserResolver(nil, nil, ""))
|
|
|
|
require.ErrorIs(t, err, ErrActionsNotApplicable)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromActions(t *testing.T) {
|
|
|
|
t.Run("s3 actions", func(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
action string
|
|
|
|
res []string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
action: "withoutPrefix",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:*Object",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "*",
|
|
|
|
res: []string{Wildcard},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:PutObject",
|
|
|
|
res: []string{"s3:PutObject", "s3:PostObject", "s3:CopyObject",
|
|
|
|
"s3:UploadPart", "s3:UploadPartCopy", "s3:CreateMultipartUpload", "s3:CompleteMultipartUpload"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:Put*",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:*",
|
|
|
|
res: []string{"s3:*"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "iam:ListAccessKeys",
|
|
|
|
res: []string{"iam:ListAccessKeys"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "iam:*",
|
|
|
|
res: []string{"iam:*"},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
actions, err := formS3ActionNames([]string{tc.action})
|
|
|
|
if tc.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, tc.res, actions)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("native actions", func(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
action string
|
|
|
|
res []string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
action: "withoutPrefix",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:*Object",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "*",
|
|
|
|
res: []string{Wildcard},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:PutObject",
|
|
|
|
res: []string{native.MethodGetContainer, native.MethodPutObject},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:Put*",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:*",
|
|
|
|
res: []string{Wildcard},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "s3:",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "iam:ListAccessKeys",
|
|
|
|
res: []string{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "iam:*",
|
|
|
|
res: []string{},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
actions, err := formNativeActionNames([]string{tc.action})
|
|
|
|
if tc.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.ElementsMatch(t, tc.res, actions)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2023-11-28 14:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrincipalParsing(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
principal string
|
|
|
|
expectedAccount string
|
|
|
|
expectedUser string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
principal: "withoutPrefix",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "*",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam:::dummy",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam::",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam:::dummy/test",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam:::user/",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam:::user/user/",
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam:::user/name",
|
|
|
|
expectedUser: "name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam:::user/path/name",
|
|
|
|
expectedUser: "name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
principal: "arn:aws:iam::root:user/path/name",
|
|
|
|
expectedAccount: "root",
|
|
|
|
expectedUser: "name",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
account, user, err := parsePrincipalAsIAMUser(tc.principal)
|
|
|
|
if tc.err {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expectedAccount, account)
|
|
|
|
require.Equal(t, tc.expectedUser, user)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResourceParsing(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
2023-12-19 07:35:14 +00:00
|
|
|
resource string
|
|
|
|
err bool
|
2023-11-28 14:56:36 +00:00
|
|
|
}{
|
2023-12-19 07:35:14 +00:00
|
|
|
{resource: "withoutPrefixAnd", err: true},
|
|
|
|
{resource: "arn:aws:s3:::*/obj", err: true},
|
|
|
|
{resource: "arn:aws:s3:::bkt/*"},
|
|
|
|
{resource: "arn:aws:s3:::bkt"},
|
|
|
|
{resource: "arn:aws:s3:::bkt/"},
|
|
|
|
{resource: "arn:aws:s3:::*"},
|
|
|
|
{resource: "*"},
|
2023-11-28 14:56:36 +00:00
|
|
|
} {
|
|
|
|
t.Run("", func(t *testing.T) {
|
2023-12-19 07:35:14 +00:00
|
|
|
err := validateResource(tc.resource)
|
2023-11-28 14:56:36 +00:00
|
|
|
if tc.err {
|
|
|
|
require.Error(t, err)
|
2023-12-19 07:35:14 +00:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2023-11-28 14:56:36 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 11:18:18 +00:00
|
|
|
func TestTagsConditions(t *testing.T) {
|
|
|
|
policy := `
|
|
|
|
{
|
|
|
|
"Version": "2012-10-17",
|
|
|
|
"Statement": [
|
|
|
|
{
|
|
|
|
"Effect": "Allow",
|
|
|
|
"Principal": "*",
|
|
|
|
"Action": "s3:PutObjectTagging",
|
|
|
|
"Resource": "*",
|
|
|
|
"Condition": {
|
|
|
|
"StringEquals": {
|
|
|
|
"aws:PrincipalTag/department": "hr",
|
|
|
|
"aws:ResourceTag/owner": "hr-admin",
|
2024-04-10 08:14:57 +00:00
|
|
|
"aws:RequestTag/scope": "*"
|
2024-04-04 11:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
expectedConditions := []chain.Condition{
|
|
|
|
{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
2024-04-10 08:14:57 +00:00
|
|
|
Key: fmt.Sprintf(common.PropertyKeyFormatFrostFSIDUserClaim, "tag-department"),
|
2024-04-04 11:18:18 +00:00
|
|
|
Value: "hr",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
2024-04-10 08:14:57 +00:00
|
|
|
Key: fmt.Sprintf(s3.PropertyKeyFormatResourceTag, "owner"),
|
2024-04-04 11:18:18 +00:00
|
|
|
Value: "hr-admin",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Op: chain.CondStringEquals,
|
|
|
|
Object: chain.ObjectRequest,
|
2024-04-10 08:14:57 +00:00
|
|
|
Key: fmt.Sprintf(s3.PropertyKeyFormatRequestTag, "scope"),
|
2024-04-04 11:18:18 +00:00
|
|
|
Value: "*",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var p Policy
|
|
|
|
err := json.Unmarshal([]byte(policy), &p)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s3Chain, err := ConvertToS3Chain(p, newMockUserResolver(nil, nil, ""))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, s3Chain.Rules, 1)
|
|
|
|
require.ElementsMatch(t, expectedConditions, s3Chain.Rules[0].Condition)
|
|
|
|
|
|
|
|
nativeChain, err := ConvertToNativeChain(p, newMockUserResolver(nil, nil, ""))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nativeChain.Rules, 1)
|
|
|
|
require.ElementsMatch(t, expectedConditions, nativeChain.Rules[0].Condition)
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
func requireChainRulesMatch(t *testing.T, expected, actual []chain.Rule) {
|
|
|
|
require.Equal(t, len(expected), len(actual), "length of chain rules differ")
|
|
|
|
|
|
|
|
seen := make(map[int]int)
|
|
|
|
for i, expRule := range expected {
|
|
|
|
for j, actRule := range actual {
|
|
|
|
if _, ok := seen[j]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if areRulesMatched(expRule, actRule) {
|
|
|
|
seen[j] = i
|
|
|
|
break
|
|
|
|
}
|
2023-10-19 13:15:21 +00:00
|
|
|
}
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require.Len(t, seen, len(expected), "expected unique rules")
|
|
|
|
}
|
|
|
|
|
|
|
|
func areRulesMatched(rule1, rule2 chain.Rule) bool {
|
|
|
|
if rule1.Status != rule2.Status ||
|
|
|
|
rule1.Any != rule2.Any ||
|
|
|
|
rule1.Resources.Inverted != rule2.Resources.Inverted ||
|
|
|
|
len(rule1.Resources.Names) != len(rule2.Resources.Names) ||
|
|
|
|
rule1.Actions.Inverted != rule2.Actions.Inverted ||
|
|
|
|
len(rule1.Actions.Names) != len(rule2.Actions.Names) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-03-29 12:47:49 +00:00
|
|
|
seen := make(map[int]struct{})
|
|
|
|
for _, name1 := range rule1.Resources.Names {
|
|
|
|
for j, name2 := range rule2.Resources.Names {
|
|
|
|
if _, ok := seen[j]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if name1 == name2 {
|
|
|
|
seen[j] = struct{}{}
|
|
|
|
break
|
|
|
|
}
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-29 12:47:49 +00:00
|
|
|
if len(seen) != len(rule1.Resources.Names) {
|
|
|
|
return false
|
|
|
|
}
|
2023-11-10 14:56:41 +00:00
|
|
|
|
2024-03-29 12:47:49 +00:00
|
|
|
seen = make(map[int]struct{})
|
|
|
|
for _, name1 := range rule1.Actions.Names {
|
|
|
|
for j, name2 := range rule2.Actions.Names {
|
|
|
|
if _, ok := seen[j]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if name1 == name2 {
|
|
|
|
seen[j] = struct{}{}
|
|
|
|
break
|
|
|
|
}
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-29 12:47:49 +00:00
|
|
|
if len(seen) != len(rule1.Actions.Names) {
|
|
|
|
return false
|
|
|
|
}
|
2023-11-10 14:56:41 +00:00
|
|
|
|
2024-03-29 12:47:49 +00:00
|
|
|
seen = make(map[int]struct{})
|
2023-11-10 14:56:41 +00:00
|
|
|
for _, cond1 := range rule1.Condition {
|
|
|
|
for j, cond2 := range rule2.Condition {
|
|
|
|
if _, ok := seen[j]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cond1 == cond2 {
|
|
|
|
seen[j] = struct{}{}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(seen) == len(rule1.Condition)
|
2023-10-19 13:15:21 +00:00
|
|
|
}
|
2024-03-29 12:47:49 +00:00
|
|
|
|
|
|
|
func assertChainsEqual(t *testing.T, chain1, chain2 *chain.Chain) {
|
|
|
|
require.Equal(t, string(chain1.ID), string(chain2.ID))
|
|
|
|
require.Equal(t, chain1.MatchType, chain2.MatchType)
|
|
|
|
require.Equal(t, len(chain1.Rules), len(chain2.Rules))
|
|
|
|
|
|
|
|
for i, rule := range chain1.Rules {
|
|
|
|
require.Equal(t, rule.Any, chain2.Rules[i].Any)
|
|
|
|
require.Equal(t, rule.Resources.Inverted, chain2.Rules[i].Resources.Inverted)
|
|
|
|
require.ElementsMatch(t, rule.Resources.Names, chain2.Rules[i].Resources.Names)
|
|
|
|
require.Equal(t, rule.Status, chain2.Rules[i].Status)
|
|
|
|
require.ElementsMatch(t, rule.Condition, chain2.Rules[i].Condition)
|
|
|
|
require.Equal(t, rule.Actions.Inverted, chain2.Rules[i].Actions.Inverted)
|
|
|
|
require.ElementsMatch(t, rule.Actions.Names, chain2.Rules[i].Actions.Names)
|
|
|
|
}
|
|
|
|
}
|