2

Not able to connect to Googe Cloud Spanner with Java code Getting SSLException error.

Written a code in java which tries to connect to Google Spanner I have already created the Spanner instance and also the database along with a table.

However when I try to run the Java code it gives error of getting error com.google.cloud.spanner.SpannerException: UNKNOWN: javax.net.ssl.SSLException: unable to setup trustmanager

try (InputStream credentialsStream = new FileInputStream(credentialsFile)){
   credentials = GoogleCredentials.fromStream(new FileInputStream(credentialsFile));
} catch (FileNotFoundException e) {
   e.printStackTrace();
} catch (IOException e) {
   e.printStackTrace();
}       
SpannerOptions options = SpannerOptions.newBuilder().setProjectId("coral-velocity-252502").build();
Spanner spanner = options.getService();
Stefan G.
  • 890
  • 5
  • 10
  • gtting this error stack flow com.google.cloud.spanner.SpannerException: UNKNOWN: javax.net.ssl.SSLException: unable to setup trustmanager at com.google.cloud.spanner.SpannerExceptionFactory.newSpannerExceptionPreformatted(SpannerExceptionFactory.java:151) at com.google.cloud.spanner.SpannerExceptionFactory.newSpannerException(SpannerExceptionFactory.java:45) at .. – Amit Nerkar Sep 12 '19 at 07:27
  • Can you add the full stack trace to the question please? Also do you need to use a proxy to access the internet? – RedPandaCurios Sep 12 '19 at 07:44
  • Also, could it be that you are using a 32-bit version of Windows? – Knut Olav Løite Sep 12 '19 at 07:50
  • Hi @RedPandaCurios no not using proxy. using my direct internet connection. – Amit Nerkar Sep 12 '19 at 07:54
  • Hi @KnutOlavLoite - its 64 bit windows 10 Enterprise. – Amit Nerkar Sep 12 '19 at 07:57
  • its giving the error at this line Spanner spanner = options.getService(); – Amit Nerkar Sep 12 '19 at 07:59
  • this is not a spanner error, but a Java SSL error - to see exactly where you need to post the full stack trace. Most likely there is a problem with Java's system certificate store, or your personal root certificate store. – RedPandaCurios Sep 12 '19 at 09:36
  • I agree this seems to be a Java SSL error, why don't you try to use a Cloud Function to connect to Spanner so we can confirm that the issue is in Java and not with your code. Maybe you're using an older Java version? I see that many others use a databaseID have a [look](https://pastebin.com/00y9JQa1). – ericcco Sep 12 '19 at 10:18
  • @RedPandaCurios - yes you are right. Its a java certificate issue. But the question is how to install the cert into java. Can you help in this. – Amit Nerkar Sep 12 '19 at 10:25
  • My first step would be to reinstall java. Second would be to try to create a simple java test program that just tries to get https://googleapis.com (you should get a 404, not a cert error). See this other SO question for ways of looking whats in your cert store: https://stackoverflow.com/questions/5871279/java-ssl-and-cert-keystore – RedPandaCurios Sep 13 '19 at 11:49

2 Answers2

1

Could you try the suggested setup guides here:

snehashah
  • 161
  • 3
0

Got Resolved!

The issue was i was using PowerMock to test the Google DB Spanner apis. Somehow the mock messes up with the SSL issues. The solution for that is to use an annotation on the test class:

@PowerMockIgnore("javax.net.ssl.*")

this is taken from When a TrustManagerFactory is not a TrustManagerFactory (Java)