Skip to content
This repository has been archived by the owner on Jan 21, 2021. It is now read-only.

MK: Fixed failing test cases; Added ssh support; #118

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions opensrp-connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@
<artifactId>atomfeed-client</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.54</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-ext-jdk15on</artifactId>
<version>1.54</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.opensrp.common.util.HttpResponse;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -38,7 +53,7 @@ public HttpUtil() {
*/
public static HttpResponse post(String url, String payload, String data, String username,String password) {
try {
HttpURLConnection con = makeConnection(url, payload, HttpMethod.POST, true, username, password);
HttpsURLConnection con = makeConnection(url, payload, HttpMethod.POST, true, username, password);
con.setDoOutput(true);
con.setRequestProperty("Content-Type", "application/json");
String charset = "utf-8";
Expand Down Expand Up @@ -66,7 +81,7 @@ public static HttpResponse post(String url, String payload, String data, String
*/
public static HttpResponse get(String url, String payload, String username, String password) {
try {
HttpURLConnection con = makeConnection(url, payload, HttpMethod.GET, true, username, password);
HttpsURLConnection con = makeConnection(url, payload, HttpMethod.GET, true, username, password);
System.out.println(url);
HttpResponse resp = new HttpResponse(con.getResponseCode() == HttpStatus.SC_OK, IOUtils.toString(con.getInputStream()));
System.out.println(resp);
Expand All @@ -83,15 +98,15 @@ public static HttpResponse get(String url, String payload, String username, Stri
}
}

static HttpURLConnection makeConnection(String url, String payload, HttpMethod requestMethod, boolean useBasicAuth, String username, String password) throws IOException {
static HttpsURLConnection makeConnection(String url, String payload, HttpMethod requestMethod, boolean useBasicAuth, String username, String password) throws IOException {
String charset = "UTF-8";

if(url.endsWith("/")){
url = url.substring(0, url.lastIndexOf("/"));
}
url = (url+(StringUtils.isEmptyOrWhitespaceOnly(payload)?"":("?"+payload))).replaceAll(" ", "%20");
URL urlo = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlo.openConnection();
HttpsURLConnection conn = (HttpsURLConnection) urlo.openConnection();
conn.setRequestProperty("Accept-Charset", charset);

if(useBasicAuth){
Expand All @@ -110,4 +125,51 @@ public static String removeEndingSlash(String str){
public static String removeTrailingSlash(String str){
return str.startsWith("/")?str.substring(1):str;
}

static {
disableSslVerification();
}

private static void disableSslVerification() {
System.setProperty("disable_bad_sslciphers", "yes");
System.setProperty("jsse.enableSNIExtension", "false");
Security.addProvider(new BouncyCastleProvider());

try
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
}
};

// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory sf = sc.getSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(new SecureSocketFactory(sf));

// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};

// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.opensrp.connector;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.List;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

public class SecureSocketFactory extends SSLSocketFactory {

private final SSLSocketFactory delegate;

public SecureSocketFactory(SSLSocketFactory delegate) {

this.delegate = delegate;
}

@Override
public String[] getDefaultCipherSuites() {

return this.delegate.getDefaultCipherSuites();
}

@Override
public String[] getSupportedCipherSuites() {

return this.delegate.getSupportedCipherSuites();
}

@Override
public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException {
Socket socket = this.delegate.createSocket(arg0, arg1);
return handleSocket(socket);
}

private Socket handleSocket(Socket socket){
List<String> limited = new LinkedList<String>();
for (String suite : ((SSLSocket) socket).getEnabledCipherSuites()) {
if (!suite.contains("_ECDHE_") && !suite.contains("_DH_") && !suite.contains("_DHE_")) {
limited.add(suite);
}
}
((SSLSocket) socket).setEnabledCipherSuites(limited.toArray(new String[limited.size()]));
return socket;
}

@Override
public Socket createSocket(InetAddress arg0, int arg1) throws IOException {
Socket socket = this.delegate.createSocket(arg0, arg1);
return handleSocket(socket);
}

@Override
public Socket createSocket(Socket arg0, String arg1, int arg2, boolean arg3) throws IOException {
Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);
return handleSocket(socket);
}

@Override
public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException {
Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);
return handleSocket(socket);
}

@Override
public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException {
Socket socket = this.delegate.createSocket(arg0, arg1, arg2, arg3);
return handleSocket(socket);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public synchronized Event addEvent(Event event)
}

event.setDateCreated(new Date());
event.setDateEdited(new Date());
if(StringUtils.isEmptyOrWhitespaceOnly(event.getEventId())){
event.setEventId(System.currentTimeMillis()+"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static void main(String[] args) {
// } catch (IOException e) {
// e.printStackTrace();
// }

System.out.println(DateTime.now().toString("dd-MM-yyyy"));
}

public boolean downloadFormFiles(String directory,String username ,String formPath, String password,String formId, String formPk) throws IOException{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package org.opensrp.service.autosys;

import static java.text.MessageFormat.format;
import static java.util.Collections.sort;
import static org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace;

import java.text.MessageFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import org.joda.time.DateTime;
import org.motechproject.scheduler.domain.MotechEvent;
import org.motechproject.server.event.annotations.MotechListener;
import org.opensrp.common.AllConstants;
import org.opensrp.domain.AppStateToken;
import org.opensrp.domain.ErrorTrace;
import org.opensrp.domain.Event;
import org.opensrp.dto.form.FormSubmissionDTO;
import org.opensrp.form.domain.FormSubmission;
import org.opensrp.form.service.FormSubmissionService;
import org.opensrp.service.ClientService;
import org.opensrp.service.ConfigService;
import org.opensrp.service.ErrorTraceService;
import org.opensrp.service.EventService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Component
public class DataUpdateListener {
private static Logger logger = LoggerFactory.getLogger(DataUpdateListener.class.toString());
private static final ReentrantLock lock = new ReentrantLock();
private ClientService clientService;
private EventService eventService;
private ConfigService configService;
private FormSubmissionProcessor fsp;
private ErrorTraceService errorTraceService;

@Autowired
public DataUpdateListener(ClientService clientService, EventService eventService,
FormSubmissionProcessor fsp,
ConfigService configService, ErrorTraceService errorTraceService) {
this.clientService = clientService;
this.eventService = eventService;
this.configService = configService;
this.errorTraceService = errorTraceService;
this.fsp = fsp;
this.configService.registerAppStateToken(AllConstants.Config.FORM_ENTITY_PARSER_LAST_SYNCED_FORM_SUBMISSION,
0, "Token to keep track of forms processed for client n event parsing and schedule handling", true);
}

@MotechListener(subjects = AllConstants.FORM_SCHEDULE_SUBJECT)
public void parseForms(MotechEvent event) {
if (!lock.tryLock()) {
logger.warn("Not fetching forms from Message Queue. It is already in progress.");
return;
}
try {
logger.info("Fetching Forms");
long version = getVersion();

List<Event> events = eventService.findEventsBy(null, null, null, null, null, null, null, new DateTime(version) , DateTime.now());
if (events.isEmpty()) {
logger.info("No new events found. Export token: " + version);
return;
}

logger.info(format("Fetched {0} new forms found. Export token: {1}", events.size(), version));

sort(events, serverVersionComparator());

for (Event e : events) {
try{
logger.info(format("Invoking save for form with instance Id: {0} and for entity Id: {1}", submission.instanceId(), submission.entityId()));

if(submission.getField("no_client_event") == null || submission.getField("no_client_event").contains("false")){
fsp.processFormSubmission(submission);
}

configService.updateAppStateToken(AllConstants.Config.FORM_ENTITY_PARSER_LAST_SYNCED_FORM_SUBMISSION, submission.serverVersion());
}
catch(Exception e){
e.printStackTrace();
errorTraceService.addError(new ErrorTrace(new Date(), "FormSubmissionProcessor", this.getClass().getName(), e.getStackTrace().toString(), "unsolved", FormSubmission.class.getName()));
}
}
} catch (Exception e) {
logger.error(MessageFormat.format("{0} occurred while trying to fetch forms. Message: {1} with stack trace {2}",
e.toString(), e.getMessage(), getFullStackTrace(e)));
} finally {
lock.unlock();
}
}

private long getVersion() {
AppStateToken token = configService.getAppStateTokenByName(AllConstants.Config.FORM_ENTITY_PARSER_LAST_SYNCED_FORM_SUBMISSION);
return token==null?0L:token.longValue();
}

private Comparator<Event> serverVersionComparator() {
return new Comparator<Event>() {
public int compare(Event first, Event second) {
long firstTimestamp = first.getDateCreated().getTime();
long secondTimestamp = second.getDateCreated().getTime();
return firstTimestamp == secondTimestamp ? 0 : firstTimestamp < secondTimestamp ? -1 : 1;
}
};
}
}
Loading