diff --git a/changelog.md b/changelog.md index c51d4e9c..c5f3c1b3 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,7 @@ Features * Let the `--dsn` argument accept literal DSNs as well as aliases. * Accept `--character-set` as an alias for `--charset` at the CLI. * Add SSL/TLS version to `status` output. +* Accept new-style `ssl_mode` in DSN URI query parameters, to match CLI argument. Bug Fixes diff --git a/mycli/main.py b/mycli/main.py index a3899fe0..ce8c6208 100755 --- a/mycli/main.py +++ b/mycli/main.py @@ -1874,7 +1874,7 @@ def get_password_from_file(password_file: str | None) -> str | None: if ssl_enable is not None: click.secho( "Warning: The --ssl/--no-ssl CLI options are deprecated and will be removed in a future release. " - "Please use the ssl_mode config or --ssl-mode CLI options instead. " + "Please use the \"default_ssl_mode\" config option or --ssl-mode CLI flag instead. " "See issue https://github.com/dbcli/mycli/issues/1507", err=True, fg="yellow", @@ -1971,28 +1971,38 @@ def get_password_from_file(password_file: str | None) -> str | None: dsn_params = {} if params := dsn_params.get('ssl'): - ssl_enable = ssl_enable or (params[0].lower() == 'true') + click.secho( + 'Warning: The "ssl" DSN URI parameter is deprecated and will be removed in a future release. ' + 'Please use the "ssl_mode" parameter instead. ' + 'See issue https://github.com/dbcli/mycli/issues/1507', + err=True, + fg='yellow', + ) + if params[0].lower() == 'true': + ssl_mode = 'on' + if params := dsn_params.get('ssl_mode'): + ssl_mode = ssl_mode or params[0] if params := dsn_params.get('ssl_ca'): ssl_ca = ssl_ca or params[0] - ssl_enable = True + ssl_mode = ssl_mode or 'on' if params := dsn_params.get('ssl_capath'): ssl_capath = ssl_capath or params[0] - ssl_enable = True + ssl_mode = ssl_mode or 'on' if params := dsn_params.get('ssl_cert'): ssl_cert = ssl_cert or params[0] - ssl_enable = True + ssl_mode = ssl_mode or 'on' if params := dsn_params.get('ssl_key'): ssl_key = ssl_key or params[0] - ssl_enable = True + ssl_mode = ssl_mode or 'on' if params := dsn_params.get('ssl_cipher'): ssl_cipher = ssl_cipher or params[0] - ssl_enable = True + ssl_mode = ssl_mode or 'on' if params := dsn_params.get('tls_version'): tls_version = tls_version or params[0] - ssl_enable = True + ssl_mode = ssl_mode or 'on' if params := dsn_params.get('ssl_verify_server_cert'): ssl_verify_server_cert = ssl_verify_server_cert or (params[0].lower() == 'true') - ssl_enable = True + ssl_mode = ssl_mode or 'on' ssl_mode = ssl_mode or mycli.ssl_mode # cli option or config option diff --git a/test/test_main.py b/test/test_main.py index 1415f598..c51a9102 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -882,7 +882,7 @@ def run_query(self, query, new_line=True): ) # Use a DSN with query parameters - result = runner.invoke(mycli.main.cli, args=["mysql://dsn_user:dsn_passwd@dsn_host:6/dsn_database?ssl=True"]) + result = runner.invoke(mycli.main.cli, args=["mysql://dsn_user:dsn_passwd@dsn_host:6/dsn_database?ssl_mode=off"]) assert result.exit_code == 0, result.output + " " + str(result.exception) assert ( MockMyCli.connect_args["user"] == "dsn_user" @@ -890,27 +890,25 @@ def run_query(self, query, new_line=True): and MockMyCli.connect_args["host"] == "dsn_host" and MockMyCli.connect_args["port"] == 6 and MockMyCli.connect_args["database"] == "dsn_database" - and MockMyCli.connect_args["ssl"]["enable"] is True + and MockMyCli.connect_args["ssl"] is None ) - # When a user uses a DSN with query parameters, and used command line - # arguments, use the command line arguments. + # When a user uses a DSN with query parameters, and also used command line + # arguments, prefer the command line arguments. result = runner.invoke( mycli.main.cli, args=[ - "mysql://dsn_user:dsn_passwd@dsn_host:6/dsn_database?ssl=False", - "--ssl", + 'mysql://dsn_user:dsn_passwd@dsn_host:6/dsn_database?ssl_mode=off', + '--ssl-mode=on', ], ) - assert result.exit_code == 0, result.output + " " + str(result.exception) - assert ( - MockMyCli.connect_args["user"] == "dsn_user" - and MockMyCli.connect_args["passwd"] == "dsn_passwd" - and MockMyCli.connect_args["host"] == "dsn_host" - and MockMyCli.connect_args["port"] == 6 - and MockMyCli.connect_args["database"] == "dsn_database" - and MockMyCli.connect_args["ssl"]["enable"] is True - ) + assert result.exit_code == 0, result.output + ' ' + str(result.exception) + assert MockMyCli.connect_args['user'] == 'dsn_user' + assert MockMyCli.connect_args['passwd'] == 'dsn_passwd' + assert MockMyCli.connect_args['host'] == 'dsn_host' + assert MockMyCli.connect_args['port'] == 6 + assert MockMyCli.connect_args['database'] == 'dsn_database' + assert MockMyCli.connect_args['ssl']['mode'] == 'on' # Accept a literal DSN with the --dsn flag (not only an alias) result = runner.invoke(