MdpEdgeDeviceController.cs 3.5 KB

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