Skip to content

Commit 8592de9

Browse files
weizhouapacheMarcus Sorensen
andauthored
Move PassphraseVO to use String instead of byte[] to support Encrypt annotation (#7302)
Co-authored-by: Marcus Sorensen <mls@apple.com>
1 parent cb26721 commit 8592de9

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,7 @@ public VolumeVO allocateDuplicateVolumeVO(Volume oldVol, Long templateId) {
319319
newVol.setFormat(oldVol.getFormat());
320320

321321
if (oldVol.getPassphraseId() != null) {
322-
PassphraseVO passphrase = passphraseDao.persist(new PassphraseVO());
323-
passphrase.clearPassphrase();
322+
PassphraseVO passphrase = passphraseDao.persist(new PassphraseVO(true));
324323
newVol.setPassphraseId(passphrase.getId());
325324
}
326325

@@ -1801,8 +1800,7 @@ private VolumeVO setPassphraseForVolumeEncryption(VolumeVO volume) {
18011800
}
18021801
s_logger.debug("Creating passphrase for the volume: " + volume.getName());
18031802
long startTime = System.currentTimeMillis();
1804-
PassphraseVO passphrase = passphraseDao.persist(new PassphraseVO());
1805-
passphrase.clearPassphrase();
1803+
PassphraseVO passphrase = passphraseDao.persist(new PassphraseVO(true));
18061804
volume.setPassphraseId(passphrase.getId());
18071805
long finishTime = System.currentTimeMillis();
18081806
s_logger.debug("Creating and persisting passphrase took: " + (finishTime - startTime) + " ms for the volume: " + volume.toString());

engine/schema/src/main/java/org/apache/cloudstack/secret/PassphraseVO.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
import com.cloud.utils.db.Encrypt;
2222
import com.cloud.utils.exception.CloudRuntimeException;
23+
import org.apache.commons.lang3.StringUtils;
2324

2425
import javax.persistence.Column;
2526
import javax.persistence.Entity;
2627
import javax.persistence.GeneratedValue;
2728
import javax.persistence.GenerationType;
2829
import javax.persistence.Id;
2930
import javax.persistence.Table;
31+
3032
import java.security.NoSuchAlgorithmException;
3133
import java.security.SecureRandom;
3234
import java.util.Arrays;
@@ -42,32 +44,39 @@ public class PassphraseVO {
4244

4345
@Column(name = "passphrase")
4446
@Encrypt
45-
private byte[] passphrase;
47+
private String passphrase;
4648

4749
public PassphraseVO() {
48-
try {
49-
SecureRandom random = SecureRandom.getInstanceStrong();
50-
byte[] temporary = new byte[48]; // 48 byte random passphrase buffer
51-
this.passphrase = new byte[64]; // 48 byte random passphrase as base64 for usability
52-
random.nextBytes(temporary);
53-
Base64.getEncoder().encode(temporary, this.passphrase);
54-
Arrays.fill(temporary, (byte) 0); // clear passphrase from buffer
55-
} catch (NoSuchAlgorithmException ex ) {
56-
throw new CloudRuntimeException("Volume encryption requested but system is missing specified algorithm to generate passphrase");
50+
}
51+
52+
public PassphraseVO(boolean initialize) {
53+
if (initialize) {
54+
try {
55+
SecureRandom random = SecureRandom.getInstanceStrong();
56+
byte[] temporary = new byte[48]; // 48 byte random passphrase buffer
57+
random.nextBytes(temporary);
58+
this.passphrase = Base64.getEncoder().encodeToString(temporary);
59+
Arrays.fill(temporary, (byte) 0); // clear passphrase from buffer
60+
} catch (NoSuchAlgorithmException ex ) {
61+
throw new CloudRuntimeException("Volume encryption requested but system is missing specified algorithm to generate passphrase");
62+
}
5763
}
5864
}
5965

6066
public PassphraseVO(PassphraseVO existing) {
61-
this.passphrase = existing.getPassphrase();
67+
this.passphrase = existing.getPassphraseString();
6268
}
6369

64-
public void clearPassphrase() {
65-
if (this.passphrase != null) {
66-
Arrays.fill(this.passphrase, (byte) 0);
70+
public byte[] getPassphrase() {
71+
if (StringUtils.isBlank(this.passphrase)) {
72+
return new byte[]{};
6773
}
74+
return this.passphrase.getBytes();
6875
}
6976

70-
public byte[] getPassphrase() { return this.passphrase; }
77+
public String getPassphraseString() {
78+
return this.passphrase;
79+
}
7180

7281
public Long getId() { return this.id; }
7382
}

plugins/hypervisors/kvm/src/test/java/org/apache/cloudstack/utils/cryptsetup/CryptSetupTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void cryptSetupTest() throws IOException, CryptSetupException {
5151
file.close();
5252

5353
String filePath = path.toAbsolutePath().toString();
54-
PassphraseVO passphrase = new PassphraseVO();
54+
PassphraseVO passphrase = new PassphraseVO(true);
5555

5656
cryptSetup.luksFormat(passphrase.getPassphrase(), CryptSetup.LuksType.LUKS, filePath);
5757

0 commit comments

Comments
 (0)