فهرست منبع

Merge branch 'next' of https://gitee.com/zuohuaijun/Admin.NET into next

zuohuaijun 2 سال پیش
والد
کامیت
3ec0acea30

+ 8 - 1
Admin.NET/Admin.NET.Application/Configuration/Logging.json

@@ -21,7 +21,14 @@
     },
     "ElasticSearch": {
       "Enabled": false, // 启用ES日志
-      "ServerUris": [ "http://dilon:123456@192.168.1.100:9200" ], // 地址
+      "AuthType": "Basic", //ES认证类型,可选 Basic、ApiKey、Base64ApiKey
+      "User": "dilon", //Basic认证的用户名,使用Basic认证类型时必填
+      "Password": "123456", //Basic认证的密码,使用Basic认证类型时必填
+      "ApiId": "", //使用ApiKey认证类型时必填
+      "ApiKey": "", //使用ApiKey认证类型时必填
+      "Base64ApiKey": "TmtrOEszNEJuQ0NyaWlydGtROFk6SG1RZ0w3YzBTc2lCanJTYlV3aXNzZw==", //使用Base64ApiKey认证类型时必填
+      "Fingerprint": "37:08:6A:C6:06:CC:9A:43:CF:ED:25:A2:1C:A4:69:57:90:31:2C:06:CA:61:56:39:6A:9C:46:11:BD:22:51:DA", //ES使用Https时的证书指纹,使用证书请自行实现
+      "ServerUris": [ "http://192.168.1.100:9200" ], // 地址
       "DefaultIndex": "adminnet" // 索引
     },
     "Monitor": {

+ 28 - 0
Admin.NET/Admin.NET.Core/Enum/EsAuthTypeEnum.cs

@@ -0,0 +1,28 @@
+// 大名科技(天津)有限公司版权所有  电话:18020030720  QQ:515096995
+//
+// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
+
+namespace Admin.NET.Core;
+/// <summary>
+/// ES认证类型枚举
+/// <para>https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/_options_on_elasticsearchclientsettings.html</para>
+/// </summary>
+[Description("ES认证类型枚举")]
+public enum EsAuthTypeEnum
+{
+	/// <summary>
+	/// BasicAuthentication
+	/// </summary>
+	[Description("BasicAuthentication")]
+	Basic = 1,
+	/// <summary>
+	/// ApiKey
+	/// </summary>
+	[Description("ApiKey")]
+	ApiKey = 2,
+	/// <summary>
+	/// Base64ApiKey
+	/// </summary>
+	[Description("Base64ApiKey")]
+	Base64ApiKey = 3
+}

+ 33 - 16
Admin.NET/Admin.NET.Core/Logging/ElasticSearchSetup.cs

@@ -12,23 +12,40 @@ namespace Admin.NET.Core;
 /// </summary>
 public static class ElasticSearchSetup
 {
-    public static void AddElasticSearch(this IServiceCollection services)
-    {
-        var enabled = App.GetConfig<bool>("Logging:ElasticSearch:Enabled", true);
-        if (!enabled) return;
+	public static void AddElasticSearch(this IServiceCollection services)
+	{
+		var option = App.GetConfig<ElasticSearchOptions>("Logging:ElasticSearch");
+		if (!option.Enabled) return;
 
-        var serverUris = App.GetConfig<List<string>>("Logging:ElasticSearch:ServerUris", true);
-        var defaultIndex = App.GetConfig<string>("Logging:ElasticSearch:DefaultIndex", true);
+		var uris = option.ServerUris.Select(u => new Uri(u));
+		// 集群
+		var connectionPool = new SniffingConnectionPool(uris);
+		var connectionSettings = new ConnectionSettings(connectionPool).DefaultIndex(option.DefaultIndex);
+		// 单连接
+		//var connectionSettings = new ConnectionSettings(new SingleNodeConnectionPool(uris.FirstOrDefault())).DefaultIndex(option.DefaultIndex);
+		//  认证类型
+		if (option.AuthType == EsAuthTypeEnum.Basic)// Basic 认证
+		{
+			connectionSettings.BasicAuthentication(option.User, option.Password);
+		}
+		else if (option.AuthType == EsAuthTypeEnum.ApiKey) //ApiKey 认证
+		{
+			connectionSettings.ApiKeyAuthentication(option.ApiId, option.ApiKey);
+		}
+		else if (option.AuthType == EsAuthTypeEnum.Base64ApiKey)// Base64ApiKey 认证
+		{
+			connectionSettings.ApiKeyAuthentication(new ApiKeyAuthenticationCredentials(option.Base64ApiKey));
+		}
+		else return;
+		//ES使用Https时的证书指纹,使用证书请自行实现
+		if (!string.IsNullOrEmpty(option.Fingerprint))
+		{
+			connectionSettings.CertificateFingerprint(option.Fingerprint);
+		}
 
-        var uris = serverUris.Select(u => new Uri(u));
-        // 集群
-        var connectionPool = new SniffingConnectionPool(uris);
-        var connectionSettings = new ConnectionSettings(connectionPool).DefaultIndex(defaultIndex);
-        // 单连接
-        //var connectionSettings = new ConnectionSettings(new SingleNodeConnectionPool(uris.FirstOrDefault())).DefaultIndex(defaultIndex);
-        var client = new ElasticClient(connectionSettings);
-        client.Indices.Create(defaultIndex, u => u.Map<SysLogOp>(m => m.AutoMap()));
+		var client = new ElasticClient(connectionSettings);
+		client.Indices.Create(option.DefaultIndex, u => u.Map<SysLogOp>(m => m.AutoMap()));
 
-        services.AddSingleton(client); // 单例注册
-    }
+		services.AddSingleton(client); // 单例注册
+	}
 }

+ 59 - 0
Admin.NET/Admin.NET.Core/Option/ElasticSearchOptions.cs

@@ -0,0 +1,59 @@
+// 大名科技(天津)有限公司版权所有  电话:18020030720  QQ:515096995
+//
+// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
+
+namespace Admin.NET.Core;
+public class ElasticSearchOptions
+{
+	/// <summary>
+	/// 是否启用
+	/// </summary>
+	public bool Enabled { get; set; } = false;
+
+	/// <summary>
+	/// ES认证类型,可选 Basic、ApiKey、Base64ApiKey
+	/// </summary>
+	public EsAuthTypeEnum AuthType { get; set; }
+
+	/// <summary>
+	/// Basic认证的用户名
+	/// </summary>
+	public string User { get; set; }
+
+	/// <summary>
+	/// Basic认证的密码
+	/// </summary>
+	public string Password { get; set; }
+
+	/// <summary>
+	/// ApiKey认证的ApiId
+	/// </summary>
+	public string ApiId { get; set; }
+
+	/// <summary>
+	/// ApiKey认证的ApiKey
+	/// </summary>
+	public string ApiKey { get; set; }
+
+	/// <summary>
+	/// Base64ApiKey认证时加密的加密字符串
+	/// </summary>
+	public string Base64ApiKey { get; set; }
+
+	/// <summary>
+	/// ES使用Https时的证书指纹,使用证书请自行实现
+	/// <para>https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/connecting.html</para>
+	/// </summary>
+	public string Fingerprint { get; set; }
+
+	/// <summary>
+	/// 地址
+	/// </summary>
+	public List<string> ServerUris { get; set; } = new List<string>();
+
+	/// <summary>
+	/// 索引
+	/// </summary>
+
+	public string DefaultIndex { get; set; }
+}