AipFileApiService.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. using AipGateway.AIP;
  2. using AipGateway.API.Configurations;
  3. using AipGateway.API.Domain.Entities;
  4. using AipGateway.API.Domain.IRepositories.IGenericRepositories;
  5. using AipGateway.API.Infrastructure.Persistence;
  6. using AipGateway.API.Models;
  7. using AipGateway.API.Utils;
  8. using System.Collections;
  9. using System.Runtime.CompilerServices;
  10. namespace AipGateway.API.Services
  11. {
  12. public class AipFileApiService
  13. {
  14. private readonly ILogger<AipFileJobService> _log;
  15. private readonly AipSettings _aipSetting;
  16. private readonly ApplicationDbContext _dbContext;
  17. private readonly IUnitOfWork _unitOfWork;
  18. private int _ServerId;
  19. public AipFileApiService(ILogger<AipFileJobService> logger, IConfiguration configuration, ApplicationDbContext dbContext, IUnitOfWork unitOfWork)
  20. {
  21. _log = logger;
  22. _dbContext = dbContext;
  23. _unitOfWork = unitOfWork;
  24. _aipSetting = new();
  25. configuration.GetSection(nameof(AipSettings)).Bind(_aipSetting);
  26. if (!int.TryParse(configuration["ServerId"], out _ServerId))
  27. {
  28. _ServerId = 0;
  29. }
  30. _log.LogError("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: AipFileDbService");
  31. }
  32. public void LoadConfig()
  33. {
  34. _log.LogError("Before: {0}", _aipSetting.ToString());
  35. List<TbAipConfig> result = _dbContext.AipConfigs.Where(p => p.AipServerId == _ServerId).ToList();
  36. foreach(TbAipConfig config in result)
  37. {
  38. _log.LogInformation("{0}, {1}, {2}", config.Id, config.ConfigKey, config.ConfigValue);
  39. _aipSetting.SetValue(config.ConfigKey, config.ConfigValue);
  40. }
  41. _log.LogError(" After: {0}", _aipSetting.ToString());
  42. }
  43. public AipSettings GetAipSettings()
  44. {
  45. return _aipSetting;
  46. }
  47. private void DownloadAipFileLabels()
  48. {
  49. //using var transaction = _dbContext.Database.BeginTransaction();
  50. try
  51. {
  52. _log.LogInformation("AipFileApiService.DownloadAipFileLabels: Start.");
  53. Hashtable keyMap = new Hashtable();
  54. List<TbAipLabel> newLabels = new List<TbAipLabel>();
  55. List<TbAipLabel> updLabels = new List<TbAipLabel>();
  56. Hashtable labelMap = new Hashtable();
  57. List<AipLabel>? lavels = ContainerService.aipFileManager.SensitivityLabels();
  58. _log.LogInformation("AipFileApiService.DownloadAipFileLabels.SensitivityLabels(): {} EA.", lavels?.Count);
  59. if (lavels == null)
  60. {
  61. return;
  62. }
  63. FormattableString sql = $"SELECT t.LabelId, t.CreatedAt, t.DeletedAt, t.LabelDesc, t.LabelGuid, t.LabelName, t.UseYn FROM TB_AIP_LABEL AS t;";
  64. var result = _dbContext.SqlQuery<TbAipLabel>(sql).ToList();
  65. if (result != null)
  66. {
  67. foreach (TbAipLabel label in result)
  68. {
  69. labelMap.Add(label.LabelGuid, label);
  70. }
  71. }
  72. foreach (AipLabel label in lavels)
  73. {
  74. keyMap.Add(label.Id, label);
  75. if (labelMap.ContainsKey(label.Id))
  76. {
  77. TbAipLabel? orgLabel = labelMap[label.Id] as TbAipLabel;
  78. if (orgLabel != null)
  79. {
  80. if (orgLabel.IsChanged(label))
  81. {
  82. _log.LogInformation("변경된 레벨 데이터: {0}", label.Id);
  83. orgLabel.LabelName = label.Name;
  84. orgLabel.LabelDesc = label.Description;
  85. updLabels.Add(orgLabel);
  86. }
  87. else
  88. {
  89. _log.LogInformation("동일한 레벨 데이터: {0}", label.Id);
  90. }
  91. }
  92. else
  93. {
  94. _log.LogInformation("Not Found Label In Map: {0}", label.Id);
  95. }
  96. }
  97. else
  98. {
  99. _log.LogInformation("새로운 레벨 데이터: {0}", label.Id);
  100. TbAipLabel obj = new TbAipLabel()
  101. {
  102. LabelGuid = label.Id,
  103. LabelName = label.Name,
  104. LabelDesc = label.Description,
  105. CreatedAt = DateTime.Now,
  106. DeletedAt = null,
  107. UseYn = true,
  108. };
  109. newLabels.Add(obj);
  110. }
  111. }
  112. ContainerService.aipLableMap = keyMap;
  113. _log.LogInformation("AipFileApiService.DownloadAipFileLabels: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
  114. int updateCount = 0;
  115. foreach (TbAipLabel label in updLabels)
  116. {
  117. FormattableString usql = $"UPDATE TB_AIP_LABEL SET LabelDesc = {label.LabelDesc}, LabelName = {label.LabelName} WHERE LabelId = {label.LabelId};";
  118. updateCount += _dbContext.ExecuteSql(usql);
  119. }
  120. int insertCount = 0;
  121. foreach (TbAipLabel label in newLabels)
  122. {
  123. FormattableString isql = $"INSERT INTO TB_AIP_LABEL(LabelGuid, LabelName, LabelDesc, UseYn) VALUES({label.LabelGuid}, {label.LabelName}, {label.LabelDesc}, 1);";
  124. insertCount += _dbContext.ExecuteSql(isql);
  125. }
  126. _log.LogInformation("AipFileApiService.DownloadAipFileLabels: ..END. UPDATE {0}/{1}, NEW {2}/{3}.",
  127. updLabels.Count, updateCount, newLabels.Count, insertCount);
  128. }
  129. finally
  130. {
  131. //transaction.Commit();
  132. }
  133. #if false
  134. //var newLables = new List<TbAipLabel>();
  135. foreach(AipLabel label in lavels)
  136. {
  137. try
  138. {
  139. var val = _dbContext.AipLabels.AsNoTracking().Where(t => t.LabelGuid == label.Id).FirstOrDefault();
  140. if (val != null)
  141. {
  142. var m = _dbContext.Find<TbAipLabel>(val.LabelId);
  143. if (m != null)
  144. {
  145. _log.LogError("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: {0}, {1}", m.ToString(), label.Id);
  146. m.LabelDesc = label.Description;
  147. m.LabelName = label.Name;
  148. _dbContext.AipLabels.Update(m);
  149. }
  150. else
  151. {
  152. val.LabelDesc = label.Description;
  153. val.LabelName = label.Name;
  154. _dbContext.AipLabels.Update(val);
  155. }
  156. _dbContext.SaveChanges();
  157. }
  158. else
  159. {
  160. TbAipLabel obj = new TbAipLabel()
  161. {
  162. LabelGuid = label.Id,
  163. LabelName = label.Name,
  164. LabelDesc = label.Description,
  165. CreatedAt = DateTime.Now,
  166. DeletedAt = null,
  167. UseYn = true,
  168. };
  169. //newLables.Add(obj);
  170. //await _unitOfWork.AipLabelRepository.AddAsync(obj);
  171. //_unitOfWork.Complete();
  172. //_log.LogError("{0}", obj.ToString());
  173. _dbContext.Add<TbAipLabel>(obj);
  174. _dbContext.SaveChanges();
  175. }
  176. }
  177. catch(Exception ex)
  178. {
  179. _log.LogError(ex.ToString(), ex);
  180. }
  181. }
  182. #endif
  183. //if (newLables.Count > 0)
  184. //{
  185. // _dbContext.AipLabels.AddRange(newLables);
  186. // _dbContext.SaveChanges();
  187. //}
  188. }
  189. private void DownloadAipFilePolicies()
  190. {
  191. //using var transaction = _dbContext.Database.BeginTransaction();
  192. try
  193. {
  194. _log.LogInformation("AipFileApiService.DownloadAipFilePolicies: Start.");
  195. Hashtable keyMap = new Hashtable();
  196. List<TbAipPolicy> newLabels = new List<TbAipPolicy>();
  197. List<TbAipPolicy> updLabels = new List<TbAipPolicy>();
  198. Hashtable labelMap = new Hashtable();
  199. List<AipLabel>? lavels = ContainerService.aipFileManager.ListSensitivityLabels();
  200. _log.LogInformation("AipFileApiService.DownloadAipFilePolicies.ListSensitivityLabels(): {} EA.", lavels?.Count);
  201. if (lavels == null)
  202. {
  203. return;
  204. }
  205. FormattableString sql = $"SELECT t.PolicyId, t.CreatedAt, t.DeletedAt, t.PolicyDesc, t.PolicyGuid, t.PolicyName, t.UseYn FROM TB_AIP_POLICY AS t;";
  206. var result = _dbContext.SqlQuery<TbAipPolicy>(sql).ToList();
  207. if (result != null)
  208. {
  209. foreach (TbAipPolicy policy in result)
  210. {
  211. labelMap.Add(policy.PolicyGuid, policy);
  212. }
  213. }
  214. foreach (AipLabel label in lavels)
  215. {
  216. keyMap.Add(label.Id, label);
  217. if (labelMap.ContainsKey(label.Id))
  218. {
  219. TbAipPolicy? orgPolicy = labelMap[label.Id] as TbAipPolicy;
  220. if (orgPolicy != null)
  221. {
  222. if (orgPolicy.IsChanged(label))
  223. {
  224. _log.LogInformation("변경된 정책 데이터: {0}", label.Id);
  225. orgPolicy.PolicyName = label.Name;
  226. orgPolicy.PolicyDesc = label.Description;
  227. updLabels.Add(orgPolicy);
  228. }
  229. else
  230. {
  231. _log.LogInformation("동일한 정책 데이터: {0}", label.Id);
  232. }
  233. }
  234. else
  235. {
  236. _log.LogInformation("Not Found Policy In Map: {0}", label.Id);
  237. }
  238. }
  239. else
  240. {
  241. _log.LogInformation("새로운 정책 데이터: {0}", label.Id);
  242. TbAipPolicy obj = new TbAipPolicy()
  243. {
  244. PolicyGuid = label.Id,
  245. PolicyName = label.Name,
  246. PolicyDesc = label.Description,
  247. CreatedAt = DateTime.Now,
  248. DeletedAt = null,
  249. UseYn = true,
  250. };
  251. newLabels.Add(obj);
  252. }
  253. }
  254. ContainerService.aipPolicyMap = keyMap;
  255. _log.LogInformation("AipFileApiService.DownloadAipFilePolicies: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
  256. int updateCount = 0;
  257. foreach (TbAipPolicy label in updLabels)
  258. {
  259. FormattableString usql = $"UPDATE TB_AIP_POLICY SET PolicyDesc = {label.PolicyDesc}, PolicyName = {label.PolicyName} WHERE PolicyId = {label.PolicyId};";
  260. updateCount += _dbContext.ExecuteSql(usql);
  261. }
  262. int insertCount = 0;
  263. foreach (TbAipPolicy label in newLabels)
  264. {
  265. FormattableString isql = $"INSERT INTO TB_AIP_POLICY(PolicyGuid, PolicyName, PolicyDesc, UseYn) VALUES({label.PolicyGuid}, {label.PolicyName}, {label.PolicyDesc}, 1);";
  266. insertCount += _dbContext.ExecuteSql(isql);
  267. }
  268. _log.LogInformation("AipFileApiService.DownloadAipFilePolicies: ..END. UPDATE {0}/{1}, NEW {2}/{3}.",
  269. updLabels.Count, updateCount, newLabels.Count, insertCount);
  270. }
  271. finally
  272. {
  273. //transaction.Commit();
  274. }
  275. }
  276. private void DownloadAipFileProtections()
  277. {
  278. //using var transaction = _dbContext.Database.BeginTransaction();
  279. try
  280. {
  281. _log.LogInformation("AipFileApiService.DownloadAipFileProtections: Start.");
  282. Hashtable keyMap = new Hashtable();
  283. List<TbAipProtection> newLabels = new List<TbAipProtection>();
  284. List<TbAipProtection> updLabels = new List<TbAipProtection>();
  285. Hashtable labelMap = new Hashtable();
  286. List<AipTemplate>? templates = ContainerService.aipFileManager.GetTemplates();
  287. _log.LogInformation("AipFileApiService.DownloadAipFileProtections.GetTemplates(): {} EA.", templates?.Count);
  288. if (templates == null)
  289. {
  290. return;
  291. }
  292. FormattableString sql = $"SELECT t.ProtectionId, t.CreatedAt, t.DeletedAt, t.ProtectionDesc, t.ProtectionGuid, t.ProtectionName, t.UseYn FROM TB_AIP_PROTECTION AS t;";
  293. var result = _dbContext.SqlQuery<TbAipProtection>(sql).ToList();
  294. if (result != null)
  295. {
  296. foreach (TbAipProtection policy in result)
  297. {
  298. labelMap.Add(policy.ProtectionGuid, policy);
  299. }
  300. }
  301. foreach (AipTemplate template in templates)
  302. {
  303. keyMap.Add(template.Id, template);
  304. if (labelMap.ContainsKey(template.Id))
  305. {
  306. TbAipProtection? orgProtection = labelMap[template.Id] as TbAipProtection;
  307. if (orgProtection != null)
  308. {
  309. if (orgProtection.IsChanged(template))
  310. {
  311. _log.LogInformation("변경된 정책 데이터: {0}", template.Id);
  312. orgProtection.ProtectionName = template.Name;
  313. orgProtection.ProtectionDesc = template.Description;
  314. updLabels.Add(orgProtection);
  315. }
  316. else
  317. {
  318. _log.LogInformation("동일한 정책 데이터: {0}", template.Id);
  319. }
  320. }
  321. else
  322. {
  323. _log.LogInformation("Not Found Policy In Map: {0}", template.Id);
  324. }
  325. }
  326. else
  327. {
  328. _log.LogInformation("새로운 정책 데이터: {0}", template.Id);
  329. TbAipProtection obj = new TbAipProtection()
  330. {
  331. ProtectionGuid = template.Id,
  332. ProtectionName = template.Name,
  333. ProtectionDesc = template.Description,
  334. CreatedAt = DateTime.Now,
  335. DeletedAt = null,
  336. UseYn = true,
  337. };
  338. newLabels.Add(obj);
  339. }
  340. }
  341. ContainerService.aipProtectionMap = keyMap;
  342. _log.LogInformation("AipFileApiService.DownloadAipFileProtections: UPDATE {0}, NEW {1}.", updLabels.Count, newLabels.Count);
  343. int updateCount = 0;
  344. foreach (TbAipProtection protection in updLabels)
  345. {
  346. FormattableString usql = $"UPDATE TB_AIP_PROTECTION SET ProtectionDesc = {protection.ProtectionDesc}, ProtectionName = {protection.ProtectionName} WHERE ProtectionId = {protection.ProtectionId};";
  347. updateCount += _dbContext.ExecuteSql(usql);
  348. }
  349. int insertCount = 0;
  350. foreach (TbAipProtection protection in newLabels)
  351. {
  352. FormattableString isql = $"INSERT INTO TB_AIP_PROTECTION(ProtectionGuid, ProtectionName, ProtectionDesc, UseYn) VALUES({protection.ProtectionGuid}, {protection.ProtectionName}, {protection.ProtectionDesc}, 1);";
  353. insertCount += _dbContext.ExecuteSql(isql);
  354. }
  355. _log.LogInformation("AipFileApiService.DownloadAipFileProtections: ..END. UPDATE {0}/{1}, NEW {2}/{3}.",
  356. updLabels.Count, updateCount, newLabels.Count, insertCount);
  357. }
  358. finally
  359. {
  360. //transaction.Commit();
  361. }
  362. }
  363. public void DownloadAipFileInformations()
  364. {
  365. DownloadAipFileLabels();
  366. DownloadAipFilePolicies();
  367. DownloadAipFileProtections();
  368. }
  369. public void LoadLinkedApiKeys()
  370. {
  371. Hashtable keyMap = new Hashtable();
  372. string sql = @"SELECT A.Id AS ApiKeyId,
  373. A.ApiKey,
  374. A.policyLookupYn,
  375. A.fileInfoLookupYn,
  376. A.applyLabelYn,
  377. A.releaseLabelYn,
  378. A.encryptionFileYn,
  379. A.decryptionFileYn,
  380. A.ExpiredAt,
  381. B.ServerId, B.ServerIpAddr, B.ServerDesc,
  382. C.SystemId, C.SystemName
  383. FROM TB_LINKED_API_KEY A
  384. INNER JOIN TB_LINKED_SERVER B
  385. ON A.ServerId = B.ServerId
  386. AND A.UseYn = 1
  387. AND B.UseYn = 1
  388. INNER JOIN TB_LINKED_SYSTEM C
  389. ON B.SystemId = C.SystemId
  390. AND C.UseYn = 1;";
  391. List<LinkedApiKey> result = (List<LinkedApiKey>)_dbContext.SqlQuery<LinkedApiKey>(FormattableStringFactory.Create(sql)).ToList();
  392. if (result != null)
  393. {
  394. foreach (LinkedApiKey key in result)
  395. {
  396. LinkedApiKey? apiKey = keyMap[key.ApiKey] as LinkedApiKey;
  397. if (apiKey == null)
  398. {
  399. // SERVER 추가
  400. key.serverMap = new Hashtable();
  401. key.serverMap.Add(key.ServerIpAddr, key);
  402. // API KEY 추가
  403. keyMap.Add(key.ApiKey, key);
  404. }
  405. else
  406. {
  407. LinkedApiKey? server = apiKey.serverMap[key.ServerIpAddr] as LinkedApiKey;
  408. if (server == null)
  409. {
  410. apiKey.serverMap.Add(key.ServerIpAddr, key);
  411. }
  412. }
  413. }
  414. }
  415. ContainerService.apiKeyMap = keyMap;
  416. foreach(string key in ContainerService.apiKeyMap.Keys)
  417. {
  418. LinkedApiKey? apiKey = ContainerService.apiKeyMap[key] as LinkedApiKey;
  419. if (apiKey != null)
  420. {
  421. _log.LogError(" API KEY: {0}", apiKey.ApiKey);
  422. foreach (string serverIp in apiKey.serverMap.Keys)
  423. {
  424. LinkedApiKey? server = apiKey.serverMap[serverIp] as LinkedApiKey;
  425. if (server != null)
  426. {
  427. _log.LogError("API KEY SERVER: {0}, {1}", server.ApiKey, server.ServerIpAddr);
  428. }
  429. }
  430. }
  431. }
  432. }
  433. public void LoadLinkedDecryptKeys()
  434. {
  435. Hashtable keyMap = new Hashtable();
  436. string sql = @"SELECT A.Id AS DecryptKeyId, A.DecryptKey, A.ExpiredAt,
  437. B.ServerId, B.ServerIpAddr, B.ServerDesc,
  438. C.SystemId, C.SystemName
  439. FROM TB_LINKED_DECRYPT_KEY A
  440. INNER JOIN TB_LINKED_SERVER B
  441. ON A.ServerId = B.ServerId
  442. AND A.UseYn = 1
  443. AND B.UseYn = 1
  444. INNER JOIN TB_LINKED_SYSTEM C
  445. ON B.SystemId = C.SystemId
  446. AND C.UseYn = 1;";
  447. List<LinkedDecryptApiKey> result = (List<LinkedDecryptApiKey>)_dbContext.SqlQuery<LinkedDecryptApiKey>(FormattableStringFactory.Create(sql)).ToList();
  448. if (result != null)
  449. {
  450. foreach (LinkedDecryptApiKey key in result)
  451. {
  452. LinkedDecryptApiKey? apiKey = keyMap[key.DecryptKey] as LinkedDecryptApiKey;
  453. if (apiKey == null)
  454. {
  455. // SERVER 추가
  456. key.serverMap = new Hashtable();
  457. key.serverMap.Add(key.ServerIpAddr, key);
  458. // DESCRYPT KEY 추가
  459. keyMap.Add(key.DecryptKey, key);
  460. }
  461. else
  462. {
  463. LinkedDecryptApiKey? server = apiKey.serverMap[key.ServerIpAddr] as LinkedDecryptApiKey;
  464. if (server == null)
  465. {
  466. apiKey.serverMap.Add(key.ServerIpAddr, key);
  467. }
  468. }
  469. }
  470. }
  471. ContainerService.decryptKeyMap = keyMap;
  472. foreach (string key in ContainerService.decryptKeyMap.Keys)
  473. {
  474. LinkedDecryptApiKey? apiKey = ContainerService.apiKeyMap[key] as LinkedDecryptApiKey;
  475. if (apiKey != null)
  476. {
  477. _log.LogError(" DESCRYPT KEY: {0}", apiKey.DecryptKey);
  478. foreach (string serverIp in apiKey.serverMap.Keys)
  479. {
  480. LinkedDecryptApiKey? server = apiKey.serverMap[serverIp] as LinkedDecryptApiKey;
  481. if (server != null)
  482. {
  483. _log.LogError("DESCRYPT KEY SERVER: {0}, {1}", server.DecryptKey, server.ServerIpAddr);
  484. }
  485. }
  486. }
  487. }
  488. }
  489. }
  490. }