5555import static java .lang .String .format ;
5656
5757/**
58- *
5958 * Holds details of network and channel configurations typically loaded from an external config file.
6059 * <br>
6160 * Also contains convenience methods for utilizing the config details,
6261 * including the main {@link HFClient#getChannel(String)} method
63- *
6462 */
6563
6664public class NetworkConfig {
@@ -81,7 +79,6 @@ public class NetworkConfig {
8179
8280 private static final Log logger = LogFactory .getLog (NetworkConfig .class );
8381
84-
8582 private NetworkConfig (JsonObject jsonConfig ) throws InvalidArgumentException , NetworkConfigurationException {
8683
8784 this .jsonConfig = jsonConfig ;
@@ -106,27 +103,24 @@ private NetworkConfig(JsonObject jsonConfig) throws InvalidArgumentException, Ne
106103 createAllCertificateAuthorities ();
107104 createAllOrganizations ();
108105
109-
110106 // Validate the organization for this client
111107 JsonObject jsonClient = getJsonObject (jsonConfig , "client" );
112108 String orgName = jsonClient == null ? null : getJsonValueAsString (jsonClient .get ("organization" ));
113109 if (orgName == null || orgName .isEmpty ()) {
114110 throw new InvalidArgumentException ("A client organization must be specified" );
115111 }
116112
117-
118113 clientOrganization = getOrganizationInfo (orgName );
119114 if (clientOrganization == null ) {
120115 throw new InvalidArgumentException ("Client organization " + orgName + " is not defined" );
121116 }
122117
123118 }
124119
125-
126120 /**
127121 * Creates a new NetworkConfig instance configured with details supplied in a YAML file.
128122 *
129- * @param configFile The file containing the network configuration
123+ * @param configFile The file containing the network configuration
130124 * @return A new NetworkConfig instance
131125 * @throws InvalidArgumentException
132126 * @throws IOException
@@ -138,7 +132,7 @@ public static NetworkConfig fromYamlFile(File configFile) throws InvalidArgument
138132 /**
139133 * Creates a new NetworkConfig instance configured with details supplied in a JSON file.
140134 *
141- * @param configFile The file containing the network configuration
135+ * @param configFile The file containing the network configuration
142136 * @return A new NetworkConfig instance
143137 * @throws InvalidArgumentException
144138 * @throws IOException
@@ -165,7 +159,7 @@ public static NetworkConfig fromYamlStream(InputStream configStream) throws Inva
165159
166160 Yaml yaml = new Yaml ();
167161
168- @ SuppressWarnings ("unchecked" )
162+ @ SuppressWarnings ("unchecked" )
169163 Map <String , Object > map = (Map <String , Object >) yaml .load (configStream );
170164
171165 JsonObjectBuilder builder = Json .createObjectBuilder (map );
@@ -256,7 +250,7 @@ private static NetworkConfig load(JsonObject jsonConfig) throws InvalidArgumentE
256250 throw new InvalidArgumentException ("config must be specified" );
257251 }
258252
259- return new NetworkConfig (jsonConfig );
253+ return new NetworkConfig (jsonConfig );
260254 }
261255
262256 public OrgInfo getClientOrganization () {
@@ -273,6 +267,7 @@ public Collection<OrgInfo> getOrganizationInfos() {
273267
274268 /**
275269 * Returns the admin user associated with the client organization
270+ *
276271 * @return The admin user details
277272 * @throws NetworkConfigurationException
278273 */
@@ -298,17 +293,14 @@ public UserInfo getPeerAdmin(String orgName) throws NetworkConfigurationExceptio
298293 return org .getPeerAdmin ();
299294 }
300295
301-
302-
303296 //public Set<CertificateAuthority> getPeerCertificateAuthorites(String peerName) {
304297 // return null;
305298 //}
306299
307-
308300 /**
309301 * Returns a channel configured using the details in the Network Configuration file
310302 *
311- * @param client The associated client
303+ * @param client The associated client
312304 * @param channelName The name of the channel
313305 * @return A configured Channel instance
314306 */
@@ -510,7 +502,6 @@ private void createAllOrganizations() throws NetworkConfigurationException {
510502
511503 }
512504
513-
514505 // Reconstructs an existing channel
515506 private Channel reconstructChannel (HFClient client , String channelName , JsonObject jsonChannel ) throws NetworkConfigurationException {
516507
@@ -525,7 +516,7 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
525516
526517 //out("Orderer names: " + (ordererNames == null ? "null" : ordererNames.toString()));
527518 if (ordererNames != null ) {
528- for (JsonValue jsonVal : ordererNames ) {
519+ for (JsonValue jsonVal : ordererNames ) {
529520
530521 String ordererName = getJsonValueAsString (jsonVal );
531522 Orderer orderer = getOrderer (client , ordererName );
@@ -594,7 +585,6 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
594585 throw new NetworkConfigurationException (format ("Error constructing channel %s. At least one peer must be specified" , channelName ));
595586 }
596587
597-
598588 } catch (InvalidArgumentException e ) {
599589 throw new IllegalArgumentException (e );
600590 }
@@ -635,6 +625,12 @@ private Node createNode(String nodeName, JsonObject jsonOrderer, String urlPropN
635625 Properties props = extractProperties (jsonOrderer , "grpcOptions" );
636626
637627 // Extract the pem details
628+ getTLSCerts (nodeName , jsonOrderer , props );
629+
630+ return new Node (nodeName , url , props );
631+ }
632+
633+ private void getTLSCerts (String nodeName , JsonObject jsonOrderer , Properties props ) throws NetworkConfigurationException {
638634 JsonObject jsonTlsCaCerts = getJsonObject (jsonOrderer , "tlsCACerts" );
639635 if (jsonTlsCaCerts != null ) {
640636 String pemFilename = getJsonValueAsString (jsonTlsCaCerts .get ("path" ));
@@ -648,23 +644,15 @@ private Node createNode(String nodeName, JsonObject jsonOrderer, String urlPropN
648644 // Determine full pathname and ensure the file exists
649645 File pemFile = new File (pemFilename );
650646 String fullPathname = pemFile .getAbsolutePath ();
651- if (!pemFile .exists ()) {
652- throw new NetworkConfigurationException (format ("Endpoint %s: Pem file %s does not exist" , nodeName , fullPathname ));
653- }
654647 props .put ("pemFile" , fullPathname );
655648 }
656649
657650 if (pemBytes != null ) {
658651 props .put ("pemBytes" , pemBytes .getBytes ());
659652 }
660653 }
661-
662- return new Node (nodeName , url , props );
663654 }
664655
665-
666-
667-
668656 // Creates a new OrgInfo instance from a JSON object
669657 private OrgInfo createOrg (String orgName , JsonObject jsonOrg ) throws NetworkConfigurationException {
670658
@@ -678,7 +666,7 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg) throws NetworkConf
678666 // Peers
679667 JsonArray jsonPeers = getJsonValueAsArray (jsonOrg .get ("peers" ));
680668 if (jsonPeers != null ) {
681- for (JsonValue peer : jsonPeers ) {
669+ for (JsonValue peer : jsonPeers ) {
682670 String peerName = getJsonValueAsString (peer );
683671 if (peerName != null ) {
684672 org .addPeerName (peerName );
@@ -689,7 +677,7 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg) throws NetworkConf
689677 // CAs
690678 JsonArray jsonCertificateAuthorities = getJsonValueAsArray (jsonOrg .get ("certificateAuthorities" ));
691679 if (jsonCertificateAuthorities != null ) {
692- for (JsonValue jsonCA : jsonCertificateAuthorities ) {
680+ for (JsonValue jsonCA : jsonCertificateAuthorities ) {
693681 String caName = getJsonValueAsString (jsonCA );
694682 if (caName != null ) {
695683 //org.addCAName(caName);
@@ -702,7 +690,6 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg) throws NetworkConf
702690 }
703691 }
704692
705-
706693 String adminPrivateKeyString = extractPemString (jsonOrg , "adminPrivateKey" , msgPrefix );
707694 String signedCert = extractPemString (jsonOrg , "signedCert" , msgPrefix );
708695
@@ -715,7 +702,6 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg) throws NetworkConf
715702 }
716703 }
717704
718-
719705 if (privateKey != null ) {
720706 org .setAdminPrivateKey (privateKey );
721707 }
@@ -742,15 +728,12 @@ private static PrivateKey getPrivateKeyFromString(String data)
742728 return privateKey ;
743729 }
744730
745-
746-
747731 // Returns the PEM (as a String) from either a path or a pem field
748732 private static String extractPemString (JsonObject json , String fieldName , String msgPrefix ) throws NetworkConfigurationException {
749733
750734 String path = null ;
751735 String pemString = null ;
752736
753-
754737 JsonObject jsonField = getJsonValueAsObject (json .get (fieldName ));
755738 if (jsonField != null ) {
756739 path = getJsonValueAsString (jsonField .get ("path" ));
@@ -761,7 +744,6 @@ private static String extractPemString(JsonObject json, String fieldName, String
761744 throw new NetworkConfigurationException (format ("%s should not specify both %s path and pem" , msgPrefix , fieldName ));
762745 }
763746
764-
765747 if (path != null ) {
766748 // Determine full pathname and ensure the file exists
767749 File pemFile = new File (path );
@@ -781,7 +763,7 @@ private static String extractPemString(JsonObject json, String fieldName, String
781763 }
782764
783765 // Creates a new CAInfo instance from a JSON object
784- private static CAInfo createCA (String name , JsonObject jsonCA ) {
766+ private CAInfo createCA (String name , JsonObject jsonCA ) throws NetworkConfigurationException {
785767
786768 String url = getJsonValueAsString (jsonCA .get ("url" ));
787769 Properties httpOptions = extractProperties (jsonCA , "httpOptions" );
@@ -802,7 +784,12 @@ private static CAInfo createCA(String name, JsonObject jsonCA) {
802784 caInfo .setCaName (caName );
803785 }
804786
805- // TODO: Implement tlsCACerts???
787+ Properties properties = new Properties ();
788+ if (null != httpOptions && "false" .equals (httpOptions .getProperty ("verify" ))) {
789+ properties .setProperty ("allowAllHostNames" , "true" );
790+ }
791+ getTLSCerts (name , jsonCA , properties );
792+ caInfo .setProperties (properties );
806793
807794 return caInfo ;
808795 }
@@ -824,7 +811,6 @@ private static Properties extractProperties(JsonObject json, String fieldName) {
824811 return props ;
825812 }
826813
827-
828814 // Returns a new Peer instance for the specified peer name
829815 private Peer getPeer (HFClient client , String peerName ) throws InvalidArgumentException {
830816 Peer peer = null ;
@@ -835,7 +821,6 @@ private Peer getPeer(HFClient client, String peerName) throws InvalidArgumentExc
835821 return peer ;
836822 }
837823
838-
839824 // Returns a new EventHub instance for the specified name
840825 private EventHub getEventHub (HFClient client , String name ) throws InvalidArgumentException {
841826 EventHub ehub = null ;
@@ -846,7 +831,6 @@ private EventHub getEventHub(HFClient client, String name) throws InvalidArgumen
846831 return ehub ;
847832 }
848833
849-
850834 // Returns the specified JsonValue in a suitable format
851835 // If it's a JsonString - it returns the string
852836 // If it's a number = it returns the string represenation of that number
@@ -911,7 +895,6 @@ private static JsonObject getJsonObject(JsonObject object, String propName) {
911895 return obj ;
912896 }
913897
914-
915898 // Holds a network "node" (eg. Peer, Orderer, EventHub)
916899 private class Node {
917900
@@ -937,12 +920,10 @@ private Properties getProperties() {
937920 return properties ;
938921 }
939922
940-
941923 }
942924
943925 /**
944926 * Holds details of a User
945- *
946927 */
947928 public static class UserInfo {
948929
@@ -969,7 +950,7 @@ public String getMspId() {
969950 }
970951
971952 public PrivateKey getPrivateKey () {
972- return parentOrg .getAdminPrivateKey ();
953+ return parentOrg .getAdminPrivateKey ();
973954 }
974955
975956 public String getSignedCert () {
@@ -979,7 +960,6 @@ public String getSignedCert() {
979960
980961 /**
981962 * Holds details of an Organization
982- *
983963 */
984964 public static class OrgInfo {
985965
@@ -990,7 +970,6 @@ public static class OrgInfo {
990970 private final List <String > peerNames = new ArrayList <>();
991971 private final List <CAInfo > certificateAuthorities = new ArrayList <>();
992972
993-
994973 OrgInfo (String orgName , String mspId ) {
995974 this .name = orgName ;
996975 this .mspId = mspId ;
@@ -1012,7 +991,6 @@ private void setSignedCert(String signedCert) {
1012991 this .signedCert = signedCert ;
1013992 }
1014993
1015-
1016994 public String getName () {
1017995 return name ;
1018996 }
@@ -1063,17 +1041,16 @@ public UserInfo getPeerAdmin() throws NetworkConfigurationException {
10631041 }
10641042
10651043 /**
1066- *
1067- * Holds the details of a Certificate Authority
1068- *
1069- */
1044+ * Holds the details of a Certificate Authority
1045+ */
10701046 public static class CAInfo {
10711047 private final String name ;
10721048 private final String url ;
10731049 private final String registrarEnrollId ;
10741050 private final String registrarEnrollSecret ;
10751051 private final Properties httpOptions ;
10761052 private String caName ; // The "optional" caName specified in the config, as opposed to its "config" name
1053+ private Properties properties ;
10771054
10781055 CAInfo (String name , String url , String registrarEnrollId , String registrarEnrollSecret , Properties httpOptions ) {
10791056 this .name = name ;
@@ -1111,6 +1088,13 @@ public Properties getHttpOptions() {
11111088 return httpOptions ;
11121089 }
11131090
1091+ void setProperties (Properties properties ) {
1092+ this .properties = properties ;
1093+ }
1094+
1095+ public Properties getProperties () {
1096+ return this .properties ;
1097+ }
11141098 }
11151099
11161100}
0 commit comments