Commit 4392c544 authored by femiadeyemi's avatar femiadeyemi
Browse files

fix uuid generation

Motivation:

A new uuid is generated for an entity that will be save on the db..
This is use as a primary way of uniquely identify a particular node.
To ensure the uniqueness of the uuid, two factor were considered
which are:
- uuid version 1
- secure random number
However the secured random number is currently generated causing the
thread to block in a VM (for details explaination see
https://tersesystems.com/blog/2015/12/17/the-right-way-to-use-securerandom/).
Hence the behaviour of the system become unpredicate and most likely
will fail if the request will required the uuid to be generated.

Modification:

- change how the secured random number is generated
- minor typo fix inside the MarketUserRepository

Result:

The generation of cerebrum custom uuid now work seemly on both local
machine and VMs. Hence, all requests are now process as expected.

Target: master
parent 31fee152
......@@ -23,7 +23,7 @@ public interface MarketUserRepository extends Neo4jRepository<MarketUser, Long>
MarketUser findByEmailAndSub(@Param("email") String email, @Param("sub") String sub);
@Query("MATCH (user:MarketUser),(org:Organization) " +
"WHERE user.uuid = userUuid AND org.uuid = $orgUuid " +
"WHERE user.uuid = $userUuid AND org.uuid = $orgUuid " +
"CREATE (user)-[r:BELONGS_TO { status : $status, isAContactPerson : $isAContactPerson }]->(org) " +
"RETURN user, r")
MarketUser createBelongsToRelationship(@Param("userUuid") String userUuid,
......
package de.helmholtz.marketplace.cerebrum.utils;
import org.neo4j.ogm.id.IdStrategy;
import org.springframework.web.server.ServerErrorException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;
import de.helmholtz.marketplace.cerebrum.errorhandling.exception.CerebrumInvalidUuidException;
......@@ -63,7 +60,7 @@ public class CerebrumEntityUuidGenerator implements IdStrategy
public static synchronized String generate(String prefix)
{
PrefixEnum.checkPrefixValidity(prefix);
return prefix + "-" + generateType1UUID().toString();
return prefix + "-" + generateType1UUID();
}
public static Boolean isValid(String id)
......@@ -98,14 +95,10 @@ public class CerebrumEntityUuidGenerator implements IdStrategy
private static long get64LeastSignificantBitsForVersion1()
{
try {
Random random = SecureRandom.getInstanceStrong();
long random63BitLong = random.nextLong() & 0x3FFFFFFFFFFFFFFFL;
long variant3BitFlag = 0x8000000000000000L;
return random63BitLong + variant3BitFlag;
} catch (NoSuchAlgorithmException e) {
throw new ServerErrorException("Error in generating secure random number for uuid", e);
}
SecureRandom random = new SecureRandom();
long random63BitLong = random.nextLong() & 0x3FFFFFFFFFFFFFFFL;
long variant3BitFlag = 0x8000000000000000L;
return random63BitLong + variant3BitFlag;
}
private static long get64MostSignificantBitsForVersion1()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment