Skip to content

Commit f702f7f

Browse files
Remove sensitive params (VmPassword, etc) from VMWork log (#8553)
1 parent 3550f6a commit f702f7f

File tree

6 files changed

+160
-6
lines changed

6 files changed

+160
-6
lines changed

engine/components-api/src/main/java/com/cloud/vm/VmWork.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,21 @@
1717
package com.cloud.vm;
1818

1919
import java.io.Serializable;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import org.apache.commons.collections.CollectionUtils;
25+
import org.apache.commons.lang3.StringUtils;
26+
27+
import com.cloud.serializer.GsonHelper;
28+
import com.fasterxml.jackson.core.JsonProcessingException;
29+
import com.fasterxml.jackson.databind.ObjectMapper;
30+
import com.google.gson.Gson;
2031

2132
public class VmWork implements Serializable {
2233
private static final long serialVersionUID = -6946320465729853589L;
34+
private static final Gson gsonLogger = GsonHelper.getGsonLogger();
2335

2436
long userId;
2537
long accountId;
@@ -56,4 +68,31 @@ public long getVmId() {
5668
public String getHandlerName() {
5769
return handlerName;
5870
}
71+
72+
@Override
73+
public String toString() {
74+
return gsonLogger.toJson(this);
75+
}
76+
77+
protected String toStringAfterRemoveParams(String paramsObjName, List<String> params) {
78+
String ObjJsonStr = gsonLogger.toJson(this);
79+
if (StringUtils.isBlank(ObjJsonStr) || StringUtils.isBlank(paramsObjName) || CollectionUtils.isEmpty(params)) {
80+
return ObjJsonStr;
81+
}
82+
83+
try {
84+
Map<String, Object> ObjMap = new ObjectMapper().readValue(ObjJsonStr, HashMap.class);
85+
if (ObjMap != null && ObjMap.containsKey(paramsObjName)) {
86+
for (String param : params) {
87+
((Map<String, String>)ObjMap.get(paramsObjName)).remove(param);
88+
}
89+
String resultJson = new ObjectMapper().writeValueAsString(ObjMap);
90+
return resultJson;
91+
}
92+
} catch (final JsonProcessingException e) {
93+
// Ignore json exception
94+
}
95+
96+
return ObjJsonStr;
97+
}
5998
}

engine/components-api/src/main/java/com/cloud/vm/VmWorkJobHandlerProxy.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@
2121
import java.util.HashMap;
2222
import java.util.Map;
2323

24-
import org.apache.log4j.Logger;
25-
26-
import com.google.gson.Gson;
27-
2824
import org.apache.cloudstack.framework.jobs.impl.JobSerializerHelper;
2925
import org.apache.cloudstack.jobs.JobInfo;
26+
import org.apache.log4j.Logger;
3027

3128
import com.cloud.serializer.GsonHelper;
3229
import com.cloud.utils.Pair;
30+
import com.google.gson.Gson;
3331

3432
/**
3533
* VmWorkJobHandlerProxy can not be used as standalone due to run-time
@@ -102,12 +100,12 @@ public Pair<JobInfo.Status, String> handleVmWorkJob(VmWork work) throws Exceptio
102100

103101
try {
104102
if (s_logger.isDebugEnabled())
105-
s_logger.debug("Execute VM work job: " + work.getClass().getName() + _gsonLogger.toJson(work));
103+
s_logger.debug("Execute VM work job: " + work.getClass().getName() + work);
106104

107105
Object obj = method.invoke(_target, work);
108106

109107
if (s_logger.isDebugEnabled())
110-
s_logger.debug("Done executing VM work job: " + work.getClass().getName() + _gsonLogger.toJson(work));
108+
s_logger.debug("Done executing VM work job: " + work.getClass().getName() + work);
111109

112110
assert (obj instanceof Pair);
113111
return (Pair<JobInfo.Status, String>)obj;

engine/orchestration/src/main/java/com/cloud/vm/VmWorkReboot.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package com.cloud.vm;
1818

1919
import java.io.Serializable;
20+
import java.util.ArrayList;
2021
import java.util.HashMap;
22+
import java.util.List;
2123
import java.util.Map;
2224

2325
import org.apache.cloudstack.framework.jobs.impl.JobSerializerHelper;
@@ -62,4 +64,11 @@ public void setParams(Map<VirtualMachineProfile.Param, Object> params) {
6264
}
6365
}
6466
}
67+
68+
@Override
69+
public String toString() {
70+
List<String> params = new ArrayList<>();
71+
params.add(VirtualMachineProfile.Param.VmPassword.getName());
72+
return super.toStringAfterRemoveParams("rawParams", params);
73+
}
6574
}

engine/orchestration/src/main/java/com/cloud/vm/VmWorkStart.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
package com.cloud.vm;
1919

2020
import java.io.Serializable;
21+
import java.util.ArrayList;
2122
import java.util.HashMap;
23+
import java.util.List;
2224
import java.util.Map;
2325

2426
import org.apache.cloudstack.context.CallContext;
@@ -135,4 +137,11 @@ public void setParams(Map<VirtualMachineProfile.Param, Object> params) {
135137
}
136138
}
137139
}
140+
141+
@Override
142+
public String toString() {
143+
List<String> params = new ArrayList<>();
144+
params.add(VirtualMachineProfile.Param.VmPassword.getName());
145+
return super.toStringAfterRemoveParams("rawParams", params);
146+
}
138147
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.vm;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.apache.cloudstack.framework.jobs.impl.JobSerializerHelper;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
public class VmWorkRebootTest {
27+
28+
@Test
29+
public void testToString() {
30+
VmWork vmWork = new VmWork(1l, 1l, 1l, "testhandler");
31+
Map<VirtualMachineProfile.Param, Object> params = new HashMap<>();
32+
String lastHost = "rO0ABXQABHRydWU";
33+
String lastHostSerialized = JobSerializerHelper.toObjectSerializedString(lastHost);
34+
params.put(VirtualMachineProfile.Param.ConsiderLastHost, lastHost);
35+
params.put(VirtualMachineProfile.Param.VmPassword, "rO0ABXQADnNhdmVkX3Bhc3N3b3Jk");
36+
VmWorkReboot workInfo = new VmWorkReboot(vmWork, params);
37+
String expectedVmWorkRebootStr = "{\"accountId\":1,\"vmId\":1,\"handlerName\":\"testhandler\",\"userId\":1,\"rawParams\":{\"ConsiderLastHost\":\"" + lastHostSerialized + "\"}}";
38+
39+
String vmWorkRebootStr = workInfo.toString();
40+
Assert.assertEquals(expectedVmWorkRebootStr, vmWorkRebootStr);
41+
}
42+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.vm;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
import org.apache.cloudstack.framework.jobs.impl.JobSerializerHelper;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
public class VmWorkStartTest {
27+
28+
@Test
29+
public void testToStringWithParams() {
30+
VmWork vmWork = new VmWork(1l, 1l, 1l, "testhandler");
31+
VmWorkStart workInfo = new VmWorkStart(vmWork);
32+
Map<VirtualMachineProfile.Param, Object> params = new HashMap<>();
33+
String lastHost = "rO0ABXQABHRydWU";
34+
String lastHostSerialized = JobSerializerHelper.toObjectSerializedString(lastHost);
35+
params.put(VirtualMachineProfile.Param.ConsiderLastHost, lastHost);
36+
params.put(VirtualMachineProfile.Param.VmPassword, "rO0ABXQADnNhdmVkX3Bhc3N3b3Jk");
37+
workInfo.setParams(params);
38+
String expectedVmWorkStartStr = "{\"accountId\":1,\"dcId\":0,\"vmId\":1,\"handlerName\":\"testhandler\",\"userId\":1,\"rawParams\":{\"ConsiderLastHost\":\"" + lastHostSerialized + "\"}}";
39+
40+
String vmWorkStartStr = workInfo.toString();
41+
Assert.assertEquals(expectedVmWorkStartStr, vmWorkStartStr);
42+
}
43+
44+
@Test
45+
public void testToStringWithRawParams() {
46+
VmWork vmWork = new VmWork(1l, 1l, 1l, "testhandler");
47+
VmWorkStart workInfo = new VmWorkStart(vmWork);
48+
Map<String, String> rawParams = new HashMap<>();
49+
rawParams.put(VirtualMachineProfile.Param.ConsiderLastHost.getName(), "rO0ABXQABHRydWU");
50+
rawParams.put(VirtualMachineProfile.Param.VmPassword.getName(), "rO0ABXQADnNhdmVkX3Bhc3N3b3Jk");
51+
workInfo.setRawParams(rawParams);
52+
String expectedVmWorkStartStr = "{\"accountId\":1,\"dcId\":0,\"vmId\":1,\"handlerName\":\"testhandler\",\"userId\":1,\"rawParams\":{\"ConsiderLastHost\":\"rO0ABXQABHRydWU\"}}";
53+
54+
String vmWorkStartStr = workInfo.toString();
55+
Assert.assertEquals(expectedVmWorkStartStr, vmWorkStartStr);
56+
}
57+
}

0 commit comments

Comments
 (0)