Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SSL certificate in Karate-gRPC. Caught exception while waiting for rpc #19

Open
debayandesarkar opened this issue Jun 8, 2021 · 6 comments

Comments

@debayandesarkar
Copy link

Hi,
Thanks for sharing this implementation. I am trying to automate a gRPC service call following the example documented here. My feature file looks like below

Feature: DeterminationAPI GetTax

  Background:
    * def Client = Java.type('com.github.thinkerou.karate.GrpcClient')
    * def client = Client.create('eg-gtp-tax-engine-adapter-mvp-grpc.kmc-default.us-west-2.test.beardsley.abc.com',443)

  Scenario: do it
    * def payload = read('getTax.json')
    * def response = client.call('com.abc.tax.amounts.v1beta1.DeterminationAPI/getTax', payload)
    * def response = JSON.parse(response)
    * print response

I have included the jar file which has the .proto file of the gRPC service (e.g. com.abc.tax.amounts.v1beta1.DeterminationAPI). However, I am getting the below error.

Jun 08, 2021 12:01:51 AM com.github.thinkerou.demo.helloworld.HelloWorldServerMain start
INFO: Server started listening on 50051
00:01:51.182 [main] WARN  testing.ServerStart - Started server on port...Jun 08, 2021 12:01:51 AM com.github.thinkerou.karate.service.GrpcCall excute
WARNING: Call grpc failed, maybe you should see the follow grpc information.
Jun 08, 2021 12:01:51 AM com.github.thinkerou.karate.service.GrpcCall excute
INFO: []

com.intuit.karate.exception.KarateException: determinationAPI.feature:8 - javascript evaluation failed: client.call('com.abc.tax.amounts.v1beta1.DeterminationAPI/getTax', payload), Can't find service with name: DeterminationAPI

	at ✽.* def response = client.call('com.abc.tax.amounts.v1beta1.DeterminationAPI/getTax', payload) (determinationAPI.feature:8)


Can you please help troubleshooting this issue?

@s3naid
Copy link

s3naid commented Jun 10, 2021

If I remember right, I had similar error. Make sure Your have this:
/.karate-grpc/protobuf-descriptor-sets/karate-grpc.protobin

This file is generated using Your proto files located in:
<protoSourceRoot>${project.build.directory}/dependency/demo</protoSourceRoot>

@debayandesarkar
Copy link
Author

debayandesarkar commented Jun 10, 2021

@s3naid Thanks for your response. I updated the protoSourceRoot to the path where all the .proto files are located for the project and was able to compile them successfully. With that , I got past the error Can't find service with name: DeterminationAPI. However, the service uses a .pem certificate for the API call and since I haven't added the certificate in the karate.feature file, possibly getting the below error now.

Jun 10, 2021 10:20:05 AM com.github.thinkerou.demo.helloworld.HelloWorldServerMain start
INFO: Server started listening on 50051
10:20:05.655 [main] WARN  testing.ServerStart - Started server on port...
com.intuit.karate.exception.KarateException: determinationAPI.feature:8 - javascript evaluation failed: client.call('abc.tax.amounts.v1beta1.DeterminationAPI/GetTax', payload), Caught exception while waiting for rpc

	at ✽.* def response = client.call('abc.tax.amounts.v1beta1.DeterminationAPI/GetTax', payload) (determinationAPI.feature:8)

Is there a way I can include the .pem certificate in the call? Please find below my feature file code.

Feature: DeterminationAPI GetTax
  Background:
    * def Client = Java.type('com.github.thinkerou.karate.GrpcClient')
    * def client = Client.create('eg-gtp-tax-engine-adapter-mvp-grpc.kmc-default.us-west-2.test.beardsley.abc.com', 443)

  Scenario: do it
    * def payload = read('getTax.json')
    * def response = client.call('abc.tax.amounts.v1beta1.DeterminationAPI/GetTax', payload)
    * def response = JSON.parse(response)
    * print response

**Proto files compiled successfully as shown below.**

[INFO] Compiling 744 proto file(s) to /opt/GitHub/karate-grpc-master/karate-grpc-helper/target/generated-sources/protobuf/java

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for karate-grpc-parent 1.0.3:
[INFO] 
[INFO] karate-grpc-parent ................................. SUCCESS [  0.335 s]
[INFO] karate-grpc-core ................................... SUCCESS [  7.018 s]
[INFO] karate-grpc-demo ................................... SUCCESS [  1.321 s]
[INFO] karate-grpc-helper ................................. SUCCESS [ 29.553 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  39.694 s
[INFO] Finished at: 2021-06-10T10:13:49-07:00

Regards
Debayan

@s3naid
Copy link

s3naid commented Jun 11, 2021

@debayandesarkar Check karate dsl netty, it is possible to configure ssl certificate
[https://intuit.github.io/karate/karate-netty/#mock-server]

I didnt use this so I cant help You further, but if You manage to do it pls. post your solution here

@debayandesarkar
Copy link
Author

debayandesarkar commented Jun 11, 2021

@s3naid I asked this question about using configure ssl certificate here https://stackoverflow.com/questions/67882647/how-to-configure-a-pem-certificate-in-karate-feature-file?noredirect=1#comment119988730_67882647. This won't work for karate-grpc. We checked the implementation of kara-grpc core and looks like the implementation doesn't support SSL .

Refer https://github.com/pecker-io/karate-grpc/blob/master/karate-grpc-core/src/main/java/com/github/thinkerou/karate/grpc/ChannelFactory.java which uses .usePlaintext().build();

We tried to modify the method in this class as below to include the SSL certificate. However, getting error in calling the service.

public final class ChannelFactory {

    public static ManagedChannel create(String host, int port) {
        try {
            System.out.println("start loading the certification file");
            File certFile = new File("/opt/GitHub/karate-grpc-master/devK8sCert.pem");
            ManagedChannel channel2 = NettyChannelBuilder.forAddress(host, port)
                    .useTransportSecurity()
                    .sslContext(GrpcSslContexts.forClient().trustManager(certFile)
                            .build())
                    .build();
            System.out.println("finish loading the certification file");
            return channel2;

        } catch (SSLException ex) {

        }
        return null;
    }
}

Exception:

start loading the certification file
finish loading the certification file

com.intuit.karate.exception.KarateException: determinationAPI.feature:9 - javascript evaluation failed: client.call('abc.tax.amounts.v1beta1.TaxEngineAPI/GetTaxEngine', payload), Caught exception while waiting for rpc

	at ✽.* def response = client.call('abc.tax.amounts.v1beta1.TaxEngineAPI/GetTaxEngine', payload) (determinationAPI.feature:9)

@debayandesarkar debayandesarkar changed the title Can't find service with name Caught exception while waiting for rpc Jun 11, 2021
@debayandesarkar debayandesarkar changed the title Caught exception while waiting for rpc Using SSL certificate in Karate-gRPC. Caught exception while waiting for rpc Jun 11, 2021
@s3naid
Copy link

s3naid commented Jun 14, 2021

@debayandesarkar
I am not sure I can help with that. I see You added System.out.println("finish loading the certification file"); and this is printed in the logs. Try to add more logs on println statements inside karate-grpc to see where exactly your call fails.

Also one more note, karate-grpc is not compatible with latest version of karate. You can use 0.9.6 karate version. I have incorporated karate and karate-grpc into my own code, so I cant easily share my code. But try 0.9.6 and if it works then You have to fix karate-grpc to make it compatible with karate 1.0.1.

@dev-rgupta
Copy link

dev-rgupta commented Jan 19, 2022

Hi @debayandesarkar,
I was also facing the same issue when I was using the default implementation of ChannelFactory like below

com.test.provider.feature:10 - javascript evaluation failed: client.call('com.xyz..ProviderAPI/RetrieveInfo',payload), Caught exception while waiting for rpc

and my API communication needs a cert so used NettyChannelBuilder and pass the cert and than my problem resolved.

public static ManagedChannel createSSLChannel(String host, int port) {
ManagedChannel managedChannel=null;
try {
managedChannel = NettyChannelBuilder.forAddress(host, port)
.sslContext(GrpcSslContexts.forClient().trustManager(new File("src/test/resources/Cert.pem")).build())
.keepAliveWithoutCalls(true)
.build();
} catch (SSLException e) {
e.printStackTrace();
}
return managedChannel;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants