KNOX-1367 - Revert the previous schema changes, make a switch to enable strict topolo...
authorSandeep More <more@apache.org>
Fri, 29 Jun 2018 20:07:22 +0000 (16:07 -0400)
committerSandeep More <more@apache.org>
Fri, 29 Jun 2018 20:08:03 +0000 (16:08 -0400)
gateway-server/src/main/java/org/apache/knox/gateway/GatewayMessages.java
gateway-server/src/main/java/org/apache/knox/gateway/config/impl/GatewayConfigImpl.java
gateway-server/src/main/java/org/apache/knox/gateway/services/topology/impl/DefaultTopologyService.java
gateway-server/src/main/resources/conf/topology-v1.xsd
gateway-server/src/test/java/org/apache/knox/gateway/util/KnoxCLITest.java
gateway-spi/src/main/java/org/apache/knox/gateway/config/GatewayConfig.java
gateway-test-release-utils/src/main/java/org/apache/knox/gateway/GatewayTestConfig.java

index 743b958..71c5396 100644 (file)
@@ -114,6 +114,10 @@ public interface GatewayMessages {
   @Message( level = MessageLevel.ERROR, text = "Failed to undeploy topology {0}: {1}" )
   void failedToUndeployTopology( String name, @StackTrace(level=MessageLevel.DEBUG) Exception e );
 
+  @Message( level = MessageLevel.WARN, text = "Failed to validate topology {0} error {1}. Please "
+      + "consider using descriptors instead of topologies" )
+  void failedToValidateTopology( String name, String cause );
+
   @Message( level = MessageLevel.INFO, text = "Deleting topology {0}" )
   void deletingTopology( String topologyName );
 
index 0ff8b22..51abba2 100644 (file)
@@ -191,8 +191,8 @@ public class GatewayConfigImpl extends Configuration implements GatewayConfig {
   public static final String DEFAULT_DATA_DIR = "data";
   private static final String PROVIDERCONFIG_DIR_NAME = "shared-providers";
   private static final String DESCRIPTORS_DIR_NAME = "descriptors";
-
   public static final String REMOTE_ALIAS_SERVICE_ENABLED = GATEWAY_CONFIG_FILE_PREFIX + ".remote.alias.service.enabled";
+  public static final String STRICT_TOPOLOGY_VALIDATION = GATEWAY_CONFIG_FILE_PREFIX + ".strict.topology.validation";
 
   /**
    * Comma-separated list of topology names, which should be forcibly treated as read-only.
@@ -212,9 +212,8 @@ public class GatewayConfigImpl extends Configuration implements GatewayConfig {
   public static final int DEFAULT_WEBSOCKET_IDLE_TIMEOUT = 300000;
 
   public static final boolean DEFAULT_GATEWAY_PORT_MAPPING_ENABLED = true;
-
   public static final boolean DEFAULT_REMOTE_ALIAS_SERVICE_ENABLED = true;
-
+  public static final boolean DEFAULT_STRICT_TOPOLOGY_VALIDATION = false;
 
   /**
    * Default list of MIME Type to be compressed.
@@ -1110,4 +1109,18 @@ public class GatewayConfigImpl extends Configuration implements GatewayConfig {
     return result;
   }
 
+  /**
+   * Returns true when strict topology validation is enabled, in which case if
+   * topology validation fails Knox will throw a runtime exception. If false and
+   * topology validation fails Knox will log an ERROR and move on.
+   *
+   * @return
+   * @since 1.1.0
+   */
+  @Override
+  public boolean isTopologyValidationEnabled() {
+    final String result = get(STRICT_TOPOLOGY_VALIDATION, Boolean.toString(DEFAULT_STRICT_TOPOLOGY_VALIDATION));
+    return Boolean.parseBoolean(result);
+  }
+
 }
index d2f6ad0..fede61b 100644 (file)
@@ -115,6 +115,8 @@ public class DefaultTopologyService
 
   private RemoteConfigurationMonitor remoteMonitor = null;
 
+  private GatewayConfig config;
+
   private Topology loadTopology(File file) throws IOException, SAXException, URISyntaxException, InterruptedException {
     final long TIMEOUT = 250; //ms
     final long DELAY = 50; //ms
@@ -164,7 +166,13 @@ public class DefaultTopologyService
       TopologyValidator tv = new TopologyValidator(topology);
 
       if(!tv.validateTopology()) {
-        throw new SAXException(tv.getErrorString());
+        if(config != null && config.isTopologyValidationEnabled()) {
+          /* If strict validation enabled we fail */
+          throw new SAXException(tv.getErrorString());
+        } else {
+          /* Log and move on */
+          log.failedToValidateTopology(topology.getName(), tv.getErrorString());
+        }
       }
 
       long start = System.currentTimeMillis();
@@ -606,6 +614,7 @@ public class DefaultTopologyService
   @Override
   public void init(GatewayConfig config, Map<String, String> options) throws ServiceLifecycleException {
 
+    this.config = config;
     String gatewayConfDir = config.getGatewayConfDir();
     if (gatewayConfDir != null) {
       System.setProperty(ServiceDiscovery.CONFIG_DIR_PROPERTY, gatewayConfDir);
index db77156..722a365 100644 (file)
@@ -30,7 +30,7 @@ limitations under the License.
 
                             <h:element name="provider" maxOccurs="unbounded">
                                 <h:complexType>
-                                    <h:choice minOccurs="1" maxOccurs="unbounded">
+                                    <h:sequence>
                                         <h:element name="role" type="h:string" minOccurs="1" maxOccurs="1"/>
                                         <h:element name="name" type="h:string" minOccurs="1" maxOccurs="1"/>
                                         <h:element name="enabled" type="h:boolean" minOccurs="1" maxOccurs="1"/>
@@ -45,7 +45,7 @@ limitations under the License.
                                                 <h:attribute name="value" type="h:string" use="optional" />
                                             </h:complexType>
                                         </h:element>
-                                    </h:choice>
+                                    </h:sequence>
                                 </h:complexType>
                             </h:element>
 
index ae837df..116b8dd 100644 (file)
@@ -1011,7 +1011,7 @@ public class KnoxCLITest {
     assertThat(outContent.toString(), containsString(config.getGatewayTopologyDir()));
     assertThat(outContent.toString(), containsString("test-cluster-bad"));
     assertThat(outContent.toString(), containsString("unsuccessful"));
-    //assertThat(outContent.toString(), containsString("Invalid content"));
+    assertThat(outContent.toString(), containsString("Invalid content"));
     assertThat(outContent.toString(), containsString("Line"));
 
 
index c5e2337..7cce80d 100644 (file)
@@ -433,5 +433,16 @@ public interface GatewayConfig {
    */
   List<String> getDispatchWhitelistServices();
 
+  /**
+   * Returns true when strict topology validation is enabled,
+   * in which case if topology validation fails Knox will throw
+   * a runtime exception. If false and topology validation fails
+   * Knox will log an ERROR and move on.
+   *
+   * @since 1.1.0
+   * @return
+   */
+  boolean isTopologyValidationEnabled();
+
 
 }
index f8d4ec7..003e875 100644 (file)
@@ -732,4 +732,17 @@ public class GatewayTestConfig extends Configuration implements GatewayConfig {
     return Collections.emptyList();
   }
 
+  /**
+   * Returns true when strict topology validation is enabled, in which case if
+   * topology validation fails Knox will throw a runtime exception. If false and
+   * topology validation fails Knox will log an ERROR and move on.
+   *
+   * @return
+   * @since 1.1.0
+   */
+  @Override
+  public boolean isTopologyValidationEnabled() {
+    return false;
+  }
+
 }