Enhanced enterprise config support (#666)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-02-19 20:43:47 +01:00
1 parent 6e33c361dc
commit b445600a52
31 files changed
+733 -426

No files matched your search

@@ -3,4 +3,8 @@ namespace AIStudio.Tools.PluginSystem;
public interface IAvailablePlugin : IPluginMetadata
{
public string LocalPath { get; }
public bool IsManagedByConfigServer { get; }
public Guid? ManagedConfigurationId { get; }
}
@@ -17,6 +17,11 @@ public sealed class PluginConfiguration(bool isInternal, LuaState state, PluginT
/// The list of configuration objects. Configuration objects are, e.g., providers or chat templates.
/// </summary>
public IEnumerable<PluginConfigurationObject> ConfigObjects => this.configObjects;
/// <summary>
/// True/false when explicitly configured in the plugin, otherwise null.
/// </summary>
public bool? DeployedUsingConfigServer { get; } = ReadDeployedUsingConfigServer(state);
public async Task InitializeAsync(bool dryRun)
{
@@ -69,6 +74,14 @@ public sealed class PluginConfiguration(bool isInternal, LuaState state, PluginT
/// </summary>
private sealed record TemporarySecretId(string SecretId, string SecretName) : ISecretId;
private static bool? ReadDeployedUsingConfigServer(LuaState state)
{
if (state.Environment["DEPLOYED_USING_CONFIG_SERVER"].TryRead<bool>(out var deployedUsingConfigServer))
return deployedUsingConfigServer;
return null;
}
/// <summary>
/// Tries to initialize the UI text content of the plugin.
/// </summary>
@@ -79,13 +79,13 @@ public sealed record PluginConfigurationObject
if (luaTableName is null)
{
LOG.LogError($"The configuration object type '{configObjectType}' is not supported yet.");
LOG.LogError("The configuration object type '{ConfigObjectType}' is not supported yet (config plugin id: {ConfigPluginId}).", configObjectType, configPluginId);
return false;
}
if (!mainTable.TryGetValue(luaTableName, out var luaValue) || !luaValue.TryRead<LuaTable>(out var luaTable))
{
LOG.LogWarning($"The {luaTableName} table does not exist or is not a valid table.");
LOG.LogWarning("The table '{LuaTableName}' does not exist or is not a valid table (config plugin id: {ConfigPluginId}).", luaTableName, configPluginId);
return false;
}
@@ -97,7 +97,7 @@ public sealed record PluginConfigurationObject
var luaObjectTableValue = luaTable[i];
if (!luaObjectTableValue.TryRead<LuaTable>(out var luaObjectTable))
{
LOG.LogWarning($"The {luaObjectTable} table at index {i} is not a valid table.");
LOG.LogWarning("The table '{LuaTableName}' entry at index {Index} is not a valid table (config plugin id: {ConfigPluginId}).", luaTableName, i, configPluginId);
continue;
}
@@ -151,12 +151,12 @@ public sealed record PluginConfigurationObject
random ??= new ThreadSafeRandom();
configObject = configObject with { Num = (uint)random.Next(500_000, 1_000_000) };
storedObjects.Add((TClass)configObject);
LOG.LogWarning($"The next number for the configuration object '{configObject.Name}' (id={configObject.Id}) could not be incremented. Using a random number instead.");
LOG.LogWarning("The next number for the configuration object '{ConfigObjectName}' (id={ConfigObjectId}) could not be incremented. Using a random number instead (config plugin id: {ConfigPluginId}).", configObject.Name, configObject.Id, configPluginId);
}
}
}
else
LOG.LogWarning($"The {luaObjectTable} table at index {i} does not contain a valid chat template configuration.");
LOG.LogWarning("The table '{LuaTableName}' entry at index {Index} does not contain a valid configuration object (type={ConfigObjectType}, config plugin id: {ConfigPluginId}).", luaTableName, i, configObjectType, configPluginId);
}
return true;
@@ -5,10 +5,10 @@ namespace AIStudio.Tools.PluginSystem;
public static partial class PluginFactory
{
public static async Task<EntityTagHeaderValue?> DetermineConfigPluginETagAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default)
public static async Task<(bool Success, EntityTagHeaderValue? ETag, string? Issue)> DetermineConfigPluginETagAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default)
{
if(configPlugId == Guid.Empty || string.IsNullOrWhiteSpace(configServerUrl))
return null;
return (false, null, "Configuration ID or server URL is missing.");
try
{
@@ -18,18 +18,24 @@ public static partial class PluginFactory
using var http = new HttpClient();
using var request = new HttpRequestMessage(HttpMethod.Get, downloadUrl);
var response = await http.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
return response.Headers.ETag;
if (!response.IsSuccessStatusCode)
{
LOG.LogError($"Failed to determine the ETag for configuration plugin '{configPlugId}'. HTTP Status: {response.StatusCode}");
return (false, null, $"HTTP status: {response.StatusCode}");
}
return (true, response.Headers.ETag, null);
}
catch (Exception e)
{
LOG.LogError(e, "An error occurred while determining the ETag for the configuration plugin.");
return null;
return (false, null, e.Message);
}
}
public static async Task<bool> TryDownloadingConfigPluginAsync(Guid configPlugId, string configServerUrl, CancellationToken cancellationToken = default)
{
if(!IS_INITIALIZED)
if(!IsInitialized)
{
LOG.LogWarning("Plugin factory is not yet initialized. Cannot download configuration plugin.");
return false;
@@ -40,36 +46,72 @@ public static partial class PluginFactory
LOG.LogInformation($"Try to download configuration plugin with ID='{configPlugId}' from server='{configServerUrl}' (GET {downloadUrl})");
var tempDownloadFile = Path.GetTempFileName();
var stagedDirectory = Path.Join(CONFIGURATION_PLUGINS_ROOT, $"{configPlugId}.staging-{Guid.NewGuid():N}");
string? backupDirectory = null;
var wasSuccessful = false;
try
{
await LockHotReloadAsync();
using var httpClient = new HttpClient();
var response = await httpClient.GetAsync(downloadUrl, cancellationToken);
if (response.IsSuccessStatusCode)
if (!response.IsSuccessStatusCode)
{
await using(var tempFileStream = File.Create(tempDownloadFile))
{
await response.Content.CopyToAsync(tempFileStream, cancellationToken);
}
var configDirectory = Path.Join(CONFIGURATION_PLUGINS_ROOT, configPlugId.ToString());
if(Directory.Exists(configDirectory))
Directory.Delete(configDirectory, true);
Directory.CreateDirectory(configDirectory);
ZipFile.ExtractToDirectory(tempDownloadFile, configDirectory);
LOG.LogInformation($"Configuration plugin with ID='{configPlugId}' downloaded and extracted successfully to '{configDirectory}'.");
}
else
LOG.LogError($"Failed to download the enterprise configuration plugin. HTTP Status: {response.StatusCode}");
return false;
}
await using(var tempFileStream = File.Create(tempDownloadFile))
{
await response.Content.CopyToAsync(tempFileStream, cancellationToken);
}
ZipFile.ExtractToDirectory(tempDownloadFile, stagedDirectory);
var configDirectory = Path.Join(CONFIGURATION_PLUGINS_ROOT, configPlugId.ToString());
if (Directory.Exists(configDirectory))
{
backupDirectory = Path.Join(CONFIGURATION_PLUGINS_ROOT, $"{configPlugId}.backup-{Guid.NewGuid():N}");
Directory.Move(configDirectory, backupDirectory);
}
Directory.Move(stagedDirectory, configDirectory);
if (!string.IsNullOrWhiteSpace(backupDirectory) && Directory.Exists(backupDirectory))
Directory.Delete(backupDirectory, true);
LOG.LogInformation($"Configuration plugin with ID='{configPlugId}' downloaded and extracted successfully to '{configDirectory}'.");
wasSuccessful = true;
}
catch (Exception e)
{
LOG.LogError(e, "An error occurred while downloading or extracting the enterprise configuration plugin.");
var configDirectory = Path.Join(CONFIGURATION_PLUGINS_ROOT, configPlugId.ToString());
if (!string.IsNullOrWhiteSpace(backupDirectory) && Directory.Exists(backupDirectory) && !Directory.Exists(configDirectory))
{
try
{
Directory.Move(backupDirectory, configDirectory);
}
catch (Exception restoreException)
{
LOG.LogError(restoreException, "Failed to restore the previous configuration plugin after a failed update.");
}
}
}
finally
{
if (Directory.Exists(stagedDirectory))
{
try
{
Directory.Delete(stagedDirectory, true);
}
catch (Exception e)
{
LOG.LogError(e, "Failed to delete the staged configuration plugin directory.");
}
}
if (File.Exists(tempDownloadFile))
{
try
@@ -85,6 +127,6 @@ public static partial class PluginFactory
UnlockHotReload();
}
return true;
return wasSuccessful;
}
}
@@ -6,7 +6,7 @@ public static partial class PluginFactory
public static void SetUpHotReloading()
{
if (!IS_INITIALIZED)
if (!IsInitialized)
{
LOG.LogError("PluginFactory is not initialized. Please call Setup() before using it.");
return;
@@ -10,7 +10,7 @@ public static partial class PluginFactory
{
public static async Task EnsureInternalPlugins()
{
if (!IS_INITIALIZED)
if (!IsInitialized)
{
LOG.LogError("PluginFactory is not initialized. Please call Setup() before using it.");
return;
@@ -30,7 +30,7 @@ public static partial class PluginFactory
/// </remarks>
public static async Task LoadAll(CancellationToken cancellationToken = default)
{
if (!IS_INITIALIZED)
if (!IsInitialized)
{
LOG.LogError("PluginFactory is not initialized. Please call Setup() before using it.");
return;
@@ -104,16 +104,40 @@ public static partial class PluginFactory
LOG.LogInformation($"Successfully loaded plugin: '{pluginMainFile}' (Id='{plugin.Id}', Type='{plugin.Type}', Name='{plugin.Name}', Version='{plugin.Version}', Authors='{string.Join(", ", plugin.Authors)}')");
// For configuration plugins, validate that the plugin ID matches the enterprise config ID
// (the directory name under which the plugin was downloaded):
if (plugin.Type is PluginType.CONFIGURATION && pluginPath.StartsWith(CONFIGURATION_PLUGINS_ROOT, StringComparison.OrdinalIgnoreCase))
var isConfigurationPluginInConfigDirectory =
plugin.Type is PluginType.CONFIGURATION &&
pluginPath.StartsWith(CONFIGURATION_PLUGINS_ROOT, StringComparison.OrdinalIgnoreCase);
var isManagedByConfigServer = false;
Guid? managedConfigurationId = null;
if (plugin is PluginConfiguration configPlugin)
{
var directoryName = Path.GetFileName(pluginPath);
if (Guid.TryParse(directoryName, out var enterpriseConfigId) && enterpriseConfigId != plugin.Id)
LOG.LogWarning($"The configuration plugin's ID ('{plugin.Id}') does not match the enterprise configuration ID ('{enterpriseConfigId}'). These IDs should be identical. Please update the plugin's ID field to match the enterprise configuration ID.");
if (configPlugin.DeployedUsingConfigServer.HasValue)
isManagedByConfigServer = configPlugin.DeployedUsingConfigServer.Value;
else if (isConfigurationPluginInConfigDirectory)
{
isManagedByConfigServer = true;
LOG.LogWarning($"The configuration plugin '{plugin.Id}' does not define 'DEPLOYED_USING_CONFIG_SERVER'. Falling back to the plugin path and treating it as managed because it is stored under '{CONFIGURATION_PLUGINS_ROOT}'.");
}
}
AVAILABLE_PLUGINS.Add(new PluginMetadata(plugin, pluginPath));
// For configuration plugins, validate that the plugin ID matches the enterprise config ID
// (the directory name under which the plugin was downloaded):
if (isConfigurationPluginInConfigDirectory && isManagedByConfigServer)
{
var directoryName = Path.GetFileName(pluginPath);
if (Guid.TryParse(directoryName, out var enterpriseConfigId))
{
managedConfigurationId = enterpriseConfigId;
if (enterpriseConfigId != plugin.Id)
LOG.LogWarning($"The configuration plugin's ID ('{plugin.Id}') does not match the enterprise configuration ID ('{enterpriseConfigId}'). These IDs should be identical. Please update the plugin's ID field to match the enterprise configuration ID.");
}
else
LOG.LogWarning($"Could not determine the managed configuration ID for configuration plugin '{plugin.Id}'. The plugin directory '{pluginPath}' does not end with a valid GUID.");
}
AVAILABLE_PLUGINS.Add(new PluginMetadata(plugin, pluginPath, isManagedByConfigServer, managedConfigurationId));
}
catch (Exception e)
{
@@ -1,54 +1,129 @@
using System.Text.RegularExpressions;
namespace AIStudio.Tools.PluginSystem;
public static partial class PluginFactory
{
public static void RemovePluginAsync(Guid pluginId)
private const string REASON_NO_LONGER_REFERENCED = "no longer referenced by active enterprise environments";
public static void RemoveUnreferencedManagedConfigurationPlugins(ISet<Guid> activeConfigurationIds)
{
if (!IS_INITIALIZED)
if (!IsInitialized)
return;
LOG.LogWarning($"Try to remove plugin with ID: {pluginId}");
var pluginIdsToRemove = new HashSet<Guid>();
// Case 1: Plugins are already loaded and metadata is available.
foreach (var plugin in AVAILABLE_PLUGINS.Where(plugin =>
plugin.Type is PluginType.CONFIGURATION &&
plugin.IsManagedByConfigServer &&
!activeConfigurationIds.Contains(plugin.Id)))
pluginIdsToRemove.Add(plugin.Id);
// Case 2: Startup cleanup before the initial plugin load.
// In this case, we inspect the .config directories directly.
if (Directory.Exists(CONFIGURATION_PLUGINS_ROOT))
{
foreach (var pluginDirectory in Directory.EnumerateDirectories(CONFIGURATION_PLUGINS_ROOT))
{
var directoryName = Path.GetFileName(pluginDirectory);
if (!Guid.TryParse(directoryName, out var pluginId))
continue;
if (activeConfigurationIds.Contains(pluginId))
continue;
var deployFlag = ReadDeployFlagFromPluginFile(pluginDirectory);
var isManagedByConfigServer = deployFlag ?? true;
if (!deployFlag.HasValue)
LOG.LogWarning($"Configuration plugin '{pluginId}' does not define 'DEPLOYED_USING_CONFIG_SERVER'. Falling back to the plugin path and treating it as managed because it is stored under '{CONFIGURATION_PLUGINS_ROOT}'.");
if (isManagedByConfigServer)
pluginIdsToRemove.Add(pluginId);
}
}
foreach (var pluginId in pluginIdsToRemove)
RemovePluginAsync(pluginId, REASON_NO_LONGER_REFERENCED);
}
private static void RemovePluginAsync(Guid pluginId, string reason)
{
if (!IsInitialized)
return;
LOG.LogWarning("Removing plugin with ID '{PluginId}'. Reason: {Reason}.", pluginId, reason);
//
// Remove the plugin from the available plugins list:
//
var availablePluginToRemove = AVAILABLE_PLUGINS.FirstOrDefault(p => p.Id == pluginId);
if (availablePluginToRemove == null)
{
LOG.LogWarning($"No plugin found with ID: {pluginId}");
return;
}
AVAILABLE_PLUGINS.Remove(availablePluginToRemove);
if (availablePluginToRemove != null)
AVAILABLE_PLUGINS.Remove(availablePluginToRemove);
else
LOG.LogWarning("No available plugin found with ID '{PluginId}' while removing plugin. Reason: {Reason}.", pluginId, reason);
//
// Remove the plugin from the running plugins list:
//
var runningPluginToRemove = RUNNING_PLUGINS.FirstOrDefault(p => p.Id == pluginId);
if (runningPluginToRemove == null)
LOG.LogWarning($"No running plugin found with ID: {pluginId}");
LOG.LogWarning("No running plugin found with ID '{PluginId}' while removing plugin. Reason: {Reason}.", pluginId, reason);
else
RUNNING_PLUGINS.Remove(runningPluginToRemove);
//
// Delete the plugin directory:
//
var pluginDirectory = Path.Join(CONFIGURATION_PLUGINS_ROOT, availablePluginToRemove.Id.ToString());
if (Directory.Exists(pluginDirectory))
{
try
{
Directory.Delete(pluginDirectory, true);
LOG.LogInformation($"Plugin directory '{pluginDirectory}' deleted successfully.");
}
catch (Exception ex)
{
LOG.LogError(ex, $"Failed to delete plugin directory '{pluginDirectory}'.");
}
}
else
LOG.LogWarning($"Plugin directory '{pluginDirectory}' does not exist.");
DeleteConfigurationPluginDirectory(pluginId);
LOG.LogInformation($"Plugin with ID: {pluginId} removed successfully.");
LOG.LogInformation("Plugin with ID '{PluginId}' removed successfully. Reason: {Reason}.", pluginId, reason);
}
private static bool? ReadDeployFlagFromPluginFile(string pluginDirectory)
{
try
{
var pluginFile = Path.Join(pluginDirectory, "plugin.lua");
if (!File.Exists(pluginFile))
return null;
var pluginCode = File.ReadAllText(pluginFile);
var match = DeployedByConfigServerRegex().Match(pluginCode);
if (!match.Success)
return null;
return bool.TryParse(match.Groups[1].Value, out var deployFlag)
? deployFlag
: null;
}
catch (Exception ex)
{
LOG.LogWarning(ex, $"Failed to parse deployment flag from plugin directory '{pluginDirectory}'.");
return null;
}
}
private static void DeleteConfigurationPluginDirectory(Guid pluginId)
{
var pluginDirectory = Path.Join(CONFIGURATION_PLUGINS_ROOT, pluginId.ToString());
if (!Directory.Exists(pluginDirectory))
{
LOG.LogWarning($"Plugin directory '{pluginDirectory}' does not exist.");
return;
}
try
{
Directory.Delete(pluginDirectory, true);
LOG.LogInformation($"Plugin directory '{pluginDirectory}' deleted successfully.");
}
catch (Exception ex)
{
LOG.LogError(ex, $"Failed to delete plugin directory '{pluginDirectory}'.");
}
}
[GeneratedRegex(@"^\s*DEPLOYED_USING_CONFIG_SERVER\s*=\s*(true|false)\s*(?:--.*)?$", RegexOptions.IgnoreCase | RegexOptions.Multiline)]
private static partial Regex DeployedByConfigServerRegex();
}
@@ -34,7 +34,7 @@ public static partial class PluginFactory
if (startedBasePlugin is PluginLanguage languagePlugin)
{
BASE_LANGUAGE_PLUGIN = languagePlugin;
BaseLanguage = languagePlugin;
RUNNING_PLUGINS.Add(languagePlugin);
LOG.LogInformation($"Successfully started the base language plugin: Id='{languagePlugin.Id}', Type='{languagePlugin.Type}', Name='{languagePlugin.Name}', Version='{languagePlugin.Version}'");
}
@@ -44,7 +44,7 @@ public static partial class PluginFactory
catch (Exception e)
{
LOG.LogError(e, $"An error occurred while starting the base language plugin: Id='{baseLanguagePluginId}'.");
BASE_LANGUAGE_PLUGIN = NoPluginLanguage.INSTANCE;
BaseLanguage = NoPluginLanguage.INSTANCE;
}
}
@@ -106,8 +106,8 @@ public static partial class PluginFactory
//
// When this is a language plugin, we need to set the base language plugin.
//
if (plugin is PluginLanguage languagePlugin && BASE_LANGUAGE_PLUGIN != NoPluginLanguage.INSTANCE)
languagePlugin.SetBaseLanguage(BASE_LANGUAGE_PLUGIN);
if (plugin is PluginLanguage languagePlugin && BaseLanguage != NoPluginLanguage.INSTANCE)
languagePlugin.SetBaseLanguage(BaseLanguage);
if(plugin is PluginConfiguration configPlugin)
await configPlugin.InitializeAsync(false);
@@ -6,17 +6,17 @@ public static partial class PluginFactory
{
private static readonly ILogger LOG = Program.LOGGER_FACTORY.CreateLogger(nameof(PluginFactory));
private static readonly SettingsManager SETTINGS_MANAGER = Program.SERVICE_PROVIDER.GetRequiredService<SettingsManager>();
private static bool IS_INITIALIZED;
private static string DATA_DIR = string.Empty;
private static string PLUGINS_ROOT = string.Empty;
private static string INTERNAL_PLUGINS_ROOT = string.Empty;
private static string CONFIGURATION_PLUGINS_ROOT = string.Empty;
private static string HOT_RELOAD_LOCK_FILE = string.Empty;
private static FileSystemWatcher HOT_RELOAD_WATCHER = null!;
private static ILanguagePlugin BASE_LANGUAGE_PLUGIN = NoPluginLanguage.INSTANCE;
public static ILanguagePlugin BaseLanguage => BASE_LANGUAGE_PLUGIN;
public static ILanguagePlugin BaseLanguage { get; private set; } = NoPluginLanguage.INSTANCE;
public static bool IsInitialized { get; private set; }
/// <summary>
/// Gets the enterprise encryption instance for decrypting API keys in configuration plugins.
@@ -47,7 +47,7 @@ public static partial class PluginFactory
/// </summary>
public static bool Setup()
{
if(IS_INITIALIZED)
if(IsInitialized)
return false;
LOG.LogInformation("Initializing plugin factory...");
@@ -61,14 +61,14 @@ public static partial class PluginFactory
Directory.CreateDirectory(PLUGINS_ROOT);
HOT_RELOAD_WATCHER = new(PLUGINS_ROOT);
IS_INITIALIZED = true;
IsInitialized = true;
LOG.LogInformation("Plugin factory initialized successfully.");
return true;
}
private static async Task LockHotReloadAsync()
{
if (!IS_INITIALIZED)
if (!IsInitialized)
{
LOG.LogError("PluginFactory is not initialized.");
return;
@@ -92,7 +92,7 @@ public static partial class PluginFactory
private static void UnlockHotReload()
{
if (!IS_INITIALIZED)
if (!IsInitialized)
{
LOG.LogError("PluginFactory is not initialized.");
return;
@@ -113,7 +113,7 @@ public static partial class PluginFactory
public static void Dispose()
{
if(!IS_INITIALIZED)
if(!IsInitialized)
return;
HOT_RELOAD_WATCHER.Dispose();
@@ -1,6 +1,6 @@
namespace AIStudio.Tools.PluginSystem;
public sealed class PluginMetadata(PluginBase plugin, string localPath) : IAvailablePlugin
public sealed class PluginMetadata(PluginBase plugin, string localPath, bool isManagedByConfigServer = false, Guid? managedConfigurationId = null) : IAvailablePlugin
{
#region Implementation of IPluginMetadata
@@ -51,6 +51,10 @@ public sealed class PluginMetadata(PluginBase plugin, string localPath) : IAvail
#region Implementation of IAvailablePlugin
public string LocalPath { get; } = localPath;
public bool IsManagedByConfigServer { get; } = isManagedByConfigServer;
public Guid? ManagedConfigurationId { get; } = managedConfigurationId;
#endregion
}
}