From 142d10c3741c71b24194ff9b390e2e1ad33bbe9c Mon Sep 17 00:00:00 2001 From: snoop Date: Wed, 23 Aug 2017 15:44:24 +0900 Subject: [PATCH] fixed target create --- .../module/infra/dao/InfraHostDAO.java | 1 + .../module/infra/dao/InfraOSPortDAO.java | 5 + .../module/infra/dao/InfraServiceDAO.java | 5 + .../infra/service/InfraHostService.java | 4 + .../infra/service/InfraOSPortService.java | 4 + .../infra/service/InfraServiceService.java | 5 + .../service/TargetDiscoveryService.java | 200 +++++++++++------- .../infra/service/InfraHostServiceTest.java | 9 + .../infra/service/InfraOSPortServiceTest.java | 10 + .../service/InfraServiceServiceTest.java | 9 + 10 files changed, 173 insertions(+), 79 deletions(-) diff --git a/src/main/java/com/loafle/overflow/module/infra/dao/InfraHostDAO.java b/src/main/java/com/loafle/overflow/module/infra/dao/InfraHostDAO.java index 0c1eb90..5894a8b 100644 --- a/src/main/java/com/loafle/overflow/module/infra/dao/InfraHostDAO.java +++ b/src/main/java/com/loafle/overflow/module/infra/dao/InfraHostDAO.java @@ -9,4 +9,5 @@ import org.springframework.stereotype.Repository; */ @Repository public interface InfraHostDAO extends JpaRepository { + InfraHost findByIp(long ip); } \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/module/infra/dao/InfraOSPortDAO.java b/src/main/java/com/loafle/overflow/module/infra/dao/InfraOSPortDAO.java index 80d4be5..3c2f900 100644 --- a/src/main/java/com/loafle/overflow/module/infra/dao/InfraOSPortDAO.java +++ b/src/main/java/com/loafle/overflow/module/infra/dao/InfraOSPortDAO.java @@ -2,6 +2,8 @@ package com.loafle.overflow.module.infra.dao; import com.loafle.overflow.module.infra.model.InfraOSPort; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; /** @@ -9,4 +11,7 @@ import org.springframework.stereotype.Repository; */ @Repository public interface InfraOSPortDAO extends JpaRepository { + + @Query("SELECT p from com.loafle.overflow.module.infra.model.InfraOSPort p WHERE p.os.id = (:osId) AND p.port = (:portNumber) AND p.portType = (:portType)") + InfraOSPort findByPort(@Param("osId") long osId,@Param("portNumber") int portNumber,@Param("portType") String portType); } \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/module/infra/dao/InfraServiceDAO.java b/src/main/java/com/loafle/overflow/module/infra/dao/InfraServiceDAO.java index 1c89a97..91f3a90 100644 --- a/src/main/java/com/loafle/overflow/module/infra/dao/InfraServiceDAO.java +++ b/src/main/java/com/loafle/overflow/module/infra/dao/InfraServiceDAO.java @@ -2,6 +2,8 @@ package com.loafle.overflow.module.infra.dao; import com.loafle.overflow.module.infra.model.InfraService; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; /** @@ -9,4 +11,7 @@ import org.springframework.stereotype.Repository; */ @Repository public interface InfraServiceDAO extends JpaRepository { + + @Query("SELECT ins from com.loafle.overflow.module.infra.model.InfraService ins WHERE ins.host.id = (:hostId) AND ins.port = (:portNumber) AND ins.portType = (:portType)") + InfraService findByService(@Param("hostId") long hostId,@Param("portNumber") int portNumber,@Param("portType") String portType); } \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/module/infra/service/InfraHostService.java b/src/main/java/com/loafle/overflow/module/infra/service/InfraHostService.java index 02a134f..35b8b7d 100644 --- a/src/main/java/com/loafle/overflow/module/infra/service/InfraHostService.java +++ b/src/main/java/com/loafle/overflow/module/infra/service/InfraHostService.java @@ -22,4 +22,8 @@ public class InfraHostService { public InfraHost read(long id) { return this.infraHostDAO.findOne(id); } + + public InfraHost readByIp(long ip) { + return this.infraHostDAO.findByIp(ip); + } } diff --git a/src/main/java/com/loafle/overflow/module/infra/service/InfraOSPortService.java b/src/main/java/com/loafle/overflow/module/infra/service/InfraOSPortService.java index 8c99823..4ef895d 100644 --- a/src/main/java/com/loafle/overflow/module/infra/service/InfraOSPortService.java +++ b/src/main/java/com/loafle/overflow/module/infra/service/InfraOSPortService.java @@ -22,4 +22,8 @@ public class InfraOSPortService { public InfraOSPort read(long id) { return this.infraOSPortDAO.findOne(id); } + + public InfraOSPort readByPort(long osId, int portNumber, String portType) { + return this.infraOSPortDAO.findByPort(osId, portNumber, portType); + } } diff --git a/src/main/java/com/loafle/overflow/module/infra/service/InfraServiceService.java b/src/main/java/com/loafle/overflow/module/infra/service/InfraServiceService.java index 78a5a04..64c9326 100644 --- a/src/main/java/com/loafle/overflow/module/infra/service/InfraServiceService.java +++ b/src/main/java/com/loafle/overflow/module/infra/service/InfraServiceService.java @@ -1,6 +1,7 @@ package com.loafle.overflow.module.infra.service; import com.loafle.overflow.module.infra.dao.InfraServiceDAO; +import com.loafle.overflow.module.infra.model.InfraService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -19,4 +20,8 @@ public class InfraServiceService { public com.loafle.overflow.module.infra.model.InfraService read(long id) { return this.infraServiceDAO.findOne(id); } + + public InfraService readByService(long hostId, int portNumber, String portType) { + return this.infraServiceDAO.findByService(hostId, portNumber, portType); + } } diff --git a/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java b/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java index 12f3b8b..1e395da 100644 --- a/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java +++ b/src/main/java/com/loafle/overflow/module/target/service/TargetDiscoveryService.java @@ -3,14 +3,12 @@ package com.loafle.overflow.module.target.service; import com.loafle.overflow.module.discovery.model.Host; import com.loafle.overflow.module.discovery.model.Port; import com.loafle.overflow.module.discovery.type.PortType; -import com.loafle.overflow.module.infra.dao.*; import com.loafle.overflow.module.infra.model.*; import com.loafle.overflow.module.infra.model.InfraService; import com.loafle.overflow.module.infra.service.*; import com.loafle.overflow.module.meta.model.MetaInfraType; import com.loafle.overflow.module.meta.model.MetaInfraVendor; import com.loafle.overflow.module.probe.model.Probe; -import com.loafle.overflow.module.target.dao.TargetDAO; import com.loafle.overflow.module.target.model.Target; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -48,23 +46,125 @@ public class TargetDiscoveryService { @Transactional public void saveAllTarget(List hosts, Probe probe) { - MetaInfraType typeMachine = new MetaInfraType(); - typeMachine.setId(1); // 1 = Machine; + InfraHost infraHost = null; - MetaInfraType typeOS = new MetaInfraType(); - typeOS.setId(3); // 3 = Os + for(Host host : hosts) { - MetaInfraType typeHost = new MetaInfraType(); - typeHost.setId(2); // 2 = Host + infraHost = this.createAndReadHost(host, probe); - MetaInfraType typePort = new MetaInfraType(); - typePort.setId(6); + this.createPort(infraHost, host, probe); + + } + + } + + private void createService(InfraHost infraHost, Port port, Probe probe) { + + if(port.getServices() == null) { + return; + } MetaInfraType typeService = new MetaInfraType(); typeService.setId(7); + String portType = "UDP"; - for(Host host : hosts) { + if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { + portType = "TCP"; + } + + for(com.loafle.overflow.module.discovery.model.Service service : port.getServices()) { + + InfraService dbInfraService = this.infraServiceService.readByService(infraHost.getId(), port.getPortNumber(), portType); + + InfraService infraService = new InfraService(); + infraService.setHost(infraHost); + infraService.setPort(port.getPortNumber()); + infraService.setPortType(portType); + infraService.setInfraType(typeService); + infraService.setProbe(probe); + + if (port.getPortType() == PortType.TLS) { + infraService.setTlsType(true); + } + infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); + + if(service.isTarget()) { + Target targetService = new Target(); + targetService.setDisplayName(service.getServiceName() + "-Service"); + this.targetService.regist(targetService); + infraService.setTarget(targetService); + } + + this.infraServiceService.regist(infraService); + + } + + } + + private void createPort(InfraHost infraHost, Host host, Probe probe) { + + if(host.getPorts() == null) { + return; + } + + String portType = "UDP"; + + MetaInfraType typePort = new MetaInfraType(); + typePort.setId(6); + + InfraOS infraOS = infraHost.getOs(); + + for(Port port : host.getPorts()) { + + if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { + portType = "TCP"; + } + + InfraOSPort dbInfraOSPort = this.infraOSPortService.readByPort(infraOS.getId(), port.getPortNumber(), portType); + if(dbInfraOSPort == null) { + InfraOSPort infraOSPort = new InfraOSPort(); + infraOSPort.setOs(infraOS); + infraOSPort.setPort(port.getPortNumber()); + infraOSPort.setPortType(portType); + infraOSPort.setProbe(probe); + infraOSPort.setInfraType(typePort); + + if (port.getPortType() == PortType.TLS) { + infraOSPort.setTlsType(true); + } + infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber())); + this.infraOSPortService.regist(infraOSPort); + } + + this.createService(infraHost, port, probe); + } + } + + private InfraHost createAndReadHost(Host host, Probe probe) { + + InfraHost infraHost = this.infraHostService.readByIp(host.getIp()); + if(infraHost != null) { + + if(host.isTarget() && infraHost.getTarget() == null) { + Target target = new Target(); + target.setDisplayName(String.valueOf(host.getIp()) + "-Host"); + + this.targetService.regist(target); + infraHost.setTarget(target); + this.infraHostService.regist(infraHost); + } + + return infraHost; + } else { + MetaInfraType typeMachine = new MetaInfraType(); + typeMachine.setId(1); // 1 = Machine; + + MetaInfraType typeOS = new MetaInfraType(); + typeOS.setId(3); // 3 = Os + + MetaInfraType typeHost = new MetaInfraType(); + typeHost.setId(2); // 2 = Host InfraMachine infraMachine = new InfraMachine(); infraMachine.setProbe(probe); @@ -78,86 +178,28 @@ public class TargetDiscoveryService { infraOS.setProbe(probe); this.infraOSService.regist(infraOS); - InfraHost infraHost = new InfraHost(); - infraHost.setIp(host.getIp()); - infraHost.setMac(host.getMac()); - infraHost.setOs(infraOS); - infraHost.setInfraType(typeHost); - infraHost.setProbe(probe); + InfraHost newInfraHost = new InfraHost(); + newInfraHost.setIp(host.getIp()); + newInfraHost.setMac(host.getMac()); + newInfraHost.setOs(infraOS); + newInfraHost.setInfraType(typeHost); + newInfraHost.setProbe(probe); if(host.isTarget()) { Target target = new Target(); target.setDisplayName(String.valueOf(host.getIp()) + "-Host"); this.targetService.regist(target); - infraHost.setTarget(target); + newInfraHost.setTarget(target); } - this.infraHostService.regist(infraHost); - - if(host.getPorts() == null) { - continue; - } - - for(Port port : host.getPorts()) { - - InfraOSPort infraOSPort = new InfraOSPort(); - infraOSPort.setOs(infraOS); - infraOSPort.setPort(port.getPortNumber()); - infraOSPort.setPortType("UDP"); - infraOSPort.setProbe(probe); - infraOSPort.setInfraType(typePort); - if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { - infraOSPort.setPortType("TCP"); - } - if (port.getPortType() == PortType.TLS) { - infraOSPort.setTlsType(true); - } - infraOSPort.setVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber())); - this.infraOSPortService.regist(infraOSPort); - - if(port.getServices() == null) { - continue; - } - - for(com.loafle.overflow.module.discovery.model.Service service : port.getServices()) { - - InfraService infraService = new InfraService(); - infraService.setHost(infraHost); - infraService.setPort(port.getPortNumber()); - infraService.setPortType("UDP"); - infraService.setInfraType(typeService); - infraService.setProbe(probe); - if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { - infraService.setPortType("TCP"); - } - if (port.getPortType() == PortType.TLS) { - infraService.setTlsType(true); - } - infraService.setVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); - - if(service.isTarget()) { - Target targetService = new Target(); - targetService.setDisplayName(service.getServiceName() + "-Service"); - this.targetService.regist(targetService); - infraService.setTarget(targetService); - } - - this.infraServiceService.regist(infraService); - - } - - } - - + this.infraHostService.regist(newInfraHost); + infraHost = newInfraHost; } - } - - private InfraHost createAndReadHost(Host host, Probe probe) { - return null; + return infraHost; } diff --git a/src/test/java/com/loafle/overflow/module/infra/service/InfraHostServiceTest.java b/src/test/java/com/loafle/overflow/module/infra/service/InfraHostServiceTest.java index 6bb746b..83e8c2c 100644 --- a/src/test/java/com/loafle/overflow/module/infra/service/InfraHostServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/infra/service/InfraHostServiceTest.java @@ -60,4 +60,13 @@ public class InfraHostServiceTest { } + @Test + public void readByIp() { + + InfraHost infraHost = this.infraHostService.readByIp(3232235980L); + + Assert.assertNotEquals(infraHost, null); + + } + } \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/module/infra/service/InfraOSPortServiceTest.java b/src/test/java/com/loafle/overflow/module/infra/service/InfraOSPortServiceTest.java index e841058..1902dd0 100644 --- a/src/test/java/com/loafle/overflow/module/infra/service/InfraOSPortServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/infra/service/InfraOSPortServiceTest.java @@ -4,6 +4,7 @@ import com.loafle.overflow.module.infra.model.InfraOS; import com.loafle.overflow.module.infra.model.InfraOSPort; import com.loafle.overflow.module.meta.model.MetaInfraType; import com.loafle.overflow.spring.AppConfigTest; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -45,4 +46,13 @@ public class InfraOSPortServiceTest { public void read() throws Exception { } + @Test + public void readByPort() { + + InfraOSPort infraOSPort = this.infraOSPortService.readByPort(1, 22, "TCP"); + + Assert.assertNotEquals(infraOSPort, null); + + } + } \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/module/infra/service/InfraServiceServiceTest.java b/src/test/java/com/loafle/overflow/module/infra/service/InfraServiceServiceTest.java index 8ee7311..9bccd39 100644 --- a/src/test/java/com/loafle/overflow/module/infra/service/InfraServiceServiceTest.java +++ b/src/test/java/com/loafle/overflow/module/infra/service/InfraServiceServiceTest.java @@ -56,4 +56,13 @@ public class InfraServiceServiceTest { public void read() throws Exception { } + + @Test + public void readByService() { + + InfraService infraService = this.infraServiceService.readByService(3, 80, "TCP"); + + Assert.assertNotEquals(infraService, null); + + } } \ No newline at end of file