Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize exports - remove characters that are not letters, numbers, or _ #35

Merged
merged 2 commits into from
Feb 15, 2023
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
17 changes: 16 additions & 1 deletion db_facts/exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,23 @@
}


def char_valid(char):
Brunope marked this conversation as resolved.
Show resolved Hide resolved
return char.isalnum() or char == '_'


def only_valid_chars(string):
Brunope marked this conversation as resolved.
Show resolved Hide resolved
return ''.join(filter(char_valid, string))


def format_key(key):
Brunope marked this conversation as resolved.
Show resolved Hide resolved
if key in formatted_key:
return formatted_key[key]

return only_valid_chars(key).upper()


def print_exports(exports):
for k, v in sorted(exports.items()):
env_var = formatted_key.get(k, k.upper())
env_var = format_key(k)
print("export " + env_var)
print(env_var + "=" + pipes.quote(str(v)))
3 changes: 3 additions & 0 deletions tests/test_runner_success.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_runner_lpass(self,
'port': 123,
'database': 'dbname',
'lastpass_share_name_suffix': 'lastpass_share_name_suffix',
'not-valid*_characters1': 'whatever',
'connection_type': 'connection_type',
}

Expand All @@ -38,6 +39,8 @@ def test_runner_lpass(self,
'export LASTPASS_SHARE_NAME_SUFFIX\n'
'LASTPASS_SHARE_NAME_SUFFIX='
'lastpass_share_name_suffix\n'
'export NOTVALID_CHARACTERS1\n'
'NOTVALID_CHARACTERS1=whatever\n'
'export DB_PASSWORD\n'
'DB_PASSWORD=password\n'
'export DB_PORT\n'
Expand Down