Skip to content

Commit

Permalink
Simplify SetPolicyAsync and GetPolicyAsync api calls (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
poornas authored and deekoder committed Mar 20, 2018
1 parent ad23851 commit ef88bcc
Show file tree
Hide file tree
Showing 31 changed files with 51 additions and 2,951 deletions.
22 changes: 10 additions & 12 deletions Docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,24 +393,23 @@ catch (MinioException e)
```

<a name="getBucketPolicy"></a>
### GetPolicyAsync(string bucketName, string objectPrefix)
`Task<PolicyType> GetPolicyAsync(string bucketName, string objectPrefix, CancellationToken cancellationToken = default(CancellationToken))`
### GetPolicyAsync(string bucketName)
`Task<String> GetPolicyAsync(string bucketName, CancellationToken cancellationToken = default(CancellationToken))`

Get bucket policy at given objectPrefix.
Get bucket policy.


__Parameters__

|Param | Type | Description |
|:--- |:--- |:--- |
| ``bucketName`` | _string_ | Name of the bucket. |
| ``objectPrefix`` | _string_ | Policy applies to objects with prefix |
| ``cancellationToken``| _System.Threading.CancellationToken_ | Optional parameter. Defaults to default(CancellationToken) |


| Return Type | Exceptions |
|:--- |:--- |
| ``Task<PolicyType>``: The current bucket policy for given bucket and objectPrefix. | Listed Exceptions: |
| ``Task<String>``: The current bucket policy for given bucket as a json string. | Listed Exceptions: |
| | ``InvalidBucketNameException `` : upon invalid bucket name. |
| | ``InvalidObjectPrefixException`` : upon invalid object prefix. |
| | ``ConnectionException`` : upon connection error. |
Expand All @@ -425,7 +424,7 @@ __Example__
```cs
try
{
PolicyType policy = await minioClient.GetPolicyAsync("myBucket", objectPrefix:"downloads");
String policyJson = await minioClient.GetPolicyAsync("myBucket");
Console.Out.WriteLine("Current policy: " + policy.GetType().ToString());
}
catch (MinioException e)
Expand All @@ -435,18 +434,17 @@ catch (MinioException e)
```

<a name="setBucketPolicy"></a>
### SetPolicyAsync(string bucketName, string objectPrefix, PolicyType policyType)
`Task SetPolicyAsync(string bucketName, string objectPrefix, PolicyType policyType, CancellationToken cancellationToken = default(CancellationToken))`
### SetPolicyAsync(string bucketName, string policyJson)
`Task SetPolicyAsync(string bucketName, string policyJson, CancellationToken cancellationToken = default(CancellationToken))`

Set policy on bucket and object prefix.
Set policy on bucket.

__Parameters__

|Param | Type | Description |
|:--- |:--- |:--- |
| ``bucketName`` | _string_ | Name of the bucket |
| ``objectPrefix`` | _string_ | Policy applies to objects with prefix |
| ``PolicyType`` | _PolicyType_ | Policy to apply |
| ``policyJson`` | _string_ | Policy as a json string |
| ``cancellationToken``| _System.Threading.CancellationToken_ | Optional parameter. Defaults to default(CancellationToken) |


Expand All @@ -466,7 +464,7 @@ __Example__
```cs
try
{
await minioClient.SetPolicyAsync("myBucket", "uploads",PolicyType.WRITE_ONLY);
await minioClient.SetPolicyAsync("myBucket");
}
catch (MinioException e)
{
Expand Down
15 changes: 1 addition & 14 deletions Minio.Core/Minio.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<AssemblyName>Minio</AssemblyName>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<RootNamespace>Minio</RootNamespace>
<Version>1.0.8</Version>
<Version>1.1.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release.Core|AnyCPU'">
Expand Down Expand Up @@ -58,19 +58,6 @@
<Compile Include="..\Minio\DataModel\Notification\TopicConfig.cs" Link="DataModel\Notification\TopicConfig.cs" />
<Compile Include="..\Minio\DataModel\ObjectStat.cs" Link="DataModel\ObjectStat.cs" />
<Compile Include="..\Minio\DataModel\Part.cs" Link="DataModel\Part.cs" />
<Compile Include="..\Minio\DataModel\Policy\ActionJsonConverter.cs" Link="DataModel\Policy\ActionJsonConverter.cs" />
<Compile Include="..\Minio\DataModel\Policy\BucketPolicy.cs" Link="DataModel\Policy\BucketPolicy.cs" />
<Compile Include="..\Minio\DataModel\Policy\ConditionKeyMap.cs" Link="DataModel\Policy\ConditionKeyMap.cs" />
<Compile Include="..\Minio\DataModel\Policy\ConditionKeyMapConverter.cs" Link="DataModel\Policy\ConditionKeyMapConverter.cs" />
<Compile Include="..\Minio\DataModel\Policy\ConditionMap.cs" Link="DataModel\Policy\ConditionMap.cs" />
<Compile Include="..\Minio\DataModel\Policy\PolicyConstants.cs" Link="DataModel\Policy\PolicyConstants.cs" />
<Compile Include="..\Minio\DataModel\Policy\PolicyType.cs" Link="DataModel\Policy\PolicyType.cs" />
<Compile Include="..\Minio\DataModel\Policy\Principal.cs" Link="DataModel\Policy\Principal.cs" />
<Compile Include="..\Minio\DataModel\Policy\PrincipalJsonConverter.cs" Link="DataModel\Policy\PrincipalJsonConverter.cs" />
<Compile Include="..\Minio\DataModel\Policy\ResourceJsonConverter.cs" Link="DataModel\Policy\ResourceJsonConverter.cs" />
<Compile Include="..\Minio\DataModel\Policy\Resources.cs" Link="DataModel\Policy\Resources.cs" />
<Compile Include="..\Minio\DataModel\Policy\SingleOrArrayConverter.cs" Link="DataModel\Policy\SingleOrArrayConverter.cs" />
<Compile Include="..\Minio\DataModel\Policy\Statement.cs" Link="DataModel\Policy\Statement.cs" />
<Compile Include="..\Minio\DataModel\PostPolicy.cs" Link="DataModel\PostPolicy.cs" />
<Compile Include="..\Minio\DataModel\Prefix.cs" Link="DataModel\Prefix.cs" />
<Compile Include="..\Minio\DataModel\Upload.cs" Link="DataModel\Upload.cs" />
Expand Down
4 changes: 2 additions & 2 deletions Minio.Examples/Cases/GetBucketPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public async static Task Run(Minio.MinioClient minio,
try
{
Console.Out.WriteLine("Running example for API: GetPolicyAsync");
PolicyType policy = await minio.GetPolicyAsync(bucketName);
Console.Out.WriteLine("Current Policy is " + policy.ToString() + " for bucket " + bucketName);
String policyJson = await minio.GetPolicyAsync(bucketName);
Console.Out.WriteLine("Current Policy is " + policyJson + " for bucket " + bucketName);
Console.Out.WriteLine();
}
catch (Exception e)
Expand Down
11 changes: 4 additions & 7 deletions Minio.Examples/Cases/SetBucketPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@ class SetBucketPolicy
{
// Set bucket policy
public async static Task Run(Minio.MinioClient minio,
PolicyType policy,
string bucketName = "my-bucket-name",
string objectPrefix="")
string bucketName = "my-bucket-name")
{
try
{
Console.Out.WriteLine("Running example for API: SetPolicyAsync");
string policyJson = $@"{{""Version"":""2012-10-17"",""Statement"":[{{""Action"":[""s3:GetBucketLocation""],""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}""],""Sid"":""""}},{{""Action"":[""s3:ListBucket""],""Condition"":{{""StringEquals"":{{""s3:prefix"":[""foo"",""prefix/""]}}}},""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}""],""Sid"":""""}},{{""Action"":[""s3:GetObject""],""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}/foo*"",""arn:aws:s3:::{bucketName}/prefix/*""],""Sid"":""""}}]}}";
// Change policy type parameter
await minio.SetPolicyAsync(bucketName,
objectPrefix,
policy);
Console.Out.WriteLine("Policy " + policy.ToString() + " set for the bucket " + bucketName + " successfully");
await minio.SetPolicyAsync(bucketName, policyJson);
Console.Out.WriteLine("Policy " + policyJson + " set for the bucket " + bucketName + " successfully");
Console.Out.WriteLine();
}
catch (Exception e)
Expand Down
8 changes: 3 additions & 5 deletions Minio.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,12 @@ public static void Main(string[] args)

// Set HTTP Tracing Off
// minioClient.SetTraceOff();

// Check if bucket exists
Cases.BucketExists.Run(minioClient, bucketName).Wait();

// Create a new bucket
Cases.MakeBucket.Run(minioClient, bucketName).Wait();

Cases.MakeBucket.Run(minioClient, destBucketName).Wait();


Expand Down Expand Up @@ -161,11 +160,10 @@ public static void Main(string[] args)
Cases.RemoveIncompleteUpload.Run(minioClient, bucketName, objectName).Wait();

// Set a policy for given bucket
Cases.SetBucketPolicy.Run(minioClient, PolicyType.READ_ONLY, bucketName).Wait();

Cases.SetBucketPolicy.Run(minioClient, bucketName).Wait();
// Get the policy for given bucket
Cases.GetBucketPolicy.Run(minioClient, bucketName).Wait();

// Set bucket notifications
Cases.SetBucketNotification.Run(minioClient, bucketName).Wait();

Expand Down
20 changes: 9 additions & 11 deletions Minio.Functional.Tests/FunctionalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class FunctionalTest
private static string presignedGetObjectSignature = "Task<string> PresignedGetObjectAsync(string bucketName, string objectName, int expiresInt, Dictionary<string,string> reqParams = null)";
private static string presignedPutObjectSignature = "Task<string> PresignedPutObjectAsync(string bucketName, string objectName, int expiresInt)";
private static string presignedPostPolicySignature = "Task<Dictionary<string, string>> PresignedPostPolicyAsync(PostPolicy policy)";
private static string getBucketPolicySignature = "Task<PolicyType> GetPolicyAsync(string bucketName, string objectPrefix, CancellationToken cancellationToken = default(CancellationToken))";
private static string setBucketPolicySignature = "Task SetPolicyAsync(string bucketName, string objectPrefix, PolicyType policyType, CancellationToken cancellationToken = default(CancellationToken))";
private static string getBucketPolicySignature = "Task<String> GetPolicyAsync(string bucketName,CancellationToken cancellationToken = default(CancellationToken))";
private static string setBucketPolicySignature = "Task SetPolicyAsync(string bucketName, string policyJson, CancellationToken cancellationToken = default(CancellationToken))";
private static string getBucketNotificationSignature = "Task<BucketNotification> GetBucketNotificationAsync(string bucketName, CancellationToken cancellationToken = default(CancellationToken))";
private static string setBucketNotificationSignature = "Task SetBucketNotificationAsync(string bucketName, BucketNotification notification, CancellationToken cancellationToken = default(CancellationToken))";
private static string removeAllBucketsNotificationSignature = "Task RemoveAllBucketNotificationsAsync(string bucketName, CancellationToken cancellationToken = default(CancellationToken))";
Expand Down Expand Up @@ -1957,8 +1957,7 @@ await minio.PutObjectAsync(bucketName,
}

// Validate
PolicyType policy = await minio.GetPolicyAsync(bucketName, objectName.Substring(5));
Assert.AreEqual(policy.GetType(), PolicyType.READ_ONLY);
String policy = await minio.GetPolicyAsync(bucketName);
await minio.RemoveObjectAsync(bucketName, objectName);
await TearDown(minio, bucketName);
new MintLogger("PresignedPostPolicy_Test1",presignedPostPolicySignature,"Tests whether PresignedPostPolicy url applies policy on server",TestStatus.PASS,(DateTime.Now - startTime), args:args).Log();
Expand Down Expand Up @@ -2196,9 +2195,9 @@ private async static Task SetBucketPolicy_Test1(MinioClient minio)
await minio.PutObjectAsync(bucketName,
objectName,
filestream, filestream.Length, null);
string policyJson = $@"{{""Version"":""2012-10-17"",""Statement"":[{{""Action"":[""s3:GetObject""],""Effect"":""Allow"",""Principal"":{{""AWS"":[""*""]}},""Resource"":[""arn:aws:s3:::{bucketName}/foo*"",""arn:aws:s3:::{bucketName}/prefix/*""],""Sid"":""""}}]}}";
await minio.SetPolicyAsync(bucketName,
objectName.Substring(5),
PolicyType.READ_ONLY);
policyJson);
await minio.RemoveObjectAsync(bucketName, objectName);

await TearDown(minio, bucketName);
Expand All @@ -2219,20 +2218,19 @@ private async static Task GetBucketPolicy_Test1(MinioClient minio)
Dictionary<string,string> args = new Dictionary<string,string>
{
{"bucketName", bucketName},
{"objectPrefix", objectName.Substring(5)},
};
try
{
await Setup_Test(minio, bucketName);
String policyJson="";
using (MemoryStream filestream = rsg.GenerateStreamFromSeed(1 * MB))
await minio.PutObjectAsync(bucketName,
objectName,
filestream, filestream.Length, null);
await minio.SetPolicyAsync(bucketName,
objectName.Substring(5),
PolicyType.READ_ONLY);
PolicyType policy = await minio.GetPolicyAsync(bucketName, objectName.Substring(5));
Assert.IsTrue(policy.Equals(PolicyType.READ_ONLY));
policyJson);
String policy = await minio.GetPolicyAsync(bucketName);
Assert.IsTrue(policy.Equals(policyJson));
await minio.RemoveObjectAsync(bucketName, objectName);

await TearDown(minio, bucketName);
Expand Down
39 changes: 0 additions & 39 deletions Minio.Net452/Minio.Net452.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,45 +183,6 @@
<Compile Include="..\Minio\DataModel\Part.cs">
<Link>DataModel\Part.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\ActionJsonConverter.cs">
<Link>DataModel\Policy\ActionJsonConverter.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\BucketPolicy.cs">
<Link>DataModel\Policy\BucketPolicy.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\ConditionKeyMap.cs">
<Link>DataModel\Policy\ConditionKeyMap.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\ConditionKeyMapConverter.cs">
<Link>DataModel\Policy\ConditionKeyMapConverter.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\ConditionMap.cs">
<Link>DataModel\Policy\ConditionMap.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\PolicyConstants.cs">
<Link>DataModel\Policy\PolicyConstants.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\PolicyType.cs">
<Link>DataModel\Policy\PolicyType.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\Principal.cs">
<Link>DataModel\Policy\Principal.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\PrincipalJsonConverter.cs">
<Link>DataModel\Policy\PrincipalJsonConverter.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\ResourceJsonConverter.cs">
<Link>DataModel\Policy\ResourceJsonConverter.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\Resources.cs">
<Link>DataModel\Policy\Resources.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\SingleOrArrayConverter.cs">
<Link>DataModel\Policy\SingleOrArrayConverter.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\Policy\Statement.cs">
<Link>DataModel\Policy\Statement.cs</Link>
</Compile>
<Compile Include="..\Minio\DataModel\PostPolicy.cs">
<Link>DataModel\PostPolicy.cs</Link>
</Compile>
Expand Down
4 changes: 2 additions & 2 deletions Minio.Net452/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.8.0")]
[assembly: AssemblyFileVersion("1.0.8.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]

1 change: 0 additions & 1 deletion Minio.Tests/AuthenticatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Minio.DataModel;
using Minio.DataModel.Policy;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
Expand Down
2 changes: 0 additions & 2 deletions Minio.Tests/Minio.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@
<ItemGroup>
<Compile Include="DictionaryExtensionMethods.cs" />
<Compile Include="NotificationTest.cs" />
<Compile Include="PolicyConditionTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="EndpointTest.cs" />
<Compile Include="TestHelper.cs" />
<Compile Include="PolicyTests.cs" />
<Compile Include="AuthenticatorTest.cs" />
<Compile Include="RegionTest.cs" />
<Compile Include="UtilsTest.cs" />
Expand Down
Loading

0 comments on commit ef88bcc

Please sign in to comment.