How to solve the error: "Connection could not be allocated because the hostname could not be verified by hostnameverifier"

2.7k views Asked by At

When I try to make some request to my AWS EC2 instance I'm getting the error "Connection could not be allocated because my_dns_of_postgresql_database could not be verified by hostnameverifier PgjdbcHostnameVerifier", what could I do in this case?

I deployed a server in Glassfish 4.1 (a war file) in an EC2 instance of AWS (a standard one t2.micro). This project makes calls to Heroku's database server in postgreSQL (the database is not in the AWS instance). I've tried to change the security group rules to accept the postgreSQL traffic inbound and outbound, but this doesn't works.

When I don't make a request (just typing the dns address, port and war file name application - like:http://ec2-2-81-22-142.compute-1.amazonaws.com:8080/TamboServer-1.0-SNAPSHOT/) shows the Servlet response, a Hello World; and when I try it in my PC (localhost) I get a succefull response, so I don't think that it's a problem in the server application. Maybe some configuration in Heroku's database or EC2 instance.

Here are some lines of the server.logs file in Glassfish:

[2019-04-06T04:03:45.933+0000] [glassfish 4.1] [INFO] [] [] [tid: _ThreadID=30 _ThreadName=Thread-8] [timeMillis: 1554523425933] [levelValue: 800] [[
  [EL Info]: 2019-04-06 04:03:45.932--ServerSession(1545400102)--EclipseLink, version: Eclipse Persistence Services - 2.6.1.v20150605-31e8258]]

[2019-04-06T04:03:45.963+0000] [glassfish 4.1] [SEVERE] [] [org.postgresql.ssl.PGjdbcHostnameVerifier] [tid: _ThreadID=30 _ThreadName=http-listener-1(4)] [timeMillis: 1554523425963] [levelValue: 1000] [[
  Server name validation failed: hostname ec2-107-22-163-8.compute-1.amazonaws.com does not match common name ip-10-0-63-18.ec2.internal]]

[2019-04-06T04:03:45.963+0000] [glassfish 4.1] [WARNING] [poolmgr.create_resource_error] [javax.enterprise.resource.resourceadapter.com.sun.enterprise.resource.allocator] [tid: _ThreadID=30 _ThreadName=http-listener-1(4)] [timeMillis: 1554523425963] [levelValue: 900] [[
  RAR5038:Unexpected exception while creating resource for pool { PoolInfo : (name=java:app/post-gre-sql_...), (applicationName=TamboServer-1.0-SNAPSHOT) }. Exception : javax.resource.spi.ResourceAllocationException: Connection could not be allocated because: The hostname ec2-107-22-163-8.compute-1.amazonaws.com could not be verified by hostnameverifier PgjdbcHostnameVerifier.]]

I expect to be able to connect the server, running in a EC2 instance, to the Heroku's database (actually, it is allocated in an AWS machine too). If not, should I migrate the database to the EC2 instance? Thanks in advance and sorry if I have made some mistakes writting (I'm not a native english speaker)

1

There are 1 answers

0
sebastian_mp On

Currentyl I deleted the parameter ssl URL in my JDBC connection pool and added sslfactory=org.postgresql.ssl.NonValidatingFactory how sugest this response. This is a brute-force solution because allows "man in the middle" atacks, making it unsafe, how says the documentation, but it's okey for what I'm doing.