From 771d62a43a6e73dcd425dc582c90666349e8794a Mon Sep 17 00:00:00 2001 From: Roberto Borges Date: Wed, 14 Jun 2023 12:58:37 -0400 Subject: [PATCH 1/4] Updating how the migration works when swtching from docker run vs Visual Studio --- src/Services/Identity/Identity.API/SeedData.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Services/Identity/Identity.API/SeedData.cs b/src/Services/Identity/Identity.API/SeedData.cs index fa8365f9e..dd1df8e17 100644 --- a/src/Services/Identity/Identity.API/SeedData.cs +++ b/src/Services/Identity/Identity.API/SeedData.cs @@ -9,7 +9,14 @@ public class SeedData await retryPolicy.ExecuteAsync(async () => { - await context.Database.MigrateAsync(); + try + { + await context.Database.MigrateAsync(); + } + catch (Exception) + { + //If running on Docker on command line Migrations will take place automaticaly + } var userMgr = scope.ServiceProvider.GetRequiredService>(); var alice = await userMgr.FindByNameAsync("alice"); From 7edb45179f47fd93d4e001839a641b1ff485c9c7 Mon Sep 17 00:00:00 2001 From: Roberto Borges Date: Fri, 16 Jun 2023 08:37:52 -0400 Subject: [PATCH 2/4] Rollback API deleted by mistake on Webhook Client --- src/Web/WebhookClient/Program.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Web/WebhookClient/Program.cs b/src/Web/WebhookClient/Program.cs index d697a943a..653cb25a6 100644 --- a/src/Web/WebhookClient/Program.cs +++ b/src/Web/WebhookClient/Program.cs @@ -14,6 +14,37 @@ builder.Services.AddControllers(); var app = builder.Build(); app.UseServiceDefaults(); +app.Map("/check", capp => +{ + capp.Run(async (context) => + { + if ("OPTIONS".Equals(context.Request.Method, StringComparison.InvariantCultureIgnoreCase)) + { + var validateToken = bool.TrueString.Equals(builder.Configuration["ValidateToken"], StringComparison.InvariantCultureIgnoreCase); + var header = context.Request.Headers[HeaderNames.WebHookCheckHeader]; + var value = header.FirstOrDefault(); + var tokenToValidate = builder.Configuration["Token"]; + if (!validateToken || value == tokenToValidate) + { + if (!string.IsNullOrWhiteSpace(tokenToValidate)) + { + context.Response.Headers.Add(HeaderNames.WebHookCheckHeader, tokenToValidate); + } + context.Response.StatusCode = (int)HttpStatusCode.OK; + } + else + { + await context.Response.WriteAsync("Invalid token"); + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; + } + } + else + { + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; + } + }); +}); + // Fix samesite issue when running eShop from docker-compose locally as by default http protocol is being used // Refer to https://github.com/dotnet-architecture/eShopOnContainers/issues/1391 app.UseCookiePolicy(new CookiePolicyOptions { MinimumSameSitePolicy = SameSiteMode.Lax }); From 5da4dff0da81b369ce6168c259d7ef8bd9081e3a Mon Sep 17 00:00:00 2001 From: Roberto Borges Date: Fri, 16 Jun 2023 08:44:50 -0400 Subject: [PATCH 3/4] Removing Debug code --- src/Services/Identity/Identity.API/SeedData.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Services/Identity/Identity.API/SeedData.cs b/src/Services/Identity/Identity.API/SeedData.cs index dd1df8e17..fa8365f9e 100644 --- a/src/Services/Identity/Identity.API/SeedData.cs +++ b/src/Services/Identity/Identity.API/SeedData.cs @@ -9,14 +9,7 @@ public class SeedData await retryPolicy.ExecuteAsync(async () => { - try - { - await context.Database.MigrateAsync(); - } - catch (Exception) - { - //If running on Docker on command line Migrations will take place automaticaly - } + await context.Database.MigrateAsync(); var userMgr = scope.ServiceProvider.GetRequiredService>(); var alice = await userMgr.FindByNameAsync("alice"); From 44a081b1b43eabb0333c16c2da7fdb511cc26d72 Mon Sep 17 00:00:00 2001 From: Roberto Borges Date: Wed, 28 Jun 2023 10:37:52 -0400 Subject: [PATCH 4/4] Implementing External listner to test Pub/Sub --- .../Controllers/WebhooksReceivedController.cs | 64 ++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs b/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs index da051a2bf..df4e8e41e 100644 --- a/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs +++ b/src/Web/WebhookClient/Controllers/WebhooksReceivedController.cs @@ -1,4 +1,10 @@ -namespace WebhookClient.Controllers; +using System; +using System.Security.Policy; +using System.Text; +using Azure; +using Microsoft.CodeAnalysis.VisualBasic.Syntax; + +namespace WebhookClient.Controllers; [ApiController] [Route("webhook-received")] @@ -8,11 +14,17 @@ public class WebhooksReceivedController : Controller private readonly ILogger _logger; private readonly IHooksRepository _hooksRepository; - public WebhooksReceivedController(IOptions options, ILogger logger, IHooksRepository hooksRepository) + private readonly IConfiguration _configuration; + + public WebhooksReceivedController(IOptions options, + ILogger logger, + IHooksRepository hooksRepository, + IConfiguration configuration) { _options = options.Value; _logger = logger; _hooksRepository = hooksRepository; + _configuration = configuration; } [HttpPost] @@ -31,6 +43,9 @@ public class WebhooksReceivedController : Controller When = hook.When, Token = token }; + + sendMessageToServiceBus(hook.Payload); + await _hooksRepository.AddNew(newHook); _logger.LogInformation("Received hook was processed."); return Ok(newHook); @@ -39,4 +54,49 @@ public class WebhooksReceivedController : Controller _logger.LogInformation("Received hook is NOT processed - Bad Request returned."); return BadRequest(); } + + //function to send the message to Service Bus + public void sendMessageToServiceBus(string data) + { + //get the sas key from app config + var saskey = _configuration["ApimWAF:SASKey"]; + var url = _configuration["ApimWAF:Url"]; + var apimKey = _configuration["ApimWAF:apimKey"]; + + if (saskey == null || url == null || apimKey == null) + { + _logger.LogInformation("Skiping sending APIM Request - missing configuration"); + return; + } + + try + { + + var handler = new HttpClientHandler(); + + //ignore the certificate errors + handler.ServerCertificateCustomValidationCallback = + HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; + + System.Net.ServicePointManager.ServerCertificateValidationCallback += + (s, cert, chain, sslPolicyErrors) => true; + + using (var client = new HttpClient(handler)) + { + var msg = new HttpRequestMessage(HttpMethod.Post, url); + msg.Headers.Add("Authorization", saskey); + msg.Headers.Add("Ocp-Apim-Subscription-Key", apimKey); + + msg.Content = new StringContent(data, Encoding.UTF8, "application/json"); + + var response = client.Send(msg); + + _logger.LogInformation("Sending the paid message to {Url} with \"{Data}\" result: \"{response}\"", url, data ?? string.Empty, response.Content.ReadAsStringAsync().GetAwaiter().GetResult()); + } + } + catch (Exception ex) + { + _logger.LogInformation("Error sending APIM Request {}", ex); + } + } }