Skip to content

Commit 66534d2

Browse files
christophstroblmp911de
authored andcommitted
DATAKV-136 - Choose SecureRandom algorithm based on operating system and availability.
We now distinguish between operating systems when choosing a SecureRandom algorithm. Additionally we check the availability of the implementations and choose the first one available. Original pull request: #21.
1 parent 906a6da commit 66534d2

File tree

2 files changed

+140
-11
lines changed

2 files changed

+140
-11
lines changed

src/main/java/org/springframework/data/keyvalue/core/DefaultIdentifierGenerator.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014 the original author or authors.
2+
* Copyright 2014-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,11 +17,15 @@
1717

1818
import java.security.NoSuchAlgorithmException;
1919
import java.security.SecureRandom;
20+
import java.util.Arrays;
21+
import java.util.List;
2022
import java.util.UUID;
23+
import java.util.concurrent.atomic.AtomicReference;
2124

2225
import org.springframework.dao.InvalidDataAccessApiUsageException;
2326
import org.springframework.data.util.TypeInformation;
2427
import org.springframework.util.ClassUtils;
28+
import org.springframework.util.StringUtils;
2529

2630
/**
2731
* Default implementation of {@link IdentifierGenerator} to generate identifiers of types {@link UUID}, String,
@@ -33,7 +37,7 @@ enum DefaultIdentifierGenerator implements IdentifierGenerator {
3337

3438
INSTANCE;
3539

36-
private static final String ALGORITHM = "NativePRNGBlocking";
40+
private final AtomicReference<SecureRandom> secureRandom = new AtomicReference<SecureRandom>(null);
3741

3842
/*
3943
* (non-Javadoc)
@@ -50,22 +54,55 @@ public <T> T generateIdentifierOfType(TypeInformation<T> identifierType) {
5054
} else if (ClassUtils.isAssignable(String.class, type)) {
5155
return (T) UUID.randomUUID().toString();
5256
} else if (ClassUtils.isAssignable(Integer.class, type)) {
57+
return (T) Integer.valueOf(getSecureRandom().nextInt());
58+
} else if (ClassUtils.isAssignable(Long.class, type)) {
59+
return (T) Long.valueOf(getSecureRandom().nextLong());
60+
}
5361

54-
try {
55-
return (T) SecureRandom.getInstance(ALGORITHM);
56-
} catch (NoSuchAlgorithmException e) {
57-
throw new InvalidDataAccessApiUsageException("Could not create SecureRandom instance.", e);
58-
}
62+
throw new InvalidDataAccessApiUsageException("Non gereratable id type....");
63+
}
5964

60-
} else if (ClassUtils.isAssignable(Long.class, type)) {
65+
private SecureRandom getSecureRandom() {
6166

67+
SecureRandom secureRandom = this.secureRandom.get();
68+
if (secureRandom != null) {
69+
return secureRandom;
70+
}
71+
72+
for (String algorithm : OsTools.secureRandomAlgorithmNames()) {
6273
try {
63-
return (T) Long.valueOf(SecureRandom.getInstance(ALGORITHM).nextLong());
74+
secureRandom = SecureRandom.getInstance(algorithm);
6475
} catch (NoSuchAlgorithmException e) {
65-
throw new InvalidDataAccessApiUsageException("Could not create SecureRandom instance.", e);
76+
// ignore and try next.
6677
}
6778
}
6879

69-
throw new InvalidDataAccessApiUsageException("Non gereratable id type....");
80+
if (secureRandom == null) {
81+
throw new InvalidDataAccessApiUsageException(
82+
String.format("Could not create SecureRandom instance for one of the algorithms '%s'.",
83+
StringUtils.collectionToCommaDelimitedString(OsTools.secureRandomAlgorithmNames())));
84+
}
85+
86+
this.secureRandom.compareAndSet(null, secureRandom);
87+
88+
return secureRandom;
89+
}
90+
91+
/**
92+
* @author Christoph Strobl
93+
* @since 1.1.2
94+
*/
95+
private static class OsTools {
96+
97+
private static final String OPERATING_SYSTEM_NAME = System.getProperty("os.name").toLowerCase();
98+
99+
private static final List<String> SECURE_RANDOM_ALGORITHMS_LINUX_OSX_SOLARIS = Arrays.asList("NativePRNGBlocking",
100+
"NativePRNGNonBlocking", "NativePRNG", "SHA1PRNG");
101+
private static final List<String> SECURE_RANDOM_ALGORITHMS_WINDOWS = Arrays.asList("SHA1PRNG", "Windows-PRNG");
102+
103+
static List<String> secureRandomAlgorithmNames() {
104+
return OPERATING_SYSTEM_NAME.indexOf("win") >= 0 ? SECURE_RANDOM_ALGORITHMS_WINDOWS
105+
: SECURE_RANDOM_ALGORITHMS_LINUX_OSX_SOLARIS;
106+
}
70107
}
71108
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.keyvalue.core;
17+
18+
import static org.hamcrest.core.Is.*;
19+
import static org.hamcrest.core.IsInstanceOf.*;
20+
import static org.hamcrest.core.IsNull.*;
21+
import static org.junit.Assert.*;
22+
23+
import java.util.Date;
24+
import java.util.UUID;
25+
26+
import org.junit.Test;
27+
import org.springframework.dao.InvalidDataAccessApiUsageException;
28+
import org.springframework.data.util.ClassTypeInformation;
29+
30+
/**
31+
* @author Christoph Strobl
32+
*/
33+
public class DefaultIdentifierGeneratorUnitTests {
34+
35+
DefaultIdentifierGenerator generator = DefaultIdentifierGenerator.INSTANCE;
36+
37+
/**
38+
* @DATAKV-136
39+
*/
40+
@Test(expected = InvalidDataAccessApiUsageException.class)
41+
public void shouldThrowExceptionForUnsupportedType() {
42+
generator.generateIdentifierOfType(ClassTypeInformation.from(Date.class));
43+
}
44+
45+
/**
46+
* @DATAKV-136
47+
*/
48+
@Test
49+
public void shouldGenerateUUIDValueCorrectly() {
50+
51+
Object value = generator.generateIdentifierOfType(ClassTypeInformation.from(UUID.class));
52+
53+
assertThat(value, is(notNullValue()));
54+
assertThat(value, instanceOf(UUID.class));
55+
}
56+
57+
/**
58+
* @DATAKV-136
59+
*/
60+
@Test
61+
public void shouldGenerateStringValueCorrectly() {
62+
63+
Object value = generator.generateIdentifierOfType(ClassTypeInformation.from(String.class));
64+
65+
assertThat(value, is(notNullValue()));
66+
assertThat(value, instanceOf(String.class));
67+
}
68+
69+
/**
70+
* @DATAKV-136
71+
*/
72+
@Test
73+
public void shouldGenerateLongValueCorrectly() {
74+
75+
Object value = generator.generateIdentifierOfType(ClassTypeInformation.from(Long.class));
76+
77+
assertThat(value, is(notNullValue()));
78+
assertThat(value, instanceOf(Long.class));
79+
}
80+
81+
/**
82+
* @DATAKV-136
83+
*/
84+
@Test
85+
public void shouldGenerateIntValueCorrectly() {
86+
87+
Object value = generator.generateIdentifierOfType(ClassTypeInformation.from(Integer.class));
88+
89+
assertThat(value, is(notNullValue()));
90+
assertThat(value, instanceOf(Integer.class));
91+
}
92+
}

0 commit comments

Comments
 (0)