99 */ 
1010package  org .opensearch .security .grpc ;
1111
12+ import  java .net .InetSocketAddress ;
1213import  java .util .Map ;
1314
14- import  com . carrotsearch . randomizedtesting . annotations . ThreadLeakScope ;
15+ import  io . grpc . ManagedChannel ;
1516import  io .netty .handler .ssl .ClientAuth ;
16- import  org .junit .ClassRule ;
17- import  org .junit .Test ;
18- import  org .junit .runner .RunWith ;
1917
18+ import  org .opensearch .common .transport .PortsRange ;
19+ import  org .opensearch .core .common .transport .TransportAddress ;
2020import  org .opensearch .plugin .transport .grpc .GrpcPlugin ;
21+ import  org .opensearch .protobufs .BulkRequest ;
22+ import  org .opensearch .protobufs .BulkRequestBody ;
23+ import  org .opensearch .protobufs .BulkResponse ;
24+ import  org .opensearch .protobufs .IndexOperation ;
2125import  org .opensearch .protobufs .MatchAllQuery ;
2226import  org .opensearch .protobufs .QueryContainer ;
2327import  org .opensearch .protobufs .SearchRequest ;
2428import  org .opensearch .protobufs .SearchRequestBody ;
2529import  org .opensearch .protobufs .SearchResponse ;
30+ import  org .opensearch .protobufs .services .DocumentServiceGrpc ;
31+ import  org .opensearch .protobufs .services .SearchServiceGrpc ;
2632import  org .opensearch .security .support .ConfigConstants ;
2733import  org .opensearch .test .framework .certificate .TestCertificates ;
2834import  org .opensearch .test .framework .cluster .ClusterManager ;
2935import  org .opensearch .test .framework .cluster .LocalCluster ;
30- import  org .opensearch .test .framework .cluster .TestGrpcClient ;
31- import  org .opensearch .test .framework .cluster .TestRestClient ;
3236
33- import  static  org .hamcrest .MatcherAssert .assertThat ;
37+ import  javax .net .ssl .SSLException ;
38+ 
3439import  static  org .opensearch .plugin .transport .grpc .ssl .SecureNetty4GrpcServerTransport .GRPC_SECURE_TRANSPORT_SETTING_KEY ;
3540import  static  org .opensearch .plugin .transport .grpc .ssl .SecureNetty4GrpcServerTransport .SETTING_GRPC_SECURE_PORT ;
3641import  static  org .opensearch .security .ssl .util .SSLConfigConstants .SECURITY_SSL_AUX_CLIENTAUTH_MODE ;
@@ -46,10 +51,12 @@ public class GrpcHelpers {
4651    protected  static  final  Map <String , Object > CLIENT_AUTH_OPT  = Map .of (SECURITY_SSL_AUX_CLIENTAUTH_MODE .getConcreteSettingForNamespace (GRPC_SECURE_TRANSPORT_SETTING_KEY ).getKey (), ClientAuth .OPTIONAL .name ());
4752    protected  static  final  Map <String , Object > CLIENT_AUTH_REQUIRE  = Map .of (SECURITY_SSL_AUX_CLIENTAUTH_MODE .getConcreteSettingForNamespace (GRPC_SECURE_TRANSPORT_SETTING_KEY ).getKey (), ClientAuth .REQUIRE .name ());
4853
54+     private  static  final  PortsRange  PORTS_RANGE  = new  PortsRange ("9400-9500" );
55+     private  static  final  String  HOST_ADDR  = "127.0.0.1" ;
4956    private  static  final  Map <String , Object > SECURE_GRPC_TRANSPORT_SETTINGS  = Map .of (
5057            ConfigConstants .SECURITY_SSL_ONLY , true ,
5158            AUX_TRANSPORT_TYPES_KEY , GRPC_SECURE_TRANSPORT_SETTING_KEY ,
52-             SETTING_GRPC_SECURE_PORT .getKey (), "9400-9500" ,
59+             SETTING_GRPC_SECURE_PORT .getKey (), PORTS_RANGE . getPortRangeString () ,
5360            SECURITY_SSL_AUX_ENABLED .getConcreteSettingForNamespace (GRPC_SECURE_TRANSPORT_SETTING_KEY ).getKey (), true ,
5461            SECURITY_SSL_AUX_PEMKEY_FILEPATH .getConcreteSettingForNamespace (GRPC_SECURE_TRANSPORT_SETTING_KEY ).getKey (), TEST_CERTIFICATES .getNodeKey (0 , null ).getAbsolutePath (),
5562            SECURITY_SSL_AUX_PEMCERT_FILEPATH .getConcreteSettingForNamespace (GRPC_SECURE_TRANSPORT_SETTING_KEY ).getKey (), TEST_CERTIFICATES .getNodeCertificate (0 ).getAbsolutePath (),
@@ -66,19 +73,60 @@ public static LocalCluster.Builder baseGrpcCluster(){
6673                .sslOnly (true );
6774    }
6875
69-     public  static  void  createTestIndex (TestRestClient  client , String  index , long  numDocs ) {
70-         try  (client ) {
71-             client .put (index ).assertStatusCode (200 );
72-             for  (int  i  = 0 ; i  < numDocs ; i ++) {
73-                 String  docURI  = index  + "/_doc/"  + i ;
74-                 String  docBody  = "{\" field\" : \" doc "  + i  + " body\" }" ;
75-                 client .postJson (docURI , docBody )
76-                         .assertStatusCode (201 );
77-             }
76+     public  static  ManagedChannel  plaintextManagedChannel () throws  SSLException  {
77+         return  new  NettyGrpcChannelBuilder ()
78+                 .setAddress (new  TransportAddress (new  InetSocketAddress (HOST_ADDR , PORTS_RANGE .ports ()[0 ])))
79+                 .build ();
80+     }
81+ 
82+     public  static  ManagedChannel  noAuthChannel () throws  SSLException  {
83+         return  new  NettyGrpcChannelBuilder ()
84+                 .setAddress (new  TransportAddress (new  InetSocketAddress (HOST_ADDR , PORTS_RANGE .ports ()[0 ])))
85+                 .clientAuth (ClientAuth .NONE )
86+                 .build ();
87+     }
88+ 
89+     public  static  ManagedChannel  optAuthChannel () throws  SSLException  {
90+         return  new  NettyGrpcChannelBuilder ()
91+                 .setAddress (new  TransportAddress (new  InetSocketAddress (HOST_ADDR , PORTS_RANGE .ports ()[0 ])))
92+                 .clientAuth (ClientAuth .OPTIONAL )
93+                 .keyManager (
94+                         TEST_CERTIFICATES .getNodeKey (0 , null ).getAbsolutePath (),
95+                         TEST_CERTIFICATES .getNodeCertificate (0 ).getAbsolutePath ()
96+                 )
97+                 .build ();
98+     }
99+ 
100+     public  static  ManagedChannel  requireAuthChannel () throws  SSLException  {
101+         return  new  NettyGrpcChannelBuilder ()
102+                 .setAddress (new  TransportAddress (new  InetSocketAddress (HOST_ADDR , PORTS_RANGE .ports ()[0 ])))
103+                 .clientAuth (ClientAuth .REQUIRE )
104+                 .keyManager (
105+                         TEST_CERTIFICATES .getNodeKey (0 , null ).getAbsolutePath (),
106+                         TEST_CERTIFICATES .getNodeCertificate (0 ).getAbsolutePath ()
107+                 )
108+                 .build ();
109+     }
110+ 
111+     public  static  BulkResponse  doBulk (ManagedChannel  channel , String  index , long  numDocs ) {
112+         BulkRequest .Builder  requestBuilder  = BulkRequest .newBuilder ();
113+         for  (int  i  = 0 ; i  < numDocs ; i ++) {
114+             String  docBody  = "{\" field\" : \" doc "  + i  + " body\" }" ;
115+             IndexOperation  indexOp  = IndexOperation .newBuilder ()
116+                     .setIndex (index )
117+                     .setId (String .valueOf (i ))
118+                     .build ();
119+             BulkRequestBody  requestBody  = BulkRequestBody .newBuilder ()
120+                     .setIndex (indexOp )
121+                     .setDoc (com .google .protobuf .ByteString .copyFromUtf8 (docBody ))
122+                     .build ();
123+             requestBuilder .addRequestBody (requestBody );
78124        }
125+         DocumentServiceGrpc .DocumentServiceBlockingStub  stub  = DocumentServiceGrpc .newBlockingStub (channel );
126+         return  stub .bulk (requestBuilder .build ());
79127    }
80128
81-     protected  static  SearchResponse  grpcMatchAllQuery ( TestGrpcClient   client , String  index , int  size ) {
129+     public  static  SearchResponse  doMatchAll ( ManagedChannel   channel , String  index , int  size ) {
82130        QueryContainer  query  = QueryContainer .newBuilder ()
83131                .setMatchAll (MatchAllQuery .newBuilder ().build ())
84132                .build ();
@@ -91,6 +139,7 @@ protected static SearchResponse grpcMatchAllQuery(TestGrpcClient client, String
91139                .addIndex (index )
92140                .setRequestBody (requestBody )
93141                .build ();
94-         return  client .search (searchRequest );
142+         SearchServiceGrpc .SearchServiceBlockingStub  stub  = SearchServiceGrpc .newBlockingStub (channel );
143+         return  stub .search (searchRequest );
95144    }
96145}
0 commit comments