From 03a3ccb4de7a7480889ba39ea8e08e2cc73d85cd Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 25 Mar 2024 13:48:20 +0100 Subject: [PATCH] prototype of pre-build yaml hash injection --- ProjectPlugins/CodexPlugin/CodexPlugin.cs | 4 ++- ProjectPlugins/CodexPlugin/CodexPlugin.csproj | 4 +++ .../CodexPluginPrebuild.csproj | 10 ++++++ ProjectPlugins/CodexPluginPrebuild/Program.cs | 36 +++++++++++++++++++ cs-codex-dist-testing.sln | 7 ++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj create mode 100644 ProjectPlugins/CodexPluginPrebuild/Program.cs diff --git a/ProjectPlugins/CodexPlugin/CodexPlugin.cs b/ProjectPlugins/CodexPlugin/CodexPlugin.cs index 3e40a99c..624862e5 100644 --- a/ProjectPlugins/CodexPlugin/CodexPlugin.cs +++ b/ProjectPlugins/CodexPlugin/CodexPlugin.cs @@ -1,4 +1,4 @@ -using Core; +using Core; using KubernetesWorkflow.Types; namespace CodexPlugin @@ -9,6 +9,8 @@ namespace CodexPlugin private readonly IPluginTools tools; private readonly CodexLogLevel defaultLogLevel = CodexLogLevel.Trace; + private const string OpenApiYamlHash = ""; + public CodexPlugin(IPluginTools tools) { codexStarter = new CodexStarter(tools); diff --git a/ProjectPlugins/CodexPlugin/CodexPlugin.csproj b/ProjectPlugins/CodexPlugin/CodexPlugin.csproj index 47899a0b..2bb12562 100644 --- a/ProjectPlugins/CodexPlugin/CodexPlugin.csproj +++ b/ProjectPlugins/CodexPlugin/CodexPlugin.csproj @@ -34,4 +34,8 @@ + + + + diff --git a/ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj b/ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj new file mode 100644 index 00000000..f02677bf --- /dev/null +++ b/ProjectPlugins/CodexPluginPrebuild/CodexPluginPrebuild.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/ProjectPlugins/CodexPluginPrebuild/Program.cs b/ProjectPlugins/CodexPluginPrebuild/Program.cs new file mode 100644 index 00000000..f33246da --- /dev/null +++ b/ProjectPlugins/CodexPluginPrebuild/Program.cs @@ -0,0 +1,36 @@ +using System.Security.Cryptography; + +public static class Program +{ + private const string OpenApiFile = "../CodexPlugin/openapi.yaml"; + private const string Search = ""; + private const string TargetFile = "CodexPlugin.cs"; + + private static string CreateHash() + { + var fileBytes = File.ReadAllBytes(OpenApiFile); + var sha = SHA256.Create(); + var hash = sha.ComputeHash(fileBytes); + return BitConverter.ToString(hash); + } + + private static void SearchAndReplace(string hash) + { + var lines = File.ReadAllLines(TargetFile); + lines = lines.Select(l => l.Replace(Search, hash)).ToArray(); + File.WriteAllLines(TargetFile, lines); + } + + public static void Main(string[] args) + { + Console.WriteLine("Injecting hash of 'openapi.yaml'..."); + // This hash is used to verify that the Codex docker image being used is compatible + // with the openapi.yaml being used by the Codex plugin. + // If the openapi.yaml files don't match, an exception is thrown. + + var hash = CreateHash(); + SearchAndReplace(hash); + + Console.WriteLine("Done!"); + } +} \ No newline at end of file diff --git a/cs-codex-dist-testing.sln b/cs-codex-dist-testing.sln index 3a403fe3..b9b56934 100644 --- a/cs-codex-dist-testing.sln +++ b/cs-codex-dist-testing.sln @@ -59,6 +59,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GethConnector", "Framework\ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordRewards", "Framework\DiscordRewards\DiscordRewards.csproj", "{B07820C4-309F-4454-BCC1-1D4902C9C67B}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodexPluginPrebuild", "ProjectPlugins\CodexPluginPrebuild\CodexPluginPrebuild.csproj", "{88C212E9-308A-46A4-BAAD-468E8EBD8EDF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -161,6 +163,10 @@ Global {B07820C4-309F-4454-BCC1-1D4902C9C67B}.Debug|Any CPU.Build.0 = Debug|Any CPU {B07820C4-309F-4454-BCC1-1D4902C9C67B}.Release|Any CPU.ActiveCfg = Release|Any CPU {B07820C4-309F-4454-BCC1-1D4902C9C67B}.Release|Any CPU.Build.0 = Release|Any CPU + {88C212E9-308A-46A4-BAAD-468E8EBD8EDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {88C212E9-308A-46A4-BAAD-468E8EBD8EDF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {88C212E9-308A-46A4-BAAD-468E8EBD8EDF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {88C212E9-308A-46A4-BAAD-468E8EBD8EDF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -190,6 +196,7 @@ Global {570C0DBE-0EF1-47B5-9A3B-E1F7895722A5} = {7591C5B3-D86E-4AE4-8ED2-B272D17FE7E3} {F730DA73-1C92-4107-BCFB-D33759DAB0C3} = {81AE04BC-CBFA-4E6F-B039-8208E9AFAAE7} {B07820C4-309F-4454-BCC1-1D4902C9C67B} = {81AE04BC-CBFA-4E6F-B039-8208E9AFAAE7} + {88C212E9-308A-46A4-BAAD-468E8EBD8EDF} = {8F1F1C2A-E313-4E0C-BE40-58FB0BA91124} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {237BF0AA-9EC4-4659-AD9A-65DEB974250C}