Skip to content

Commit 697e12f

Browse files
mlsorensenMarcus Sorensen
andauthored
kvm: volume encryption feature (#6522)
This PR introduces a feature designed to allow CloudStack to manage a generic volume encryption setting. The encryption is handled transparently to the guest OS, and is intended to handle VM guest data encryption at rest and possibly over the wire, though the actual encryption implementation is up to the primary storage driver. In some cases cloud customers may still prefer to maintain their own guest-level volume encryption, if they don't trust the cloud provider. However, for private cloud cases this greatly simplifies the guest OS experience in terms of running volume encryption for guests without the user having to manage keys, deal with key servers and guest booting being dependent on network connectivity to them (i.e. Tang), etc, especially in cases where users are attaching/detaching data disks and moving them between VMs occasionally. The feature can be thought of as having two parts - the API/control plane (which includes scheduling aspects), and the storage driver implementation. This initial PR adds the encryption setting to disk offerings and service offerings (for root volume), and implements encryption support for KVM SharedMountPoint, NFS, Local, and ScaleIO storage pools. NOTE: While not required, operations can be significantly sped up by ensuring that hosts have the `rng-tools` package and service installed and running on the management server and hypervisors. For EL hosts the service is `rngd` and for Debian it is `rng-tools`. In particular, the use of SecureRandom for generating volume passphrases can be slow if there isn't a good source of entropy. This could affect testing and build environments, and otherwise would only affect users who actually use the encryption feature. If you find tests or volume creates blocking on encryption, check this first. ### Management Server ##### API * createDiskOffering now has an 'encrypt' Boolean * createServiceOffering now has an 'encryptroot' Boolean. The 'root' suffix is added here in case there is ever any other need to encrypt something related to the guest configuration, like the RAM of a VM. This has been refactored to deal with the new separation of service offering from disk offering internally. * listDiskOfferings shows encryption support on each offering, and has an encrypt boolean to choose to list only offerings that do or do not support encryption * listServiceOfferings shows encryption support on each offering, and has an encrypt boolean to choose to list only offerings that do or do not support encryption * listHosts now shows encryption support of each hypervisor host via `encryptionsupported` * Volumes themselves don't show encryption on/off, rather the offering should be referenced. This follows the same pattern as other disk offering based settings such as the IOPS of the volume. ##### Volume functions A decent effort has been made to ensure that the most common volume functions have either been cleanly supported or blocked. However, for the first release it is advised to mark this feature as *experimental*, as the code base is complex and there are certainly edge cases to be found. Many of these features could eventually be supported over time, such as creating templates from encrypted volumes, but the effort and size of the change is already overwhelming. Supported functions: * Data Volume create * VM root volume create * VM root volume reinstall * Offline volume snapshot/restore * Migration of VM with storage (e.g. local storage VM migration) * Resize volume * Detach/attach volume Blocked functions: * Online volume snapshot * VM snapshot w/memory * Scheduled snapshots (would fail when VM is running) * Disk offering migration to offerings that don't have matching encryption * Creating template from encrypted volume * Creating volume from encrypted volume * Volume extraction (would we decrypt it first, or expose the key? Probably the former). ##### Primary Storage Support For storage developers, adding encryption support involves: 1. Updating the `StoragePoolType` for your primary storage to advertise encryption support. This is used during allocation of storage to match storage types that support encryption to storage that supports it. 2. Implementing encryption feature when your `PrimaryDataStoreDriver` is called to perform volume lifecycle functions on volumes that are requesting encryption. You are free to do what your storage supports - this could be as simple as calling a storage API with the right flag when creating a volume. Or (as is the case with the KVM storage types), as complex as managing volume details directly at the hypervisor host. The data objects passed to the storage driver will contain volume passphrases, if encryption is requested. ##### Scheduling For the KVM implementations specified above, we are dependent on the KVM hosts having support for volume encryption tools. As such, the hosts `StartupRoutingCommand` has been modified to advertise whether the host supports encryption. This is done via a probe during agent startup to look for functioning `cryptsetup` and support in `qemu-img`. This is also visible via the listHosts API and the host details in the UI. This was patterned after other features that require hypervisor support such as UEFI. The `EndPointSelector` interface and `DefaultEndpointSelector` have had new methods added, which allow the caller to ask for endpoints that support encryption. This can be used by storage drivers to find the proper hosts to send storage commands that involve encryption. Not all volume activities will require a host to support encryption (for example a snapshot backup is a simple file copy), and this is the reason why the interface has been modified to allow for the storage driver to decide, rather than just passing the data objects to the EndpointSelector and letting the implementation decide. VM scheduling has also been modified. When a VM start is requested, if any volume that requires encryption is attached, it will filter out hosts that don't support encryption. ##### DB Changes A volume whose disk offering enables encryption will get a passphrase generated for it before its first use. This is stored in the new 'passphrase' table, and is encrypted using the CloudStack installation's standard configured DB encryption. A field has been added to the volumes table, referencing this passphrase, and a foreign key added to ensure passphrases that are referenced can't be removed from the database. The volumes table now also contains an encryption format field, which is set by the implementer of the encryption and used as it sees fit. #### KVM Agent For the KVM storage pool types supported, the encryption has been implemented at Qemu itself, using the built-in LUKS storage support. This means that the storage remains encrypted all the way to the VM process, and decrypted before the block device is visible to the guest. This may not be necessary in order to implement encryption for /your/ storage pool type, maybe you have a kernel driver that decrypts before the block device on the system, or something like that. However, it seemed like the simplest, common place to terminate the encryption, and provides the lowest surface area for decrypted guest data. For qcow2 based storage, `qemu-img` is used to set up a qcow2 file with LUKS encryption. For block based (currently just ScaleIO storage), the `cryptsetup` utility is used to format the block device as LUKS for data disks, but `qemu-img` and its LUKS support is used for template copy. Any volume that requires encryption will contain a passphrase ID as a byte array when handed down to the KVM agent. Care has been taken to ensure this doesn't get logged, and it is cleared after use in attempt to avoid exposing it before garbage collection occurs. On the agent side, this passphrase is used in two ways: 1. In cases where the volume experiences some libvirt interaction it is loaded into libvirt as an ephemeral, private secret and then referenced by secret UUID in any libvirt XML. This applies to things like VM startup, migration preparation, etc. 2. In cases where `qemu-img` needs to use this passphrase for volume operations, it is written to a `KeyFile` on the cloudstack agent's configured tmpfs and passed along. The `KeyFile` is a `Closeable` and when it is closed, it is deleted. This allows us to try-with-resources any volume operations and get the KeyFile removed regardless. In order to support the advanced syntax required to handle encryption and passphrases with `qemu-img`, the `QemuImg` utility has been modified to support the new `--object` and `--image-opts` flags. These are modeled as `QemuObject` and `QemuImageOptions`. These `qemu-img` flags have been designed to supersede some of the existing, older flags being used today (such as choosing file formats and paths), and an effort could be made to switch over to these wholesale. However, for now we have instead opted to keep existing functions and do some wrapping to ensure backward compatibility, so callers of `QemuImg` can choose to use either way. It should be noted that there are also a few different Enums that represent the encryption format for various purposes. While these are analogous in principle, they represent different things and should not be confused. For example, the supported encryption format strings for the `cryptsetup` utility has `LuksType.LUKS` while `QemuImg` has a `QemuImg.PhysicalDiskFormat.LUKS`. Some additional effort could potentially be made to support advanced encryption configurations, such as choosing between LUKS1 and LUKS2 or changing cipher details. These may require changes all the way up through the control plane. However, in practice Libvirt and Qemu currently only support LUKS1 today. Additionally, the cipher details aren't required in order to use an encrypted volume, as they're stored in the LUKS header on the volume there is no need to store these elsewhere. As such, we need only set the one encryption format upon volume creation, which is persisted in the volumes table and then available later as needed. In the future when LUKS2 is standard and fully supported, we could move to it as the default and old volumes will still reference LUKS1 and have the headers on-disk to ensure they remain usable. We could also possibly support an automatic upgrade of the headers down the road, or a volume migration mechanism. Every version of cryptsetup and qemu-img tested on variants of EL7 and Ubuntu that support encryption use the XTS-AES 256 cipher, which is the leading industry standard and widely used cipher today (e.g. BitLocker and FileVault). Signed-off-by: Marcus Sorensen <mls@apple.com> Co-authored-by: Marcus Sorensen <mls@apple.com>
1 parent d4c6586 commit 697e12f

File tree

114 files changed

+4329
-434
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+4329
-434
lines changed

api/src/main/java/com/cloud/agent/api/to/DiskTO.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class DiskTO {
4040
public static final String VMDK = "vmdk";
4141
public static final String EXPAND_DATASTORE = "expandDatastore";
4242
public static final String TEMPLATE_RESIGN = "templateResign";
43+
public static final String SECRET_CONSUMER_DETAIL = "storageMigrateSecretConsumer";
4344

4445
private DataTO data;
4546
private Long diskSeq;

api/src/main/java/com/cloud/agent/api/to/StorageFilerTO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717
package com.cloud.agent.api.to;
1818

19+
import com.cloud.agent.api.LogLevel;
1920
import com.cloud.storage.Storage.StoragePoolType;
2021
import com.cloud.storage.StoragePool;
2122

@@ -24,6 +25,7 @@ public class StorageFilerTO {
2425
String uuid;
2526
String host;
2627
String path;
28+
@LogLevel(LogLevel.Log4jLevel.Off)
2729
String userInfo;
2830
int port;
2931
StoragePoolType type;

api/src/main/java/com/cloud/host/Host.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static String[] toStrings(Host.Type... types) {
5353
}
5454
}
5555
public static final String HOST_UEFI_ENABLE = "host.uefi.enable";
56+
public static final String HOST_VOLUME_ENCRYPTION = "host.volume.encryption";
5657

5758
/**
5859
* @return name of the machine.

api/src/main/java/com/cloud/offering/DiskOffering.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,8 @@ public String toString() {
149149
boolean isComputeOnly();
150150

151151
boolean getDiskSizeStrictness();
152+
153+
boolean getEncrypt();
154+
155+
void setEncrypt(boolean encrypt);
152156
}

api/src/main/java/com/cloud/storage/MigrationOptions.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class MigrationOptions implements Serializable {
2525
private String srcPoolUuid;
2626
private Storage.StoragePoolType srcPoolType;
2727
private Type type;
28+
private ScopeType scopeType;
2829
private String srcBackingFilePath;
2930
private boolean copySrcTemplate;
3031
private String srcVolumeUuid;
@@ -37,18 +38,20 @@ public enum Type {
3738
public MigrationOptions() {
3839
}
3940

40-
public MigrationOptions(String srcPoolUuid, Storage.StoragePoolType srcPoolType, String srcBackingFilePath, boolean copySrcTemplate) {
41+
public MigrationOptions(String srcPoolUuid, Storage.StoragePoolType srcPoolType, String srcBackingFilePath, boolean copySrcTemplate, ScopeType scopeType) {
4142
this.srcPoolUuid = srcPoolUuid;
4243
this.srcPoolType = srcPoolType;
4344
this.type = Type.LinkedClone;
45+
this.scopeType = scopeType;
4446
this.srcBackingFilePath = srcBackingFilePath;
4547
this.copySrcTemplate = copySrcTemplate;
4648
}
4749

48-
public MigrationOptions(String srcPoolUuid, Storage.StoragePoolType srcPoolType, String srcVolumeUuid) {
50+
public MigrationOptions(String srcPoolUuid, Storage.StoragePoolType srcPoolType, String srcVolumeUuid, ScopeType scopeType) {
4951
this.srcPoolUuid = srcPoolUuid;
5052
this.srcPoolType = srcPoolType;
5153
this.type = Type.FullClone;
54+
this.scopeType = scopeType;
5255
this.srcVolumeUuid = srcVolumeUuid;
5356
}
5457

@@ -60,6 +63,8 @@ public Storage.StoragePoolType getSrcPoolType() {
6063
return srcPoolType;
6164
}
6265

66+
public ScopeType getScopeType() { return scopeType; }
67+
6368
public String getSrcBackingFilePath() {
6469
return srcBackingFilePath;
6570
}

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,33 +130,35 @@ public static enum TemplateType {
130130
}
131131

132132
public static enum StoragePoolType {
133-
Filesystem(false, true), // local directory
134-
NetworkFilesystem(true, true), // NFS
135-
IscsiLUN(true, false), // shared LUN, with a clusterfs overlay
136-
Iscsi(true, false), // for e.g., ZFS Comstar
137-
ISO(false, false), // for iso image
138-
LVM(false, false), // XenServer local LVM SR
139-
CLVM(true, false),
140-
RBD(true, true), // http://libvirt.org/storage.html#StorageBackendRBD
141-
SharedMountPoint(true, false),
142-
VMFS(true, true), // VMware VMFS storage
143-
PreSetup(true, true), // for XenServer, Storage Pool is set up by customers.
144-
EXT(false, true), // XenServer local EXT SR
145-
OCFS2(true, false),
146-
SMB(true, false),
147-
Gluster(true, false),
148-
PowerFlex(true, true), // Dell EMC PowerFlex/ScaleIO (formerly VxFlexOS)
149-
ManagedNFS(true, false),
150-
Linstor(true, true),
151-
DatastoreCluster(true, true), // for VMware, to abstract pool of clusters
152-
StorPool(true, true);
133+
Filesystem(false, true, true), // local directory
134+
NetworkFilesystem(true, true, true), // NFS
135+
IscsiLUN(true, false, false), // shared LUN, with a clusterfs overlay
136+
Iscsi(true, false, false), // for e.g., ZFS Comstar
137+
ISO(false, false, false), // for iso image
138+
LVM(false, false, false), // XenServer local LVM SR
139+
CLVM(true, false, false),
140+
RBD(true, true, false), // http://libvirt.org/storage.html#StorageBackendRBD
141+
SharedMountPoint(true, false, true),
142+
VMFS(true, true, false), // VMware VMFS storage
143+
PreSetup(true, true, false), // for XenServer, Storage Pool is set up by customers.
144+
EXT(false, true, false), // XenServer local EXT SR
145+
OCFS2(true, false, false),
146+
SMB(true, false, false),
147+
Gluster(true, false, false),
148+
PowerFlex(true, true, true), // Dell EMC PowerFlex/ScaleIO (formerly VxFlexOS)
149+
ManagedNFS(true, false, false),
150+
Linstor(true, true, false),
151+
DatastoreCluster(true, true, false), // for VMware, to abstract pool of clusters
152+
StorPool(true, true, false);
153153

154154
private final boolean shared;
155155
private final boolean overprovisioning;
156+
private final boolean encryption;
156157

157-
StoragePoolType(boolean shared, boolean overprovisioning) {
158+
StoragePoolType(boolean shared, boolean overprovisioning, boolean encryption) {
158159
this.shared = shared;
159160
this.overprovisioning = overprovisioning;
161+
this.encryption = encryption;
160162
}
161163

162164
public boolean isShared() {
@@ -166,6 +168,8 @@ public boolean isShared() {
166168
public boolean supportsOverProvisioning() {
167169
return overprovisioning;
168170
}
171+
172+
public boolean supportsEncryption() { return encryption; }
169173
}
170174

171175
public static List<StoragePoolType> getNonSharedStoragePoolTypes() {

api/src/main/java/com/cloud/storage/Volume.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,12 @@ enum Event {
247247
String getExternalUuid();
248248

249249
void setExternalUuid(String externalUuid);
250+
251+
public Long getPassphraseId();
252+
253+
public void setPassphraseId(Long id);
254+
255+
public String getEncryptFormat();
256+
257+
public void setEncryptFormat(String encryptFormat);
250258
}

api/src/main/java/com/cloud/vm/DiskProfile.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class DiskProfile {
4444
private String cacheMode;
4545
private Long minIops;
4646
private Long maxIops;
47+
private boolean requiresEncryption;
4748

4849
private HypervisorType hyperType;
4950

@@ -63,6 +64,12 @@ public DiskProfile(long volumeId, Volume.Type type, String name, long diskOfferi
6364
this.volumeId = volumeId;
6465
}
6566

67+
public DiskProfile(long volumeId, Volume.Type type, String name, long diskOfferingId, long size, String[] tags, boolean useLocalStorage, boolean recreatable,
68+
Long templateId, boolean requiresEncryption) {
69+
this(volumeId, type, name, diskOfferingId, size, tags, useLocalStorage, recreatable, templateId);
70+
this.requiresEncryption = requiresEncryption;
71+
}
72+
6673
public DiskProfile(Volume vol, DiskOffering offering, HypervisorType hyperType) {
6774
this(vol.getId(),
6875
vol.getVolumeType(),
@@ -75,6 +82,7 @@ public DiskProfile(Volume vol, DiskOffering offering, HypervisorType hyperType)
7582
null);
7683
this.hyperType = hyperType;
7784
this.provisioningType = offering.getProvisioningType();
85+
this.requiresEncryption = offering.getEncrypt() || vol.getPassphraseId() != null;
7886
}
7987

8088
public DiskProfile(DiskProfile dp) {
@@ -230,7 +238,6 @@ public String getCacheMode() {
230238
return cacheMode;
231239
}
232240

233-
234241
public Long getMinIops() {
235242
return minIops;
236243
}
@@ -247,4 +254,7 @@ public void setMaxIops(Long maxIops) {
247254
this.maxIops = maxIops;
248255
}
249256

257+
public boolean requiresEncryption() { return requiresEncryption; }
258+
259+
public void setEncryption(boolean encrypt) { this.requiresEncryption = encrypt; }
250260
}

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ public class ApiConstants {
109109
public static final String CUSTOM_JOB_ID = "customjobid";
110110
public static final String CURRENT_START_IP = "currentstartip";
111111
public static final String CURRENT_END_IP = "currentendip";
112+
public static final String ENCRYPT = "encrypt";
113+
public static final String ENCRYPT_ROOT = "encryptroot";
114+
public static final String ENCRYPTION_SUPPORTED = "encryptionsupported";
112115
public static final String MIN_IOPS = "miniops";
113116
public static final String MAX_IOPS = "maxiops";
114117
public static final String HYPERVISOR_SNAPSHOT_RESERVE = "hypervisorsnapshotreserve";

api/src/main/java/org/apache/cloudstack/api/command/admin/offering/CreateDiskOfferingCmd.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,14 @@ public class CreateDiskOfferingCmd extends BaseCmd {
163163
@Parameter(name = ApiConstants.DISK_SIZE_STRICTNESS, type = CommandType.BOOLEAN, description = "To allow or disallow the resize operation on the disks created from this disk offering, if the flag is true then resize is not allowed", since = "4.17")
164164
private Boolean diskSizeStrictness;
165165

166+
@Parameter(name = ApiConstants.ENCRYPT, type = CommandType.BOOLEAN, required=false, description = "Volumes using this offering should be encrypted", since = "4.18")
167+
private Boolean encrypt;
168+
166169
@Parameter(name = ApiConstants.DETAILS, type = CommandType.MAP, description = "details to specify disk offering parameters", since = "4.16")
167170
private Map details;
168171

172+
173+
169174
/////////////////////////////////////////////////////
170175
/////////////////// Accessors ///////////////////////
171176
/////////////////////////////////////////////////////
@@ -202,6 +207,13 @@ public Long getMaxIops() {
202207
return maxIops;
203208
}
204209

210+
public boolean getEncrypt() {
211+
if (encrypt == null) {
212+
return false;
213+
}
214+
return encrypt;
215+
}
216+
205217
public List<Long> getDomainIds() {
206218
if (CollectionUtils.isNotEmpty(domainIds)) {
207219
Set<Long> set = new LinkedHashSet<>(domainIds);

0 commit comments

Comments
 (0)