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

feat: Expose different hooks dynamically to inject different database #29281

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

wugeer
Copy link

@wugeer wugeer commented Jun 18, 2024

This environment configuration setting hook allows administrators to alter different database connection parameters on the fly based on user information and hook strategy. This can be use for a variety of purposes:

  • rewire a subset of users to use different database user accounts,for example, integrating with ldap
  • pass user related information to the database for logging or QoS purposes
  • custom, per-database-engine, environment-specific impersonation

SUMMARY

Although there is already a DB_CONNECTION_MUTATOR hook available to inject database connection logic on the fly, if we do not modify the code in superset/models/core.py within the Database.get_sqla_engine method, this injection will affect all data source connections. Specifically, if different data sources require different injection logic, then we need to make different modifications to the code within superset/models/core.py to handle the different database types. Therefore, I have added a DB_CONNECTION_MODIFIER hook (clearly, DB_CONNECTION_MUTATOR would be a better name, but it is already in use, so I used a synonym instead), which allows for different data sources to implement different injection logic on the fly, as long as these database injection logic classes implement the run method of the ImpalaURLModifier class. This increases the flexibility of injecting database connection logic on the fly.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

For example, when each user connects to the postgres data source, their use the current username to connect to the database, and the password is new_password_ str plus the username.
Add a new class PostgresDBConnectModifier in file superset/utils/database_connect_modifier.py

class PostgresDBConnectModifier(BaseDBConnectModifier):
    # When connecting to a postgres data source,
    # replace the default connection username and password

    @classmethod
    def run(cls, sqlalchemy_url: URL, params: dict[str, Any], username: str, *args: Any,
            **kwargs: Any) -> (URL, dict[str, Any]):
        new_password = cls._get_new_password(username)
        sqlalchemy_url.username = username
        sqlalchemy_url.password = new_password
        return sqlalchemy_url, params

    @staticmethod
    def _get_new_password(username):
        # Implement password generation logic
        return 'new_password_' + username

superset/config.py中

DB_CONNECTION_MODIFIER_ENABLED = True
DB_CONNECTION_MODIFIER: dict[str, type[BaseDBConnectModifier]] = {
    "postgresql": PostgresDBConnectModifier,
}

The user logs in to the superset page and queries the postgres database table in sqllab; or the underlying data table triggered by the chart page is the postgres data source.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@dosubot dosubot bot added data:connect Namespace | Anything related to db connections / integrations enhancement:db Suggest new DB connections labels Jun 18, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congrats on making your first PR and thank you for contributing to Superset! 🎉 ❤️

We hope to see you in our Slack community too! Not signed up? Use our Slack App to self-register.

@wugeer
Copy link
Author

wugeer commented Jul 5, 2024

Hello, I found that all the checks related to docker pull failed. Is there anything I need to do?

@sfirke
Copy link
Member

sfirke commented Jul 16, 2024

Are you able to start by fixing the failed tests in the details of the pre-commit check?

@wugeer
Copy link
Author

wugeer commented Jul 17, 2024

Are you able to start by fixing the failed tests in the details of the pre-commit check?

Yeah,I fix the pre-commit hook errros and pushed the submission again! thanks you for help! :)

Copy link

codecov bot commented Jul 17, 2024

Codecov Report

Attention: Patch coverage is 81.25000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 83.70%. Comparing base (76d897e) to head (a97a76f).
Report is 482 commits behind head on master.

Files Patch % Lines
superset/models/core.py 66.66% 2 Missing ⚠️
superset/utils/database_connect_modifier.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #29281       +/-   ##
===========================================
+ Coverage   60.48%   83.70%   +23.21%     
===========================================
  Files        1931      527     -1404     
  Lines       76236    37937    -38299     
  Branches     8568        0     -8568     
===========================================
- Hits        46114    31754    -14360     
+ Misses      28017     6183    -21834     
+ Partials     2105        0     -2105     
Flag Coverage Δ
hive 49.12% <81.25%> (-0.04%) ⬇️
javascript ?
mysql 76.90% <81.25%> (?)
postgres 77.00% <81.25%> (?)
presto 53.68% <81.25%> (-0.13%) ⬇️
python 83.70% <81.25%> (+20.21%) ⬆️
sqlite 76.48% <81.25%> (?)
unit 59.90% <81.25%> (+2.28%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

wugeer and others added 2 commits July 18, 2024 09:14
add license info

Co-authored-by: Evan Rusackas <evan@preset.io>
@wugeer wugeer requested a review from rusackas July 20, 2024 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
data:connect Namespace | Anything related to db connections / integrations enhancement:db Suggest new DB connections size/L
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants