diff --git a/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java b/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java index 838130d..a34130d 100644 --- a/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java +++ b/src/main/java/com/loafle/overflow/central/module/noauthprobe/service/CentralNoAuthProbeService.java @@ -7,20 +7,34 @@ import com.loafle.overflow.central.commons.stereotype.ProbeAPI; import com.loafle.overflow.central.commons.stereotype.WebappAPI; import com.loafle.overflow.central.commons.utils.GenerateKey; import com.loafle.overflow.central.commons.utils.SessionMetadata; +import com.loafle.overflow.central.commons.utils.StringConvertor; +import com.loafle.overflow.central.module.infra.service.CentralInfraMachineService; +import com.loafle.overflow.central.module.infra.service.CentralInfraOSService; import com.loafle.overflow.central.module.noauthprobe.dao.NoAuthProbeDAO; import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.model.apikey.ApiKey; import com.loafle.overflow.model.domain.Domain; import com.loafle.overflow.model.domain.DomainMember; +import com.loafle.overflow.model.infra.InfraHost; +import com.loafle.overflow.model.infra.InfraMachine; +import com.loafle.overflow.model.infra.InfraOS; import com.loafle.overflow.model.member.Member; +import com.loafle.overflow.model.meta.MetaInfraType; +import com.loafle.overflow.model.meta.MetaInfraVendor; import com.loafle.overflow.model.meta.MetaNoAuthProbeStatus; import com.loafle.overflow.model.meta.MetaProbeStatus; import com.loafle.overflow.model.noauthprobe.NoAuthProbe; import com.loafle.overflow.model.probe.Probe; +import com.loafle.overflow.model.probe.ProbeHost; import com.loafle.overflow.service.central.apikey.ApiKeyService; import com.loafle.overflow.service.central.domain.DomainMemberService; +import com.loafle.overflow.service.central.infra.InfraHostService; +import com.loafle.overflow.service.central.infra.InfraMachineService; +import com.loafle.overflow.service.central.infra.InfraOSService; import com.loafle.overflow.service.central.noauthprobe.NoAuthProbeService; +import com.loafle.overflow.service.central.probe.ProbeHostService; import com.loafle.overflow.service.central.probe.ProbeService; +import com.sun.jna.platform.win32.Variant.VARIANT._VARIANT.__VARIANT; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @@ -31,11 +45,13 @@ import java.util.List; import java.util.Map; import java.util.UUID; +import javax.transaction.Transactional; + /** * Created by snoop on 17. 6. 28. */ @Service("NoAuthProbeService") -public class CentralNoAuthProbeService implements NoAuthProbeService{ +public class CentralNoAuthProbeService implements NoAuthProbeService { @Autowired private NoAuthProbeDAO noAuthProbeDAO; @@ -54,22 +70,30 @@ public class CentralNoAuthProbeService implements NoAuthProbeService{ @Autowired private Gson gson; - + + @Autowired + private InfraMachineService infraMachineService; + @Autowired + private InfraOSService infraOSService; + @Autowired + private InfraHostService infraHostService; + @Autowired + private ProbeHostService probeHostService; + @ProbeAPI - public NoAuthProbe regist(NoAuthProbe noAuthProbe) throws OverflowException{ + public NoAuthProbe regist(NoAuthProbe noAuthProbe) throws OverflowException { ApiKey apiKey = apiKeyService.readByApiKey(noAuthProbe.getApiKey()); noAuthProbe.setDomain(apiKey.getDomain()); noAuthProbe.setTempProbeKey(GenerateKey.getKey()); - noAuthProbe.setStatus(new MetaNoAuthProbeStatus((short)3)); + noAuthProbe.setStatus(new MetaNoAuthProbeStatus((short) 3)); messagePublisher.publishToDomainMembers(apiKey.getDomain().getId(), "NoAuthProbeService.regist", noAuthProbe); return this.noAuthProbeDAO.save(noAuthProbe); } - public List readAllByDomain(Domain domain) throws OverflowException { return this.noAuthProbeDAO.findAllByDomain(domain); @@ -80,47 +104,101 @@ public class CentralNoAuthProbeService implements NoAuthProbeService{ } @WebappAPI + @Transactional public List acceptNoAuthProbe(NoAuthProbe noAuthProbe) throws OverflowException { - String memberEmail = SessionMetadata.getTargetID(); - // Todo domain injection & member injection + HashMap objMap = this.gson.fromJson(noAuthProbe.getDescription(), + TypeToken.getParameterized(HashMap.class, String.class, Object.class).getType()); + Map hostMap = (Map) objMap.get("host"); + Map netMap = (Map) objMap.get("network"); + + Probe probe = this.newProbe(noAuthProbe, hostMap, netMap); + InfraMachine machine = this.newInfraMachine(noAuthProbe); + InfraOS os = this.newInfraOS(machine, hostMap); + InfraHost host = this.newInfraHost(os, netMap); + this.newProbeHost(host, probe); + + noAuthProbe.setStatus(new MetaNoAuthProbeStatus((short) 1)); + this.noAuthProbeDAO.save(noAuthProbe); + + messagePublisher.publishToNoAuthProbe(noAuthProbe.getTempProbeKey(), "NoAuthProbeService.Accept", + probe.getProbeKey()); + + return this.readAllByDomain(noAuthProbe.getDomain()); + } + + private Probe newProbe(NoAuthProbe noauthprobe, Map hostMap, Map netMap) throws OverflowException { BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String encryptKey = passwordEncoder.encode(UUID.randomUUID().toString()); - ApiKey apiKey = apiKeyService.readByApiKey(noAuthProbe.getApiKey()); + ApiKey apiKey = apiKeyService.readByApiKey(noauthprobe.getApiKey()); + String memberEmail = SessionMetadata.getTargetID(); DomainMember domainMember = domainMemberService.readByMemberEmail(memberEmail); Probe probe = new Probe(); probe.setEncryptionKey(encryptKey); probe.setProbeKey(GenerateKey.getKey()); - probe.setDomain(new Domain(apiKey.getDomain().getId())); probe.setAuthorizeMember(new Member(domainMember.getMember().getId())); - probe.setStatus(new MetaProbeStatus((short)1)); - - HashMap objMap = this.gson.fromJson(noAuthProbe.getDescription(), TypeToken.getParameterized(HashMap.class, String.class, Object.class).getType()); - Map hostMap = (Map) objMap.get("host"); - Map netMap = (Map) objMap.get("network"); + probe.setStatus(new MetaProbeStatus((short) 1)); String dispName = hostMap.get("name"); dispName += " probe"; probe.setDisplayName(dispName); String addrStr = netMap.get("address"); - String[] addrArr = addrStr.split("\\|"); - probe.setCidr(addrArr[0]); - noAuthProbe.setStatus(new MetaNoAuthProbeStatus((short) 1)); + return this.probeService.regist(probe); + } - this.probeService.regist(probe); - this.noAuthProbeDAO.save(noAuthProbe); + private InfraMachine newInfraMachine(NoAuthProbe noauthprobe) throws OverflowException { + MetaInfraType infraType = new MetaInfraType(); + infraType.setId(1); - messagePublisher.publishToNoAuthProbe(noAuthProbe.getTempProbeKey(), "NoAuthProbeService.Accept", probe.getProbeKey()); + InfraMachine infraMachine = new InfraMachine(); + infraMachine.setMeta(noauthprobe.getDescription()); + infraMachine.setInfraType(infraType); - return this.readAllByDomain(noAuthProbe.getDomain()); + return this.infraMachineService.regist(infraMachine); + } + + private InfraOS newInfraOS(InfraMachine infraMachine, Map hostMap) throws OverflowException { + MetaInfraType infraType = new MetaInfraType(); + infraType.setId(3); + + InfraOS infraOS = new InfraOS(); + infraOS.setMachine(infraMachine); + infraOS.setInfraType(infraType); + infraOS.setVendor(MetaInfraVendor.CreateInfraVendorByOS(hostMap.get("os"))); + + return this.infraOSService.regist(infraOS); + } + + private InfraHost newInfraHost(InfraOS infraOS, Map netMap) throws OverflowException { + MetaInfraType infraType = new MetaInfraType(); + infraType.setId(2); + + InfraHost infraHost = new InfraHost(); + String addrStr = netMap.get("address"); + String[] addrArr = addrStr.split("\\|"); + + infraHost.setIp(StringConvertor.ipToLong(addrArr[1])); + infraHost.setMac(StringConvertor.macStrToLong(netMap.get("macAddress"))); + infraHost.setOs(infraOS); + infraHost.setInfraType(infraType); + + return this.infraHostService.regist(infraHost); + } + + private void newProbeHost(InfraHost infraHost, Probe probe) throws OverflowException { + ProbeHost probeHost = new ProbeHost(); + probeHost.setHost(infraHost); + probeHost.setProbe(probe); + + this.probeHostService.regist(probeHost); } @WebappAPI