mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
* Add missing \" on obfuscatePassword * Add tests for AsyncJobManagerImpl.obfuscatePassword * fix for "password},"
This commit is contained in:
parent
db3fdf4142
commit
1ba6a49fa4
@ -475,14 +475,19 @@ public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager,
|
||||
return job;
|
||||
}
|
||||
|
||||
private String obfuscatePassword(String result, boolean hidePassword) {
|
||||
public String obfuscatePassword(String result, boolean hidePassword) {
|
||||
if (hidePassword) {
|
||||
String pattern = "\"password\":";
|
||||
if (result != null) {
|
||||
if (result.contains(pattern)) {
|
||||
String[] resp = result.split(pattern);
|
||||
String psswd = resp[1].toString().split(",")[0];
|
||||
result = resp[0] + pattern + psswd.replace(psswd.substring(2, psswd.length() - 1), "*****") + "," + resp[1].split(",", 2)[1];
|
||||
if (psswd.endsWith("}")) {
|
||||
psswd = psswd.substring(0, psswd.length() - 1);
|
||||
result = resp[0] + pattern + psswd.replace(psswd.substring(2, psswd.length() - 1), "*****") + "}," + resp[1].split(",", 2)[1];
|
||||
} else {
|
||||
result = resp[0] + pattern + psswd.replace(psswd.substring(2, psswd.length() - 1), "*****") + "," + resp[1].split(",", 2)[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,116 +16,67 @@
|
||||
// under the License.
|
||||
package org.apache.cloudstack.framework.jobs;
|
||||
|
||||
/*
|
||||
* This integration test requires real DB setup, it is not meant to run at per-build
|
||||
* basis, it can only be opened in developer's run
|
||||
*
|
||||
*
|
||||
import org.apache.cloudstack.framework.jobs.impl.AsyncJobManagerImpl;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "classpath:/AsyncJobManagerTestContext.xml")
|
||||
public class AsyncJobManagerTest extends TestCase {
|
||||
private static final Logger s_logger =
|
||||
Logger.getLogger(AsyncJobManagerTest.class);
|
||||
@RunWith (MockitoJUnitRunner.class)
|
||||
public class AsyncJobManagerTest {
|
||||
|
||||
@Inject
|
||||
AsyncJobManager _jobMgr;
|
||||
@Spy
|
||||
AsyncJobManagerImpl asyncJobManager;
|
||||
|
||||
@Inject
|
||||
AsyncJobTestDashboard _testDashboard;
|
||||
String input = "\"haprovider\":\"kvmhaprovider\"},\"outofbandmanagement\":{\"powerstate\":\"On\",\"enabled\":true,\"driver\":\"redfish\",\"address\":\"oob-address.com\",\"port\":\"80\",\"username\":\"root\",\"password\":\"password\"},\"resourcestate\":\"PrepareForMaintenance\",\"hahost\":false";
|
||||
String expected = "\"haprovider\":\"kvmhaprovider\"},\"outofbandmanagement\":{\"powerstate\":\"On\",\"enabled\":true,\"driver\":\"redfish\",\"address\":\"oob-address.com\",\"port\":\"80\",\"username\":\"root\",\"password\":\"p*****\"},\"resourcestate\":\"PrepareForMaintenance\",\"hahost\":false";
|
||||
String obfuscatedInput = "\"haprovider\":\"kvmhaprovider\"},\"outofbandmanagement\":{\"powerstate\":\"On\",\"enabled\":true,\"driver\":\"redfish\",\"address\":\"oob-address.com\",\"port\":\"80\",\"username\":\"root\",\"password\":\"p***\"},\"resourcestate\":\"PrepareForMaintenance\",\"hahost\":false";
|
||||
String noPassword = "\"haprovider\":\"kvmhaprovider\"},\"outofbandmanagement\":{\"powerstate\":\"On\",\"enabled\":true,\"driver\":\"redfish\",\"address\":\"oob-address.com\",\"port\":\"80\",\"username\":\"root\"},\"resourcestate\":\"PrepareForMaintenance\",\"hahost\":false";
|
||||
|
||||
@Override
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
try {
|
||||
ComponentContext.initComponentsLifeCycle();
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
s_logger.error(ex.getMessage());
|
||||
}
|
||||
}
|
||||
String inputNoBraces = "\"password\":\"password\"\",\"action\":\"OFF\"";
|
||||
String expectedNoBraces = "\"password\":\"p*****\",\"action\":\"OFF\"";
|
||||
|
||||
@Override
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
public void testWaitBehave() {
|
||||
|
||||
final Object me = this;
|
||||
new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
s_logger.info("Sleeping...");
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
s_logger.debug("[ignored] .");
|
||||
}
|
||||
|
||||
s_logger.info("wakeup");
|
||||
synchronized (me) {
|
||||
me.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
}).start();
|
||||
|
||||
s_logger.info("First wait");
|
||||
synchronized (me) {
|
||||
try {
|
||||
wait(5000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
s_logger.info("First wait done");
|
||||
|
||||
s_logger.info("Second wait");
|
||||
synchronized (me) {
|
||||
try {
|
||||
wait(5000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
s_logger.info("Second wait done");
|
||||
@Test
|
||||
public void obfuscatePasswordTest() {
|
||||
String result = asyncJobManager.obfuscatePassword(input, true);
|
||||
Assert.assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
final int TOTAL_JOBS_PER_QUEUE = 5;
|
||||
final int TOTAL_QUEUES = 100;
|
||||
|
||||
for (int i = 0; i < TOTAL_QUEUES; i++) {
|
||||
for (int j = 0; j < TOTAL_JOBS_PER_QUEUE; j++) {
|
||||
AsyncJobVO job = new AsyncJobVO();
|
||||
job.setCmd("TestCmd");
|
||||
job.setDispatcher("TestJobDispatcher");
|
||||
job.setCmdInfo("TestCmd info");
|
||||
|
||||
_jobMgr.submitAsyncJob(job, "fakequeue", i);
|
||||
|
||||
s_logger.info("Job submitted. job " + job.getId() + ", queue: " + i);
|
||||
}
|
||||
}
|
||||
|
||||
while (true) {
|
||||
if (_testDashboard.getCompletedJobCount() == TOTAL_JOBS_PER_QUEUE * TOTAL_QUEUES)
|
||||
break;
|
||||
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
s_logger.debug("[ignored] .");
|
||||
}
|
||||
}
|
||||
|
||||
s_logger.info("Test done with " + _testDashboard.getCompletedJobCount() + " job executed");
|
||||
public void obfuscatePasswordTestNoBraces() {
|
||||
String result = asyncJobManager.obfuscatePassword(inputNoBraces, true);
|
||||
Assert.assertEquals(expectedNoBraces, result);
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
@Test
|
||||
public void obfuscatePasswordTestHidePasswordFalse() {
|
||||
String result = asyncJobManager.obfuscatePassword(input, false);
|
||||
Assert.assertEquals(input, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void obfuscatePasswordTestObfuscatedInput() {
|
||||
String result = asyncJobManager.obfuscatePassword(obfuscatedInput, true);
|
||||
Assert.assertEquals(expected, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void obfuscatePasswordTestHidePasswordFalseObfuscatedInput() {
|
||||
String result = asyncJobManager.obfuscatePassword(obfuscatedInput, false);
|
||||
Assert.assertEquals(obfuscatedInput, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void obfuscatePasswordTestNoPassword() {
|
||||
String result = asyncJobManager.obfuscatePassword(noPassword, true);
|
||||
Assert.assertEquals(noPassword, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void obfuscatePasswordTestHidePasswordNoPassword() {
|
||||
String result = asyncJobManager.obfuscatePassword(noPassword, false);
|
||||
Assert.assertEquals(noPassword, result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user