diff --git a/pom.xml b/pom.xml index 844b274..c375285 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,7 @@ 1.1.2 3.1.5 4.5.3 + 4.2.0 @@ -50,7 +51,7 @@ com.loafle.overflow commons-java - 1.0.53-SNAPSHOT + 1.0.54-SNAPSHOT @@ -224,6 +225,12 @@ java-uuid-generator ${fasterxml.uuid.verion} + + + com.github.seancfoley + ipaddress + ${seancfoley.ipaddress.version} + 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 1808348..752e624 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,6 +6,8 @@ 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; @@ -13,15 +15,20 @@ 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; @@ -32,6 +39,9 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; +import inet.ipaddr.IPAddress; +import inet.ipaddr.IPAddressString; + import java.io.IOException; import java.util.Date; import java.util.List; @@ -62,22 +72,33 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { @Autowired private ObjectMapper objectMapper; - + @Autowired - @Qualifier("InfraService") private CentralInfraService infraService; @Autowired private ProbeHostService probeHostService; + @Autowired + private CentralMetaIPTypeService metaIPTypeService; + + @Autowired + private CentralMetaTargetHostTypeService metaTargetHostTypeService; + @ProbeAPI public NoAuthProbe regist(NoAuthProbe noAuthProbe) throws OverflowException { + if (null == noAuthProbe) { + throw new OverflowException("NoAuthProbe is not valid"); + } ApiKey apiKey = apiKeyService.readByApiKey(noAuthProbe.getApiKey()); - noAuthProbe.setDomain(apiKey.getDomain()); + if (null == apiKey) { + throw new OverflowException(String.format("APIKey[%s] of NoAuthProbe is not valid", noAuthProbe.getApiKey())); + } + noAuthProbe.setDomain(apiKey.getDomain()); noAuthProbe.setTempProbeKey(GenerateKey.getKey()); - noAuthProbe.setMetaNoAuthProbeStatus(new MetaNoAuthProbeStatus((short) 3)); + noAuthProbe.setMetaNoAuthProbeStatus(MetaNoAuthProbeStatus.Enum.PROCESSING.to()); messagePublisher.publishToDomainMembers(apiKey.getDomain().getId(), "NoAuthProbeService.regist", noAuthProbe); @@ -85,8 +106,8 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { } public List readAllByDomainID(Long domainID) throws OverflowException { - - return this.noAuthProbeDAO.findAllByDomainIdAndMetaNoAuthProbeStatusId(domainID, (short) 3); + return this.noAuthProbeDAO.findAllByDomainIdAndMetaNoAuthProbeStatusId(domainID, + MetaNoAuthProbeStatus.Enum.PROCESSING.getValue()); } public NoAuthProbe read(Long id) { @@ -108,24 +129,81 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { throw new OverflowException("json error", e); } - if (null == oriInfraHost.getInfraHostIPs()) { + if (null == oriInfraHost.getInfraHostIPs() || 0 == oriInfraHost.getInfraHostIPs().size()) { throw new OverflowException("IP of Probe InfraHost is not valild"); } - for (InfraHostIP infraHostIP : oriInfraHost.getInfraHostIPs()) { - + if (null == zoneCIDR && 1 < oriInfraHost.getInfraHostIPs().size()) { + throw new OverflowException(String.format("Zone count[%d] of Probe is not single, must specified", + oriInfraHost.getInfraHostIPs().size())); } + InfraHostIP infraHostIP = null; + if (1 == oriInfraHost.getInfraHostIPs().size()) { + infraHostIP = oriInfraHost.getInfraHostIPs().get(0); + } else { + IPAddress ZoneIPAddress = new IPAddressString(zoneCIDR).getAddress(); - InfraZone infraZone = new InfraZone(); - InfraHost infraHost = new InfraHost(); + for (InfraHostIP _infraHostIP : oriInfraHost.getInfraHostIPs()) { + if (null == _infraHostIP.getAddress()) { + continue; + } + IPAddress ipAddress = new IPAddressString(_infraHostIP.getAddress()).getAddress(); + if (ipAddress.toPrefixBlock().equals(ZoneIPAddress)) { + infraHostIP = _infraHostIP; + break; + } + } + } + if (null == infraHostIP) { + throw new OverflowException("Network is not valid"); + } + if (null == oriInfraHost.getInfraHostOS()) { + throw new OverflowException("InfraHostOS is not valid"); + } + if (null == oriInfraHost.getInfraHostMachine()) { + throw new OverflowException("InfraHostMachine is not valid"); + } - Probe probe = this.newProbe(noAuthProbe, infraHost); - InfraHost host = this.infraService.registByNoAuthProbe(probe.getId(), infraHost); - this.newProbeHost(host, probe); + BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); + String encryptKey = passwordEncoder.encode(UUID.randomUUID().toString()); - noAuthProbe.setMetaNoAuthProbeStatus(new MetaNoAuthProbeStatus((short) 1)); + 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 = oriInfraHost.getInfraHostOS().getName(); + name += " probe"; + probe.setName(name); + IPAddress ipAddress = new IPAddressString(infraHostIP.getAddress()).getAddress(); + probe.setCidr(ipAddress.toPrefixBlock().toString()); + + probe = this.probeService.regist(probe); + + InfraZone infraZone = this.newInfraZone(infraHostIP); + + InfraHost infraHost = this.newInfraHost(oriInfraHost); + infraHost.setInfraZone(infraZone); + + 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); + + noAuthProbe.setMetaNoAuthProbeStatus(MetaNoAuthProbeStatus.Enum.ACCEPTED.to()); this.noAuthProbeDAO.save(noAuthProbe); messagePublisher.publishToNoAuthProbe(noAuthProbe.getTempProbeKey(), "NoAuthProbeService.Accept", @@ -134,8 +212,63 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { return this.readAllByDomainID(noAuthProbe.getDomain().getId()); } - private Probe newProbe(NoAuthProbe noauthprobe, InfraHost infraHost) - throws OverflowException { + 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()); @@ -155,16 +288,12 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { String name = infraHost.getInfraHostOS().getName(); name += " probe"; probe.setName(name); - - // String addrStr = noAuthProbeDescription.getNetwork().getAddress(); - // String[] addrArr = addrStr.split("\\|"); - // probe.setCidr(addrArr[0]); + probe.setCidr(infraZone.getNetwork()); return this.probeService.regist(probe); } - - private void newProbeHost(InfraHost infraHost, Probe probe) throws OverflowException { + private void newProbeHost(Probe probe, InfraHost infraHost) throws OverflowException { ProbeHost probeHost = new ProbeHost(); probeHost.setInfraHost(infraHost); probeHost.setProbe(probe); @@ -176,7 +305,7 @@ public class CentralNoAuthProbeService implements NoAuthProbeService { public List denyNoauthProbe(Long noAuthProbeID) throws OverflowException { NoAuthProbe noAuthProbe = this.noAuthProbeDAO.findById(noAuthProbeID).get(); - noAuthProbe.setMetaNoAuthProbeStatus(new MetaNoAuthProbeStatus((short) 2)); + noAuthProbe.setMetaNoAuthProbeStatus(MetaNoAuthProbeStatus.Enum.DENIED.to()); this.noAuthProbeDAO.save(noAuthProbe); messagePublisher.publishToNoAuthProbe(noAuthProbe.getTempProbeKey(), "NoAuthProbeService.Deny");