Skip to content

Commit 9536569

Browse files
devdeepchaturvedia
authored andcommitted
CLOUDSTACK-6030: Encrypt the primary and secondary smb storage password when it is stored in the db.
(cherry picked from commit 96d8e3c) Signed-off-by: Animesh Chaturvedi <animesh@apache.org>
1 parent 74c98a1 commit 9536569

8 files changed

Lines changed: 102 additions & 18 deletions

File tree

‎api/src/com/cloud/storage/Storage.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static enum TemplateType {
9595

9696
public static enum StoragePoolType {
9797
Filesystem(false), // local directory
98-
NetworkFilesystem(true), // NFS or CIFS
98+
NetworkFilesystem(true), // NFS
9999
IscsiLUN(true), // shared LUN, with a clusterfs overlay
100100
Iscsi(true), // for e.g., ZFS Comstar
101101
ISO(false), // for iso image
@@ -106,7 +106,8 @@ public static enum StoragePoolType {
106106
VMFS(true), // VMware VMFS storage
107107
PreSetup(true), // for XenServer, Storage Pool is set up by customers.
108108
EXT(false), // XenServer local EXT SR
109-
OCFS2(true);
109+
OCFS2(true),
110+
SMB(true);
110111

111112
boolean shared;
112113

‎engine/schema/src/org/apache/cloudstack/storage/datastore/db/ImageStoreVO.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.cloud.storage.DataStoreRole;
3232
import com.cloud.storage.ImageStore;
3333
import com.cloud.storage.ScopeType;
34+
import com.cloud.utils.UriUtils;
3435
import com.cloud.utils.db.GenericDao;
3536

3637
@Entity
@@ -144,11 +145,18 @@ public String getUuid() {
144145
}
145146

146147
public String getUrl() {
147-
return url;
148+
String updatedUrl = url;
149+
if ("cifs".equalsIgnoreCase(this.protocol)) {
150+
updatedUrl = UriUtils.getUpdateUri(updatedUrl, false);
151+
}
152+
return updatedUrl;
148153
}
149154

150155
public void setUrl(String url) {
151156
this.url = url;
157+
if ("cifs".equalsIgnoreCase(this.protocol)) {
158+
this.url = UriUtils.getUpdateUri(url, true);
159+
}
152160
}
153161

154162
public Date getCreated() {

‎engine/schema/src/org/apache/cloudstack/storage/datastore/db/StoragePoolVO.java‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
import javax.persistence.TemporalType;
3131

3232
import com.cloud.hypervisor.Hypervisor.HypervisorType;
33-
import com.cloud.storage.Storage.StoragePoolType;
3433
import com.cloud.storage.ScopeType;
34+
import com.cloud.storage.Storage.StoragePoolType;
3535
import com.cloud.storage.StoragePool;
3636
import com.cloud.storage.StoragePoolStatus;
37+
import com.cloud.utils.UriUtils;
3738
import com.cloud.utils.db.GenericDao;
3839

3940
@Entity
@@ -135,10 +136,10 @@ public StoragePoolVO(long poolId, String name, String uuid, StoragePoolType type
135136
this.usedBytes = availableBytes;
136137
this.capacityBytes = capacityBytes;
137138
this.hostAddress = hostAddress;
138-
this.path = hostPath;
139139
this.port = port;
140140
this.podId = podId;
141141
this.setStatus(StoragePoolStatus.Initial);
142+
this.setPath(hostPath);
142143
}
143144

144145
public StoragePoolVO(StoragePoolVO that) {
@@ -150,9 +151,9 @@ public StoragePoolVO(StoragePoolType type, String hostAddress, int port, String
150151
this.poolType = type;
151152
this.hostAddress = hostAddress;
152153
this.port = port;
153-
this.path = path;
154154
this.setStatus(StoragePoolStatus.Initial);
155155
this.uuid = UUID.randomUUID().toString();
156+
this.setPath(path);
156157
}
157158

158159
public String getName() {
@@ -244,7 +245,12 @@ public void setHostAddress(String host) {
244245
}
245246

246247
public String getPath() {
247-
return path;
248+
String updatedPath = path;
249+
if (this.poolType == StoragePoolType.SMB) {
250+
updatedPath = UriUtils.getUpdateUri(updatedPath, false);
251+
}
252+
253+
return updatedPath;
248254
}
249255

250256
public String getUserInfo() {
@@ -273,6 +279,9 @@ public void setUuid(String uuid) {
273279

274280
public void setPath(String path) {
275281
this.path = path;
282+
if (this.poolType == StoragePoolType.SMB) {
283+
this.path = UriUtils.getUpdateUri(this.path, true);
284+
}
276285
}
277286

278287
public void setUserInfo(String userInfo) {

‎engine/storage/src/org/apache/cloudstack/storage/volume/datastore/PrimaryDataStoreHelper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public DataStore createPrimaryDataStore(PrimaryDataStoreParameters params) {
6969
dataStoreVO = new StoragePoolVO();
7070
dataStoreVO.setStorageProviderName(params.getProviderName());
7171
dataStoreVO.setHostAddress(params.getHost());
72-
dataStoreVO.setPath(params.getPath());
7372
dataStoreVO.setPoolType(params.getType());
73+
dataStoreVO.setPath(params.getPath());
7474
dataStoreVO.setPort(params.getPort());
7575
dataStoreVO.setName(params.getName());
7676
dataStoreVO.setUuid(params.getUuid());

‎plugins/hypervisors/hyperv/DotNet/ServerResource/HypervResource/CloudStackTypes.cs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public string UncPath
6161
get
6262
{
6363
string uncPath = null;
64-
if (uri != null && (uri.Scheme.Equals("cifs") || uri.Scheme.Equals("networkfilesystem")))
64+
if (uri != null && (uri.Scheme.Equals("cifs") || uri.Scheme.Equals("networkfilesystem") || uri.Scheme.Equals("smb")))
6565
{
6666
uncPath = @"\\" + uri.Host + uri.LocalPath;
6767
}
@@ -584,7 +584,11 @@ public enum StoragePoolType
584584
/// <summary>
585585
///
586586
/// </summary>
587-
OCFS2
587+
OCFS2,
588+
/// <summary>
589+
/// for hyper-v
590+
/// </summary>
591+
SMB
588592
}
589593

590594
public enum StorageResourceType

‎plugins/hypervisors/hyperv/DotNet/ServerResource/HypervResource/HypervResourceController.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ public JContainer ModifyStoragePoolCommand([FromBody]dynamic cmd)
916916
GetCapacityForLocalPath(localPath, out capacityBytes, out availableBytes);
917917
hostPath = localPath;
918918
}
919-
else if (poolType == StoragePoolType.NetworkFilesystem)
919+
else if (poolType == StoragePoolType.NetworkFilesystem ||
920+
poolType == StoragePoolType.SMB)
920921
{
921922
NFSTO share = new NFSTO();
922923
String uriStr = "cifs://" + (string)cmd.pool.host + (string)cmd.pool.path;
@@ -972,7 +973,8 @@ private bool ValidateStoragePoolCommand(dynamic cmd, out string localPath, out S
972973
}
973974

974975
if (poolType != StoragePoolType.Filesystem &&
975-
poolType != StoragePoolType.NetworkFilesystem)
976+
poolType != StoragePoolType.NetworkFilesystem &&
977+
poolType != StoragePoolType.SMB)
976978
{
977979
details = "Request to create / modify unsupported pool type: " + (poolTypeStr == null ? "NULL" : poolTypeStr) + "in cmd " + JsonConvert.SerializeObject(cmd);
978980
logger.Error(details);
@@ -1715,7 +1717,7 @@ public JContainer GetStorageStatsCommand([FromBody]dynamic cmd)
17151717
used = capacity - available;
17161718
result = true;
17171719
}
1718-
else if (poolType == StoragePoolType.NetworkFilesystem)
1720+
else if (poolType == StoragePoolType.NetworkFilesystem || poolType == StoragePoolType.SMB)
17191721
{
17201722
string sharePath = config.getPrimaryStorage((string)cmd.id);
17211723
if (sharePath != null)

‎plugins/storage/volume/default/src/org/apache/cloudstack/storage/datastore/lifecycle/CloudStackPrimaryDataStoreLifeCycleImpl.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import javax.inject.Inject;
2929

3030
import org.apache.log4j.Logger;
31-
3231
import org.apache.cloudstack.engine.subsystem.api.storage.ClusterScope;
3332
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
3433
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
@@ -206,12 +205,11 @@ public DataStore initialize(Map<String, Object> dsInfos) {
206205
if (port == -1) {
207206
port = 445;
208207
}
209-
parameters.setType(StoragePoolType.NetworkFilesystem);
208+
209+
parameters.setType(StoragePoolType.SMB);
210210
parameters.setHost(storageHost);
211211
parameters.setPort(port);
212212
parameters.setPath(hostPath);
213-
parameters.setUserInfo(uri.getQuery());
214-
215213
} else if (scheme.equalsIgnoreCase("file")) {
216214
if (port == -1) {
217215
port = 0;
@@ -356,7 +354,8 @@ protected boolean createStoragePool(long hostId, StoragePool pool) {
356354
&& pool.getPoolType() != StoragePoolType.IscsiLUN && pool.getPoolType() != StoragePoolType.Iscsi
357355
&& pool.getPoolType() != StoragePoolType.VMFS && pool.getPoolType() != StoragePoolType.SharedMountPoint
358356
&& pool.getPoolType() != StoragePoolType.PreSetup && pool.getPoolType() != StoragePoolType.OCFS2
359-
&& pool.getPoolType() != StoragePoolType.RBD && pool.getPoolType() != StoragePoolType.CLVM) {
357+
&& pool.getPoolType() != StoragePoolType.RBD && pool.getPoolType() != StoragePoolType.CLVM
358+
&& pool.getPoolType() != StoragePoolType.SMB) {
360359
s_logger.warn(" Doesn't support storage pool type " + pool.getPoolType());
361360
return false;
362361
}

‎utils/src/com/cloud/utils/UriUtils.java‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
import java.net.URISyntaxException;
2727
import java.net.URLEncoder;
2828
import java.net.UnknownHostException;
29+
import java.util.ArrayList;
2930
import java.util.List;
31+
import java.util.ListIterator;
32+
import java.util.StringTokenizer;
3033

3134
import javax.net.ssl.HttpsURLConnection;
3235

@@ -37,10 +40,14 @@
3740
import org.apache.commons.httpclient.UsernamePasswordCredentials;
3841
import org.apache.commons.httpclient.auth.AuthScope;
3942
import org.apache.commons.httpclient.methods.GetMethod;
43+
import org.apache.commons.httpclient.util.URIUtil;
4044
import org.apache.http.NameValuePair;
45+
import org.apache.http.message.BasicNameValuePair;
46+
import org.apache.http.client.utils.URIBuilder;
4147
import org.apache.http.client.utils.URLEncodedUtils;
4248
import org.apache.log4j.Logger;
4349

50+
import com.cloud.utils.crypt.DBEncryptionUtil;
4451
import com.cloud.utils.exception.CloudRuntimeException;
4552

4653
public class UriUtils {
@@ -143,6 +150,60 @@ else if (name.equals("password")) {
143150
}
144151
return (foundUser && foundPswd);
145152
}
153+
154+
public static String getUpdateUri(String url, boolean encrypt) {
155+
String updatedPath = null;
156+
try {
157+
String query = URIUtil.getQuery(url);
158+
URIBuilder builder = new URIBuilder(url);
159+
builder.removeQuery();
160+
161+
String updatedQuery = new String();
162+
List<NameValuePair> queryParams = getUserDetails(query);
163+
ListIterator<NameValuePair> iterator = queryParams.listIterator();
164+
while (iterator.hasNext()) {
165+
NameValuePair param = iterator.next();
166+
String value = null;
167+
if ("password".equalsIgnoreCase(param.getName()) &&
168+
param.getValue() != null) {
169+
value = encrypt ? DBEncryptionUtil.encrypt(param.getValue()) : DBEncryptionUtil.decrypt(param.getValue());
170+
} else {
171+
value = param.getValue();
172+
}
173+
174+
if (updatedQuery.isEmpty()) {
175+
updatedQuery += (param.getName() + "=" + value);
176+
} else {
177+
updatedQuery += ("&" + param.getName() + "=" + value);
178+
}
179+
}
180+
181+
String schemeAndHost = new String();
182+
URI newUri = builder.build();
183+
if (newUri.getScheme() != null) {
184+
schemeAndHost = newUri.getScheme() + "://" + newUri.getHost();
185+
}
186+
187+
updatedPath = schemeAndHost + newUri.getPath() + "?" + updatedQuery;
188+
} catch (URISyntaxException e) {
189+
throw new CloudRuntimeException("Couldn't generate an updated uri. " + e.getMessage());
190+
}
191+
192+
return updatedPath;
193+
}
194+
195+
private static List<NameValuePair> getUserDetails(String query) {
196+
List<NameValuePair> details = new ArrayList<NameValuePair>();
197+
StringTokenizer allParams = new StringTokenizer(query, "&");
198+
while (allParams.hasMoreTokens()) {
199+
String param = allParams.nextToken();
200+
details.add(new BasicNameValuePair(param.substring(0, param.indexOf("=")),
201+
param.substring(param.indexOf("=") + 1)));
202+
}
203+
204+
return details;
205+
}
206+
146207
// Get the size of a file from URL response header.
147208
public static Long getRemoteSize(String url) {
148209
Long remoteSize = (long) 0;

0 commit comments

Comments
 (0)