1616 */
1717package org .apache .hadoop .hdds .protocolPB ;
1818
19- import com .google .protobuf .RpcController ;
20- import com .google .protobuf .ServiceException ;
2119import java .io .Closeable ;
2220import java .io .IOException ;
21+ import java .util .function .Consumer ;
22+
23+ import org .apache .hadoop .hdds .protocol .SCMSecurityProtocol ;
2324import org .apache .hadoop .hdds .protocol .proto .HddsProtos .DatanodeDetailsProto ;
2425import org .apache .hadoop .hdds .protocol .proto .HddsProtos .OzoneManagerDetailsProto ;
26+ import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos ;
2527import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .SCMGetCACertificateRequestProto ;
2628import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .SCMGetCertResponseProto ;
2729import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .SCMGetCertificateRequestProto ;
28- import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .SCMGetCertificateRequestProto .Builder ;
2930import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .SCMGetDataNodeCertRequestProto ;
30- import org .apache .hadoop .hdds .protocol .SCMSecurityProtocol ;
31+ import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .SCMSecurityRequest ;
32+ import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .SCMSecurityRequest .Builder ;
33+ import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .SCMSecurityResponse ;
34+ import org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .Type ;
35+ import org .apache .hadoop .hdds .tracing .TracingUtil ;
3136import org .apache .hadoop .ipc .ProtobufHelper ;
3237import org .apache .hadoop .ipc .ProtocolTranslator ;
3338import org .apache .hadoop .ipc .RPC ;
3439
40+ import com .google .protobuf .RpcController ;
41+ import com .google .protobuf .ServiceException ;
3542import static org .apache .hadoop .hdds .protocol .proto .SCMSecurityProtocolProtos .SCMGetOMCertRequestProto ;
3643
3744/**
@@ -52,6 +59,28 @@ public SCMSecurityProtocolClientSideTranslatorPB(
5259 this .rpcProxy = rpcProxy ;
5360 }
5461
62+ /**
63+ * Helper method to wrap the request and send the message.
64+ */
65+ private SCMSecurityResponse submitRequest (
66+ SCMSecurityProtocolProtos .Type type ,
67+ Consumer <Builder > builderConsumer ) throws IOException {
68+ final SCMSecurityResponse response ;
69+ try {
70+
71+ Builder builder = SCMSecurityRequest .newBuilder ()
72+ .setCmdType (type )
73+ .setTraceID (TracingUtil .exportCurrentSpan ());
74+ builderConsumer .accept (builder );
75+ SCMSecurityRequest wrapper = builder .build ();
76+
77+ response = rpcProxy .submitRequest (NULL_RPC_CONTROLLER , wrapper );
78+ } catch (ServiceException ex ) {
79+ throw ProtobufHelper .getRemoteException (ex );
80+ }
81+ return response ;
82+ }
83+
5584 /**
5685 * Closes this stream and releases any system resources associated
5786 * with it. If the stream is already closed then invoking this
@@ -87,8 +116,8 @@ public String getDataNodeCertificate(DatanodeDetailsProto dataNodeDetails,
87116 /**
88117 * Get SCM signed certificate for OM.
89118 *
90- * @param omDetails - OzoneManager Details.
91- * @param certSignReq - Certificate signing request.
119+ * @param omDetails - OzoneManager Details.
120+ * @param certSignReq - Certificate signing request.
92121 * @return byte[] - SCM signed certificate.
93122 */
94123 @ Override
@@ -100,64 +129,61 @@ public String getOMCertificate(OzoneManagerDetailsProto omDetails,
100129 /**
101130 * Get SCM signed certificate for OM.
102131 *
103- * @param omDetails - OzoneManager Details.
104- * @param certSignReq - Certificate signing request.
132+ * @param omDetails - OzoneManager Details.
133+ * @param certSignReq - Certificate signing request.
105134 * @return byte[] - SCM signed certificate.
106135 */
107136 public SCMGetCertResponseProto getOMCertChain (
108137 OzoneManagerDetailsProto omDetails , String certSignReq )
109138 throws IOException {
110- SCMGetOMCertRequestProto . Builder builder = SCMGetOMCertRequestProto
139+ SCMGetOMCertRequestProto request = SCMGetOMCertRequestProto
111140 .newBuilder ()
112141 .setCSR (certSignReq )
113- .setOmDetails (omDetails );
114- try {
115- return rpcProxy .getOMCertificate (NULL_RPC_CONTROLLER , builder .build ());
116- } catch (ServiceException e ) {
117- throw ProtobufHelper .getRemoteException (e );
118- }
142+ .setOmDetails (omDetails )
143+ .build ();
144+ return submitRequest (Type .GetOMCertificate ,
145+ builder -> builder .setGetOMCertRequest (request ))
146+ .getGetCertResponseProto ();
119147 }
120148
121149 /**
122150 * Get SCM signed certificate with given serial id. Throws exception if
123151 * certificate is not found.
124152 *
125- * @param certSerialId - Certificate serial id.
153+ * @param certSerialId - Certificate serial id.
126154 * @return string - pem encoded certificate.
127155 */
128156 @ Override
129157 public String getCertificate (String certSerialId ) throws IOException {
130- Builder builder = SCMGetCertificateRequestProto
158+ SCMGetCertificateRequestProto request = SCMGetCertificateRequestProto
131159 .newBuilder ()
132- .setCertSerialId (certSerialId );
133- try {
134- return rpcProxy .getCertificate (NULL_RPC_CONTROLLER , builder .build ())
135- .getX509Certificate ();
136- } catch (ServiceException e ) {
137- throw ProtobufHelper .getRemoteException (e );
138- }
160+ .setCertSerialId (certSerialId )
161+ .build ();
162+ return submitRequest (Type .GetCertificate ,
163+ builder -> builder .setGetCertificateRequest (request ))
164+ .getGetCertResponseProto ()
165+ .getX509Certificate ();
139166 }
140167
141168 /**
142169 * Get SCM signed certificate for Datanode.
143170 *
144- * @param dnDetails - Datanode Details.
145- * @param certSignReq - Certificate signing request.
171+ * @param dnDetails - Datanode Details.
172+ * @param certSignReq - Certificate signing request.
146173 * @return byte[] - SCM signed certificate.
147174 */
148175 public SCMGetCertResponseProto getDataNodeCertificateChain (
149176 DatanodeDetailsProto dnDetails , String certSignReq )
150177 throws IOException {
151- SCMGetDataNodeCertRequestProto .Builder builder =
178+
179+ SCMGetDataNodeCertRequestProto request =
152180 SCMGetDataNodeCertRequestProto .newBuilder ()
153181 .setCSR (certSignReq )
154- .setDatanodeDetails (dnDetails );
155- try {
156- return rpcProxy .getDataNodeCertificate (NULL_RPC_CONTROLLER ,
157- builder .build ());
158- } catch (ServiceException e ) {
159- throw ProtobufHelper .getRemoteException (e );
160- }
182+ .setDatanodeDetails (dnDetails )
183+ .build ();
184+ return submitRequest (Type .GetDataNodeCertificate ,
185+ builder -> builder .setGetDataNodeCertRequest (request ))
186+ .getGetCertResponseProto ();
161187 }
162188
163189 /**
@@ -169,12 +195,10 @@ public SCMGetCertResponseProto getDataNodeCertificateChain(
169195 public String getCACertificate () throws IOException {
170196 SCMGetCACertificateRequestProto protoIns = SCMGetCACertificateRequestProto
171197 .getDefaultInstance ();
172- try {
173- return rpcProxy .getCACertificate (NULL_RPC_CONTROLLER , protoIns )
174- .getX509Certificate ();
175- } catch (ServiceException e ) {
176- throw ProtobufHelper .getRemoteException (e );
177- }
198+ return submitRequest (Type .GetCACertificate ,
199+ builder -> builder .setGetCACertificateRequest (protoIns ))
200+ .getGetCertResponseProto ().getX509Certificate ();
201+
178202 }
179203
180204 /**
0 commit comments