fixed target create

This commit is contained in:
snoop 2017-08-23 15:44:24 +09:00
parent bc14645d0b
commit 142d10c374
10 changed files with 173 additions and 79 deletions

View File

@ -9,4 +9,5 @@ import org.springframework.stereotype.Repository;
*/
@Repository
public interface InfraHostDAO extends JpaRepository<InfraHost, Long> {
InfraHost findByIp(long ip);
}

View File

@ -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<InfraOSPort, Long> {
@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);
}

View File

@ -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<InfraService, Long> {
@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);
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<Host> 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;
}

View File

@ -60,4 +60,13 @@ public class InfraHostServiceTest {
}
@Test
public void readByIp() {
InfraHost infraHost = this.infraHostService.readByIp(3232235980L);
Assert.assertNotEquals(infraHost, null);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}