diff --git a/gradle/settings.gradle b/gradle/settings.gradle index 348b14f5ad7954318552954f9151fa8dd804aa7a..0f4799502f81df5188fb80752a4dc1030aa58c98 100644 --- a/gradle/settings.gradle +++ b/gradle/settings.gradle @@ -1,5 +1,5 @@ includeFlat 'commonbase', 'common', 'openbis_api', 'openbis-common', 'authentication', 'dbmigration', 'openbis', 'datastore_server', 'screening', 'deep_sequencing_unit', 'rtd_yeastx', 'openbis_standard_technologies', 'installation', 'image_readers', 'ui-test', 'js-test', 'datamover', - 'plasmid', 'rtd_cina', 'openbis_oai_pmh', 'big_data_link_server', 'openbis_ng_ui', 'microservice_server_template' - + 'plasmid', 'rtd_cina', 'openbis_oai_pmh', 'big_data_link_server', 'openbis_ng_ui', 'microservice_server_template', + 'openbis-monitor-tool' diff --git a/openbis-monitor-tool/.gitignore b/openbis-monitor-tool/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..0112423af107f951e0b220b2d2b4c61fae7708b6 --- /dev/null +++ b/openbis-monitor-tool/.gitignore @@ -0,0 +1,6 @@ +/build/ +/bin/ +/out/ +/.idea/ +*.iml +*.eml \ No newline at end of file diff --git a/openbis-monitor-tool/README.md b/openbis-monitor-tool/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e912d0fc09a02e1510e89b0b5aa649c7d91171c8 --- /dev/null +++ b/openbis-monitor-tool/README.md @@ -0,0 +1,11 @@ +# MICROSERVICE SERVER # + +## Introduction ## + +This project is supposed to be used to monitor openBIS. + +## Build ## + +./gradlew distZip + +The build will be found at ./build/distributions/openbis_monitor_tool.zip \ No newline at end of file diff --git a/openbis-monitor-tool/build.gradle b/openbis-monitor-tool/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..36fc50ed62f00965898de027d0d3fc6773c15cfd --- /dev/null +++ b/openbis-monitor-tool/build.gradle @@ -0,0 +1,13 @@ +apply plugin: 'java' +apply plugin: 'application' + +repositories { + ivy { + ivyPattern "http://svnsis.ethz.ch/repos/cisd/ivy-repository/trunk/[organisation]/[module]/[revision]/ivy.xml" + artifactPattern "http://svnsis.ethz.ch/repos/cisd/ivy-repository/trunk/[organisation]/[module]/[revision]/[artifact]-[revision](-[classifier]).[ext]" + } +} + +dependencies { + compile 'openbis:openbis-v3-api-batteries-included:20.10.0' +} diff --git a/openbis-monitor-tool/src/main/java/ch/ethz/sis/monitor/Monitor.java b/openbis-monitor-tool/src/main/java/ch/ethz/sis/monitor/Monitor.java new file mode 100644 index 0000000000000000000000000000000000000000..8c576c1cbd7c08b0cd16b3ca09ce563c6f5f2c5b --- /dev/null +++ b/openbis-monitor-tool/src/main/java/ch/ethz/sis/monitor/Monitor.java @@ -0,0 +1,67 @@ +package ch.ethz.sis.monitor; +import java.util.Arrays; +import java.util.Map; + +import ch.ethz.sis.openbis.generic.asapi.v3.IApplicationServerApi; +import ch.ethz.sis.openbis.generic.asapi.v3.dto.common.search.SearchResult; +import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.DataSet; +import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.fetchoptions.DataSetFetchOptions; +import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.id.DataSetPermId; +import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.id.IDataSetId; +import ch.ethz.sis.openbis.generic.dssapi.v3.IDataStoreServerApi; +import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.DataSetFile; +import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.fetchoptions.DataSetFileFetchOptions; +import ch.ethz.sis.openbis.generic.dssapi.v3.dto.datasetfile.search.DataSetFileSearchCriteria; +import ch.systemsx.cisd.common.spring.HttpInvokerUtils; + +public class Monitor +{ + private static IApplicationServerApi v3 = null; + private static IDataStoreServerApi dssv3 = null; + + public static void main(String... args) { + String sessionToken = null; + try { + if(args.length != 5) { + System.out.println("Usage example: java -jar openbis-monitor-tool.jar URL TIMEOUT userId password datasetPermId"); + return; + } + // get a reference to API + if(v3 == null) { + if (args[0].startsWith("https")) { + SslCertificateHelper.trustAnyCertificate(args[0]); + } + v3 = HttpInvokerUtils.createServiceStub(IApplicationServerApi.class, args[0] + "openbis/openbis" + IApplicationServerApi.SERVICE_URL, Integer.parseInt(args[1])); + } + if(dssv3 == null) { + if (args[0].startsWith("https")) { + SslCertificateHelper.trustAnyCertificate(args[0]); + } + dssv3 = HttpInvokerUtils.createServiceStub(IDataStoreServerApi.class, args[0] + "datastore_server" + IDataStoreServerApi.SERVICE_URL, Integer.parseInt(args[1])); + } + // login to obtain a session token + sessionToken = v3.login(args[2], args[3]); + if(sessionToken == null) { + System.out.println(Boolean.FALSE); + return; + } + boolean isSessionActive = v3.isSessionActive(sessionToken); + + System.out.println("sessionToken: " + sessionToken); + System.out.println("isSessionActive: " + isSessionActive); + + + // Search for the dataset + Map<IDataSetId, DataSet> results = v3.getDataSets(sessionToken, Arrays.asList(new DataSetPermId(args[4])), new DataSetFetchOptions()); + + DataSetFileSearchCriteria criteria = new DataSetFileSearchCriteria(); + criteria.withDataSet().withCode().thatEquals(args[4]); + SearchResult<DataSetFile> files = dssv3.searchFiles(sessionToken, criteria, new DataSetFileFetchOptions()); + + System.out.println(!results.isEmpty() && files.getTotalCount() > 0); + v3.logout(sessionToken); + } catch(Exception ex) { + throw new RuntimeException(ex); + } + } +} diff --git a/openbis-monitor-tool/src/main/java/ch/ethz/sis/monitor/MonitorTest.java b/openbis-monitor-tool/src/main/java/ch/ethz/sis/monitor/MonitorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8053412c19a37ff63148bf315a1b24ade9ca33ea --- /dev/null +++ b/openbis-monitor-tool/src/main/java/ch/ethz/sis/monitor/MonitorTest.java @@ -0,0 +1,13 @@ +package ch.ethz.sis.monitor; + +public class MonitorTest +{ + public static void main(String[] args) { + String URL = "https://openbis-eln-peter.ethz.ch/"; + String TIMEOUT = "30000"; + String user = "admin"; + String password = "changeit"; + String datasetPermId = "20150407224113301-26972"; + Monitor.main(URL, TIMEOUT, user, password, datasetPermId); + } +} diff --git a/openbis-monitor-tool/src/main/java/ch/ethz/sis/monitor/SslCertificateHelper.java b/openbis-monitor-tool/src/main/java/ch/ethz/sis/monitor/SslCertificateHelper.java new file mode 100755 index 0000000000000000000000000000000000000000..a42485230a11fc6dc2227088ca9d5607d0a376eb --- /dev/null +++ b/openbis-monitor-tool/src/main/java/ch/ethz/sis/monitor/SslCertificateHelper.java @@ -0,0 +1,216 @@ +package ch.ethz.sis.monitor; + + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.net.URL; +import java.security.KeyStore; +import java.security.cert.Certificate; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +/** + * Helper class for retrieving and locally storing SSL certificates from a server. + * + * @author Chandrasekhar Ramakrishnan + */ +public class SslCertificateHelper +{ + private final String serviceURL; + + private final File keystoreFile; + + private final String certificateEntryName; + + /** + * Create a helper that retrieves a certificate from the serviceURL, and stores it in a keystore file with the name "keystore" in the + * configDirectory. + * + * @param serviceURL The URL to retrieve the certificate from. + * @param keystoreFile The file where to store the certificate in. + * @param certificateEntryName The name in the keystore the certificate is stored under. + */ + public SslCertificateHelper(String serviceURL, File keystoreFile, String certificateEntryName) + { + this.serviceURL = serviceURL; + this.keystoreFile = keystoreFile; + this.certificateEntryName = certificateEntryName; + } + + public void setUpKeyStore() + { + if (serviceURL.startsWith("https")) + { + Certificate[] certificates = getServerCertificate(); + KeyStore keyStore; + try + { + keyStore = KeyStore.getInstance("JKS"); + keyStore.load(null, null); + for (int i = 0; i < certificates.length; i++) + { + keyStore.setCertificateEntry(certificateEntryName + i, certificates[i]); + } + } catch (Exception ex) + { + throw new RuntimeException(ex); + } + FileOutputStream fileOutputStream = null; + try + { + fileOutputStream = new FileOutputStream(keystoreFile); + keyStore.store(fileOutputStream, "changeit".toCharArray()); + fileOutputStream.close(); + System.setProperty("javax.net.ssl.trustStore", keystoreFile.getAbsolutePath()); + } catch (Exception ex) + { + throw new RuntimeException(ex); + } finally + { + closeQuietly(fileOutputStream); + } + } + } + + private static void closeQuietly(OutputStream output) + { + try + { + if (output != null) + { + output.close(); + } + } catch (IOException ioe) + { + // ignore + } + } + + private Certificate[] getServerCertificate() + { + workAroundABugInJava6(); + + // Create a trust manager that does not validate certificate chains + setUpAllAcceptingTrustManager(); + setUpAllAcceptingHostNameVerifier(); + SSLSocket socket = null; + try + { + URL url = new URL(serviceURL); + int port = url.getPort(); + if (port == -1) + { + port = 443; // standard port for https + } + String hostname = url.getHost(); + SSLSocketFactory factory = HttpsURLConnection.getDefaultSSLSocketFactory(); + socket = (SSLSocket) factory.createSocket(hostname, port); + socket.startHandshake(); + return socket.getSession().getPeerCertificates(); + } catch (Exception e) + { + throw new RuntimeException(e); + } finally + { + if (socket != null) + { + try + { + socket.close(); + } catch (IOException ex) + { + // ignored + } + } + } + } + + private void setUpAllAcceptingTrustManager() + { + TrustManager[] trustAllCerts = new TrustManager[] + { new X509TrustManager() + { + @Override + public java.security.cert.X509Certificate[] getAcceptedIssuers() + { + return null; + } + + @Override + public void checkClientTrusted(java.security.cert.X509Certificate[] certs, + String authType) + { + } + + @Override + public void checkServerTrusted(java.security.cert.X509Certificate[] certs, + String authType) + { + } + } }; + + // Install the all-trusting trust manager + try + { + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + } catch (Exception e) + { + } + } + + private void setUpAllAcceptingHostNameVerifier() + { + HostnameVerifier acceptAllHostNames = new HostnameVerifier() + { + @Override + public boolean verify(String hostname, SSLSession session) + { + return true; + } + }; + HttpsURLConnection.setDefaultHostnameVerifier(acceptAllHostNames); + } + + // WORKAROUND: see comment submitted on 31-JAN-2008 for + // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6514454 + private void workAroundABugInJava6() + { + try + { + SSLContext.getInstance("SSL").createSSLEngine(); + } catch (Exception ex) + { + // Ignore this one. + } + } + + public static void trustAnyCertificate(String url) + { + if (url.startsWith("https://")) + { + try + { + // The windows server coudn't create a temp file automatically without indicating the folder + //File tempKeyStore = File.createTempFile("cert", "keystore"); + File tempKeyStore = File.createTempFile("cert", "keystore"); + tempKeyStore.deleteOnExit(); + SslCertificateHelper helper = new SslCertificateHelper(url, tempKeyStore, "cert"); + helper.setUpKeyStore(); + } catch (IOException ioex) + { + throw new RuntimeException(ioex); + } + } + } +} \ No newline at end of file