Skip to content

Commit

Permalink
fixes incorrect versioning & lock config XML settings (#933)
Browse files Browse the repository at this point in the history
* fixes incorrect versioning & lock config XML settings

* lint change

---------

Co-authored-by: Ersan Bozduman <[email protected]>
  • Loading branch information
ebozduman and Ersan Bozduman authored Nov 30, 2023
1 parent 9f653ee commit 313638a
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,4 @@ dotnet_separate_import_directive_groups = false
dotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typed

MA0051.maximum_lines_per_method = 150
MA0051.maximum_statements_per_method = 60
MA0051.maximum_statements_per_method = 70
22 changes: 13 additions & 9 deletions Minio.Functional.Tests/FunctionalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,10 @@ internal static async Task TearDown(IMinioClient minio, string bucketName)
ObjectLockConfiguration lockConfig = null;
try
{
var versioningConfig = await minio.GetVersioningAsync(new GetVersioningArgs()
.WithBucket(bucketName)).ConfigureAwait(false);
VersioningConfiguration versioningConfig = null;
versioningConfig = await minio.GetVersioningAsync(new GetVersioningArgs()
.WithBucket(bucketName)
.WithVersions(true)).ConfigureAwait(false);
if (versioningConfig is not null &&
(versioningConfig.Status.Contains("Enabled", StringComparison.Ordinal) ||
versioningConfig.Status.Contains("Suspended", StringComparison.Ordinal)))
Expand Down Expand Up @@ -2216,18 +2218,18 @@ internal static async Task ObjectLockConfigurationAsync_Test1(IMinioClient minio
}
catch (NotImplementedException ex)
{
await TearDown(minio, bucketName).ConfigureAwait(false);
new MintLogger(nameof(ObjectLockConfigurationAsync_Test1), setObjectLockConfigurationSignature,
"Tests whether SetObjectLockConfigurationAsync passes", TestStatus.NA, DateTime.Now - startTime,
ex.Message, ex.ToString(), args: args).Log();
await TearDown(minio, bucketName).ConfigureAwait(false);
return;
}
catch (Exception ex)
{
await TearDown(minio, bucketName).ConfigureAwait(false);
new MintLogger(nameof(ObjectLockConfigurationAsync_Test1), setObjectLockConfigurationSignature,
"Tests whether SetObjectLockConfigurationAsync passes", TestStatus.FAIL, DateTime.Now - startTime,
ex.Message, ex.ToString(), args: args).Log();
await TearDown(minio, bucketName).ConfigureAwait(false);
throw;
}

Expand All @@ -2246,16 +2248,17 @@ internal static async Task ObjectLockConfigurationAsync_Test1(IMinioClient minio
catch (NotImplementedException ex)
{
setLockNotImplemented = true;
await TearDown(minio, bucketName).ConfigureAwait(false);
new MintLogger(nameof(ObjectLockConfigurationAsync_Test1), setObjectLockConfigurationSignature,
"Tests whether SetObjectLockConfigurationAsync passes", TestStatus.NA, DateTime.Now - startTime,
ex.Message, ex.ToString(), args: args).Log();
}
catch (Exception ex)
{
await TearDown(minio, bucketName).ConfigureAwait(false);
new MintLogger(nameof(ObjectLockConfigurationAsync_Test1), setObjectLockConfigurationSignature,
"Tests whether SetObjectLockConfigurationAsync passes", TestStatus.FAIL, DateTime.Now - startTime,
ex.Message, ex.ToString(), args: args).Log();
await TearDown(minio, bucketName).ConfigureAwait(false);
throw;
}

Expand All @@ -2276,6 +2279,7 @@ internal static async Task ObjectLockConfigurationAsync_Test1(IMinioClient minio
}
catch (NotImplementedException ex)
{
await TearDown(minio, bucketName).ConfigureAwait(false);
getLockNotImplemented = true;
new MintLogger(nameof(ObjectLockConfigurationAsync_Test1), getObjectLockConfigurationSignature,
"Tests whether GetObjectLockConfigurationAsync passes", TestStatus.NA, DateTime.Now - startTime,
Expand Down Expand Up @@ -2329,7 +2333,6 @@ internal static async Task ObjectLockConfigurationAsync_Test1(IMinioClient minio
}
finally
{
await Task.Delay(1500).ConfigureAwait(false);
await TearDown(minio, bucketName).ConfigureAwait(false);
}
}
Expand Down Expand Up @@ -2734,7 +2737,6 @@ internal static async Task ListenBucketNotificationsAsync_Test1(IMinioClient min
listenBucketNotificationsSignature,
"Tests whether ListenBucketNotifications passes for small object",
TestStatus.PASS, DateTime.Now - startTime, args: args).Log();
await TearDown(minio, bucketName).ConfigureAwait(false);
}
catch (NotImplementedException ex)
{
Expand All @@ -2743,7 +2745,6 @@ internal static async Task ListenBucketNotificationsAsync_Test1(IMinioClient min
"Tests whether ListenBucketNotifications passes for small object",
TestStatus.NA, DateTime.Now - startTime, ex.Message,
ex.ToString(), args: args).Log();
await TearDown(minio, bucketName).ConfigureAwait(false);
}
catch (Exception ex)
{
Expand Down Expand Up @@ -2775,10 +2776,13 @@ static bool isAWS(string endPoint)
"Tests whether ListenBucketNotifications passes for small object",
TestStatus.FAIL, DateTime.Now - startTime, ex.Message,
ex.ToString(), args: args).Log();
await TearDown(minio, bucketName).ConfigureAwait(false);
throw;
}
}
finally
{
await TearDown(minio, bucketName).ConfigureAwait(false);
}
}

internal static async Task ListenBucketNotificationsAsync_Test2(IMinioClient minio)
Expand Down
6 changes: 3 additions & 3 deletions Minio/Credentials/AssumeRoleResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
namespace Minio.Credentials;

[Serializable]
[XmlRoot(ElementName = "AssumeRoleResponse", Namespace = "https://sts.amazonaws.com/doc/2011-06-15/")]
[XmlRoot(ElementName = "AssumeRoleResponse", Namespace = "http://s3.amazonaws.com/doc/2006-03-01/")]
public class AssumeRoleResponse
{
[XmlElement(ElementName = "AssumeRoleResult")]
Expand All @@ -33,9 +33,9 @@ public string ToXML()
using var ms = new MemoryStream();
using var xmlWriter = XmlWriter.Create(ms, settings);
var names = new XmlSerializerNamespaces();
names.Add(string.Empty, "https://sts.amazonaws.com/doc/2011-06-15/");
names.Add(string.Empty, "http://s3.amazonaws.com/doc/2006-03-01/");

var cs = new XmlSerializer(typeof(CertificateResponse));
var cs = new XmlSerializer(typeof(AssumeRoleResponse));
cs.Serialize(xmlWriter, this, names);

ms.Flush();
Expand Down
8 changes: 8 additions & 0 deletions Minio/DataModel/Args/GetVersioningArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public GetVersioningArgs()
RequestMethod = HttpMethod.Get;
}

internal bool Versions { get; private set; }

public GetVersioningArgs WithVersions(bool ver)
{
Versions = ver;
return this;
}

internal override HttpRequestMessageBuilder BuildRequest(HttpRequestMessageBuilder requestMessageBuilder)
{
requestMessageBuilder.AddQueryParameter("versioning", "");
Expand Down
18 changes: 18 additions & 0 deletions Minio/DataModel/ObjectLock/ObjectLockConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

using System.Xml;
using System.Xml.Serialization;

namespace Minio.DataModel.ObjectLock;
Expand All @@ -38,4 +39,21 @@ public ObjectLockConfiguration(ObjectRetentionMode mode, int numOfDays)
[XmlElement("ObjectLockEnabled")] public string ObjectLockEnabled { get; set; }

[XmlElement("Rule")] public ObjectLockRule Rule { get; set; }

public string ToXML()
{
var settings = new XmlWriterSettings { OmitXmlDeclaration = true };
using var ms = new MemoryStream();
using var xmlWriter = XmlWriter.Create(ms, settings);
var names = new XmlSerializerNamespaces();
names.Add(string.Empty, "http://s3.amazonaws.com/doc/2006-03-01/");

var cs = new XmlSerializer(typeof(ObjectLockConfiguration));
cs.Serialize(xmlWriter, this, names);

ms.Flush();
_ = ms.Seek(0, SeekOrigin.Begin);
using var streamReader = new StreamReader(ms);
return streamReader.ReadToEnd();
}
}
5 changes: 1 addition & 4 deletions Minio/DataModel/Response/GetVersioningResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/

using System.Net;
using System.Text;
using CommunityToolkit.HighPerformance;
using Minio.Helper;

namespace Minio.DataModel.Response;
Expand All @@ -30,8 +28,7 @@ internal GetVersioningResponse(HttpStatusCode statusCode, string responseContent
!HttpStatusCode.OK.Equals(statusCode))
return;

using var stream = Encoding.UTF8.GetBytes(responseContent).AsMemory().AsStream();
VersioningConfig = Utils.DeserializeXml<VersioningConfiguration>(stream);
VersioningConfig = Utils.DeserializeXml<VersioningConfiguration>(responseContent);
}

internal VersioningConfiguration VersioningConfig { get; set; }
Expand Down
26 changes: 21 additions & 5 deletions Minio/DataModel/VersioningConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

using System.Xml;
using System.Xml.Serialization;

namespace Minio.DataModel;
Expand All @@ -30,10 +31,7 @@ public VersioningConfiguration()

public VersioningConfiguration(bool enableVersioning = true)
{
if (enableVersioning)
Status = "Enabled";
else
Status = "Suspended";
Status = enableVersioning ? "Enabled" : "Suspended";
}

public VersioningConfiguration(VersioningConfiguration vc)
Expand All @@ -44,7 +42,25 @@ public VersioningConfiguration(VersioningConfiguration vc)
MfaDelete = vc.MfaDelete;
}

[XmlElement] public string Status { get; set; }
[XmlElement(ElementName = "Status")] public string Status { get; set; }

[XmlElement(ElementName = "MfaDelete")]
public string MfaDelete { get; set; }

public string ToXML()
{
var settings = new XmlWriterSettings { OmitXmlDeclaration = true };
using var ms = new MemoryStream();
using var xmlWriter = XmlWriter.Create(ms, settings);
var names = new XmlSerializerNamespaces();
names.Add(string.Empty, "http://s3.amazonaws.com/doc/2006-03-01/");

var cs = new XmlSerializer(typeof(VersioningConfiguration));
cs.Serialize(xmlWriter, this, names);

ms.Flush();
_ = ms.Seek(0, SeekOrigin.Begin);
using var streamReader = new StreamReader(ms);
return streamReader.ReadToEnd();
}
}
20 changes: 6 additions & 14 deletions Minio/Helper/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,23 +1065,15 @@ public static string SerializeToXml<T>(T anyobject) where T : class
using var stringReader = new StringReader(xml);
using var xmlReader = XmlReader.Create(stringReader, settings);

try
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(xmlReader);
}
catch (InvalidOperationException)
{
return default;
}
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(xmlReader);
}

private static string GetNamespace<T>()
{
if (typeof(T).GetCustomAttributes(typeof(XmlRootAttribute), true)
.FirstOrDefault() is XmlRootAttribute xmlRootAttribute)
return xmlRootAttribute.Namespace;

return null;
return typeof(T).GetCustomAttributes(typeof(XmlRootAttribute), true)
.FirstOrDefault() is XmlRootAttribute xmlRootAttribute
? xmlRootAttribute.Namespace
: null;
}
}

0 comments on commit 313638a

Please sign in to comment.