This commit is contained in:
crusader 2018-06-12 18:29:30 +09:00
parent 7d6ff23fbe
commit f556496665
2 changed files with 57 additions and 140 deletions

View File

@ -15,6 +15,7 @@ import com.loafle.overflow.model.discovery.Zone;
import com.loafle.overflow.model.infra.Infra; import com.loafle.overflow.model.infra.Infra;
import com.loafle.overflow.model.infra.InfraHost; import com.loafle.overflow.model.infra.InfraHost;
import com.loafle.overflow.model.infra.InfraHostIP; 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.InfraHostPort;
import com.loafle.overflow.model.infra.InfraZone; import com.loafle.overflow.model.infra.InfraZone;
import com.loafle.overflow.model.meta.MetaCryptoType; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressString;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -68,7 +72,7 @@ public class CentralInfraService implements InfraService {
@Override @Override
@Transactional @Transactional
public InfraZone registZone(Long probeID, Zone zone) throws OverflowException { public InfraZone registByZone(Long probeID, Zone zone) throws OverflowException {
if (null == zone) { if (null == zone) {
throw new OverflowException("Zone is not valid"); throw new OverflowException("Zone is not valid");
} }
@ -116,7 +120,7 @@ public class CentralInfraService implements InfraService {
@Override @Override
@Transactional @Transactional
public InfraHost registHost(Long probeID, Host host) throws OverflowException { public InfraHost registByHost(Long probeID, Host host) throws OverflowException {
if (null == host) { if (null == host) {
throw new OverflowException("Host is not valid"); throw new OverflowException("Host is not valid");
} }
@ -131,7 +135,7 @@ public class CentralInfraService implements InfraService {
throw new OverflowException("Zone is not valid"); throw new OverflowException("Zone is not valid");
} }
InfraZone infraZone = this.registZone(probeID, zone); InfraZone infraZone = this.registByZone(probeID, zone);
MetaIPType metaIPType = new MetaIPType(); MetaIPType metaIPType = new MetaIPType();
metaIPType.setKey(host.getMetaIPTypeKey()); metaIPType.setKey(host.getMetaIPTypeKey());
@ -163,7 +167,7 @@ public class CentralInfraService implements InfraService {
@Override @Override
@Transactional @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 { com.loafle.overflow.model.discovery.Service service) throws OverflowException {
if (null == service) { if (null == service) {
@ -220,8 +224,8 @@ public class CentralInfraService implements InfraService {
infraHostPort = this.infraHostPortService.validate(infraHostPort); infraHostPort = this.infraHostPortService.validate(infraHostPort);
metaPortType = infraHostPort.getMetaPortType(); metaPortType = infraHostPort.getMetaPortType();
InfraZone infraZone = this.registZone(probeID, zone); InfraZone infraZone = this.registByZone(probeID, zone);
InfraHost infraHost = this.registHost(probeID, host); InfraHost infraHost = this.registByHost(probeID, host);
infraHostIP = this.infraHostIPService.readByInfraHostIDAndMetaIPTypeKeyAndAddress(infraHost.getId(), infraHostIP = this.infraHostIPService.readByInfraHostIDAndMetaIPTypeKeyAndAddress(infraHost.getId(),
metaIPType.getKey(), infraHostIP.getAddress()); metaIPType.getKey(), infraHostIP.getAddress());
@ -257,38 +261,60 @@ public class CentralInfraService implements InfraService {
if (null != hosts) { if (null != hosts) {
for (Host host : hosts) { for (Host host : hosts) {
infras.add(this.registHost(probeID, host)); infras.add(this.registByHost(probeID, host));
} }
} }
if (null != services) { if (null != services) {
for (com.loafle.overflow.model.discovery.Service service : services) { for (com.loafle.overflow.model.discovery.Service service : services) {
infras.add(this.registService(probeID, service)); infras.add(this.registByService(probeID, service));
} }
} }
return infras; return infras;
} }
public InfraHost registByNoAuthProbe(Long probeID, InfraHost infraHost) throws OverflowException { public InfraZone registInfraZoneByInfraHostIP(Long probeID, InfraHostIP infraHostIP) throws OverflowException {
// NoAuthProbeDescriptionNetwork networkInfo = if (null == infraHostIP) {
// noAuthProbeDescription.getNetwork(); throw new OverflowException("InfraHostIP is not valid");
// InfraHostIP infraHostIP = new InfraHostIP(); }
// // FIXME getting IPType from networkInfo
// infraHostIP.setMetaIPType(new MetaIPType((short) 1));
// infraHostIP.setAddress(networkInfo.getAddress());
// infraHostIP.setMac(networkInfo.getMacAddress());
// List<InfraHostIP> infraHostIPs = new ArrayList<>();
// infraHostIPs.add(infraHostIP);
// InfraHost infraHost = new InfraHost(); infraHostIP = this.infraHostIPService.validate(infraHostIP);
// infraHost.setProbe(new Probe(probeID));
// infraHost.setMetaInfraType(MetaInfraType.Enum.HOST.to());
// infraHost.setMetaTargetHostType(new MetaTargetHostType(1));
// infraHost.setInfraHostIPs(infraHostIPs);
// 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 @Override
@ -320,5 +346,4 @@ public class CentralInfraService implements InfraService {
return null; return null;
} }
} }

View File

@ -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.GenerateKey;
import com.loafle.overflow.central.commons.utils.SessionMetadata; import com.loafle.overflow.central.commons.utils.SessionMetadata;
import com.loafle.overflow.central.module.infra.service.CentralInfraService; 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.central.module.noauthprobe.dao.NoAuthProbeDAO;
import com.loafle.overflow.core.exception.OverflowException; import com.loafle.overflow.core.exception.OverflowException;
import com.loafle.overflow.model.apikey.ApiKey; 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.domain.DomainMember;
import com.loafle.overflow.model.infra.InfraHost; import com.loafle.overflow.model.infra.InfraHost;
import com.loafle.overflow.model.infra.InfraHostIP; 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.infra.InfraZone;
import com.loafle.overflow.model.member.Member; 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.MetaNoAuthProbeStatus;
import com.loafle.overflow.model.meta.MetaProbeStatus; 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.noauthprobe.NoAuthProbe;
import com.loafle.overflow.model.probe.Probe; import com.loafle.overflow.model.probe.Probe;
import com.loafle.overflow.model.probe.ProbeHost; import com.loafle.overflow.model.probe.ProbeHost;
import com.loafle.overflow.service.central.apikey.ApiKeyService; import com.loafle.overflow.service.central.apikey.ApiKeyService;
import com.loafle.overflow.service.central.domain.DomainMemberService; 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.noauthprobe.NoAuthProbeService;
import com.loafle.overflow.service.central.probe.ProbeHostService; import com.loafle.overflow.service.central.probe.ProbeHostService;
import com.loafle.overflow.service.central.probe.ProbeService; import com.loafle.overflow.service.central.probe.ProbeService;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -79,12 +71,6 @@ public class CentralNoAuthProbeService implements NoAuthProbeService {
@Autowired @Autowired
private ProbeHostService probeHostService; private ProbeHostService probeHostService;
@Autowired
private CentralMetaIPTypeService metaIPTypeService;
@Autowired
private CentralMetaTargetHostTypeService metaTargetHostTypeService;
@ProbeAPI @ProbeAPI
public NoAuthProbe regist(NoAuthProbe noAuthProbe) throws OverflowException { public NoAuthProbe regist(NoAuthProbe noAuthProbe) throws OverflowException {
if (null == noAuthProbe) { if (null == noAuthProbe) {
@ -189,19 +175,14 @@ public class CentralNoAuthProbeService implements NoAuthProbeService {
probe = this.probeService.regist(probe); 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); ProbeHost probeHost = new ProbeHost();
infraHost.setInfraZone(infraZone); probeHost.setInfraHost(infraHost);
probeHost.setProbe(probe);
Probe probe = this.newProbe(noAuthProbe, infraZone, infraHost); this.probeHostService.regist(probeHost);
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()); noAuthProbe.setMetaNoAuthProbeStatus(MetaNoAuthProbeStatus.Enum.ACCEPTED.to());
this.noAuthProbeDAO.save(noAuthProbe); this.noAuthProbeDAO.save(noAuthProbe);
@ -212,95 +193,6 @@ public class CentralNoAuthProbeService implements NoAuthProbeService {
return this.readAllByDomainID(noAuthProbe.getDomain().getId()); 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 @WebappAPI
public List<NoAuthProbe> denyNoauthProbe(Long noAuthProbeID) throws OverflowException { public List<NoAuthProbe> denyNoauthProbe(Long noAuthProbeID) throws OverflowException {
NoAuthProbe noAuthProbe = this.noAuthProbeDAO.findById(noAuthProbeID).get(); NoAuthProbe noAuthProbe = this.noAuthProbeDAO.findById(noAuthProbeID).get();