55
55
import static java .lang .String .format ;
56
56
57
57
/**
58
- *
59
58
* Holds details of network and channel configurations typically loaded from an external config file.
60
59
* <br>
61
60
* Also contains convenience methods for utilizing the config details,
62
61
* including the main {@link HFClient#getChannel(String)} method
63
- *
64
62
*/
65
63
66
64
public class NetworkConfig {
@@ -81,7 +79,6 @@ public class NetworkConfig {
81
79
82
80
private static final Log logger = LogFactory .getLog (NetworkConfig .class );
83
81
84
-
85
82
private NetworkConfig (JsonObject jsonConfig ) throws InvalidArgumentException , NetworkConfigurationException {
86
83
87
84
this .jsonConfig = jsonConfig ;
@@ -106,27 +103,24 @@ private NetworkConfig(JsonObject jsonConfig) throws InvalidArgumentException, Ne
106
103
createAllCertificateAuthorities ();
107
104
createAllOrganizations ();
108
105
109
-
110
106
// Validate the organization for this client
111
107
JsonObject jsonClient = getJsonObject (jsonConfig , "client" );
112
108
String orgName = jsonClient == null ? null : getJsonValueAsString (jsonClient .get ("organization" ));
113
109
if (orgName == null || orgName .isEmpty ()) {
114
110
throw new InvalidArgumentException ("A client organization must be specified" );
115
111
}
116
112
117
-
118
113
clientOrganization = getOrganizationInfo (orgName );
119
114
if (clientOrganization == null ) {
120
115
throw new InvalidArgumentException ("Client organization " + orgName + " is not defined" );
121
116
}
122
117
123
118
}
124
119
125
-
126
120
/**
127
121
* Creates a new NetworkConfig instance configured with details supplied in a YAML file.
128
122
*
129
- * @param configFile The file containing the network configuration
123
+ * @param configFile The file containing the network configuration
130
124
* @return A new NetworkConfig instance
131
125
* @throws InvalidArgumentException
132
126
* @throws IOException
@@ -138,7 +132,7 @@ public static NetworkConfig fromYamlFile(File configFile) throws InvalidArgument
138
132
/**
139
133
* Creates a new NetworkConfig instance configured with details supplied in a JSON file.
140
134
*
141
- * @param configFile The file containing the network configuration
135
+ * @param configFile The file containing the network configuration
142
136
* @return A new NetworkConfig instance
143
137
* @throws InvalidArgumentException
144
138
* @throws IOException
@@ -165,7 +159,7 @@ public static NetworkConfig fromYamlStream(InputStream configStream) throws Inva
165
159
166
160
Yaml yaml = new Yaml ();
167
161
168
- @ SuppressWarnings ("unchecked" )
162
+ @ SuppressWarnings ("unchecked" )
169
163
Map <String , Object > map = (Map <String , Object >) yaml .load (configStream );
170
164
171
165
JsonObjectBuilder builder = Json .createObjectBuilder (map );
@@ -256,7 +250,7 @@ private static NetworkConfig load(JsonObject jsonConfig) throws InvalidArgumentE
256
250
throw new InvalidArgumentException ("config must be specified" );
257
251
}
258
252
259
- return new NetworkConfig (jsonConfig );
253
+ return new NetworkConfig (jsonConfig );
260
254
}
261
255
262
256
public OrgInfo getClientOrganization () {
@@ -273,6 +267,7 @@ public Collection<OrgInfo> getOrganizationInfos() {
273
267
274
268
/**
275
269
* Returns the admin user associated with the client organization
270
+ *
276
271
* @return The admin user details
277
272
* @throws NetworkConfigurationException
278
273
*/
@@ -298,17 +293,14 @@ public UserInfo getPeerAdmin(String orgName) throws NetworkConfigurationExceptio
298
293
return org .getPeerAdmin ();
299
294
}
300
295
301
-
302
-
303
296
//public Set<CertificateAuthority> getPeerCertificateAuthorites(String peerName) {
304
297
// return null;
305
298
//}
306
299
307
-
308
300
/**
309
301
* Returns a channel configured using the details in the Network Configuration file
310
302
*
311
- * @param client The associated client
303
+ * @param client The associated client
312
304
* @param channelName The name of the channel
313
305
* @return A configured Channel instance
314
306
*/
@@ -510,7 +502,6 @@ private void createAllOrganizations() throws NetworkConfigurationException {
510
502
511
503
}
512
504
513
-
514
505
// Reconstructs an existing channel
515
506
private Channel reconstructChannel (HFClient client , String channelName , JsonObject jsonChannel ) throws NetworkConfigurationException {
516
507
@@ -525,7 +516,7 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
525
516
526
517
//out("Orderer names: " + (ordererNames == null ? "null" : ordererNames.toString()));
527
518
if (ordererNames != null ) {
528
- for (JsonValue jsonVal : ordererNames ) {
519
+ for (JsonValue jsonVal : ordererNames ) {
529
520
530
521
String ordererName = getJsonValueAsString (jsonVal );
531
522
Orderer orderer = getOrderer (client , ordererName );
@@ -594,7 +585,6 @@ private Channel reconstructChannel(HFClient client, String channelName, JsonObje
594
585
throw new NetworkConfigurationException (format ("Error constructing channel %s. At least one peer must be specified" , channelName ));
595
586
}
596
587
597
-
598
588
} catch (InvalidArgumentException e ) {
599
589
throw new IllegalArgumentException (e );
600
590
}
@@ -635,6 +625,12 @@ private Node createNode(String nodeName, JsonObject jsonOrderer, String urlPropN
635
625
Properties props = extractProperties (jsonOrderer , "grpcOptions" );
636
626
637
627
// 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 {
638
634
JsonObject jsonTlsCaCerts = getJsonObject (jsonOrderer , "tlsCACerts" );
639
635
if (jsonTlsCaCerts != null ) {
640
636
String pemFilename = getJsonValueAsString (jsonTlsCaCerts .get ("path" ));
@@ -648,23 +644,15 @@ private Node createNode(String nodeName, JsonObject jsonOrderer, String urlPropN
648
644
// Determine full pathname and ensure the file exists
649
645
File pemFile = new File (pemFilename );
650
646
String fullPathname = pemFile .getAbsolutePath ();
651
- if (!pemFile .exists ()) {
652
- throw new NetworkConfigurationException (format ("Endpoint %s: Pem file %s does not exist" , nodeName , fullPathname ));
653
- }
654
647
props .put ("pemFile" , fullPathname );
655
648
}
656
649
657
650
if (pemBytes != null ) {
658
651
props .put ("pemBytes" , pemBytes .getBytes ());
659
652
}
660
653
}
661
-
662
- return new Node (nodeName , url , props );
663
654
}
664
655
665
-
666
-
667
-
668
656
// Creates a new OrgInfo instance from a JSON object
669
657
private OrgInfo createOrg (String orgName , JsonObject jsonOrg ) throws NetworkConfigurationException {
670
658
@@ -678,7 +666,7 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg) throws NetworkConf
678
666
// Peers
679
667
JsonArray jsonPeers = getJsonValueAsArray (jsonOrg .get ("peers" ));
680
668
if (jsonPeers != null ) {
681
- for (JsonValue peer : jsonPeers ) {
669
+ for (JsonValue peer : jsonPeers ) {
682
670
String peerName = getJsonValueAsString (peer );
683
671
if (peerName != null ) {
684
672
org .addPeerName (peerName );
@@ -689,7 +677,7 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg) throws NetworkConf
689
677
// CAs
690
678
JsonArray jsonCertificateAuthorities = getJsonValueAsArray (jsonOrg .get ("certificateAuthorities" ));
691
679
if (jsonCertificateAuthorities != null ) {
692
- for (JsonValue jsonCA : jsonCertificateAuthorities ) {
680
+ for (JsonValue jsonCA : jsonCertificateAuthorities ) {
693
681
String caName = getJsonValueAsString (jsonCA );
694
682
if (caName != null ) {
695
683
//org.addCAName(caName);
@@ -702,7 +690,6 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg) throws NetworkConf
702
690
}
703
691
}
704
692
705
-
706
693
String adminPrivateKeyString = extractPemString (jsonOrg , "adminPrivateKey" , msgPrefix );
707
694
String signedCert = extractPemString (jsonOrg , "signedCert" , msgPrefix );
708
695
@@ -715,7 +702,6 @@ private OrgInfo createOrg(String orgName, JsonObject jsonOrg) throws NetworkConf
715
702
}
716
703
}
717
704
718
-
719
705
if (privateKey != null ) {
720
706
org .setAdminPrivateKey (privateKey );
721
707
}
@@ -742,15 +728,12 @@ private static PrivateKey getPrivateKeyFromString(String data)
742
728
return privateKey ;
743
729
}
744
730
745
-
746
-
747
731
// Returns the PEM (as a String) from either a path or a pem field
748
732
private static String extractPemString (JsonObject json , String fieldName , String msgPrefix ) throws NetworkConfigurationException {
749
733
750
734
String path = null ;
751
735
String pemString = null ;
752
736
753
-
754
737
JsonObject jsonField = getJsonValueAsObject (json .get (fieldName ));
755
738
if (jsonField != null ) {
756
739
path = getJsonValueAsString (jsonField .get ("path" ));
@@ -761,7 +744,6 @@ private static String extractPemString(JsonObject json, String fieldName, String
761
744
throw new NetworkConfigurationException (format ("%s should not specify both %s path and pem" , msgPrefix , fieldName ));
762
745
}
763
746
764
-
765
747
if (path != null ) {
766
748
// Determine full pathname and ensure the file exists
767
749
File pemFile = new File (path );
@@ -781,7 +763,7 @@ private static String extractPemString(JsonObject json, String fieldName, String
781
763
}
782
764
783
765
// 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 {
785
767
786
768
String url = getJsonValueAsString (jsonCA .get ("url" ));
787
769
Properties httpOptions = extractProperties (jsonCA , "httpOptions" );
@@ -802,7 +784,12 @@ private static CAInfo createCA(String name, JsonObject jsonCA) {
802
784
caInfo .setCaName (caName );
803
785
}
804
786
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 );
806
793
807
794
return caInfo ;
808
795
}
@@ -824,7 +811,6 @@ private static Properties extractProperties(JsonObject json, String fieldName) {
824
811
return props ;
825
812
}
826
813
827
-
828
814
// Returns a new Peer instance for the specified peer name
829
815
private Peer getPeer (HFClient client , String peerName ) throws InvalidArgumentException {
830
816
Peer peer = null ;
@@ -835,7 +821,6 @@ private Peer getPeer(HFClient client, String peerName) throws InvalidArgumentExc
835
821
return peer ;
836
822
}
837
823
838
-
839
824
// Returns a new EventHub instance for the specified name
840
825
private EventHub getEventHub (HFClient client , String name ) throws InvalidArgumentException {
841
826
EventHub ehub = null ;
@@ -846,7 +831,6 @@ private EventHub getEventHub(HFClient client, String name) throws InvalidArgumen
846
831
return ehub ;
847
832
}
848
833
849
-
850
834
// Returns the specified JsonValue in a suitable format
851
835
// If it's a JsonString - it returns the string
852
836
// 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) {
911
895
return obj ;
912
896
}
913
897
914
-
915
898
// Holds a network "node" (eg. Peer, Orderer, EventHub)
916
899
private class Node {
917
900
@@ -937,12 +920,10 @@ private Properties getProperties() {
937
920
return properties ;
938
921
}
939
922
940
-
941
923
}
942
924
943
925
/**
944
926
* Holds details of a User
945
- *
946
927
*/
947
928
public static class UserInfo {
948
929
@@ -969,7 +950,7 @@ public String getMspId() {
969
950
}
970
951
971
952
public PrivateKey getPrivateKey () {
972
- return parentOrg .getAdminPrivateKey ();
953
+ return parentOrg .getAdminPrivateKey ();
973
954
}
974
955
975
956
public String getSignedCert () {
@@ -979,7 +960,6 @@ public String getSignedCert() {
979
960
980
961
/**
981
962
* Holds details of an Organization
982
- *
983
963
*/
984
964
public static class OrgInfo {
985
965
@@ -990,7 +970,6 @@ public static class OrgInfo {
990
970
private final List <String > peerNames = new ArrayList <>();
991
971
private final List <CAInfo > certificateAuthorities = new ArrayList <>();
992
972
993
-
994
973
OrgInfo (String orgName , String mspId ) {
995
974
this .name = orgName ;
996
975
this .mspId = mspId ;
@@ -1012,7 +991,6 @@ private void setSignedCert(String signedCert) {
1012
991
this .signedCert = signedCert ;
1013
992
}
1014
993
1015
-
1016
994
public String getName () {
1017
995
return name ;
1018
996
}
@@ -1063,17 +1041,16 @@ public UserInfo getPeerAdmin() throws NetworkConfigurationException {
1063
1041
}
1064
1042
1065
1043
/**
1066
- *
1067
- * Holds the details of a Certificate Authority
1068
- *
1069
- */
1044
+ * Holds the details of a Certificate Authority
1045
+ */
1070
1046
public static class CAInfo {
1071
1047
private final String name ;
1072
1048
private final String url ;
1073
1049
private final String registrarEnrollId ;
1074
1050
private final String registrarEnrollSecret ;
1075
1051
private final Properties httpOptions ;
1076
1052
private String caName ; // The "optional" caName specified in the config, as opposed to its "config" name
1053
+ private Properties properties ;
1077
1054
1078
1055
CAInfo (String name , String url , String registrarEnrollId , String registrarEnrollSecret , Properties httpOptions ) {
1079
1056
this .name = name ;
@@ -1111,6 +1088,13 @@ public Properties getHttpOptions() {
1111
1088
return httpOptions ;
1112
1089
}
1113
1090
1091
+ void setProperties (Properties properties ) {
1092
+ this .properties = properties ;
1093
+ }
1094
+
1095
+ public Properties getProperties () {
1096
+ return this .properties ;
1097
+ }
1114
1098
}
1115
1099
1116
1100
}
0 commit comments