From 752be72906cea1b9cb3dfb7099331f7c50a09bd9 Mon Sep 17 00:00:00 2001 From: snoop Date: Wed, 25 Apr 2018 20:56:07 +0900 Subject: [PATCH] fixed targetdiscovery --- .../module/infra/dao/InfraOSPortDAO.java | 5 +- .../CentralTargetDiscoveryService.java | 11 +- .../module/infra/dao/InfraOSPortDAOTest.java | 30 +++ .../member/service/MemberServiceTest.java | 134 ++++++------ .../service/TargetDiscoveryServiceTest.java | 202 +++++++++--------- 5 files changed, 216 insertions(+), 166 deletions(-) create mode 100644 src/test/java/com/loafle/overflow/central/module/infra/dao/InfraOSPortDAOTest.java diff --git a/src/main/java/com/loafle/overflow/central/module/infra/dao/InfraOSPortDAO.java b/src/main/java/com/loafle/overflow/central/module/infra/dao/InfraOSPortDAO.java index cf09d62..4fefafc 100644 --- a/src/main/java/com/loafle/overflow/central/module/infra/dao/InfraOSPortDAO.java +++ b/src/main/java/com/loafle/overflow/central/module/infra/dao/InfraOSPortDAO.java @@ -3,6 +3,7 @@ package com.loafle.overflow.central.module.infra.dao; import com.loafle.overflow.model.infra.InfraOSPort; import org.springframework.data.jpa.repository.JpaRepository; // import org.springframework.data.jpa.repository.Query; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @@ -11,6 +12,6 @@ import org.springframework.stereotype.Repository; */ @Repository public interface InfraOSPortDAO extends JpaRepository { - // @Query("SELECT p from InfraOSPort p WHERE p.os.id = (:osId) AND p.port = (:portNumber) AND p.portType = (:portType)") - InfraOSPort findByPort(@Param("osId") long osId,@Param("portNumber") int portNumber,@Param("portType") String portType); + @Query("SELECT p from com.loafle.overflow.model.infra.InfraOSPort p WHERE p.os.id = (:osId) AND p.port = (:portNumber) AND p.portType = (:portType)") + InfraOSPort findByPort(@Param("osId") long osId,@Param("portNumber") Integer portNumber,@Param("portType") String portType); } \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetDiscoveryService.java b/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetDiscoveryService.java index 3d3e1b8..d91125c 100644 --- a/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetDiscoveryService.java +++ b/src/main/java/com/loafle/overflow/central/module/target/service/CentralTargetDiscoveryService.java @@ -69,10 +69,14 @@ public class CentralTargetDiscoveryService implements TargetDiscoveryService{ String portType = "UDP"; - if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP) { + if(port.getPortType() == PortType.TLS || port.getPortType() == PortType.TCP || port.getPortType() == null) { portType = "TCP"; } + if(port.getServiceList() == null) { + return; + } + // for(String key : port.getServices().keySet()) { for(com.loafle.overflow.model.discovery.Service service : port.getServiceList()) { @@ -130,6 +134,11 @@ public class CentralTargetDiscoveryService implements TargetDiscoveryService{ InfraOS infraOS = infraHost.getOs(); // for( String key: host.getPorts().keySet()) { + + if(host.getPortList() == null) { + return; + } + for( Port port: host.getPortList()) { // Port port = host.getPorts().get(key); diff --git a/src/test/java/com/loafle/overflow/central/module/infra/dao/InfraOSPortDAOTest.java b/src/test/java/com/loafle/overflow/central/module/infra/dao/InfraOSPortDAOTest.java new file mode 100644 index 0000000..8906489 --- /dev/null +++ b/src/test/java/com/loafle/overflow/central/module/infra/dao/InfraOSPortDAOTest.java @@ -0,0 +1,30 @@ +package com.loafle.overflow.central.module.infra.dao; + +import com.loafle.overflow.central.spring.AppConfigTest; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.*; + +/** + * Created by snoop on 18. 4. 25. + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {AppConfigTest.class}) +public class InfraOSPortDAOTest { + + @Autowired + private InfraOSPortDAO infraOSPortDAO; + + @Test + public void findByPort() throws Exception { + + +// this.infraOSPortDAO.findByPort(1, 22, "UDP"); + + } + +} \ No newline at end of file diff --git a/src/test/java/com/loafle/overflow/central/module/member/service/MemberServiceTest.java b/src/test/java/com/loafle/overflow/central/module/member/service/MemberServiceTest.java index 161c984..8a72050 100644 --- a/src/test/java/com/loafle/overflow/central/module/member/service/MemberServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/member/service/MemberServiceTest.java @@ -1,37 +1,37 @@ -// package com.loafle.overflow.central.module.member.service; + package com.loafle.overflow.central.module.member.service; -// import com.loafle.overflow.central.spring.AppConfigTest; -// import com.loafle.overflow.model.member.Member; -// import com.loafle.overflow.service.central.member.MemberService; -// import com.loafle.overflow.service.central.member.MemberTotpService; + import com.loafle.overflow.central.spring.AppConfigTest; + import com.loafle.overflow.model.member.Member; + import com.loafle.overflow.service.central.member.MemberService; + import com.loafle.overflow.service.central.member.MemberTotpService; -// import org.junit.Assert; -// import org.junit.Ignore; -// import org.junit.Test; -// import org.junit.runner.RunWith; -// import org.springframework.beans.factory.annotation.Autowired; -// import org.springframework.test.context.ActiveProfiles; -// import org.springframework.test.context.ContextConfiguration; -// import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + import org.junit.Assert; + import org.junit.Ignore; + import org.junit.Test; + import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.test.context.ActiveProfiles; + import org.springframework.test.context.ContextConfiguration; + import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -// import java.util.Date; -// import java.util.List; -// import java.util.regex.Matcher; -// import java.util.regex.Pattern; + import java.util.Date; + import java.util.List; + import java.util.regex.Matcher; + import java.util.regex.Pattern; -// /** -// * Created by insanity on 17. 6. 28. -// */ + /** + * Created by insanity on 17. 6. 28. + */ -// @RunWith(SpringJUnit4ClassRunner.class) -// @ActiveProfiles("test") -// @ContextConfiguration(classes = {AppConfigTest.class}) -// public class MemberServiceTest { -// @Autowired -// MemberService memberService; + @RunWith(SpringJUnit4ClassRunner.class) + @ActiveProfiles("test") + @ContextConfiguration(classes = {AppConfigTest.class}) + public class MemberServiceTest { + @Autowired + MemberService memberService; -// @Autowired -// MemberTotpService totpService; + @Autowired + MemberTotpService totpService; // @Test @@ -44,53 +44,53 @@ // m.setEmail("geekhot@hotmail.co.kr"); // m.setName("administrator"); // m.setPhone("00000000"); - +// // Member res = this.memberService.signup(m,"!@#$qwer1234"); // Assert.assertNotNull(res); // } -// @Test -// public void signin() throws Exception { -// // Member m = this.memberService.signin("overflow@loafle.com", "!@#$qwer1234"); -// // Assert.assertNotNull(m); -// } + @Test + public void signin() throws Exception { + // Member m = this.memberService.signin("overflow@loafle.com", "!@#$qwer1234"); + // Assert.assertNotNull(m); + } // @Test // public void readAllMemberByProbeKey() { - +// // List members = this.memberService.readAllByProbeKey("95d8bcdc739741dca74c4a0e489e0774"); - +// // Assert.assertNotEquals(members.size(), 0); - +// // } - +// // @Test // public void readAllMemberByApiKey() { - +// // List members = this.memberService.readAllByApiKey("52abd6fd57e511e7ac52080027658d13"); - +// // Assert.assertNotEquals(members.size(), 0); // } - +// // @Test // public void readAllMemberByDomain() { - +// // Domain domain = new Domain(); - +// // domain.setId(1); - +// // List members = this.memberService.readAllByDomain(domain); - +// // Assert.assertNotEquals(members.size(), 0); - +// // } - +// // @Test // public void TestPasswordStrong() { - +// // String arrpw[] = new String[]{"!@#$Qwer1234", "Zxasqw12!!","@Cosmos@5795"}; // // String pass = "!@#$Qwer1234"; - +// // boolean check = false; // for (int i = 0; i < arrpw.length; i++) { // check = this.memberService.isPasswordStrong(arrpw[i]); @@ -99,22 +99,22 @@ // } // } -// // -// // String regex = "(" + -// // "(?=.*[a-z])" + -// // "(?=.*\\d)" + -// // "(?=.*[A-Z])" + -// // "(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?])" + -// // "." + -// // "{6,40}" + -// // ")"; + // + // String regex = "(" + + // "(?=.*[a-z])" + + // "(?=.*\\d)" + + // "(?=.*[A-Z])" + + // "(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?])" + + // "." + + // "{6,40}" + + // ")"; -// // @Test -// // public void TestPassword() { -// // Pattern pattern = Pattern.compile(regex); -// // Matcher matcher = pattern.matcher("qwe1231@Q\\"); -// // System.out.println("Length"+"qwe1231@Q\\".length()); -// // -// // System.out.println("matcher.matches() = " + matcher.matches()); -// // } -// } + // @Test + // public void TestPassword() { + // Pattern pattern = Pattern.compile(regex); + // Matcher matcher = pattern.matcher("qwe1231@Q\\"); + // System.out.println("Length"+"qwe1231@Q\\".length()); + // + // System.out.println("matcher.matches() = " + matcher.matches()); + // } + } diff --git a/src/test/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryServiceTest.java b/src/test/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryServiceTest.java index 35686de..d535065 100644 --- a/src/test/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryServiceTest.java @@ -1,134 +1,144 @@ -// package com.loafle.overflow.central.module.target.service; + package com.loafle.overflow.central.module.target.service; -// import com.loafle.overflow.central.module.probe.model.Probe; -// import com.loafle.overflow.central.spring.AppConfigTest; -// import org.codehaus.jackson.map.DeserializationConfig; -// import org.codehaus.jackson.map.ObjectMapper; -// import org.junit.Ignore; -// import org.junit.Test; -// import org.junit.runner.RunWith; -// import org.springframework.aop.support.AopUtils; -// import org.springframework.beans.factory.annotation.Autowired; -// import org.springframework.core.io.ResourceLoader; -// import org.springframework.test.context.ContextConfiguration; -// import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -// import java.io.BufferedReader; -// import java.io.FileReader; -// import java.io.IOException; -// import java.lang.reflect.Method; -// import java.lang.reflect.ParameterizedType; -// import java.lang.reflect.Type; -// import java.util.List; + import com.google.gson.Gson; + import com.google.gson.reflect.TypeToken; + import com.loafle.overflow.central.spring.AppConfigTest; -// /** -// * Created by snoop on 17. 6. 28. -// */ + import com.loafle.overflow.model.discovery.Host; + import com.loafle.overflow.model.probe.Probe; + import com.loafle.overflow.service.central.target.TargetDiscoveryService; + import org.junit.Ignore; + import org.junit.Test; + import org.junit.runner.RunWith; + import org.springframework.aop.support.AopUtils; + import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.core.io.ResourceLoader; + import org.springframework.test.context.ContextConfiguration; + import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -// @RunWith(SpringJUnit4ClassRunner.class) -// @ContextConfiguration(classes = {AppConfigTest.class}) -// public class TargetDiscoveryServiceTest { + import java.io.BufferedReader; + import java.io.FileReader; + import java.io.IOException; + import java.lang.reflect.Method; + import java.lang.reflect.ParameterizedType; + import java.lang.reflect.Type; + import java.util.List; -// @Autowired -// private ResourceLoader resourceLoader; + /** + * Created by snoop on 17. 6. 28. + */ -// @Autowired -// private TargetDiscoveryService targetDiscoveryService; + @RunWith(SpringJUnit4ClassRunner.class) + @ContextConfiguration(classes = {AppConfigTest.class}) + public class TargetDiscoveryServiceTest { + + @Autowired + private ResourceLoader resourceLoader; + + @Autowired + private TargetDiscoveryService targetDiscoveryService; // @Ignore -// @Test -// public void saveAllTarget() throws Exception { + @Test + public void saveAllTarget() throws Exception { -// String json = readFileAsString(resourceLoader.getResource("classpath:dh.json").getURI().getPath()); + String json = readFileAsString(resourceLoader.getResource("classpath:2018-04-25-tds.json").getURI().getPath()); + + Gson gson = new Gson(); // ObjectMapper mapper = new ObjectMapper(); - +// // mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); // List hosts = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Host.class)); -// Probe probe = new Probe(); -// probe.setId(1); + Type listType = new TypeToken>(){}.getType(); -// // this.targetDiscoveryService.saveAllTarget(hosts, probe); -// } + List hosts = gson.fromJson(json, listType); -// private String readFileAsString(String filePath) throws IOException { -// StringBuffer fileData = new StringBuffer(); -// BufferedReader reader = new BufferedReader( -// new FileReader(filePath)); -// char[] buf = new char[1024]; -// int numRead=0; -// while((numRead=reader.read(buf)) != -1){ -// String readData = String.valueOf(buf, 0, numRead); -// fileData.append(readData); -// } -// reader.close(); -// return fileData.toString(); -// } + Probe probe = new Probe(); + probe.setId(1); + + this.targetDiscoveryService.saveAllTarget(hosts, probe); + } + + private String readFileAsString(String filePath) throws IOException { + StringBuffer fileData = new StringBuffer(); + BufferedReader reader = new BufferedReader( + new FileReader(filePath)); + char[] buf = new char[1024]; + int numRead=0; + while((numRead=reader.read(buf)) != -1){ + String readData = String.valueOf(buf, 0, numRead); + fileData.append(readData); + } + reader.close(); + return fileData.toString(); + } -// @Ignore -// @Test -// public void testParam() throws NoSuchMethodException { + @Ignore + @Test + public void testParam() throws NoSuchMethodException { -// Method m = this.targetDiscoveryService.getClass().getMethod("saveAllTarget", List.class, Probe.class); -// // Method m = TargetDiscoveryService.class.getMethod("saveAllTarget", List.class, Probe.class); + Method m = this.targetDiscoveryService.getClass().getMethod("saveAllTarget", List.class, Probe.class); + // Method m = TargetDiscoveryService.class.getMethod("saveAllTarget", List.class, Probe.class); -// Class clazz = AopUtils.getTargetClass(this.targetDiscoveryService); -// Method[] ms = clazz.getMethods(); + Class clazz = AopUtils.getTargetClass(this.targetDiscoveryService); + Method[] ms = clazz.getMethods(); -// Method sm = null; -// for(Method mm : ms){ -// if ("saveAllTarget".equals(mm.getName())) { -// sm = mm; -// break; -// } -// } + Method sm = null; + for(Method mm : ms){ + if ("saveAllTarget".equals(mm.getName())) { + sm = mm; + break; + } + } -// if (sm != null) { -// Type[] ts = sm.getGenericParameterTypes(); -// for(Type t : ts){ -// if (t instanceof ParameterizedType) { -// ParameterizedType pt = (ParameterizedType)t; -// System.out.println(pt.getActualTypeArguments()[0].getTypeName()); -// } -// System.out.println(t.getTypeName()); + if (sm != null) { + Type[] ts = sm.getGenericParameterTypes(); + for(Type t : ts){ + if (t instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType)t; + System.out.println(pt.getActualTypeArguments()[0].getTypeName()); + } + System.out.println(t.getTypeName()); -// } -// } + } + } -// System.out.println(m.getName()); -// Type[] genericParameterTypes = m.getGenericParameterTypes(); + System.out.println(m.getName()); + Type[] genericParameterTypes = m.getGenericParameterTypes(); -// for(Type genericParameterType : genericParameterTypes){ -// if(genericParameterType instanceof ParameterizedType){ -// ParameterizedType aType = (ParameterizedType) genericParameterType; -// Type[] parameterArgTypes = aType.getActualTypeArguments(); -// for(Type parameterArgType : parameterArgTypes){ -// Class parameterArgClass = (Class) parameterArgType; -// System.out.println("parameterArgClass = " + parameterArgClass); -// } -// } -// } -// } + for(Type genericParameterType : genericParameterTypes){ + if(genericParameterType instanceof ParameterizedType){ + ParameterizedType aType = (ParameterizedType) genericParameterType; + Type[] parameterArgTypes = aType.getActualTypeArguments(); + for(Type parameterArgType : parameterArgTypes){ + Class parameterArgClass = (Class) parameterArgType; + System.out.println("parameterArgClass = " + parameterArgClass); + } + } + } + } -// @Ignore -// @Test -// public void testName() throws ClassNotFoundException { + @Ignore + @Test + public void testName() throws ClassNotFoundException { -// Object o = this.targetDiscoveryService; + Object o = this.targetDiscoveryService; -// String nnn = o.getClass().getSimpleName(); -// System.out.println(nnn); + String nnn = o.getClass().getSimpleName(); + System.out.println(nnn); -// } + } -// } \ No newline at end of file + } \ No newline at end of file