2023-11-10 14:56:41 +00:00
|
|
|
package iam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/s3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type S3Resolver interface {
|
|
|
|
GetUserAddress(account, user string) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConvertToS3Chain(p Policy, resolver S3Resolver) (*chain.Chain, error) {
|
|
|
|
if err := p.Validate(ResourceBasedPolicyType); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var engineChain chain.Chain
|
|
|
|
|
|
|
|
for _, statement := range p.Statement {
|
|
|
|
status := formStatus(statement)
|
|
|
|
|
2023-12-19 07:35:14 +00:00
|
|
|
actions, actionInverted := statement.action()
|
|
|
|
if err := validateS3ActionNames(actions); err != nil {
|
2023-11-28 14:56:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-19 07:35:14 +00:00
|
|
|
ruleAction := chain.Actions{Inverted: actionInverted, Names: actions}
|
2024-01-26 08:16:12 +00:00
|
|
|
if len(ruleAction.Names) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2023-11-10 14:56:41 +00:00
|
|
|
|
2023-12-19 07:35:14 +00:00
|
|
|
resources, resourceInverted := statement.resource()
|
|
|
|
if err := validateS3ResourceNames(resources); err != nil {
|
2023-11-28 14:56:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-19 07:35:14 +00:00
|
|
|
ruleResource := chain.Resources{Inverted: resourceInverted, Names: resources}
|
2023-11-10 14:56:41 +00:00
|
|
|
|
|
|
|
groupedConditions, err := convertToS3ChainCondition(statement.Conditions, resolver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
splitConditions := splitGroupedConditions(groupedConditions)
|
|
|
|
|
|
|
|
principals, principalCondFn, err := getS3PrincipalsAndConditionFunc(statement, resolver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, principal := range principals {
|
|
|
|
for _, conditions := range splitConditions {
|
|
|
|
r := chain.Rule{
|
|
|
|
Status: status,
|
|
|
|
Actions: ruleAction,
|
|
|
|
Resources: ruleResource,
|
|
|
|
Condition: append([]chain.Condition{principalCondFn(principal)}, conditions...),
|
|
|
|
}
|
|
|
|
engineChain.Rules = append(engineChain.Rules, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-26 08:16:12 +00:00
|
|
|
if len(engineChain.Rules) == 0 {
|
|
|
|
return nil, ErrActionsNotApplicable
|
|
|
|
}
|
|
|
|
|
2023-11-10 14:56:41 +00:00
|
|
|
return &engineChain, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getS3PrincipalsAndConditionFunc(statement Statement, resolver S3Resolver) ([]string, formPrincipalConditionFunc, error) {
|
|
|
|
var principals []string
|
|
|
|
var op chain.ConditionType
|
|
|
|
statementPrincipal, inverted := statement.principal()
|
|
|
|
if _, ok := statementPrincipal[Wildcard]; ok { // this can be true only if 'inverted' false
|
|
|
|
principals = []string{Wildcard}
|
|
|
|
op = chain.CondStringLike
|
|
|
|
} else {
|
|
|
|
for principalType, principal := range statementPrincipal {
|
|
|
|
if principalType != AWSPrincipalType {
|
|
|
|
return nil, nil, fmt.Errorf("unsupported principal type '%s'", principalType)
|
|
|
|
}
|
|
|
|
parsedPrincipal, err := formS3Principal(principal, resolver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("parse principal: %w", err)
|
|
|
|
}
|
|
|
|
principals = append(principals, parsedPrincipal...)
|
|
|
|
}
|
|
|
|
|
|
|
|
op = chain.CondStringEquals
|
|
|
|
if inverted {
|
|
|
|
op = chain.CondStringNotEquals
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return principals, func(principal string) chain.Condition {
|
|
|
|
return chain.Condition{
|
|
|
|
Op: op,
|
|
|
|
Object: chain.ObjectRequest,
|
|
|
|
Key: s3.PropertyKeyOwner,
|
|
|
|
Value: principal,
|
|
|
|
}
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertToS3ChainCondition(c Conditions, resolver S3Resolver) ([]GroupedConditions, error) {
|
|
|
|
return convertToChainConditions(c, func(gr GroupedConditions) (GroupedConditions, error) {
|
|
|
|
for i := range gr.Conditions {
|
|
|
|
if gr.Conditions[i].Key == condKeyAWSPrincipalARN {
|
|
|
|
gr.Conditions[i].Key = s3.PropertyKeyOwner
|
|
|
|
val, err := formPrincipalOwner(gr.Conditions[i].Value, resolver)
|
|
|
|
if err != nil {
|
|
|
|
return GroupedConditions{}, err
|
|
|
|
}
|
|
|
|
gr.Conditions[i].Value = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gr, nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func formS3Principal(principal []string, resolver S3Resolver) ([]string, error) {
|
|
|
|
res := make([]string, len(principal))
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for i := range principal {
|
|
|
|
if res[i], err = formPrincipalOwner(principal[i], resolver); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func formPrincipalOwner(principal string, resolver S3Resolver) (string, error) {
|
|
|
|
account, user, err := parsePrincipalAsIAMUser(principal)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
address, err := resolver.GetUserAddress(account, user)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("get user address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return address, nil
|
|
|
|
}
|
|
|
|
|
2023-12-19 07:35:14 +00:00
|
|
|
func validateS3ResourceNames(names []string) error {
|
2023-11-10 14:56:41 +00:00
|
|
|
for i := range names {
|
2023-12-19 07:35:14 +00:00
|
|
|
if err := validateResource(names[i]); err != nil {
|
|
|
|
return err
|
2023-11-28 14:56:36 +00:00
|
|
|
}
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 07:35:14 +00:00
|
|
|
return nil
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 07:35:14 +00:00
|
|
|
func validateS3ActionNames(names []string) error {
|
2023-11-10 14:56:41 +00:00
|
|
|
for i := range names {
|
2023-12-19 07:35:14 +00:00
|
|
|
if err := validateAction(names[i]); err != nil {
|
|
|
|
return err
|
2023-11-28 14:56:36 +00:00
|
|
|
}
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 07:35:14 +00:00
|
|
|
return nil
|
2023-11-10 14:56:41 +00:00
|
|
|
}
|