[jboss-cvs] JBossAS SVN: r114610 - in branches/JBPAPP_5: testsuite/src/main/org/jboss/test/security/test and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Thu Dec 19 01:42:09 EST 2013
Author: istudens at redhat.com
Date: 2013-12-19 01:42:08 -0500 (Thu, 19 Dec 2013)
New Revision: 114610
Added:
branches/JBPAPP_5/security/src/main/org/jboss/security/plugins/JaasSecurityDomainUtil.java
Modified:
branches/JBPAPP_5/testsuite/src/main/org/jboss/test/security/test/JaasSecurityDomainUnitTestCase.java
Log:
JBPAPP-10913 Provide a helper class for JAAS Security Domain encrypt/decrypt functions
Added: branches/JBPAPP_5/security/src/main/org/jboss/security/plugins/JaasSecurityDomainUtil.java
===================================================================
--- branches/JBPAPP_5/security/src/main/org/jboss/security/plugins/JaasSecurityDomainUtil.java (rev 0)
+++ branches/JBPAPP_5/security/src/main/org/jboss/security/plugins/JaasSecurityDomainUtil.java 2013-12-19 06:42:08 UTC (rev 114610)
@@ -0,0 +1,127 @@
+package org.jboss.security.plugins;
+
+import org.jboss.mx.util.MBeanServerLocator;
+
+import javax.management.MBeanServer;
+import javax.management.MBeanServerConnection;
+import javax.management.ObjectName;
+
+/**
+ * A helper encryting/decrypting application level API based on JAAS Security Domain.
+ *
+ * @author <a href="mailto:istudens at redhat.com">Ivo Studensky</a>
+ */
+public class JaasSecurityDomainUtil {
+
+ /** Encode a secret using the securityDomain.
+ * @param securityDomain the security domain mbean name
+ * @param secret - the byte sequence to encrypt
+ * @return the encrypted byte sequence
+ * @throws Exception
+ */
+ public static byte[] encode(String securityDomain, byte[] secret) throws Exception
+ {
+ return (byte[]) invoke(securityDomain, "encode", new Object[]{secret}, new String[]{byte[].class.getName()});
+ }
+
+ /** Encode a secret using the securityDomain.
+ * @param server jboss mbean server
+ * @param securityDomain the security domain mbean name
+ * @param secret - the byte sequence to encrypt
+ * @return the encrypted byte sequence
+ * @throws Exception
+ */
+ public static byte[] encode(MBeanServerConnection server, String securityDomain, byte[] secret) throws Exception
+ {
+ return (byte[]) invoke(server, securityDomain, "encode", new Object[]{secret}, new String[]{byte[].class.getName()});
+ }
+
+ /** Decode a secret using the securityDomain.
+ * @param securityDomain the security domain mbean name
+ * @param secret - the byte sequence to decrypt
+ * @return the decrypted byte sequence
+ * @throws Exception
+ */
+ public static byte[] decode(String securityDomain, byte[] secret) throws Exception
+ {
+ return (byte[]) invoke(securityDomain, "decode", new Object[]{secret}, new String[]{byte[].class.getName()});
+ }
+
+ /** Decode a secret using the securityDomain.
+ * @param server jboss mbean server
+ * @param securityDomain the security domain mbean name
+ * @param secret - the byte sequence to decrypt
+ * @return the decrypted byte sequence
+ * @throws Exception
+ */
+ public static byte[] decode(MBeanServerConnection server, String securityDomain, byte[] secret) throws Exception
+ {
+ return (byte[]) invoke(server, securityDomain, "decode", new Object[]{secret}, new String[]{byte[].class.getName()});
+ }
+
+ /** Encode a secret using the securityDomain.
+ * @param securityDomain the security domain mbean name
+ * @param secret - the byte sequence to encrypt as a base64 string using
+ * the Util.tob64() function
+ * @return the encrypted byte sequence
+ * @throws Exception
+ */
+ public static String encode64(String securityDomain, byte[] secret) throws Exception
+ {
+ return (String) invoke(securityDomain, "encode64", new Object[]{secret}, new String[]{byte[].class.getName()});
+ }
+
+ /** Encode a secret using the securityDomain.
+ * @param server jboss mbean server
+ * @param securityDomain the security domain mbean name
+ * @param secret - the byte sequence to encrypt as a base64 string using
+ * the Util.tob64() function
+ * @return the encrypted byte sequence
+ * @throws Exception
+ */
+ public static String encode64(MBeanServerConnection server, String securityDomain, byte[] secret) throws Exception
+ {
+ return (String) invoke(server, securityDomain, "encode64", new Object[]{secret}, new String[]{byte[].class.getName()});
+ }
+
+ /** Decode a secret using the securityDomain.
+ * @param securityDomain the security domain mbean name
+ * @param secret - the Util.tob64 string represention to decrypt
+ * @return the decrypted byte sequence
+ * @throws Exception
+ */
+ public static byte[] decode64(String securityDomain, String secret) throws Exception
+ {
+ return (byte[]) invoke(securityDomain, "decode64", new Object[] {secret}, new String[] {String.class.getName()});
+ }
+
+ /** Decode a secret using the securityDomain.
+ * @param server jboss mbean server
+ * @param securityDomain the security domain mbean name
+ * @param secret - the Util.tob64 string represention to decrypt
+ * @return the decrypted byte sequence
+ * @throws Exception
+ */
+ public static byte[] decode64(MBeanServerConnection server, String securityDomain, String secret) throws Exception
+ {
+ return (byte[]) invoke(server, securityDomain, "decode64", new Object[] {secret}, new String[] {String.class.getName()});
+ }
+
+ /*
+ * Invokes a method methodName on a securityDomain with its params.
+ */
+ private static Object invoke(String securityDomain, String methodName, Object[] params, String[] signature) throws Exception
+ {
+ MBeanServer server = MBeanServerLocator.locateJBoss();
+ return invoke(server, securityDomain, methodName, params, signature);
+ }
+
+ /*
+ * Invokes a method methodName on a securityDomain with its params.
+ */
+ private static Object invoke(MBeanServerConnection server, String securityDomain, String methodName, Object[] params, String[] signature) throws Exception
+ {
+ return server.invoke(new ObjectName(securityDomain), methodName, params, signature);
+ }
+
+}
Modified: branches/JBPAPP_5/testsuite/src/main/org/jboss/test/security/test/JaasSecurityDomainUnitTestCase.java
===================================================================
--- branches/JBPAPP_5/testsuite/src/main/org/jboss/test/security/test/JaasSecurityDomainUnitTestCase.java 2013-12-18 16:50:51 UTC (rev 114609)
+++ branches/JBPAPP_5/testsuite/src/main/org/jboss/test/security/test/JaasSecurityDomainUnitTestCase.java 2013-12-19 06:42:08 UTC (rev 114610)
@@ -31,6 +31,7 @@
import javax.crypto.SecretKey;
import javax.crypto.Cipher;
+import org.jboss.security.plugins.JaasSecurityDomainUtil;
import org.jboss.test.JBossTestCase;
import org.jboss.test.JBossTestSetup;
import org.jboss.security.plugins.FilePassword;
@@ -133,6 +134,25 @@
assertTrue("secret == decode2", Arrays.equals(secret, decode2));
}
+ public void testEncodeDecodeWithUtil() throws Exception
+ {
+ String securityDomain = "jboss.security:service=JaasSecurityDomain,domain=encode-decode";
+ byte[] secret = "secret".getBytes();
+ byte[] encode = JaasSecurityDomainUtil.encode(getServer(), securityDomain, secret);
+ assertTrue("secret != encode", Arrays.equals(secret, encode) == false);
+ byte[] decode = JaasSecurityDomainUtil.decode(getServer(), securityDomain, encode);
+ assertTrue("secret == decode", Arrays.equals(secret, decode));
+
+ // repeat the test, this time invoking the sec domain that was configured as a microcontainer bean.
+ String exportedSecurityDomain = "jboss.security:service=JaasSecurityDomain,domain=encode-decode2";
+ // invoke the encode operation using the bean's exported MBean interface.
+ byte[] encode2 = JaasSecurityDomainUtil.encode(getServer(), exportedSecurityDomain, secret);
+ assertTrue("secret != encode2", Arrays.equals(secret, encode2) == false);
+ // invoke the decode operation using the bean's exported MBean interface.
+ byte[] decode2 = JaasSecurityDomainUtil.decode(getServer(), exportedSecurityDomain, encode2);
+ assertTrue("secret == decode2", Arrays.equals(secret, decode2));
+ }
+
public void testEncodeDecode64() throws Exception
{
ObjectName name = new ObjectName("jboss.security:service=JaasSecurityDomain,domain=encode-decode");
@@ -156,6 +176,23 @@
assertTrue("secret == decode2", Arrays.equals(secret, decode2));
}
+ public void testEncodeDecode64WithUtil() throws Exception
+ {
+ String securityDomain = "jboss.security:service=JaasSecurityDomain,domain=encode-decode";
+ byte[] secret = "secret".getBytes();
+ String encode = JaasSecurityDomainUtil.encode64(getServer(), securityDomain, secret);
+ byte[] decode = JaasSecurityDomainUtil.decode64(getServer(), securityDomain, encode);
+ assertTrue("secret == decode", Arrays.equals(secret, decode));
+
+ // repeat the test, this time invoking the sec domain that was configured as a microcontainer bean.
+ String exportedSecurityDomain = "jboss.security:service=JaasSecurityDomain,domain=encode-decode2";
+ // invoke the encode64 operation using the bean's exported MBean interface.
+ String encode2 = JaasSecurityDomainUtil.encode64(getServer(), exportedSecurityDomain, secret);
+ // invoke the decode64 operation using the bean's exported MBean interface.
+ byte[] decode2 = JaasSecurityDomainUtil.decode64(getServer(), exportedSecurityDomain, encode2);
+ assertTrue("secret == decode2", Arrays.equals(secret, decode2));
+ }
+
/**
* Setup the test suite.
*/
More information about the jboss-cvs-commits
mailing list