From f556496665d945ec9c6c27f7e70d63edbaa7f4f3 Mon Sep 17 00:00:00 2001 From: crusader Date: Tue, 12 Jun 2018 18:29:30 +0900 Subject: [PATCH] ing --- .../infra/service/CentralInfraService.java | 77 +++++++---- .../service/CentralNoAuthProbeService.java | 120 +----------------- 2 files changed, 57 insertions(+), 140 deletions(-) diff --git a/src/main/java/com/loafle/overflow/central/module/infra/service/CentralInfraService.java b/src/main/java/com/loafle/overflow/central/module/infra/service/CentralInfraService.java index 7b8032e..87e60d5 100644 --- a/src/main/java/com/loafle/overflow/central/module/infra/service/CentralInfraService.java +++ b/src/main/java/com/loafle/overflow/central/module/infra/service/CentralInfraService.java @@ -15,6 +15,7 @@ import com.loafle.overflow.model.discovery.Zone; import com.loafle.overflow.model.infra.Infra; import com.loafle.overflow.model.infra.InfraHost; import com.loafle.overflow.model.infra.InfraHostIP; +import com.loafle.overflow.model.infra.InfraHostOS; import com.loafle.overflow.model.infra.InfraHostPort; import com.loafle.overflow.model.infra.InfraZone; import com.loafle.overflow.model.meta.MetaCryptoType; @@ -31,6 +32,9 @@ import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import inet.ipaddr.IPAddress; +import inet.ipaddr.IPAddressString; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -68,7 +72,7 @@ public class CentralInfraService implements InfraService { @Override @Transactional - public InfraZone registZone(Long probeID, Zone zone) throws OverflowException { + public InfraZone registByZone(Long probeID, Zone zone) throws OverflowException { if (null == zone) { throw new OverflowException("Zone is not valid"); } @@ -116,7 +120,7 @@ public class CentralInfraService implements InfraService { @Override @Transactional - public InfraHost registHost(Long probeID, Host host) throws OverflowException { + public InfraHost registByHost(Long probeID, Host host) throws OverflowException { if (null == host) { throw new OverflowException("Host is not valid"); } @@ -131,7 +135,7 @@ public class CentralInfraService implements InfraService { throw new OverflowException("Zone is not valid"); } - InfraZone infraZone = this.registZone(probeID, zone); + InfraZone infraZone = this.registByZone(probeID, zone); MetaIPType metaIPType = new MetaIPType(); metaIPType.setKey(host.getMetaIPTypeKey()); @@ -163,7 +167,7 @@ public class CentralInfraService implements InfraService { @Override @Transactional - public com.loafle.overflow.model.infra.InfraService registService(Long probeID, + public com.loafle.overflow.model.infra.InfraService registByService(Long probeID, com.loafle.overflow.model.discovery.Service service) throws OverflowException { if (null == service) { @@ -220,8 +224,8 @@ public class CentralInfraService implements InfraService { infraHostPort = this.infraHostPortService.validate(infraHostPort); metaPortType = infraHostPort.getMetaPortType(); - InfraZone infraZone = this.registZone(probeID, zone); - InfraHost infraHost = this.registHost(probeID, host); + InfraZone infraZone = this.registByZone(probeID, zone); + InfraHost infraHost = this.registByHost(probeID, host); infraHostIP = this.infraHostIPService.readByInfraHostIDAndMetaIPTypeKeyAndAddress(infraHost.getId(), metaIPType.getKey(), infraHostIP.getAddress()); @@ -257,38 +261,60 @@ public class CentralInfraService implements InfraService { if (null != hosts) { for (Host host : hosts) { - infras.add(this.registHost(probeID, host)); + infras.add(this.registByHost(probeID, host)); } } if (null != services) { for (com.loafle.overflow.model.discovery.Service service : services) { - infras.add(this.registService(probeID, service)); + infras.add(this.registByService(probeID, service)); } } return infras; } - public InfraHost registByNoAuthProbe(Long probeID, InfraHost infraHost) throws OverflowException { - // NoAuthProbeDescriptionNetwork networkInfo = - // noAuthProbeDescription.getNetwork(); - // InfraHostIP infraHostIP = new InfraHostIP(); - // // FIXME getting IPType from networkInfo - // infraHostIP.setMetaIPType(new MetaIPType((short) 1)); - // infraHostIP.setAddress(networkInfo.getAddress()); - // infraHostIP.setMac(networkInfo.getMacAddress()); - // List infraHostIPs = new ArrayList<>(); - // infraHostIPs.add(infraHostIP); + public InfraZone registInfraZoneByInfraHostIP(Long probeID, InfraHostIP infraHostIP) throws OverflowException { + if (null == infraHostIP) { + throw new OverflowException("InfraHostIP is not valid"); + } - // InfraHost infraHost = new InfraHost(); - // infraHost.setProbe(new Probe(probeID)); - // infraHost.setMetaInfraType(MetaInfraType.Enum.HOST.to()); - // infraHost.setMetaTargetHostType(new MetaTargetHostType(1)); - // infraHost.setInfraHostIPs(infraHostIPs); + infraHostIP = this.infraHostIPService.validate(infraHostIP); - // return this.infraDAO.save(infraHost); + InfraZone infraZone = new InfraZone(); + infraZone.setProbe(new Probe(probeID)); + infraZone.setMetaInfraType(MetaInfraType.Enum.ZONE.to()); + infraZone.setMetaIPType(infraHostIP.getMetaIPType()); + infraZone.setAddress(infraHostIP.getAddress()); + infraZone.setIface(infraHostIP.getIface()); + infraZone.setMac(infraHostIP.getMac()); - return null; + IPAddress ipAddress = new IPAddressString(infraHostIP.getAddress()).getAddress(); + infraZone.setNetwork(ipAddress.toPrefixBlock().toString()); + + return this.infraZoneDAO.save(infraZone); + } + + public InfraHost registInfraHostByInfraHost(Long probeID, Long infraZoneID, InfraHost oriInfraHost) throws OverflowException { + InfraHost infraHost = new InfraHost(); + infraHost.setProbe(new Probe(probeID)); + infraHost.setInfraZone(new InfraZone(infraZoneID)); + infraHost.setMetaInfraType(MetaInfraType.Enum.HOST.to()); + + InfraHostOS infraHostOS = oriInfraHost.getInfraHostOS(); + if (null != infraHostOS) { + // MetaTargetHostType metaTargetHostType = this.metaTargetHostTypeService.readByInfraHostOS(infraHostOS); + infraHost.setMetaTargetHostType(MetaTargetHostType.Enum.UNKNOWN.to()); + infraHost.setInfraHostOS(infraHostOS); + } else { + infraHost.setMetaTargetHostType(MetaTargetHostType.Enum.UNKNOWN.to()); + } + + infraHost.setInfraHostIPs(oriInfraHost.getInfraHostIPs()); + infraHost.setInfraHostPorts(oriInfraHost.getInfraHostPorts()); + infraHost.setInfraHostApplications(oriInfraHost.getInfraHostApplications()); + infraHost.setInfraHostDaemons(oriInfraHost.getInfraHostDaemons()); + + return this.infraHostDAO.save(infraHost); } @Override @@ -320,5 +346,4 @@ public class CentralInfraService implements InfraService { return null; } - } 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 752e624..ee54807 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 @@ -6,8 +6,6 @@ import com.loafle.overflow.core.annotation.WebappAPI; import com.loafle.overflow.central.commons.utils.GenerateKey; import com.loafle.overflow.central.commons.utils.SessionMetadata; import com.loafle.overflow.central.module.infra.service.CentralInfraService; -import com.loafle.overflow.central.module.meta.service.CentralMetaIPTypeService; -import com.loafle.overflow.central.module.meta.service.CentralMetaTargetHostTypeService; import com.loafle.overflow.central.module.noauthprobe.dao.NoAuthProbeDAO; import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.model.apikey.ApiKey; @@ -15,27 +13,21 @@ 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.InfraHostIP; -import com.loafle.overflow.model.infra.InfraHostOS; import com.loafle.overflow.model.infra.InfraZone; import com.loafle.overflow.model.member.Member; -import com.loafle.overflow.model.meta.MetaIPType; -import com.loafle.overflow.model.meta.MetaInfraType; import com.loafle.overflow.model.meta.MetaNoAuthProbeStatus; import com.loafle.overflow.model.meta.MetaProbeStatus; -import com.loafle.overflow.model.meta.MetaTargetHostType; 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.meta.MetaIPTypeService; 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 org.codehaus.jackson.map.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; @@ -79,12 +71,6 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { @Autowired private ProbeHostService probeHostService; - @Autowired - private CentralMetaIPTypeService metaIPTypeService; - - @Autowired - private CentralMetaTargetHostTypeService metaTargetHostTypeService; - @ProbeAPI public NoAuthProbe regist(NoAuthProbe noAuthProbe) throws OverflowException { if (null == noAuthProbe) { @@ -189,19 +175,14 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { probe = this.probeService.regist(probe); - InfraZone infraZone = this.newInfraZone(infraHostIP); + InfraZone infraZone = this.infraService.registInfraZoneByInfraHostIP(probe.getId(), infraHostIP); + InfraHost infraHost = this.infraService.registInfraHostByInfraHost(probe.getId(), infraZone.getId(), oriInfraHost); - InfraHost infraHost = this.newInfraHost(oriInfraHost); - infraHost.setInfraZone(infraZone); + ProbeHost probeHost = new ProbeHost(); + probeHost.setInfraHost(infraHost); + probeHost.setProbe(probe); - Probe probe = this.newProbe(noAuthProbe, infraZone, infraHost); - infraZone.setProbe(probe); - infraHost.setProbe(probe); - - infraZone = this.infraService.registInfraZone(infraZone); - infraHost = this.infraService.registInfraHost(infraHost); - - this.newProbeHost(probe, infraHost); + this.probeHostService.regist(probeHost); noAuthProbe.setMetaNoAuthProbeStatus(MetaNoAuthProbeStatus.Enum.ACCEPTED.to()); this.noAuthProbeDAO.save(noAuthProbe); @@ -212,95 +193,6 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { return this.readAllByDomainID(noAuthProbe.getDomain().getId()); } - private InfraZone newInfraZone(InfraHostIP infraHostIP) throws OverflowException { - if (null == infraHostIP) { - throw new OverflowException("InfraHostIP is not valid"); - } - - InfraZone infraZone = new InfraZone(); - infraZone.setMetaInfraType(MetaInfraType.Enum.ZONE.to()); - - if (null == infraHostIP.getMetaIPType() || null == infraHostIP.getMetaIPType().getKey()) { - throw new OverflowException("MetaIPType is not valid"); - } - MetaIPType metaIPType = this.metaIPTypeService.readByKey(infraHostIP.getMetaIPType().getKey()); - if (null == metaIPType) { - throw new OverflowException( - String.format("Key[%s] of MetaIPType is not valid", infraHostIP.getMetaIPType().getKey())); - } - infraZone.setMetaIPType(metaIPType); - - if (null == infraHostIP.getAddress()) { - throw new OverflowException("Address is not valid"); - } - infraZone.setAddress(infraHostIP.getAddress()); - - IPAddress ipAddress = new IPAddressString(infraHostIP.getAddress()).getAddress(); - infraZone.setNetwork(ipAddress.toPrefixBlock().toString()); - - if (null == infraHostIP.getAddress()) { - throw new OverflowException("Iface is not valid"); - } - infraZone.setIface(infraHostIP.getIface()); - - if (null == infraHostIP.getMac()) { - throw new OverflowException("Mac is not valid"); - } - infraZone.setMac(infraHostIP.getMac()); - - return infraZone; - } - - private InfraHost newInfraHost(InfraHost oriInfraHost) throws OverflowException { - InfraHost infraHost = new InfraHost(); - infraHost.setMetaInfraType(MetaInfraType.Enum.HOST.to()); - - InfraHostOS infraHostOS = oriInfraHost.getInfraHostOS(); - // MetaTargetHostType metaTargetHostType = this.metaTargetHostTypeService.readByInfraHostOS(infraHostOS); - infraHost.setMetaTargetHostType(MetaTargetHostType.Enum.UNKNOWN.to()); - infraHost.setInfraHostOS(infraHostOS); - - infraHost.setInfraHostIPs(oriInfraHost.getInfraHostIPs()); - infraHost.setInfraHostPorts(oriInfraHost.getInfraHostPorts()); - infraHost.setInfraHostApplications(oriInfraHost.getInfraHostApplications()); - infraHost.setInfraHostDaemons(oriInfraHost.getInfraHostDaemons()); - - return infraHost; - } - - private Probe newProbe(NoAuthProbe noauthprobe, InfraZone infraZone, InfraHost infraHost) throws OverflowException { - - BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); - String encryptKey = passwordEncoder.encode(UUID.randomUUID().toString()); - - 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.setTargetCount(0); - probe.setMetaProbeStatus(MetaProbeStatus.Enum.INITIAL.to()); - - String name = infraHost.getInfraHostOS().getName(); - name += " probe"; - probe.setName(name); - probe.setCidr(infraZone.getNetwork()); - - return this.probeService.regist(probe); - } - - private void newProbeHost(Probe probe, InfraHost infraHost) throws OverflowException { - ProbeHost probeHost = new ProbeHost(); - probeHost.setInfraHost(infraHost); - probeHost.setProbe(probe); - - this.probeHostService.regist(probeHost); - } - @WebappAPI public List denyNoauthProbe(Long noAuthProbeID) throws OverflowException { NoAuthProbe noAuthProbe = this.noAuthProbeDAO.findById(noAuthProbeID).get();