EdgeRelease.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.IO.Compression;
  2. using System.Runtime.InteropServices;
  3. using System.Security.Cryptography;
  4. using System.Text.Json;
  5. using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge;
  6. namespace Admin.NET.DataPlatform.Edge;
  7. internal sealed record EdgeReleaseManifest(string Version, string Platform, string Sha256, int QueueFormat);
  8. internal static class EdgeRelease
  9. {
  10. internal static string Stage(string stateDirectory, string trustedPublisherKey, string manifestFile, string signatureFile, string archiveFile)
  11. {
  12. var manifestBytes = File.ReadAllBytes(manifestFile);
  13. if (manifestBytes.Length is < 1 or > 4096) throw new EdgeContractException("EDGE_RELEASE_MANIFEST_INVALID");
  14. using var signer = ECDsa.Create();
  15. signer.ImportFromPem(File.ReadAllText(trustedPublisherKey));
  16. var signature = Convert.FromBase64String(File.ReadAllText(signatureFile).Trim());
  17. if (!signer.VerifyData(manifestBytes, signature, HashAlgorithmName.SHA256, DSASignatureFormat.Rfc3279DerSequence))
  18. throw new EdgeContractException("EDGE_RELEASE_SIGNATURE_INVALID");
  19. var manifest = JsonSerializer.Deserialize<EdgeReleaseManifest>(manifestBytes, EdgeProtocol.Json)
  20. ?? throw new EdgeContractException("EDGE_RELEASE_MANIFEST_INVALID");
  21. var platform = (OperatingSystem.IsWindows() ? "win-" : "linux-") + RuntimeInformation.OSArchitecture.ToString().ToLowerInvariant();
  22. var current = typeof(EdgeRelease).Assembly.GetName().Version!;
  23. if (!Version.TryParse(manifest.Version, out var target) || target <= current || manifest.Platform != platform ||
  24. manifest.QueueFormat != 1)
  25. throw new EdgeContractException("EDGE_RELEASE_INCOMPATIBLE");
  26. using var archive = File.OpenRead(archiveFile);
  27. if (archive.Length is < 1 or > 536870912 || Convert.ToHexString(SHA256.HashData(archive)).ToLowerInvariant() != manifest.Sha256)
  28. throw new EdgeContractException("EDGE_RELEASE_DIGEST_INVALID");
  29. archive.Position = 0;
  30. var releases = Path.Combine(stateDirectory, "releases");
  31. Directory.CreateDirectory(releases);
  32. var final = Path.Combine(releases, target.ToString());
  33. if (Directory.Exists(final)) throw new EdgeContractException("EDGE_RELEASE_ALREADY_STAGED");
  34. var temporary = Path.Combine(releases, ".stage-" + Guid.NewGuid().ToString("N"));
  35. Directory.CreateDirectory(temporary);
  36. try
  37. {
  38. using var zip = new ZipArchive(archive, ZipArchiveMode.Read);
  39. if (zip.Entries.Count > 4096 || zip.Entries.Sum(entry => entry.Length) > 1073741824)
  40. throw new EdgeContractException("EDGE_RELEASE_TOO_LARGE");
  41. zip.ExtractToDirectory(temporary);
  42. var binary = Path.Combine(temporary, OperatingSystem.IsWindows() ? "Admin.NET.DataPlatform.Edge.exe" : "Admin.NET.DataPlatform.Edge");
  43. if (!File.Exists(binary)) throw new EdgeContractException("EDGE_RELEASE_EXECUTABLE_MISSING");
  44. if (!OperatingSystem.IsWindows()) File.SetUnixFileMode(binary, UnixFileMode.UserRead | UnixFileMode.UserExecute);
  45. EdgeLocalState.AtomicWrite(Path.Combine(temporary, "verified-manifest.json"), manifestBytes);
  46. Directory.Move(temporary, final);
  47. return final;
  48. }
  49. finally
  50. {
  51. if (Directory.Exists(temporary)) Directory.Delete(temporary, recursive: true);
  52. }
  53. }
  54. }