Repository: knox Updated Branches: refs/heads/master 3ddb28ea3 -> c2635885d
KNOX-66 Ambari REST API support Project: http://git-wip-us.apache.org/repos/asf/knox/repo Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/c2635885 Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/c2635885 Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/c2635885 Branch: refs/heads/master Commit: c2635885dddf6cfb4db85cfe16dbe5dfd476cbb5 Parents: 3ddb28e Author: Sumit Gupta <[email protected]> Authored: Tue Mar 8 10:34:39 2016 -0500 Committer: Sumit Gupta <[email protected]> Committed: Tue Mar 8 10:35:07 2016 -0500 ---------------------------------------------------------------------- .../api/UrlRewriteFilterContentDescriptor.java | 4 + .../UrlRewriteFilterContentDescriptorImpl.java | 24 +- .../filter/rewrite/impl/UrlRewriteResponse.java | 7 + .../impl/xml/XmlRewriteRulesDigester.java | 7 +- .../resources/services/ambari/2.2.0/rewrite.xml | 30 +++ .../resources/services/ambari/2.2.0/service.xml | 24 ++ .../dispatch/AbstractGatewayDispatch.java | 25 +- .../gateway/dispatch/DefaultDispatch.java | 1 + .../dispatch/PassAllHeadersDispatch.java | 34 +++ .../gateway/AmbariServiceDefinitionTest.java | 239 ++++++++++++++++++ .../clusters-response-expected.json | 13 + .../clusters-response.json | 13 + .../history-server-response-expected.json | 250 +++++++++++++++++++ .../history-server-response.json | 250 +++++++++++++++++++ .../test-svcs/readme.txt | 18 ++ .../test-topology.xml | 34 +++ 16 files changed, 964 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteFilterContentDescriptor.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteFilterContentDescriptor.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteFilterContentDescriptor.java index a101b77..73ea970 100644 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteFilterContentDescriptor.java +++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/api/UrlRewriteFilterContentDescriptor.java @@ -21,8 +21,12 @@ public interface UrlRewriteFilterContentDescriptor extends UrlRewriteFilterGroup String type(); + String asType(); + UrlRewriteFilterContentDescriptor type( String type ); + UrlRewriteFilterContentDescriptor asType( String type ); + UrlRewriteFilterBufferDescriptor addBuffer( String path ); } http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteFilterContentDescriptorImpl.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteFilterContentDescriptorImpl.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteFilterContentDescriptorImpl.java index 83b41b2..3d42537 100644 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteFilterContentDescriptorImpl.java +++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteFilterContentDescriptorImpl.java @@ -17,7 +17,6 @@ */ package org.apache.hadoop.gateway.filter.rewrite.impl; -import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterApplyDescriptor; import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterBufferDescriptor; import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; @@ -27,6 +26,8 @@ public class UrlRewriteFilterContentDescriptorImpl private String type; + private String asType; + public UrlRewriteFilterContentDescriptorImpl() { } @@ -36,12 +37,23 @@ public class UrlRewriteFilterContentDescriptorImpl } @Override + public String asType() { + return asType; + } + + @Override public UrlRewriteFilterContentDescriptor type( String type ) { this.type = type; return this; } - public void setType( String type ) { + @Override + public UrlRewriteFilterContentDescriptor asType( String type ) { + asType = type; + return this; + } + + public void setType(String type ) { type( type ); } @@ -49,6 +61,14 @@ public class UrlRewriteFilterContentDescriptorImpl return type; } + public String getAsType() { + return asType; + } + + public void setAsType(String asType) { + this.asType = asType; + } + @Override public UrlRewriteFilterBufferDescriptor addBuffer( String path ) { UrlRewriteFilterBufferDescriptor descriptor = new UrlRewriteFilterBufferDescriptorImpl(); http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java index 792605e..3a85516 100644 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java +++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/UrlRewriteResponse.java @@ -26,6 +26,7 @@ import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteStreamFilterFactor import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriter; import org.apache.hadoop.gateway.filter.rewrite.i18n.UrlRewriteMessages; import org.apache.hadoop.gateway.i18n.messages.MessagesFactory; +import org.apache.hadoop.gateway.util.MimeTypes; import org.apache.hadoop.gateway.util.Urls; import org.apache.hadoop.gateway.util.urltemplate.Params; import org.apache.hadoop.gateway.util.urltemplate.Parser; @@ -171,6 +172,12 @@ public class UrlRewriteResponse extends GatewayResponseWrapper implements Params MimeType mimeType = getMimeType(); UrlRewriteFilterContentDescriptor filterContentConfig = getRewriteFilterConfig( rewriter.getConfig(), bodyFilterName, mimeType ); + if (filterContentConfig != null) { + String asType = filterContentConfig.asType(); + if ( asType != null && asType.trim().length() > 0 ) { + mimeType = MimeTypes.create(asType, getCharacterEncoding()); + } + } InputStream filteredInput = UrlRewriteStreamFilterFactory.create( mimeType, null, inStream, rewriter, this, UrlRewriter.Direction.OUT, filterContentConfig ); outStream = (isGzip) ? new GZIPOutputStream(output) : output; http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesDigester.java ---------------------------------------------------------------------- diff --git a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesDigester.java b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesDigester.java index ddd5d00..d4aa7ea 100644 --- a/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesDigester.java +++ b/gateway-provider-rewrite/src/main/java/org/apache/hadoop/gateway/filter/rewrite/impl/xml/XmlRewriteRulesDigester.java @@ -21,6 +21,7 @@ import org.apache.commons.digester3.Digester; import org.apache.commons.digester3.Rule; import org.apache.commons.digester3.SetPropertiesRule; import org.apache.commons.digester3.binder.AbstractRulesModule; +import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterContentDescriptor; import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterDescriptor; import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterGroupDescriptor; import org.apache.hadoop.gateway.filter.rewrite.api.UrlRewriteFilterPathDescriptor; @@ -135,7 +136,11 @@ public class XmlRewriteRulesDigester extends AbstractRulesModule implements XmlR @Override public Object create( String namespace, String name, Attributes attributes ) { UrlRewriteFilterDescriptor parent = getDigester().peek(); - return parent.addContent( attributes.getValue( "type" ) ); + UrlRewriteFilterContentDescriptor descriptor = parent.addContent( attributes.getValue( "type" ) ); + if (attributes.getValue( "asType" ) != null) { + descriptor = descriptor.asType(attributes.getValue( "asType" )); + } + return descriptor; } } http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-service-definitions/src/main/resources/services/ambari/2.2.0/rewrite.xml ---------------------------------------------------------------------- diff --git a/gateway-service-definitions/src/main/resources/services/ambari/2.2.0/rewrite.xml b/gateway-service-definitions/src/main/resources/services/ambari/2.2.0/rewrite.xml new file mode 100644 index 0000000..68f2791 --- /dev/null +++ b/gateway-service-definitions/src/main/resources/services/ambari/2.2.0/rewrite.xml @@ -0,0 +1,30 @@ +<!-- + 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. +--> +<rules> + <rule dir="IN" name="AMBARI/ambari/inbound" pattern="*://*:*/**/ambari/api/v1/{path=**}?{**}"> + <rewrite template="{$serviceUrl[AMBARI]}/api/v1/{path=**}?{**}"/> + </rule> + <rule dir="OUT" name="AMBARI/ambari/href/outbound"> + <match pattern="*://*:*/api/{**}"/> + <rewrite template="{$frontend[url]}/ambari/api/{**}"/> + </rule> + <filter name="AMBARI/ambari/api/outbound"> + <content type="text/plain" asType="application/json"> + <apply path="$.**.href" rule="AMBARI/ambari/href/outbound"/> + </content> + </filter> +</rules> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-service-definitions/src/main/resources/services/ambari/2.2.0/service.xml ---------------------------------------------------------------------- diff --git a/gateway-service-definitions/src/main/resources/services/ambari/2.2.0/service.xml b/gateway-service-definitions/src/main/resources/services/ambari/2.2.0/service.xml new file mode 100644 index 0000000..0bd2150 --- /dev/null +++ b/gateway-service-definitions/src/main/resources/services/ambari/2.2.0/service.xml @@ -0,0 +1,24 @@ +<!-- + 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. +--> +<service role="AMBARI" name="ambari" version="2.7.0"> + <routes> + <route path="/ambari/api/v1/**"> + <rewrite apply="AMBARI/ambari/api/outbound" to="response.body"/> + </route> + </routes> + <dispatch classname="org.apache.hadoop.gateway.dispatch.PassAllHeadersDispatch"/> +</service> http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/AbstractGatewayDispatch.java ---------------------------------------------------------------------- diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/AbstractGatewayDispatch.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/AbstractGatewayDispatch.java index f578bbb..2cc1dc0 100644 --- a/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/AbstractGatewayDispatch.java +++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/AbstractGatewayDispatch.java @@ -29,18 +29,27 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; -import java.util.Arrays; import java.util.Enumeration; -import java.util.List; +import java.util.HashSet; +import java.util.Set; public abstract class AbstractGatewayDispatch implements Dispatch { private static int STREAM_COPY_BUFFER_SIZE = 4096; - private static final List<String> EXCLUDE_HEADERS = Arrays.asList( "Host", "Authorization", "Content-Length", "Transfer-Encoding" ); + private static Set<String> REQUEST_EXCLUDE_HEADERS; protected HttpClient client; - protected void writeResponse( HttpServletRequest request, HttpServletResponse response, InputStream stream ) + @Override + public void init() { + REQUEST_EXCLUDE_HEADERS = new HashSet<>(); + REQUEST_EXCLUDE_HEADERS.add("Host"); + REQUEST_EXCLUDE_HEADERS.add("Authorization"); + REQUEST_EXCLUDE_HEADERS.add("Content-Length"); + REQUEST_EXCLUDE_HEADERS.add("Transfer-Encoding"); + } + + protected void writeResponse(HttpServletRequest request, HttpServletResponse response, InputStream stream ) throws IOException { // ResponseStreamer streamer = // (ResponseStreamer)request.getAttribute( RESPONSE_STREAMER_ATTRIBUTE_NAME ); @@ -93,17 +102,21 @@ public abstract class AbstractGatewayDispatch implements Dispatch { response.sendError( HttpServletResponse.SC_METHOD_NOT_ALLOWED ); } - public static void copyRequestHeaderFields(HttpUriRequest outboundRequest, + public void copyRequestHeaderFields(HttpUriRequest outboundRequest, HttpServletRequest inboundRequest) { Enumeration<String> headerNames = inboundRequest.getHeaderNames(); while( headerNames.hasMoreElements() ) { String name = headerNames.nextElement(); if ( !outboundRequest.containsHeader( name ) - && !EXCLUDE_HEADERS.contains( name ) ) { + && !getOutboundRequestExcludeHeaders().contains( name ) ) { String value = inboundRequest.getHeader( name ); outboundRequest.addHeader( name, value ); } } } + public Set<String> getOutboundRequestExcludeHeaders() { + return REQUEST_EXCLUDE_HEADERS; + } + } http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/DefaultDispatch.java ---------------------------------------------------------------------- diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/DefaultDispatch.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/DefaultDispatch.java index 9f3cfd3..992a1a6 100644 --- a/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/DefaultDispatch.java +++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/DefaultDispatch.java @@ -73,6 +73,7 @@ public class DefaultDispatch extends AbstractGatewayDispatch { @Override public void init() { + super.init(); outboundResponseExcludeHeaders = new HashSet<>(); outboundResponseExcludeHeaders.add(SET_COOKIE); outboundResponseExcludeHeaders.add(WWW_AUTHENTICATE); http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/PassAllHeadersDispatch.java ---------------------------------------------------------------------- diff --git a/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/PassAllHeadersDispatch.java b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/PassAllHeadersDispatch.java new file mode 100644 index 0000000..7b8260d --- /dev/null +++ b/gateway-spi/src/main/java/org/apache/hadoop/gateway/dispatch/PassAllHeadersDispatch.java @@ -0,0 +1,34 @@ +/** + * 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.dispatch; + +import java.util.Collections; +import java.util.Set; + +public class PassAllHeadersDispatch extends DefaultDispatch { + + @Override + public Set<String> getOutboundResponseExcludeHeaders() { + return Collections.EMPTY_SET; + } + + @Override + public Set<String> getOutboundRequestExcludeHeaders() { + return Collections.EMPTY_SET; + } +} http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-test/src/test/java/org/apache/hadoop/gateway/AmbariServiceDefinitionTest.java ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/java/org/apache/hadoop/gateway/AmbariServiceDefinitionTest.java b/gateway-test/src/test/java/org/apache/hadoop/gateway/AmbariServiceDefinitionTest.java new file mode 100644 index 0000000..11563d1 --- /dev/null +++ b/gateway-test/src/test/java/org/apache/hadoop/gateway/AmbariServiceDefinitionTest.java @@ -0,0 +1,239 @@ +/** + * 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 + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> + * 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 org.apache.commons.io.FileUtils; +import org.apache.hadoop.gateway.services.DefaultGatewayServices; +import org.apache.hadoop.gateway.services.GatewayServices; +import org.apache.hadoop.gateway.services.ServiceLifecycleException; +import org.apache.hadoop.gateway.services.topology.TopologyService; +import org.apache.hadoop.test.TestUtils; +import org.apache.hadoop.test.mock.MockServer; +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.MatcherAssert; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.StringWriter; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.UUID; + +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.*; +import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs; + +public class AmbariServiceDefinitionTest { + + private static Logger LOG = LoggerFactory.getLogger( AmbariServiceDefinitionTest.class ); + private static Class DAT = AmbariServiceDefinitionTest.class; + + private static GatewayTestConfig config; + private static DefaultGatewayServices services; + private static GatewayServer gateway; + private static int gatewayPort; + private static String gatewayUrl; + private static String clusterUrl; + private static Properties params; + private static TopologyService topos; + private static MockServer mockAmbari; + + private static VelocityEngine velocity; + private static VelocityContext context; + + @BeforeClass + public static void setupSuite() throws Exception { + LOG_ENTER(); + setupGateway(); + String topoStr = TestUtils.merge( DAT, "test-topology.xml", params ); + File topoFile = new File( config.getGatewayTopologyDir(), "test-topology.xml" ); + FileUtils.writeStringToFile( topoFile, topoStr ); + topos.reloadTopologies(); + LOG_EXIT(); + } + + @AfterClass + public static void cleanupSuite() throws Exception { + LOG_ENTER(); + gateway.stop(); + FileUtils.deleteQuietly( new File( config.getGatewayHomeDir() ) ); + LOG_EXIT(); + } + + @After + public void cleanupTest() throws Exception { + FileUtils.cleanDirectory( new File( config.getGatewayTopologyDir() ) ); + FileUtils.cleanDirectory( new File( config.getGatewayDeploymentDir() ) ); + } + + 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(); + + config = new GatewayTestConfig(); + config.setGatewayHomeDir( gatewayDir.getAbsolutePath() ); + + URL svcsFileUrl = TestUtils.getResourceUrl( DAT, "test-svcs/readme.txt" ); + File svcsFile = new File( svcsFileUrl.getFile() ); + File svcsDir = svcsFile.getParentFile(); + config.setGatewayServicesDir( svcsDir.getAbsolutePath() ); + + String pathToStacksSource = "gateway-service-definitions/src/main/resources/services"; + File stacksSourceDir = new File( targetDir.getParent(), pathToStacksSource); + if (!stacksSourceDir.exists()) { + stacksSourceDir = new File( targetDir.getParentFile().getParent(), pathToStacksSource); + } + if (stacksSourceDir.exists()) { + FileUtils.copyDirectoryToDirectory(stacksSourceDir, svcsDir); + } + + File topoDir = new File( config.getGatewayTopologyDir() ); + topoDir.mkdirs(); + + File deployDir = new File( config.getGatewayDeploymentDir() ); + deployDir.mkdirs(); + + setupMockServers(); + startGatewayServer(); + } + + public static void setupMockServers() throws Exception { + mockAmbari = new MockServer( "AMBARI", true ); + } + + public static void startGatewayServer() throws Exception { + services = new DefaultGatewayServices(); + Map<String,String> options = new HashMap<String,String>(); + options.put( "persist-master", "false" ); + options.put( "master", "password" ); + try { + services.init( config, options ); + } catch ( ServiceLifecycleException e ) { + e.printStackTrace(); // I18N not required. + } + topos = services.getService(GatewayServices.TOPOLOGY_SERVICE); + + gateway = GatewayServer.startGateway( config, services ); + MatcherAssert.assertThat( "Failed to start gateway.", gateway, notNullValue() ); + + gatewayPort = gateway.getAddresses()[0].getPort(); + gatewayUrl = "http://localhost:" + gatewayPort + "/" + config.getGatewayPath(); + clusterUrl = gatewayUrl + "/test-topology"; + + LOG.info( "Gateway port = " + gateway.getAddresses()[ 0 ].getPort() ); + + params = new Properties(); + params.put( "AMBARI_URL", "http://localhost:" + mockAmbari.getPort() ); + + velocity = new VelocityEngine(); + velocity.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem" ); + velocity.setProperty( RuntimeConstants.RESOURCE_LOADER, "classpath" ); + velocity.setProperty( "classpath.resource.loader.class", ClasspathResourceLoader.class.getName() ); + velocity.init(); + + context = new VelocityContext(); + context.put( "cluster_url", clusterUrl ); + + } + + @Test( timeout = TestUtils.MEDIUM_TIMEOUT ) + public void clusters() throws Exception { + LOG_ENTER(); + + String username = "guest"; + String password = "guest-password"; + String serviceUrl = clusterUrl + "/ambari/api/v1/clusters"; + + mockAmbari.expect() + .method( "GET" ) + .pathInfo( "/api/v1/clusters" ) + .respond() + .status( HttpStatus.SC_OK ) + .content( TestUtils.getResourceStream( DAT, "clusters-response.json" ) ) + .contentType( "text/plain" ); + + String body = given() +// .log().all() + .auth().preemptive().basic( username, password ) + .expect() +// .log().all() + .statusCode( HttpStatus.SC_OK ) + .contentType( "text/plain" ) + .when().get( serviceUrl ).asString(); + + + String name = TestUtils.getResourceName( this.getClass(), "clusters-response-expected.json" ); + Template template = velocity.getTemplate( name ); + StringWriter sw = new StringWriter(); + template.merge( context, sw ); + String expected = sw.toString(); + + MatcherAssert.assertThat(body, sameJSONAs(expected)); + LOG_EXIT(); + } + + @Test( timeout = TestUtils.MEDIUM_TIMEOUT ) + public void historyServer() throws Exception { + LOG_ENTER(); + + String username = "guest"; + String password = "guest-password"; + String serviceUrl = clusterUrl + "/ambari/api/v1/clusters/test/hosts/c6401.ambari.apache.org/host_components/HISTORYSERVER"; + + mockAmbari.expect() + .method( "GET" ) + .pathInfo( "/api/v1/clusters/test/hosts/c6401.ambari.apache.org/host_components/HISTORYSERVER" ) + .respond() + .status( HttpStatus.SC_OK ) + .content( TestUtils.getResourceStream( DAT, "history-server-response.json" ) ) + .contentType( "text/plain" ); + + String body = given() + .auth().preemptive().basic( username, password ) + .expect() + .statusCode( HttpStatus.SC_OK ) + .contentType( "text/plain" ) + .when().get( serviceUrl ).asString(); + + + String name = TestUtils.getResourceName( this.getClass(), "history-server-response-expected.json" ); + Template template = velocity.getTemplate( name ); + StringWriter sw = new StringWriter(); + template.merge( context, sw ); + String expected = sw.toString(); + + MatcherAssert.assertThat(body, sameJSONAs(expected)); + LOG_EXIT(); + } + +} http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/clusters-response-expected.json ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/clusters-response-expected.json b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/clusters-response-expected.json new file mode 100644 index 0000000..7618e36 --- /dev/null +++ b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/clusters-response-expected.json @@ -0,0 +1,13 @@ +{ + "href": "$cluster_url/ambari/api/v1/clusters", + "items": [ + { + "href": "$cluster_url/ambari/api/v1/clusters/test", + "Clusters": { + "cluster_name": "test", + "version": "HDP-2.3" + } + } + ] +} + http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/clusters-response.json ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/clusters-response.json b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/clusters-response.json new file mode 100644 index 0000000..692eca3 --- /dev/null +++ b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/clusters-response.json @@ -0,0 +1,13 @@ + +{ + "href" : "http://c6401.ambari.apache.org:8080/api/v1/clusters", + "items" : [ + { + "href" : "http://c6401.ambari.apache.org:8080/api/v1/clusters/test", + "Clusters" : { + "cluster_name" : "test", + "version" : "HDP-2.3" + } + } + ] +} http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/history-server-response-expected.json ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/history-server-response-expected.json b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/history-server-response-expected.json new file mode 100644 index 0000000..7a86ebc --- /dev/null +++ b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/history-server-response-expected.json @@ -0,0 +1,250 @@ +{ + "href" : "$cluster_url/ambari/api/v1/clusters/test/hosts/c6401.ambari.apache.org/host_components/HISTORYSERVER", + "HostRoles" : { + "cluster_name" : "test", + "component_name" : "HISTORYSERVER", + "desired_stack_id" : "HDP-2.3", + "desired_state" : "STARTED", + "hdp_version" : "HDP-2.3.4.0-3485", + "host_name" : "c6401.ambari.apache.org", + "maintenance_state" : "OFF", + "service_name" : "MAPREDUCE2", + "stack_id" : "HDP-2.3", + "stale_configs" : false, + "state" : "UNKNOWN", + "upgrade_state" : "NONE", + "actual_configs" : { + "admin-properties" : { + "default" : "version1454604858144" + }, + "capacity-scheduler" : { + "default" : "version1455118418013" + }, + "cluster-env" : { + "default" : "version1455118418522" + }, + "core-site" : { + "default" : "version1455118418648" + }, + "gateway-log4j" : { + "default" : "version1" + }, + "gateway-site" : { + "default" : "version1455118418151" + }, + "hadoop-env" : { + "default" : "version1453930080957" + }, + "hadoop-policy" : { + "default" : "version1" + }, + "hbase-env" : { + "default" : "version1454359592877" + }, + "hbase-log4j" : { + "default" : "version1454359591233" + }, + "hbase-policy" : { + "default" : "version1454359591233" + }, + "hbase-site" : { + "default" : "version1455118418258" + }, + "hcat-env" : { + "default" : "version1" + }, + "hdfs-log4j" : { + "default" : "version1" + }, + "hdfs-site" : { + "default" : "version1455118417916" + }, + "hive-env" : { + "default" : "version1" + }, + "hive-exec-log4j" : { + "default" : "version1" + }, + "hive-log4j" : { + "default" : "version1" + }, + "hive-site" : { + "default" : "version1455118418323" + }, + "hiveserver2-site" : { + "default" : "version1453995823280" + }, + "kerberos-env" : { + "default" : "version1453929906086" + }, + "knox-env" : { + "default" : "version1453930080890" + }, + "krb5-conf" : { + "default" : "version1453929906086" + }, + "ldap-log4j" : { + "default" : "version1" + }, + "mapred-env" : { + "default" : "version1" + }, + "mapred-site" : { + "default" : "version1453930080915" + }, + "oozie-env" : { + "default" : "version1454359591233" + }, + "oozie-log4j" : { + "default" : "version1454359591233" + }, + "oozie-site" : { + "default" : "version1455118418401" + }, + "pig-env" : { + "default" : "version1" + }, + "pig-log4j" : { + "default" : "version1" + }, + "pig-properties" : { + "default" : "version1" + }, + "ranger-admin-site" : { + "default" : "version1454619167059" + }, + "ranger-env" : { + "default" : "version1454606224810" + }, + "ranger-hbase-audit" : { + "default" : "version1454604858137" + }, + "ranger-hbase-plugin-properties" : { + "default" : "version1454604858136" + }, + "ranger-hbase-policymgr-ssl" : { + "default" : "version1454604858137" + }, + "ranger-hbase-security" : { + "default" : "version1454604858136" + }, + "ranger-hdfs-audit" : { + "default" : "version1454604858129" + }, + "ranger-hdfs-plugin-properties" : { + "default" : "version1454606224830" + }, + "ranger-hdfs-policymgr-ssl" : { + "default" : "version1454604858132" + }, + "ranger-hdfs-security" : { + "default" : "version1454604858129" + }, + "ranger-hive-audit" : { + "default" : "version1454604858135" + }, + "ranger-hive-plugin-properties" : { + "default" : "version1454604858135" + }, + "ranger-hive-policymgr-ssl" : { + "default" : "version1454604858136" + }, + "ranger-hive-security" : { + "default" : "version1454604858135" + }, + "ranger-knox-audit" : { + "default" : "version1454604858138" + }, + "ranger-knox-plugin-properties" : { + "default" : "version1454606224851" + }, + "ranger-knox-policymgr-ssl" : { + "default" : "version1454604858138" + }, + "ranger-knox-security" : { + "default" : "version1454604858138" + }, + "ranger-site" : { + "default" : "version1454604858144" + }, + "ranger-ugsync-site" : { + "default" : "version1454604858144" + }, + "ranger-yarn-audit" : { + "default" : "version1454604858134" + }, + "ranger-yarn-plugin-properties" : { + "default" : "version1454604858133" + }, + "ranger-yarn-policymgr-ssl" : { + "default" : "version1454604858134" + }, + "ranger-yarn-security" : { + "default" : "version1454604858133" + }, + "ssl-client" : { + "default" : "version1" + }, + "ssl-server" : { + "default" : "version1" + }, + "tez-env" : { + "default" : "version1" + }, + "tez-site" : { + "default" : "version1455118418459" + }, + "topology" : { + "default" : "version1454606224851" + }, + "users-ldif" : { + "default" : "version1" + }, + "usersync-properties" : { + "default" : "version1454604858144" + }, + "webhcat-env" : { + "default" : "version1" + }, + "webhcat-log4j" : { + "default" : "version1" + }, + "webhcat-site" : { + "default" : "version1455118418562" + }, + "yarn-env" : { + "default" : "version1" + }, + "yarn-log4j" : { + "default" : "version1" + }, + "yarn-site" : { + "default" : "version1455118418065" + }, + "zoo.cfg" : { + "default" : "version1" + }, + "zookeeper-env" : { + "default" : "version1453930081017" + }, + "zookeeper-log4j" : { + "default" : "version1" + } + } + }, + "host" : { + "href" : "$cluster_url/ambari/api/v1/clusters/test/hosts/c6401.ambari.apache.org" + }, + "component" : [ + { + "href" : "$cluster_url/ambari/api/v1/clusters/test/services/MAPREDUCE2/components/HISTORYSERVER", + "ServiceComponentInfo" : { + "cluster_name" : "test", + "component_name" : "HISTORYSERVER", + "service_name" : "MAPREDUCE2" + } + } + ], + "processes" : [ ] +} + http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/history-server-response.json ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/history-server-response.json b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/history-server-response.json new file mode 100644 index 0000000..d6064bd --- /dev/null +++ b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/history-server-response.json @@ -0,0 +1,250 @@ +{ + "href" : "http://c6401.ambari.apache.org:8080/api/v1/clusters/test/hosts/c6401.ambari.apache.org/host_components/HISTORYSERVER", + "HostRoles" : { + "cluster_name" : "test", + "component_name" : "HISTORYSERVER", + "desired_stack_id" : "HDP-2.3", + "desired_state" : "STARTED", + "hdp_version" : "HDP-2.3.4.0-3485", + "host_name" : "c6401.ambari.apache.org", + "maintenance_state" : "OFF", + "service_name" : "MAPREDUCE2", + "stack_id" : "HDP-2.3", + "stale_configs" : false, + "state" : "UNKNOWN", + "upgrade_state" : "NONE", + "actual_configs" : { + "admin-properties" : { + "default" : "version1454604858144" + }, + "capacity-scheduler" : { + "default" : "version1455118418013" + }, + "cluster-env" : { + "default" : "version1455118418522" + }, + "core-site" : { + "default" : "version1455118418648" + }, + "gateway-log4j" : { + "default" : "version1" + }, + "gateway-site" : { + "default" : "version1455118418151" + }, + "hadoop-env" : { + "default" : "version1453930080957" + }, + "hadoop-policy" : { + "default" : "version1" + }, + "hbase-env" : { + "default" : "version1454359592877" + }, + "hbase-log4j" : { + "default" : "version1454359591233" + }, + "hbase-policy" : { + "default" : "version1454359591233" + }, + "hbase-site" : { + "default" : "version1455118418258" + }, + "hcat-env" : { + "default" : "version1" + }, + "hdfs-log4j" : { + "default" : "version1" + }, + "hdfs-site" : { + "default" : "version1455118417916" + }, + "hive-env" : { + "default" : "version1" + }, + "hive-exec-log4j" : { + "default" : "version1" + }, + "hive-log4j" : { + "default" : "version1" + }, + "hive-site" : { + "default" : "version1455118418323" + }, + "hiveserver2-site" : { + "default" : "version1453995823280" + }, + "kerberos-env" : { + "default" : "version1453929906086" + }, + "knox-env" : { + "default" : "version1453930080890" + }, + "krb5-conf" : { + "default" : "version1453929906086" + }, + "ldap-log4j" : { + "default" : "version1" + }, + "mapred-env" : { + "default" : "version1" + }, + "mapred-site" : { + "default" : "version1453930080915" + }, + "oozie-env" : { + "default" : "version1454359591233" + }, + "oozie-log4j" : { + "default" : "version1454359591233" + }, + "oozie-site" : { + "default" : "version1455118418401" + }, + "pig-env" : { + "default" : "version1" + }, + "pig-log4j" : { + "default" : "version1" + }, + "pig-properties" : { + "default" : "version1" + }, + "ranger-admin-site" : { + "default" : "version1454619167059" + }, + "ranger-env" : { + "default" : "version1454606224810" + }, + "ranger-hbase-audit" : { + "default" : "version1454604858137" + }, + "ranger-hbase-plugin-properties" : { + "default" : "version1454604858136" + }, + "ranger-hbase-policymgr-ssl" : { + "default" : "version1454604858137" + }, + "ranger-hbase-security" : { + "default" : "version1454604858136" + }, + "ranger-hdfs-audit" : { + "default" : "version1454604858129" + }, + "ranger-hdfs-plugin-properties" : { + "default" : "version1454606224830" + }, + "ranger-hdfs-policymgr-ssl" : { + "default" : "version1454604858132" + }, + "ranger-hdfs-security" : { + "default" : "version1454604858129" + }, + "ranger-hive-audit" : { + "default" : "version1454604858135" + }, + "ranger-hive-plugin-properties" : { + "default" : "version1454604858135" + }, + "ranger-hive-policymgr-ssl" : { + "default" : "version1454604858136" + }, + "ranger-hive-security" : { + "default" : "version1454604858135" + }, + "ranger-knox-audit" : { + "default" : "version1454604858138" + }, + "ranger-knox-plugin-properties" : { + "default" : "version1454606224851" + }, + "ranger-knox-policymgr-ssl" : { + "default" : "version1454604858138" + }, + "ranger-knox-security" : { + "default" : "version1454604858138" + }, + "ranger-site" : { + "default" : "version1454604858144" + }, + "ranger-ugsync-site" : { + "default" : "version1454604858144" + }, + "ranger-yarn-audit" : { + "default" : "version1454604858134" + }, + "ranger-yarn-plugin-properties" : { + "default" : "version1454604858133" + }, + "ranger-yarn-policymgr-ssl" : { + "default" : "version1454604858134" + }, + "ranger-yarn-security" : { + "default" : "version1454604858133" + }, + "ssl-client" : { + "default" : "version1" + }, + "ssl-server" : { + "default" : "version1" + }, + "tez-env" : { + "default" : "version1" + }, + "tez-site" : { + "default" : "version1455118418459" + }, + "topology" : { + "default" : "version1454606224851" + }, + "users-ldif" : { + "default" : "version1" + }, + "usersync-properties" : { + "default" : "version1454604858144" + }, + "webhcat-env" : { + "default" : "version1" + }, + "webhcat-log4j" : { + "default" : "version1" + }, + "webhcat-site" : { + "default" : "version1455118418562" + }, + "yarn-env" : { + "default" : "version1" + }, + "yarn-log4j" : { + "default" : "version1" + }, + "yarn-site" : { + "default" : "version1455118418065" + }, + "zoo.cfg" : { + "default" : "version1" + }, + "zookeeper-env" : { + "default" : "version1453930081017" + }, + "zookeeper-log4j" : { + "default" : "version1" + } + } + }, + "host" : { + "href" : "http://c6401.ambari.apache.org:8080/api/v1/clusters/test/hosts/c6401.ambari.apache.org" + }, + "component" : [ + { + "href" : "http://c6401.ambari.apache.org:8080/api/v1/clusters/test/services/MAPREDUCE2/components/HISTORYSERVER", + "ServiceComponentInfo" : { + "cluster_name" : "test", + "component_name" : "HISTORYSERVER", + "service_name" : "MAPREDUCE2" + } + } + ], + "processes" : [ ] +} + http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/test-svcs/readme.txt ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/test-svcs/readme.txt b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/test-svcs/readme.txt new file mode 100644 index 0000000..cd2eef8 --- /dev/null +++ b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/test-svcs/readme.txt @@ -0,0 +1,18 @@ +########################################################################## +# 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. +########################################################################## +This file is here to help the tests find the parent directory. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/knox/blob/c2635885/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/test-topology.xml ---------------------------------------------------------------------- diff --git a/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/test-topology.xml b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/test-topology.xml new file mode 100644 index 0000000..519d426 --- /dev/null +++ b/gateway-test/src/test/resources/org/apache/hadoop/gateway/AmbariServiceDefinitionTest/test-topology.xml @@ -0,0 +1,34 @@ +<!-- + 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. +--> +<topology> + <gateway> + <provider> + <role>authentication</role> + <name>Anonymous</name> + <enabled>true</enabled> + </provider> + <provider> + <role>identity-assertion</role> + <name>Default</name> + <enabled>false</enabled> + </provider> + </gateway> + <service> + <role>AMBARI</role> + <url>$AMBARI_URL</url> + </service> +</topology>
