16
16
17
17
package org .hyperledger .java .shim ;
18
18
19
- import java .io .File ;
20
-
21
- import javax .net .ssl .SSLException ;
22
-
23
- import org .apache .commons .cli .CommandLine ;
24
- import org .apache .commons .cli .DefaultParser ;
25
- import org .apache .commons .cli .Options ;
26
-
27
19
import com .google .protobuf .ByteString ;
28
-
29
- import org .apache .commons .logging .Log ;
30
- import org .apache .commons .logging .LogFactory ;
31
-
32
20
import io .grpc .ManagedChannel ;
33
21
import io .grpc .netty .GrpcSslContexts ;
34
22
import io .grpc .netty .NegotiationType ;
35
23
import io .grpc .netty .NettyChannelBuilder ;
36
24
import io .grpc .stub .StreamObserver ;
37
25
import io .netty .handler .ssl .SslContext ;
26
+ import org .apache .commons .cli .CommandLine ;
27
+ import org .apache .commons .cli .DefaultParser ;
28
+ import org .apache .commons .cli .Options ;
29
+ import org .apache .commons .logging .Log ;
30
+ import org .apache .commons .logging .LogFactory ;
38
31
import org .hyperledger .protos .Chaincode .ChaincodeID ;
39
32
import org .hyperledger .protos .Chaincode .ChaincodeMessage ;
40
33
import org .hyperledger .protos .Chaincode .ChaincodeMessage .Type ;
41
34
import org .hyperledger .protos .ChaincodeSupportGrpc ;
42
35
import org .hyperledger .protos .ChaincodeSupportGrpc .ChaincodeSupportStub ;
43
36
37
+ import javax .net .ssl .SSLException ;
38
+ import java .io .File ;
39
+
44
40
public abstract class ChaincodeBase {
45
41
46
42
private static Log logger = LogFactory .getLog (ChaincodeBase .class );
@@ -54,6 +50,9 @@ public abstract class ChaincodeBase {
54
50
55
51
private String host = DEFAULT_HOST ;
56
52
private int port = DEFAULT_PORT ;
53
+ private String hostOverrideAuthority = "" ;
54
+ private static final String ROOTCERT_PEM = "/root/certs/rootcert.pem" ;
55
+ private boolean tlsEnabled =false ;
57
56
58
57
private Handler handler ;
59
58
private String id = getChaincodeID ();
@@ -64,7 +63,7 @@ public void start(String[] args) {
64
63
options .addOption ("a" , "peerAddress" , true , "Address of peer to connect to" );
65
64
options .addOption ("s" , "securityEnabled" , false , "Present if security is enabled" );
66
65
options .addOption ("i" , "id" , true , "Identity of chaincode" );
67
-
66
+ options . addOption ( "o" , "hostNameOverride" , true , "Hostname override for server certificate" );
68
67
try {
69
68
CommandLine cl = new DefaultParser ().parse (options , args );
70
69
if (cl .hasOption ('a' )) {
@@ -73,8 +72,12 @@ public void start(String[] args) {
73
72
host = host .split (":" )[0 ];
74
73
}
75
74
if (cl .hasOption ('s' )) {
76
- //TODO
77
- logger .warn ("securityEnabled option not implemented yet" );
75
+ tlsEnabled = true ;
76
+ logger .debug ("TLS enabled" );
77
+ if (cl .hasOption ('o' )){
78
+ hostOverrideAuthority = cl .getOptionValue ('o' );
79
+ logger .debug ("server host override given " + hostOverrideAuthority );
80
+ }
78
81
}
79
82
if (cl .hasOption ('i' )) {
80
83
id = cl .getOptionValue ('i' );
@@ -96,21 +99,27 @@ public void start(String[] args) {
96
99
97
100
public ManagedChannel newPeerClientConnection () {
98
101
NettyChannelBuilder builder = NettyChannelBuilder .forAddress (host , port );
99
- //TODO security
100
- if (false ) {//"true".equals(params.get("peer.tls.enabled"))) {
102
+ logger .info ("Inside newPeerCLientConnection" );
103
+
104
+ if (tlsEnabled ) {
105
+ logger .info ("tls enable" );
101
106
try {
102
- SslContext sslContext = GrpcSslContexts .forClient (). trustManager (
103
- new File ( "pathToServerCertPemFile" )). keyManager (new File ("pathToOwnCertPemFile" ),
104
- new File ( "pathToOwnPrivateKeyPemFile" )) .build ();
107
+ SslContext sslContext = GrpcSslContexts .forClient ()
108
+ . trustManager (new File (ROOTCERT_PEM ))
109
+ .build ();
105
110
builder .negotiationType (NegotiationType .TLS );
111
+ if (!hostOverrideAuthority .equals ("" )){
112
+ logger .info ("host override " + hostOverrideAuthority );
113
+ builder .overrideAuthority (hostOverrideAuthority );
114
+ }
106
115
builder .sslContext (sslContext );
116
+ logger .info ("context built" + sslContext );
107
117
} catch (SSLException e ) {
108
118
logger .error ("failed connect to peer with SSLException" ,e );
109
119
}
110
120
} else {
111
121
builder .usePlaintext (true );
112
122
}
113
-
114
123
return builder .build ();
115
124
}
116
125
0 commit comments