Lazily generate san string for certificates

The san string used by certificate generation for ssl tests currently
runs at gradle configuration time. This takes several seconds, and
significantly slows down gradle configuration on every invocation.
This change wraps the code into a lazy evaluator that will be invoked at
runtime, and cache the string once it is generated.

Original commit: elastic/x-pack-elasticsearch@812036f416
This commit is contained in:
Ryan Ernst 2016-03-14 17:22:05 -07:00
parent 7d377a5b7f
commit 1b21f20e5b

View File

@ -8,7 +8,7 @@ dependencies {
} }
// needed to be consistent with ssl host checking // needed to be consistent with ssl host checking
String san = getSubjectAlternativeNameString() Object san = new SanEvaluator()
// location of generated keystores and certificates // location of generated keystores and certificates
File keystoreDir = new File(project.buildDir, 'keystore') File keystoreDir = new File(project.buildDir, 'keystore')
@ -200,10 +200,23 @@ processTestResources {
} }
} }
/** A lazy evaluator to find the san to use for certificate generation. */
class SanEvaluator {
// Code stolen from NetworkUtils/InetAddresses/NetworkAddress to support SAN private static String san = null
/** Return all interfaces (and subinterfaces) on the system */
static List<NetworkInterface> getInterfaces() throws SocketException { String toString() {
synchronized (SanEvaluator.class) {
if (san == null) {
san = getSubjectAlternativeNameString()
}
}
return san
}
// Code stolen from NetworkUtils/InetAddresses/NetworkAddress to support SAN
/** Return all interfaces (and subinterfaces) on the system */
private static List<NetworkInterface> getInterfaces() throws SocketException {
List<NetworkInterface> all = new ArrayList<>(); List<NetworkInterface> all = new ArrayList<>();
addAllInterfaces(all, Collections.list(NetworkInterface.getNetworkInterfaces())); addAllInterfaces(all, Collections.list(NetworkInterface.getNetworkInterfaces()));
Collections.sort(all, new Comparator<NetworkInterface>() { Collections.sort(all, new Comparator<NetworkInterface>() {
@ -213,19 +226,19 @@ static List<NetworkInterface> getInterfaces() throws SocketException {
} }
}); });
return all; return all;
} }
/** Helper for getInterfaces, recursively adds subinterfaces to {@code target} */ /** Helper for getInterfaces, recursively adds subinterfaces to {@code target} */
private static void addAllInterfaces(List<NetworkInterface> target, List<NetworkInterface> level) { private static void addAllInterfaces(List<NetworkInterface> target, List<NetworkInterface> level) {
if (!level.isEmpty()) { if (!level.isEmpty()) {
target.addAll(level); target.addAll(level);
for (NetworkInterface intf : level) { for (NetworkInterface intf : level) {
addAllInterfaces(target, Collections.list(intf.getSubInterfaces())); addAllInterfaces(target, Collections.list(intf.getSubInterfaces()));
} }
} }
} }
private static String getSubjectAlternativeNameString() { private static String getSubjectAlternativeNameString() {
List<InetAddress> list = new ArrayList<>(); List<InetAddress> list = new ArrayList<>();
for (NetworkInterface intf : getInterfaces()) { for (NetworkInterface intf : getInterfaces()) {
if (intf.isUp()) { if (intf.isUp()) {
@ -264,9 +277,9 @@ private static String getSubjectAlternativeNameString() {
} }
return builder.toString(); return builder.toString();
} }
private static String compressedIPV6Address(Inet6Address inet6Address) { private static String compressedIPV6Address(Inet6Address inet6Address) {
byte[] bytes = inet6Address.getAddress(); byte[] bytes = inet6Address.getAddress();
int[] hextets = new int[8]; int[] hextets = new int[8];
for (int i = 0; i < hextets.length; i++) { for (int i = 0; i < hextets.length; i++) {
@ -274,9 +287,9 @@ private static String compressedIPV6Address(Inet6Address inet6Address) {
} }
compressLongestRunOfZeroes(hextets); compressLongestRunOfZeroes(hextets);
return hextetsToIPv6String(hextets); return hextetsToIPv6String(hextets);
} }
/** /**
* Identify and mark the longest run of zeroes in an IPv6 address. * Identify and mark the longest run of zeroes in an IPv6 address.
* *
* <p>Only runs of two or more hextets are considered. In case of a tie, the * <p>Only runs of two or more hextets are considered. In case of a tie, the
@ -285,7 +298,7 @@ private static String compressedIPV6Address(Inet6Address inet6Address) {
* *
* @param hextets {@code int[]} mutable array of eight 16-bit hextets * @param hextets {@code int[]} mutable array of eight 16-bit hextets
*/ */
private static void compressLongestRunOfZeroes(int[] hextets) { private static void compressLongestRunOfZeroes(int[] hextets) {
int bestRunStart = -1; int bestRunStart = -1;
int bestRunLength = -1; int bestRunLength = -1;
int runStart = -1; int runStart = -1;
@ -306,9 +319,9 @@ private static void compressLongestRunOfZeroes(int[] hextets) {
if (bestRunLength >= 2) { if (bestRunLength >= 2) {
Arrays.fill(hextets, bestRunStart, bestRunStart + bestRunLength, -1); Arrays.fill(hextets, bestRunStart, bestRunStart + bestRunLength, -1);
} }
} }
/** /**
* Convert a list of hextets into a human-readable IPv6 address. * Convert a list of hextets into a human-readable IPv6 address.
* *
* <p>In order for "::" compression to work, the input should contain negative * <p>In order for "::" compression to work, the input should contain negative
@ -316,7 +329,7 @@ private static void compressLongestRunOfZeroes(int[] hextets) {
* *
* @param hextets {@code int[]} array of eight 16-bit hextets, or -1s * @param hextets {@code int[]} array of eight 16-bit hextets, or -1s
*/ */
private static String hextetsToIPv6String(int[] hextets) { private static String hextetsToIPv6String(int[] hextets) {
/* /*
* While scanning the array, handle these state transitions: * While scanning the array, handle these state transitions:
* start->num => "num" start->gap => "::" * start->num => "num" start->gap => "::"
@ -340,4 +353,6 @@ private static String hextetsToIPv6String(int[] hextets) {
lastWasNumber = thisIsNumber; lastWasNumber = thisIsNumber;
} }
return buf.toString(); return buf.toString();
}
} }