Skip to content

Commit 330571c

Browse files
Sakshamsdevdeep
authored andcommitted
CLOUDSTACK-5692: obscure passwords when using cifs as storage
1 parent 04492ba commit 330571c

6 files changed

Lines changed: 108 additions & 20 deletions

File tree

‎core/src/com/cloud/agent/transport/Request.java‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.lang.reflect.Type;
2424
import java.nio.ByteBuffer;
2525
import java.util.ArrayList;
26+
import java.util.Collections;
2627
import java.util.Iterator;
2728
import java.util.List;
2829
import java.util.zip.GZIPInputStream;
@@ -49,6 +50,7 @@
4950
import com.cloud.serializer.GsonHelper;
5051
import com.cloud.utils.NumbersUtil;
5152
import com.cloud.utils.Pair;
53+
import com.cloud.utils.StringUtils;
5254
import com.cloud.utils.exception.CloudRuntimeException;
5355

5456
/**
@@ -439,11 +441,35 @@ protected String log(String msg, boolean logContent, Level level) {
439441
}
440442
buf.append(", Ver: ").append(_ver.toString());
441443
buf.append(", Flags: ").append(Integer.toBinaryString(getFlags())).append(", ");
442-
buf.append(content);
444+
String cleanContent = content.toString();
445+
if(cleanContent.contains("password")) {
446+
buf.append(cleanPassword(cleanContent));
447+
} else {
448+
buf.append(content);
449+
}
443450
buf.append(" }");
444451
return buf.toString();
445452
}
446453

454+
public static String cleanPassword(String logString) {
455+
String cleanLogString = null;
456+
if (logString != null) {
457+
cleanLogString = logString;
458+
String[] temp = logString.split(",");
459+
int i = 0;
460+
if (temp != null) {
461+
while (i < temp.length) {
462+
temp[i] = StringUtils.cleanString(temp[i]);
463+
i++;
464+
}
465+
List<String> stringList = new ArrayList<String>();
466+
Collections.addAll(stringList, temp);
467+
cleanLogString = StringUtils.join(stringList, ",");
468+
}
469+
}
470+
return cleanLogString;
471+
}
472+
447473
/**
448474
* Factory method for Request and Response. It expects the bytes to be
449475
* correctly formed so it's possible that it throws underflow exceptions

‎plugins/hypervisors/hyperv/src/com/cloud/hypervisor/hyperv/resource/HypervDirectConnectResource.java‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.security.UnrecoverableKeyException;
3232
import java.security.cert.CertificateException;
3333
import java.security.cert.X509Certificate;
34+
import java.util.ArrayList;
35+
import java.util.Collections;
3436
import java.util.HashMap;
3537
import java.util.List;
3638
import java.util.Map;
@@ -41,6 +43,7 @@
4143
import javax.inject.Inject;
4244
import javax.naming.ConfigurationException;
4345

46+
import org.apache.commons.lang.StringEscapeUtils;
4447
import org.apache.http.HttpResponse;
4548
import org.apache.http.HttpStatus;
4649
import org.apache.http.client.ClientProtocolException;
@@ -130,7 +133,6 @@
130133
import com.cloud.utils.ssh.SshHelper;
131134
import com.cloud.vm.VirtualMachine;
132135
import com.cloud.vm.VirtualMachineName;
133-
134136
/**
135137
* Implementation of dummy resource to be returned from discoverer.
136138
**/
@@ -472,8 +474,8 @@ public final Answer executeRequest(final Command cmd) {
472474
// Only Answer instances are returned by remote agents.
473475
// E.g. see Response.getAnswers()
474476
Answer[] result = s_gson.fromJson(ansStr, Answer[].class);
475-
s_logger.debug("executeRequest received response "
476-
+ s_gson.toJson(result));
477+
String logResult = cleanPassword(StringEscapeUtils.unescapeJava(result.toString()));
478+
s_logger.debug("executeRequest received response " + logResult);
477479
if (result.length > 0) {
478480
return result[0];
479481
}
@@ -1741,8 +1743,10 @@ public static String postHttpRequest(final String jsonCmd,
17411743
// comment to use Apache HttpClient
17421744
// http://stackoverflow.com/a/2793153/939250, but final comment is to
17431745
// use Apache.
1744-
s_logger.debug("POST request to" + agentUri.toString()
1745-
+ " with contents" + jsonCmd);
1746+
String logMessage = StringEscapeUtils.unescapeJava(jsonCmd);
1747+
logMessage = cleanPassword(logMessage);
1748+
s_logger.debug("POST request to " + agentUri.toString()
1749+
+ " with contents " + logMessage);
17461750

17471751
// Create request
17481752
HttpClient httpClient = null;
@@ -1783,7 +1787,7 @@ public boolean isTrusted(X509Certificate[] chain, String authType)
17831787
request.addHeader("content-type", "application/json");
17841788
request.setEntity(cmdJson);
17851789
s_logger.debug("Sending cmd to " + agentUri.toString()
1786-
+ " cmd data:" + jsonCmd);
1790+
+ " cmd data:" + logMessage);
17871791
HttpResponse response = httpClient.execute(request);
17881792

17891793
// Unsupported commands will not route.
@@ -1811,7 +1815,8 @@ public boolean isTrusted(X509Certificate[] chain, String authType)
18111815
return null;
18121816
} else {
18131817
result = EntityUtils.toString(response.getEntity());
1814-
s_logger.debug("POST response is" + result);
1818+
String logResult = cleanPassword(StringEscapeUtils.unescapeJava(result));
1819+
s_logger.debug("POST response is " + logResult);
18151820
}
18161821
} catch (ClientProtocolException protocolEx) {
18171822
// Problem with HTTP message exchange
@@ -1937,4 +1942,22 @@ protected String connect(final String vmName, final String ipAddress, final int
19371942
return "Unable to connect";
19381943
}
19391944

1945+
public static String cleanPassword(String logString) {
1946+
String cleanLogString = null;
1947+
if (logString != null) {
1948+
cleanLogString = logString;
1949+
String[] temp = logString.split(",");
1950+
int i = 0;
1951+
if (temp != null) {
1952+
while (i < temp.length) {
1953+
temp[i] = StringUtils.cleanString(temp[i]);
1954+
i++;
1955+
}
1956+
List<String> stringList = new ArrayList<String>();
1957+
Collections.addAll(stringList, temp);
1958+
cleanLogString = StringUtils.join(stringList, ",");
1959+
}
1960+
}
1961+
return cleanLogString;
1962+
}
19401963
}

‎plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/lifecycle/CloudStackImageStoreLifeCycleImpl.java‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.net.URI;
2020
import java.net.URISyntaxException;
21+
import java.util.ArrayList;
22+
import java.util.Collections;
2123
import java.util.HashMap;
2224
import java.util.List;
2325
import java.util.Map;
@@ -26,7 +28,7 @@
2628

2729
import org.apache.log4j.Logger;
2830

29-
import com.ibm.wsdl.util.StringUtils;
31+
import com.cloud.utils.StringUtils;
3032

3133
import org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope;
3234
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
@@ -86,7 +88,13 @@ public DataStore initialize(Map<String, Object> dsInfos) {
8688
DataStoreRole role = (DataStoreRole) dsInfos.get("role");
8789
Map<String, String> details = (Map<String, String>) dsInfos.get("details");
8890

89-
s_logger.info("Trying to add a new data store at " + StringUtils.cleanString(url) + " to data center " + dcId);
91+
String logString = "";
92+
if(url.contains("cifs")) {
93+
logString = cleanPassword(url);
94+
} else {
95+
logString = StringUtils.cleanString(url);
96+
}
97+
s_logger.info("Trying to add a new data store at " + logString + " to data center " + dcId);
9098

9199
URI uri = null;
92100
try {
@@ -172,4 +180,22 @@ public boolean migrateToObjectStore(DataStore store) {
172180
return imageStoreHelper.convertToStagingStore(store);
173181
}
174182

183+
public static String cleanPassword(String logString) {
184+
String cleanLogString = null;
185+
if (logString != null) {
186+
cleanLogString = logString;
187+
String[] temp = logString.split(",");
188+
int i = 0;
189+
if (temp != null) {
190+
while (i < temp.length) {
191+
temp[i] = StringUtils.cleanString(temp[i]);
192+
i++;
193+
}
194+
List<String> stringList = new ArrayList<String>();
195+
Collections.addAll(stringList, temp);
196+
cleanLogString = StringUtils.join(stringList, ",");
197+
}
198+
}
199+
return cleanLogString;
200+
}
175201
}

‎server/src/com/cloud/api/query/dao/ImageStoreJoinDaoImpl.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,16 @@
2525
import org.apache.cloudstack.api.response.ImageStoreDetailResponse;
2626
import org.apache.cloudstack.api.response.ImageStoreResponse;
2727
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
28-
2928
import org.apache.log4j.Logger;
3029
import org.springframework.stereotype.Component;
3130

31+
import com.cloud.utils.StringUtils;
3232
import com.cloud.api.query.vo.ImageStoreJoinVO;
3333
import com.cloud.storage.ImageStore;
3434
import com.cloud.utils.db.GenericDaoBase;
3535
import com.cloud.utils.db.SearchBuilder;
3636
import com.cloud.utils.db.SearchCriteria;
3737

38-
3938
@Component
4039
@Local(value={ImageStoreJoinDao.class})
4140
public class ImageStoreJoinDaoImpl extends GenericDaoBase<ImageStoreJoinVO, Long> implements ImageStoreJoinDao {
@@ -73,7 +72,12 @@ public ImageStoreResponse newImageStoreResponse(ImageStoreJoinVO ids) {
7372
osResponse.setName(ids.getName());
7473
osResponse.setProviderName(ids.getProviderName());
7574
osResponse.setProtocol(ids.getProtocol());
76-
osResponse.setUrl(ids.getUrl());
75+
String url = ids.getUrl();
76+
//if store is type cifs, remove the password
77+
if(ids.getProtocol().equals("cifs".toString())) {
78+
url = StringUtils.cleanString(url);
79+
}
80+
osResponse.setUrl(url);
7781
osResponse.setScope(ids.getScope());
7882
osResponse.setZoneId(ids.getZoneUuid());
7983
osResponse.setZoneName(ids.getZoneName());

‎server/src/com/cloud/api/query/dao/StoragePoolJoinDaoImpl.java‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@
2222
import javax.ejb.Local;
2323
import javax.inject.Inject;
2424

25-
import org.apache.cloudstack.api.response.StoragePoolResponse;
26-
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
27-
2825
import org.apache.log4j.Logger;
2926
import org.springframework.stereotype.Component;
3027

28+
import org.apache.cloudstack.api.response.StoragePoolResponse;
29+
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
30+
3131
import com.cloud.api.ApiDBUtils;
3232
import com.cloud.api.query.vo.StoragePoolJoinVO;
3333
import com.cloud.capacity.Capacity;
3434
import com.cloud.storage.ScopeType;
3535
import com.cloud.storage.StoragePool;
3636
import com.cloud.storage.StorageStats;
37+
import com.cloud.utils.StringUtils;
3738
import com.cloud.utils.db.GenericDaoBase;
3839
import com.cloud.utils.db.SearchBuilder;
3940
import com.cloud.utils.db.SearchCriteria;
@@ -60,7 +61,7 @@ protected StoragePoolJoinDaoImpl() {
6061
spIdSearch.and("id", spIdSearch.entity().getId(), SearchCriteria.Op.EQ);
6162
spIdSearch.done();
6263

63-
this._count = "select count(distinct id) from storage_pool_view WHERE ";
64+
_count = "select count(distinct id) from storage_pool_view WHERE ";
6465
}
6566

6667
@Override
@@ -69,7 +70,10 @@ public StoragePoolResponse newStoragePoolResponse(StoragePoolJoinVO pool) {
6970
poolResponse.setId(pool.getUuid());
7071
poolResponse.setName(pool.getName());
7172
poolResponse.setState(pool.getStatus());
72-
poolResponse.setPath(pool.getPath());
73+
String path = pool.getPath();
74+
//cifs store may contain password entry, remove the password
75+
path = StringUtils.cleanString(path);
76+
poolResponse.setPath(path);
7377
poolResponse.setIpAddress(pool.getHostAddress());
7478
poolResponse.setZoneId(pool.getZoneUuid());
7579
poolResponse.setZoneName(pool.getZoneName());
@@ -129,7 +133,10 @@ public StoragePoolResponse newStoragePoolForMigrationResponse(StoragePoolJoinVO
129133
poolResponse.setId(pool.getUuid());
130134
poolResponse.setName(pool.getName());
131135
poolResponse.setState(pool.getStatus());
132-
poolResponse.setPath(pool.getPath());
136+
String path = pool.getPath();
137+
//cifs store may contain password entry, remove the password
138+
path = StringUtils.cleanString(path);
139+
poolResponse.setPath(path);
133140
poolResponse.setIpAddress(pool.getHostAddress());
134141
poolResponse.setZoneId(pool.getZoneUuid());
135142
poolResponse.setZoneName(pool.getZoneName());

‎ui/scripts/sharedFunctions.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,9 @@ var processPropertiesInImagestoreObject = function(jsonObj) {
12011201
var url = jsonObj.url; //e.g. 'cifs://10.1.1.1/aaa/aaa2/aaa3?user=bbb&password=ccc&domain=ddd'
12021202
var passwordIndex = url.indexOf('&password='); //38
12031203
var domainIndex = url.indexOf('&domain='); //51
1204-
jsonObj.url = url.substring(0, passwordIndex) + url.substring(domainIndex); //remove '&password=ccc' from jsonObj.url
1204+
if (passwordIndex >= 0) {
1205+
jsonObj.url = url.substring(0, passwordIndex) + url.substring(domainIndex); //remove '&password=ccc' from jsonObj.url
1206+
}
12051207
}
12061208
}
12071209

0 commit comments

Comments
 (0)