diff --git a/src/main/java/com/loafle/overflow/module/auth/dao/AuthCrawlerDAO.java b/src/main/java/com/loafle/overflow/module/auth/dao/AuthCrawlerDAO.java index 093c1cf..2f9ccb1 100644 --- a/src/main/java/com/loafle/overflow/module/auth/dao/AuthCrawlerDAO.java +++ b/src/main/java/com/loafle/overflow/module/auth/dao/AuthCrawlerDAO.java @@ -1,6 +1,8 @@ package com.loafle.overflow.module.auth.dao; import com.loafle.overflow.module.auth.model.AuthCrawler; +import com.loafle.overflow.module.meta.model.MetaCrawler; +import com.loafle.overflow.module.target.model.Target; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -9,4 +11,5 @@ import org.springframework.stereotype.Repository; */ @Repository public interface AuthCrawlerDAO extends JpaRepository { + AuthCrawler findByCrawlerAndTarget(MetaCrawler metaCrawler, Target target); } diff --git a/src/main/java/com/loafle/overflow/module/auth/service/AuthCrawlerService.java b/src/main/java/com/loafle/overflow/module/auth/service/AuthCrawlerService.java index 2bd1488..f8b4ef7 100644 --- a/src/main/java/com/loafle/overflow/module/auth/service/AuthCrawlerService.java +++ b/src/main/java/com/loafle/overflow/module/auth/service/AuthCrawlerService.java @@ -5,6 +5,7 @@ import com.loafle.overflow.module.auth.model.AuthCrawler; import com.loafle.overflow.module.infra.model.Infra; import com.loafle.overflow.module.infra.service.InfraService; import com.loafle.overflow.module.meta.model.MetaCrawler; +import com.loafle.overflow.module.target.model.Target; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -28,8 +29,14 @@ public class AuthCrawlerService { Infra infra = this.infraService.read(infraId); - // FIXME: Check Crawler + // FIXME: Check Crawler on Probe return false; } + + public AuthCrawler readAuth(MetaCrawler metaCrawler, Target target) { + + return this.authCrawlerDAO.findByCrawlerAndTarget(metaCrawler, target); + } + } diff --git a/src/main/java/com/loafle/overflow/module/generator/service/GenerateUtil.java b/src/main/java/com/loafle/overflow/module/generator/service/GenerateUtil.java index cb9ef76..538caad 100644 --- a/src/main/java/com/loafle/overflow/module/generator/service/GenerateUtil.java +++ b/src/main/java/com/loafle/overflow/module/generator/service/GenerateUtil.java @@ -1,15 +1,11 @@ package com.loafle.overflow.module.generator.service; -import com.loafle.overflow.commons.utils.StringConvertor; -import com.loafle.overflow.crawler.config.Connection; import com.loafle.overflow.crawler.config.Crawler; import com.loafle.overflow.crawler.config.Keys; -import com.loafle.overflow.module.infra.model.InfraHost; import com.loafle.overflow.module.meta.model.MetaCrawler; import com.loafle.overflow.module.meta.model.MetaSensorItemKey; import com.loafle.overflow.module.meta.service.MetaSensorItemKeyService; import com.loafle.overflow.module.meta.type.MetaCrawlerEnum; -import com.loafle.overflow.module.sensor.model.Sensor; import com.loafle.overflow.module.sensor.model.SensorItem; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -28,6 +24,8 @@ public class GenerateUtil { @Autowired private MetaSensorItemKeyService metaSensorItemKeyService; + + private Map> mappingMap = null; public Map initMappingMap(MetaCrawler metaCrawler) { @@ -48,18 +46,7 @@ public class GenerateUtil { return resultMap; } - public Map getAuth(MetaCrawler metaCrawler, com.loafle.overflow.module.infra.model.InfraService infraService) { - Map auth = new HashMap<>(); - - if(metaCrawler.getId() == MetaCrawlerEnum.MYSQL_CRAWLER.getValue()) { - auth.put("url", "jdbc:mysql://"+ StringConvertor.intToIp(infraService.getHost().getIp())+":"+String.valueOf(infraService.getPort())); - auth.put("id", "docker"); // FIXME: Auth Info - auth.put("pw", "qwer5795QWER"); // FIXME: Auth Info - } - - return auth; - } public Crawler getCrawler(MetaCrawler metaCrawler) { @@ -85,30 +72,7 @@ public class GenerateUtil { return crawler; } - public com.loafle.overflow.crawler.config.Target createTarget(InfraHost infraHost, Sensor dbSensor) { - com.loafle.overflow.crawler.config.Target target = new com.loafle.overflow.crawler.config.Target(); - Connection connection = new Connection(); - connection.setIp(StringConvertor.intToIp(infraHost.getIp())); - - if(dbSensor.getCrawler().getId() == MetaCrawlerEnum.WMI_CRAWLER.getValue()) { - connection.setPort("135"); - connection.setPortType("tcp"); - connection.setSsl(false); - - target.setConnection(connection); - - // FIXME: load crawler Auth Info - Map auth = new HashMap<>(); - auth.put("id", "administrator"); - auth.put("pw", "dbseogns18"); - - target.setAuth(auth); - - } - - return target; - } public Map> sortItems(List sensorItems, Map keyMap) { diff --git a/src/main/java/com/loafle/overflow/module/generator/service/InfraHostGenerator.java b/src/main/java/com/loafle/overflow/module/generator/service/InfraHostGenerator.java index f89dd02..72dd495 100644 --- a/src/main/java/com/loafle/overflow/module/generator/service/InfraHostGenerator.java +++ b/src/main/java/com/loafle/overflow/module/generator/service/InfraHostGenerator.java @@ -1,8 +1,12 @@ package com.loafle.overflow.module.generator.service; +import com.loafle.overflow.commons.utils.StringConvertor; import com.loafle.overflow.crawler.config.Config; +import com.loafle.overflow.crawler.config.Connection; import com.loafle.overflow.crawler.config.Crawler; import com.loafle.overflow.crawler.config.Schedule; +import com.loafle.overflow.module.auth.model.AuthCrawler; +import com.loafle.overflow.module.auth.service.AuthCrawlerService; import com.loafle.overflow.module.infra.model.Infra; import com.loafle.overflow.module.infra.model.InfraHost; import com.loafle.overflow.module.meta.model.MetaSensorItemKey; @@ -10,10 +14,12 @@ import com.loafle.overflow.module.meta.type.MetaCrawlerEnum; import com.loafle.overflow.module.sensor.model.Sensor; import com.loafle.overflow.module.sensor.model.SensorItem; import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -29,6 +35,9 @@ public class InfraHostGenerator { @Autowired private InfraHostWMIGenerator infraHostWMIGenerator; + @Autowired + private AuthCrawlerService authCrawlerService; + public String process(Sensor dbSensor, List sensorItems, Infra infra) throws IOException { InfraHost infraHost = (InfraHost)infra; @@ -36,7 +45,11 @@ public class InfraHostGenerator { Config config = new Config(); config.setId(String.valueOf(dbSensor.getId())); - com.loafle.overflow.crawler.config.Target target = this.generateUtil.createTarget(infraHost, dbSensor); + com.loafle.overflow.crawler.config.Target target = this.createTarget(infraHost, dbSensor); + + if(target == null) { + return null; + } config.setTarget(target); @@ -57,4 +70,37 @@ public class InfraHostGenerator { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.writeValueAsString(config); } + + private com.loafle.overflow.crawler.config.Target createTarget(InfraHost infraHost, Sensor dbSensor) throws IOException { + + AuthCrawler authCrawler = this.authCrawlerService.readAuth(dbSensor.getCrawler(), dbSensor.getTarget()); + + if(authCrawler == null) { + return null; + } + + com.loafle.overflow.crawler.config.Target target = new com.loafle.overflow.crawler.config.Target(); + Connection connection = new Connection(); + connection.setIp(StringConvertor.intToIp(infraHost.getIp())); + + HashMap optionMap; + optionMap = new ObjectMapper().readValue(authCrawler.getAuthJson(), new TypeReference>() {}); + + if(dbSensor.getCrawler().getId() == MetaCrawlerEnum.WMI_CRAWLER.getValue()) { + connection.setPort("135"); + connection.setPortType("tcp"); + connection.setSsl(false); + + target.setConnection(connection); + + Map auth = new HashMap<>(); + auth.put("id", optionMap.get("ID")); + auth.put("pw", optionMap.get("PassWord")); + + target.setAuth(auth); + + } + + return target; + } } diff --git a/src/main/java/com/loafle/overflow/module/generator/service/InfraServiceGenerator.java b/src/main/java/com/loafle/overflow/module/generator/service/InfraServiceGenerator.java index 636bf53..0351c16 100644 --- a/src/main/java/com/loafle/overflow/module/generator/service/InfraServiceGenerator.java +++ b/src/main/java/com/loafle/overflow/module/generator/service/InfraServiceGenerator.java @@ -1,20 +1,23 @@ package com.loafle.overflow.module.generator.service; import com.loafle.overflow.commons.utils.StringConvertor; -import com.loafle.overflow.crawler.config.Config; -import com.loafle.overflow.crawler.config.Connection; -import com.loafle.overflow.crawler.config.Crawler; -import com.loafle.overflow.crawler.config.Schedule; +import com.loafle.overflow.crawler.config.*; +import com.loafle.overflow.module.auth.model.AuthCrawler; +import com.loafle.overflow.module.auth.service.AuthCrawlerService; import com.loafle.overflow.module.infra.model.Infra; +import com.loafle.overflow.module.infra.model.InfraService; +import com.loafle.overflow.module.meta.model.MetaCrawler; import com.loafle.overflow.module.meta.model.MetaSensorItemKey; import com.loafle.overflow.module.meta.type.MetaCrawlerEnum; import com.loafle.overflow.module.sensor.model.Sensor; import com.loafle.overflow.module.sensor.model.SensorItem; import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -30,24 +33,21 @@ public class InfraServiceGenerator { @Autowired private InfraServiceMysqlGenerator infraServiceMysqlGenerator; + @Autowired + private AuthCrawlerService authCrawlerService; + public String process(Sensor dbSensor, List sensorItems, Infra infra) throws IOException { com.loafle.overflow.module.infra.model.InfraService infraService = (com.loafle.overflow.module.infra.model.InfraService)infra; Config config = new Config(); config.setId(String.valueOf(dbSensor.getId())); - com.loafle.overflow.crawler.config.Target target = new com.loafle.overflow.crawler.config.Target(); - Connection connection = new Connection(); - connection.setIp(StringConvertor.intToIp(infraService.getHost().getIp())); - connection.setPort(String.valueOf(infraService.getPort())); - connection.setPortType(infraService.getPortType()); - connection.setSsl(infraService.isTlsType()); + Target target = this.createTarget(infraService, dbSensor); - target.setConnection(connection); + if(target == null) { + return null; + } - Map auth = this.generateUtil.getAuth(dbSensor.getCrawler(), infraService); - - target.setAuth(auth); config.setTarget(target); @@ -68,4 +68,49 @@ public class InfraServiceGenerator { ObjectMapper objectMapper = new ObjectMapper(); return objectMapper.writeValueAsString(config); } + + private Target createTarget(InfraService infraService, Sensor sensor) throws IOException { + + AuthCrawler authCrawler = this.authCrawlerService.readAuth(sensor.getCrawler(), sensor.getTarget()); + + if(authCrawler == null) { + return null; + } + + Target target = new com.loafle.overflow.crawler.config.Target(); + Connection connection = new Connection(); + connection.setIp(StringConvertor.intToIp(infraService.getHost().getIp())); + connection.setPort(String.valueOf(infraService.getPort())); + connection.setPortType(infraService.getPortType()); + connection.setSsl(infraService.isTlsType()); + + target.setConnection(connection); + + HashMap optionMap; + optionMap = new ObjectMapper().readValue(authCrawler.getAuthJson(), new TypeReference>() {}); + + Map auth = new HashMap<>(); + + if(sensor.getCrawler().getId() == MetaCrawlerEnum.MYSQL_CRAWLER.getValue()) { + auth.put("url", "jdbc:mysql://"+ StringConvertor.intToIp(infraService.getHost().getIp())+":"+String.valueOf(infraService.getPort())); + auth.put("id", optionMap.get("ID")); // FIXME: Auth Info + auth.put("pw", optionMap.get("PassWord")); // FIXME: Auth Info + } + + target.setAuth(auth); + + return target; + } + private Map getAuth(MetaCrawler metaCrawler, com.loafle.overflow.module.infra.model.InfraService infraService) { + + Map auth = new HashMap<>(); + + if(metaCrawler.getId() == MetaCrawlerEnum.MYSQL_CRAWLER.getValue()) { + auth.put("url", "jdbc:mysql://"+ StringConvertor.intToIp(infraService.getHost().getIp())+":"+String.valueOf(infraService.getPort())); + auth.put("id", "docker"); // FIXME: Auth Info + auth.put("pw", "qwer5795QWER"); // FIXME: Auth Info + } + + return auth; + } } diff --git a/src/main/resources/local/init.sql b/src/main/resources/local/init.sql index 750884f..e79b7c5 100644 --- a/src/main/resources/local/init.sql +++ b/src/main/resources/local/init.sql @@ -1465,6 +1465,6 @@ INSERT INTO public.notification (id,confirm_date,create_date,message,title,membe INSERT INTO public.auth_crawler (auth_json,create_date,crawler_id,target_id) VALUES ( '{"Community":"public"}','2017-09-08 18:31:05.126',20,5); INSERT INTO public.auth_crawler (auth_json,create_date,crawler_id,target_id) VALUES ( -'{"ID":"administrator","PassWord":"dbseogns18"}','2017-09-08 18:31:18.581',23,5); +'{"ID":"administrator","PassWord":"dbseogns18"}','2017-09-08 18:31:18.581',23,3); INSERT INTO public.auth_crawler (auth_json,create_date,crawler_id,target_id) VALUES ( -'{"DB Name":"test","ID":"docker","PassWord":"qwer5795QWER"}','2017-09-08 18:31:37.206',11,5); \ No newline at end of file +'{"DB Name":"test","ID":"docker","PassWord":"qwer5795QWER"}','2017-09-08 18:31:37.206',11,4); \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/module/auth/service/AuthCrawlerServiceTest.java b/src/test/java/com/loafle/overflow/module/auth/service/AuthCrawlerServiceTest.java index bd3011a..d5e0701 100644 --- a/src/test/java/com/loafle/overflow/module/auth/service/AuthCrawlerServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/auth/service/AuthCrawlerServiceTest.java @@ -46,4 +46,20 @@ public class AuthCrawlerServiceTest { } + @Test + public void findAuth() { + + MetaCrawler metaCrawler = new MetaCrawler(); + metaCrawler.setId((short)23); + + Target target = new Target(); + target.setId(5); + + + AuthCrawler authCrawler = this.authCrawlerService.readAuth(metaCrawler, target); + + Assert.assertNotEquals(authCrawler, null); + + } + } \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/module/sensor/service/SensorServiceTest.java b/src/test/java/com/loafle/overflow/module/sensor/service/SensorServiceTest.java index f504da9..6eb4f92 100644 --- a/src/test/java/com/loafle/overflow/module/sensor/service/SensorServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/sensor/service/SensorServiceTest.java @@ -65,7 +65,7 @@ public class SensorServiceTest { public void generateSensorConfig() throws IOException { Sensor sensor = new Sensor(); - sensor.setId(4); + sensor.setId(3); String result = this.sensorService.generateSensorConfig(sensor); System.out.println(result);