From 313638a5290c5865ca1a04a66513b52c3cf6bf44 Mon Sep 17 00:00:00 2001 From: ebozduman Date: Wed, 29 Nov 2023 20:03:30 -0800 Subject: [PATCH] fixes incorrect versioning & lock config XML settings (#933) * fixes incorrect versioning & lock config XML settings * lint change --------- Co-authored-by: Ersan Bozduman --- .editorconfig | 2 +- Minio.Functional.Tests/FunctionalTest.cs | 22 +++++++++------- Minio/Credentials/AssumeRoleResponse.cs | 6 ++--- Minio/DataModel/Args/GetVersioningArgs.cs | 8 ++++++ .../ObjectLock/ObjectLockConfiguration.cs | 18 +++++++++++++ .../Response/GetVersioningResponse.cs | 5 +--- Minio/DataModel/VersioningConfiguration.cs | 26 +++++++++++++++---- Minio/Helper/Utils.cs | 20 +++++--------- 8 files changed, 71 insertions(+), 36 deletions(-) diff --git a/.editorconfig b/.editorconfig index 480544e6f..2424895e5 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 \ No newline at end of file +MA0051.maximum_statements_per_method = 70 \ No newline at end of file diff --git a/Minio.Functional.Tests/FunctionalTest.cs b/Minio.Functional.Tests/FunctionalTest.cs index 0024f3512..978426dbf 100644 --- a/Minio.Functional.Tests/FunctionalTest.cs +++ b/Minio.Functional.Tests/FunctionalTest.cs @@ -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))) @@ -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; } @@ -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; } @@ -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, @@ -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); } } @@ -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) { @@ -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) { @@ -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) diff --git a/Minio/Credentials/AssumeRoleResponse.cs b/Minio/Credentials/AssumeRoleResponse.cs index 0773ddec4..3692a2c6f 100644 --- a/Minio/Credentials/AssumeRoleResponse.cs +++ b/Minio/Credentials/AssumeRoleResponse.cs @@ -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")] @@ -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(); diff --git a/Minio/DataModel/Args/GetVersioningArgs.cs b/Minio/DataModel/Args/GetVersioningArgs.cs index 54fbf4bca..f6b408519 100644 --- a/Minio/DataModel/Args/GetVersioningArgs.cs +++ b/Minio/DataModel/Args/GetVersioningArgs.cs @@ -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", ""); diff --git a/Minio/DataModel/ObjectLock/ObjectLockConfiguration.cs b/Minio/DataModel/ObjectLock/ObjectLockConfiguration.cs index 746a64180..68b92af7b 100644 --- a/Minio/DataModel/ObjectLock/ObjectLockConfiguration.cs +++ b/Minio/DataModel/ObjectLock/ObjectLockConfiguration.cs @@ -14,6 +14,7 @@ * limitations under the License. */ +using System.Xml; using System.Xml.Serialization; namespace Minio.DataModel.ObjectLock; @@ -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(); + } } diff --git a/Minio/DataModel/Response/GetVersioningResponse.cs b/Minio/DataModel/Response/GetVersioningResponse.cs index cc6994743..d742476ec 100644 --- a/Minio/DataModel/Response/GetVersioningResponse.cs +++ b/Minio/DataModel/Response/GetVersioningResponse.cs @@ -15,8 +15,6 @@ */ using System.Net; -using System.Text; -using CommunityToolkit.HighPerformance; using Minio.Helper; namespace Minio.DataModel.Response; @@ -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(stream); + VersioningConfig = Utils.DeserializeXml(responseContent); } internal VersioningConfiguration VersioningConfig { get; set; } diff --git a/Minio/DataModel/VersioningConfiguration.cs b/Minio/DataModel/VersioningConfiguration.cs index f3ec6b476..383d80073 100644 --- a/Minio/DataModel/VersioningConfiguration.cs +++ b/Minio/DataModel/VersioningConfiguration.cs @@ -14,6 +14,7 @@ * limitations under the License. */ +using System.Xml; using System.Xml.Serialization; namespace Minio.DataModel; @@ -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) @@ -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(); + } } diff --git a/Minio/Helper/Utils.cs b/Minio/Helper/Utils.cs index 6f234eac2..21527477d 100644 --- a/Minio/Helper/Utils.cs +++ b/Minio/Helper/Utils.cs @@ -1065,23 +1065,15 @@ public static string SerializeToXml(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() { - 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; } }