| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using System.IO.Compression;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography;
- using System.Text.Json;
- using Admin.NET.Plugin.AiDOP.DataPlatform.Core.Edge;
- namespace Admin.NET.DataPlatform.Edge;
- internal sealed record EdgeReleaseManifest(string Version, string Platform, string Sha256, int QueueFormat);
- internal static class EdgeRelease
- {
- internal static string Stage(string stateDirectory, string trustedPublisherKey, string manifestFile, string signatureFile, string archiveFile)
- {
- var manifestBytes = File.ReadAllBytes(manifestFile);
- if (manifestBytes.Length is < 1 or > 4096) throw new EdgeContractException("EDGE_RELEASE_MANIFEST_INVALID");
- using var signer = ECDsa.Create();
- signer.ImportFromPem(File.ReadAllText(trustedPublisherKey));
- var signature = Convert.FromBase64String(File.ReadAllText(signatureFile).Trim());
- if (!signer.VerifyData(manifestBytes, signature, HashAlgorithmName.SHA256, DSASignatureFormat.Rfc3279DerSequence))
- throw new EdgeContractException("EDGE_RELEASE_SIGNATURE_INVALID");
- var manifest = JsonSerializer.Deserialize<EdgeReleaseManifest>(manifestBytes, EdgeProtocol.Json)
- ?? throw new EdgeContractException("EDGE_RELEASE_MANIFEST_INVALID");
- var platform = (OperatingSystem.IsWindows() ? "win-" : "linux-") + RuntimeInformation.OSArchitecture.ToString().ToLowerInvariant();
- var current = typeof(EdgeRelease).Assembly.GetName().Version!;
- if (!Version.TryParse(manifest.Version, out var target) || target <= current || manifest.Platform != platform ||
- manifest.QueueFormat != 1)
- throw new EdgeContractException("EDGE_RELEASE_INCOMPATIBLE");
- using var archive = File.OpenRead(archiveFile);
- if (archive.Length is < 1 or > 536870912 || Convert.ToHexString(SHA256.HashData(archive)).ToLowerInvariant() != manifest.Sha256)
- throw new EdgeContractException("EDGE_RELEASE_DIGEST_INVALID");
- archive.Position = 0;
- var releases = Path.Combine(stateDirectory, "releases");
- Directory.CreateDirectory(releases);
- var final = Path.Combine(releases, target.ToString());
- if (Directory.Exists(final)) throw new EdgeContractException("EDGE_RELEASE_ALREADY_STAGED");
- var temporary = Path.Combine(releases, ".stage-" + Guid.NewGuid().ToString("N"));
- Directory.CreateDirectory(temporary);
- try
- {
- using var zip = new ZipArchive(archive, ZipArchiveMode.Read);
- if (zip.Entries.Count > 4096 || zip.Entries.Sum(entry => entry.Length) > 1073741824)
- throw new EdgeContractException("EDGE_RELEASE_TOO_LARGE");
- zip.ExtractToDirectory(temporary);
- var binary = Path.Combine(temporary, OperatingSystem.IsWindows() ? "Admin.NET.DataPlatform.Edge.exe" : "Admin.NET.DataPlatform.Edge");
- if (!File.Exists(binary)) throw new EdgeContractException("EDGE_RELEASE_EXECUTABLE_MISSING");
- if (!OperatingSystem.IsWindows()) File.SetUnixFileMode(binary, UnixFileMode.UserRead | UnixFileMode.UserExecute);
- EdgeLocalState.AtomicWrite(Path.Combine(temporary, "verified-manifest.json"), manifestBytes);
- Directory.Move(temporary, final);
- return final;
- }
- finally
- {
- if (Directory.Exists(temporary)) Directory.Delete(temporary, recursive: true);
- }
- }
- }
|