2024-03-26 13:42:47 +00:00
|
|
|
using Core;
|
|
|
|
using KubernetesWorkflow.Types;
|
|
|
|
using Logging;
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
using System.Text;
|
2024-10-08 07:29:26 +00:00
|
|
|
using Utils;
|
2024-03-26 13:42:47 +00:00
|
|
|
|
|
|
|
namespace CodexPlugin
|
|
|
|
{
|
|
|
|
public class ApiChecker
|
|
|
|
{
|
|
|
|
// <INSERT-OPENAPI-YAML-HASH>
|
2024-10-22 10:57:07 +00:00
|
|
|
private const string OpenApiYamlHash = "C1-50-18-D5-73-64-1C-D8-74-DB-75-0D-C1-3D-AB-82-D6-A3-05-3E-49-2B-82-0B-0F-92-BC-DC-32-BE-56-C9";
|
2024-03-26 13:42:47 +00:00
|
|
|
private const string OpenApiFilePath = "/codex/openapi.yaml";
|
|
|
|
private const string DisableEnvironmentVariable = "CODEXPLUGIN_DISABLE_APICHECK";
|
|
|
|
|
|
|
|
private const bool Disable = false;
|
|
|
|
|
|
|
|
private const string Warning =
|
|
|
|
"Warning: CodexPlugin was unable to find the openapi.yaml file in the Codex container. Are you running an old version of Codex? " +
|
|
|
|
"Plugin will continue as normal, but API compatibility is not guaranteed!";
|
|
|
|
|
|
|
|
private const string Failure =
|
|
|
|
"Codex API compatibility check failed! " +
|
2024-10-08 07:29:26 +00:00
|
|
|
"openapi.yaml used by CodexPlugin does not match openapi.yaml in Codex container. The openapi.yaml in " +
|
|
|
|
"'ProjectPlugins/CodexPlugin' has been overwritten with the container one. " +
|
|
|
|
"Please and rebuild this project. If you wish to disable API compatibility checking, please set " +
|
2024-03-26 13:42:47 +00:00
|
|
|
$"the environment variable '{DisableEnvironmentVariable}' or set the disable bool in 'ProjectPlugins/CodexPlugin/ApiChecker.cs'.";
|
|
|
|
|
|
|
|
private static bool checkPassed = false;
|
|
|
|
|
|
|
|
private readonly IPluginTools pluginTools;
|
|
|
|
private readonly ILog log;
|
|
|
|
|
|
|
|
public ApiChecker(IPluginTools pluginTools)
|
|
|
|
{
|
|
|
|
this.pluginTools = pluginTools;
|
|
|
|
log = pluginTools.GetLog();
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(OpenApiYamlHash)) throw new Exception("OpenAPI yaml hash was not inserted by pre-build trigger.");
|
|
|
|
}
|
|
|
|
|
2024-04-13 14:09:17 +00:00
|
|
|
public void CheckCompatibility(RunningPod[] containers)
|
2024-03-26 13:42:47 +00:00
|
|
|
{
|
|
|
|
if (checkPassed) return;
|
|
|
|
|
|
|
|
Log("CodexPlugin is checking API compatibility...");
|
|
|
|
|
|
|
|
if (Disable || !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(DisableEnvironmentVariable)))
|
|
|
|
{
|
|
|
|
Log("API compatibility checking has been disabled.");
|
|
|
|
checkPassed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var workflow = pluginTools.CreateWorkflow();
|
|
|
|
var container = containers.First().Containers.First();
|
|
|
|
var containerApi = workflow.ExecuteCommand(container, "cat", OpenApiFilePath);
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(containerApi))
|
|
|
|
{
|
|
|
|
log.Error(Warning);
|
|
|
|
|
|
|
|
checkPassed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var containerHash = Hash(containerApi);
|
|
|
|
if (containerHash == OpenApiYamlHash)
|
|
|
|
{
|
|
|
|
Log("API compatibility check passed.");
|
|
|
|
checkPassed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-08 07:29:26 +00:00
|
|
|
OverwriteOpenApiYaml(containerApi);
|
|
|
|
|
2024-03-26 13:42:47 +00:00
|
|
|
log.Error(Failure);
|
|
|
|
throw new Exception(Failure);
|
|
|
|
}
|
|
|
|
|
2024-10-08 07:29:26 +00:00
|
|
|
private void OverwriteOpenApiYaml(string containerApi)
|
|
|
|
{
|
|
|
|
Log("API compatibility check failed. Updating CodexPlugin...");
|
|
|
|
var openApiFilePath = Path.Combine(PluginPathUtils.ProjectPluginsDir, "CodexPlugin", "openapi.yaml");
|
|
|
|
if (!File.Exists(openApiFilePath)) throw new Exception("Unable to locate CodexPlugin/openapi.yaml. Expected: " + openApiFilePath);
|
|
|
|
|
|
|
|
File.Delete(openApiFilePath);
|
|
|
|
File.WriteAllText(openApiFilePath, containerApi);
|
|
|
|
Log("CodexPlugin/openapi.yaml has been updated.");
|
|
|
|
}
|
|
|
|
|
2024-03-26 13:42:47 +00:00
|
|
|
private string Hash(string file)
|
|
|
|
{
|
2024-03-26 15:10:50 +00:00
|
|
|
var fileBytes = Encoding.ASCII.GetBytes(file
|
|
|
|
.Replace(Environment.NewLine, ""));
|
2024-03-26 13:42:47 +00:00
|
|
|
var sha = SHA256.Create();
|
|
|
|
var hash = sha.ComputeHash(fileBytes);
|
|
|
|
return BitConverter.ToString(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Log(string msg)
|
|
|
|
{
|
|
|
|
log.Log(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|