|
|
@ -19,8 +19,8 @@ package org.apache.dolphinscheduler.plugin.datasource.api.utils; |
|
|
|
|
|
|
|
|
|
|
|
import static org.apache.dolphinscheduler.spi.utils.Constants.DATASOURCE_ENCRYPTION_ENABLE; |
|
|
|
import static org.apache.dolphinscheduler.spi.utils.Constants.DATASOURCE_ENCRYPTION_ENABLE; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.utils.PropertyUtils; |
|
|
|
import org.apache.dolphinscheduler.spi.utils.Constants; |
|
|
|
import org.apache.dolphinscheduler.spi.utils.Constants; |
|
|
|
import org.apache.dolphinscheduler.spi.utils.PropertyUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration; |
|
|
|
import org.apache.hadoop.conf.Configuration; |
|
|
|
import org.apache.hadoop.security.UserGroupInformation; |
|
|
|
import org.apache.hadoop.security.UserGroupInformation; |
|
|
@ -28,54 +28,50 @@ import org.apache.hadoop.security.UserGroupInformation; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
|
|
|
|
import org.mockito.MockedStatic; |
|
|
|
|
|
|
|
import org.mockito.Mockito; |
|
|
|
|
|
|
|
import org.mockito.junit.MockitoJUnitRunner; |
|
|
|
import org.mockito.stubbing.Answer; |
|
|
|
import org.mockito.stubbing.Answer; |
|
|
|
import org.powermock.api.mockito.PowerMockito; |
|
|
|
|
|
|
|
import org.powermock.core.classloader.annotations.PowerMockIgnore; |
|
|
|
@RunWith(MockitoJUnitRunner.class) |
|
|
|
import org.powermock.core.classloader.annotations.PrepareForTest; |
|
|
|
|
|
|
|
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; |
|
|
|
|
|
|
|
import org.powermock.modules.junit4.PowerMockRunner; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(PowerMockRunner.class) |
|
|
|
|
|
|
|
@SuppressStaticInitializationFor( |
|
|
|
|
|
|
|
value = {"org.apache.dolphinscheduler.spi.utils.PropertyUtils", "org.apache.hadoop.security.UserGroupInformation"} |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
@PrepareForTest(value = {PropertyUtils.class, UserGroupInformation.class, CommonUtils.class, PasswordUtils.class}) |
|
|
|
|
|
|
|
@PowerMockIgnore({"jdk.xml.*", "org.apache.hadoop.security.*"}) |
|
|
|
|
|
|
|
public class CommonUtilsTest { |
|
|
|
public class CommonUtilsTest { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testGetKerberosStartupState() { |
|
|
|
public void testGetKerberosStartupState() { |
|
|
|
PowerMockito.mockStatic(CommonUtils.class); |
|
|
|
try ( |
|
|
|
PowerMockito.when(CommonUtils.getKerberosStartupState()).thenAnswer((Answer<Boolean>) invocation -> false); |
|
|
|
MockedStatic<CommonUtils> mockedCommonUtils = Mockito.mockStatic(CommonUtils.class); |
|
|
|
|
|
|
|
MockedStatic<PropertyUtils> mockedPropertyUtils = Mockito.mockStatic(PropertyUtils.class)) { |
|
|
|
|
|
|
|
Mockito.when(CommonUtils.getKerberosStartupState()).thenAnswer((Answer<Boolean>) invocation -> false); |
|
|
|
boolean kerberosStartupState = CommonUtils.getKerberosStartupState(); |
|
|
|
boolean kerberosStartupState = CommonUtils.getKerberosStartupState(); |
|
|
|
Assert.assertFalse(kerberosStartupState); |
|
|
|
Assert.assertFalse(kerberosStartupState); |
|
|
|
|
|
|
|
|
|
|
|
PowerMockito.mockStatic(PropertyUtils.class); |
|
|
|
Mockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)) |
|
|
|
PowerMockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)).thenAnswer((Answer<String>) invocation -> "HDFS"); |
|
|
|
.thenAnswer((Answer<String>) invocation -> "HDFS"); |
|
|
|
PowerMockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, true)) |
|
|
|
Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, true)) |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE); |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE); |
|
|
|
kerberosStartupState = CommonUtils.getKerberosStartupState(); |
|
|
|
kerberosStartupState = CommonUtils.getKerberosStartupState(); |
|
|
|
Assert.assertFalse(kerberosStartupState); |
|
|
|
Assert.assertFalse(kerberosStartupState); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testLoadKerberosConf() { |
|
|
|
public void testLoadKerberosConf() { |
|
|
|
|
|
|
|
try ( |
|
|
|
|
|
|
|
MockedStatic<PropertyUtils> mockedPropertyUtils = Mockito.mockStatic(PropertyUtils.class); |
|
|
|
|
|
|
|
MockedStatic<UserGroupInformation> mockedUserGroupInformation = |
|
|
|
|
|
|
|
Mockito.mockStatic(UserGroupInformation.class)) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
PowerMockito.mockStatic(PropertyUtils.class); |
|
|
|
Mockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)) |
|
|
|
PowerMockito.when(PropertyUtils.getUpperCaseString(Constants.RESOURCE_STORAGE_TYPE)).thenAnswer((Answer<String>) invocation -> "HDFS"); |
|
|
|
.thenAnswer((Answer<String>) invocation -> "HDFS"); |
|
|
|
PowerMockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) |
|
|
|
Mockito.when(PropertyUtils.getBoolean(Constants.HADOOP_SECURITY_AUTHENTICATION_STARTUP_STATE, false)) |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE); |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE); |
|
|
|
PowerMockito.when(PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH)) |
|
|
|
Mockito.when(PropertyUtils.getString(Constants.JAVA_SECURITY_KRB5_CONF_PATH)) |
|
|
|
.thenAnswer((Answer<String>) invocation -> "/opt/krb5.conf"); |
|
|
|
.thenAnswer((Answer<String>) invocation -> "/opt/krb5.conf"); |
|
|
|
PowerMockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME)) |
|
|
|
Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_USERNAME)) |
|
|
|
.thenAnswer((Answer<String>) invocation -> "hdfs-mycluster@ESZ.COM"); |
|
|
|
.thenAnswer((Answer<String>) invocation -> "hdfs-mycluster@ESZ.COM"); |
|
|
|
PowerMockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH)) |
|
|
|
Mockito.when(PropertyUtils.getString(Constants.LOGIN_USER_KEY_TAB_PATH)) |
|
|
|
.thenAnswer((Answer<String>) invocation -> "/opt/hdfs.headless.keytab"); |
|
|
|
.thenAnswer((Answer<String>) invocation -> "/opt/hdfs.headless.keytab"); |
|
|
|
|
|
|
|
Configuration configuration = Mockito.mock(Configuration.class); |
|
|
|
PowerMockito.mockStatic(UserGroupInformation.class); |
|
|
|
|
|
|
|
Configuration configuration = PowerMockito.mock(Configuration.class); |
|
|
|
|
|
|
|
PowerMockito.whenNew(Configuration.class).withNoArguments().thenReturn(configuration); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean result = CommonUtils.loadKerberosConf(configuration); |
|
|
|
boolean result = CommonUtils.loadKerberosConf(configuration); |
|
|
|
Assert.assertTrue(result); |
|
|
|
Assert.assertTrue(result); |
|
|
|
|
|
|
|
|
|
|
@ -85,57 +81,60 @@ public class CommonUtilsTest { |
|
|
|
Assert.fail("load Kerberos Conf failed" + e.getMessage()); |
|
|
|
Assert.fail("load Kerberos Conf failed" + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void encodePassword() { |
|
|
|
public void encodePassword() { |
|
|
|
PowerMockito.mockStatic(PropertyUtils.class); |
|
|
|
try (MockedStatic<PropertyUtils> mockedPropertyUtils = Mockito.mockStatic(PropertyUtils.class)) { |
|
|
|
PowerMockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) |
|
|
|
Mockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE); |
|
|
|
.thenReturn(Boolean.TRUE); |
|
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("", PasswordUtils.encodePassword("")); |
|
|
|
Assert.assertEquals("", PasswordUtils.encodePassword("")); |
|
|
|
Assert.assertEquals("bnVsbE1USXpORFUy", PasswordUtils.encodePassword("123456")); |
|
|
|
Assert.assertEquals("bnVsbE1USXpORFUy", PasswordUtils.encodePassword("123456")); |
|
|
|
Assert.assertEquals("bnVsbElWRkJXbGhUVjBBPQ==", PasswordUtils.encodePassword("!QAZXSW@")); |
|
|
|
Assert.assertEquals("bnVsbElWRkJXbGhUVjBBPQ==", PasswordUtils.encodePassword("!QAZXSW@")); |
|
|
|
Assert.assertEquals("bnVsbE5XUm1aMlZ5S0VBPQ==", PasswordUtils.encodePassword("5dfger(@")); |
|
|
|
Assert.assertEquals("bnVsbE5XUm1aMlZ5S0VBPQ==", PasswordUtils.encodePassword("5dfger(@")); |
|
|
|
|
|
|
|
|
|
|
|
PowerMockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) |
|
|
|
Mockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.FALSE); |
|
|
|
.thenReturn(Boolean.FALSE); |
|
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals("", PasswordUtils.encodePassword("")); |
|
|
|
Assert.assertEquals("", PasswordUtils.encodePassword("")); |
|
|
|
Assert.assertEquals("123456", PasswordUtils.encodePassword("123456")); |
|
|
|
Assert.assertEquals("123456", PasswordUtils.encodePassword("123456")); |
|
|
|
Assert.assertEquals("!QAZXSW@", PasswordUtils.encodePassword("!QAZXSW@")); |
|
|
|
Assert.assertEquals("!QAZXSW@", PasswordUtils.encodePassword("!QAZXSW@")); |
|
|
|
Assert.assertEquals("5dfger(@", PasswordUtils.encodePassword("5dfger(@")); |
|
|
|
Assert.assertEquals("5dfger(@", PasswordUtils.encodePassword("5dfger(@")); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void decodePassword() { |
|
|
|
public void decodePassword() { |
|
|
|
PowerMockito.mockStatic(PropertyUtils.class); |
|
|
|
try ( |
|
|
|
PowerMockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) |
|
|
|
MockedStatic<CommonUtils> mockedCommonUtils = Mockito.mockStatic(CommonUtils.class); |
|
|
|
|
|
|
|
MockedStatic<PasswordUtils> mockedPasswordUtils = Mockito.mockStatic(PasswordUtils.class)) { |
|
|
|
|
|
|
|
Mockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE); |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.TRUE); |
|
|
|
|
|
|
|
|
|
|
|
PropertyUtils.setValue(DATASOURCE_ENCRYPTION_ENABLE, "true"); |
|
|
|
PropertyUtils.setValue(DATASOURCE_ENCRYPTION_ENABLE, "true"); |
|
|
|
|
|
|
|
|
|
|
|
PowerMockito.mockStatic(PasswordUtils.class); |
|
|
|
Mockito.when(PasswordUtils.decodePassword("bnVsbE1USXpORFUy")).thenReturn("123456"); |
|
|
|
PowerMockito.when(PasswordUtils.decodePassword("bnVsbE1USXpORFUy")).thenReturn("123456"); |
|
|
|
Mockito.when(PasswordUtils.decodePassword("bnVsbElWRkJXbGhUVjBBPQ==")).thenReturn("!QAZXSW@"); |
|
|
|
PowerMockito.when(PasswordUtils.decodePassword("bnVsbElWRkJXbGhUVjBBPQ==")).thenReturn("!QAZXSW@"); |
|
|
|
Mockito.when(PasswordUtils.decodePassword("bnVsbE5XUm1aMlZ5S0VBPQ==")).thenReturn("5dfger(@"); |
|
|
|
PowerMockito.when(PasswordUtils.decodePassword("bnVsbE5XUm1aMlZ5S0VBPQ==")).thenReturn("5dfger(@"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals(null, PasswordUtils.decodePassword("")); |
|
|
|
Assert.assertEquals(null, PasswordUtils.decodePassword("")); |
|
|
|
Assert.assertEquals("123456", PasswordUtils.decodePassword("bnVsbE1USXpORFUy")); |
|
|
|
Assert.assertEquals("123456", PasswordUtils.decodePassword("bnVsbE1USXpORFUy")); |
|
|
|
Assert.assertEquals("!QAZXSW@", PasswordUtils.decodePassword("bnVsbElWRkJXbGhUVjBBPQ==")); |
|
|
|
Assert.assertEquals("!QAZXSW@", PasswordUtils.decodePassword("bnVsbElWRkJXbGhUVjBBPQ==")); |
|
|
|
Assert.assertEquals("5dfger(@", PasswordUtils.decodePassword("bnVsbE5XUm1aMlZ5S0VBPQ==")); |
|
|
|
Assert.assertEquals("5dfger(@", PasswordUtils.decodePassword("bnVsbE5XUm1aMlZ5S0VBPQ==")); |
|
|
|
|
|
|
|
|
|
|
|
PowerMockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) |
|
|
|
Mockito.when(PropertyUtils.getBoolean(DATASOURCE_ENCRYPTION_ENABLE, false)) |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.FALSE); |
|
|
|
.thenAnswer((Answer<Boolean>) invocation -> Boolean.FALSE); |
|
|
|
|
|
|
|
|
|
|
|
PowerMockito.when(PasswordUtils.decodePassword("123456")).thenReturn("123456"); |
|
|
|
Mockito.when(PasswordUtils.decodePassword("123456")).thenReturn("123456"); |
|
|
|
PowerMockito.when(PasswordUtils.decodePassword("!QAZXSW@")).thenReturn("!QAZXSW@"); |
|
|
|
Mockito.when(PasswordUtils.decodePassword("!QAZXSW@")).thenReturn("!QAZXSW@"); |
|
|
|
PowerMockito.when(PasswordUtils.decodePassword("5dfger(@")).thenReturn("5dfger(@"); |
|
|
|
Mockito.when(PasswordUtils.decodePassword("5dfger(@")).thenReturn("5dfger(@"); |
|
|
|
|
|
|
|
|
|
|
|
Assert.assertEquals(null, PasswordUtils.decodePassword("")); |
|
|
|
Assert.assertEquals(null, PasswordUtils.decodePassword("")); |
|
|
|
Assert.assertEquals("123456", PasswordUtils.decodePassword("123456")); |
|
|
|
Assert.assertEquals("123456", PasswordUtils.decodePassword("123456")); |
|
|
|
Assert.assertEquals("!QAZXSW@", PasswordUtils.decodePassword("!QAZXSW@")); |
|
|
|
Assert.assertEquals("!QAZXSW@", PasswordUtils.decodePassword("!QAZXSW@")); |
|
|
|
Assert.assertEquals("5dfger(@", PasswordUtils.decodePassword("5dfger(@")); |
|
|
|
Assert.assertEquals("5dfger(@", PasswordUtils.decodePassword("5dfger(@")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|