From 797b1da15c76f6598dcc48f675c1b82cb27a17ed Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 23 Apr 2019 00:14:35 +0800 Subject: Remove qcloud cs sdk. I will write one by myself. Develop signature algorithm. --- Timeline.Tests/QCloudCosServiceUnitTest.cs | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Timeline.Tests/QCloudCosServiceUnitTest.cs (limited to 'Timeline.Tests/QCloudCosServiceUnitTest.cs') diff --git a/Timeline.Tests/QCloudCosServiceUnitTest.cs b/Timeline.Tests/QCloudCosServiceUnitTest.cs new file mode 100644 index 00000000..c02f70be --- /dev/null +++ b/Timeline.Tests/QCloudCosServiceUnitTest.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using Timeline.Services; +using Xunit; + +namespace Timeline.Tests +{ + public class QCloudCosServiceUnitTest + { + [Fact] + public void GenerateSignatureTest() + { + var credential = new QCloudCosService.QCloudCredentials + { + SecretId = "AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q", + SecretKey = "BQYIM75p8x0iWVFSIgqEKwFprpRSVHlz" + }; + + var request = new QCloudCosService.RequestInfo + { + Method = "put", + Uri = "/exampleobject", + Parameters = new Dictionary(), + Headers = new Dictionary + { + ["Host"] = "examplebucket-1250000000.cos.ap-beijing.myqcloud.com", + ["x-cos-storage-class"] = "standard", + ["x-cos-content-sha1"] = "b502c3a1f48c8609ae212cdfb639dee39673f5e" + } + }; + + var signValidTime = new QCloudCosService.TimeDuration + { + Start = DateTimeOffset.FromUnixTimeSeconds(1417773892), + End = DateTimeOffset.FromUnixTimeSeconds(1417853898) + }; + + Assert.Equal("q-sign-algorithm=sha1&q-ak=AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q&q-sign-time=1417773892;1417853898&q-key-time=1417773892;1417853898&q-header-list=host;x-cos-content-sha1;x-cos-storage-class&q-url-param-list=&q-signature=0ab12f43e74cbe148d705cd9fae8adc9a6d39cc1", QCloudCosService.GenerateSign(credential, request, signValidTime)); + } + } +} -- cgit v1.2.3 From 3d6938ca60691f73bc0b570e7ca4af4f8251741c Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 24 Apr 2019 00:22:25 +0800 Subject: Add ObjectExists implementation in cos. Remove Test host environment. --- .../Helpers/WebApplicationFactoryExtensions.cs | 1 - Timeline.Tests/QCloudCosServiceUnitTest.cs | 33 +++++++- Timeline/EnvironmentConstants.cs | 14 ---- Timeline/Services/TencentCloudCosService.cs | 88 +++++++++++++++++++--- Timeline/Startup.cs | 11 +-- Timeline/appsettings.Test.json | 14 ---- 6 files changed, 112 insertions(+), 49 deletions(-) delete mode 100644 Timeline/EnvironmentConstants.cs delete mode 100644 Timeline/appsettings.Test.json (limited to 'Timeline.Tests/QCloudCosServiceUnitTest.cs') diff --git a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs index a34217f4..a7616b41 100644 --- a/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs +++ b/Timeline.Tests/Helpers/WebApplicationFactoryExtensions.cs @@ -15,7 +15,6 @@ namespace Timeline.Tests.Helpers return factory.WithWebHostBuilder(builder => { builder - .UseEnvironment(EnvironmentConstants.TestEnvironmentName) .ConfigureLogging(logging => { logging.AddXunit(outputHelper); diff --git a/Timeline.Tests/QCloudCosServiceUnitTest.cs b/Timeline.Tests/QCloudCosServiceUnitTest.cs index c02f70be..b99352b9 100644 --- a/Timeline.Tests/QCloudCosServiceUnitTest.cs +++ b/Timeline.Tests/QCloudCosServiceUnitTest.cs @@ -1,12 +1,24 @@ -using System; +using Microsoft.AspNetCore.Mvc.Testing; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; +using System.Threading.Tasks; using Timeline.Services; +using Timeline.Tests.Helpers; using Xunit; +using Xunit.Abstractions; namespace Timeline.Tests { - public class QCloudCosServiceUnitTest + public class QCloudCosServiceUnitTest : IClassFixture> { + private readonly WebApplicationFactory _factory; + + public QCloudCosServiceUnitTest(WebApplicationFactory factory, ITestOutputHelper outputHelper) + { + _factory = factory.WithTestConfig(outputHelper); + } + [Fact] public void GenerateSignatureTest() { @@ -37,5 +49,22 @@ namespace Timeline.Tests Assert.Equal("q-sign-algorithm=sha1&q-ak=AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q&q-sign-time=1417773892;1417853898&q-key-time=1417773892;1417853898&q-header-list=host;x-cos-content-sha1;x-cos-storage-class&q-url-param-list=&q-signature=0ab12f43e74cbe148d705cd9fae8adc9a6d39cc1", QCloudCosService.GenerateSign(credential, request, signValidTime)); } + + /* + [Fact] + public async Task ObjectExistsTest() + { + _factory.CreateDefaultClient().Dispose(); + + using (var serviceScope = _factory.Server.Host.Services.CreateScope()) + { + var services = serviceScope.ServiceProvider; + var service = services.GetRequiredService(); + Assert.True(await service.ObjectExists("avatar", "__default")); + Assert.False(await service.ObjectExists("avatar", "haha")); + Assert.False(await service.ObjectExists("haha", "haha")); + } + } + */ } } diff --git a/Timeline/EnvironmentConstants.cs b/Timeline/EnvironmentConstants.cs deleted file mode 100644 index 5ffc3623..00000000 --- a/Timeline/EnvironmentConstants.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Microsoft.AspNetCore.Hosting; - -namespace Timeline -{ - public static class EnvironmentConstants - { - public const string TestEnvironmentName = "Test"; - - public static bool IsTest(this IHostingEnvironment environment) - { - return environment.EnvironmentName == TestEnvironmentName; - } - } -} diff --git a/Timeline/Services/TencentCloudCosService.cs b/Timeline/Services/TencentCloudCosService.cs index 1bfcf745..8dbd3614 100644 --- a/Timeline/Services/TencentCloudCosService.cs +++ b/Timeline/Services/TencentCloudCosService.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Net; +using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; @@ -20,13 +21,15 @@ namespace Timeline.Services public class QCloudCosService : IQCloudCosService { - private readonly QCloudCosConfig _config; + private readonly IOptionsMonitor _config; private readonly ILogger _logger; + private readonly IHttpClientFactory _httpClientFactory; - public QCloudCosService(IOptions config, ILogger logger) + public QCloudCosService(IOptionsMonitor config, ILogger logger, IHttpClientFactory httpClientFactory) { - _config = config.Value; + _config = config; _logger = logger; + _httpClientFactory = httpClientFactory; } public class QCloudCredentials @@ -45,6 +48,17 @@ namespace Timeline.Services public class TimeDuration { + public TimeDuration() + { + + } + + public TimeDuration(DateTimeOffset start, DateTimeOffset end) + { + Start = start; + End = end; + } + public DateTimeOffset Start { get; set; } public DateTimeOffset End { get; set; } } @@ -57,14 +71,15 @@ namespace Timeline.Services Debug.Assert(request != null); Debug.Assert(request.Method != null); Debug.Assert(request.Uri != null); - Debug.Assert(request.Parameters != null); - Debug.Assert(request.Headers != null); Debug.Assert(signValidTime != null); Debug.Assert(signValidTime.Start < signValidTime.End, "Start must be before End in sign valid time."); List<(string key, string value)> Transform(IEnumerable> raw) { - var sorted= raw.Select(p => (key: p.Key.ToLower(), value: WebUtility.UrlEncode(p.Value))).ToList(); + if (raw == null) + return new List<(string key, string value)>(); + + var sorted = raw.Select(p => (key: p.Key.ToLower(), value: WebUtility.UrlEncode(p.Value))).ToList(); sorted.Sort((left, right) => string.CompareOrdinal(left.key, right.key)); return sorted; } @@ -103,7 +118,7 @@ namespace Timeline.Services } var httpString = new StringBuilder() - .Append(request.Method).Append('\n') + .Append(request.Method.ToLower()).Append('\n') .Append(request.Uri).Append('\n') .Append(Join(transformedParameters)).Append('\n') .Append(Join(transformedHeaders)).Append('\n') @@ -130,9 +145,64 @@ namespace Timeline.Services return Join(result); } - public Task ObjectExists(string bucket, string key) + private QCloudCredentials GetCredentials() { - throw new NotImplementedException(); + var config = _config.CurrentValue; + return new QCloudCredentials + { + SecretId = config.SecretId, + SecretKey = config.SecretKey + }; + } + + private string GetHost(string bucket) + { + var config = _config.CurrentValue; + return $"{bucket}-{config.AppId}.cos.{config.Region}.myqcloud.com"; + } + + public async Task ObjectExists(string bucket, string key) + { + if (bucket == null) + throw new ArgumentNullException(nameof(bucket)); + if (key == null) + throw new ArgumentNullException(nameof(key)); + + var client = _httpClientFactory.CreateClient(); + + var host = GetHost(bucket); + + var request = new HttpRequestMessage(); + request.Method = HttpMethod.Head; + request.RequestUri = new Uri($"https://{host}/{key}"); + request.Headers.Host = host; + request.Headers.Date = DateTimeOffset.Now; + request.Headers.TryAddWithoutValidation("Authorization", GenerateSign(GetCredentials(), new RequestInfo + { + Method = "head", + Uri = "/" + key, + Headers = new Dictionary + { + ["Host"] = host + } + }, new TimeDuration(DateTimeOffset.Now, DateTimeOffset.Now.AddMinutes(2)))); + + try + { + var response = await client.SendAsync(request); + + if (response.IsSuccessStatusCode) + return true; + if (response.StatusCode == HttpStatusCode.NotFound) + return false; + + throw new Exception($"Unknown response code. {response.ToString()}"); + } + catch (Exception e) + { + _logger.LogError(e, "An error occured when test a cos object existence."); + return false; + } } public string GetObjectUrl(string bucket, string key) diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 12d60843..46d0afe5 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -81,6 +81,8 @@ namespace Timeline }); }); + services.AddHttpClient(); + services.Configure(Configuration.GetSection(nameof(QCloudCosConfig))); services.AddSingleton(); } @@ -88,15 +90,6 @@ namespace Timeline // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { - if (Environment.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else - { - app.UseExceptionHandler("/Error"); - } - app.UseCors(corsPolicyName); app.UseForwardedHeaders(new ForwardedHeadersOptions diff --git a/Timeline/appsettings.Test.json b/Timeline/appsettings.Test.json deleted file mode 100644 index ea32348b..00000000 --- a/Timeline/appsettings.Test.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information", - "Microsoft.AspNetCore.Authentication": "Debug", - "Microsoft.AspNetCore.Authorization": "Debug" - } - }, - "JwtConfig": { - "SigningKey": "crupest hahahahahahahhahahahahaha" - } -} -- cgit v1.2.3 From 86e2384b8a186234a76611b7cbd505ad4f7dbd63 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 25 Apr 2019 18:53:05 +0800 Subject: Implement generate object get url in cos. --- Timeline.Tests/QCloudCosServiceUnitTest.cs | 45 +++++++++++++++++++--- Timeline/Services/TencentCloudCosService.cs | 58 +++++++++++++++++++++++++---- Timeline/Services/UserService.cs | 6 +-- 3 files changed, 93 insertions(+), 16 deletions(-) (limited to 'Timeline.Tests/QCloudCosServiceUnitTest.cs') diff --git a/Timeline.Tests/QCloudCosServiceUnitTest.cs b/Timeline.Tests/QCloudCosServiceUnitTest.cs index b99352b9..b0e6a868 100644 --- a/Timeline.Tests/QCloudCosServiceUnitTest.cs +++ b/Timeline.Tests/QCloudCosServiceUnitTest.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; +using System.Net; using System.Threading.Tasks; using Timeline.Services; using Timeline.Tests.Helpers; @@ -17,6 +18,19 @@ namespace Timeline.Tests public QCloudCosServiceUnitTest(WebApplicationFactory factory, ITestOutputHelper outputHelper) { _factory = factory.WithTestConfig(outputHelper); + _factory.CreateDefaultClient().Dispose(); // Ensure test server is created. + } + + [Fact] + public void ValidateBucketNameTest() + { + Assert.True(QCloudCosService.ValidateBucketName("hello")); + Assert.True(QCloudCosService.ValidateBucketName("hello0123")); + Assert.True(QCloudCosService.ValidateBucketName("hello0123-hello")); + Assert.False(QCloudCosService.ValidateBucketName("-hello")); + Assert.False(QCloudCosService.ValidateBucketName("hello-")); + Assert.False(QCloudCosService.ValidateBucketName("helloU")); + Assert.False(QCloudCosService.ValidateBucketName("hello!")); } [Fact] @@ -50,21 +64,40 @@ namespace Timeline.Tests Assert.Equal("q-sign-algorithm=sha1&q-ak=AKIDQjz3ltompVjBni5LitkWHFlFpwkn9U5q&q-sign-time=1417773892;1417853898&q-key-time=1417773892;1417853898&q-header-list=host;x-cos-content-sha1;x-cos-storage-class&q-url-param-list=&q-signature=0ab12f43e74cbe148d705cd9fae8adc9a6d39cc1", QCloudCosService.GenerateSign(credential, request, signValidTime)); } - /* +/* +// Tests in this part need secret configs in cos. +#region SecretTests [Fact] public async Task ObjectExistsTest() { - _factory.CreateDefaultClient().Dispose(); + using (var serviceScope = _factory.Server.Host.Services.CreateScope()) + { + var services = serviceScope.ServiceProvider; + var service = services.GetRequiredService(); + Assert.True(await service.IsObjectExists("avatar", "__default")); + Assert.False(await service.IsObjectExists("avatar", "haha")); + Assert.False(await service.IsObjectExists("haha", "haha")); + } + } + // Although this test does not pass on my archlunux system. But the GenerateObjectGetUrl actually works well. + // And I don't know why. + [Fact] + public async Task GenerateObjectGetUrlTest() + { using (var serviceScope = _factory.Server.Host.Services.CreateScope()) { var services = serviceScope.ServiceProvider; var service = services.GetRequiredService(); - Assert.True(await service.ObjectExists("avatar", "__default")); - Assert.False(await service.ObjectExists("avatar", "haha")); - Assert.False(await service.ObjectExists("haha", "haha")); + var url = service.GenerateObjectGetUrl("avatar", "__default"); + using (var client = _factory.CreateClient()) + { + var res = await client.GetAsync(url); + Assert.Equal(HttpStatusCode.OK, res.StatusCode); + } } } - */ +#endregion +*/ } } diff --git a/Timeline/Services/TencentCloudCosService.cs b/Timeline/Services/TencentCloudCosService.cs index 8dbd3614..f4358714 100644 --- a/Timeline/Services/TencentCloudCosService.cs +++ b/Timeline/Services/TencentCloudCosService.cs @@ -8,6 +8,7 @@ using System.Net; using System.Net.Http; using System.Security.Cryptography; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Timeline.Configs; @@ -15,8 +16,21 @@ namespace Timeline.Services { public interface IQCloudCosService { - Task ObjectExists(string bucket, string key); - string GetObjectUrl(string bucket, string key); + /// + /// Test if an object in the bucket exists. + /// + /// The bucket name. + /// The object key. + /// True if exists. False if not. + Task IsObjectExists(string bucket, string key); + + /// + /// Generate a presignated url to access the object. + /// + /// The bucket name. + /// The object key. + /// The presignated url. + string GenerateObjectGetUrl(string bucket, string key); } public class QCloudCosService : IQCloudCosService @@ -32,6 +46,13 @@ namespace Timeline.Services _httpClientFactory = httpClientFactory; } + private const string BucketNamePattern = @"^(([a-z0-9][a-z0-9-]*[a-z0-9])|[a-z0-9])$"; + + public static bool ValidateBucketName(string bucketName) + { + return Regex.IsMatch(bucketName, BucketNamePattern); + } + public class QCloudCredentials { public string SecretId { get; set; } @@ -161,26 +182,29 @@ namespace Timeline.Services return $"{bucket}-{config.AppId}.cos.{config.Region}.myqcloud.com"; } - public async Task ObjectExists(string bucket, string key) + public async Task IsObjectExists(string bucket, string key) { if (bucket == null) throw new ArgumentNullException(nameof(bucket)); if (key == null) throw new ArgumentNullException(nameof(key)); + if (!ValidateBucketName(bucket)) + throw new ArgumentException($"Bucket name is not valid. Param is {bucket} .", nameof(bucket)); var client = _httpClientFactory.CreateClient(); var host = GetHost(bucket); + var encodedKey = WebUtility.UrlEncode(key); var request = new HttpRequestMessage(); request.Method = HttpMethod.Head; - request.RequestUri = new Uri($"https://{host}/{key}"); + request.RequestUri = new Uri($"https://{host}/{encodedKey}"); request.Headers.Host = host; request.Headers.Date = DateTimeOffset.Now; request.Headers.TryAddWithoutValidation("Authorization", GenerateSign(GetCredentials(), new RequestInfo { Method = "head", - Uri = "/" + key, + Uri = "/" + encodedKey, Headers = new Dictionary { ["Host"] = host @@ -205,9 +229,29 @@ namespace Timeline.Services } } - public string GetObjectUrl(string bucket, string key) + public string GenerateObjectGetUrl(string bucket, string key) { - throw new NotImplementedException(); + if (bucket == null) + throw new ArgumentNullException(nameof(bucket)); + if (key == null) + throw new ArgumentNullException(nameof(key)); + if (!ValidateBucketName(bucket)) + throw new ArgumentException($"Bucket name is not valid. Param is {bucket} .", nameof(bucket)); + + var host = GetHost(bucket); + var encodedKey = WebUtility.UrlEncode(key); + + var signature = GenerateSign(GetCredentials(), new RequestInfo + { + Method = "get", + Uri = "/" + encodedKey, + Headers = new Dictionary + { + ["Host"] = host + } + }, new TimeDuration(DateTimeOffset.Now, DateTimeOffset.Now.AddMinutes(6))); + + return $"https://{host}/{encodedKey}?{signature}"; } } } diff --git a/Timeline/Services/UserService.cs b/Timeline/Services/UserService.cs index d1555660..4a47ca0f 100644 --- a/Timeline/Services/UserService.cs +++ b/Timeline/Services/UserService.cs @@ -301,11 +301,11 @@ namespace Timeline.Services public async Task GetAvatarUrl(string username) { - var exists = await _cosService.ObjectExists("avatar", username); + var exists = await _cosService.IsObjectExists("avatar", username); if (exists) - return _cosService.GetObjectUrl("avatar", username); + return _cosService.GenerateObjectGetUrl("avatar", username); else - return _cosService.GetObjectUrl("avatar", "__default"); + return _cosService.GenerateObjectGetUrl("avatar", "__default"); } } } -- cgit v1.2.3 From 214ceda8903bafc28981b58a0530c2a15d7812cc Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 25 Apr 2019 19:03:46 +0800 Subject: Rename the cos service file. Fix a bug in test. --- Timeline.Tests/QCloudCosServiceUnitTest.cs | 7 +- Timeline/Services/QCloudCosService.cs | 257 ++++++++++++++++++++++++++++ Timeline/Services/TencentCloudCosService.cs | 257 ---------------------------- 3 files changed, 261 insertions(+), 260 deletions(-) create mode 100644 Timeline/Services/QCloudCosService.cs delete mode 100644 Timeline/Services/TencentCloudCosService.cs (limited to 'Timeline.Tests/QCloudCosServiceUnitTest.cs') diff --git a/Timeline.Tests/QCloudCosServiceUnitTest.cs b/Timeline.Tests/QCloudCosServiceUnitTest.cs index b0e6a868..0940c70d 100644 --- a/Timeline.Tests/QCloudCosServiceUnitTest.cs +++ b/Timeline.Tests/QCloudCosServiceUnitTest.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Net; +using System.Net.Http; using System.Threading.Tasks; using Timeline.Services; using Timeline.Tests.Helpers; @@ -80,8 +81,6 @@ namespace Timeline.Tests } } - // Although this test does not pass on my archlunux system. But the GenerateObjectGetUrl actually works well. - // And I don't know why. [Fact] public async Task GenerateObjectGetUrlTest() { @@ -90,7 +89,9 @@ namespace Timeline.Tests var services = serviceScope.ServiceProvider; var service = services.GetRequiredService(); var url = service.GenerateObjectGetUrl("avatar", "__default"); - using (var client = _factory.CreateClient()) + // never use the following line! Because client created by factory can't access Internet. + //using (var client = _factory.CreateClient()) + using (var client = services.GetRequiredService().CreateClient()) { var res = await client.GetAsync(url); Assert.Equal(HttpStatusCode.OK, res.StatusCode); diff --git a/Timeline/Services/QCloudCosService.cs b/Timeline/Services/QCloudCosService.cs new file mode 100644 index 00000000..f4358714 --- /dev/null +++ b/Timeline/Services/QCloudCosService.cs @@ -0,0 +1,257 @@ +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Security.Cryptography; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Timeline.Configs; + +namespace Timeline.Services +{ + public interface IQCloudCosService + { + /// + /// Test if an object in the bucket exists. + /// + /// The bucket name. + /// The object key. + /// True if exists. False if not. + Task IsObjectExists(string bucket, string key); + + /// + /// Generate a presignated url to access the object. + /// + /// The bucket name. + /// The object key. + /// The presignated url. + string GenerateObjectGetUrl(string bucket, string key); + } + + public class QCloudCosService : IQCloudCosService + { + private readonly IOptionsMonitor _config; + private readonly ILogger _logger; + private readonly IHttpClientFactory _httpClientFactory; + + public QCloudCosService(IOptionsMonitor config, ILogger logger, IHttpClientFactory httpClientFactory) + { + _config = config; + _logger = logger; + _httpClientFactory = httpClientFactory; + } + + private const string BucketNamePattern = @"^(([a-z0-9][a-z0-9-]*[a-z0-9])|[a-z0-9])$"; + + public static bool ValidateBucketName(string bucketName) + { + return Regex.IsMatch(bucketName, BucketNamePattern); + } + + public class QCloudCredentials + { + public string SecretId { get; set; } + public string SecretKey { get; set; } + } + + public class RequestInfo + { + public string Method { get; set; } + public string Uri { get; set; } + public IEnumerable> Parameters { get; set; } + public IEnumerable> Headers { get; set; } + } + + public class TimeDuration + { + public TimeDuration() + { + + } + + public TimeDuration(DateTimeOffset start, DateTimeOffset end) + { + Start = start; + End = end; + } + + public DateTimeOffset Start { get; set; } + public DateTimeOffset End { get; set; } + } + + public static string GenerateSign(QCloudCredentials credentials, RequestInfo request, TimeDuration signValidTime) + { + Debug.Assert(credentials != null); + Debug.Assert(credentials.SecretId != null); + Debug.Assert(credentials.SecretKey != null); + Debug.Assert(request != null); + Debug.Assert(request.Method != null); + Debug.Assert(request.Uri != null); + Debug.Assert(signValidTime != null); + Debug.Assert(signValidTime.Start < signValidTime.End, "Start must be before End in sign valid time."); + + List<(string key, string value)> Transform(IEnumerable> raw) + { + if (raw == null) + return new List<(string key, string value)>(); + + var sorted = raw.Select(p => (key: p.Key.ToLower(), value: WebUtility.UrlEncode(p.Value))).ToList(); + sorted.Sort((left, right) => string.CompareOrdinal(left.key, right.key)); + return sorted; + } + + var transformedParameters = Transform(request.Parameters); + var transformedHeaders = Transform(request.Headers); + + List<(string, string)> result = new List<(string, string)>(); + + const string signAlgorithm = "sha1"; + result.Add(("q-sign-algorithm", signAlgorithm)); + + result.Add(("q-ak", credentials.SecretId)); + + var signTime = $"{signValidTime.Start.ToUnixTimeSeconds().ToString()};{signValidTime.End.ToUnixTimeSeconds().ToString()}"; + var keyTime = signTime; + result.Add(("q-sign-time", signTime)); + result.Add(("q-key-time", keyTime)); + + result.Add(("q-header-list", string.Join(';', transformedHeaders.Select(h => h.key)))); + result.Add(("q-url-param-list", string.Join(';', transformedParameters.Select(p => p.key)))); + + HMACSHA1 hmac = new HMACSHA1(); + + string ByteArrayToString(byte[] bytes) + { + return BitConverter.ToString(bytes).Replace("-", "").ToLower(); + } + + hmac.Key = Encoding.UTF8.GetBytes(credentials.SecretKey); + var signKey = ByteArrayToString(hmac.ComputeHash(Encoding.UTF8.GetBytes(keyTime))); + + string Join(IEnumerable<(string key, string value)> raw) + { + return string.Join('&', raw.Select(p => string.Concat(p.key, "=", p.value))); + } + + var httpString = new StringBuilder() + .Append(request.Method.ToLower()).Append('\n') + .Append(request.Uri).Append('\n') + .Append(Join(transformedParameters)).Append('\n') + .Append(Join(transformedHeaders)).Append('\n') + .ToString(); + + string Sha1(string data) + { + var sha1 = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(data)); + return ByteArrayToString(sha1); + } + + var stringToSign = new StringBuilder() + .Append(signAlgorithm).Append('\n') + .Append(signTime).Append('\n') + .Append(Sha1(httpString)).Append('\n') + .ToString(); + + hmac.Key = Encoding.UTF8.GetBytes(signKey); + var signature = ByteArrayToString(hmac.ComputeHash( + Encoding.UTF8.GetBytes(stringToSign))); + + result.Add(("q-signature", signature)); + + return Join(result); + } + + private QCloudCredentials GetCredentials() + { + var config = _config.CurrentValue; + return new QCloudCredentials + { + SecretId = config.SecretId, + SecretKey = config.SecretKey + }; + } + + private string GetHost(string bucket) + { + var config = _config.CurrentValue; + return $"{bucket}-{config.AppId}.cos.{config.Region}.myqcloud.com"; + } + + public async Task IsObjectExists(string bucket, string key) + { + if (bucket == null) + throw new ArgumentNullException(nameof(bucket)); + if (key == null) + throw new ArgumentNullException(nameof(key)); + if (!ValidateBucketName(bucket)) + throw new ArgumentException($"Bucket name is not valid. Param is {bucket} .", nameof(bucket)); + + var client = _httpClientFactory.CreateClient(); + + var host = GetHost(bucket); + var encodedKey = WebUtility.UrlEncode(key); + + var request = new HttpRequestMessage(); + request.Method = HttpMethod.Head; + request.RequestUri = new Uri($"https://{host}/{encodedKey}"); + request.Headers.Host = host; + request.Headers.Date = DateTimeOffset.Now; + request.Headers.TryAddWithoutValidation("Authorization", GenerateSign(GetCredentials(), new RequestInfo + { + Method = "head", + Uri = "/" + encodedKey, + Headers = new Dictionary + { + ["Host"] = host + } + }, new TimeDuration(DateTimeOffset.Now, DateTimeOffset.Now.AddMinutes(2)))); + + try + { + var response = await client.SendAsync(request); + + if (response.IsSuccessStatusCode) + return true; + if (response.StatusCode == HttpStatusCode.NotFound) + return false; + + throw new Exception($"Unknown response code. {response.ToString()}"); + } + catch (Exception e) + { + _logger.LogError(e, "An error occured when test a cos object existence."); + return false; + } + } + + public string GenerateObjectGetUrl(string bucket, string key) + { + if (bucket == null) + throw new ArgumentNullException(nameof(bucket)); + if (key == null) + throw new ArgumentNullException(nameof(key)); + if (!ValidateBucketName(bucket)) + throw new ArgumentException($"Bucket name is not valid. Param is {bucket} .", nameof(bucket)); + + var host = GetHost(bucket); + var encodedKey = WebUtility.UrlEncode(key); + + var signature = GenerateSign(GetCredentials(), new RequestInfo + { + Method = "get", + Uri = "/" + encodedKey, + Headers = new Dictionary + { + ["Host"] = host + } + }, new TimeDuration(DateTimeOffset.Now, DateTimeOffset.Now.AddMinutes(6))); + + return $"https://{host}/{encodedKey}?{signature}"; + } + } +} diff --git a/Timeline/Services/TencentCloudCosService.cs b/Timeline/Services/TencentCloudCosService.cs deleted file mode 100644 index f4358714..00000000 --- a/Timeline/Services/TencentCloudCosService.cs +++ /dev/null @@ -1,257 +0,0 @@ -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Net; -using System.Net.Http; -using System.Security.Cryptography; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; -using Timeline.Configs; - -namespace Timeline.Services -{ - public interface IQCloudCosService - { - /// - /// Test if an object in the bucket exists. - /// - /// The bucket name. - /// The object key. - /// True if exists. False if not. - Task IsObjectExists(string bucket, string key); - - /// - /// Generate a presignated url to access the object. - /// - /// The bucket name. - /// The object key. - /// The presignated url. - string GenerateObjectGetUrl(string bucket, string key); - } - - public class QCloudCosService : IQCloudCosService - { - private readonly IOptionsMonitor _config; - private readonly ILogger _logger; - private readonly IHttpClientFactory _httpClientFactory; - - public QCloudCosService(IOptionsMonitor config, ILogger logger, IHttpClientFactory httpClientFactory) - { - _config = config; - _logger = logger; - _httpClientFactory = httpClientFactory; - } - - private const string BucketNamePattern = @"^(([a-z0-9][a-z0-9-]*[a-z0-9])|[a-z0-9])$"; - - public static bool ValidateBucketName(string bucketName) - { - return Regex.IsMatch(bucketName, BucketNamePattern); - } - - public class QCloudCredentials - { - public string SecretId { get; set; } - public string SecretKey { get; set; } - } - - public class RequestInfo - { - public string Method { get; set; } - public string Uri { get; set; } - public IEnumerable> Parameters { get; set; } - public IEnumerable> Headers { get; set; } - } - - public class TimeDuration - { - public TimeDuration() - { - - } - - public TimeDuration(DateTimeOffset start, DateTimeOffset end) - { - Start = start; - End = end; - } - - public DateTimeOffset Start { get; set; } - public DateTimeOffset End { get; set; } - } - - public static string GenerateSign(QCloudCredentials credentials, RequestInfo request, TimeDuration signValidTime) - { - Debug.Assert(credentials != null); - Debug.Assert(credentials.SecretId != null); - Debug.Assert(credentials.SecretKey != null); - Debug.Assert(request != null); - Debug.Assert(request.Method != null); - Debug.Assert(request.Uri != null); - Debug.Assert(signValidTime != null); - Debug.Assert(signValidTime.Start < signValidTime.End, "Start must be before End in sign valid time."); - - List<(string key, string value)> Transform(IEnumerable> raw) - { - if (raw == null) - return new List<(string key, string value)>(); - - var sorted = raw.Select(p => (key: p.Key.ToLower(), value: WebUtility.UrlEncode(p.Value))).ToList(); - sorted.Sort((left, right) => string.CompareOrdinal(left.key, right.key)); - return sorted; - } - - var transformedParameters = Transform(request.Parameters); - var transformedHeaders = Transform(request.Headers); - - List<(string, string)> result = new List<(string, string)>(); - - const string signAlgorithm = "sha1"; - result.Add(("q-sign-algorithm", signAlgorithm)); - - result.Add(("q-ak", credentials.SecretId)); - - var signTime = $"{signValidTime.Start.ToUnixTimeSeconds().ToString()};{signValidTime.End.ToUnixTimeSeconds().ToString()}"; - var keyTime = signTime; - result.Add(("q-sign-time", signTime)); - result.Add(("q-key-time", keyTime)); - - result.Add(("q-header-list", string.Join(';', transformedHeaders.Select(h => h.key)))); - result.Add(("q-url-param-list", string.Join(';', transformedParameters.Select(p => p.key)))); - - HMACSHA1 hmac = new HMACSHA1(); - - string ByteArrayToString(byte[] bytes) - { - return BitConverter.ToString(bytes).Replace("-", "").ToLower(); - } - - hmac.Key = Encoding.UTF8.GetBytes(credentials.SecretKey); - var signKey = ByteArrayToString(hmac.ComputeHash(Encoding.UTF8.GetBytes(keyTime))); - - string Join(IEnumerable<(string key, string value)> raw) - { - return string.Join('&', raw.Select(p => string.Concat(p.key, "=", p.value))); - } - - var httpString = new StringBuilder() - .Append(request.Method.ToLower()).Append('\n') - .Append(request.Uri).Append('\n') - .Append(Join(transformedParameters)).Append('\n') - .Append(Join(transformedHeaders)).Append('\n') - .ToString(); - - string Sha1(string data) - { - var sha1 = SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(data)); - return ByteArrayToString(sha1); - } - - var stringToSign = new StringBuilder() - .Append(signAlgorithm).Append('\n') - .Append(signTime).Append('\n') - .Append(Sha1(httpString)).Append('\n') - .ToString(); - - hmac.Key = Encoding.UTF8.GetBytes(signKey); - var signature = ByteArrayToString(hmac.ComputeHash( - Encoding.UTF8.GetBytes(stringToSign))); - - result.Add(("q-signature", signature)); - - return Join(result); - } - - private QCloudCredentials GetCredentials() - { - var config = _config.CurrentValue; - return new QCloudCredentials - { - SecretId = config.SecretId, - SecretKey = config.SecretKey - }; - } - - private string GetHost(string bucket) - { - var config = _config.CurrentValue; - return $"{bucket}-{config.AppId}.cos.{config.Region}.myqcloud.com"; - } - - public async Task IsObjectExists(string bucket, string key) - { - if (bucket == null) - throw new ArgumentNullException(nameof(bucket)); - if (key == null) - throw new ArgumentNullException(nameof(key)); - if (!ValidateBucketName(bucket)) - throw new ArgumentException($"Bucket name is not valid. Param is {bucket} .", nameof(bucket)); - - var client = _httpClientFactory.CreateClient(); - - var host = GetHost(bucket); - var encodedKey = WebUtility.UrlEncode(key); - - var request = new HttpRequestMessage(); - request.Method = HttpMethod.Head; - request.RequestUri = new Uri($"https://{host}/{encodedKey}"); - request.Headers.Host = host; - request.Headers.Date = DateTimeOffset.Now; - request.Headers.TryAddWithoutValidation("Authorization", GenerateSign(GetCredentials(), new RequestInfo - { - Method = "head", - Uri = "/" + encodedKey, - Headers = new Dictionary - { - ["Host"] = host - } - }, new TimeDuration(DateTimeOffset.Now, DateTimeOffset.Now.AddMinutes(2)))); - - try - { - var response = await client.SendAsync(request); - - if (response.IsSuccessStatusCode) - return true; - if (response.StatusCode == HttpStatusCode.NotFound) - return false; - - throw new Exception($"Unknown response code. {response.ToString()}"); - } - catch (Exception e) - { - _logger.LogError(e, "An error occured when test a cos object existence."); - return false; - } - } - - public string GenerateObjectGetUrl(string bucket, string key) - { - if (bucket == null) - throw new ArgumentNullException(nameof(bucket)); - if (key == null) - throw new ArgumentNullException(nameof(key)); - if (!ValidateBucketName(bucket)) - throw new ArgumentException($"Bucket name is not valid. Param is {bucket} .", nameof(bucket)); - - var host = GetHost(bucket); - var encodedKey = WebUtility.UrlEncode(key); - - var signature = GenerateSign(GetCredentials(), new RequestInfo - { - Method = "get", - Uri = "/" + encodedKey, - Headers = new Dictionary - { - ["Host"] = host - } - }, new TimeDuration(DateTimeOffset.Now, DateTimeOffset.Now.AddMinutes(6))); - - return $"https://{host}/{encodedKey}?{signature}"; - } - } -} -- cgit v1.2.3