syntax error fix

This commit is contained in:
insanity 2018-06-05 22:20:07 +09:00
parent 07ef365ad3
commit b4eed25b5f
8 changed files with 274 additions and 200 deletions

View File

@ -49,7 +49,7 @@
<dependency> <dependency>
<groupId>com.loafle.overflow</groupId> <groupId>com.loafle.overflow</groupId>
<artifactId>commons-java</artifactId> <artifactId>commons-java</artifactId>
<version>1.0.6-SNAPSHOT</version> <version>1.0.8-SNAPSHOT</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -9,6 +9,6 @@ import org.springframework.stereotype.Repository;
*/ */
@Repository @Repository
public interface InfraHostDAO extends JpaRepository<InfraHost, Long> { public interface InfraHostDAO extends JpaRepository<InfraHost, Long> {
InfraHost findByIpv4(String ipv4); InfraHost findByProbeIdAndIpv4(Long probeId, String ipv4);
} }

View File

@ -1,5 +1,6 @@
package com.loafle.overflow.central.module.infra.service; package com.loafle.overflow.central.module.infra.service;
import com.loafle.overflow.central.module.infra.dao.InfraHostDAO; import com.loafle.overflow.central.module.infra.dao.InfraHostDAO;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.model.infra.InfraHost; import com.loafle.overflow.model.infra.InfraHost;
@ -24,8 +25,8 @@ public class CentralInfraHostService implements InfraHostService {
return this.infraHostDAO.findOne(id); return this.infraHostDAO.findOne(id);
} }
public InfraHost readByIp(String ip) throws OverflowException { public InfraHost readByProbeIdAndIpv4(Long probeId, String ip) throws OverflowException {
return this.infraHostDAO.findByIpv4(ip); return this.infraHostDAO.findByProbeIdAndIpv4(probeId, ip);
} }
} }

View File

@ -39,7 +39,6 @@ public class CentralInfraService implements InfraService {
public Infra read(Long id) throws OverflowException { public Infra read(Long id) throws OverflowException {
Infra infra = this.infraDAO.findOne(id); Infra infra = this.infraDAO.findOne(id);
infra.getTarget().setSensors(this.sensorDAO.findAllByTargetId(infra.getTarget().getId()));
return infra; return infra;
} }
@ -55,9 +54,6 @@ public class CentralInfraService implements InfraService {
} }
Page<Infra> infraList = this.infraDAO.findAllByProbeInAndTargetNotNull(probeList, PageUtil.getPageRequest(pageParams)); Page<Infra> infraList = this.infraDAO.findAllByProbeInAndTargetNotNull(probeList, PageUtil.getPageRequest(pageParams));
for (Infra infra : infraList) {
infra.getTarget().setSensors(this.sensorDAO.findAllByTargetId(infra.getTarget().getId()));
}
return infraList; return infraList;
} }

View File

@ -73,13 +73,12 @@ public class CentralSensorService implements SensorService {
} }
public Page<Sensor> readAllByInfraID(Long infraID, PageParams pageParams) throws OverflowException { public Page<Sensor> readAllByInfraID(Long infraID, PageParams pageParams) throws OverflowException {
Infra dbInfra = this.infraService.read(infraID); // Infra dbInfra = this.infraService.read(infraID);
// if (dbInfra == null) {
if (dbInfra == null || dbInfra.getTarget() == null) { // throw new OverflowException("", new Throwable());
throw new OverflowException("", new Throwable()); // }
} // return this.sensorDAO.findAllByTargetId(dbInfra.getTarget().getId(), PageUtil.getPageRequest(pageParams));
return null;
return this.sensorDAO.findAllByTargetId(dbInfra.getTarget().getId(), PageUtil.getPageRequest(pageParams));
} }
public Page<Sensor> readAllByTargetID(Long targetID, PageParams pageParams) throws OverflowException { public Page<Sensor> readAllByTargetID(Long targetID, PageParams pageParams) throws OverflowException {

View File

@ -9,4 +9,5 @@ import org.springframework.stereotype.Repository;
*/ */
@Repository @Repository
public interface TargetDAO extends JpaRepository<Target, Long> { public interface TargetDAO extends JpaRepository<Target, Long> {
Target findByInfraId(Long infraId);
} }

View File

@ -1,226 +1,226 @@
package com.loafle.overflow.central.module.target.service; // package com.loafle.overflow.central.module.target.service;
import com.loafle.overflow.central.module.infra.service.*; // import com.loafle.overflow.central.module.infra.service.*;
import com.loafle.overflow.core.exception.OverflowException; // import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.core.type.PortType; // import com.loafle.overflow.core.type.PortType;
import com.loafle.overflow.model.discovery.Host; // import com.loafle.overflow.model.discovery.Host;
import com.loafle.overflow.model.discovery.Port; // import com.loafle.overflow.model.discovery.Port;
import com.loafle.overflow.model.infra.*; // import com.loafle.overflow.model.infra.*;
import com.loafle.overflow.model.meta.MetaInfraType; // import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.model.meta.MetaInfraVendor; // import com.loafle.overflow.model.meta.MetaInfraVendor;
import com.loafle.overflow.model.probe.Probe; // import com.loafle.overflow.model.probe.Probe;
import com.loafle.overflow.model.target.Target; // import com.loafle.overflow.model.target.Target;
import com.loafle.overflow.service.central.target.*; // import com.loafle.overflow.service.central.target.*;
import org.springframework.beans.factory.annotation.Autowired; // import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; // import org.springframework.stereotype.Service;
import javax.transaction.Transactional; // import javax.transaction.Transactional;
import java.util.List; // import java.util.List;
/** // /**
* Created by snoop on 17. 6. 28. // * Created by snoop on 17. 6. 28.
*/ // */
@Service("TargetDiscoveryService") // @Service("TargetDiscoveryService")
public class CentralTargetDiscoveryService implements TargetDiscoveryService { // public class CentralTargetDiscoveryService implements TargetDiscoveryService {
@Autowired // @Autowired
private TargetService targetService; // private TargetService targetService;
@Autowired // @Autowired
private CentralInfraMachineService infraMachineService; // private CentralInfraMachineService infraMachineService;
@Autowired // @Autowired
private CentralInfraOSService infraOSService; // private CentralInfraOSService infraOSService;
@Autowired // @Autowired
private CentralInfraHostService infraHostService; // private CentralInfraHostService infraHostService;
// @Autowired // // @Autowired
// private CentralInfraService infraService; // // private CentralInfraService infraService;
@Autowired // @Autowired
private CentralInfraOSPortService infraOSPortService; // private CentralInfraOSPortService infraOSPortService;
@Autowired // @Autowired
private CentralInfraServiceService infraServiceService; // private CentralInfraServiceService infraServiceService;
@Transactional // @Transactional
public boolean saveAllTarget(List<Host> hosts, Probe probe) throws OverflowException { // public boolean saveAllTarget(List<Host> hosts, Probe probe) throws OverflowException {
InfraHost infraHost = null; // InfraHost infraHost = null;
for (Host host : hosts) { // for (Host host : hosts) {
infraHost = this.createAndReadHost(host, probe); // infraHost = this.createAndReadHost(host, probe);
this.createPort(infraHost, host, probe); // this.createPort(infraHost, host, probe);
} // }
return true; // return true;
} // }
private void createService(InfraHost infraHost, Port port, Probe probe) throws OverflowException { // private void createService(InfraHost infraHost, Port port, Probe probe) throws OverflowException {
MetaInfraType typeService = new MetaInfraType(7); // MetaInfraType typeService = new MetaInfraType(7);
String portType = "UDP"; // String portType = "UDP";
if (port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP || port.getPortType() == null) { // if (port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP || port.getPortType() == null) {
portType = "TCP"; // portType = "TCP";
} // }
if (port.getServiceList() == null) { // if (port.getServiceList() == null) {
return; // return;
} // }
// for(String key : port.getServices().keySet()) { // // for(String key : port.getServices().keySet()) {
for (com.loafle.overflow.model.discovery.Service service : port.getServiceList()) { // for (com.loafle.overflow.model.discovery.Service service : port.getServiceList()) {
// com.loafle.overflow.module.discovery.model.Service service = // // com.loafle.overflow.module.discovery.model.Service service =
// port.getServices().get(key); // // port.getServices().get(key);
InfraService dbInfraService = this.infraServiceService // InfraService dbInfraService = this.infraServiceService
.readByInfraHostIDAndPortAndPortType(infraHost.getId(), port.getPortNumber(), portType); // .readByInfraHostIDAndPortAndPortType(infraHost.getId(), port.getPortNumber(), portType);
if (dbInfraService != null) { // if (dbInfraService != null) {
if (service.isTarget() && dbInfraService.getTarget() == null) { // if (service.isTarget() && dbInfraService.getTarget() == null) {
Target targetService = new Target(); // Target targetService = new Target();
targetService.setDisplayName(service.getServiceName()); // targetService.setDisplayName(service.getServiceName());
this.targetService.regist(targetService, probe); // this.targetService.regist(targetService, probe);
dbInfraService.setTarget(targetService); // dbInfraService.setTarget(targetService);
this.infraServiceService.regist(dbInfraService); // this.infraServiceService.regist(dbInfraService);
} // }
continue; // continue;
} // }
InfraService infraService = new InfraService(); // InfraService infraService = new InfraService();
infraService.setInfraHost(infraHost); // infraService.setInfraHost(infraHost);
infraService.setPort(port.getPortNumber()); // infraService.setPort(port.getPortNumber());
infraService.setPortType(portType); // infraService.setPortType(portType);
infraService.setMetaInfraType(typeService); // infraService.setMetaInfraType(typeService);
infraService.setProbe(probe); // infraService.setProbe(probe);
if (port.getPortType() == PortType.TLS) { // if (port.getPortType() == PortType.TLS) {
infraService.setTlsType(true); // infraService.setTlsType(true);
} // }
infraService.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName())); // infraService.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName()));
if (service.isTarget()) { // if (service.isTarget()) {
Target targetService = new Target(); // Target targetService = new Target();
targetService.setDisplayName(service.getServiceName()); // targetService.setDisplayName(service.getServiceName());
this.targetService.regist(targetService, probe); // this.targetService.regist(targetService, probe);
infraService.setTarget(targetService); // infraService.setTarget(targetService);
} // }
this.infraServiceService.regist(infraService); // this.infraServiceService.regist(infraService);
} // }
} // }
private void createPort(InfraHost infraHost, Host host, Probe probe) throws OverflowException { // private void createPort(InfraHost infraHost, Host host, Probe probe) throws OverflowException {
// if(host.getPorts() == null) { // // if(host.getPorts() == null) {
// return; // // return;
// } // // }
String portType = "UDP"; // String portType = "UDP";
MetaInfraType typePort = new MetaInfraType(6); // MetaInfraType typePort = new MetaInfraType(6);
InfraOS infraOS = infraHost.getInfraOS(); // InfraOS infraOS = infraHost.getInfraOS();
// for( String key: host.getPorts().keySet()) { // // for( String key: host.getPorts().keySet()) {
if (host.getPortList() == null) { // if (host.getPortList() == null) {
return; // return;
} // }
for (Port port : host.getPortList()) { // for (Port port : host.getPortList()) {
// Port port = host.getPorts().get(key); // // Port port = host.getPorts().get(key);
if (port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { // if (port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) {
portType = "TCP"; // portType = "TCP";
} // }
InfraOSPort dbInfraOSPort = this.infraOSPortService.readByInfraOSIDAndPortAndPortType(infraOS.getId(), port.getPortNumber(), // InfraOSPort dbInfraOSPort = this.infraOSPortService.readByInfraOSIDAndPortAndPortType(infraOS.getId(), port.getPortNumber(),
portType); // portType);
if (dbInfraOSPort == null) { // if (dbInfraOSPort == null) {
InfraOSPort infraOSPort = new InfraOSPort(); // InfraOSPort infraOSPort = new InfraOSPort();
infraOSPort.setInfraOS(infraOS); // infraOSPort.setInfraOS(infraOS);
infraOSPort.setPort(port.getPortNumber()); // infraOSPort.setPort(port.getPortNumber());
infraOSPort.setPortType(portType); // infraOSPort.setPortType(portType);
infraOSPort.setProbe(probe); // infraOSPort.setProbe(probe);
infraOSPort.setMetaInfraType(typePort); // infraOSPort.setMetaInfraType(typePort);
if (port.getPortType() == PortType.TLS) { // if (port.getPortType() == PortType.TLS) {
infraOSPort.setTlsType(true); // infraOSPort.setTlsType(true);
} // }
infraOSPort.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber())); // infraOSPort.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByPort(port.getPortNumber()));
this.infraOSPortService.regist(infraOSPort); // this.infraOSPortService.regist(infraOSPort);
} // }
this.createService(infraHost, port, probe); // this.createService(infraHost, port, probe);
} // }
} // }
private InfraHost createAndReadHost(Host host, Probe probe) throws OverflowException { // private InfraHost createAndReadHost(Host host, Probe probe) throws OverflowException {
InfraHost infraHost = this.infraHostService.readByIp(host.getIpv4()); // InfraHost infraHost = this.infraHostService.readByIp(host.getIpv4());
if (infraHost != null) { // if (infraHost != null) {
if (host.isTarget() && infraHost.getTarget() == null) { // if (host.isTarget() && infraHost.getTarget() == null) {
Target target = new Target(); // Target target = new Target();
target.setDisplayName(host.getIpv4() + "-Host"); // target.setDisplayName(host.getIpv4() + "-Host");
this.targetService.regist(target, probe); // this.targetService.regist(target, probe);
infraHost.setTarget(target); // infraHost.setTarget(target);
this.infraHostService.regist(infraHost); // this.infraHostService.regist(infraHost);
} // }
return infraHost; // return infraHost;
} else { // } else {
MetaInfraType typeMachine = new MetaInfraType(1); // 1 = Machine; // MetaInfraType typeMachine = new MetaInfraType(1); // 1 = Machine;
MetaInfraType typeOS = new MetaInfraType(3);// 3 = Os // MetaInfraType typeOS = new MetaInfraType(3);// 3 = Os
MetaInfraType typeHost = new MetaInfraType(2); // 2 = Host // MetaInfraType typeHost = new MetaInfraType(2); // 2 = Host
InfraMachine infraMachine = new InfraMachine(); // InfraMachine infraMachine = new InfraMachine();
infraMachine.setProbe(probe); // infraMachine.setProbe(probe);
infraMachine.setMetaInfraType(typeMachine); // infraMachine.setMetaInfraType(typeMachine);
infraMachine.setMeta(host.getIpv4() + "-MACHINE"); // infraMachine.setMeta(host.getIpv4() + "-MACHINE");
this.infraMachineService.regist(infraMachine); // this.infraMachineService.regist(infraMachine);
InfraOS infraOS = new InfraOS(); // InfraOS infraOS = new InfraOS();
infraOS.setInfraMachine(infraMachine); // infraOS.setInfraMachine(infraMachine);
infraOS.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByOS(host.getOs())); // infraOS.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByOS(host.getOs()));
infraOS.setMetaInfraType(typeOS); // infraOS.setMetaInfraType(typeOS);
infraOS.setProbe(probe); // infraOS.setProbe(probe);
infraOS.setMeta(host.getIpv4() + "-OS"); // infraOS.setMeta(host.getIpv4() + "-OS");
this.infraOSService.regist(infraOS); // this.infraOSService.regist(infraOS);
InfraHost newInfraHost = new InfraHost(); // InfraHost newInfraHost = new InfraHost();
newInfraHost.setIpv4(host.getIpv4()); // newInfraHost.setIpv4(host.getIpv4());
newInfraHost.setMac(host.getMac()); // newInfraHost.setMac(host.getMac());
newInfraHost.setInfraOS(infraOS); // newInfraHost.setInfraOS(infraOS);
newInfraHost.setMetaInfraType(typeHost); // newInfraHost.setMetaInfraType(typeHost);
newInfraHost.setProbe(probe); // newInfraHost.setProbe(probe);
if (host.isTarget()) { // if (host.isTarget()) {
Target target = new Target(); // Target target = new Target();
target.setDisplayName(host.getIpv4() + "-Host"); // target.setDisplayName(host.getIpv4() + "-Host");
this.targetService.regist(target, probe); // this.targetService.regist(target, probe);
newInfraHost.setTarget(target); // newInfraHost.setTarget(target);
} // }
this.infraHostService.regist(newInfraHost); // this.infraHostService.regist(newInfraHost);
infraHost = newInfraHost; // infraHost = newInfraHost;
} // }
return infraHost; // return infraHost;
} // }
} // }

View File

@ -1,24 +1,31 @@
package com.loafle.overflow.central.module.target.service; package com.loafle.overflow.central.module.target.service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.loafle.overflow.central.module.infra.service.CentralInfraHostService; import com.loafle.overflow.central.module.infra.service.CentralInfraHostService;
import com.loafle.overflow.central.module.infra.service.CentralInfraServiceService;
import com.loafle.overflow.central.module.probe.service.CentralProbeService; import com.loafle.overflow.central.module.probe.service.CentralProbeService;
import com.loafle.overflow.central.module.target.dao.TargetDAO; import com.loafle.overflow.central.module.target.dao.TargetDAO;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.core.type.CryptoType;
import com.loafle.overflow.model.discovery.Host; import com.loafle.overflow.model.discovery.Host;
import com.loafle.overflow.model.discovery.Service;
import com.loafle.overflow.model.infra.InfraHost;
import com.loafle.overflow.model.infra.InfraService;
import com.loafle.overflow.model.meta.MetaInfraType;
import com.loafle.overflow.model.meta.MetaInfraVendor;
import com.loafle.overflow.model.probe.Probe; import com.loafle.overflow.model.probe.Probe;
import com.loafle.overflow.model.target.Target; import com.loafle.overflow.model.target.Target;
import com.loafle.overflow.service.central.target.TargetService; import com.loafle.overflow.service.central.target.TargetService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
/** /**
* Created by insanity on 17. 6. 28. * Created by insanity on 17. 6. 28.
*/ */
@Service("TargetService") @org.springframework.stereotype.Service("TargetService")
public class CentralTargetService implements TargetService { public class CentralTargetService implements TargetService {
@Autowired @Autowired
@ -27,6 +34,8 @@ public class CentralTargetService implements TargetService {
private CentralProbeService probeService; private CentralProbeService probeService;
@Autowired @Autowired
private CentralInfraHostService infraHostService; private CentralInfraHostService infraHostService;
@Autowired
private CentralInfraServiceService infraServiceService;
@Transactional @Transactional
public Target regist(Target target, Probe probe) throws OverflowException { public Target regist(Target target, Probe probe) throws OverflowException {
@ -73,20 +82,88 @@ public class CentralTargetService implements TargetService {
return this.targetDAO.save(t); return this.targetDAO.save(t);
} }
@Transactional
public List<Target> registDiscoveredTargets(Long probeId, List<Host> hosts, public Target readExistHostTarget(Long probeId, String ip) throws OverflowException {
List<com.loafle.overflow.model.discovery.Service> services) throws OverflowException { InfraHost infraHost = this.infraHostService.readByProbeIdAndIpv4(probeId, ip);
/* if (null == infraHost) return null;
* 해당 host의 mac에 해당하는 infraHost가 이미 존재하는지 검사 infraHost가 없다면 create infraHost가 return this.targetDAO.findByInfraId(infraHost.getId());
* 있다면 update infraHost.getTarget() 있어도 무조건 새로운 target으로 연결
*
*
* for(Host host : hosts) { Target target = new Target();
*
* MetaInfraType infraType = new MetaInfraType(); infraType.setId(2); InfraHost
* infraHost = new InfraHost(); infraHost.setInfraType(infraType);
* infraHost.setProbe(new Probe(probeId)); infraHost.setTarget(target); }
*/
return null;
} }
public Target readExistServiceTarget(Long hostId, int portNumber, String portType) throws OverflowException {
InfraService infraService = this.infraServiceService.readByInfraHostIDAndPortAndPortType(hostId, portNumber, portType);
if (null == infraService) return null;
return this.targetDAO.findByInfraId(infraService.getId());
}
@Transactional
public List<Target> registDiscoveredTargets(Long probeId, List<Host> hosts, List<Service> services)
throws OverflowException {
List<Target> targets = new ArrayList<Target>();
targets.addAll(registDiscoveredHostTargets(probeId, hosts));
targets.addAll(this.registDiscoveredServiceTargets(probeId, services));
return targets;
}
private List<Target> registDiscoveredHostTargets(Long probeId, List<Host> hosts) throws OverflowException {
List<Target> targets = new ArrayList<>();
if (null == hosts)
return targets;
for (Host host : hosts) {
InfraHost infraHost = this.registInfraHostByDiscoveredHost(host, probeId);
Target target = new Target();
target.setInfra(infraHost);
String displayName = host.getIpv6() == null ? host.getIpv6() : host.getIpv4();
target.setDisplayName(displayName);
targets.add(this.targetDAO.save(target));
}
return targets;
}
private List<Target> registDiscoveredServiceTargets(Long probeId, List<Service> services) throws OverflowException {
List<Target> targets = new ArrayList<>();
if (null == services)
return targets;
for (Service service : services) {
Host host = service.getPort().getHost();
InfraHost infraHost = null;
InfraHost existInfraHost = this.infraHostService.readByProbeIdAndIpv4(probeId, host.getIpv4());
InfraService infraService = null;
if (null != existInfraHost) {
infraHost = existInfraHost;
} else {
infraHost = this.registInfraHostByDiscoveredHost(host, probeId);
infraService = this.registInfraServiceByDiscoveredService(infraHost, service, probeId);
}
Target target = new Target();
target.setInfra(infraService);
String displayName = service.getServiceName();
target.setDisplayName(displayName);
targets.add(this.targetDAO.save(target));
}
return targets;
}
private InfraHost registInfraHostByDiscoveredHost(Host host, Long probeId) throws OverflowException {
InfraHost infraHost = new InfraHost();
infraHost.setMetaInfraType(new MetaInfraType(2));
infraHost.setProbe(new Probe(probeId));
infraHost.setIpv4(host.getIpv4());
infraHost.setIpv6(host.getIpv6());
infraHost.setMac(host.getMac());
return this.infraHostService.regist(infraHost);
}
private InfraService registInfraServiceByDiscoveredService(InfraHost infraHost, Service service, Long probeId) throws OverflowException {
InfraService infraService = new InfraService();
infraHost.setMetaInfraType(new MetaInfraType(7));
infraService.setInfraHost(infraHost);
infraService.setPort(service.getPort().getPortNumber());
infraService.setPortType(service.getPort().getPortType().toString());
infraService.setTlsType(service.getCryptoType() == CryptoType.TLS);
infraService.setMetaInfraVendor(MetaInfraVendor.CreateInfraVendorByService(service.getServiceName()));
return this.infraServiceService.regist(infraService);
}
} }