package org.jboss.test.naming.test;
import java.util.Properties;
import java.security.Principal;
import java.lang.reflect.UndeclaredThrowableException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NamingEnumeration;
import javax.security.auth.login.LoginContext;
import javax.rmi.PortableRemoteObject;
import org.jboss.test.JBossTestCase;
import org.jboss.test.naming.interfaces.TestENC;
import org.jboss.test.naming.interfaces.TestENCHome;
import org.jboss.test.util.AppCallbackHandler;
import org.jboss.security.SecurityAssociation;
public class SecurityUnitTestCase extends JBossTestCase
{
public SecurityUnitTestCase(String name)
{
super(name);
}
public void testSecureHttpInvokerFailure() throws Exception
{
getLog().debug("+++ testSecureHttpInvokerFailure");
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
env.setProperty(Context.PROVIDER_URL, "http://localhost:8080/invoker/restricted/JNDIFactory");
getLog().debug("Creating InitialContext with env="+env);
try
{
getLog().debug("Testing without valid login");
InitialContext ctx1 = new InitialContext(env);
getLog().debug("Created InitialContext");
Object obj1 = ctx1.lookup("jmx");
getLog().debug("lookup(jmx) : "+obj1);
fail("Should not have been able to lookup(jmx)");
}
catch(Exception e)
{
getLog().debug("Lookup failed as expected", e);
}
}
public void testSecureHttpInvoker() throws Exception
{
getLog().debug("+++ testSecureHttpInvoker");
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
String authConf = super.getResourceURL("security/auth.conf");
getLog().debug("Using auth.conf: "+authConf);
System.setProperty("java.security.auth.login.config", authConf);
AppCallbackHandler handler = new AppCallbackHandler("admin", "admin".toCharArray());
LoginContext lc = new LoginContext("testSecureHttpInvoker", handler);
lc.login();
env.setProperty(Context.PROVIDER_URL, "http://localhost:8080/invoker/restricted/JNDIFactory");
getLog().debug("Creating InitialContext with env="+env);
InitialContext ctx = new InitialContext(env);
getLog().debug("Created InitialContext");
Object obj = ctx.lookup("jmx");
getLog().debug("lookup(jmx) : "+obj);
Context jmxCtx = (Context) obj;
NamingEnumeration list = jmxCtx.list("");
while( list.hasMore() )
{
Object entry = list.next();
getLog().debug(" + "+entry);
}
ctx.close();
lc.logout();
Principal p = SecurityAssociation.getPrincipal();
assertTrue("SecurityAssociation.getPrincipal is null", p == null);
}
public void testHttpReadonlyLookup() throws Exception
{
getLog().debug("+++ testHttpReadonlyLookup");
InitialContext bootCtx = new InitialContext();
try
{
bootCtx.unbind("readonly");
}
catch(NamingException ignore)
{
}
Context readonly = bootCtx.createSubcontext("readonly");
readonly.bind("data", "somedata");
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.HttpNamingContextFactory");
env.setProperty(Context.PROVIDER_URL, "http://localhost:8080/invoker/ReadOnlyJNDIFactory");
getLog().debug("Creating InitialContext with env="+env);
InitialContext ctx = new InitialContext(env);
Object data = ctx.lookup("readonly/data");
getLog().debug("lookup(readonly/data) : "+data);
try
{
ctx.bind("readonly/mydata", "otherdata");
fail("Was able to bind into the readonly context");
}
catch(UndeclaredThrowableException e)
{
getLog().debug("Invalid exception", e);
fail("UndeclaredThrowableException thrown");
}
catch(Exception e)
{
getLog().debug("Bind failed as expected", e);
}
try
{
ctx.lookup("invokers");
fail("Was able to lookup(invokers)");
}
catch(UndeclaredThrowableException e)
{
getLog().debug("Invalid exception", e);
fail("UndeclaredThrowableException thrown");
}
catch(Exception e)
{
getLog().debug("lookup(invokers) failed as expected", e);
}
}
public void testHttpReadonlyContextLookup() throws Exception
{
getLog().debug("+++ testHttpReadonlyContextLookup");
deploy("naming-readonly.sar");
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.jboss.test.naming.test.BootstrapNamingContextFactory");
env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
env.setProperty("bootstrap-binding", "naming/Naming");
getLog().debug("Creating bootstrap InitialContext with env="+env);
InitialContext bootCtx = new InitialContext(env);
try
{
bootCtx.unbind("readonly");
}
catch(NamingException ignore)
{
}
getLog().debug("Creating readonly context");
bootCtx.createSubcontext("readonly");
bootCtx.bind("readonly/data", "somedata");
env.setProperty("bootstrap-binding", "naming/ReadOnlyNaming");
getLog().debug("Creating InitialContext with env="+env);
InitialContext ctx = new InitialContext(env);
Object data = ctx.lookup("readonly/data");
getLog().debug("lookup(readonly/data) : "+data);
Object robinding = ctx.lookup("readonly");
getLog().debug("Looked up readonly: "+robinding);
Context roctx = (Context) robinding;
data = roctx.lookup("data");
getLog().debug("Looked up data: "+data);
assertTrue("lookup(data) == somedata: "+data, "somedata".equals(data));
try
{
roctx.bind("mydata", "otherdata");
fail("Was able to bind into the readonly context");
}
catch(UndeclaredThrowableException e)
{
getLog().debug("Invalid exception", e);
fail("UndeclaredThrowableException thrown");
}
catch(NamingException e)
{
getLog().debug("Bind failed as expected", e);
}
try
{
ctx.lookup("invokers");
fail("Was able to lookup(invokers)");
}
catch(UndeclaredThrowableException e)
{
getLog().debug("Invalid exception", e);
fail("UndeclaredThrowableException thrown");
}
catch(Exception e)
{
getLog().debug("lookup(invokers) failed as expected", e);
}
undeploy("naming-readonly.sar");
}
public void testLoginInitialContext() throws Exception
{
getLog().debug("+++ testLoginInitialContext");
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.LoginInitialContextFactory");
env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099/");
env.setProperty(Context.SECURITY_CREDENTIALS, "theduke");
env.setProperty(Context.SECURITY_PRINCIPAL, "jduke");
env.setProperty(Context.SECURITY_PROTOCOL, "testLoginInitialContext");
String authConf = super.getResourceURL("security/auth.conf");
System.setProperty("java.security.auth.login.config", authConf);
getLog().debug("Creating InitialContext with env="+env);
InitialContext ctx = new InitialContext(env);
getLog().debug("Created InitialContext");
Object obj = ctx.lookup("jmx");
getLog().debug("lookup(jmx) : "+obj);
Context jmxCtx = (Context) obj;
NamingEnumeration list = jmxCtx.list("");
while( list.hasMore() )
{
Object entry = list.next();
getLog().debug(" + "+entry);
}
ctx.close();
env.setProperty(Context.SECURITY_CREDENTIALS, "badpass");
try
{
getLog().debug("Creating InitialContext with env="+env);
ctx = new InitialContext(env);
fail("Was able to create InitialContext with badpass");
}
catch(NamingException e)
{
getLog().debug("InitialContext failed as expected with exception", e);
}
}
public void testSecureEJBViaLoginInitialContextFactory() throws Exception
{
getLog().debug("+++ testSecureEJBViaLoginInitialContextFactory");
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.LoginInitialContextFactory");
env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099/");
env.setProperty(Context.SECURITY_CREDENTIALS, "theduke");
env.setProperty(Context.SECURITY_PRINCIPAL, "jduke");
env.setProperty(Context.SECURITY_PROTOCOL, "testLoginInitialContext");
String authConf = super.getResourceURL("security/auth.conf");
log.info("auth.conf: "+authConf);
System.setProperty("java.security.auth.login.config", authConf);
getLog().debug("Creating InitialContext with env="+env);
InitialContext ctx = new InitialContext(env);
getLog().debug("Created InitialContext, ctx="+ctx);
super.deploy("naming.jar");
Object obj = getInitialContext().lookup("ENCTests/ejbs/SecuredENCBean");
obj = PortableRemoteObject.narrow(obj, TestENCHome.class);
TestENCHome home = (TestENCHome)obj;
try
{
TestENC bean = home.create();
getLog().debug("Created SecuredENCBean");
bean.accessENC();
bean.remove();
System.setProperty("java.security.auth.login.config", "invalid");
}
finally
{
super.undeploy("naming.jar");
}
}
public void testSecureEJBViaJndiLoginInitialContextFactory() throws Exception
{
getLog().debug("+++ testSecureEJBViaJndiLoginInitialContextFactory");
Properties env = new Properties();
env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.security.jndi.JndiLoginInitialContextFactory");
env.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099/");
env.setProperty(Context.SECURITY_CREDENTIALS, "theduke");
env.setProperty(Context.SECURITY_PRINCIPAL, "jduke");
getLog().debug("Creating InitialContext with env="+env);
InitialContext ctx = new InitialContext(env);
getLog().debug("Created InitialContext, ctx="+ctx);
super.deploy("naming.jar");
Object obj = getInitialContext().lookup("ENCTests/ejbs/SecuredENCBean");
obj = PortableRemoteObject.narrow(obj, TestENCHome.class);
TestENCHome home = (TestENCHome)obj;
getLog().debug("Found SecuredENCBean");
try
{
TestENC bean = home.create();
getLog().debug("Created SecuredENCBean");
bean.accessENC();
bean.remove();
}
finally
{
super.undeploy("naming.jar");
}
}
}