http://git-wip-us.apache.org/repos/asf/knox/blob/56cedc0a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayDeployFuncTest.java ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayDeployFuncTest.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayDeployFuncTest.java index 79ba84b..c944796 100644 --- a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayDeployFuncTest.java +++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayDeployFuncTest.java @@ -62,293 +62,293 @@ import static org.junit.Assert.assertThat; public class GatewayDeployFuncTest { -// private static final long SHORT_TIMEOUT = 1000L; -// private static final long LONG_TIMEOUT = 30 * 1000L; -// -// private static Class RESOURCE_BASE_CLASS = GatewayDeployFuncTest.class; -// private static Logger LOG = LoggerFactory.getLogger( GatewayDeployFuncTest.class ); -// -// public static Enumeration<Appender> appenders; -// public static GatewayConfig config; -// public static GatewayServer gateway; -// public static File gatewayHome; -// public static String gatewayUrl; -// public static String clusterUrl; -// public static SimpleLdapDirectoryServer ldap; -// public static TcpTransport ldapTransport; -// -// @BeforeClass -// public static void setupSuite() throws Exception { -// LOG_ENTER(); -// //appenders = NoOpAppender.setUp(); -// setupLdap(); -// LOG_EXIT(); -// } -// -// @AfterClass -// public static void cleanupSuite() throws Exception { -// LOG_ENTER(); -// ldap.stop( true ); -// //FileUtils.deleteQuietly( new File( config.getGatewayHomeDir() ) ); -// //NoOpAppender.tearDown( appenders ); -// LOG_EXIT(); -// } -// -// public static void setupLdap() throws Exception { -// URL usersUrl = getResourceUrl( "users.ldif" ); -// int port = findFreePort(); -// ldapTransport = new TcpTransport( port ); -// ldap = new SimpleLdapDirectoryServer( "dc=hadoop,dc=apache,dc=org", new File( usersUrl.toURI() ), ldapTransport ); -// ldap.start(); -// LOG.info( "LDAP port = " + ldapTransport.getPort() ); -// } -// -// @Before -// public void setupGateway() throws Exception { -// -// File targetDir = new File( System.getProperty( "user.dir" ), "target" ); -// File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() ); -// gatewayDir.mkdirs(); -// gatewayHome = gatewayDir; -// -// GatewayTestConfig testConfig = new GatewayTestConfig(); -// config = testConfig; -// testConfig.setGatewayHomeDir( gatewayDir.getAbsolutePath() ); -// -// File topoDir = new File( testConfig.getGatewayTopologyDir() ); -// topoDir.mkdirs(); -// -// File deployDir = new File( testConfig.getGatewayDeploymentDir() ); -// deployDir.mkdirs(); -// -// DefaultGatewayServices srvcs = new DefaultGatewayServices(); -// Map<String,String> options = new HashMap<String,String>(); -// options.put( "persist-master", "false" ); -// options.put( "master", "password" ); -// try { -// srvcs.init( testConfig, options ); -// } catch ( ServiceLifecycleException e ) { -// e.printStackTrace(); // I18N not required. -// } -// gateway = GatewayServer.startGateway( testConfig, srvcs ); -// MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() ); -// -// LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() ); -// -// gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath(); -// clusterUrl = gatewayUrl + "/test-cluster"; -// } -// -// @After -// public void cleanupGateway() throws Exception { -// gateway.stop(); -// FileUtils.deleteQuietly( gatewayHome ); -// } -// -// private static XMLTag createTopology() { -// XMLTag xml = XMLDoc.newDocument( true ) -// .addRoot( "topology" ) -// .addTag( "gateway" ) -// -// .addTag( "provider" ) -// .addTag( "role" ).addText( "authentication" ) -// .addTag( "name" ).addText( "ShiroProvider" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm" ) -// .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm" ).gotoParent() -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.userDnTemplate" ) -// .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ).gotoParent() -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.url" ) -// .addTag( "value" ).addText( "ldap://localhost:" + ldapTransport.getPort() ).gotoParent() -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.authenticationMechanism" ) -// .addTag( "value" ).addText( "simple" ).gotoParent() -// .addTag( "param" ) -// .addTag( "name" ).addText( "urls./**" ) -// .addTag( "value" ).addText( "authcBasic" ).gotoParent().gotoParent() -// .addTag( "provider" ) -// .addTag( "role" ).addText( "identity-assertion" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "name" ).addText( "Default" ).gotoParent() -// .addTag( "provider" ) -// .gotoRoot() -// .addTag( "service" ) -// .addTag( "role" ).addText( "test-service-role" ) -// .gotoRoot(); -// return xml; -// } -// -// private static int findFreePort() throws IOException { -// ServerSocket socket = new ServerSocket(0); -// int port = socket.getLocalPort(); -// socket.close(); -// return port; -// } -// -// public static InputStream getResourceStream( String resource ) throws IOException { -// return getResourceUrl( resource ).openStream(); -// } -// -// public static URL getResourceUrl( String resource ) { -// URL url = ClassLoader.getSystemResource( getResourceName( resource ) ); -// assertThat( "Failed to find test resource " + resource, url, Matchers.notNullValue() ); -// return url; -// } -// -// public static String getResourceName( String resource ) { -// return getResourceBaseName() + resource; -// } -// -// public static String getResourceBaseName() { -// return RESOURCE_BASE_CLASS.getName().replaceAll( "\\.", "/" ) + "/"; -// } -// -// //@Test -// public void waitForManualTesting() throws IOException { -// System.in.read(); -// } -// -// @Test( timeout = LONG_TIMEOUT ) -// public void testDeployRedeployUndeploy() throws InterruptedException, IOException { -// LOG_ENTER(); -// long sleep = 200; -// int numFilesInWar = 5; -// String username = "guest"; -// String password = "guest-password"; -// String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; -// long topoTimestampBefore, topoTimestampAfter; -// -// File topoDir = new File( config.getGatewayTopologyDir() ); -// File deployDir = new File( config.getGatewayDeploymentDir() ); -// File warDir; -// -// // Make sure deployment directory is empty. -// assertThat( topoDir.listFiles().length, is( 0 ) ); -// assertThat( deployDir.listFiles().length, is( 0 ) ); -// -// File descriptor = writeTestTopology( "test-cluster", createTopology() ); -// long writeTime = System.currentTimeMillis(); -// -// warDir = waitForFiles( deployDir, "test-cluster.war\\.[0-9A-Fa-f]+", 1, 0, sleep ); -// for( File webInfDir : warDir.listFiles() ) { -// waitForFiles( webInfDir, ".*", numFilesInWar, 0, sleep ); -// } -// waitForAccess( serviceUrl, username, password, sleep ); -// -// // Wait to make sure a second has passed to ensure the the file timestamps are different. -// waitForElapsed( writeTime, 1000, 100 ); -// -// // Redeploy and make sure the timestamp is updated. -// topoTimestampBefore = descriptor.lastModified(); -// GatewayServer.redeployTopologies( null ); -// writeTime = System.currentTimeMillis(); -// topoTimestampAfter = descriptor.lastModified(); -// assertThat( topoTimestampAfter, greaterThan( topoTimestampBefore ) ); -// -// // Check to make sure there are two war directories with the same root. -// warDir = waitForFiles( deployDir, "test-cluster.war\\.[0-9A-Fa-f]+", 2, 1, sleep ); -// for( File webInfDir : warDir.listFiles() ) { -// waitForFiles( webInfDir, ".*", numFilesInWar, 0, sleep ); -// } -// waitForAccess( serviceUrl, username, password, sleep ); -// -// // Wait to make sure a second has passed to ensure the the file timestamps are different. -// waitForElapsed( writeTime, 1000, 100 ); -// -// // Redeploy and make sure the timestamp is updated. -// topoTimestampBefore = descriptor.lastModified(); -// GatewayServer.redeployTopologies( "test-cluster" ); -// writeTime = System.currentTimeMillis(); -// topoTimestampAfter = descriptor.lastModified(); -// assertThat( topoTimestampAfter, greaterThan( topoTimestampBefore ) ); -// -// // Check to make sure there are two war directories with the same root. -// warDir = waitForFiles( deployDir, "test-cluster.war\\.[0-9A-Fa-f]+", 3, 2, sleep ); -// for( File webInfDir : warDir.listFiles() ) { -// waitForFiles( webInfDir, ".*", numFilesInWar, 0, sleep ); -// } -// waitForAccess( serviceUrl, username, password, sleep ); -// -// // Delete the test topology. -// assertThat( "Failed to delete the topology file.", descriptor.delete(), is( true ) ); -// -// // Wait to make sure a second has passed to ensure the the file timestamps are different. -// waitForElapsed( writeTime, 1000, 100 ); -// -// waitForFiles( deployDir, ".*", 0, -1, sleep ); -// -// // Wait a bit more to make sure undeployment finished. -// Thread.sleep( sleep ); -// -// // Make sure the test topology is not accessible. -// given().auth().preemptive().basic( username, password ) -// .expect().statusCode( HttpStatus.SC_NOT_FOUND ) -// .when().get( serviceUrl ); -// -// // Make sure deployment directory is empty. -// assertThat( topoDir.listFiles().length, is( 0 ) ); -// assertThat( deployDir.listFiles().length, is( 0 ) ); -// LOG_EXIT(); -// } -// -// private void waitForElapsed( long from, long total, long sleep ) throws InterruptedException { -// while( System.currentTimeMillis() - from < total ) { -// Thread.sleep( sleep ); -// } -// } -// -// private File writeTestTopology( String name, XMLTag xml ) throws IOException { -// // Create the test topology. -// File tempFile = new File( config.getGatewayTopologyDir(), name + ".xml." + UUID.randomUUID() ); -// FileOutputStream stream = new FileOutputStream( tempFile ); -// xml.toStream( stream ); -// stream.close(); -// File descriptor = new File( config.getGatewayTopologyDir(), name + ".xml" ); -// tempFile.renameTo( descriptor ); -// return descriptor; -// } -// -// private File waitForFiles( File dir, String pattern, int count, int index, long sleep ) throws InterruptedException { -// RegexDirFilter filter = new RegexDirFilter( pattern ); -// while( true ) { -// File[] files = dir.listFiles( filter ); -// if( files.length == count ) { -// return ( index < 0 ) ? null : files[ index ]; -// } -// Thread.sleep( sleep ); -// } -// } -// -// private void waitForAccess( String url, String username, String password, long sleep ) throws InterruptedException { -// while( true ) { -// Response response = given() -// .auth().preemptive().basic( username, password ) -// .when().get( url ).andReturn(); -// if( response.getStatusCode() == HttpStatus.SC_NOT_FOUND ) { -// Thread.sleep( sleep ); -// continue; -// } -// assertThat( response.getContentType(), containsString( "text/plain" ) ); -// assertThat( response.getBody().asString(), is( "test-service-response" ) ); -// break; -// } -// } -// -// private class RegexDirFilter implements FilenameFilter { -// -// Pattern pattern; -// -// RegexDirFilter( String regex ) { -// pattern = Pattern.compile( regex ); -// } -// -// @Override -// public boolean accept( File dir, String name ) { -// return pattern.matcher( name ).matches(); -// } -// } + private static final long SHORT_TIMEOUT = 1000L; + private static final long LONG_TIMEOUT = 30 * 1000L; + + private static Class RESOURCE_BASE_CLASS = GatewayDeployFuncTest.class; + private static Logger LOG = LoggerFactory.getLogger( GatewayDeployFuncTest.class ); + + public static Enumeration<Appender> appenders; + public static GatewayConfig config; + public static GatewayServer gateway; + public static File gatewayHome; + public static String gatewayUrl; + public static String clusterUrl; + public static SimpleLdapDirectoryServer ldap; + public static TcpTransport ldapTransport; + + @BeforeClass + public static void setupSuite() throws Exception { + LOG_ENTER(); + //appenders = NoOpAppender.setUp(); + setupLdap(); + LOG_EXIT(); + } + + @AfterClass + public static void cleanupSuite() throws Exception { + LOG_ENTER(); + ldap.stop( true ); + //FileUtils.deleteQuietly( new File( config.getGatewayHomeDir() ) ); + //NoOpAppender.tearDown( appenders ); + LOG_EXIT(); + } + + public static void setupLdap() throws Exception { + URL usersUrl = getResourceUrl( "users.ldif" ); + int port = findFreePort(); + ldapTransport = new TcpTransport( port ); + ldap = new SimpleLdapDirectoryServer( "dc=hadoop,dc=apache,dc=org", new File( usersUrl.toURI() ), ldapTransport ); + ldap.start(); + LOG.info( "LDAP port = " + ldapTransport.getPort() ); + } + + @Before + public void setupGateway() throws Exception { + + File targetDir = new File( System.getProperty( "user.dir" ), "target" ); + File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() ); + gatewayDir.mkdirs(); + gatewayHome = gatewayDir; + + GatewayTestConfig testConfig = new GatewayTestConfig(); + config = testConfig; + testConfig.setGatewayHomeDir( gatewayDir.getAbsolutePath() ); + + File topoDir = new File( testConfig.getGatewayTopologyDir() ); + topoDir.mkdirs(); + + File deployDir = new File( testConfig.getGatewayDeploymentDir() ); + deployDir.mkdirs(); + + DefaultGatewayServices srvcs = new DefaultGatewayServices(); + Map<String,String> options = new HashMap<String,String>(); + options.put( "persist-master", "false" ); + options.put( "master", "password" ); + try { + srvcs.init( testConfig, options ); + } catch ( ServiceLifecycleException e ) { + e.printStackTrace(); // I18N not required. + } + gateway = GatewayServer.startGateway( testConfig, srvcs ); + MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() ); + + LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() ); + + gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath(); + clusterUrl = gatewayUrl + "/test-cluster"; + } + + @After + public void cleanupGateway() throws Exception { + gateway.stop(); + FileUtils.deleteQuietly( gatewayHome ); + } + + private static XMLTag createTopology() { + XMLTag xml = XMLDoc.newDocument( true ) + .addRoot( "topology" ) + .addTag( "gateway" ) + + .addTag( "provider" ) + .addTag( "role" ).addText( "authentication" ) + .addTag( "name" ).addText( "ShiroProvider" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm" ) + .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm" ).gotoParent() + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.userDnTemplate" ) + .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ).gotoParent() + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.url" ) + .addTag( "value" ).addText( "ldap://localhost:" + ldapTransport.getPort() ).gotoParent() + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.authenticationMechanism" ) + .addTag( "value" ).addText( "simple" ).gotoParent() + .addTag( "param" ) + .addTag( "name" ).addText( "urls./**" ) + .addTag( "value" ).addText( "authcBasic" ).gotoParent().gotoParent() + .addTag( "provider" ) + .addTag( "role" ).addText( "identity-assertion" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "name" ).addText( "Default" ).gotoParent() + .addTag( "provider" ) + .gotoRoot() + .addTag( "service" ) + .addTag( "role" ).addText( "test-service-role" ) + .gotoRoot(); + return xml; + } + + private static int findFreePort() throws IOException { + ServerSocket socket = new ServerSocket(0); + int port = socket.getLocalPort(); + socket.close(); + return port; + } + + public static InputStream getResourceStream( String resource ) throws IOException { + return getResourceUrl( resource ).openStream(); + } + + public static URL getResourceUrl( String resource ) { + URL url = ClassLoader.getSystemResource( getResourceName( resource ) ); + assertThat( "Failed to find test resource " + resource, url, Matchers.notNullValue() ); + return url; + } + + public static String getResourceName( String resource ) { + return getResourceBaseName() + resource; + } + + public static String getResourceBaseName() { + return RESOURCE_BASE_CLASS.getName().replaceAll( "\\.", "/" ) + "/"; + } + + //@Test + public void waitForManualTesting() throws IOException { + System.in.read(); + } + + @Test( timeout = LONG_TIMEOUT ) + public void testDeployRedeployUndeploy() throws InterruptedException, IOException { + LOG_ENTER(); + long sleep = 200; + int numFilesInWar = 5; + String username = "guest"; + String password = "guest-password"; + String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; + long topoTimestampBefore, topoTimestampAfter; + + File topoDir = new File( config.getGatewayTopologyDir() ); + File deployDir = new File( config.getGatewayDeploymentDir() ); + File warDir; + + // Make sure deployment directory is empty. + assertThat( topoDir.listFiles().length, is( 0 ) ); + assertThat( deployDir.listFiles().length, is( 0 ) ); + + File descriptor = writeTestTopology( "test-cluster", createTopology() ); + long writeTime = System.currentTimeMillis(); + + warDir = waitForFiles( deployDir, "test-cluster.war\\.[0-9A-Fa-f]+", 1, 0, sleep ); + for( File webInfDir : warDir.listFiles() ) { + waitForFiles( webInfDir, ".*", numFilesInWar, 0, sleep ); + } + waitForAccess( serviceUrl, username, password, sleep ); + + // Wait to make sure a second has passed to ensure the the file timestamps are different. + waitForElapsed( writeTime, 1000, 100 ); + + // Redeploy and make sure the timestamp is updated. + topoTimestampBefore = descriptor.lastModified(); + GatewayServer.redeployTopologies( null ); + writeTime = System.currentTimeMillis(); + topoTimestampAfter = descriptor.lastModified(); + assertThat( topoTimestampAfter, greaterThan( topoTimestampBefore ) ); + + // Check to make sure there are two war directories with the same root. + warDir = waitForFiles( deployDir, "test-cluster.war\\.[0-9A-Fa-f]+", 2, 1, sleep ); + for( File webInfDir : warDir.listFiles() ) { + waitForFiles( webInfDir, ".*", numFilesInWar, 0, sleep ); + } + waitForAccess( serviceUrl, username, password, sleep ); + + // Wait to make sure a second has passed to ensure the the file timestamps are different. + waitForElapsed( writeTime, 1000, 100 ); + + // Redeploy and make sure the timestamp is updated. + topoTimestampBefore = descriptor.lastModified(); + GatewayServer.redeployTopologies( "test-cluster" ); + writeTime = System.currentTimeMillis(); + topoTimestampAfter = descriptor.lastModified(); + assertThat( topoTimestampAfter, greaterThan( topoTimestampBefore ) ); + + // Check to make sure there are two war directories with the same root. + warDir = waitForFiles( deployDir, "test-cluster.war\\.[0-9A-Fa-f]+", 3, 2, sleep ); + for( File webInfDir : warDir.listFiles() ) { + waitForFiles( webInfDir, ".*", numFilesInWar, 0, sleep ); + } + waitForAccess( serviceUrl, username, password, sleep ); + + // Delete the test topology. + assertThat( "Failed to delete the topology file.", descriptor.delete(), is( true ) ); + + // Wait to make sure a second has passed to ensure the the file timestamps are different. + waitForElapsed( writeTime, 1000, 100 ); + + waitForFiles( deployDir, ".*", 0, -1, sleep ); + + // Wait a bit more to make sure undeployment finished. + Thread.sleep( sleep ); + + // Make sure the test topology is not accessible. + given().auth().preemptive().basic( username, password ) + .expect().statusCode( HttpStatus.SC_NOT_FOUND ) + .when().get( serviceUrl ); + + // Make sure deployment directory is empty. + assertThat( topoDir.listFiles().length, is( 0 ) ); + assertThat( deployDir.listFiles().length, is( 0 ) ); + LOG_EXIT(); + } + + private void waitForElapsed( long from, long total, long sleep ) throws InterruptedException { + while( System.currentTimeMillis() - from < total ) { + Thread.sleep( sleep ); + } + } + + private File writeTestTopology( String name, XMLTag xml ) throws IOException { + // Create the test topology. + File tempFile = new File( config.getGatewayTopologyDir(), name + ".xml." + UUID.randomUUID() ); + FileOutputStream stream = new FileOutputStream( tempFile ); + xml.toStream( stream ); + stream.close(); + File descriptor = new File( config.getGatewayTopologyDir(), name + ".xml" ); + tempFile.renameTo( descriptor ); + return descriptor; + } + + private File waitForFiles( File dir, String pattern, int count, int index, long sleep ) throws InterruptedException { + RegexDirFilter filter = new RegexDirFilter( pattern ); + while( true ) { + File[] files = dir.listFiles( filter ); + if( files.length == count ) { + return ( index < 0 ) ? null : files[ index ]; + } + Thread.sleep( sleep ); + } + } + + private void waitForAccess( String url, String username, String password, long sleep ) throws InterruptedException { + while( true ) { + Response response = given() + .auth().preemptive().basic( username, password ) + .when().get( url ).andReturn(); + if( response.getStatusCode() == HttpStatus.SC_NOT_FOUND ) { + Thread.sleep( sleep ); + continue; + } + assertThat( response.getContentType(), containsString( "text/plain" ) ); + assertThat( response.getBody().asString(), is( "test-service-response" ) ); + break; + } + } + + private class RegexDirFilter implements FilenameFilter { + + Pattern pattern; + + RegexDirFilter( String regex ) { + pattern = Pattern.compile( regex ); + } + + @Override + public boolean accept( File dir, String name ) { + return pattern.matcher( name ).matches(); + } + } }
http://git-wip-us.apache.org/repos/asf/knox/blob/56cedc0a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapDynamicGroupFuncTest.java ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapDynamicGroupFuncTest.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapDynamicGroupFuncTest.java index 5eafc0d..bcede37 100755 --- a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapDynamicGroupFuncTest.java +++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapDynamicGroupFuncTest.java @@ -67,272 +67,272 @@ import com.mycila.xmltool.XMLTag; */ public class GatewayLdapDynamicGroupFuncTest { -// private static final long SHORT_TIMEOUT = 2000L; -// private static final long MEDIUM_TIMEOUT = 5 * SHORT_TIMEOUT; -// -// private static Class RESOURCE_BASE_CLASS = GatewayLdapDynamicGroupFuncTest.class; -// private static Logger LOG = LoggerFactory.getLogger( GatewayLdapDynamicGroupFuncTest.class ); -// -// public static Enumeration<Appender> appenders; -// public static GatewayConfig config; -// public static GatewayServer gateway; -// public static String gatewayUrl; -// public static String clusterUrl; -// public static SimpleLdapDirectoryServer ldap; -// public static TcpTransport ldapTransport; -// -// @BeforeClass -// public static void setupSuite() throws Exception { -// LOG_ENTER(); -// //appenders = NoOpAppender.setUp(); -// int port = setupLdap(); -// setupGateway(port); -// LOG_EXIT(); -// } -// -// @AfterClass -// public static void cleanupSuite() throws Exception { -// LOG_ENTER(); -// gateway.stop(); -// ldap.stop( true ); -// //FileUtils.deleteQuietly( new File( config.getGatewayHomeDir() ) ); -// //NoOpAppender.tearDown( appenders ); -// LOG_EXIT(); -// } -// -// public static int setupLdap() throws Exception { -// URL usersUrl = getResourceUrl( "users.ldif" ); -// int port = findFreePort(); -// ldapTransport = new TcpTransport( port ); -// ldap = new SimpleLdapDirectoryServer( "dc=hadoop,dc=apache,dc=org", new File( usersUrl.toURI() ), ldapTransport ); -// ldap.start(); -// LOG.info( "LDAP port = " + ldapTransport.getPort() ); -// return port; -// } -// -// public static void setupGateway(int ldapPort) throws IOException, Exception { -// -// File targetDir = new File( System.getProperty( "user.dir" ), "target" ); -// File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() ); -// gatewayDir.mkdirs(); -// -// GatewayTestConfig testConfig = new GatewayTestConfig(); -// config = testConfig; -// testConfig.setGatewayHomeDir( gatewayDir.getAbsolutePath() ); -// -// File topoDir = new File( testConfig.getGatewayTopologyDir() ); -// topoDir.mkdirs(); -// -// File deployDir = new File( testConfig.getGatewayDeploymentDir() ); -// deployDir.mkdirs(); -// -// File descriptor = new File( topoDir, "testdg-cluster.xml" ); -// FileOutputStream stream = new FileOutputStream( descriptor ); -// createTopology(ldapPort).toStream( stream ); -// stream.close(); -// -// DefaultGatewayServices srvcs = new DefaultGatewayServices(); -// Map<String,String> options = new HashMap<String,String>(); -// options.put( "persist-master", "false" ); -// options.put( "master", "password" ); -// try { -// srvcs.init( testConfig, options ); -// } catch ( ServiceLifecycleException e ) { -// e.printStackTrace(); // I18N not required. -// } -// -// /* -// System.setProperty(GatewayConfig.GATEWAY_HOME_VAR, gatewayDir.getAbsolutePath()); -// System.err.println("GH 10: " + System.getProperty(GatewayConfig.GATEWAY_HOME_VAR)); -// ByteArrayOutputStream outContent = new ByteArrayOutputStream(); -// System.setOut(new PrintStream(outContent)); -// String[] argvals = {"create-alias", "ldcSystemPassword", "--value", "guest-password", "--master", "hadoop", "--cluster", "testdg-cluster"}; -// KnoxCLI cli = new KnoxCLI(); -// cli.setConf(new GatewayConfigImpl()); -// cli.run(argvals); -// -// outContent.reset(); -// String[] args1 = {"list-alias", "--cluster", "testdg-cluster", "--master", "hadoop"}; -// cli = new KnoxCLI(); -// cli.run(args1); -// System.err.println("ALIAS LIST: " + outContent.toString()); -// -// AliasService as1 = cli.getGatewayServices().getService(GatewayServices.ALIAS_SERVICE); -// char[] passwordChars1 = as1.getPasswordFromAliasForCluster( "test-cluster", "ldcsystemPassword"); -// System.err.println("ALIAS value1: " + new String(passwordChars1)); -// */ -// -// gateway = GatewayServer.startGateway( testConfig, srvcs ); -// MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() ); -// -// LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() ); -// -// gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath(); -// clusterUrl = gatewayUrl + "/testdg-cluster"; -// -// ///* -// GatewayServices services = GatewayServer.getGatewayServices(); -// AliasService aliasService = (AliasService)services.getService(GatewayServices.ALIAS_SERVICE); -// aliasService.addAliasForCluster("testdg-cluster", "ldcSystemPassword", "guest-password"); -// -// char[] password1 = aliasService.getPasswordFromAliasForCluster( "testdg-cluster", "ldcSystemPassword"); -// //System.err.println("SETUP password 10: " + ((password1 == null) ? "NULL" : new String(password1))); -// -// descriptor = new File( topoDir, "testdg-cluster.xml" ); -// stream = new FileOutputStream( descriptor ); -// createTopology(ldapPort).toStream( stream ); -// stream.close(); -// -// try { -// Thread.sleep(5000); -// } catch (Exception e) { -// -// } -// //*/ -// } -// -// private static XMLTag createTopology(int ldapPort) { -// XMLTag xml = XMLDoc.newDocument( true ) -// .addRoot( "topology" ) -// .addTag( "gateway" ) -// -// .addTag( "provider" ) -// .addTag( "role" ).addText( "authentication" ) -// .addTag( "name" ).addText( "ShiroProvider" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm" ) -// .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapGroupContextFactory" ) -// .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapContextFactory" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory" ) -// .addTag( "value" ).addText( "$ldapGroupContextFactory" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.authenticationMechanism" ) -// .addTag( "value" ).addText( "simple" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.url" ) -// .addTag( "value" ).addText( "ldap://localhost:" + ldapPort) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.userDnTemplate" ) -// .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.authorizationEnabled" ) -// .addTag( "value" ).addText( "true" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemAuthenticationMechanism" ) -// .addTag( "value" ).addText( "simple" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.searchBase" ) -// .addTag( "value" ).addText( "ou=groups,dc=hadoop,dc=apache,dc=org" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.groupObjectClass" ) -// .addTag( "value" ).addText( "groupofurls" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.memberAttribute" ) -// .addTag( "value" ).addText( "memberurl" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.memberAttributeValueTemplate" ) -// .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemUsername" ) -// .addTag( "value" ).addText( "uid=guest,ou=people,dc=hadoop,dc=apache,dc=org" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.clusterName" ) -// .addTag( "value" ).addText( "testdg-cluster" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemPassword" ) -// .addTag( "value" ).addText( "S{ALIAS=ldcSystemPassword}" ) -// // .addTag( "value" ).addText( "guest-password" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "urls./**" ) -// .addTag( "value" ).addText( "authcBasic" ) -// -// .gotoParent().gotoParent().addTag( "provider" ) -// .addTag( "role" ).addText( "authorization" ) -// .addTag( "name" ).addText( "AclsAuthz" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "param" ) -// .addTag( "name" ).addText( "test-service-role.acl" ) // FIXME[dilli] -// .addTag( "value" ).addText( "*;directors;*" ) -// -// .gotoParent().gotoParent().addTag( "provider" ) -// .addTag( "role" ).addText( "identity-assertion" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "name" ).addText( "Default" ).gotoParent() -// -// .gotoRoot() -// .addTag( "service" ) -// .addTag( "role" ).addText( "test-service-role" ) -// .gotoRoot(); -// // System.out.println( "GATEWAY=" + xml.toString() ); -// return xml; -// } -// -// private static int findFreePort() throws IOException { -// ServerSocket socket = new ServerSocket(0); -// int port = socket.getLocalPort(); -// socket.close(); -// return port; -// } -// -// public static InputStream getResourceStream( String resource ) throws IOException { -// return getResourceUrl( resource ).openStream(); -// } -// -// public static URL getResourceUrl( String resource ) { -// URL url = ClassLoader.getSystemResource( getResourceName( resource ) ); -// assertThat( "Failed to find test resource " + resource, url, Matchers.notNullValue() ); -// return url; -// } -// -// public static String getResourceName( String resource ) { -// return getResourceBaseName() + resource; -// } -// -// public static String getResourceBaseName() { -// return RESOURCE_BASE_CLASS.getName().replaceAll( "\\.", "/" ) + "/"; -// } -// -// // @Test -// public void waitForManualTesting() throws IOException { -// System.in.read(); -// } -// -// @Test( timeout = MEDIUM_TIMEOUT ) -// public void testGroupMember() throws ClassNotFoundException, Exception { -// LOG_ENTER(); -// String username = "bob"; -// String password = "bob-password"; -// String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; -// given() -// //.log().all() -// .auth().preemptive().basic( username, password ) -// .expect() -// //.log().all() -// .statusCode( HttpStatus.SC_OK ) -// .contentType( "text/plain" ) -// .body( is( "test-service-response" ) ) -// .when().get( serviceUrl ); -// LOG_EXIT(); -// } -// -// @Test( timeout = MEDIUM_TIMEOUT ) -// public void testNonGroupMember() throws ClassNotFoundException { -// LOG_ENTER(); -// String username = "guest"; -// String password = "guest-password"; -// String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; -// given() -// //.log().all() -// .auth().preemptive().basic( username, password ) -// .expect() -// //.log().all() -// .statusCode( HttpStatus.SC_FORBIDDEN ) -// .when().get( serviceUrl ); -// LOG_EXIT(); -// } + private static final long SHORT_TIMEOUT = 2000L; + private static final long MEDIUM_TIMEOUT = 5 * SHORT_TIMEOUT; + + private static Class RESOURCE_BASE_CLASS = GatewayLdapDynamicGroupFuncTest.class; + private static Logger LOG = LoggerFactory.getLogger( GatewayLdapDynamicGroupFuncTest.class ); + + public static Enumeration<Appender> appenders; + public static GatewayConfig config; + public static GatewayServer gateway; + public static String gatewayUrl; + public static String clusterUrl; + public static SimpleLdapDirectoryServer ldap; + public static TcpTransport ldapTransport; + + @BeforeClass + public static void setupSuite() throws Exception { + LOG_ENTER(); + //appenders = NoOpAppender.setUp(); + int port = setupLdap(); + setupGateway(port); + LOG_EXIT(); + } + + @AfterClass + public static void cleanupSuite() throws Exception { + LOG_ENTER(); + gateway.stop(); + ldap.stop( true ); + //FileUtils.deleteQuietly( new File( config.getGatewayHomeDir() ) ); + //NoOpAppender.tearDown( appenders ); + LOG_EXIT(); + } + + public static int setupLdap() throws Exception { + URL usersUrl = getResourceUrl( "users.ldif" ); + int port = findFreePort(); + ldapTransport = new TcpTransport( port ); + ldap = new SimpleLdapDirectoryServer( "dc=hadoop,dc=apache,dc=org", new File( usersUrl.toURI() ), ldapTransport ); + ldap.start(); + LOG.info( "LDAP port = " + ldapTransport.getPort() ); + return port; + } + + public static void setupGateway(int ldapPort) throws IOException, Exception { + + File targetDir = new File( System.getProperty( "user.dir" ), "target" ); + File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() ); + gatewayDir.mkdirs(); + + GatewayTestConfig testConfig = new GatewayTestConfig(); + config = testConfig; + testConfig.setGatewayHomeDir( gatewayDir.getAbsolutePath() ); + + File topoDir = new File( testConfig.getGatewayTopologyDir() ); + topoDir.mkdirs(); + + File deployDir = new File( testConfig.getGatewayDeploymentDir() ); + deployDir.mkdirs(); + + File descriptor = new File( topoDir, "testdg-cluster.xml" ); + FileOutputStream stream = new FileOutputStream( descriptor ); + createTopology(ldapPort).toStream( stream ); + stream.close(); + + DefaultGatewayServices srvcs = new DefaultGatewayServices(); + Map<String,String> options = new HashMap<String,String>(); + options.put( "persist-master", "false" ); + options.put( "master", "password" ); + try { + srvcs.init( testConfig, options ); + } catch ( ServiceLifecycleException e ) { + e.printStackTrace(); // I18N not required. + } + + /* + System.setProperty(GatewayConfig.GATEWAY_HOME_VAR, gatewayDir.getAbsolutePath()); + System.err.println("GH 10: " + System.getProperty(GatewayConfig.GATEWAY_HOME_VAR)); + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + String[] argvals = {"create-alias", "ldcSystemPassword", "--value", "guest-password", "--master", "hadoop", "--cluster", "testdg-cluster"}; + KnoxCLI cli = new KnoxCLI(); + cli.setConf(new GatewayConfigImpl()); + cli.run(argvals); + + outContent.reset(); + String[] args1 = {"list-alias", "--cluster", "testdg-cluster", "--master", "hadoop"}; + cli = new KnoxCLI(); + cli.run(args1); + System.err.println("ALIAS LIST: " + outContent.toString()); + + AliasService as1 = cli.getGatewayServices().getService(GatewayServices.ALIAS_SERVICE); + char[] passwordChars1 = as1.getPasswordFromAliasForCluster( "test-cluster", "ldcsystemPassword"); + System.err.println("ALIAS value1: " + new String(passwordChars1)); + */ + + gateway = GatewayServer.startGateway( testConfig, srvcs ); + MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() ); + + LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() ); + + gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath(); + clusterUrl = gatewayUrl + "/testdg-cluster"; + + ///* + GatewayServices services = GatewayServer.getGatewayServices(); + AliasService aliasService = (AliasService)services.getService(GatewayServices.ALIAS_SERVICE); + aliasService.addAliasForCluster("testdg-cluster", "ldcSystemPassword", "guest-password"); + + char[] password1 = aliasService.getPasswordFromAliasForCluster( "testdg-cluster", "ldcSystemPassword"); + //System.err.println("SETUP password 10: " + ((password1 == null) ? "NULL" : new String(password1))); + + descriptor = new File( topoDir, "testdg-cluster.xml" ); + stream = new FileOutputStream( descriptor ); + createTopology(ldapPort).toStream( stream ); + stream.close(); + + try { + Thread.sleep(5000); + } catch (Exception e) { + + } + //*/ + } + + private static XMLTag createTopology(int ldapPort) { + XMLTag xml = XMLDoc.newDocument( true ) + .addRoot( "topology" ) + .addTag( "gateway" ) + + .addTag( "provider" ) + .addTag( "role" ).addText( "authentication" ) + .addTag( "name" ).addText( "ShiroProvider" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm" ) + .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapGroupContextFactory" ) + .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapContextFactory" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory" ) + .addTag( "value" ).addText( "$ldapGroupContextFactory" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.authenticationMechanism" ) + .addTag( "value" ).addText( "simple" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.url" ) + .addTag( "value" ).addText( "ldap://localhost:" + ldapPort) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.userDnTemplate" ) + .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.authorizationEnabled" ) + .addTag( "value" ).addText( "true" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemAuthenticationMechanism" ) + .addTag( "value" ).addText( "simple" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.searchBase" ) + .addTag( "value" ).addText( "ou=groups,dc=hadoop,dc=apache,dc=org" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.groupObjectClass" ) + .addTag( "value" ).addText( "groupofurls" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.memberAttribute" ) + .addTag( "value" ).addText( "memberurl" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.memberAttributeValueTemplate" ) + .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemUsername" ) + .addTag( "value" ).addText( "uid=guest,ou=people,dc=hadoop,dc=apache,dc=org" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.clusterName" ) + .addTag( "value" ).addText( "testdg-cluster" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemPassword" ) + .addTag( "value" ).addText( "S{ALIAS=ldcSystemPassword}" ) + // .addTag( "value" ).addText( "guest-password" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "urls./**" ) + .addTag( "value" ).addText( "authcBasic" ) + + .gotoParent().gotoParent().addTag( "provider" ) + .addTag( "role" ).addText( "authorization" ) + .addTag( "name" ).addText( "AclsAuthz" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "param" ) + .addTag( "name" ).addText( "test-service-role.acl" ) // FIXME[dilli] + .addTag( "value" ).addText( "*;directors;*" ) + + .gotoParent().gotoParent().addTag( "provider" ) + .addTag( "role" ).addText( "identity-assertion" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "name" ).addText( "Default" ).gotoParent() + + .gotoRoot() + .addTag( "service" ) + .addTag( "role" ).addText( "test-service-role" ) + .gotoRoot(); + // System.out.println( "GATEWAY=" + xml.toString() ); + return xml; + } + + private static int findFreePort() throws IOException { + ServerSocket socket = new ServerSocket(0); + int port = socket.getLocalPort(); + socket.close(); + return port; + } + + public static InputStream getResourceStream( String resource ) throws IOException { + return getResourceUrl( resource ).openStream(); + } + + public static URL getResourceUrl( String resource ) { + URL url = ClassLoader.getSystemResource( getResourceName( resource ) ); + assertThat( "Failed to find test resource " + resource, url, Matchers.notNullValue() ); + return url; + } + + public static String getResourceName( String resource ) { + return getResourceBaseName() + resource; + } + + public static String getResourceBaseName() { + return RESOURCE_BASE_CLASS.getName().replaceAll( "\\.", "/" ) + "/"; + } + + // @Test + public void waitForManualTesting() throws IOException { + System.in.read(); + } + + @Test( timeout = MEDIUM_TIMEOUT ) + public void testGroupMember() throws ClassNotFoundException, Exception { + LOG_ENTER(); + String username = "bob"; + String password = "bob-password"; + String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; + given() + //.log().all() + .auth().preemptive().basic( username, password ) + .expect() + //.log().all() + .statusCode( HttpStatus.SC_OK ) + .contentType( "text/plain" ) + .body( is( "test-service-response" ) ) + .when().get( serviceUrl ); + LOG_EXIT(); + } + + @Test( timeout = MEDIUM_TIMEOUT ) + public void testNonGroupMember() throws ClassNotFoundException { + LOG_ENTER(); + String username = "guest"; + String password = "guest-password"; + String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; + given() + //.log().all() + .auth().preemptive().basic( username, password ) + .expect() + //.log().all() + .statusCode( HttpStatus.SC_FORBIDDEN ) + .when().get( serviceUrl ); + LOG_EXIT(); + } } http://git-wip-us.apache.org/repos/asf/knox/blob/56cedc0a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapGroupFuncTest.java ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapGroupFuncTest.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapGroupFuncTest.java index 6c60931..8719473 100644 --- a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapGroupFuncTest.java +++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLdapGroupFuncTest.java @@ -67,273 +67,273 @@ import com.mycila.xmltool.XMLTag; */ public class GatewayLdapGroupFuncTest { -// private static final long SHORT_TIMEOUT = 2000L; -// private static final long MEDIUM_TIMEOUT = 5 * 1000L; -// -// private static Class RESOURCE_BASE_CLASS = GatewayLdapGroupFuncTest.class; -// private static Logger LOG = LoggerFactory.getLogger( GatewayLdapGroupFuncTest.class ); -// -// public static Enumeration<Appender> appenders; -// public static GatewayConfig config; -// public static GatewayServer gateway; -// public static String gatewayUrl; -// public static String clusterUrl; -// public static SimpleLdapDirectoryServer ldap; -// public static TcpTransport ldapTransport; -// -// @BeforeClass -// public static void setupSuite() throws Exception { -// LOG_ENTER(); -// //appenders = NoOpAppender.setUp(); -// int port = setupLdap(); -// setupGateway(port); -// LOG_EXIT(); -// } -// -// @AfterClass -// public static void cleanupSuite() throws Exception { -// LOG_ENTER(); -// gateway.stop(); -// ldap.stop( true ); -// //FileUtils.deleteQuietly( new File( config.getGatewayHomeDir() ) ); -// //NoOpAppender.tearDown( appenders ); -// LOG_EXIT(); -// } -// -// public static int setupLdap() throws Exception { -// URL usersUrl = getResourceUrl( "users.ldif" ); -// int port = findFreePort(); -// ldapTransport = new TcpTransport( port ); -// ldap = new SimpleLdapDirectoryServer( "dc=hadoop,dc=apache,dc=org", new File( usersUrl.toURI() ), ldapTransport ); -// ldap.start(); -// LOG.info( "LDAP port = " + ldapTransport.getPort() ); -// return port; -// } -// -// public static void setupGateway(int ldapPort) throws Exception { -// -// File targetDir = new File( System.getProperty( "user.dir" ), "target" ); -// File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() ); -// gatewayDir.mkdirs(); -// -// GatewayTestConfig testConfig = new GatewayTestConfig(); -// config = testConfig; -// testConfig.setGatewayHomeDir( gatewayDir.getAbsolutePath() ); -// -// File topoDir = new File( testConfig.getGatewayTopologyDir() ); -// topoDir.mkdirs(); -// -// File deployDir = new File( testConfig.getGatewayDeploymentDir() ); -// deployDir.mkdirs(); -// -// File descriptor = new File( topoDir, "test-cluster.xml" ); -// FileOutputStream stream = new FileOutputStream( descriptor ); -// createTopology(ldapPort).toStream( stream ); -// stream.close(); -// -// DefaultGatewayServices srvcs = new DefaultGatewayServices(); -// Map<String,String> options = new HashMap<String,String>(); -// options.put( "persist-master", "true" ); -// options.put( "master", "hadoop" ); -// -// try { -// srvcs.init( testConfig, options ); -// } catch ( ServiceLifecycleException e ) { -// e.printStackTrace(); // I18N not required. -// } -// -// /* -// System.setProperty(GatewayConfig.GATEWAY_HOME_VAR, gatewayDir.getAbsolutePath()); -// System.err.println("GH 10: " + System.getProperty(GatewayConfig.GATEWAY_HOME_VAR)); -// ByteArrayOutputStream outContent = new ByteArrayOutputStream(); -// System.setOut(new PrintStream(outContent)); -// String[] argvals = {"create-alias", "ldcSystemPassword", "--value", "guest-password", "--master", "hadoop", "--cluster", "test-cluster"}; -// KnoxCLI cli = new KnoxCLI(); -// cli.setConf(new GatewayConfigImpl()); -// cli.run(argvals); -// -// outContent.reset(); -// String[] args1 = {"list-alias", "--cluster", "test-cluster", "--master", "hadoop"}; -// cli = new KnoxCLI(); -// cli.run(args1); -// System.err.println("ALIAS LIST: " + outContent.toString()); -// -// AliasService as1 = cli.getGatewayServices().getService(GatewayServices.ALIAS_SERVICE); -// char[] passwordChars1 = as1.getPasswordFromAliasForCluster( "test-cluster", "ldcsystemPassword"); -// System.err.println("ALIAS value1: " + new String(passwordChars1)); -// */ -// -// gateway = GatewayServer.startGateway( testConfig, srvcs ); -// MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() ); -// -// LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() ); -// -// gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath(); -// clusterUrl = gatewayUrl + "/test-cluster"; -// -// ///* -// GatewayServices services = GatewayServer.getGatewayServices(); -// AliasService aliasService = (AliasService)services.getService(GatewayServices.ALIAS_SERVICE); -// aliasService.addAliasForCluster("test-cluster", "ldcSystemPassword", "guest-password"); -// -// char[] password1 = aliasService.getPasswordFromAliasForCluster( "test-cluster", "ldcSystemPassword"); -// //System.err.println("SETUP password 10: " + ((password1 == null) ? "NULL" : new String(password1))); -// -// descriptor = new File( topoDir, "test-cluster.xml" ); -// stream = new FileOutputStream( descriptor ); -// createTopology(ldapPort).toStream( stream ); -// stream.close(); -// -// try { -// Thread.sleep(5000); -// } catch (Exception e) { -// -// } -// //*/ -// } -// -// private static XMLTag createTopology(int ldapPort) { -// XMLTag xml = XMLDoc.newDocument( true ) -// .addRoot( "topology" ) -// .addTag( "gateway" ) -// -// .addTag( "provider" ) -// .addTag( "role" ).addText( "authentication" ) -// .addTag( "name" ).addText( "ShiroProvider" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm" ) -// .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapGroupContextFactory" ) -// .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapContextFactory" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory" ) -// .addTag( "value" ).addText( "$ldapGroupContextFactory" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.authenticationMechanism" ) -// .addTag( "value" ).addText( "simple" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.url" ) -// .addTag( "value" ).addText( "ldap://localhost:" + ldapPort) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.userDnTemplate" ) -// .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.authorizationEnabled" ) -// .addTag( "value" ).addText( "true" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemAuthenticationMechanism" ) -// .addTag( "value" ).addText( "simple" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.searchBase" ) -// .addTag( "value" ).addText( "ou=groups,dc=hadoop,dc=apache,dc=org" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.groupObjectClass" ) -// .addTag( "value" ).addText( "groupofnames" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.memberAttribute" ) -// .addTag( "value" ).addText( "member" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.memberAttributeValueTemplate" ) -// .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.clusterName" ) -// .addTag( "value" ).addText( "test-cluster" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemUsername" ) -// .addTag( "value" ).addText( "uid=guest,ou=people,dc=hadoop,dc=apache,dc=org" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemPassword" ) -// .addTag( "value" ).addText( "S{ALIAS=ldcSystemPassword}" ) -// .gotoParent().addTag( "param" ) -// .addTag( "name" ).addText( "urls./**" ) -// .addTag( "value" ).addText( "authcBasic" ) -// -// .gotoParent().gotoParent().addTag( "provider" ) -// .addTag( "role" ).addText( "authorization" ) -// .addTag( "name" ).addText( "AclsAuthz" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "param" ) -// .addTag( "name" ).addText( "test-service-role.acl" ) // FIXME[dilli] -// .addTag( "value" ).addText( "*;analyst;*" ) -// -// .gotoParent().gotoParent().addTag( "provider" ) -// .addTag( "role" ).addText( "identity-assertion" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "name" ).addText( "Default" ).gotoParent() -// -// .gotoRoot() -// .addTag( "service" ) -// .addTag( "role" ).addText( "test-service-role" ) -// .gotoRoot(); -// // System.out.println( "GATEWAY=" + xml.toString() ); -// return xml; -// } -// -// private static int findFreePort() throws IOException { -// ServerSocket socket = new ServerSocket(0); -// int port = socket.getLocalPort(); -// socket.close(); -// return port; -// } -// -// public static InputStream getResourceStream( String resource ) throws IOException { -// return getResourceUrl( resource ).openStream(); -// } -// -// public static URL getResourceUrl( String resource ) { -// URL url = ClassLoader.getSystemResource( getResourceName( resource ) ); -// assertThat( "Failed to find test resource " + resource, url, Matchers.notNullValue() ); -// return url; -// } -// -// public static String getResourceName( String resource ) { -// return getResourceBaseName() + resource; -// } -// -// public static String getResourceBaseName() { -// return RESOURCE_BASE_CLASS.getName().replaceAll( "\\.", "/" ) + "/"; -// } -// -// @Ignore -// // @Test -// public void waitForManualTesting() throws IOException { -// System.in.read(); -// } -// -// @Test( timeout = MEDIUM_TIMEOUT ) -// public void testGroupMember() throws ClassNotFoundException, Exception { -// LOG_ENTER(); -// String username = "sam"; -// String password = "sam-password"; -// String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; -// given() -// //.log().all() -// .auth().preemptive().basic( username, password ) -// .expect() -// //.log().all() -// .statusCode( HttpStatus.SC_OK ) -// .contentType( "text/plain" ) -// .body( is( "test-service-response" ) ) -// .when().get( serviceUrl ); -// LOG_EXIT(); -// } -// -// @Test( timeout = MEDIUM_TIMEOUT ) -// public void testNonGroupMember() throws ClassNotFoundException { -// LOG_ENTER(); -// String username = "guest"; -// String password = "guest-password"; -// String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; -// given() -// //.log().all() -// .auth().preemptive().basic( username, password ) -// .expect() -// //.log().all() -// .statusCode( HttpStatus.SC_FORBIDDEN ) -// .when().get( serviceUrl ); -// LOG_EXIT(); -// } + private static final long SHORT_TIMEOUT = 2000L; + private static final long MEDIUM_TIMEOUT = 5 * 1000L; + + private static Class RESOURCE_BASE_CLASS = GatewayLdapGroupFuncTest.class; + private static Logger LOG = LoggerFactory.getLogger( GatewayLdapGroupFuncTest.class ); + + public static Enumeration<Appender> appenders; + public static GatewayConfig config; + public static GatewayServer gateway; + public static String gatewayUrl; + public static String clusterUrl; + public static SimpleLdapDirectoryServer ldap; + public static TcpTransport ldapTransport; + + @BeforeClass + public static void setupSuite() throws Exception { + LOG_ENTER(); + //appenders = NoOpAppender.setUp(); + int port = setupLdap(); + setupGateway(port); + LOG_EXIT(); + } + + @AfterClass + public static void cleanupSuite() throws Exception { + LOG_ENTER(); + gateway.stop(); + ldap.stop( true ); + //FileUtils.deleteQuietly( new File( config.getGatewayHomeDir() ) ); + //NoOpAppender.tearDown( appenders ); + LOG_EXIT(); + } + + public static int setupLdap() throws Exception { + URL usersUrl = getResourceUrl( "users.ldif" ); + int port = findFreePort(); + ldapTransport = new TcpTransport( port ); + ldap = new SimpleLdapDirectoryServer( "dc=hadoop,dc=apache,dc=org", new File( usersUrl.toURI() ), ldapTransport ); + ldap.start(); + LOG.info( "LDAP port = " + ldapTransport.getPort() ); + return port; + } + + public static void setupGateway(int ldapPort) throws Exception { + + File targetDir = new File( System.getProperty( "user.dir" ), "target" ); + File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() ); + gatewayDir.mkdirs(); + + GatewayTestConfig testConfig = new GatewayTestConfig(); + config = testConfig; + testConfig.setGatewayHomeDir( gatewayDir.getAbsolutePath() ); + + File topoDir = new File( testConfig.getGatewayTopologyDir() ); + topoDir.mkdirs(); + + File deployDir = new File( testConfig.getGatewayDeploymentDir() ); + deployDir.mkdirs(); + + File descriptor = new File( topoDir, "test-cluster.xml" ); + FileOutputStream stream = new FileOutputStream( descriptor ); + createTopology(ldapPort).toStream( stream ); + stream.close(); + + DefaultGatewayServices srvcs = new DefaultGatewayServices(); + Map<String,String> options = new HashMap<String,String>(); + options.put( "persist-master", "true" ); + options.put( "master", "hadoop" ); + + try { + srvcs.init( testConfig, options ); + } catch ( ServiceLifecycleException e ) { + e.printStackTrace(); // I18N not required. + } + + /* + System.setProperty(GatewayConfig.GATEWAY_HOME_VAR, gatewayDir.getAbsolutePath()); + System.err.println("GH 10: " + System.getProperty(GatewayConfig.GATEWAY_HOME_VAR)); + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + String[] argvals = {"create-alias", "ldcSystemPassword", "--value", "guest-password", "--master", "hadoop", "--cluster", "test-cluster"}; + KnoxCLI cli = new KnoxCLI(); + cli.setConf(new GatewayConfigImpl()); + cli.run(argvals); + + outContent.reset(); + String[] args1 = {"list-alias", "--cluster", "test-cluster", "--master", "hadoop"}; + cli = new KnoxCLI(); + cli.run(args1); + System.err.println("ALIAS LIST: " + outContent.toString()); + + AliasService as1 = cli.getGatewayServices().getService(GatewayServices.ALIAS_SERVICE); + char[] passwordChars1 = as1.getPasswordFromAliasForCluster( "test-cluster", "ldcsystemPassword"); + System.err.println("ALIAS value1: " + new String(passwordChars1)); + */ + + gateway = GatewayServer.startGateway( testConfig, srvcs ); + MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() ); + + LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() ); + + gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath(); + clusterUrl = gatewayUrl + "/test-cluster"; + + ///* + GatewayServices services = GatewayServer.getGatewayServices(); + AliasService aliasService = (AliasService)services.getService(GatewayServices.ALIAS_SERVICE); + aliasService.addAliasForCluster("test-cluster", "ldcSystemPassword", "guest-password"); + + char[] password1 = aliasService.getPasswordFromAliasForCluster( "test-cluster", "ldcSystemPassword"); + //System.err.println("SETUP password 10: " + ((password1 == null) ? "NULL" : new String(password1))); + + descriptor = new File( topoDir, "test-cluster.xml" ); + stream = new FileOutputStream( descriptor ); + createTopology(ldapPort).toStream( stream ); + stream.close(); + + try { + Thread.sleep(5000); + } catch (Exception e) { + + } + //*/ + } + + private static XMLTag createTopology(int ldapPort) { + XMLTag xml = XMLDoc.newDocument( true ) + .addRoot( "topology" ) + .addTag( "gateway" ) + + .addTag( "provider" ) + .addTag( "role" ).addText( "authentication" ) + .addTag( "name" ).addText( "ShiroProvider" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm" ) + .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapGroupContextFactory" ) + .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapContextFactory" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory" ) + .addTag( "value" ).addText( "$ldapGroupContextFactory" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.authenticationMechanism" ) + .addTag( "value" ).addText( "simple" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.url" ) + .addTag( "value" ).addText( "ldap://localhost:" + ldapPort) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.userDnTemplate" ) + .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.authorizationEnabled" ) + .addTag( "value" ).addText( "true" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemAuthenticationMechanism" ) + .addTag( "value" ).addText( "simple" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.searchBase" ) + .addTag( "value" ).addText( "ou=groups,dc=hadoop,dc=apache,dc=org" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.groupObjectClass" ) + .addTag( "value" ).addText( "groupofnames" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.memberAttribute" ) + .addTag( "value" ).addText( "member" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.memberAttributeValueTemplate" ) + .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.clusterName" ) + .addTag( "value" ).addText( "test-cluster" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemUsername" ) + .addTag( "value" ).addText( "uid=guest,ou=people,dc=hadoop,dc=apache,dc=org" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.systemPassword" ) + .addTag( "value" ).addText( "S{ALIAS=ldcSystemPassword}" ) + .gotoParent().addTag( "param" ) + .addTag( "name" ).addText( "urls./**" ) + .addTag( "value" ).addText( "authcBasic" ) + + .gotoParent().gotoParent().addTag( "provider" ) + .addTag( "role" ).addText( "authorization" ) + .addTag( "name" ).addText( "AclsAuthz" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "param" ) + .addTag( "name" ).addText( "test-service-role.acl" ) // FIXME[dilli] + .addTag( "value" ).addText( "*;analyst;*" ) + + .gotoParent().gotoParent().addTag( "provider" ) + .addTag( "role" ).addText( "identity-assertion" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "name" ).addText( "Default" ).gotoParent() + + .gotoRoot() + .addTag( "service" ) + .addTag( "role" ).addText( "test-service-role" ) + .gotoRoot(); + // System.out.println( "GATEWAY=" + xml.toString() ); + return xml; + } + + private static int findFreePort() throws IOException { + ServerSocket socket = new ServerSocket(0); + int port = socket.getLocalPort(); + socket.close(); + return port; + } + + public static InputStream getResourceStream( String resource ) throws IOException { + return getResourceUrl( resource ).openStream(); + } + + public static URL getResourceUrl( String resource ) { + URL url = ClassLoader.getSystemResource( getResourceName( resource ) ); + assertThat( "Failed to find test resource " + resource, url, Matchers.notNullValue() ); + return url; + } + + public static String getResourceName( String resource ) { + return getResourceBaseName() + resource; + } + + public static String getResourceBaseName() { + return RESOURCE_BASE_CLASS.getName().replaceAll( "\\.", "/" ) + "/"; + } + + @Ignore + // @Test + public void waitForManualTesting() throws IOException { + System.in.read(); + } + + @Test( timeout = MEDIUM_TIMEOUT ) + public void testGroupMember() throws ClassNotFoundException, Exception { + LOG_ENTER(); + String username = "sam"; + String password = "sam-password"; + String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; + given() + //.log().all() + .auth().preemptive().basic( username, password ) + .expect() + //.log().all() + .statusCode( HttpStatus.SC_OK ) + .contentType( "text/plain" ) + .body( is( "test-service-response" ) ) + .when().get( serviceUrl ); + LOG_EXIT(); + } + + @Test( timeout = MEDIUM_TIMEOUT ) + public void testNonGroupMember() throws ClassNotFoundException { + LOG_ENTER(); + String username = "guest"; + String password = "guest-password"; + String serviceUrl = clusterUrl + "/test-service-path/test-service-resource"; + given() + //.log().all() + .auth().preemptive().basic( username, password ) + .expect() + //.log().all() + .statusCode( HttpStatus.SC_FORBIDDEN ) + .when().get( serviceUrl ); + LOG_EXIT(); + } } http://git-wip-us.apache.org/repos/asf/knox/blob/56cedc0a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLocalServiceFuncTest.java ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLocalServiceFuncTest.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLocalServiceFuncTest.java index 4475732..aa93422 100644 --- a/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLocalServiceFuncTest.java +++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/GatewayLocalServiceFuncTest.java @@ -56,170 +56,170 @@ import static org.junit.Assert.assertThat; public class GatewayLocalServiceFuncTest { -// private static final long SHORT_TIMEOUT = 2000L; -// private static final long MEDIUM_TIMEOUT = 5 * SHORT_TIMEOUT; -// -// private static Class RESOURCE_BASE_CLASS = GatewayLocalServiceFuncTest.class; -// private static Logger LOG = LoggerFactory.getLogger( GatewayFuncTestDriver.class ); -// -// public static Enumeration<Appender> appenders; -// public static GatewayConfig config; -// public static GatewayServer gateway; -// public static String gatewayUrl; -// public static String clusterUrl; -// public static SimpleLdapDirectoryServer ldap; -// public static TcpTransport ldapTransport; -// -// @BeforeClass -// public static void setupSuite() throws Exception { -// LOG_ENTER(); -// appenders = NoOpAppender.setUp(); -// setupLdap(); -// setupGateway(); -// LOG_EXIT(); -// } -// -// @AfterClass -// public static void cleanupSuite() throws Exception { -// LOG_ENTER(); -// gateway.stop(); -// ldap.stop( true ); -// FileUtils.deleteQuietly( new File( config.getGatewayConfDir() ) ); -// FileUtils.deleteQuietly( new File( config.getGatewayDataDir() ) ); -// NoOpAppender.tearDown( appenders ); -// LOG_EXIT(); -// } -// -// public static void setupLdap() throws Exception { -// URL usersUrl = getResourceUrl( "users.ldif" ); -// int port = findFreePort(); -// ldapTransport = new TcpTransport( port ); -// ldap = new SimpleLdapDirectoryServer( "dc=hadoop,dc=apache,dc=org", new File( usersUrl.toURI() ), ldapTransport ); -// ldap.start(); -// LOG.info( "LDAP port = " + ldapTransport.getPort() ); -// } -// -// public static void setupGateway() throws Exception { -// -// File targetDir = new File( System.getProperty( "user.dir" ), "target" ); -// File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() ); -// gatewayDir.mkdirs(); -// -// GatewayTestConfig testConfig = new GatewayTestConfig(); -// config = testConfig; -// testConfig.setGatewayHomeDir( gatewayDir.getAbsolutePath() ); -// -// File topoDir = new File( testConfig.getGatewayTopologyDir() ); -// topoDir.mkdirs(); -// -// File deployDir = new File( testConfig.getGatewayDeploymentDir() ); -// deployDir.mkdirs(); -// -// File descriptor = new File( topoDir, "cluster.xml" ); -// FileOutputStream stream = new FileOutputStream( descriptor ); -// createTopology().toStream( stream ); -// stream.close(); -// -// DefaultGatewayServices srvcs = new DefaultGatewayServices(); -// Map<String,String> options = new HashMap<String,String>(); -// options.put( "persist-master", "false" ); -// options.put( "master", "password" ); -// try { -// srvcs.init( testConfig, options ); -// } catch ( ServiceLifecycleException e ) { -// e.printStackTrace(); // I18N not required. -// } -// gateway = GatewayServer.startGateway( testConfig, srvcs ); -// MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() ); -// -// LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() ); -// -// gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath(); -// clusterUrl = gatewayUrl + "/cluster"; -// } -// -// private static XMLTag createTopology() { -// XMLTag xml = XMLDoc.newDocument( true ) -// .addRoot( "topology" ) -// .addTag( "gateway" ) -// .addTag( "provider" ) -// .addTag( "role" ).addText( "authentication" ) -// .addTag( "name" ).addText( "ShiroProvider" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm" ) -// .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm" ).gotoParent() -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.userDnTemplate" ) -// .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ).gotoParent() -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.url" ) -// .addTag( "value" ).addText( "ldap://localhost:" + ldapTransport.getPort() ).gotoParent() -// .addTag( "param" ) -// .addTag( "name" ).addText( "main.ldapRealm.contextFactory.authenticationMechanism" ) -// .addTag( "value" ).addText( "simple" ).gotoParent() -// .addTag( "param" ) -// .addTag( "name" ).addText( "urls./**" ) -// .addTag( "value" ).addText( "authcBasic" ).gotoParent().gotoParent() -// .addTag( "provider" ) -// .addTag( "role" ).addText( "identity-assertion" ) -// .addTag( "enabled" ).addText( "true" ) -// .addTag( "name" ).addText( "Default" ).gotoParent() -// .addTag( "provider" ) -// .gotoRoot() -// .addTag( "service" ) -// .addTag( "role" ).addText( "test-jersey-service-role" ) -// .gotoRoot(); -// // System.out.println( "GATEWAY=" + xml.toString() ); -// return xml; -// } -// -// @Test( timeout = MEDIUM_TIMEOUT ) -// public void testJerseyService() throws ClassNotFoundException { -// LOG_ENTER(); -// assertThat( ClassLoader.getSystemClassLoader().loadClass( "org.glassfish.jersey.servlet.ServletContainer" ), notNullValue() ); -// assertThat( ClassLoader.getSystemClassLoader().loadClass( "org.apache.hadoop.gateway.jersey.JerseyDispatchDeploymentContributor" ), notNullValue() ); -// assertThat( ClassLoader.getSystemClassLoader().loadClass( "org.apache.hadoop.gateway.jersey.JerseyServiceDeploymentContributorBase" ), notNullValue() ); -// assertThat( ClassLoader.getSystemClassLoader().loadClass( "org.apache.hadoop.gateway.TestJerseyService" ), notNullValue() ); -// -// String username = "guest"; -// String password = "guest-password"; -// String serviceUrl = clusterUrl + "/test-jersey-service/test-jersey-resource-path"; -// given() -// //.log().all() -// .auth().preemptive().basic( username, password ) -// .expect() -// //.log().all() -// .statusCode( HttpStatus.SC_OK ) -// .contentType( "text/plain" ) -// .body( is( "test-jersey-resource-response" ) ) -// .when().get( serviceUrl ); -// LOG_EXIT(); -// } -// -// private static int findFreePort() throws IOException { -// ServerSocket socket = new ServerSocket(0); -// int port = socket.getLocalPort(); -// socket.close(); -// return port; -// } -// -// public static InputStream getResourceStream( String resource ) throws IOException { -// return getResourceUrl( resource ).openStream(); -// } -// -// public static URL getResourceUrl( String resource ) { -// URL url = ClassLoader.getSystemResource( getResourceName( resource ) ); -// assertThat( "Failed to find test resource " + resource, url, Matchers.notNullValue() ); -// return url; -// } -// -// public static String getResourceName( String resource ) { -// return getResourceBaseName() + resource; -// } -// -// public static String getResourceBaseName() { -// return RESOURCE_BASE_CLASS.getName().replaceAll( "\\.", "/" ) + "/"; -// } + private static final long SHORT_TIMEOUT = 2000L; + private static final long MEDIUM_TIMEOUT = 5 * SHORT_TIMEOUT; + + private static Class RESOURCE_BASE_CLASS = GatewayLocalServiceFuncTest.class; + private static Logger LOG = LoggerFactory.getLogger( GatewayFuncTestDriver.class ); + + public static Enumeration<Appender> appenders; + public static GatewayConfig config; + public static GatewayServer gateway; + public static String gatewayUrl; + public static String clusterUrl; + public static SimpleLdapDirectoryServer ldap; + public static TcpTransport ldapTransport; + + @BeforeClass + public static void setupSuite() throws Exception { + LOG_ENTER(); + appenders = NoOpAppender.setUp(); + setupLdap(); + setupGateway(); + LOG_EXIT(); + } + + @AfterClass + public static void cleanupSuite() throws Exception { + LOG_ENTER(); + gateway.stop(); + ldap.stop( true ); + FileUtils.deleteQuietly( new File( config.getGatewayConfDir() ) ); + FileUtils.deleteQuietly( new File( config.getGatewayDataDir() ) ); + NoOpAppender.tearDown( appenders ); + LOG_EXIT(); + } + + public static void setupLdap() throws Exception { + URL usersUrl = getResourceUrl( "users.ldif" ); + int port = findFreePort(); + ldapTransport = new TcpTransport( port ); + ldap = new SimpleLdapDirectoryServer( "dc=hadoop,dc=apache,dc=org", new File( usersUrl.toURI() ), ldapTransport ); + ldap.start(); + LOG.info( "LDAP port = " + ldapTransport.getPort() ); + } + + public static void setupGateway() throws Exception { + + File targetDir = new File( System.getProperty( "user.dir" ), "target" ); + File gatewayDir = new File( targetDir, "gateway-home-" + UUID.randomUUID() ); + gatewayDir.mkdirs(); + + GatewayTestConfig testConfig = new GatewayTestConfig(); + config = testConfig; + testConfig.setGatewayHomeDir( gatewayDir.getAbsolutePath() ); + + File topoDir = new File( testConfig.getGatewayTopologyDir() ); + topoDir.mkdirs(); + + File deployDir = new File( testConfig.getGatewayDeploymentDir() ); + deployDir.mkdirs(); + + File descriptor = new File( topoDir, "cluster.xml" ); + FileOutputStream stream = new FileOutputStream( descriptor ); + createTopology().toStream( stream ); + stream.close(); + + DefaultGatewayServices srvcs = new DefaultGatewayServices(); + Map<String,String> options = new HashMap<String,String>(); + options.put( "persist-master", "false" ); + options.put( "master", "password" ); + try { + srvcs.init( testConfig, options ); + } catch ( ServiceLifecycleException e ) { + e.printStackTrace(); // I18N not required. + } + gateway = GatewayServer.startGateway( testConfig, srvcs ); + MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() ); + + LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() ); + + gatewayUrl = "http://localhost:" + gateway.getAddresses()[0].getPort() + "/" + config.getGatewayPath(); + clusterUrl = gatewayUrl + "/cluster"; + } + + private static XMLTag createTopology() { + XMLTag xml = XMLDoc.newDocument( true ) + .addRoot( "topology" ) + .addTag( "gateway" ) + .addTag( "provider" ) + .addTag( "role" ).addText( "authentication" ) + .addTag( "name" ).addText( "ShiroProvider" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm" ) + .addTag( "value" ).addText( "org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm" ).gotoParent() + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.userDnTemplate" ) + .addTag( "value" ).addText( "uid={0},ou=people,dc=hadoop,dc=apache,dc=org" ).gotoParent() + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.url" ) + .addTag( "value" ).addText( "ldap://localhost:" + ldapTransport.getPort() ).gotoParent() + .addTag( "param" ) + .addTag( "name" ).addText( "main.ldapRealm.contextFactory.authenticationMechanism" ) + .addTag( "value" ).addText( "simple" ).gotoParent() + .addTag( "param" ) + .addTag( "name" ).addText( "urls./**" ) + .addTag( "value" ).addText( "authcBasic" ).gotoParent().gotoParent() + .addTag( "provider" ) + .addTag( "role" ).addText( "identity-assertion" ) + .addTag( "enabled" ).addText( "true" ) + .addTag( "name" ).addText( "Default" ).gotoParent() + .addTag( "provider" ) + .gotoRoot() + .addTag( "service" ) + .addTag( "role" ).addText( "test-jersey-service-role" ) + .gotoRoot(); + // System.out.println( "GATEWAY=" + xml.toString() ); + return xml; + } + + @Test( timeout = MEDIUM_TIMEOUT ) + public void testJerseyService() throws ClassNotFoundException { + LOG_ENTER(); + assertThat( ClassLoader.getSystemClassLoader().loadClass( "org.glassfish.jersey.servlet.ServletContainer" ), notNullValue() ); + assertThat( ClassLoader.getSystemClassLoader().loadClass( "org.apache.hadoop.gateway.jersey.JerseyDispatchDeploymentContributor" ), notNullValue() ); + assertThat( ClassLoader.getSystemClassLoader().loadClass( "org.apache.hadoop.gateway.jersey.JerseyServiceDeploymentContributorBase" ), notNullValue() ); + assertThat( ClassLoader.getSystemClassLoader().loadClass( "org.apache.hadoop.gateway.TestJerseyService" ), notNullValue() ); + + String username = "guest"; + String password = "guest-password"; + String serviceUrl = clusterUrl + "/test-jersey-service/test-jersey-resource-path"; + given() + //.log().all() + .auth().preemptive().basic( username, password ) + .expect() + //.log().all() + .statusCode( HttpStatus.SC_OK ) + .contentType( "text/plain" ) + .body( is( "test-jersey-resource-response" ) ) + .when().get( serviceUrl ); + LOG_EXIT(); + } + + private static int findFreePort() throws IOException { + ServerSocket socket = new ServerSocket(0); + int port = socket.getLocalPort(); + socket.close(); + return port; + } + + public static InputStream getResourceStream( String resource ) throws IOException { + return getResourceUrl( resource ).openStream(); + } + + public static URL getResourceUrl( String resource ) { + URL url = ClassLoader.getSystemResource( getResourceName( resource ) ); + assertThat( "Failed to find test resource " + resource, url, Matchers.notNullValue() ); + return url; + } + + public static String getResourceName( String resource ) { + return getResourceBaseName() + resource; + } + + public static String getResourceBaseName() { + return RESOURCE_BASE_CLASS.getName().replaceAll( "\\.", "/" ) + "/"; + } }
