Skip to content

Commit c893776

Browse files
chiacyuSlava Tutrinov
authored and
Slava Tutrinov
committed
HDDS-12504. Replace calls to deprecated RandomStringUtils methods (apache#8306)
1 parent 5c08ec8 commit c893776

File tree

97 files changed

+472
-471
lines changed

Some content is hidden

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

97 files changed

+472
-471
lines changed

hadoop-hdds/common/src/test/java/org/apache/hadoop/ozone/common/TestChecksum.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private Checksum getChecksum(ContainerProtos.ChecksumType type, boolean allowChe
5252
public void testVerifyChecksum(boolean useChecksumCache) throws Exception {
5353
Checksum checksum = getChecksum(null, useChecksumCache);
5454
int dataLen = 55;
55-
byte[] data = RandomStringUtils.randomAlphabetic(dataLen).getBytes(UTF_8);
55+
byte[] data = RandomStringUtils.secure().nextAlphabetic(dataLen).getBytes(UTF_8);
5656
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
5757

5858
ChecksumData checksumData = checksum.computeChecksum(byteBuffer, useChecksumCache);
@@ -73,7 +73,7 @@ public void testVerifyChecksum(boolean useChecksumCache) throws Exception {
7373
@ValueSource(booleans = {true, false})
7474
public void testIncorrectChecksum(boolean useChecksumCache) throws Exception {
7575
Checksum checksum = getChecksum(null, useChecksumCache);
76-
byte[] data = RandomStringUtils.randomAlphabetic(55).getBytes(UTF_8);
76+
byte[] data = RandomStringUtils.secure().nextAlphabetic(55).getBytes(UTF_8);
7777
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
7878
ChecksumData originalChecksumData = checksum.computeChecksum(byteBuffer, useChecksumCache);
7979

hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/TestSchemaTwoBackwardsCompatibility.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.hadoop.ozone.container.common;
1919

2020
import static java.nio.charset.StandardCharsets.UTF_8;
21-
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
21+
import static org.apache.commons.lang3.RandomStringUtils.secure;
2222
import static org.apache.hadoop.ozone.OzoneConsts.BLOCK_COUNT;
2323
import static org.apache.hadoop.ozone.OzoneConsts.CONTAINER_BYTES_USED;
2424
import static org.apache.hadoop.ozone.OzoneConsts.PENDING_DELETE_BLOCK_COUNT;
@@ -112,7 +112,7 @@ public class TestSchemaTwoBackwardsCompatibility {
112112
private static final int BLOCKS_PER_TXN = 2;
113113
private static final int CHUNK_LENGTH = 1024;
114114
private static final byte[] SAMPLE_DATA =
115-
randomAlphanumeric(1024).getBytes(UTF_8);
115+
secure().nextAlphanumeric(1024).getBytes(UTF_8);
116116

117117
@BeforeEach
118118
public void setup() throws Exception {

hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/volume/TestVolumeSetDiskChecks.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.apache.hadoop.ozone.container.common.volume;
1919

20-
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
20+
import static org.apache.commons.lang3.RandomStringUtils.secure;
2121
import static org.apache.hadoop.ozone.container.common.impl.ContainerImplTestUtils.newContainerSet;
2222
import static org.assertj.core.api.Assertions.assertThat;
2323
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -221,21 +221,21 @@ private OzoneConfiguration getConfWithDataNodeDirs(int numDirs) {
221221
final OzoneConfiguration ozoneConf = new OzoneConfiguration();
222222
final List<String> dirs = new ArrayList<>();
223223
for (int i = 0; i < numDirs; ++i) {
224-
dirs.add(new File(dir, randomAlphanumeric(10)).toString());
224+
dirs.add(new File(dir, secure().nextAlphanumeric(10)).toString());
225225
}
226226
ozoneConf.set(ScmConfigKeys.HDDS_DATANODE_DIR_KEY,
227227
String.join(",", dirs));
228228

229229
final List<String> metaDirs = new ArrayList<>();
230230
for (int i = 0; i < numDirs; ++i) {
231-
metaDirs.add(new File(dir, randomAlphanumeric(10)).toString());
231+
metaDirs.add(new File(dir, secure().nextAlphanumeric(10)).toString());
232232
}
233233
ozoneConf.set(OzoneConfigKeys.HDDS_CONTAINER_RATIS_DATANODE_STORAGE_DIR,
234234
String.join(",", metaDirs));
235235

236236
final List<String> dbDirs = new ArrayList<>();
237237
for (int i = 0; i < numDirs; ++i) {
238-
dbDirs.add(new File(dir, randomAlphanumeric(10)).toString());
238+
dbDirs.add(new File(dir, secure().nextAlphanumeric(10)).toString());
239239
}
240240
ozoneConf.set(OzoneConfigKeys.HDDS_DATANODE_CONTAINER_DB_DIR,
241241
String.join(",", dbDirs));

hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/keyvalue/TestKeyValueContainerIntegrityChecks.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected KeyValueContainer createContainerWithBlocks(long containerId,
124124
int bytesPerChecksum = 2 * UNIT_LEN;
125125
Checksum checksum = new Checksum(ContainerProtos.ChecksumType.SHA256,
126126
bytesPerChecksum);
127-
byte[] chunkData = RandomStringUtils.randomAscii(CHUNK_LEN).getBytes(UTF_8);
127+
byte[] chunkData = RandomStringUtils.secure().nextAscii(CHUNK_LEN).getBytes(UTF_8);
128128
ChecksumData checksumData = checksum.computeChecksum(chunkData);
129129

130130
KeyValueContainerData containerData = new KeyValueContainerData(containerId,

hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/keyvalue/impl/CommonChunkManagerTestCases.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void testReadOversizeChunk() throws IOException {
6868
ChunkManager chunkManager = createTestSubject();
6969
KeyValueContainer container = getKeyValueContainer();
7070
int tooLarge = OZONE_SCM_CHUNK_MAX_SIZE + 1;
71-
byte[] array = RandomStringUtils.randomAscii(tooLarge).getBytes(UTF_8);
71+
byte[] array = RandomStringUtils.secure().nextAscii(tooLarge).getBytes(UTF_8);
7272
assertThat(array.length).isGreaterThanOrEqualTo(tooLarge);
7373

7474
BlockID blockID = getBlockID();

hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/authority/TestDefaultCAServer.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public void init() throws IOException {
9393
@Test
9494
public void testInit() throws Exception {
9595
CertificateServer testCA = new DefaultCAServer("testCA",
96-
RandomStringUtils.randomAlphabetic(4),
97-
RandomStringUtils.randomAlphabetic(4), caStore,
96+
RandomStringUtils.secure().nextAlphabetic(4),
97+
RandomStringUtils.secure().nextAlphabetic(4), caStore,
9898
new DefaultProfile(),
9999
Paths.get(SCM_CA_CERT_STORAGE_DIR, SCM_CA_PATH).toString());
100100
testCA.init(securityConfig, CAType.ROOT);
@@ -109,8 +109,8 @@ public void testInit() throws Exception {
109109
@Test
110110
public void testMissingCertificate() throws Exception {
111111
CertificateServer testCA = new DefaultCAServer("testCA",
112-
RandomStringUtils.randomAlphabetic(4),
113-
RandomStringUtils.randomAlphabetic(4), caStore,
112+
RandomStringUtils.secure().nextAlphabetic(4),
113+
RandomStringUtils.secure().nextAlphabetic(4), caStore,
114114
new DefaultProfile(),
115115
Paths.get(SCM_CA_CERT_STORAGE_DIR, SCM_CA_PATH).toString());
116116
testCA.init(securityConfig, CAType.ROOT);
@@ -128,8 +128,8 @@ public void testMissingCertificate() throws Exception {
128128
@Test
129129
public void testMissingKey() {
130130
DefaultCAServer testCA = new DefaultCAServer("testCA",
131-
RandomStringUtils.randomAlphabetic(4),
132-
RandomStringUtils.randomAlphabetic(4), caStore,
131+
RandomStringUtils.secure().nextAlphabetic(4),
132+
RandomStringUtils.secure().nextAlphabetic(4), caStore,
133133
new DefaultProfile(),
134134
Paths.get(SCM_CA_CERT_STORAGE_DIR, SCM_CA_PATH).toString());
135135
Consumer<SecurityConfig> caInitializer =
@@ -152,8 +152,8 @@ public void testMissingKey() {
152152
*/
153153
@Test
154154
public void testRequestCertificate() throws Exception {
155-
String scmId = RandomStringUtils.randomAlphabetic(4);
156-
String clusterId = RandomStringUtils.randomAlphabetic(4);
155+
String scmId = RandomStringUtils.secure().nextAlphabetic(4);
156+
String clusterId = RandomStringUtils.secure().nextAlphabetic(4);
157157
KeyPair keyPair =
158158
new HDDSKeyGenerator(securityConfig).generateKey();
159159
//TODO: generateCSR!
@@ -221,8 +221,8 @@ public void testRequestCertificateWithInvalidSubject() throws Exception {
221221
.generateCSR();
222222

223223
CertificateServer testCA = new DefaultCAServer("testCA",
224-
RandomStringUtils.randomAlphabetic(4),
225-
RandomStringUtils.randomAlphabetic(4), caStore,
224+
RandomStringUtils.secure().nextAlphabetic(4),
225+
RandomStringUtils.secure().nextAlphabetic(4), caStore,
226226
new DefaultProfile(),
227227
Paths.get(SCM_CA_CERT_STORAGE_DIR, SCM_CA_PATH).toString());
228228
testCA.init(securityConfig, CAType.ROOT);
@@ -253,8 +253,8 @@ public void testRequestCertificateWithInvalidSubjectFailure() throws Exception {
253253
.generateCSR();
254254

255255
CertificateServer testCA = new DefaultCAServer("testCA",
256-
RandomStringUtils.randomAlphabetic(4),
257-
RandomStringUtils.randomAlphabetic(4), caStore,
256+
RandomStringUtils.secure().nextAlphabetic(4),
257+
RandomStringUtils.secure().nextAlphabetic(4), caStore,
258258
new DefaultProfile(),
259259
Paths.get(SCM_CA_CERT_STORAGE_DIR, SCM_CA_PATH).toString());
260260
testCA.init(securityConfig, CAType.ROOT);
@@ -275,8 +275,8 @@ public void testRequestCertificateWithInvalidSubjectFailure() throws Exception {
275275
public void testIntermediaryCAWithEmpty() {
276276

277277
CertificateServer scmCA = new DefaultCAServer("testCA",
278-
RandomStringUtils.randomAlphabetic(4),
279-
RandomStringUtils.randomAlphabetic(4), caStore,
278+
RandomStringUtils.secure().nextAlphabetic(4),
279+
RandomStringUtils.secure().nextAlphabetic(4), caStore,
280280
new DefaultProfile(), Paths.get("scm").toString());
281281

282282
assertThrows(IllegalStateException.class,
@@ -305,8 +305,8 @@ public void testExternalRootCA() throws Exception {
305305
CertificateCodec.getPEMEncodedString(externalCert));
306306

307307
CertificateServer testCA = new DefaultCAServer("testCA",
308-
RandomStringUtils.randomAlphabetic(4),
309-
RandomStringUtils.randomAlphabetic(4), caStore,
308+
RandomStringUtils.secure().nextAlphabetic(4),
309+
RandomStringUtils.secure().nextAlphabetic(4), caStore,
310310
new DefaultProfile(),
311311
Paths.get(SCM_CA_CERT_STORAGE_DIR, SCM_CA_PATH).toString());
312312
//When initializing a CA server with external cert
@@ -335,8 +335,8 @@ public void testInitWithCertChain() throws Exception {
335335
securityConfig);
336336
try (SCMCertificateClient scmCertificateClient =
337337
new SCMCertificateClient(securityConfig, null, null)) {
338-
String scmId = RandomStringUtils.randomAlphabetic(4);
339-
String clusterId = RandomStringUtils.randomAlphabetic(4);
338+
String scmId = RandomStringUtils.secure().nextAlphabetic(4);
339+
String clusterId = RandomStringUtils.secure().nextAlphabetic(4);
340340
KeyPair keyPair = new HDDSKeyGenerator(securityConfig).generateKey();
341341
KeyStorage keyStorage = new KeyStorage(securityConfig, "");
342342
keyStorage.storeKeyPair(keyPair);
@@ -370,8 +370,8 @@ public void testInitWithCertChain() throws Exception {
370370
CertificateCodec.getPEMEncodedString(certPath));
371371

372372
CertificateServer testCA = new DefaultCAServer("testCA",
373-
RandomStringUtils.randomAlphabetic(4),
374-
RandomStringUtils.randomAlphabetic(4), caStore,
373+
RandomStringUtils.secure().nextAlphabetic(4),
374+
RandomStringUtils.secure().nextAlphabetic(4), caStore,
375375
new DefaultProfile(),
376376
Paths.get(SCM_CA_CERT_STORAGE_DIR, SCM_CA_PATH).toString());
377377
//When initializing a CA server with external cert
@@ -387,8 +387,8 @@ void testIntermediaryCA() throws Exception {
387387
conf.set(HddsConfigKeys.HDDS_X509_MAX_DURATION, "P3650D");
388388
securityConfig = new SecurityConfig(conf);
389389

390-
String clusterId = RandomStringUtils.randomAlphanumeric(4);
391-
String scmId = RandomStringUtils.randomAlphanumeric(4);
390+
String clusterId = RandomStringUtils.secure().nextAlphanumeric(4);
391+
String scmId = RandomStringUtils.secure().nextAlphanumeric(4);
392392

393393
CertificateServer rootCA = new DefaultCAServer("rootCA",
394394
clusterId, scmId, caStore, new DefaultProfile(),

hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/client/TestDefaultCertificateClient.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private X509Certificate generateX509Cert(KeyPair keyPair) throws Exception {
191191

192192
@Test
193193
public void testSignDataStream() throws Exception {
194-
String data = RandomStringUtils.random(100);
194+
String data = RandomStringUtils.secure().next(100);
195195
FileUtils.deleteQuietly(Paths.get(
196196
dnSecurityConfig.getKeyLocation(DN_COMPONENT).toString(),
197197
dnSecurityConfig.getPrivateKeyFileName()).toFile());
@@ -228,7 +228,7 @@ private void validateHash(byte[] hash, byte[] data)
228228
*/
229229
@Test
230230
public void verifySignatureStream() throws Exception {
231-
String data = RandomStringUtils.random(500);
231+
String data = RandomStringUtils.secure().next(500);
232232
byte[] sign = dnCertClient.signData(data.getBytes(UTF_8));
233233

234234
// Positive tests.
@@ -246,7 +246,7 @@ public void verifySignatureStream() throws Exception {
246246
*/
247247
@Test
248248
public void verifySignatureDataArray() throws Exception {
249-
String data = RandomStringUtils.random(500);
249+
String data = RandomStringUtils.secure().next(500);
250250
byte[] sign = dnCertClient.signData(data.getBytes(UTF_8));
251251

252252
// Positive tests.

hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/security/x509/certificate/utils/TestCertificateCodec.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@ private X509Certificate generateTestCert() throws Exception {
188188
LocalDateTime startDate = LocalDateTime.now();
189189
LocalDateTime endDate = startDate.plusDays(1);
190190
return SelfSignedCertificate.newBuilder()
191-
.setSubject(RandomStringUtils.randomAlphabetic(4))
192-
.setClusterID(RandomStringUtils.randomAlphabetic(4))
193-
.setScmID(RandomStringUtils.randomAlphabetic(4))
191+
.setSubject(RandomStringUtils.secure().nextAlphabetic(4))
192+
.setClusterID(RandomStringUtils.secure().nextAlphabetic(4))
193+
.setScmID(RandomStringUtils.secure().nextAlphabetic(4))
194194
.setBeginDate(startDate)
195195
.setEndDate(endDate)
196196
.setConfiguration(securityConfig)

hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/TestRDBSnapshotProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ public void insertRandomData(RDBStore dbStore, int familyIndex)
228228
assertNotNull(firstTable, "Table cannot be null");
229229
for (int x = 0; x < 100; x++) {
230230
byte[] key =
231-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
231+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
232232
byte[] value =
233-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
233+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
234234
firstTable.put(key, value);
235235
}
236236
} catch (Exception e) {

hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestDBStoreBuilder.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ public void builderWithDoubleTableName(@TempDir Path tempDir)
9999

100100
try (Table<byte[], byte[]> firstTable = dbStore.getTable("FIRST")) {
101101
byte[] key =
102-
RandomStringUtils.random(9).getBytes(StandardCharsets.UTF_8);
102+
RandomStringUtils.secure().next(9).getBytes(StandardCharsets.UTF_8);
103103
byte[] value =
104-
RandomStringUtils.random(9).getBytes(StandardCharsets.UTF_8);
104+
RandomStringUtils.secure().next(9).getBytes(StandardCharsets.UTF_8);
105105
firstTable.put(key, value);
106106
byte[] temp = firstTable.get(key);
107107
assertArrayEquals(value, temp);
@@ -121,9 +121,9 @@ public void builderWithDataWrites(@TempDir Path tempDir) throws Exception {
121121
.build()) {
122122
try (Table<byte[], byte[]> firstTable = dbStore.getTable("First")) {
123123
byte[] key =
124-
RandomStringUtils.random(9).getBytes(StandardCharsets.UTF_8);
124+
RandomStringUtils.secure().next(9).getBytes(StandardCharsets.UTF_8);
125125
byte[] value =
126-
RandomStringUtils.random(9).getBytes(StandardCharsets.UTF_8);
126+
RandomStringUtils.secure().next(9).getBytes(StandardCharsets.UTF_8);
127127
firstTable.put(key, value);
128128
byte[] temp = firstTable.get(key);
129129
assertArrayEquals(value, temp);
@@ -148,9 +148,9 @@ public void builderWithDiskProfileWrites(@TempDir Path tempDir)
148148
.build()) {
149149
try (Table<byte[], byte[]> firstTable = dbStore.getTable("First")) {
150150
byte[] key =
151-
RandomStringUtils.random(9).getBytes(StandardCharsets.UTF_8);
151+
RandomStringUtils.secure().next(9).getBytes(StandardCharsets.UTF_8);
152152
byte[] value =
153-
RandomStringUtils.random(9).getBytes(StandardCharsets.UTF_8);
153+
RandomStringUtils.secure().next(9).getBytes(StandardCharsets.UTF_8);
154154
firstTable.put(key, value);
155155
byte[] temp = firstTable.get(key);
156156
assertArrayEquals(value, temp);

hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/db/TestRDBStore.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ public void insertRandomData(RDBStore dbStore, int familyIndex)
121121
assertNotNull(firstTable, "Table cannot be null");
122122
for (int x = 0; x < 100; x++) {
123123
byte[] key =
124-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
124+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
125125
byte[] value =
126-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
126+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
127127
firstTable.put(key, value);
128128
}
129129
} catch (Exception e) {
@@ -169,9 +169,9 @@ public void closeUnderlyingDB() throws Exception {
169169
@Test
170170
public void moveKey() throws Exception {
171171
byte[] key =
172-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
172+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
173173
byte[] value =
174-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
174+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
175175

176176
try (Table firstTable = rdbStore.getTable(families.get(1))) {
177177
firstTable.put(key, value);
@@ -192,12 +192,12 @@ public void moveKey() throws Exception {
192192
@Test
193193
public void moveWithValue() throws Exception {
194194
byte[] key =
195-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
195+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
196196
byte[] value =
197-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
197+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
198198

199199
byte[] nextValue =
200-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
200+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
201201
try (Table firstTable = rdbStore.getTable(families.get(1))) {
202202
firstTable.put(key, value);
203203
try (Table<byte[], byte[]> secondTable = rdbStore
@@ -355,7 +355,7 @@ public void testDowngrade() throws Exception {
355355
try (Table table = rdbStore.getTable(family)) {
356356
byte[] key = family.getBytes(StandardCharsets.UTF_8);
357357
byte[] value =
358-
RandomStringUtils.random(10).getBytes(StandardCharsets.UTF_8);
358+
RandomStringUtils.secure().next(10).getBytes(StandardCharsets.UTF_8);
359359
table.put(key, value);
360360
}
361361
}

0 commit comments

Comments
 (0)