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

As a Metric user I want opendata DB initialization to not fail #34

Closed
VitaliStupin opened this issue Mar 9, 2023 · 4 comments · Fixed by #35
Closed

As a Metric user I want opendata DB initialization to not fail #34

VitaliStupin opened this issue Mar 9, 2023 · 4 comments · Fixed by #35

Comments

@VitaliStupin
Copy link
Contributor

VitaliStupin commented Mar 9, 2023

Currently anonymizer fails to initialize empty database because of typo made while refactoring code.

Software version:
1.1.0

Source:
https://github.com/nordic-institute/X-Road-Metrics/blob/develop/anonymizer_module/opmon_anonymizer/iio/postgresql_manager.py#L112

Error message:

Failed initializing postgresql database connector.
ERROR: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/opmon_anonymizer/main.py", line 93, in setup_writer
    writer = OpenDataWriter(settings, logger)  
File "/usr/lib/python3/dist-packages/opmon_anonymizer/iio/opendata_writer.py", line 42, in __init__
    self.db_manager = PostgreSqlManager(settings['postgres'], schema, index_columns, logger)
File "/usr/lib/python3/dist-packages/opmon_anonymizer/iio/postgresql_manager.py", line 43, in __init__
    self._ensure_table(table_schema, index_columns)
File "/usr/lib/python3/dist-packages/opmon_anonymizer/iio/postgresql_manager.py", line 95, in _ensure_table
    if not self._table_exists(cursor):  
File "/usr/lib/python3/dist-packages/opmon_anonymizer/iio/postgresql_manager.py", line 112, in _table_exists
    """, (cursor._table_name,))
AttributeError: 'psycopg2.extensions.cursor' object has no attribute '_table_name'

Solution:
cursor._table_name should be renamed back to self._table_name as it was before change:
65a1357#diff-2de518dd9301590a33d32a07fdcdf45bcaba485b18e61d65f29e5f8dcfd0b3e5L110

Additional problems:

Source:

"GRANT USAGE ON SCHEMA public TO %s;", (readonly_user,)

"GRANT SELECT ON %s TO %s;", (self._table_name, readonly_user)

Notation "%s" should be used for escaping data, not Identifiers like table names and user names. The constructed SQL just silently fails because of except Exception: pass:

GRANT USAGE ON SCHEMA public TO 'opendata_inst';
GRANT SELECT ON 'logs' TO 'opendata_inst';

Solution:
Identifiers should be properly escaped: https://www.psycopg.org/docs/sql.html#module-usage
If possible exception should not "pass" on all errors.

The same problem with identifiers occurs in other places, for example it is not possible to display opendata due to SQL error because of the bug in code:

"SELECT min(requestindate), max(requestindate) FROM %s;", (self._table_name,)

@raits
Copy link
Contributor

raits commented Mar 10, 2023

Thank you for reporting these to us. We will look into them and release a hotfix.

@wisecrow
Copy link
Contributor

Thanks @VitaliStupin Fixing it.

@wisecrow
Copy link
Contributor

#35

@raits raits linked a pull request Mar 13, 2023 that will close this issue
@raits
Copy link
Contributor

raits commented May 26, 2023

This was fixed with v1.1.1.

@raits raits closed this as completed May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants