Skip to content

Flyway TLS Configuration Issue During MongoDB Migration Setup #4180

@Naveenkumar8055

Description

@Naveenkumar8055

Hi Team,
Could someone please take a look at this? I’m not sure what I might be missing, and I would appreciate guidance on the steps or details required to resolve this issue

When running the flyway/flyway:latest-alpine-mongo//flyway:11.17.0-alpine-mongo Docker image to perform migrations against a MongoDB replica set using TLS/SSL with custom CA certificates (mTLS configuration attempted initially), the connection repeatedly times out with a MongoSocketReadException: Prematurely reached end of stream. This occurs even after successfully importing the CA certificate into the JVM trust store via keytool within the container runtime. The core issue seems to be an abrupt connection drop during the TLS handshake phase, potentially related to driver configuration or network handling within the Alpine container environment

flyway docker images used :

  1. flyway:latest-alpine-mongo
  2. flyway:11.17.0-alpine-mongo
  • tried connecting to the single, standalone instance ( without overriding the ssl/tls certificates )
sudo docker run --rm \
  --network="host" \
  -v /home/user/migrations:/flyway/sql \
  -v /tmp/certs:/tmp/certs \
  flyway/flyway:latest-alpine-mongo \
  -url="mongodb://master:19051/dbName?tls=true&tlsCertificateKeyFile=/tmp/certs/client.pem&tlsCAFile=/tmp/certs/ca.crt&authSource=admin" \
  -user="user" \
  -password="passwordl" \
  -locations="filesystem:/flyway/sql" \
  -sqlMigrationSuffixes=".js" \
  migrate
  • first import the certificate and then execute Flyway ( this is also tried but same issue seen).
  • tried this command which overrides the container's default startup command to use bash instead, allowing us to run the keytool and the flyway command sequentially.
docker run --rm \
    --entrypoint bash \
    -v $(pwd)/mongo_migrations:/flyway/sql \
    -v $(pwd)/certs:/flyway/certs \
    flyway/flyway:11.17.0-alpine-mongo \
    -c "
        # 1. Import the CA certificate into Java's truststore
        keytool -import -trustcacerts -storepass changeit -noprompt \
                -alias mongo_ca -file /flyway/certs/ca.crt;

        # 2. Run the Flyway migration command
        flyway \
            -url='mongodb://host1:19051,host2:19051,host3:19051/mydb?replicaSet=myReplicaSetName&tls=true' \
            -user='yourUsername' \
            -password='yourPassword' \
            -locations='filesystem:/flyway/sql' \
            -sqlMigrationSuffixes='.js' \
            migrate
    "
  • Getting ssl handshake issue while connecting to the mongodb ( RSA architecure).

error msg :

Image

ERROR: Timed out while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=master_host:19051, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: (certificate_unknown) PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}, {address=slave_host:19051, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: (certificate_unknown) PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}, {address=arbiter_host:19051, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: (certificate_unknown) PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}, caused by {sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target}}]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions