2026-06-21 13:16:37 +00:00
|
|
|
using AIStudio.Provider;
|
2026-08-05 13:05:17 +00:00
|
|
|
using AIStudio.Settings.DataModel;
|
2026-06-21 13:16:37 +00:00
|
|
|
|
|
|
|
|
namespace AIStudio.Settings;
|
|
|
|
|
|
|
|
|
|
public static class DataSourceSecurityTrustExtensions
|
|
|
|
|
{
|
|
|
|
|
public static bool IsTrustedForDataSourceSecurityChecks(this Provider provider, SettingsManager settingsManager)
|
|
|
|
|
{
|
|
|
|
|
if (provider == Provider.NONE)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return provider.IsSelfHosted || provider.IsTrustedByConfiguration(settingsManager);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsTrustedForDataSourceSecurityChecks(this EmbeddingProvider provider, SettingsManager settingsManager)
|
|
|
|
|
{
|
|
|
|
|
if (provider == EmbeddingProvider.NONE)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return provider.IsSelfHosted || provider.IsTrustedByConfiguration(settingsManager);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsTrustedForDataSourceSecurityChecks(this TranscriptionProvider provider, SettingsManager settingsManager)
|
|
|
|
|
{
|
|
|
|
|
if (provider == TranscriptionProvider.NONE)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return provider.IsSelfHosted || provider.IsTrustedByConfiguration(settingsManager);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsTrustedForDataSourceSecurityChecks(this IProvider provider, SettingsManager settingsManager)
|
|
|
|
|
{
|
|
|
|
|
if (provider is NoProvider)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return provider.Provider is LLMProviders.SELF_HOSTED || IsTrustedProviderId(provider.ConfiguredProviderId, settingsManager);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-05 13:05:17 +00:00
|
|
|
public static ConfidenceLevel GetConfidenceLevel(this Provider provider, SettingsManager settingsManager)
|
|
|
|
|
{
|
|
|
|
|
if (provider == Provider.NONE)
|
|
|
|
|
return ConfidenceLevel.NONE;
|
|
|
|
|
|
|
|
|
|
return provider.UsedLLMProvider.GetConfidence(settingsManager).Level;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ConfidenceLevel GetConfidenceLevel(this EmbeddingProvider provider, SettingsManager settingsManager)
|
|
|
|
|
{
|
|
|
|
|
if (provider == EmbeddingProvider.NONE)
|
|
|
|
|
return ConfidenceLevel.NONE;
|
|
|
|
|
|
|
|
|
|
return provider.UsedLLMProvider.GetConfidence(settingsManager).Level;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ConfidenceLevel GetConfidenceLevel(this IProvider provider, SettingsManager settingsManager)
|
|
|
|
|
{
|
|
|
|
|
if (provider is NoProvider)
|
|
|
|
|
return ConfidenceLevel.NONE;
|
|
|
|
|
|
|
|
|
|
return provider.Provider.GetConfidence(settingsManager).Level;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-14 10:03:16 +00:00
|
|
|
public static bool AllowsDataSourceAccess(this Provider provider, SettingsManager settingsManager, DataSourceSecurity dataSourceSecurity, ConfidenceLevel requiredConfidenceLevel)
|
2026-08-05 13:05:17 +00:00
|
|
|
{
|
|
|
|
|
return provider.AllowsDataSourceSecurity(dataSourceSecurity, settingsManager)
|
2026-08-14 10:03:16 +00:00
|
|
|
&& provider.GetConfidenceLevel(settingsManager).AllowsDataSourceConfidenceLevel(requiredConfidenceLevel);
|
2026-08-05 13:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
2026-08-14 10:03:16 +00:00
|
|
|
public static bool AllowsDataSourceAccess(this IProvider provider, SettingsManager settingsManager, DataSourceSecurity dataSourceSecurity, ConfidenceLevel requiredConfidenceLevel)
|
2026-08-05 13:05:17 +00:00
|
|
|
{
|
|
|
|
|
return provider.AllowsDataSourceSecurity(dataSourceSecurity, settingsManager)
|
2026-08-14 10:03:16 +00:00
|
|
|
&& provider.GetConfidenceLevel(settingsManager).AllowsDataSourceConfidenceLevel(requiredConfidenceLevel);
|
2026-08-05 13:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool AllowsDataSourceSecurity(this Provider provider, DataSourceSecurity dataSourceSecurity, SettingsManager settingsManager)
|
|
|
|
|
=> provider.IsTrustedForDataSourceSecurityChecks(settingsManager).AllowsDataSourceSecurity(dataSourceSecurity);
|
|
|
|
|
|
|
|
|
|
public static bool AllowsDataSourceSecurity(this IProvider provider, DataSourceSecurity dataSourceSecurity, SettingsManager settingsManager)
|
|
|
|
|
=> provider.IsTrustedForDataSourceSecurityChecks(settingsManager).AllowsDataSourceSecurity(dataSourceSecurity);
|
|
|
|
|
|
|
|
|
|
public static bool AllowsDataSourceSecurity(this bool usingTrustedProvider, DataSourceSecurity dataSourceSecurity) => dataSourceSecurity switch
|
|
|
|
|
{
|
|
|
|
|
DataSourceSecurity.ALLOW_ANY => true,
|
|
|
|
|
DataSourceSecurity.SELF_HOSTED => usingTrustedProvider,
|
|
|
|
|
_ => false,
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-14 10:03:16 +00:00
|
|
|
public static bool AllowsDataSourceConfidenceLevel(this ConfidenceLevel providerConfidenceLevel, ConfidenceLevel requiredConfidenceLevel)
|
2026-08-05 13:05:17 +00:00
|
|
|
{
|
2026-08-14 10:03:16 +00:00
|
|
|
if (requiredConfidenceLevel is ConfidenceLevel.NONE)
|
2026-08-05 13:05:17 +00:00
|
|
|
return true;
|
|
|
|
|
|
2026-08-14 10:03:16 +00:00
|
|
|
return providerConfidenceLevel >= requiredConfidenceLevel;
|
2026-08-05 13:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
2026-08-14 10:03:16 +00:00
|
|
|
public static ConfidenceLevel GetRequiredConfidenceLevel(this IEnumerable<IDataSource> dataSources)
|
2026-08-05 13:05:17 +00:00
|
|
|
{
|
2026-08-14 10:03:16 +00:00
|
|
|
var requiredConfidenceLevel = ConfidenceLevel.NONE;
|
|
|
|
|
foreach (var dataSource in dataSources.OfType<IInternalDataSource>())
|
|
|
|
|
if (dataSource.ConfidenceLevel > requiredConfidenceLevel)
|
|
|
|
|
requiredConfidenceLevel = dataSource.ConfidenceLevel;
|
2026-08-05 13:05:17 +00:00
|
|
|
|
2026-08-14 10:03:16 +00:00
|
|
|
return requiredConfidenceLevel;
|
2026-08-05 13:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DataSourceSecurity GetRequiredSecurityPolicy(this IEnumerable<IDataSource> dataSources)
|
|
|
|
|
{
|
|
|
|
|
var requiredSecurityPolicy = DataSourceSecurity.ALLOW_ANY;
|
2026-08-14 10:03:16 +00:00
|
|
|
foreach (var dataSource in dataSources.OfType<IExternalDataSource>())
|
2026-08-05 13:05:17 +00:00
|
|
|
{
|
|
|
|
|
if (dataSource.SecurityPolicy is DataSourceSecurity.NOT_SPECIFIED)
|
|
|
|
|
return DataSourceSecurity.NOT_SPECIFIED;
|
|
|
|
|
|
|
|
|
|
if (dataSource.SecurityPolicy is DataSourceSecurity.SELF_HOSTED)
|
|
|
|
|
requiredSecurityPolicy = DataSourceSecurity.SELF_HOSTED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return requiredSecurityPolicy;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 13:16:37 +00:00
|
|
|
public static bool IsTrustedByConfiguration(this Provider provider, SettingsManager settingsManager) => IsTrustedProviderId(provider.Id, settingsManager);
|
|
|
|
|
|
|
|
|
|
public static bool IsTrustedByConfiguration(this EmbeddingProvider provider, SettingsManager settingsManager) => IsTrustedProviderId(provider.Id, settingsManager);
|
|
|
|
|
|
|
|
|
|
public static bool IsTrustedByConfiguration(this TranscriptionProvider provider, SettingsManager settingsManager) => IsTrustedProviderId(provider.Id, settingsManager);
|
|
|
|
|
|
|
|
|
|
private static bool IsTrustedProviderId(string providerId, SettingsManager settingsManager)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(providerId))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return settingsManager.ConfigurationData.DataSourceSecurity.TrustedProviderIds.Any(id => string.Equals(id, providerId, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
}
|
|
|
|
|
}
|