NIFI-8614 Updated FileBasedClusterNodeFirewallFactoryBean to return default implementation

- FileBasedClusterNodeFirewallFactoryBean returns PermitAllClusterNodeFirewall instead of null to avoid having the Spring Framework return a NullBean in Spring Framework 5

Signed-off-by: Matthew Burgess <mattyb149@apache.org>

This closes #5082
This commit is contained in:
exceptionfactory 2021-05-18 08:58:23 -05:00 committed by Matthew Burgess
parent 1f0425b86d
commit a223f1e71f
No known key found for this signature in database
GPG Key ID: 05D3DEB8126DAD24
2 changed files with 90 additions and 6 deletions

View File

@ -18,6 +18,7 @@ package org.apache.nifi.cluster.spring;
import java.io.File;
import org.apache.nifi.cluster.firewall.ClusterNodeFirewall;
import org.apache.nifi.cluster.firewall.impl.FileBasedClusterNodeFirewall;
import org.apache.nifi.util.NiFiProperties;
import org.springframework.beans.factory.FactoryBean;
@ -25,18 +26,20 @@ import org.springframework.beans.factory.FactoryBean;
/**
* Factory bean for creating a singleton FileBasedClusterNodeFirewall instance.
*/
public class FileBasedClusterNodeFirewallFactoryBean implements FactoryBean<FileBasedClusterNodeFirewall> {
public class FileBasedClusterNodeFirewallFactoryBean implements FactoryBean<ClusterNodeFirewall> {
private FileBasedClusterNodeFirewall firewall;
private ClusterNodeFirewall firewall;
private NiFiProperties properties;
@Override
public FileBasedClusterNodeFirewall getObject() throws Exception {
public ClusterNodeFirewall getObject() throws Exception {
if (firewall == null) {
final File config = properties.getClusterNodeFirewallFile();
final File restoreDirectory = properties.getRestoreDirectory();
if (config != null) {
if (config == null) {
firewall = new PermitAllClusterNodeFirewall();
} else {
firewall = new FileBasedClusterNodeFirewall(config, restoreDirectory);
}
}
@ -44,8 +47,8 @@ public class FileBasedClusterNodeFirewallFactoryBean implements FactoryBean<File
}
@Override
public Class<FileBasedClusterNodeFirewall> getObjectType() {
return FileBasedClusterNodeFirewall.class;
public Class<ClusterNodeFirewall> getObjectType() {
return ClusterNodeFirewall.class;
}
@Override
@ -56,4 +59,12 @@ public class FileBasedClusterNodeFirewallFactoryBean implements FactoryBean<File
public void setProperties(NiFiProperties properties) {
this.properties = properties;
}
private static class PermitAllClusterNodeFirewall implements ClusterNodeFirewall {
@Override
public boolean isPermissible(final String hostOrIp) {
return true;
}
}
}

View File

@ -0,0 +1,73 @@
/*
* 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.nifi.cluster.firewall.impl;
import org.apache.nifi.cluster.firewall.ClusterNodeFirewall;
import org.apache.nifi.cluster.spring.FileBasedClusterNodeFirewallFactoryBean;
import org.apache.nifi.util.NiFiProperties;
import org.apache.nifi.util.StringUtils;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class FileBasedClusterNodeFirewallFactoryBeanTest {
private static final String PROPERTIES_SUFFIX = ".firewall.properties";
private FileBasedClusterNodeFirewallFactoryBean factoryBean;
private NiFiProperties properties;
@Before
public void setFactoryBean() {
properties = NiFiProperties.createBasicNiFiProperties(StringUtils.EMPTY);
factoryBean = new FileBasedClusterNodeFirewallFactoryBean();
factoryBean.setProperties(properties);
}
@Test
public void testGetObjectType() {
final Class<ClusterNodeFirewall> objectType = factoryBean.getObjectType();
assertEquals(ClusterNodeFirewall.class, objectType);
}
@Test
public void testGetObjectClusterNodeFirewallFileNotConfigured() throws Exception {
final ClusterNodeFirewall clusterNodeFirewall = factoryBean.getObject();
assertNotNull(clusterNodeFirewall);
}
@Test
public void testGetObjectClusterNodeFirewallFileConfigured() throws Exception {
final File firewallProperties = File.createTempFile(FileBasedClusterNodeFirewallFactoryBeanTest.class.getName(), PROPERTIES_SUFFIX);
firewallProperties.deleteOnExit();
final Map<String, String> beanProperties = new HashMap<>();
beanProperties.put(NiFiProperties.CLUSTER_FIREWALL_FILE, firewallProperties.getAbsolutePath());
properties = NiFiProperties.createBasicNiFiProperties(StringUtils.EMPTY, beanProperties);
factoryBean.setProperties(properties);
final ClusterNodeFirewall clusterNodeFirewall = factoryBean.getObject();
assertNotNull(clusterNodeFirewall);
assertEquals(FileBasedClusterNodeFirewall.class, clusterNodeFirewall.getClass());
}
}