Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.egg-info
*.pyc
13 changes: 9 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ python:
- 3.4
- 3.5
- 3.6
- 3.7-dev
- 3.7
- 3.8
- 3.9

before_install:
- python --version
- pip install -U pip
- pip install -U pytest
install:
- pip install cryptography

- pip install ".[test]" . # install package + test dependencies
script:
- MYSQL_TEST_LOGIN_FILE=`pwd`/test.mylogin.cnf python myloginpath.py
- pytest
18 changes: 15 additions & 3 deletions myloginpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def parse(login_path: str, path=None) -> dict:
)
parser.read_string(read(path), source=path)
data = dict(parser.items(login_path))
if 'port' in data:
data['port'] = int(data['port'])
data = {key: _strip_quotes(value) for key, value in data.items()}
if "port" in data:
data["port"] = int(data["port"])
return data


Expand Down Expand Up @@ -92,7 +93,7 @@ def _read_encrypted_file(f) -> bytes:
length_buffer = f.read(_CIPHER_STORE_LENGTH)
if len(length_buffer) < _CIPHER_STORE_LENGTH:
break
line_length, = struct.unpack("<i", length_buffer)
(line_length,) = struct.unpack("<i", length_buffer)
line = _read_line(f, line_length, decryptor)
plaintext += line

Expand Down Expand Up @@ -121,6 +122,17 @@ def _remove_pad(line):
return line[:-pad_length]


def _strip_quotes(value):
"""If a value is quoted, remove the quotes at the beginning and end, then
un-escape any quote characters inside the string."""
if value.startswith('"') and value.endswith('"'):
# This is a quoted string. Remove the first and
# last quote, then unescape interior quotes.
value = value[1:-1]
value = value.replace('\\"', '"')
return value


if __name__ == "__main__":
print(read())
print(parse("test"))
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from setuptools import setup
import pathlib

readme = pathlib.Path("README.md").read_text("utf-8")
with open("README.md", "rt") as f:
readme = f.read()

setup(
name="myloginpath",
Expand All @@ -13,6 +13,6 @@
url="https://github.com/PyMySQL/myloginpath/",
keywords="MySQL",
install_requires=["cryptography"],
python_requires='>=3.4',
python_requires=">=3.4",
py_modules=["myloginpath"],
)
File renamed without changes.
34 changes: 34 additions & 0 deletions test/test_decrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
""" Read each test vector, decrypt, and check that they're correct."""
import myloginpath


def test_normal():
expected = """[test]
user = testuser
password = testpass
host = testhost
socket = testsocket
port = 1234
"""
actual = myloginpath.read("test/test.mylogin.cnf")
assert expected == actual


def test_quoted():
expected = """[test]
user = "testuser"
password = "testpass"
host = "testhost"
socket = "testsocket"
port = 1234
"""
actual = myloginpath.read("test/test_quoted.mylogin.cnf")
assert expected == actual


def test_special():
expected = """[test]
host = "host with \\" quote"
"""
actual = myloginpath.read("test/test_special.mylogin.cnf")
assert expected == actual
32 changes: 32 additions & 0 deletions test/test_parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
""" Parse each test vector, and compare them to known-good values. """
import myloginpath


def test_normal():
expected = {
"host": "testhost",
"password": "testpass",
"port": 1234,
"socket": "testsocket",
"user": "testuser",
}
actual = myloginpath.parse("test", "test/test.mylogin.cnf")
assert expected == actual


def test_quoted():
expected = {
"host": "testhost",
"password": "testpass",
"port": 1234,
"socket": "testsocket",
"user": "testuser",
}
actual = myloginpath.parse("test", "test/test_quoted.mylogin.cnf")
assert expected == actual


def test_special():
expected = {"host": 'host with " quote'}
actual = myloginpath.parse("test", "test/test_special.mylogin.cnf")
assert expected == actual
Binary file added test/test_quoted.mylogin.cnf
Binary file not shown.
Binary file added test/test_special.mylogin.cnf
Binary file not shown.