1414
1515package org .hyperledger .fabric .sdk ;
1616
17- import java .io .File ;
17+ import java .io .ByteArrayInputStream ;
18+ import java .io .IOException ;
19+ import java .io .InputStream ;
1820import java .lang .reflect .InvocationTargetException ;
1921import java .lang .reflect .Method ;
22+ import java .nio .charset .StandardCharsets ;
2023import java .nio .file .Files ;
2124import java .nio .file .Path ;
2225import java .nio .file .Paths ;
4447import org .bouncycastle .asn1 .x500 .style .BCStyle ;
4548import org .bouncycastle .asn1 .x500 .style .IETFUtils ;
4649import org .bouncycastle .cert .jcajce .JcaX509CertificateHolder ;
47- import org .hyperledger .fabric .sdk .helper .Utils ;
4850import org .hyperledger .fabric .sdk .security .CryptoPrimitives ;
4951
5052import static org .hyperledger .fabric .sdk .helper .Utils .parseGrpcUrl ;
@@ -64,10 +66,10 @@ class Endpoint {
6466 logger .trace (String .format ("Creating endpoint for url %s" , url ));
6567 this .url = url ;
6668
67- String pem = null ;
6869 String cn = null ;
6970 String sslp = null ;
7071 String nt = null ;
72+ byte [] pemBytes = null ;
7173
7274 Properties purl = parseGrpcUrl (url );
7375 String protocol = purl .getProperty ("protocol" );
@@ -76,33 +78,42 @@ class Endpoint {
7678
7779 if (properties != null ) {
7880 if ("grpcs" .equals (protocol )) {
79- try {
80- pem = properties .getProperty ("pemFile" );
81- cn = properties .getProperty ("hostnameOverride" );
82-
83- if (cn == null && "true" .equals (properties .getProperty ("trustServerCertificate" ))) {
81+ if (properties .containsKey ("pemFile" ) && properties .containsKey ("pemBytes" )) {
82+ throw new RuntimeException ("Properties \" pemBytes\" and \" pemBytes\" can not be both set." );
83+ }
84+ if (properties .containsKey ("pemFile" )) {
85+ Path path = Paths .get (properties .getProperty ("pemFile" ));
86+ try {
87+ pemBytes = Files .readAllBytes (path );
88+ } catch (IOException e ) {
89+ throw new RuntimeException (e );
90+ }
91+ } else if (properties .containsKey ("pemBytes" )) {
92+ pemBytes = (byte []) properties .get ("pemBytes" );
93+ }
94+ if (null != pemBytes ) {
95+ try {
96+ cn = properties .getProperty ("hostnameOverride" );
8497
85- File pemF = new File ( pem );
86- final String cnKey = pemF . getAbsolutePath () + pemF . length () + pemF . lastModified ( );
98+ if ( cn == null && "true" . equals ( properties . getProperty ( "trustServerCertificate" ))) {
99+ final String cnKey = new String ( pemBytes , StandardCharsets . UTF_8 );
87100
88- cn = CN_CACHE .get (cnKey );
89- if (cn == null ) {
90- Path path = Paths .get (pem );
91- byte [] data = Files .readAllBytes (path );
101+ cn = CN_CACHE .get (cnKey );
102+ if (cn == null ) {
103+ CryptoPrimitives cp = new CryptoPrimitives ();
92104
93- CryptoPrimitives cp = new CryptoPrimitives ();
105+ X500Name x500name = new JcaX509CertificateHolder ((X509Certificate ) cp .bytesToCertificate (pemBytes )).getSubject ();
106+ RDN rdn = x500name .getRDNs (BCStyle .CN )[0 ];
107+ cn = IETFUtils .valueToString (rdn .getFirst ().getValue ());
108+ CN_CACHE .put (cnKey , cn );
109+ }
94110
95- X500Name x500name = new JcaX509CertificateHolder ((X509Certificate ) cp .bytesToCertificate (data )).getSubject ();
96- RDN rdn = x500name .getRDNs (BCStyle .CN )[0 ];
97- cn = IETFUtils .valueToString (rdn .getFirst ().getValue ());
98- CN_CACHE .put (cnKey , cn );
99111 }
112+ } catch (Exception e ) {
113+ /// Mostly a development env. just log it.
114+ logger .error ("Error getting Subject CN from certificate. Try setting it specifically with hostnameOverride property. " + e .getMessage ());
100115
101116 }
102- } catch (Exception e ) {
103- /// Mostly a development env. just log it.
104- logger .error ("Error getting Subject CN from certificate. Try setting it specifically with hostnameOverride property. " + e .getMessage ());
105-
106117 }
107118
108119 sslp = properties .getProperty ("sslProvider" );
@@ -130,7 +141,7 @@ class Endpoint {
130141 .usePlaintext (true );
131142 addNettyBuilderProps (channelBuilder , properties );
132143 } else if (protocol .equalsIgnoreCase ("grpcs" )) {
133- if (Utils . isNullOrEmpty ( pem ) ) {
144+ if (pemBytes == null ) {
134145 // use root certificate
135146 this .channelBuilder = NettyChannelBuilder .forAddress (addr , port );
136147 addNettyBuilderProps (channelBuilder , properties );
@@ -140,8 +151,9 @@ class Endpoint {
140151 SslProvider sslprovider = sslp .equals ("openSSL" ) ? SslProvider .OPENSSL : SslProvider .JDK ;
141152 NegotiationType ntype = nt .equals ("TLS" ) ? NegotiationType .TLS : NegotiationType .PLAINTEXT ;
142153
154+ InputStream myInputStream = new ByteArrayInputStream (pemBytes );
143155 SslContext sslContext = GrpcSslContexts .forClient ()
144- .trustManager (new File ( pem ) )
156+ .trustManager (myInputStream )
145157 .sslProvider (sslprovider )
146158 .build ();
147159 this .channelBuilder = NettyChannelBuilder .forAddress (addr , port )
0 commit comments