为 jdbc MYSQL 连接适配器提供证书给 Python jaydebeapi lib

问题描述 投票:0回答:1

我需要通过 python 连接到一个 JDBC 服务器。

使用 jaydebeapi lib,我能够启动一个非 ssl 连接,但是当连接到一个强制执行证书的暂存数据库时,我不明白如何提供它需要通过 jdbc 连接到 mysql 的 jks 文件。

依赖项:

python==3.9.16
JayDeBeApi==1.2.3
JPype1==1.4.1

代码:

import jpype
import jaydebeapi

MYSQL_VJDBC_HOST = ""
MYSQL_VJDBC_JAR_PATH = ""
MYSQL_VJDBC_CERT_PATH = ""
MYSQL_VJDBC_CERT_PW = ""
MYSQL_VJDBC_SECRET = ""


def get_jdbc_connection(self):
    url = f"jdbc:vjdbc:servlet:{MYSQL_VJDBC_HOST}"
    jvm_path = jpype.getDefaultJVMPath()
    jpype.startJVM(
        jvm_path,
        f"-Djava.class.path={MYSQL_VJDBC_JAR_PATH}",
        f"-Djavax.net.ssl.trustStore={MYSQL_VJDBC_CERT_PATH}",
        f"-Djavax.net.ssl.trustStorePassword={MYSQL_VJDBC_CERT_PW}",
    )
    try:
        return jaydebeapi.connect(
            "de.simplicit.vjdbc.VirtualDriver",
            url,
            {
                "user": "mysql",
                "password": MYSQL_VJDBC_SECRET,
                "secure": "true",
                # "cert_file": sMYSQL_VJDBC_CERT_PATH,
                # "cert": MYSQL_VJDBC_CERT_PATH,
            },
            MYSQL_VJDBC_JAR_PATH,
        )
    except Exception as exception:
        print(f"There was an error connecting\n:{exception}")
        raise exception

错误:

  File "*/src/mysql_adapter.py", line 29, in get_jdbc_connection
return jaydebeapi.connect(
  File "*/.venv/lib/python3.9/site-packages/jaydebeapi/__init__.py", line 412, in connect
jconn = _jdbc_connect(jclassname, url, driver_args, jars, libs)
  File "*/.venv/lib/python3.9/site-packages/jaydebeapi/__init__.py", line 230, in _jdbc_connect_jpype
return jpype.java.sql.DriverManager.getConnection(url, *dargs)
  java.sql.java.sql.SQLException: java.sql.SQLException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
python jdbc jks jaydebeapi
1个回答
0
投票

在 jaydebeapi lib 中,传递驱动程序参数。

在 VJDBC 中,“truststore”是缺少的参数,它是一个字符串路径。

需要进行一些挖掘,但希望这对不熟悉 Java 的用户有所帮助。

jaydebeapi.connect(
        "de.simplicit.vjdbc.VirtualDriver",
        url,
        {
            "user": "mysql",
            "password": MYSQL_VJDBC_SECRET,
            "secure": "true",
            "truststore": MYSQL_VJDBC_CERT_PATH,
        },
        MYSQL_VJDBC_JAR_PATH,
    )
© www.soinside.com 2019 - 2024. All rights reserved.