mirror of https://github.com/apache/cloudstack.git
* 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;
|
return job;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String obfuscatePassword(String result, boolean hidePassword) {
|
public String obfuscatePassword(String result, boolean hidePassword) {
|
||||||
if (hidePassword) {
|
if (hidePassword) {
|
||||||
String pattern = "\"password\":";
|
String pattern = "\"password\":";
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
if (result.contains(pattern)) {
|
if (result.contains(pattern)) {
|
||||||
String[] resp = result.split(pattern);
|
String[] resp = result.split(pattern);
|
||||||
String psswd = resp[1].toString().split(",")[0];
|
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.
|
// under the License.
|
||||||
package org.apache.cloudstack.framework.jobs;
|
package org.apache.cloudstack.framework.jobs;
|
||||||
|
|
||||||
/*
|
import org.apache.cloudstack.framework.jobs.impl.AsyncJobManagerImpl;
|
||||||
* This integration test requires real DB setup, it is not meant to run at per-build
|
import org.junit.Assert;
|
||||||
* basis, it can only be opened in developer's run
|
import org.junit.Test;
|
||||||
*
|
import org.junit.runner.RunWith;
|
||||||
*
|
import org.mockito.Spy;
|
||||||
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith (MockitoJUnitRunner.class)
|
||||||
@ContextConfiguration(locations = "classpath:/AsyncJobManagerTestContext.xml")
|
public class AsyncJobManagerTest {
|
||||||
public class AsyncJobManagerTest extends TestCase {
|
|
||||||
private static final Logger s_logger =
|
|
||||||
Logger.getLogger(AsyncJobManagerTest.class);
|
|
||||||
|
|
||||||
@Inject
|
@Spy
|
||||||
AsyncJobManager _jobMgr;
|
AsyncJobManagerImpl asyncJobManager;
|
||||||
|
|
||||||
@Inject
|
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";
|
||||||
AsyncJobTestDashboard _testDashboard;
|
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
|
String inputNoBraces = "\"password\":\"password\"\",\"action\":\"OFF\"";
|
||||||
@Before
|
String expectedNoBraces = "\"password\":\"p*****\",\"action\":\"OFF\"";
|
||||||
public void setUp() throws Exception {
|
|
||||||
try {
|
|
||||||
ComponentContext.initComponentsLifeCycle();
|
|
||||||
} catch (Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
s_logger.error(ex.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Test
|
||||||
@After
|
public void obfuscatePasswordTest() {
|
||||||
public void tearDown() throws Exception {
|
String result = asyncJobManager.obfuscatePassword(input, true);
|
||||||
}
|
Assert.assertEquals(expected, result);
|
||||||
|
|
||||||
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
|
@Test
|
||||||
public void test() {
|
public void obfuscatePasswordTestNoBraces() {
|
||||||
final int TOTAL_JOBS_PER_QUEUE = 5;
|
String result = asyncJobManager.obfuscatePassword(inputNoBraces, true);
|
||||||
final int TOTAL_QUEUES = 100;
|
Assert.assertEquals(expectedNoBraces, result);
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
*/
|
@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…
Reference in New Issue