diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java index 327f83c..f863cd6 100644 --- a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java @@ -40,7 +40,7 @@ public class CordaClient { this.jsonMapper = jsonMapper; } - public List getVirtualNodeList() { + public List fetchVirtualNodeList() { ResponseEntity resp = this.restTemplate.exchange( "/virtualnode", HttpMethod.GET, diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/HoldingIdentity.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/HoldingIdentity.java index b0f3539..e8f86c2 100644 --- a/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/HoldingIdentity.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/HoldingIdentity.java @@ -34,4 +34,28 @@ public record HoldingIdentity(String x500Name, String shortHash) implements Seri return false; } + + public boolean isCustodian() throws InvalidNameException { + LdapName ln = new LdapName(x500Name); + + for(Rdn rdn : ln.getRdns()) { + if(rdn.getType().equalsIgnoreCase("OU")) { + return rdn.getValue().toString().equalsIgnoreCase("Custodian"); + } + } + + return false; + } + + public boolean isCordaCheckers() throws InvalidNameException { + LdapName ln = new LdapName(x500Name); + + for(Rdn rdn : ln.getRdns()) { + if(rdn.getType().equalsIgnoreCase("O")) { + return rdn.getValue().toString().equalsIgnoreCase("Checkers"); + } + } + + return false; + } } diff --git a/backend/src/main/java/djmil/cordacheckers/user/HoldingIdentityResolver.java b/backend/src/main/java/djmil/cordacheckers/user/HoldingIdentityResolver.java index 48c2c3f..fb96c09 100644 --- a/backend/src/main/java/djmil/cordacheckers/user/HoldingIdentityResolver.java +++ b/backend/src/main/java/djmil/cordacheckers/user/HoldingIdentityResolver.java @@ -5,8 +5,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; -import javax.naming.InvalidNameException; - import org.springframework.stereotype.Service; import djmil.cordacheckers.cordaclient.CordaClient; @@ -22,45 +20,49 @@ public class HoldingIdentityResolver { * Ideally, we want to be able to force update cache, had the corda * cluster configuration changed (aka in case of a cache miss) */ - final Map cache; // PlayerName to ShortHash + Map player; // PlayerName to ShortHash + HoldingIdentity custodian; HoldingIdentityResolver(CordaClient cordaClient) { - this.cache = setCache(cordaClient); + this.player = new HashMap<>(); + + fetchVNodeList(cordaClient); } - private static Map setCache(CordaClient cordaClient) { - Map map = new HashMap<>(); + private void fetchVNodeList(CordaClient cordaClient) { + final List vnodeList = cordaClient.fetchVirtualNodeList(); - try { - List vNodeList = cordaClient.getVirtualNodeList(); + for (VirtualNode vNode : vnodeList) { + final var identity = vNode.holdingIdentity(); + try { + if (!identity.isCordaCheckers()) + continue; - for (VirtualNode vNode : vNodeList) { - var identity = vNode.holdingIdentity(); + if (identity.isPlayer()) + player.put(identity.getName().toLowerCase(locale), identity); - if (identity.isPlayer()) { - map.put( - identity.getName().toLowerCase(locale), - identity - ); - } + if (identity.isCustodian()) + custodian = identity; // <<-- there shall be only one custodian } - } catch (InvalidNameException e) { - // TODO: logs - System.out.println("Unable to get ShorHash list of Corda VirtualNodes: "+e.getMessage()); + catch (Exception e) { + System.err.println("Incomprehensible VirtualNode identity " +identity); + } } - - return map; } /* * @param playerName * HoldingIdentity x500 name typically looks like - * "CN=Bob, OU=Player, O=Checkers, L=Kviv, C=UA" + * "CN=Bob, OU=Player, O=Checkers, L=Kyiv, C=UA" * CN - is a common name, expected to be unique for CordaCheckers setup. * * @return HoldingIdentity */ public HoldingIdentity getByUsername(String userName) { - return this.cache.get(userName.toLowerCase(locale)); + return this.player.get(userName.toLowerCase(locale)); + } + + public HoldingIdentity getCustodian() { + return this.custodian; } } diff --git a/corda/config/static-network-config.json b/corda/config/static-network-config.json index a49c4e4..7462400 100644 --- a/corda/config/static-network-config.json +++ b/corda/config/static-network-config.json @@ -12,7 +12,7 @@ "cpi" : "CordaCheckers" }, { - "x500Name" : "CN=Kumar, OU=Referee, O=Checkers, L=Mumbai, C=IN", + "x500Name" : "CN=Kumar, OU=Custodian, O=Checkers, L=Mumbai, C=IN", "cpi" : "CordaCheckers" }, {