Skip to content

Commit 7ce382d

Browse files
authored
Merge pull request #8 from Project-MONAI/sto-112-generate-temp-cre
Storage - Generate temporary credentials (Links to #52) #112 #123
2 parents 66f4671 + 9e8f7e9 commit 7ce382d

File tree

24 files changed

+515
-44
lines changed

24 files changed

+515
-44
lines changed

src/.sonarlint/project-monai_monai-deploy-storage/CSharp/SonarLint.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,4 @@
8686
</Parameters>
8787
</Rule>
8888
</Rules>
89-
</AnalysisInput>
89+
</AnalysisInput>

src/Storage/Common/Extensions/PolicyExtensions.cs renamed to src/Monai.Deploy.Storage.Core/Extensions/PolicyExtensions.cs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// SPDX-License-Identifier: Apache License 2.0
33

44
using Ardalis.GuardClauses;
5-
using Monai.Deploy.Storage.Common.Policies;
5+
using Monai.Deploy.Storage.Core.Policies;
6+
using Newtonsoft.Json;
7+
using static Monai.Deploy.Storage.Core.Policies.Policy;
68

7-
namespace Monai.Deploy.Storage.Common.Extensions
9+
namespace Monai.Deploy.Storage.Core.Extensions
810
{
911
public static class PolicyExtensions
1012
{
@@ -66,6 +68,64 @@ public static Policy ToPolicy(string bucketName, string folderName)
6668
};
6769
}
6870

71+
public static Policy ToPolicy(PolicyRequest[] policyRequests)
72+
{
73+
Guard.Against.NullOrEmpty(policyRequests, nameof(policyRequests));
74+
75+
var pathList = policyRequests.SelectMany(pr => GetPathList(pr.FolderName));
76+
Guard.Against.NullOrEmpty(pathList, nameof(pathList));
77+
78+
return new Policy
79+
{
80+
Statement = new List<Statement>
81+
{
82+
new Statement
83+
{
84+
Sid = "AllowUserToSeeBucketListInTheConsole",
85+
Action = new string[] {"s3:ListAllMyBuckets", "s3:GetBucketLocation" },
86+
Effect = "Allow",
87+
Resource = policyRequests.Select(pr => pr.BucketName).ToArray(),
88+
},
89+
new Statement
90+
{
91+
Sid = "AllowRootAndHomeListingOfBucket",
92+
Action = new string[] { "s3:ListBucket" },
93+
Effect = "Allow",
94+
Resource = policyRequests.Select(pr => pr.BucketName).ToArray(),
95+
Condition = new Condition
96+
{
97+
StringEquals = new StringEquals
98+
{
99+
S3Prefix = pathList.ToArray(),
100+
S3Delimiter = new string[] { "/" }
101+
}
102+
}
103+
},
104+
new Statement
105+
{
106+
Sid = "AllowListingOfUserFolder",
107+
Action = new string[] { "s3:ListBucket" },
108+
Effect = "Allow",
109+
Resource = policyRequests.Select(pr => pr.BucketName).ToArray(),
110+
Condition = new Condition
111+
{
112+
StringEquals = new StringEquals
113+
{
114+
S3Prefix = policyRequests.Select(pr => $"{pr.FolderName}/*").ToArray(),
115+
}
116+
}
117+
},
118+
new Statement
119+
{
120+
Sid = "AllowAllS3ActionsInUserFolder",
121+
Action = new string[] { "s3:*" },
122+
Effect = "Allow",
123+
Resource = policyRequests.Select(pr => $"{pr.BucketName}/{pr.FolderName}").ToArray(),
124+
},
125+
}
126+
};
127+
}
128+
69129
public static List<string> GetPathList(string folderName)
70130
{
71131
Guard.Against.NullOrWhiteSpace(folderName, nameof(folderName));
@@ -86,5 +146,7 @@ public static List<string> GetPathList(string folderName)
86146

87147
return pathList;
88148
}
149+
150+
public static string ToJson(this Policy self) => JsonConvert.SerializeObject(self, Converter.Settings);
89151
}
90152
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!--
2+
© 2021-2022 MONAI Consortium
3+
SPDX-License-Identifier: Apache License 2.0
4+
-->
5+
6+
7+
<Project Sdk="Microsoft.NET.Sdk">
8+
9+
<PropertyGroup>
10+
<TargetFramework>net6.0</TargetFramework>
11+
<ImplicitUsings>enable</ImplicitUsings>
12+
<Nullable>enable</Nullable>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Ardalis.GuardClauses" Version="4.0.0" />
17+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-FileCopyrightText: © 2021-2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
namespace Monai.Deploy.Storage.Core.Policies
5+
{
6+
public class Condition
7+
{
8+
public StringLike? StringLike { get; set; }
9+
10+
public StringEquals? StringEquals { get; set; }
11+
}
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-FileCopyrightText: © 2021-2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using Newtonsoft.Json;
5+
6+
namespace Monai.Deploy.Storage.Core.Policies
7+
{
8+
public partial class Policy
9+
{
10+
internal static class Converter
11+
{
12+
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
13+
{
14+
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
15+
DateParseHandling = DateParseHandling.None,
16+
};
17+
}
18+
}
19+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
// SPDX-FileCopyrightText: © 2021-2022 MONAI Consortium
22
// SPDX-License-Identifier: Apache License 2.0
33

4-
namespace Monai.Deploy.Storage.Common.Policies
4+
using Newtonsoft.Json;
5+
6+
namespace Monai.Deploy.Storage.Core.Policies
57
{
6-
public class Policy
8+
public partial class Policy
79
{
810
public string Version { get; set; } = "2012-10-17";
911

1012
public IList<Statement> Statement { get; set; } = new List<Statement>();
13+
14+
public static Policy? FromJson(string json) => JsonConvert.DeserializeObject<Policy>(json, Converter.Settings);
1115
}
1216
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-FileCopyrightText: © 2021-2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
using Ardalis.GuardClauses;
5+
6+
namespace Monai.Deploy.Storage.Core.Policies
7+
{
8+
public class PolicyRequest
9+
{
10+
private readonly string _bucketName;
11+
12+
public PolicyRequest(string bucketName, string folderName)
13+
{
14+
Guard.Against.NullOrWhiteSpace(bucketName, nameof(bucketName));
15+
_bucketName = bucketName;
16+
FolderName = folderName;
17+
}
18+
19+
public string BucketName { get => $"arn:aws:s3:::{_bucketName}"; }
20+
21+
public string FolderName { get; } = "";
22+
}
23+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-FileCopyrightText: © 2021-2022 MONAI Consortium
2+
// SPDX-License-Identifier: Apache License 2.0
3+
4+
namespace Monai.Deploy.Storage.Core.Policies
5+
{
6+
public class Statement
7+
{
8+
public string? Sid { get; set; }
9+
10+
public string[]? Action { get; set; }
11+
12+
public string? Effect { get; set; }
13+
14+
public string[]? Resource { get; set; }
15+
16+
public Condition? Condition { get; set; }
17+
}
18+
}

src/Storage/Common/Policies/StringEquals.cs renamed to src/Monai.Deploy.Storage.Core/Policies/StringEquals.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33

44
using Newtonsoft.Json;
55

6-
namespace Monai.Deploy.Storage.Common.Policies
6+
namespace Monai.Deploy.Storage.Core.Policies
77
{
88
public class StringEquals
99
{
1010
[JsonProperty(PropertyName = "s3:prefix")]
11-
public string[] S3Prefix { get; set; }
11+
public string[]? S3Prefix { get; set; }
1212

1313
[JsonProperty(PropertyName = "s3:delimiter")]
14-
public string[] S3Delimiter { get; set; }
14+
public string[]? S3Delimiter { get; set; }
1515
}
1616
}

src/Storage/Common/Policies/StringLike.cs renamed to src/Monai.Deploy.Storage.Core/Policies/StringLike.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
using Newtonsoft.Json;
55

6-
namespace Monai.Deploy.Storage.Common.Policies
6+
namespace Monai.Deploy.Storage.Core.Policies
77
{
88
public class StringLike
99
{
1010
[JsonProperty(PropertyName = "s3:prefix")]
11-
public string[] S3Prefix { get; set; }
11+
public string[]? S3Prefix { get; set; }
1212
}
1313
}

0 commit comments

Comments
 (0)