From 4ed4e2a225ac01803597ed1c23d643b29a29c007 Mon Sep 17 00:00:00 2001 From: Mike Duff Date: Fri, 23 Aug 2013 12:53:36 -0500 Subject: [PATCH] Issue #2180 - Pass messages back to CAVE Change-Id: Ia5b88d577fc51b449a77389354e14808515e67bc Former-commit-id: 5f462ebf6fd42e9f291b99d1d28954cc58a14920 [formerly 37de8f85f870ca494e5a641967f544e359df77b9] Former-commit-id: 20e6396c48f00f3625c20350e026f302472f2fb8 --- .../system/DataProviderPasswordComposite.java | 106 +++++++------ .../datadelivery/registry/Encryption.java | 52 ++++--- .../registry/ProviderCredentials.java | 146 ++++++++++++++++++ .../registry/ProviderKeyRequest.java | 23 +++ .../util/ProviderCredentialsUtil.java | 70 ++++++--- .../retrieval/util/WfsConnectionUtil.java | 82 +++++----- .../services/ProviderKeyRequestHandler.java | 49 ++++-- .../datadelivery/registry/EncryptionTest.java | 112 ++++++++++++++ 8 files changed, 497 insertions(+), 143 deletions(-) create mode 100644 edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/ProviderCredentials.java create mode 100644 tests/unit/com/raytheon/uf/common/datadelivery/registry/EncryptionTest.java diff --git a/cave/com.raytheon.uf.viz.datadelivery/src/com/raytheon/uf/viz/datadelivery/system/DataProviderPasswordComposite.java b/cave/com.raytheon.uf.viz.datadelivery/src/com/raytheon/uf/viz/datadelivery/system/DataProviderPasswordComposite.java index 186f0e5b41..61c907eff1 100644 --- a/cave/com.raytheon.uf.viz.datadelivery/src/com/raytheon/uf/viz/datadelivery/system/DataProviderPasswordComposite.java +++ b/cave/com.raytheon.uf.viz.datadelivery/src/com/raytheon/uf/viz/datadelivery/system/DataProviderPasswordComposite.java @@ -51,6 +51,7 @@ import com.raytheon.uf.common.serialization.comm.RequestRouter; import com.raytheon.uf.common.status.IUFStatusHandler; import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.common.status.UFStatus.Priority; +import com.raytheon.uf.viz.datadelivery.utils.DataDeliveryUtils; import com.raytheon.viz.ui.widgets.ApplyCancelComposite; import com.raytheon.viz.ui.widgets.IApplyCancelAction; @@ -86,9 +87,6 @@ public class DataProviderPasswordComposite extends Composite implements /** Username text field */ private Text userTxt; - /** Encryption type combo box */ - private Combo encryptionCbo; - /** Button composite */ private ApplyCancelComposite buttonComp; @@ -139,6 +137,7 @@ public class DataProviderPasswordComposite extends Composite implements @Override public void widgetSelected(SelectionEvent e) { handleProviderSelection(); + checkUserInput(); } }); @@ -153,12 +152,12 @@ public class DataProviderPasswordComposite extends Composite implements userTxt.addKeyListener(new KeyListener() { @Override public void keyReleased(KeyEvent e) { - checkTextFields(); + checkUserInput(); } @Override public void keyPressed(KeyEvent e) { - checkTextFields(); + checkUserInput(); } }); @@ -174,12 +173,12 @@ public class DataProviderPasswordComposite extends Composite implements passTxt.addKeyListener(new KeyListener() { @Override public void keyReleased(KeyEvent e) { - checkTextFields(); + checkUserInput(); } @Override public void keyPressed(KeyEvent e) { - checkTextFields(); + checkUserInput(); } }); @@ -206,12 +205,12 @@ public class DataProviderPasswordComposite extends Composite implements keyTxt.addKeyListener(new KeyListener() { @Override public void keyReleased(KeyEvent e) { - checkTextFields(); + checkUserInput(); } @Override public void keyPressed(KeyEvent e) { - checkTextFields(); + checkUserInput(); } }); @@ -229,14 +228,6 @@ public class DataProviderPasswordComposite extends Composite implements } }); - Label encryptionLabel = new Label(comp, SWT.NONE); - encryptionLabel.setText("Encryption Type:"); - - encryptionCbo = new Combo(comp, SWT.READ_ONLY); - encryptionCbo.add(Encryption.Algorithim.AES.toString()); - encryptionCbo.add(Encryption.Algorithim.DES.toString()); - encryptionCbo.select(0); - // Buttons buttonComp = new ApplyCancelComposite(this, SWT.NONE, this); @@ -262,11 +253,17 @@ public class DataProviderPasswordComposite extends Composite implements /** * Check the text fields to determine of buttons should be enabled or not */ - private void checkTextFields() { + private boolean validation() { if ((!userTxt.getText().isEmpty() && !keyTxt.getText().isEmpty() && !passTxt .getText().isEmpty()) && providerCombo.getSelectionIndex() > -1) { - buttonComp.enableButtons(true); + return true; } + + return false; + } + + private void checkUserInput() { + buttonComp.enableButtons(validation()); } /** @@ -314,27 +311,44 @@ public class DataProviderPasswordComposite extends Composite implements */ @Override public boolean apply() { - ProviderKeyRequest req = new ProviderKeyRequest(); - Connection conn = provider.getConnection(); - conn.setPassword(passTxt.getText()); - conn.setUserName(userTxt.getText()); - conn.setProviderKey(keyTxt.getText()); - conn.setEncryption(getEncryption(encryptionCbo.getText())); - provider.setConnection(conn); - req.setProvider(provider); - req.setRequestType(RequestType.SAVE); - req.setProviderKey(keyTxt.getText()); + if (validation()) { + ProviderKeyRequest req = new ProviderKeyRequest(); + Connection conn = provider.getConnection(); + conn.setPassword(passTxt.getText()); + conn.setUserName(userTxt.getText()); + conn.setProviderKey(keyTxt.getText()); + conn.setEncryption(getEncryption()); + provider.setConnection(conn); + req.setProvider(provider); + req.setRequestType(RequestType.SAVE); + req.setProviderKey(keyTxt.getText()); - ProviderKeyRequest resp; - Status status = Status.FAILURE; - try { - resp = (ProviderKeyRequest) RequestRouter.route(req, - RegistryConstants.EBXML_REGISTRY_SERVICE); - status = resp.getStatus(); - } catch (Exception e) { - statusHandler.handle(Priority.PROBLEM, e.getLocalizedMessage(), e); + ProviderKeyRequest resp; + Status status = Status.FAILURE; + try { + resp = (ProviderKeyRequest) RequestRouter.route(req, + RegistryConstants.EBXML_REGISTRY_SERVICE); + status = resp.getStatus(); + if (status == Status.FAILURE) { + statusHandler.error(resp.getMessage()); + } + } catch (Exception e) { + statusHandler.handle(Priority.PROBLEM, e.getLocalizedMessage(), + e); + return false; + } + + if (status == Status.SUCCESS) { + DataDeliveryUtils.showMessage(getShell(), SWT.OK, + "Change Successful", + "The username/password has been updated."); + } + + return status == Status.SUCCESS; } - return status == Status.SUCCESS; + + return false; + } /** @@ -344,18 +358,10 @@ public class DataProviderPasswordComposite extends Composite implements * The encryption algorithm * @return The Encryption object */ - private Encryption getEncryption(String text) { - Encryption enc = null; - if (text.equals(Encryption.Algorithim.AES.toString())) { - enc = new Encryption(); - enc.setAlgorithim(Algorithim.AES); - enc.setPadding(Padding.AES); - } - if (text.equals(Encryption.Algorithim.DES.toString())) { - enc = new Encryption(); - enc.setAlgorithim(Algorithim.DES); - enc.setPadding(Padding.DES); - } + private Encryption getEncryption() { + Encryption enc = new Encryption(); + enc.setAlgorithim(Algorithim.AES); + enc.setPadding(Padding.AES); return enc; } diff --git a/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/Encryption.java b/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/Encryption.java index 54dec974b4..b57f563ee5 100644 --- a/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/Encryption.java +++ b/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/Encryption.java @@ -39,7 +39,7 @@ import com.raytheon.uf.common.serialization.annotations.DynamicSerializeElement; @DynamicSerialize public class Encryption { - // entropy + // entropy input vector length private static final int IV_LENGTH = 16; private IvParameterSpec ivSpec; @@ -58,6 +58,9 @@ public class Encryption { @DynamicSerializeElement public Padding padding; + /** + * Constructor. + */ public Encryption() { } @@ -66,15 +69,11 @@ public class Encryption { public enum Algorithim { // CLEAR, No encryption // AES, AES encryption - // DES, DES encryption @XmlEnumValue(Algorithim.aes) - AES("AES"), @XmlEnumValue(Algorithim.des) - DES("DES"); + AES("AES"); private static final String aes = "AES"; - private static final String des = "DES"; - private final String algo; private Algorithim(String name) { @@ -91,15 +90,11 @@ public class Encryption { public enum Padding { // CLEAR, No encryption // AES, AES encryption - // DES, DES encryption @XmlEnumValue(Padding.aes_pad) - AES("AES/CFB8/NoPadding"), @XmlEnumValue(Padding.des_pad) - DES("DES/CBC/PKCS5Padding"); + AES("AES/CFB8/NoPadding"); private static final String aes_pad = "AES/CFB8/NoPadding"; - private static final String des_pad = "DES/CBC/PKCS5Padding"; - private final String padd; private Padding(String name) { @@ -112,18 +107,32 @@ public class Encryption { } } + /** + * @return algorithm + */ public Algorithim getAlgorithim() { return algorithim; } + /** + * @param algorithim + * the algorithim to set + */ public void setAlgorithim(Algorithim algorithim) { this.algorithim = algorithim; } + /** + * @return the padding + */ public Padding getPadding() { return padding; } + /** + * @param padding + * the padding to set + */ public void setPadding(Padding padding) { this.padding = padding; } @@ -139,13 +148,15 @@ public class Encryption { byte[] keyBytes = null; MessageDigest sha = MessageDigest.getInstance("SHA-1"); keyBytes = sha.digest(Base64.decodeBase64(sharedKey)); - keyBytes = Arrays.copyOf(keyBytes, 16); // use only first 128 bit + keyBytes = Arrays.copyOf(keyBytes, IV_LENGTH); // use only first 128 bit byte[] ivBytes = new byte[IV_LENGTH]; ivSpec = new IvParameterSpec(ivBytes); - // create the cipher with the algorithm you choose - // see javadoc for Cipher class for more info, e.g. + /* + * create the cipher with the algorithm you choose see javadoc for + * Cipher class for more info, e.g. + */ key = new SecretKeySpec(keyBytes, getAlgorithim().algo); deCipher = Cipher.getInstance(getPadding().padd); enCipher = Cipher.getInstance(getPadding().padd); @@ -155,12 +166,14 @@ public class Encryption { * Encrypts a string using AES/DES encoding * * @param sharedKey + * The encryption key * @param password - * @return + * The string to encrypt + * @return The encrypted string * @throws Exception + * on error */ - public String encrypt(String sharedKey, String password) - throws Exception { + public String encrypt(String sharedKey, String password) throws Exception { setupCipher(sharedKey); enCipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); @@ -172,9 +185,12 @@ public class Encryption { * Decrypts an AES/DES encoded string * * @param sharedKey + * The encryption key * @param encryptedPass - * @return + * The encrypted string to decrypt + * @return The encrypted string * @throws Exception + * on error */ public String decrypt(String sharedKey, String encryptedPass) throws Exception { diff --git a/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/ProviderCredentials.java b/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/ProviderCredentials.java new file mode 100644 index 0000000000..2718397929 --- /dev/null +++ b/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/ProviderCredentials.java @@ -0,0 +1,146 @@ +/** + * This software was developed and / or modified by Raytheon Company, + * pursuant to Contract DG133W-05-CQ-1067 with the US Government. + * + * U.S. EXPORT CONTROLLED TECHNICAL DATA + * This software product contains export-restricted data whose + * export/transfer/disclosure is restricted by U.S. law. Dissemination + * to non-U.S. persons whether in the United States or abroad requires + * an export license or other authorization. + * + * Contractor Name: Raytheon Company + * Contractor Address: 6825 Pine Street, Suite 340 + * Mail Stop B8 + * Omaha, NE 68106 + * 402.291.0100 + * + * See the AWIPS II Master Rights File ("Master Rights File.pdf") for + * further licensing information. + **/ +package com.raytheon.uf.common.datadelivery.registry; + +import com.raytheon.uf.common.datadelivery.registry.ProviderKeyRequest.Status; + +/** + * Credentials Data object. + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * Aug 22, 2013   2180     mpduff      Initial creation
+ * 
+ * 
+ * + * @author mpduff + * @version 1.0 + */ + +public class ProviderCredentials { + private Provider provider; + + private String providerKey; + + private String message; + + private Status status; + + private Connection connection; + + /** + * Constructor. + * + * @param provider + * The data provider object + * @param providerKey + * The provider encryption key + */ + public ProviderCredentials(Provider provider, String providerKey) { + this.providerKey = providerKey; + this.provider = provider; + } + + /** + * Default constructor. + */ + public ProviderCredentials() { + + } + + /** + * @return the connection + */ + public Connection getConnection() { + return connection; + } + + /** + * @param connection + * the connection to set + */ + public void setConnection(Connection connection) { + this.connection = connection; + } + + /** + * @return the message + */ + public String getMessage() { + return message; + } + + /** + * @param message + * the message to set + */ + public void setMessage(String message) { + this.message = message; + } + + /** + * @return the status + */ + public Status getStatus() { + return status; + } + + /** + * @param status + * the status to set + */ + public void setStatus(Status status) { + this.status = status; + } + + /** + * @return the provider + */ + public Provider getProvider() { + return provider; + } + + /** + * @param provider + * the provider to set + */ + public void setProvider(Provider provider) { + this.provider = provider; + } + + /** + * @return the providerKey + */ + public String getProviderKey() { + return providerKey; + } + + /** + * @param providerKey + * the providerKey to set + */ + public void setProviderKey(String providerKey) { + this.providerKey = providerKey; + } +} \ No newline at end of file diff --git a/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/ProviderKeyRequest.java b/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/ProviderKeyRequest.java index 6ab3bca40d..a9777eeb85 100644 --- a/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/ProviderKeyRequest.java +++ b/edexOsgi/com.raytheon.uf.common.datadelivery.registry/src/com/raytheon/uf/common/datadelivery/registry/ProviderKeyRequest.java @@ -38,6 +38,8 @@ import com.raytheon.uf.common.serialization.comm.IServerRequest; * ------------ ---------- ----------- -------------------------- * Jul 14, 2013 2184 dhladky Initial creation. * Aug 08, 2013 2180 mpduff Made serializable. + * Aug 23, 2013 2180 mpduff Added message. + * * @author dhladky * @version 1.0 */ @@ -57,6 +59,9 @@ public class ProviderKeyRequest implements IServerRequest { @DynamicSerializeElement private Status status; + @DynamicSerializeElement + private String message; + public ProviderKeyRequest() { } @@ -176,4 +181,22 @@ public class ProviderKeyRequest implements IServerRequest { this.status = status; } + /** + * Set the status message + * + * @param message + * The status message + */ + public void setMessage(String message) { + this.message = message; + } + + /** + * Get the status message. + * + * @return The status message + */ + public String getMessage() { + return this.message; + } } diff --git a/edexOsgi/com.raytheon.uf.edex.datadelivery.retrieval/src/com/raytheon/uf/edex/datadelivery/retrieval/util/ProviderCredentialsUtil.java b/edexOsgi/com.raytheon.uf.edex.datadelivery.retrieval/src/com/raytheon/uf/edex/datadelivery/retrieval/util/ProviderCredentialsUtil.java index d09a0ff0f8..8c205e82bb 100644 --- a/edexOsgi/com.raytheon.uf.edex.datadelivery.retrieval/src/com/raytheon/uf/edex/datadelivery/retrieval/util/ProviderCredentialsUtil.java +++ b/edexOsgi/com.raytheon.uf.edex.datadelivery.retrieval/src/com/raytheon/uf/edex/datadelivery/retrieval/util/ProviderCredentialsUtil.java @@ -4,6 +4,8 @@ import java.io.File; import com.raytheon.uf.common.datadelivery.registry.Connection; import com.raytheon.uf.common.datadelivery.registry.Provider; +import com.raytheon.uf.common.datadelivery.registry.ProviderCredentials; +import com.raytheon.uf.common.datadelivery.registry.ProviderKeyRequest.Status; import com.raytheon.uf.common.localization.IPathManager; import com.raytheon.uf.common.localization.LocalizationContext; import com.raytheon.uf.common.localization.LocalizationContext.LocalizationLevel; @@ -49,6 +51,7 @@ import com.raytheon.uf.edex.datadelivery.retrieval.db.ProviderKeyRecord; * ------------ ---------- ----------- -------------------------- * Jul 10, 2013 2180 dhladky Initial * Aug 08, 2013 2180 mpduff Corrected the filename and blanked the key before saving + * Aug 23, 2013 2180 mpduff Changed return types and add status messages. * * * @@ -71,12 +74,15 @@ public class ProviderCredentialsUtil { * providerKey This will be used by the SSMI to update userName and password * stores * - * @param providerKey - * @param conn + * @param creds + * ProviderCredentials object + * + * @return ProviderCredentials object with status and message set */ - public static boolean saveCredentials(String providerKey, Provider provider) { - + public static ProviderCredentials saveCredentials(ProviderCredentials creds) { + Provider provider = creds.getProvider(); Connection conn = null; + String providerKey = creds.getProviderKey(); try { // encrypt userName and password @@ -87,7 +93,9 @@ public class ProviderCredentialsUtil { } catch (Exception e) { statusHandler.handle(Priority.ERROR, "Failed! Couldn't encrypt credentials!", e); - return false; + creds.setMessage("Error encrypting credentials. See server log for error details."); + creds.setStatus(Status.FAILURE); + return creds; } try { @@ -98,7 +106,9 @@ public class ProviderCredentialsUtil { } catch (Exception e) { statusHandler.handle(Priority.ERROR, "Failed! Couldn't store provider key record!", e); - return false; + creds.setMessage("Error storing provider key record. See server log for error details."); + creds.setStatus(Status.FAILURE); + return creds; } if (conn != null && providerKey != null) { @@ -109,35 +119,41 @@ public class ProviderCredentialsUtil { .handle(Priority.ERROR, "Failed! Couldn't store encrypted Connection to Localization!", e); - return false; + creds.setStatus(Status.FAILURE); + creds.setMessage("Error saving encrypted connection. See server log for error details."); + return creds; } } - return true; + creds.setStatus(Status.SUCCESS); + return creds; } /** - * Gets the encrypted credentials connection object stored locally + * Gets the ProviderCredentials object containing the encrytped credentials. * - * @param providerKey - * @return + * @param providerName + * The data provider name + * + * @return The ProviderCredentials object + * @throws Exception + * Exception on error */ - public static Connection retrieveCredentials(String providerName) + public static ProviderCredentials retrieveCredentials(String providerName) throws Exception { + ProviderCredentials creds = new ProviderCredentials(); - // retrieve the providerKey from the name Connection conn = getConnection(providerName); if (conn != null) { - ProviderKeyDao pkd = new ProviderKeyDao(); ProviderKeyRecord pkr = pkd.queryByProvider(providerName); if (pkr != null) { conn.setProviderKey(pkr.getProviderKey()); } } - - return conn; + creds.setConnection(conn); + return creds; } /** @@ -145,9 +161,13 @@ public class ProviderCredentialsUtil { * encrypted password. * * @param providerName - * @return + * The data provider name + * @return The Connection object, or null if localization file doesn't + * exist. + * @throws Exception + * On error */ - private static Connection getConnection(String providerName) + public static Connection getConnection(String providerName) throws Exception { IPathManager pm = PathManagerFactory.getPathManager(); @@ -159,11 +179,13 @@ public class ProviderCredentialsUtil { LocalizationFile lf = pm.getLocalizationFile(lc, connectionFileName); File file = lf.getFile(); - // System.out.println("Reading -- " + file.getAbsolutePath()); + if (!file.exists()) { - statusHandler.handle(Priority.DEBUG, providerName - + " connection file: " + file.getAbsolutePath() - + " does not exist."); + if (statusHandler.isPriorityEnabled(Priority.DEBUG)) { + statusHandler.handle(Priority.DEBUG, providerName + + " connection file: " + file.getAbsolutePath() + + " does not exist."); + } return null; } @@ -179,8 +201,9 @@ public class ProviderCredentialsUtil { * username, and encrypted password. * * @param Connection + * The Connection object * @param providerName - * @return + * The data provider's name */ private static void storeConnection(Connection conn, String providerName) throws Exception { @@ -202,5 +225,4 @@ public class ProviderCredentialsUtil { conn.setProviderKey(null); SerializationUtil.jaxbMarshalToXmlFile(conn, file.getAbsolutePath()); } - } diff --git a/edexOsgi/com.raytheon.uf.edex.datadelivery.retrieval/src/com/raytheon/uf/edex/datadelivery/retrieval/util/WfsConnectionUtil.java b/edexOsgi/com.raytheon.uf.edex.datadelivery.retrieval/src/com/raytheon/uf/edex/datadelivery/retrieval/util/WfsConnectionUtil.java index cae3273e84..944fb2b1c6 100644 --- a/edexOsgi/com.raytheon.uf.edex.datadelivery.retrieval/src/com/raytheon/uf/edex/datadelivery/retrieval/util/WfsConnectionUtil.java +++ b/edexOsgi/com.raytheon.uf.edex.datadelivery.retrieval/src/com/raytheon/uf/edex/datadelivery/retrieval/util/WfsConnectionUtil.java @@ -10,6 +10,7 @@ import com.raytheon.uf.common.comm.HttpClient.HttpClientResponse; import com.raytheon.uf.common.comm.IHttpsConfiguration; import com.raytheon.uf.common.comm.IHttpsCredentialsHandler; import com.raytheon.uf.common.datadelivery.registry.Connection; +import com.raytheon.uf.common.datadelivery.registry.ProviderCredentials; import com.raytheon.uf.common.status.IUFStatusHandler; import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.common.status.UFStatus.Priority; @@ -29,6 +30,7 @@ import com.raytheon.uf.common.status.UFStatus.Priority; * Jun 17, 2013 2106 djohnson Use getUnencryptedPassword(). * Jun 18, 2013 2120 dhladky Times fixes and SSL changes * Jul 10, 2013 2180 dhladky Updated credential requests + * Aug 23, 2013 2180 mpduff Implement changes to ProviderCredentialsUtil * * * @@ -40,7 +42,18 @@ public class WfsConnectionUtil { private static final IUFStatusHandler statusHandler = UFStatus .getHandler(WfsConnectionUtil.class); - + + /** + * Connect to the provided URL and return the xml response. + * + * @param url + * The URL + * @param providerConn + * The Connection object + * @param providerName + * The data provider's name + * @return xml response + */ public static String wfsConnect(String url, Connection providerConn, String providerName) { String xmlResponse = null; @@ -52,18 +65,18 @@ public class WfsConnectionUtil { HttpGet get = new HttpGet(); URI uri = new URI(url); // check for the need to do a username password auth check - Connection localConnection = ProviderCredentialsUtil.retrieveCredentials(providerName); - - if (localConnection != null && localConnection.getProviderKey() != null) { + ProviderCredentials creds = ProviderCredentialsUtil + .retrieveCredentials(providerName); + Connection localConnection = creds.getConnection(); + + if (localConnection != null + && localConnection.getProviderKey() != null) { statusHandler.handle(Priority.INFO, "Attempting credentialed request: " + providerName); // Local Connection object contains the username, password and - // encryption method for - // password storage and decrypt. - String userName = localConnection - .getUnencryptedUsername(); - String password = localConnection - .getUnencryptedPassword(); + // encryption method for password storage and decrypt. + String userName = localConnection.getUnencryptedUsername(); + String password = localConnection.getUnencryptedPassword(); http.setHandler(new WfsCredentialsHandler(userName, password)); http.setHttpsConfiguration(new WfsHttpsConfiguration(uri)); @@ -74,8 +87,6 @@ public class WfsConnectionUtil { get.setURI(uri); HttpClientResponse response = http.executeRequest(get); xmlResponse = new String(response.data); - //System.out.println("Response: "+xmlResponse); - } catch (Exception e) { statusHandler.handle(Priority.PROBLEM, "Couldn't connect to WFS server: " + url, e); @@ -83,67 +94,66 @@ public class WfsConnectionUtil { return xmlResponse; } - - + /** * * Credentials Holder * *
-     *
+     * 
      * SOFTWARE HISTORY
-     *
+     * 
      * Date         Ticket#    Engineer    Description
      * ------------ ---------- ----------- --------------------------
      * Jun 19, 2013  2120       dhladky     Initial creation
-     *
+     * 
      * 
- * + * * @author dhladky * @version 1.0 */ - private static class WfsCredentialsHandler implements IHttpsCredentialsHandler { + private static class WfsCredentialsHandler implements + IHttpsCredentialsHandler { + + private final String username; + + private final String password; - private String username; - - private String password; - @Override public String[] getCredentials(String message) { - return new String[] { username, - password }; + return new String[] { username, password }; } - + public WfsCredentialsHandler(String username, String password) { this.password = password; this.username = username; } } - + /** * * HTTPS Configuration * *
-     *
+     * 
      * SOFTWARE HISTORY
-     *
+     * 
      * Date         Ticket#    Engineer    Description
      * ------------ ---------- ----------- --------------------------
      * Jun 19, 2013  2120       dhladky     Initial creation
-     *
+     * 
      * 
- * + * * @author dhladky * @version 1.0 */ private static class WfsHttpsConfiguration implements IHttpsConfiguration { - + private int httpsPort = 443; - + private int httpPort = 80; - public WfsHttpsConfiguration(URI uri) { + public WfsHttpsConfiguration(URI uri) throws URISyntaxException { try { if (uri.getScheme().equals("http")) { @@ -158,7 +168,8 @@ public class WfsConnectionUtil { "Invalid server"); } } catch (URISyntaxException e) { - statusHandler.handle(Priority.PROBLEM, "Syntax or URI is bad!", e); + throw new URISyntaxException(uri.toString(), + "Syntax or URI is bad!"); } } @@ -172,5 +183,4 @@ public class WfsConnectionUtil { return httpPort; } } - } diff --git a/edexOsgi/com.raytheon.uf.edex.datadelivery.service/src/com/raytheon/uf/edex/datadelivery/service/services/ProviderKeyRequestHandler.java b/edexOsgi/com.raytheon.uf.edex.datadelivery.service/src/com/raytheon/uf/edex/datadelivery/service/services/ProviderKeyRequestHandler.java index de3216f0a5..1945c315d0 100644 --- a/edexOsgi/com.raytheon.uf.edex.datadelivery.service/src/com/raytheon/uf/edex/datadelivery/service/services/ProviderKeyRequestHandler.java +++ b/edexOsgi/com.raytheon.uf.edex.datadelivery.service/src/com/raytheon/uf/edex/datadelivery/service/services/ProviderKeyRequestHandler.java @@ -1,4 +1,5 @@ package com.raytheon.uf.edex.datadelivery.service.services; + /** * This software was developed and / or modified by Raytheon Company, * pursuant to Contract DG133W-05-CQ-1067 with the US Government. @@ -20,13 +21,15 @@ package com.raytheon.uf.edex.datadelivery.service.services; **/ import com.raytheon.uf.common.datadelivery.registry.Connection; import com.raytheon.uf.common.datadelivery.registry.Provider; +import com.raytheon.uf.common.datadelivery.registry.ProviderCredentials; import com.raytheon.uf.common.datadelivery.registry.ProviderKeyRequest; import com.raytheon.uf.common.datadelivery.registry.ProviderKeyRequest.RequestType; import com.raytheon.uf.common.datadelivery.registry.ProviderKeyRequest.Status; import com.raytheon.uf.common.serialization.comm.IRequestHandler; +import com.raytheon.uf.common.status.IUFStatusHandler; +import com.raytheon.uf.common.status.UFStatus; import com.raytheon.uf.edex.datadelivery.retrieval.util.ProviderCredentialsUtil; - /** * A ProviderKeyRequestHandler. * @@ -36,38 +39,54 @@ import com.raytheon.uf.edex.datadelivery.retrieval.util.ProviderCredentialsUtil; * * Date Ticket# Engineer Description * ------------ ---------- ----------- -------------------------- - * July 14, 2013 2184 dhladky Initial creation + * Jul 14, 2013 2184 dhladky Initial creation + * Aug 23, 2013 2180 mpduff Implement changes to ProviderCredentialsUtil * * @author dhladky * @version 1.0 */ -public class ProviderKeyRequestHandler implements IRequestHandler { +public class ProviderKeyRequestHandler implements + IRequestHandler { + private static final IUFStatusHandler statusHandler = UFStatus + .getHandler(ProviderKeyRequestHandler.class); + /** + * {@inheritDoc} + */ @Override public Object handleRequest(ProviderKeyRequest request) throws Exception { - if (request.getRequestType() == RequestType.SAVE) { - - boolean status = ProviderCredentialsUtil.saveCredentials(request.getProviderKey(), - request.getProvider()); - if (status) { + ProviderCredentials creds = new ProviderCredentials( + request.getProvider(), request.getProviderKey()); + creds = ProviderCredentialsUtil.saveCredentials(creds); + if (creds.getStatus() == Status.SUCCESS) { request.setStatus(Status.SUCCESS); } else { request.setStatus(Status.FAILURE); + request.setMessage(creds.getMessage()); } - } else if (request.getRequestType() == RequestType.RETRIEVE) { - Provider provider = request.getProvider(); - Connection conn = ProviderCredentialsUtil - .retrieveCredentials(request.getProvider().getName()); - if (conn != null) { + ProviderCredentials creds = null; + Connection conn = null; + try { + creds = ProviderCredentialsUtil.retrieveCredentials(request + .getProvider().getName()); + + conn = creds.getConnection(); + } catch (Exception e) { + String msg = "Error retrieving credentials"; + statusHandler.error(msg, e); + request.setStatus(Status.FAILURE); + request.setMessage(msg); + return request; + } + + if (creds != null) { provider.setConnection(conn); request.setProvider(provider); request.setStatus(Status.SUCCESS); - } else { - request.setStatus(Status.FAILURE); } } diff --git a/tests/unit/com/raytheon/uf/common/datadelivery/registry/EncryptionTest.java b/tests/unit/com/raytheon/uf/common/datadelivery/registry/EncryptionTest.java new file mode 100644 index 0000000000..7365b88212 --- /dev/null +++ b/tests/unit/com/raytheon/uf/common/datadelivery/registry/EncryptionTest.java @@ -0,0 +1,112 @@ +/** + * This software was developed and / or modified by Raytheon Company, + * pursuant to Contract DG133W-05-CQ-1067 with the US Government. + * + * U.S. EXPORT CONTROLLED TECHNICAL DATA + * This software product contains export-restricted data whose + * export/transfer/disclosure is restricted by U.S. law. Dissemination + * to non-U.S. persons whether in the United States or abroad requires + * an export license or other authorization. + * + * Contractor Name: Raytheon Company + * Contractor Address: 6825 Pine Street, Suite 340 + * Mail Stop B8 + * Omaha, NE 68106 + * 402.291.0100 + * + * See the AWIPS II Master Rights File ("Master Rights File.pdf") for + * further licensing information. + **/ +package com.raytheon.uf.common.datadelivery.registry; + +import static org.junit.Assert.assertEquals; + +import org.junit.BeforeClass; +import org.junit.Test; + +import com.raytheon.uf.common.datadelivery.registry.Encryption.Algorithim; +import com.raytheon.uf.common.datadelivery.registry.Encryption.Padding; + +/** + * Test the encryption + * + *
+ * 
+ * SOFTWARE HISTORY
+ * 
+ * Date         Ticket#    Engineer    Description
+ * ------------ ---------- ----------- --------------------------
+ * Aug 23, 2013   2180     mpduff      Initial creation
+ * 
+ * 
+ * + * @author mpduff + * @version 1.0 + */ + +public class EncryptionTest { + private final String password = "P@ssW0rd"; + + private static Encryption encryption; + + @BeforeClass + public static void setupEncryption() { + encryption = new Encryption(); + encryption.setAlgorithim(Algorithim.AES); + encryption.setPadding(Padding.AES); + } + + @Test + public void testEncryptionCanEncryptAndDecryptWithAes() { + String encryptionKey = "ThisIsTheEncryptionKey"; + + String decryptedPassword = null; + try { + String encryptedPassword = encryption.encrypt(encryptionKey, + password); + decryptedPassword = encryption.decrypt(encryptionKey, + encryptedPassword); + + } catch (Exception e) { + e.printStackTrace(); + } + + assertEquals(password, decryptedPassword); + } + + @Test + public void testEncryptionCanEncryptAndDecryptWithAesAndShortKey() { + String encryptionKey = "K"; + + String decryptedPassword = null; + try { + String encryptedPassword = encryption.encrypt(encryptionKey, + password); + decryptedPassword = encryption.decrypt(encryptionKey, + encryptedPassword); + + } catch (Exception e) { + e.printStackTrace(); + } + + assertEquals(password, decryptedPassword); + } + + @Test + public void testEncryptionCanEncryptAndDecryptWithAesAndLongKey() { + String encryptionKey = "ThisIsTheEncryptionKeyAndIsVeryVeryVeryVeryVeryLong!!!!!!!!!!!!!!!!!!!!!!!"; + + String decryptedPassword = null; + try { + String encryptedPassword = encryption.encrypt(encryptionKey, + password); + decryptedPassword = encryption.decrypt(encryptionKey, + encryptedPassword); + + } catch (Exception e) { + e.printStackTrace(); + } + + assertEquals(password, decryptedPassword); + } +}