Skip to content

Commit e4ba771

Browse files
committed
Create policy with unique paths
Signed-off-by: Victor Chang <vicchang@nvidia.com>
1 parent dbad738 commit e4ba771

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/S3Policy/PolicyExtensions.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ public static Policy ToPolicy(PolicyRequest[] policyRequests)
9999
Sid = "AllowUserToSeeBucketListInTheConsole",
100100
Action = new string[] {"s3:ListAllMyBuckets", "s3:GetBucketLocation" },
101101
Effect = "Allow",
102-
Resource = policyRequests.Select(pr => pr.BucketName).ToArray(),
102+
Resource = policyRequests.Select(pr => pr.BucketName).Distinct().ToArray(),
103103
},
104104
new Statement
105105
{
106106
Sid = "AllowRootAndHomeListingOfBucket",
107107
Action = new string[] { "s3:ListBucket" },
108108
Effect = "Allow",
109-
Resource = policyRequests.Select(pr => pr.BucketName).ToArray(),
109+
Resource = policyRequests.Select(pr => pr.BucketName).Distinct().ToArray(),
110110
Condition = new Condition
111111
{
112112
StringEquals = new StringEquals
113113
{
114-
S3Prefix = pathList.ToArray(),
114+
S3Prefix = pathList.Distinct().ToArray(),
115115
S3Delimiter = new string[] { "/" }
116116
}
117117
}
@@ -121,13 +121,15 @@ public static Policy ToPolicy(PolicyRequest[] policyRequests)
121121
Sid = "AllowListingOfUserFolder",
122122
Action = new string[] { "s3:ListBucket" },
123123
Effect = "Allow",
124-
Resource = policyRequests.Select(pr => pr.BucketName).ToArray(),
124+
Resource = policyRequests.Select(pr => pr.BucketName).Distinct().ToArray(),
125125
Condition = new Condition
126126
{
127127
StringLike = new StringLike
128128
{
129-
S3Prefix = policyRequests.Select(pr => $"{pr.FolderName}/*")
130-
.Union( policyRequests.Select(pr => $"{pr.FolderName}")).ToArray()
129+
S3Prefix = policyRequests
130+
.Select(pr => $"{pr.FolderName}/*")
131+
.Union( policyRequests.Select(pr => $"{pr.FolderName}"))
132+
.Distinct().ToArray()
131133
}
132134
}
133135
},
@@ -136,7 +138,10 @@ public static Policy ToPolicy(PolicyRequest[] policyRequests)
136138
Sid = "AllowAllS3ActionsInUserFolder",
137139
Action = new string[] { "s3:*" },
138140
Effect = "Allow",
139-
Resource = policyRequests.Select(pr => $"{pr.BucketName}/{pr.FolderName}/*").ToArray(),
141+
Resource = policyRequests
142+
.Select(pr => System.IO.Path.Join(pr.BucketName, pr.FolderName, "*"))
143+
.Distinct()
144+
.ToArray(),
140145
},
141146
}
142147
};

0 commit comments

Comments
 (0)