Skip to content

Commit 26e8500

Browse files
authored
Merge pull request #40 from Project-MONAI/vchang/fix-policy
Include the directory in the policy
2 parents 5d478db + fbe2db9 commit 26e8500

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/S3Policy/PolicyExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static Policy ToPolicy(string? bucketName, string? folderName)
7777
Sid = "AllowAllS3ActionsInUserFolder",
7878
Action = new string[] { "s3:*" },
7979
Effect = "Allow",
80-
Resource = new string[] { $"arn:aws:s3:::{bucketName}/{folderName}/*" },
80+
Resource = new string[] { $"arn:aws:s3:::{bucketName}/{folderName}", $"arn:aws:s3:::{bucketName}/{folderName}/*" },
8181
},
8282
}
8383
};
@@ -139,7 +139,7 @@ public static Policy ToPolicy(PolicyRequest[] policyRequests)
139139
Action = new string[] { "s3:*" },
140140
Effect = "Allow",
141141
Resource = policyRequests
142-
.Select(pr => $"{pr.BucketName}/{pr.FolderName}/*")
142+
.SelectMany(pr => new []{ $"{pr.BucketName}/{pr.FolderName}" , $"{pr.BucketName}/{pr.FolderName}/*" } )
143143
.Distinct()
144144
.ToArray(),
145145
},

src/S3Policy/Tests/Extensions/PolicyExtensionsTest.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void ToPolicy_ValidBucketAndFolder()
8686

8787
var policyString = JsonConvert.SerializeObject(policy, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
8888

89-
Assert.Equal("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"AllowUserToSeeBucketListInTheConsole\",\"Action\":[\"s3:ListAllMyBuckets\",\"s3:GetBucketLocation\"],\"Effect\":\"Allow\",\"Resource\":[\"arn:aws:s3:::*\"]},{\"Sid\":\"AllowRootAndHomeListingOfBucket\",\"Action\":[\"s3:ListBucket\"],\"Effect\":\"Allow\",\"Resource\":[\"arn:aws:s3:::test-bucket\"],\"Condition\":{\"StringEquals\":{\"s3:prefix\":[\"Jack/Is/The/Best\",\"Jack/Is/The/\",\"Jack/Is/\",\"Jack/\",\"\"],\"s3:delimiter\":[\"/\"]}}},{\"Sid\":\"AllowListingOfUserFolder\",\"Action\":[\"s3:ListBucket\"],\"Effect\":\"Allow\",\"Resource\":[\"arn:aws:s3:::test-bucket\"],\"Condition\":{\"StringEquals\":{\"s3:prefix\":[\"Jack/Is/The/Best/*\"]}}},{\"Sid\":\"AllowAllS3ActionsInUserFolder\",\"Action\":[\"s3:*\"],\"Effect\":\"Allow\",\"Resource\":[\"arn:aws:s3:::test-bucket/Jack/Is/The/Best/*\"]}]}", policyString);
89+
Assert.Equal("{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"AllowUserToSeeBucketListInTheConsole\",\"Action\":[\"s3:ListAllMyBuckets\",\"s3:GetBucketLocation\"],\"Effect\":\"Allow\",\"Resource\":[\"arn:aws:s3:::*\"]},{\"Sid\":\"AllowRootAndHomeListingOfBucket\",\"Action\":[\"s3:ListBucket\"],\"Effect\":\"Allow\",\"Resource\":[\"arn:aws:s3:::test-bucket\"],\"Condition\":{\"StringEquals\":{\"s3:prefix\":[\"Jack/Is/The/Best\",\"Jack/Is/The/\",\"Jack/Is/\",\"Jack/\",\"\"],\"s3:delimiter\":[\"/\"]}}},{\"Sid\":\"AllowListingOfUserFolder\",\"Action\":[\"s3:ListBucket\"],\"Effect\":\"Allow\",\"Resource\":[\"arn:aws:s3:::test-bucket\"],\"Condition\":{\"StringEquals\":{\"s3:prefix\":[\"Jack/Is/The/Best/*\"]}}},{\"Sid\":\"AllowAllS3ActionsInUserFolder\",\"Action\":[\"s3:*\"],\"Effect\":\"Allow\",\"Resource\":[\"arn:aws:s3:::test-bucket/Jack/Is/The/Best\",\"arn:aws:s3:::test-bucket/Jack/Is/The/Best/*\"]}]}", policyString);
9090
}
9191

9292
[Fact]
@@ -111,9 +111,10 @@ public async Task ToPolicy_Should_Set_Correct_Allow_All_Path()
111111

112112
var policyMade = PolicyExtensions.ToPolicy(policys);
113113

114-
Assert.EndsWith(
115-
$"{bucketName}/{payloadId}/*",
116-
policyMade.Statement.First(p => p.Sid == "AllowAllS3ActionsInUserFolder").Resource?.First());
114+
115+
Assert.Collection(policyMade.Statement.First(p => p.Sid == "AllowAllS3ActionsInUserFolder").Resource!,
116+
(item) => item.Equals($"{bucketName}/{payloadId}"),
117+
(item) => item.Equals($"{bucketName}/{payloadId}/*"));
117118

118119
}
119120

0 commit comments

Comments
 (0)