Skip to content

Commit 685918e

Browse files
authored
HDDS-2227. GDPR key generation could benefit from secureRandom. (#1574)
1 parent ffd4e52 commit 685918e

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

hadoop-ozone/client/src/main/java/org/apache/hadoop/ozone/client/rpc/RpcClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
import java.io.IOException;
9595
import java.net.URI;
9696
import java.security.InvalidKeyException;
97+
import java.security.SecureRandom;
9798
import java.util.*;
9899
import java.util.concurrent.TimeUnit;
99100
import java.util.stream.Collectors;
@@ -587,7 +588,7 @@ public OzoneOutputStream createKey(
587588

588589
if(Boolean.valueOf(metadata.get(OzoneConsts.GDPR_FLAG))){
589590
try{
590-
GDPRSymmetricKey gKey = new GDPRSymmetricKey();
591+
GDPRSymmetricKey gKey = new GDPRSymmetricKey(new SecureRandom());
591592
metadata.putAll(gKey.getKeyDetails());
592593
}catch (Exception e) {
593594
if(e instanceof InvalidKeyException &&

hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/security/GDPRSymmetricKey.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.apache.commons.lang3.RandomStringUtils;
2121
import org.apache.hadoop.ozone.OzoneConsts;
2222

23+
import java.security.SecureRandom;
2324
import java.util.HashMap;
2425
import java.util.Map;
2526

@@ -48,10 +49,11 @@ public Cipher getCipher() {
4849
* Default constructor creates key with default values.
4950
* @throws Exception
5051
*/
51-
public GDPRSymmetricKey() throws Exception {
52+
public GDPRSymmetricKey(SecureRandom secureRandom) throws Exception {
5253
algorithm = OzoneConsts.GDPR_ALGORITHM_NAME;
53-
secret = RandomStringUtils
54-
.randomAlphabetic(OzoneConsts.GDPR_DEFAULT_RANDOM_SECRET_LENGTH);
54+
secret = RandomStringUtils.random(
55+
OzoneConsts.GDPR_DEFAULT_RANDOM_SECRET_LENGTH,
56+
0, 0, true, true, null, secureRandom);
5557
this.secretKey = new SecretKeySpec(
5658
secret.getBytes(OzoneConsts.GDPR_CHARSET), algorithm);
5759
this.cipher = Cipher.getInstance(algorithm);

hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/security/TestGDPRSymmetricKey.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
import org.junit.Assert;
2222
import org.junit.Test;
2323

24+
import java.security.SecureRandom;
25+
2426
/**
2527
* Tests GDPRSymmetricKey structure.
2628
*/
2729
public class TestGDPRSymmetricKey {
2830

2931
@Test
3032
public void testKeyGenerationWithDefaults() throws Exception {
31-
GDPRSymmetricKey gkey = new GDPRSymmetricKey();
33+
GDPRSymmetricKey gkey = new GDPRSymmetricKey(new SecureRandom());
3234

3335
Assert.assertTrue(gkey.getCipher().getAlgorithm()
3436
.equalsIgnoreCase(OzoneConsts.GDPR_ALGORITHM_NAME));

0 commit comments

Comments
 (0)