From 4fb17651a450837c33a1132872bb5c050d508df0 Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Mon, 8 May 2023 22:19:22 +0200 Subject: [PATCH 01/13] Updates for proj files --- Directory.Build.props | 10 ++++------ Minio/Minio.csproj | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index bac8e4b022..ed60dd0129 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -26,7 +26,7 @@ false latest - AllEnabledByDefault + All $(MSBuildProjectName.Contains('Test')) @@ -51,8 +51,6 @@ - full - true @@ -63,7 +61,7 @@ - + \ No newline at end of file diff --git a/Minio/Minio.csproj b/Minio/Minio.csproj index d2f8ad65aa..fa4371a86a 100644 --- a/Minio/Minio.csproj +++ b/Minio/Minio.csproj @@ -3,7 +3,6 @@ Minio Minio net6.0;net7.0;netstandard2.0 - embedded ..\Minio.snk true true @@ -14,7 +13,7 @@ - + From e4dee9761232096a120dc4b6b54f2f8939890d92 Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Mon, 8 May 2023 22:37:18 +0200 Subject: [PATCH 02/13] Cleanup --- Directory.Build.props | 2 +- Minio.Tests/RegionTest.cs | 2 +- Minio.Tests/UnitTest1.cs | 121 -------------------------- Minio.Tests/UnitTest2.cs | 142 +++++++++++++++++++++++++++++++ Minio/Helper/OperationsHelper.cs | 31 ------- Minio/Helper/OperationsUtil.cs | 50 +++++++++++ Minio/MinioClient.cs | 4 +- Minio/MinioClientExtensions.cs | 4 + 8 files changed, 200 insertions(+), 156 deletions(-) create mode 100644 Minio.Tests/UnitTest2.cs create mode 100644 Minio/Helper/OperationsUtil.cs diff --git a/Directory.Build.props b/Directory.Build.props index ed60dd0129..b847cc0e05 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -78,7 +78,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers diff --git a/Minio.Tests/RegionTest.cs b/Minio.Tests/RegionTest.cs index acd9255407..5dfb92eda4 100644 --- a/Minio.Tests/RegionTest.cs +++ b/Minio.Tests/RegionTest.cs @@ -19,7 +19,7 @@ namespace Minio.Tests; [TestClass] -public class TestRegion +public class RegionTest { [DataTestMethod] [DataRow("s3.us-east-2.amazonaws.com", "us-east-2")] diff --git a/Minio.Tests/UnitTest1.cs b/Minio.Tests/UnitTest1.cs index 2ab5a5008d..fe04a20cad 100644 --- a/Minio.Tests/UnitTest1.cs +++ b/Minio.Tests/UnitTest1.cs @@ -166,124 +166,3 @@ public void TestEndpointFailure() #endregion } - -/// -/// Summary description for UnitTest2 -/// -[TestClass] -public class UnitTest2 -{ - public UnitTest2() - { - ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 - | SecurityProtocolType.Tls11 - | SecurityProtocolType.Tls12; - using var minio = new MinioClient() - .WithEndpoint(TestHelper.Endpoint) - .WithCredentials(TestHelper.AccessKey, TestHelper.SecretKey) - .WithSSL() - .Build(); - } - - /// - /// Gets or sets the test context which provides - /// information about and functionality for the current test run. - /// - public TestContext TestContext { get; set; } - - [TestMethod] - public void TestWithUrl() - { - using var client = new MinioClient() - .WithEndpoint("localhost", 9000) - .WithCredentials("minio", "minio") - .Build(); - } - - [TestMethod] - public void TestWithoutPort() - { - using var client = new MinioClient() - .WithEndpoint("localhost") - .WithCredentials("minio", "minio") - .Build(); - } - - [TestMethod] - public void TestWithTrailingSlash() - { - using var client = new MinioClient() - .WithEndpoint("localhost", 9000) - .WithCredentials("minio", "minio") - .Build(); - } - - [TestMethod] - [ExpectedException(typeof(InvalidEndpointException))] - public void TestUrlFailsWithMalformedScheme() - { - using var client = new MinioClient() - .WithEndpoint("htp://localhost", 9000) - .WithCredentials("minio", "minio") - .Build(); - } - - [TestMethod] - [ExpectedException(typeof(InvalidEndpointException))] - public void TestUrlFailsWithPath() - { - using var client = new MinioClient().WithEndpoint("localhost:9000/foo").WithCredentials("minio", "minio") - .Build(); - } - - [TestMethod] - [ExpectedException(typeof(InvalidEndpointException))] - public void TestUrlFailsWithQuery() - { - using var client = new MinioClient() - .WithEndpoint("localhost:9000/?foo=bar") - .WithCredentials("minio", "minio") - .Build(); - } - - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void TestSetAppInfoFailsNullApp() - { - using var client = new MinioClient() - .WithEndpoint("localhost", 9000) - .WithCredentials("minio", "minio") - .Build(); - client.SetAppInfo(null, "1.2.2"); - } - - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void TestSetAppInfoFailsNullVersion() - { - using var client = new MinioClient() - .WithEndpoint("localhost", 9000) - .WithCredentials("minio", "minio") - .Build(); - client.SetAppInfo("Hello-App", null); - } - - [TestMethod] - public void TestSetAppInfoSuccess() - { - using var client = new MinioClient() - .WithEndpoint("localhost", 9000) - .WithCredentials("minio", "minio") - .Build(); - client.SetAppInfo("Hello-App", "1.2.1"); - } - - [TestMethod] - public void TestEndpointSuccess() - { - using var client = new MinioClient() - .WithEndpoint("s3.amazonaws.com") - .WithCredentials("minio", "minio") - .Build(); - } -} \ No newline at end of file diff --git a/Minio.Tests/UnitTest2.cs b/Minio.Tests/UnitTest2.cs new file mode 100644 index 0000000000..5fad28d4e3 --- /dev/null +++ b/Minio.Tests/UnitTest2.cs @@ -0,0 +1,142 @@ +/* + * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System.Net; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Minio.Exceptions; + +namespace Minio.Tests; + +/// +/// Summary description for UnitTest2 +/// +[TestClass] +public class UnitTest2 +{ + public UnitTest2() + { + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 + | SecurityProtocolType.Tls11 + | SecurityProtocolType.Tls12; + using var minio = new MinioClient() + .WithEndpoint(TestHelper.Endpoint) + .WithCredentials(TestHelper.AccessKey, TestHelper.SecretKey) + .WithSSL() + .Build(); + } + + /// + /// Gets or sets the test context which provides + /// information about and functionality for the current test run. + /// + public TestContext TestContext { get; set; } + + [TestMethod] + public void TestWithUrl() + { + using var client = new MinioClient() + .WithEndpoint("localhost", 9000) + .WithCredentials("minio", "minio") + .Build(); + } + + [TestMethod] + public void TestWithoutPort() + { + using var client = new MinioClient() + .WithEndpoint("localhost") + .WithCredentials("minio", "minio") + .Build(); + } + + [TestMethod] + public void TestWithTrailingSlash() + { + using var client = new MinioClient() + .WithEndpoint("localhost", 9000) + .WithCredentials("minio", "minio") + .Build(); + } + + [TestMethod] + [ExpectedException(typeof(InvalidEndpointException))] + public void TestUrlFailsWithMalformedScheme() + { + using var client = new MinioClient() + .WithEndpoint("htp://localhost", 9000) + .WithCredentials("minio", "minio") + .Build(); + } + + [TestMethod] + [ExpectedException(typeof(InvalidEndpointException))] + public void TestUrlFailsWithPath() + { + using var client = new MinioClient().WithEndpoint("localhost:9000/foo").WithCredentials("minio", "minio") + .Build(); + } + + [TestMethod] + [ExpectedException(typeof(InvalidEndpointException))] + public void TestUrlFailsWithQuery() + { + using var client = new MinioClient() + .WithEndpoint("localhost:9000/?foo=bar") + .WithCredentials("minio", "minio") + .Build(); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestSetAppInfoFailsNullApp() + { + using var client = new MinioClient() + .WithEndpoint("localhost", 9000) + .WithCredentials("minio", "minio") + .Build(); + client.SetAppInfo(null, "1.2.2"); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void TestSetAppInfoFailsNullVersion() + { + using var client = new MinioClient() + .WithEndpoint("localhost", 9000) + .WithCredentials("minio", "minio") + .Build(); + client.SetAppInfo("Hello-App", null); + } + + [TestMethod] + public void TestSetAppInfoSuccess() + { + using var client = new MinioClient() + .WithEndpoint("localhost", 9000) + .WithCredentials("minio", "minio") + .Build(); + client.SetAppInfo("Hello-App", "1.2.1"); + } + + [TestMethod] + public void TestEndpointSuccess() + { + using var client = new MinioClient() + .WithEndpoint("s3.amazonaws.com") + .WithCredentials("minio", "minio") + .Build(); + } +} \ No newline at end of file diff --git a/Minio/Helper/OperationsHelper.cs b/Minio/Helper/OperationsHelper.cs index a060bea234..bb27cce3f6 100644 --- a/Minio/Helper/OperationsHelper.cs +++ b/Minio/Helper/OperationsHelper.cs @@ -314,35 +314,4 @@ private async Task> RemoveObjectsHelper(RemoveObjectsArgs arg .ConfigureAwait(false); return fullErrorsList; } -} - -public static class OperationsUtil -{ - private static readonly List SupportedHeaders = new() - { - "cache-control", "content-encoding", "content-type", - "x-amz-acl", "content-disposition", "x-minio-extract" - }; - - private static readonly List SSEHeaders = new() - { - "X-Amz-Server-Side-Encryption-Customer-Algorithm", - "X-Amz-Server-Side-Encryption-Customer-Key", - "X-Amz-Server-Side-Encryption-Customer-Key-Md5", - Constants.SSEGenericHeader, - Constants.SSEKMSKeyId, - Constants.SSEKMSContext - }; - - internal static bool IsSupportedHeader(string hdr, IEqualityComparer comparer = null) - { - comparer ??= StringComparer.OrdinalIgnoreCase; - return SupportedHeaders.Contains(hdr, comparer); - } - - internal static bool IsSSEHeader(string hdr, IEqualityComparer comparer = null) - { - comparer ??= StringComparer.OrdinalIgnoreCase; - return SSEHeaders.Contains(hdr, comparer); - } } \ No newline at end of file diff --git a/Minio/Helper/OperationsUtil.cs b/Minio/Helper/OperationsUtil.cs new file mode 100644 index 0000000000..4f6fa0f1a2 --- /dev/null +++ b/Minio/Helper/OperationsUtil.cs @@ -0,0 +1,50 @@ +/* + * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2020 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using Minio.Helper; + +namespace Minio; + +public static class OperationsUtil +{ + private static readonly List SupportedHeaders = new() + { + "cache-control", "content-encoding", "content-type", + "x-amz-acl", "content-disposition", "x-minio-extract" + }; + + private static readonly List SSEHeaders = new() + { + "X-Amz-Server-Side-Encryption-Customer-Algorithm", + "X-Amz-Server-Side-Encryption-Customer-Key", + "X-Amz-Server-Side-Encryption-Customer-Key-Md5", + Constants.SSEGenericHeader, + Constants.SSEKMSKeyId, + Constants.SSEKMSContext + }; + + internal static bool IsSupportedHeader(string hdr, IEqualityComparer comparer = null) + { + comparer ??= StringComparer.OrdinalIgnoreCase; + return SupportedHeaders.Contains(hdr, comparer); + } + + internal static bool IsSSEHeader(string hdr, IEqualityComparer comparer = null) + { + comparer ??= StringComparer.OrdinalIgnoreCase; + return SSEHeaders.Contains(hdr, comparer); + } +} \ No newline at end of file diff --git a/Minio/MinioClient.cs b/Minio/MinioClient.cs index 172dd54aa6..56a63eac14 100644 --- a/Minio/MinioClient.cs +++ b/Minio/MinioClient.cs @@ -127,9 +127,9 @@ private static string SystemUserAgent /// /// Runs httpClient's GetAsync method /// - public async Task WrapperGetAsync(string url) + public Task WrapperGetAsync(string url) { - return await HttpClient.GetAsync(url).ConfigureAwait(false); + return HttpClient.GetAsync(url); } /// diff --git a/Minio/MinioClientExtensions.cs b/Minio/MinioClientExtensions.cs index f39be26146..c42e047654 100644 --- a/Minio/MinioClientExtensions.cs +++ b/Minio/MinioClientExtensions.cs @@ -95,6 +95,7 @@ public static MinioClient WithSSL(this MinioClient minioClient, bool secure = tr /// /// Uses webproxy for all requests if this method is invoked on client object. /// + /// The MinioClient instance used /// Information on the proxy server in the setup. /// public static MinioClient WithProxy(this MinioClient minioClient, IWebProxy proxy) @@ -108,6 +109,7 @@ public static MinioClient WithProxy(this MinioClient minioClient, IWebProxy prox /// /// Uses the set timeout for all requests if this method is invoked on client object /// + /// The MinioClient instance used /// Timeout in milliseconds. /// public static MinioClient WithTimeout(this MinioClient minioClient, int timeout) @@ -121,6 +123,7 @@ public static MinioClient WithTimeout(this MinioClient minioClient, int timeout) /// /// Allows to add retry policy handler /// + /// The MinioClient instance used /// Delegate that will wrap execution of http client requests. /// public static MinioClient WithRetryPolicy(this MinioClient minioClient, @@ -135,6 +138,7 @@ public static MinioClient WithRetryPolicy(this MinioClient minioClient, /// /// Allows end user to define the Http server and pass it as a parameter /// + /// The MinioClient instance used /// Instance of HttpClient /// Dispose the HttpClient when leaving /// From a06ad1c578eebc2f669d3e925cceb5302df19bb8 Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Mon, 8 May 2023 22:43:21 +0200 Subject: [PATCH 03/13] Change clientprovider to interface --- .../Cases/ChainedCredentialProvider.cs | 2 +- Minio/Credentials/AssumeRoleBaseProvider.cs | 6 +++--- Minio/Credentials/CertificateIdentityProvider.cs | 6 +++--- Minio/Credentials/ChainedProvider.cs | 16 ++++++++-------- Minio/Credentials/EnvironmentProvider.cs | 7 ++++++- Minio/Credentials/IAMAWSProvider.cs | 4 ++-- Minio/Credentials/IClientProvider.cs | 6 +++--- Minio/MinioClient.cs | 2 +- Minio/MinioClientExtensions.cs | 2 +- 9 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Minio.Examples/Cases/ChainedCredentialProvider.cs b/Minio.Examples/Cases/ChainedCredentialProvider.cs index 676d1e5755..d66aaca089 100644 --- a/Minio.Examples/Cases/ChainedCredentialProvider.cs +++ b/Minio.Examples/Cases/ChainedCredentialProvider.cs @@ -26,7 +26,7 @@ public static class ChainedCredentialProvider public static async Task Run() { var provider = new ChainedProvider() - .AddProviders(new ClientProvider[] { new AWSEnvironmentProvider(), new MinioEnvironmentProvider() }); + .AddProviders(new IClientProvider[] { new AWSEnvironmentProvider(), new MinioEnvironmentProvider() }); //Chained provider definition here. using var minioClient = new MinioClient() .WithEndpoint("s3.amazonaws.com") diff --git a/Minio/Credentials/AssumeRoleBaseProvider.cs b/Minio/Credentials/AssumeRoleBaseProvider.cs index 1cc57fc4ac..5313332e3e 100644 --- a/Minio/Credentials/AssumeRoleBaseProvider.cs +++ b/Minio/Credentials/AssumeRoleBaseProvider.cs @@ -23,7 +23,7 @@ namespace Minio.Credentials; // Assume-role credential provider -public abstract class AssumeRoleBaseProvider : ClientProvider +public abstract class AssumeRoleBaseProvider : IClientProvider where T : AssumeRoleBaseProvider { internal readonly IEnumerable NoErrorHandlers = @@ -108,7 +108,7 @@ internal virtual async Task BuildRequest() return reqBuilder; } - public override async Task GetCredentialsAsync() + public virtual async Task GetCredentialsAsync() { if (Credentials?.AreExpired() == false) return Credentials; @@ -139,7 +139,7 @@ internal virtual AccessCredentials ParseResponse(HttpResponseMessage response) return Utils.DeserializeXml(stream); } - public override AccessCredentials GetCredentials() + public virtual AccessCredentials GetCredentials() { throw new InvalidOperationException("Please use the GetCredentialsAsync method."); } diff --git a/Minio/Credentials/CertificateIdentityProvider.cs b/Minio/Credentials/CertificateIdentityProvider.cs index 3651946c60..3be6635756 100644 --- a/Minio/Credentials/CertificateIdentityProvider.cs +++ b/Minio/Credentials/CertificateIdentityProvider.cs @@ -69,7 +69,7 @@ public class CertificateResult public AccessCredentials Credentials { get; set; } } -public class CertificateIdentityProvider : ClientProvider +public class CertificateIdentityProvider : IClientProvider { private readonly int DEFAULT_DURATION_IN_SECONDS = 3600; @@ -108,14 +108,14 @@ public CertificateIdentityProvider WithCertificate(X509Certificate2 cert = null) return this; } - public override AccessCredentials GetCredentials() + public AccessCredentials GetCredentials() { var t = Task.Run(async () => await GetCredentialsAsync().ConfigureAwait(false)); t.Wait(); return t.Result; } - public override async Task GetCredentialsAsync() + public async Task GetCredentialsAsync() { if (Credentials?.AreExpired() == false) return Credentials; diff --git a/Minio/Credentials/ChainedProvider.cs b/Minio/Credentials/ChainedProvider.cs index ddecba5c50..0ca2c63c58 100644 --- a/Minio/Credentials/ChainedProvider.cs +++ b/Minio/Credentials/ChainedProvider.cs @@ -19,30 +19,30 @@ namespace Minio.Credentials; -public class ChainedProvider : ClientProvider +public class ChainedProvider : IClientProvider { public ChainedProvider() { - Providers = new List(); + Providers = new List(); } - internal List Providers { get; set; } - internal ClientProvider CurrentProvider { get; set; } + internal List Providers { get; set; } + internal IClientProvider CurrentProvider { get; set; } internal AccessCredentials Credentials { get; set; } - public ChainedProvider AddProvider(ClientProvider provider) + public ChainedProvider AddProvider(IClientProvider provider) { Providers.Add(provider); return this; } - public ChainedProvider AddProviders(ClientProvider[] providers) + public ChainedProvider AddProviders(IClientProvider[] providers) { Providers.AddRange(providers.ToList()); return this; } - public override AccessCredentials GetCredentials() + public AccessCredentials GetCredentials() { if (Credentials?.AreExpired() == false) return Credentials; if (CurrentProvider is not null && !Credentials.AreExpired()) @@ -65,7 +65,7 @@ public override AccessCredentials GetCredentials() throw new InvalidOperationException("None of the assigned providers were able to provide valid credentials."); } - public override async Task GetCredentialsAsync() + public async Task GetCredentialsAsync() { var credentials = GetCredentials(); await Task.Yield(); diff --git a/Minio/Credentials/EnvironmentProvider.cs b/Minio/Credentials/EnvironmentProvider.cs index fd400e68e7..1f8aebf741 100644 --- a/Minio/Credentials/EnvironmentProvider.cs +++ b/Minio/Credentials/EnvironmentProvider.cs @@ -15,10 +15,15 @@ * limitations under the License. */ +using Minio.DataModel; + namespace Minio.Credentials; -public abstract class EnvironmentProvider : ClientProvider +public abstract class EnvironmentProvider : IClientProvider { + public abstract AccessCredentials GetCredentials(); + public abstract Task GetCredentialsAsync(); + internal string GetEnvironmentVariable(string env) { return Environment.GetEnvironmentVariable(env); diff --git a/Minio/Credentials/IAMAWSProvider.cs b/Minio/Credentials/IAMAWSProvider.cs index da997f6873..dd92fc3f08 100644 --- a/Minio/Credentials/IAMAWSProvider.cs +++ b/Minio/Credentials/IAMAWSProvider.cs @@ -67,7 +67,7 @@ public override AccessCredentials GetCredentials() url = RequestUtil.MakeTargetURL("sts." + region + ".amazonaws.com", true); } - ClientProvider provider = new WebIdentityProvider() + IClientProvider provider = new WebIdentityProvider() .WithSTSEndpoint(url) .WithRoleAction("AssumeRoleWithWebIdentity") .WithDurationInSeconds(null) @@ -90,7 +90,7 @@ internal AccessCredentials GetAccessCredentials(string tokenFile) url = new Uri(urlStr); } - ClientProvider provider = new WebIdentityProvider() + IClientProvider provider = new WebIdentityProvider() .WithJWTSupplier(() => { var tokenContents = File.ReadAllText(tokenFile); diff --git a/Minio/Credentials/IClientProvider.cs b/Minio/Credentials/IClientProvider.cs index 4c4e312b93..ff2577246c 100644 --- a/Minio/Credentials/IClientProvider.cs +++ b/Minio/Credentials/IClientProvider.cs @@ -19,8 +19,8 @@ namespace Minio.Credentials; -public abstract class ClientProvider +public interface IClientProvider { - public abstract AccessCredentials GetCredentials(); - public abstract Task GetCredentialsAsync(); + AccessCredentials GetCredentials(); + Task GetCredentialsAsync(); } \ No newline at end of file diff --git a/Minio/MinioClient.cs b/Minio/MinioClient.cs index 56a63eac14..f91f0f82fd 100644 --- a/Minio/MinioClient.cs +++ b/Minio/MinioClient.cs @@ -57,7 +57,7 @@ public partial class MinioClient : IMinioClient private IRequestLogger logger; - internal ClientProvider Provider; + internal IClientProvider Provider; internal string Region; // Cache holding bucket to region mapping for buckets seen so far. diff --git a/Minio/MinioClientExtensions.cs b/Minio/MinioClientExtensions.cs index c42e047654..9b1546ec4b 100644 --- a/Minio/MinioClientExtensions.cs +++ b/Minio/MinioClientExtensions.cs @@ -156,7 +156,7 @@ public static MinioClient WithHttpClient(this MinioClient minioClient, HttpClien /// With provider for credentials and session token if being used /// /// - public static MinioClient WithCredentialsProvider(this MinioClient minioClient, ClientProvider provider) + public static MinioClient WithCredentialsProvider(this MinioClient minioClient, IClientProvider provider) { if (minioClient is null) throw new ArgumentNullException(nameof(minioClient)); From a6055ddf69e7be2b061dc32e2554f586e5baf97d Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Tue, 9 May 2023 09:19:04 +0200 Subject: [PATCH 04/13] Use ValueTask for async methods who only call sync --- Minio/Credentials/AWSEnvironmentProvider.cs | 9 +++------ Minio/Credentials/AssumeRoleBaseProvider.cs | 2 +- Minio/Credentials/AssumeRoleProvider.cs | 2 +- Minio/Credentials/CertificateIdentityProvider.cs | 6 ++---- Minio/Credentials/ChainedProvider.cs | 6 ++---- Minio/Credentials/EnvironmentProvider.cs | 2 +- Minio/Credentials/IAMAWSProvider.cs | 2 +- Minio/Credentials/IClientProvider.cs | 2 +- Minio/Credentials/MinioEnvironmentProvider.cs | 6 ++---- Minio/Credentials/WebIdentityProvider.cs | 2 +- 10 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Minio/Credentials/AWSEnvironmentProvider.cs b/Minio/Credentials/AWSEnvironmentProvider.cs index 47693d36e1..966cce5023 100644 --- a/Minio/Credentials/AWSEnvironmentProvider.cs +++ b/Minio/Credentials/AWSEnvironmentProvider.cs @@ -23,15 +23,12 @@ public class AWSEnvironmentProvider : EnvironmentProvider { public override AccessCredentials GetCredentials() { - var credentials = new AccessCredentials(GetAccessKey(), GetSecretKey(), GetSessionToken(), default); - return credentials; + return new AccessCredentials(GetAccessKey(), GetSecretKey(), GetSessionToken(), default); } - public override async Task GetCredentialsAsync() + public override ValueTask GetCredentialsAsync() { - var creds = GetCredentials(); - await Task.Yield(); - return creds; + return new ValueTask(GetCredentials()); } internal string GetAccessKey() diff --git a/Minio/Credentials/AssumeRoleBaseProvider.cs b/Minio/Credentials/AssumeRoleBaseProvider.cs index 5313332e3e..f7d4e2f3fe 100644 --- a/Minio/Credentials/AssumeRoleBaseProvider.cs +++ b/Minio/Credentials/AssumeRoleBaseProvider.cs @@ -108,7 +108,7 @@ internal virtual async Task BuildRequest() return reqBuilder; } - public virtual async Task GetCredentialsAsync() + public virtual async ValueTask GetCredentialsAsync() { if (Credentials?.AreExpired() == false) return Credentials; diff --git a/Minio/Credentials/AssumeRoleProvider.cs b/Minio/Credentials/AssumeRoleProvider.cs index 97bdf2e08b..6211e5b44e 100644 --- a/Minio/Credentials/AssumeRoleProvider.cs +++ b/Minio/Credentials/AssumeRoleProvider.cs @@ -93,7 +93,7 @@ public AssumeRoleProvider WithSTSEndpoint(string endpoint) return this; } - public override async Task GetCredentialsAsync() + public override async ValueTask GetCredentialsAsync() { if (Credentials?.AreExpired() == false) return Credentials; diff --git a/Minio/Credentials/CertificateIdentityProvider.cs b/Minio/Credentials/CertificateIdentityProvider.cs index 3be6635756..aa93e34388 100644 --- a/Minio/Credentials/CertificateIdentityProvider.cs +++ b/Minio/Credentials/CertificateIdentityProvider.cs @@ -110,12 +110,10 @@ public CertificateIdentityProvider WithCertificate(X509Certificate2 cert = null) public AccessCredentials GetCredentials() { - var t = Task.Run(async () => await GetCredentialsAsync().ConfigureAwait(false)); - t.Wait(); - return t.Result; + return GetCredentialsAsync().ConfigureAwait(false).GetAwaiter().GetResult(); } - public async Task GetCredentialsAsync() + public async ValueTask GetCredentialsAsync() { if (Credentials?.AreExpired() == false) return Credentials; diff --git a/Minio/Credentials/ChainedProvider.cs b/Minio/Credentials/ChainedProvider.cs index 0ca2c63c58..7e510f4e80 100644 --- a/Minio/Credentials/ChainedProvider.cs +++ b/Minio/Credentials/ChainedProvider.cs @@ -65,10 +65,8 @@ public AccessCredentials GetCredentials() throw new InvalidOperationException("None of the assigned providers were able to provide valid credentials."); } - public async Task GetCredentialsAsync() + public ValueTask GetCredentialsAsync() { - var credentials = GetCredentials(); - await Task.Yield(); - return credentials; + return new ValueTask(GetCredentials()); } } \ No newline at end of file diff --git a/Minio/Credentials/EnvironmentProvider.cs b/Minio/Credentials/EnvironmentProvider.cs index 1f8aebf741..9280e250df 100644 --- a/Minio/Credentials/EnvironmentProvider.cs +++ b/Minio/Credentials/EnvironmentProvider.cs @@ -22,7 +22,7 @@ namespace Minio.Credentials; public abstract class EnvironmentProvider : IClientProvider { public abstract AccessCredentials GetCredentials(); - public abstract Task GetCredentialsAsync(); + public abstract ValueTask GetCredentialsAsync(); internal string GetEnvironmentVariable(string env) { diff --git a/Minio/Credentials/IAMAWSProvider.cs b/Minio/Credentials/IAMAWSProvider.cs index dd92fc3f08..b4952eccb5 100644 --- a/Minio/Credentials/IAMAWSProvider.cs +++ b/Minio/Credentials/IAMAWSProvider.cs @@ -141,7 +141,7 @@ await Minio_Client.ExecuteTaskAsync(Enumerable.Empty GetCredentialsAsync() + public override async ValueTask GetCredentialsAsync() { if (Credentials?.AreExpired() == false) return Credentials; diff --git a/Minio/Credentials/IClientProvider.cs b/Minio/Credentials/IClientProvider.cs index ff2577246c..132b7d36ba 100644 --- a/Minio/Credentials/IClientProvider.cs +++ b/Minio/Credentials/IClientProvider.cs @@ -22,5 +22,5 @@ namespace Minio.Credentials; public interface IClientProvider { AccessCredentials GetCredentials(); - Task GetCredentialsAsync(); + ValueTask GetCredentialsAsync(); } \ No newline at end of file diff --git a/Minio/Credentials/MinioEnvironmentProvider.cs b/Minio/Credentials/MinioEnvironmentProvider.cs index 25a3f540ef..a0648a049a 100644 --- a/Minio/Credentials/MinioEnvironmentProvider.cs +++ b/Minio/Credentials/MinioEnvironmentProvider.cs @@ -28,10 +28,8 @@ public override AccessCredentials GetCredentials() return credentials; } - public override async Task GetCredentialsAsync() + public override ValueTask GetCredentialsAsync() { - var credentials = GetCredentials(); - await Task.Yield(); - return credentials; + return new ValueTask(GetCredentials()); } } \ No newline at end of file diff --git a/Minio/Credentials/WebIdentityProvider.cs b/Minio/Credentials/WebIdentityProvider.cs index bdcec573b7..cf899eaa49 100644 --- a/Minio/Credentials/WebIdentityProvider.cs +++ b/Minio/Credentials/WebIdentityProvider.cs @@ -45,7 +45,7 @@ public override AccessCredentials GetCredentials() return base.GetCredentials(); } - public override Task GetCredentialsAsync() + public override ValueTask GetCredentialsAsync() { Validate(); return base.GetCredentialsAsync(); From 5036e10b30559a0bf0d111e1ac5605ce9da99a2a Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Tue, 9 May 2023 09:47:52 +0200 Subject: [PATCH 05/13] Use Task based stream --- Minio.Examples/Cases/GetPartialObject.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Minio.Examples/Cases/GetPartialObject.cs b/Minio.Examples/Cases/GetPartialObject.cs index 9450f6ab30..556e39d47f 100644 --- a/Minio.Examples/Cases/GetPartialObject.cs +++ b/Minio.Examples/Cases/GetPartialObject.cs @@ -40,7 +40,7 @@ public static async Task Run(IMinioClient minio, .WithBucket(bucketName) .WithObject(objectName) .WithOffsetAndLength(1024L, 4096L) - .WithCallbackStream(async stream => + .WithCallbackStream(async (stream, cancellationToken) => { var fileStream = File.Create(fileName); await stream.CopyToAsync(fileStream).ConfigureAwait(false); From 832e9b702f7852775b16044b7423e21a09af703d Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Tue, 9 May 2023 12:45:29 +0200 Subject: [PATCH 06/13] Cleans --- Minio/Helper/AmazonAwsS3XmlReader.cs | 28 +++++++++++++++++++++++++ Minio/Helper/{s3utils.cs => S3utils.cs} | 0 Minio/Helper/Utils.cs | 9 -------- Minio/V4Authenticator.cs | 3 +-- 4 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 Minio/Helper/AmazonAwsS3XmlReader.cs rename Minio/Helper/{s3utils.cs => S3utils.cs} (100%) diff --git a/Minio/Helper/AmazonAwsS3XmlReader.cs b/Minio/Helper/AmazonAwsS3XmlReader.cs new file mode 100644 index 0000000000..b653c724a6 --- /dev/null +++ b/Minio/Helper/AmazonAwsS3XmlReader.cs @@ -0,0 +1,28 @@ +/* + * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017-2021 MinIO, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System.Xml; + +namespace Minio; + +public class AmazonAwsS3XmlReader : XmlTextReader +{ + public AmazonAwsS3XmlReader(Stream stream) : base(stream) + { + } + + public override string NamespaceURI => "http://s3.amazonaws.com/doc/2006-03-01/"; +} \ No newline at end of file diff --git a/Minio/Helper/s3utils.cs b/Minio/Helper/S3utils.cs similarity index 100% rename from Minio/Helper/s3utils.cs rename to Minio/Helper/S3utils.cs diff --git a/Minio/Helper/Utils.cs b/Minio/Helper/Utils.cs index 805e6fd738..5f38b64fdd 100644 --- a/Minio/Helper/Utils.cs +++ b/Minio/Helper/Utils.cs @@ -1028,12 +1028,3 @@ private static string GetNamespace() return null; } } - -public class AmazonAwsS3XmlReader : XmlTextReader -{ - public AmazonAwsS3XmlReader(Stream stream) : base(stream) - { - } - - public override string NamespaceURI => "http://s3.amazonaws.com/doc/2006-03-01/"; -} \ No newline at end of file diff --git a/Minio/V4Authenticator.cs b/Minio/V4Authenticator.cs index f549e79662..35f5c5d775 100644 --- a/Minio/V4Authenticator.cs +++ b/Minio/V4Authenticator.cs @@ -116,8 +116,7 @@ public string Authenticate(HttpRequestMessageBuilder requestBuilder, bool isSts ReadOnlySpan stringToSignBytes = Encoding.UTF8.GetBytes(stringToSign); var signatureBytes = SignHmac(signingKey, stringToSignBytes); var signature = BytesToHex(signatureBytes); - var authorization = GetAuthorizationHeader(signedHeaders, signature, signingDate, endpointRegion, isSts); - return authorization; + return GetAuthorizationHeader(signedHeaders, signature, signingDate, endpointRegion, isSts); } /// From 5914fb667abaad94d2aa0eeb5d4b37989b93d722 Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Tue, 9 May 2023 14:24:11 +0200 Subject: [PATCH 07/13] Fixes --- Directory.Build.props | 4 ++-- Minio/Credentials/CertificateIdentityProvider.cs | 2 +- Minio/Credentials/IAMAWSProvider.cs | 2 +- Minio/Helper/S3utils.cs | 3 +-- Minio/Helper/Utils.cs | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index b847cc0e05..7456286d1a 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -61,7 +61,7 @@ - + \ No newline at end of file diff --git a/Minio/Credentials/CertificateIdentityProvider.cs b/Minio/Credentials/CertificateIdentityProvider.cs index aa93e34388..68dded0fe2 100644 --- a/Minio/Credentials/CertificateIdentityProvider.cs +++ b/Minio/Credentials/CertificateIdentityProvider.cs @@ -110,7 +110,7 @@ public CertificateIdentityProvider WithCertificate(X509Certificate2 cert = null) public AccessCredentials GetCredentials() { - return GetCredentialsAsync().ConfigureAwait(false).GetAwaiter().GetResult(); + return GetCredentialsAsync().AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); } public async ValueTask GetCredentialsAsync() diff --git a/Minio/Credentials/IAMAWSProvider.cs b/Minio/Credentials/IAMAWSProvider.cs index b4952eccb5..d7207908a6 100644 --- a/Minio/Credentials/IAMAWSProvider.cs +++ b/Minio/Credentials/IAMAWSProvider.cs @@ -227,7 +227,7 @@ public IAMAWSProvider WithMinioClient(MinioClient minio) Minio_Client = minio; if (Credentials is null || string.IsNullOrWhiteSpace(Credentials.AccessKey) || string.IsNullOrWhiteSpace(Credentials.SecretKey)) - Credentials = GetCredentialsAsync().GetAwaiter().GetResult(); + Credentials = GetCredentialsAsync().AsTask().GetAwaiter().GetResult(); return this; } diff --git a/Minio/Helper/S3utils.cs b/Minio/Helper/S3utils.cs index 0386153f1f..392d4a9bdc 100644 --- a/Minio/Helper/S3utils.cs +++ b/Minio/Helper/S3utils.cs @@ -61,8 +61,7 @@ internal static string GetPath(string p1, string p2) { var combination = Path.Combine(p1, p2); // combination = Uri.EscapeUriString(combination); - combination = Utils.EncodePath(combination); - return combination; + return Utils.EncodePath(combination); } catch (Exception ex) { diff --git a/Minio/Helper/Utils.cs b/Minio/Helper/Utils.cs index 5f38b64fdd..d2815760c4 100644 --- a/Minio/Helper/Utils.cs +++ b/Minio/Helper/Utils.cs @@ -250,7 +250,7 @@ internal static string GetMD5SumStr(ReadOnlySpan key) using var md5 = MD5.Create(); var hashedBytes = md5.ComputeHash(key.ToArray()); #else - var hashedBytes = MD5.HashData(key); + ReadOnlySpan hashedBytes = MD5.HashData(key); #endif return Convert.ToBase64String(hashedBytes); } From cca972a19ef9e476af44be51cfbfdcba3a967fad Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Tue, 9 May 2023 15:11:29 +0200 Subject: [PATCH 08/13] Format --- Minio.Tests/UnitTest1.cs | 2 +- Minio/Credentials/AssumeRoleBaseProvider.cs | 52 +++++++++---------- .../CertificateIdentityProvider.cs | 46 ++++++++-------- Minio/Credentials/ChainedProvider.cs | 24 ++++----- Minio/Helper/OperationsHelper.cs | 1 - Minio/Helper/Utils.cs | 2 +- 6 files changed, 63 insertions(+), 64 deletions(-) diff --git a/Minio.Tests/UnitTest1.cs b/Minio.Tests/UnitTest1.cs index fe04a20cad..a38b497d1c 100644 --- a/Minio.Tests/UnitTest1.cs +++ b/Minio.Tests/UnitTest1.cs @@ -165,4 +165,4 @@ public void TestEndpointFailure() // #endregion -} +} \ No newline at end of file diff --git a/Minio/Credentials/AssumeRoleBaseProvider.cs b/Minio/Credentials/AssumeRoleBaseProvider.cs index f7d4e2f3fe..5549a883e4 100644 --- a/Minio/Credentials/AssumeRoleBaseProvider.cs +++ b/Minio/Credentials/AssumeRoleBaseProvider.cs @@ -49,6 +49,32 @@ protected AssumeRoleBaseProvider() internal string RoleARN { get; set; } internal string ExternalID { get; set; } + public virtual async ValueTask GetCredentialsAsync() + { + if (Credentials?.AreExpired() == false) return Credentials; + + var requestBuilder = await BuildRequest().ConfigureAwait(false); + if (Client is not null) + { + ResponseResult responseMessage = null; + try + { + responseMessage = await Client.ExecuteTaskAsync(NoErrorHandlers, requestBuilder).ConfigureAwait(false); + } + finally + { + responseMessage?.Dispose(); + } + } + + return null; + } + + public virtual AccessCredentials GetCredentials() + { + throw new InvalidOperationException("Please use the GetCredentialsAsync method."); + } + public T WithDurationInSeconds(uint? durationInSeconds) { DurationInSeconds = durationInSeconds; @@ -108,27 +134,6 @@ internal virtual async Task BuildRequest() return reqBuilder; } - public virtual async ValueTask GetCredentialsAsync() - { - if (Credentials?.AreExpired() == false) return Credentials; - - var requestBuilder = await BuildRequest().ConfigureAwait(false); - if (Client is not null) - { - ResponseResult responseMessage = null; - try - { - responseMessage = await Client.ExecuteTaskAsync(NoErrorHandlers, requestBuilder).ConfigureAwait(false); - } - finally - { - responseMessage?.Dispose(); - } - } - - return null; - } - internal virtual AccessCredentials ParseResponse(HttpResponseMessage response) { var content = Convert.ToString(response.Content); @@ -138,9 +143,4 @@ internal virtual AccessCredentials ParseResponse(HttpResponseMessage response) using var stream = Encoding.UTF8.GetBytes(content).AsMemory().AsStream(); return Utils.DeserializeXml(stream); } - - public virtual AccessCredentials GetCredentials() - { - throw new InvalidOperationException("Please use the GetCredentialsAsync method."); - } } \ No newline at end of file diff --git a/Minio/Credentials/CertificateIdentityProvider.cs b/Minio/Credentials/CertificateIdentityProvider.cs index 68dded0fe2..f2466ddb0f 100644 --- a/Minio/Credentials/CertificateIdentityProvider.cs +++ b/Minio/Credentials/CertificateIdentityProvider.cs @@ -85,29 +85,6 @@ public CertificateIdentityProvider() internal HttpClient HttpClient { get; set; } internal AccessCredentials Credentials { get; set; } - public CertificateIdentityProvider WithStsEndpoint(string stsEndpoint) - { - if (string.IsNullOrEmpty(stsEndpoint)) - throw new InvalidEndpointException("Missing mandatory argument: stsEndpoint"); - if (!stsEndpoint.StartsWith("https", StringComparison.OrdinalIgnoreCase)) - throw new InvalidEndpointException($"stsEndpoint {stsEndpoint} is invalid." + " The scheme must be https"); - - StsEndpoint = stsEndpoint; - return this; - } - - public CertificateIdentityProvider WithHttpClient(HttpClient httpClient = null) - { - HttpClient = httpClient; - return this; - } - - public CertificateIdentityProvider WithCertificate(X509Certificate2 cert = null) - { - ClientCertificate = cert; - return this; - } - public AccessCredentials GetCredentials() { return GetCredentialsAsync().AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); @@ -140,6 +117,29 @@ public async ValueTask GetCredentialsAsync() return Credentials; } + public CertificateIdentityProvider WithStsEndpoint(string stsEndpoint) + { + if (string.IsNullOrEmpty(stsEndpoint)) + throw new InvalidEndpointException("Missing mandatory argument: stsEndpoint"); + if (!stsEndpoint.StartsWith("https", StringComparison.OrdinalIgnoreCase)) + throw new InvalidEndpointException($"stsEndpoint {stsEndpoint} is invalid." + " The scheme must be https"); + + StsEndpoint = stsEndpoint; + return this; + } + + public CertificateIdentityProvider WithHttpClient(HttpClient httpClient = null) + { + HttpClient = httpClient; + return this; + } + + public CertificateIdentityProvider WithCertificate(X509Certificate2 cert = null) + { + ClientCertificate = cert; + return this; + } + public CertificateIdentityProvider Build() { if (string.IsNullOrEmpty(DurationInSeconds.ToString())) diff --git a/Minio/Credentials/ChainedProvider.cs b/Minio/Credentials/ChainedProvider.cs index 7e510f4e80..f6c0a59d27 100644 --- a/Minio/Credentials/ChainedProvider.cs +++ b/Minio/Credentials/ChainedProvider.cs @@ -30,18 +30,6 @@ public ChainedProvider() internal IClientProvider CurrentProvider { get; set; } internal AccessCredentials Credentials { get; set; } - public ChainedProvider AddProvider(IClientProvider provider) - { - Providers.Add(provider); - return this; - } - - public ChainedProvider AddProviders(IClientProvider[] providers) - { - Providers.AddRange(providers.ToList()); - return this; - } - public AccessCredentials GetCredentials() { if (Credentials?.AreExpired() == false) return Credentials; @@ -69,4 +57,16 @@ public ValueTask GetCredentialsAsync() { return new ValueTask(GetCredentials()); } + + public ChainedProvider AddProvider(IClientProvider provider) + { + Providers.Add(provider); + return this; + } + + public ChainedProvider AddProviders(IClientProvider[] providers) + { + Providers.AddRange(providers.ToList()); + return this; + } } \ No newline at end of file diff --git a/Minio/Helper/OperationsHelper.cs b/Minio/Helper/OperationsHelper.cs index bb27cce3f6..aa37e17fb5 100644 --- a/Minio/Helper/OperationsHelper.cs +++ b/Minio/Helper/OperationsHelper.cs @@ -16,7 +16,6 @@ using Minio.DataModel; using Minio.Exceptions; -using Minio.Helper; namespace Minio; diff --git a/Minio/Helper/Utils.cs b/Minio/Helper/Utils.cs index d2815760c4..7399ce89d7 100644 --- a/Minio/Helper/Utils.cs +++ b/Minio/Helper/Utils.cs @@ -1027,4 +1027,4 @@ private static string GetNamespace() return null; } -} +} \ No newline at end of file From 7049328f9eb586b06fdae124d51943e9f59de16a Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Tue, 9 May 2023 15:27:32 +0200 Subject: [PATCH 09/13] Update tool --- .config/dotnet-tools.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index cac204c418..93b803f9c6 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -3,7 +3,7 @@ "isRoot": true, "tools": { "jetbrains.resharper.globaltools": { - "version": "2023.1.0", + "version": "2023.1.1", "commands": [ "jb" ] From 396d2d28a9ee5a8ec02b5137d48257e1920dbf30 Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Tue, 9 May 2023 15:44:50 +0200 Subject: [PATCH 10/13] Move all to interface --- Minio/Credentials/AWSEnvironmentProvider.cs | 6 +- Minio/Credentials/EnvironmentProvider.cs | 31 -------- Minio/Credentials/IAMAWSProvider.cs | 72 +++++++++---------- Minio/Credentials/MinioEnvironmentProvider.cs | 10 +-- 4 files changed, 44 insertions(+), 75 deletions(-) delete mode 100644 Minio/Credentials/EnvironmentProvider.cs diff --git a/Minio/Credentials/AWSEnvironmentProvider.cs b/Minio/Credentials/AWSEnvironmentProvider.cs index 966cce5023..b9b1846cf3 100644 --- a/Minio/Credentials/AWSEnvironmentProvider.cs +++ b/Minio/Credentials/AWSEnvironmentProvider.cs @@ -19,14 +19,14 @@ namespace Minio.Credentials; -public class AWSEnvironmentProvider : EnvironmentProvider +public class AWSEnvironmentProvider : IClientProvider { - public override AccessCredentials GetCredentials() + public AccessCredentials GetCredentials() { return new AccessCredentials(GetAccessKey(), GetSecretKey(), GetSessionToken(), default); } - public override ValueTask GetCredentialsAsync() + public ValueTask GetCredentialsAsync() { return new ValueTask(GetCredentials()); } diff --git a/Minio/Credentials/EnvironmentProvider.cs b/Minio/Credentials/EnvironmentProvider.cs deleted file mode 100644 index 9280e250df..0000000000 --- a/Minio/Credentials/EnvironmentProvider.cs +++ /dev/null @@ -1,31 +0,0 @@ -/* - * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, - * (C) 2021 MinIO, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -using Minio.DataModel; - -namespace Minio.Credentials; - -public abstract class EnvironmentProvider : IClientProvider -{ - public abstract AccessCredentials GetCredentials(); - public abstract ValueTask GetCredentialsAsync(); - - internal string GetEnvironmentVariable(string env) - { - return Environment.GetEnvironmentVariable(env); - } -} \ No newline at end of file diff --git a/Minio/Credentials/IAMAWSProvider.cs b/Minio/Credentials/IAMAWSProvider.cs index d7207908a6..d4c65808aa 100644 --- a/Minio/Credentials/IAMAWSProvider.cs +++ b/Minio/Credentials/IAMAWSProvider.cs @@ -28,7 +28,7 @@ namespace Minio.Credentials; -public class IAMAWSProvider : EnvironmentProvider +public class IAMAWSProvider : IClientProvider { public IAMAWSProvider() { @@ -54,7 +54,7 @@ public IAMAWSProvider(string endpoint, MinioClient client) internal AccessCredentials Credentials { get; set; } internal MinioClient Minio_Client { get; set; } - public override AccessCredentials GetCredentials() + public AccessCredentials GetCredentials() { Validate(); var url = CustomEndPoint; @@ -78,6 +78,40 @@ public override AccessCredentials GetCredentials() return Credentials; } + public async ValueTask GetCredentialsAsync() + { + if (Credentials?.AreExpired() == false) return Credentials; + + var url = CustomEndPoint; + var awsTokenFile = Environment.GetEnvironmentVariable("AWS_WEB_IDENTITY_TOKEN_FILE"); + if (!string.IsNullOrWhiteSpace(awsTokenFile)) + { + Credentials = GetAccessCredentials(awsTokenFile); + return Credentials; + } + + var containerRelativeUri = Environment.GetEnvironmentVariable("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"); + var containerFullUri = Environment.GetEnvironmentVariable("AWS_CONTAINER_CREDENTIALS_FULL_URI"); + var isURLEmpty = url is null; + if (!string.IsNullOrWhiteSpace(containerRelativeUri) && isURLEmpty) + { + url = RequestUtil.MakeTargetURL("169.254.170.2" + "/" + containerRelativeUri, false); + } + else if (!string.IsNullOrWhiteSpace(containerFullUri) && isURLEmpty) + { + var fullUri = new Uri(containerFullUri); + url = RequestUtil.MakeTargetURL(fullUri.AbsolutePath, + string.Equals(fullUri.Scheme, "https", StringComparison.OrdinalIgnoreCase)); + } + else + { + url = await GetIamRoleNamedURL().ConfigureAwait(false); + } + + Credentials = await GetAccessCredentials(url).ConfigureAwait(false); + return Credentials; + } + internal AccessCredentials GetAccessCredentials(string tokenFile) { Validate(); @@ -141,40 +175,6 @@ await Minio_Client.ExecuteTaskAsync(Enumerable.Empty GetCredentialsAsync() - { - if (Credentials?.AreExpired() == false) return Credentials; - - var url = CustomEndPoint; - var awsTokenFile = Environment.GetEnvironmentVariable("AWS_WEB_IDENTITY_TOKEN_FILE"); - if (!string.IsNullOrWhiteSpace(awsTokenFile)) - { - Credentials = GetAccessCredentials(awsTokenFile); - return Credentials; - } - - var containerRelativeUri = Environment.GetEnvironmentVariable("AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"); - var containerFullUri = Environment.GetEnvironmentVariable("AWS_CONTAINER_CREDENTIALS_FULL_URI"); - var isURLEmpty = url is null; - if (!string.IsNullOrWhiteSpace(containerRelativeUri) && isURLEmpty) - { - url = RequestUtil.MakeTargetURL("169.254.170.2" + "/" + containerRelativeUri, false); - } - else if (!string.IsNullOrWhiteSpace(containerFullUri) && isURLEmpty) - { - var fullUri = new Uri(containerFullUri); - url = RequestUtil.MakeTargetURL(fullUri.AbsolutePath, - string.Equals(fullUri.Scheme, "https", StringComparison.OrdinalIgnoreCase)); - } - else - { - url = await GetIamRoleNamedURL().ConfigureAwait(false); - } - - Credentials = await GetAccessCredentials(url).ConfigureAwait(false); - return Credentials; - } - public async Task GetIamRoleNameAsync(Uri url) { Validate(); diff --git a/Minio/Credentials/MinioEnvironmentProvider.cs b/Minio/Credentials/MinioEnvironmentProvider.cs index a0648a049a..51e46b63f1 100644 --- a/Minio/Credentials/MinioEnvironmentProvider.cs +++ b/Minio/Credentials/MinioEnvironmentProvider.cs @@ -19,16 +19,16 @@ namespace Minio.Credentials; -public class MinioEnvironmentProvider : EnvironmentProvider +public class MinioEnvironmentProvider : IClientProvider { - public override AccessCredentials GetCredentials() + public AccessCredentials GetCredentials() { - var credentials = new AccessCredentials(GetEnvironmentVariable("MINIO_ACCESS_KEY"), - GetEnvironmentVariable("MINIO_SECRET_KEY"), null, default); + var credentials = new AccessCredentials(Environment.GetEnvironmentVariable("MINIO_ACCESS_KEY"), + Environment.GetEnvironmentVariable("MINIO_SECRET_KEY"), null, default); return credentials; } - public override ValueTask GetCredentialsAsync() + public ValueTask GetCredentialsAsync() { return new ValueTask(GetCredentials()); } From d654e95869accfe4e3efe13c193d3a749263f2d7 Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Wed, 10 May 2023 14:12:22 +0200 Subject: [PATCH 11/13] New user and password env --- Minio/Credentials/MinioEnvironmentProvider.cs | 13 ++++++++++--- Minio/MinioClient.cs | 6 ------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Minio/Credentials/MinioEnvironmentProvider.cs b/Minio/Credentials/MinioEnvironmentProvider.cs index 51e46b63f1..dfdec04513 100644 --- a/Minio/Credentials/MinioEnvironmentProvider.cs +++ b/Minio/Credentials/MinioEnvironmentProvider.cs @@ -23,9 +23,16 @@ public class MinioEnvironmentProvider : IClientProvider { public AccessCredentials GetCredentials() { - var credentials = new AccessCredentials(Environment.GetEnvironmentVariable("MINIO_ACCESS_KEY"), - Environment.GetEnvironmentVariable("MINIO_SECRET_KEY"), null, default); - return credentials; + var accessKey = Environment.GetEnvironmentVariable("MINIO_ROOT_USER"); + var secretKey = Environment.GetEnvironmentVariable("MINIO_ROOT_PASSWORD"); + + if(string.IsNullOrEmpty(accessKey)) + accessKey = Environment.GetEnvironmentVariable("MINIO_ACCESS_KEY"); + + if (string.IsNullOrEmpty(secretKey)) + secretKey = Environment.GetEnvironmentVariable("MINIO_SECRET_KEY"); + + return new AccessCredentials(accessKey, secretKey, null, default); } public ValueTask GetCredentialsAsync() diff --git a/Minio/MinioClient.cs b/Minio/MinioClient.cs index f91f0f82fd..e96d845478 100644 --- a/Minio/MinioClient.cs +++ b/Minio/MinioClient.cs @@ -28,12 +28,6 @@ namespace Minio; -public class InnerItemType -{ - public int SortOrder { get; set; } - public string Value { get; set; } -} - public partial class MinioClient : IMinioClient { private const string RegistryAuthHeaderKey = "X-Registry-Auth"; From ca9c723df7cea98b334e50be3464bc78169d5a28 Mon Sep 17 00:00:00 2001 From: Martijn van Dijk Date: Wed, 10 May 2023 14:15:35 +0200 Subject: [PATCH 12/13] Format --- Minio/Credentials/MinioEnvironmentProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Minio/Credentials/MinioEnvironmentProvider.cs b/Minio/Credentials/MinioEnvironmentProvider.cs index dfdec04513..4b8c26decb 100644 --- a/Minio/Credentials/MinioEnvironmentProvider.cs +++ b/Minio/Credentials/MinioEnvironmentProvider.cs @@ -26,7 +26,7 @@ public AccessCredentials GetCredentials() var accessKey = Environment.GetEnvironmentVariable("MINIO_ROOT_USER"); var secretKey = Environment.GetEnvironmentVariable("MINIO_ROOT_PASSWORD"); - if(string.IsNullOrEmpty(accessKey)) + if (string.IsNullOrEmpty(accessKey)) accessKey = Environment.GetEnvironmentVariable("MINIO_ACCESS_KEY"); if (string.IsNullOrEmpty(secretKey)) From aa9a46fce6feaaa7d560dc3b77e8be954a9492a6 Mon Sep 17 00:00:00 2001 From: Ersan Date: Sun, 21 May 2023 02:10:29 -0700 Subject: [PATCH 13/13] Replaces the shared filename --- Minio.Functional.Tests/FunctionalTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Minio.Functional.Tests/FunctionalTest.cs b/Minio.Functional.Tests/FunctionalTest.cs index 7dcf730047..be372637c5 100644 --- a/Minio.Functional.Tests/FunctionalTest.cs +++ b/Minio.Functional.Tests/FunctionalTest.cs @@ -5353,7 +5353,7 @@ internal static async Task PresignedGetObject_Test1(MinioClient minio) var bucketName = GetRandomName(15); var objectName = GetRandomObjectName(10); var expiresInt = 1000; - var downloadFile = "downloadFileName"; + var downloadFile = GetRandomObjectName(10); var args = new Dictionary { @@ -5481,7 +5481,7 @@ internal static async Task PresignedGetObject_Test3(MinioClient minio) var objectName = GetRandomObjectName(10); var expiresInt = 1000; var reqDate = DateTime.UtcNow.AddSeconds(-50); - var downloadFile = "downloadFileName"; + var downloadFile = GetRandomObjectName(10); var args = new Dictionary { { "bucketName", bucketName },