Skip to content

Instantly share code, notes, and snippets.

@risdenk
Created May 24, 2017 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save risdenk/afecc66d6fc0c9d665abd1ae5466f341 to your computer and use it in GitHub Desktop.
Save risdenk/afecc66d6fc0c9d665abd1ae5466f341 to your computer and use it in GitHub Desktop.
GatewayBasicFuncURLEncodingTest.java
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.gateway;
import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Cookie;
import com.jayway.restassured.response.Header;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.ResponseSpecification;
import com.mycila.xmltool.XMLDoc;
import com.mycila.xmltool.XMLTag;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.hadoop.gateway.util.KnoxCLI;
import org.apache.hadoop.test.TestUtils;
import org.apache.hadoop.test.category.MediumTests;
import org.apache.hadoop.test.category.VerifyTest;
import org.apache.hadoop.test.mock.MockRequestMatcher;
import org.apache.http.HttpStatus;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.core.MediaType;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import static com.jayway.restassured.RestAssured.given;
import static org.apache.hadoop.test.TestUtils.LOG_ENTER;
import static org.apache.hadoop.test.TestUtils.LOG_EXIT;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.either;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.text.IsEmptyString.isEmptyString;
import static org.xmlmatchers.XmlMatchers.isEquivalentTo;
import static org.xmlmatchers.transform.XmlConverters.the;
import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;
@Category( { VerifyTest.class, MediumTests.class } )
public class GatewayBasicFuncURLEncodingTest {
private static final Charset UTF8 = Charset.forName("UTF-8");
// Uncomment to cause the test to hang after the gateway instance is setup.
// This will allow the gateway instance to be hit directly via some external client.
// @Test
// public void hang() throws IOException {
// System.out.println( "Server on port " + driver.gateway.getAddresses()[0].getPort() );
// System.out.println();
// System.in.read();
// }
private static Logger log = LoggerFactory.getLogger( GatewayBasicFuncURLEncodingTest.class );
public static GatewayFuncTestDriver driver = new GatewayFuncTestDriver();
// Controls the host name to which the gateway dispatch requests. This may be the name of a sandbox VM
// or an EC2 instance. Currently only a single host is supported.
private static final String TEST_HOST = "vm.local";
// Specifies if the test requests should go through the gateway or directly to the services.
// This is frequently used to verify the behavior of the test both with and without the gateway.
private static final boolean USE_GATEWAY = true;
// Specifies if the test requests should be sent to mock services or the real services.
// This is frequently used to verify the behavior of the test both with and without mock services.
private static final boolean USE_MOCK_SERVICES = true;
// Specifies if the GATEWAY_HOME created for the test should be deleted when the test suite is complete.
// This is frequently used during debugging to keep the GATEWAY_HOME around for inspection.
private static final boolean CLEANUP_TEST = true;
// private static final boolean USE_GATEWAY = false;
// private static final boolean USE_MOCK_SERVICES = false;
// private static final boolean CLEANUP_TEST = false;
/**
* Creates a deployment of a gateway instance that all test methods will share. This method also creates a
* registry of sorts for all of the services that will be used by the test methods.
* The createTopology method is used to create the topology file that would normally be read from disk.
* The driver.setupGateway invocation is where the creation of GATEWAY_HOME occurs.
* @throws Exception Thrown if any failure occurs.
*/
@BeforeClass
public static void setupSuite() throws Exception {
//Log.setLog( new NoOpLogger() );
LOG_ENTER();
GatewayTestConfig config = new GatewayTestConfig();
config.setGatewayPath( "gateway" );
driver.setResourceBase(GatewayBasicFuncTest.class);
driver.setupLdap(0);
driver.setupService( "WEBHBASE", "http://" + TEST_HOST + ":60080", "/cluster/hbase", USE_MOCK_SERVICES );
driver.setupGateway( config, "cluster", createTopology(), USE_GATEWAY );
LOG_EXIT();
}
@AfterClass
public static void cleanupSuite() throws Exception {
LOG_ENTER();
if( CLEANUP_TEST ) {
driver.cleanup();
}
LOG_EXIT();
}
@After
public void cleanupTest() {
driver.reset();
}
/**
* Creates a topology that is deployed to the gateway instance for the test suite.
* Note that this topology is shared by all of the test methods in this suite.
* @return A populated XML structure for a topology file.
*/
private static XMLTag createTopology() {
XMLTag xml = XMLDoc.newDocument( true )
.addRoot( "topology" )
.addTag( "gateway" )
.addTag( "provider" )
.addTag( "role" ).addText( "webappsec" )
.addTag("name").addText("WebAppSec")
.addTag("enabled").addText("true")
.addTag( "param" )
.addTag("name").addText("csrf.enabled")
.addTag("value").addText("true").gotoParent().gotoParent()
.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( driver.getLdapUrl() ).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")
.addTag( "role" ).addText( "authorization" )
.addTag( "enabled" ).addText( "true" )
.addTag("name").addText("AclsAuthz").gotoParent()
.addTag("param")
.addTag("name").addText( "webhdfs-acl" )
.addTag("value").addText( "hdfs;*;*" ).gotoParent()
.gotoRoot()
.addTag( "service" )
.addTag( "role" ).addText( "WEBHBASE" )
.addTag( "url" ).addText( driver.getRealUrl( "WEBHBASE" ) ).gotoParent()
.addTag("service")
.addTag("role").addText("SERVICE-TEST")
.gotoRoot();
return xml;
}
@Test( timeout = TestUtils.MEDIUM_TIMEOUT )
public void testHBaseKevin() throws IOException {
LOG_ENTER();
String username = "hbase";
String password = "hbase-password";
String resourceName = "hbase/table-metadata";
String path = "/ns:table/%2frk1%2frk2";
driver.getMock("WEBHBASE")
.expect()
.method("GET")
.pathInfo(path)
.header("Accept", ContentType.XML.toString())
.respond()
.status(HttpStatus.SC_OK)
.content(driver.getResourceBytes(resourceName + ".xml"))
.contentType(ContentType.XML.toString());
Response response = given()
.auth().preemptive().basic(username, password)
.header("X-XSRF-Header", "jksdhfkhdsf")
.header("Accept", ContentType.XML.toString())
.expect()
.statusCode(HttpStatus.SC_OK)
.contentType(ContentType.XML)
.when().get(driver.getUrl("WEBHBASE") + path);
MatcherAssert
.assertThat(
the(response.getBody().asString()),
isEquivalentTo(the(driver.getResourceString(resourceName + ".xml", UTF8))));
driver.assertComplete();
LOG_EXIT();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment