MdpEdgeDeviceController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Text.Json;
  2. using Admin.NET.Plugin.AiDOP.DataPlatform.Core;
  3. using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Http;
  6. namespace Admin.NET.Plugin.AiDOP.DataPlatform.UnifiedIngress;
  7. [ApiController, AllowAnonymous, NonUnify]
  8. [Route("api/v1/edge")]
  9. public sealed class MdpEdgeDeviceController(MdpEdgeDeviceService devices) : ControllerBase
  10. {
  11. [HttpPost("register"), RequestSizeLimit(12288)]
  12. public Task<IActionResult> Register(EdgeRegistration input, CancellationToken token) =>
  13. Execute(async () => Ok(await devices.RegisterAsync(input, token)));
  14. [HttpPost("certificate/renew"), RequestSizeLimit(12288)]
  15. public Task<IActionResult> Renew(EdgeRenewal input, CancellationToken token) =>
  16. Execute(async () => Ok(await devices.RenewAsync(await DeviceCertificate(token), input, token)));
  17. [HttpPost("certificate/confirm"), RequestSizeLimit(1024)]
  18. public Task<IActionResult> Confirm(CancellationToken token) => Execute(async () =>
  19. {
  20. await devices.ConfirmCertificateAsync(await DeviceCertificate(token), token);
  21. return NoContent();
  22. });
  23. [HttpPost("heartbeat"), RequestSizeLimit(4096)]
  24. public Task<IActionResult> Poll(EdgeReport report, CancellationToken token) =>
  25. Execute(async () => HeartbeatResult(await devices.PollAsync(await DeviceCertificate(token), report)));
  26. [HttpGet("batches/{id:guid}")]
  27. public Task<IActionResult> Status(Guid id, CancellationToken token) => Execute(async () =>
  28. {
  29. var receipt = await devices.FindReceiptAsync(await DeviceCertificate(token), id);
  30. return receipt is null ? NotFound() : Ok(receipt);
  31. });
  32. [HttpPost("batches"), RequestSizeLimit(1500000)]
  33. public Task<IActionResult> Upload(EdgeBatch batch, CancellationToken token) =>
  34. Execute(async () => Ok(await devices.ReceiveAsync(await DeviceCertificate(token), batch, token)));
  35. internal static IActionResult HeartbeatResult(EdgeTask? task)
  36. {
  37. if (task is null)
  38. return new NoContentResult();
  39. return new ContentResult
  40. {
  41. Content = JsonSerializer.Serialize(task, EdgeProtocol.Json),
  42. ContentType = "application/json",
  43. StatusCode = StatusCodes.Status200OK,
  44. };
  45. }
  46. private async Task<System.Security.Cryptography.X509Certificates.X509Certificate2> DeviceCertificate(CancellationToken token) =>
  47. await HttpContext.Connection.GetClientCertificateAsync(token) ?? throw new EdgeContractException("EDGE_DEVICE_FORBIDDEN");
  48. private async Task<IActionResult> Execute(Func<Task<IActionResult>> action)
  49. {
  50. if (!Request.IsHttps) return StatusCode(403, new { code = "EDGE_TLS_REQUIRED" });
  51. Response.Headers.CacheControl = "no-store";
  52. try { return await action(); }
  53. catch (Exception error) when (error is EdgeContractException or MdpIngestHttpException or IngestContractException
  54. or System.Data.Common.DbException or SqlSugarException)
  55. {
  56. return EdgeFailure(error);
  57. }
  58. }
  59. internal static ObjectResult EdgeFailure(Exception error)
  60. {
  61. var code = error switch
  62. {
  63. EdgeContractException edge => edge.Code,
  64. MdpIngestHttpException http => http.Code,
  65. IngestContractException ingest => ingest.Code.ToString(),
  66. _ => "EDGE_STORE_UNAVAILABLE"
  67. };
  68. var status = error is MdpIngestHttpException custom ? custom.StatusCode : code switch
  69. {
  70. "EDGE_DISABLED" or "EDGE_DEVICE_NOT_FOUND" => 404,
  71. "EDGE_DEVICE_FORBIDDEN" or "EDGE_ACTIVATION_INVALID" or "EDGE_WRITER_FORBIDDEN" => 403,
  72. "EDGE_REPORT_INVALID" or "EDGE_CERT_REQUEST_INVALID" or "EDGE_BATCH_INVALID" or "EDGE_TASK_INVALID" or "EDGE_HANDOFF_INVALID" => 400,
  73. "EDGE_STORE_UNAVAILABLE" or "EDGE_CA_UNAVAILABLE" => 503,
  74. _ => 409
  75. };
  76. return new ObjectResult(new { code }) { StatusCode = status };
  77. }
  78. }