Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 19 additions & 9 deletions mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

Expand Down
28 changes: 13 additions & 15 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,35 +882,33 @@ 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"
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
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(
Expand Down