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

fix(QueryContext): validation does not validate query_context metrics #19753

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

ofekisr
Copy link
Contributor

@ofekisr ofekisr commented Apr 18, 2022

There is an option to add custom SQL expression metrics inside some of the charts visualizations so the fetched data will be aggregated based on the SQL expression.
When the data is fetched the query context itself is validated if the user can access the data
but the additional sql query added from the metric was missed out and not validated so it can be manipulated so now not allowed data will be fetched too.

The PR adds logic for it. right now only to those visualizations their data fetched by Charts API

follow-up PR's would be:

apply also to custom SQL column expressions

@ofekisr ofekisr force-pushed the fix/sql_metric_access branch from cf8be09 to 8fbce8e Compare April 18, 2022 11:15
@ofekisr ofekisr closed this Apr 18, 2022
@ofekisr ofekisr deleted the fix/sql_metric_access branch April 18, 2022 14:53
@ofekisr ofekisr reopened this Apr 18, 2022
@ofekisr ofekisr restored the fix/sql_metric_access branch April 18, 2022 14:55
@ofekisr ofekisr closed this Apr 18, 2022
@ofekisr ofekisr reopened this Apr 18, 2022
@ofekisr ofekisr force-pushed the fix/sql_metric_access branch from 8fbce8e to b2db382 Compare April 18, 2022 14:58
@ofekisr ofekisr closed this Apr 18, 2022
@ofekisr ofekisr force-pushed the fix/sql_metric_access branch from b2db382 to cf51459 Compare April 18, 2022 15:04
@ofekisr ofekisr reopened this Apr 18, 2022
@ofekisr ofekisr force-pushed the fix/sql_metric_access branch from f382cc8 to 913e4f1 Compare April 18, 2022 15:11
@codecov
Copy link

codecov bot commented Apr 18, 2022

Codecov Report

Attention: Patch coverage is 86.09865% with 31 lines in your changes missing coverage. Please review.

Project coverage is 66.58%. Comparing base (3c28cd4) to head (19f8874).
Report is 4280 commits behind head on master.

Files Patch % Lines
...data/query_context_validators/access_validators.py 87.65% 10 Missing ⚠️
superset/common/query_context.py 77.27% 5 Missing ⚠️
superset/charts/data/commands/command_factory.py 84.00% 4 Missing ⚠️
.../data/query_context_validators/validaor_factory.py 83.33% 4 Missing ⚠️
...t/charts/data/query_context_validators/__init__.py 77.77% 2 Missing ⚠️
...erset/charts/data/query_context_validators/impl.py 89.47% 2 Missing ⚠️
superset/common/query_object.py 60.00% 2 Missing ⚠️
superset/charts/data/api.py 85.71% 1 Missing ⚠️
superset/charts/data/commands/get_data_command.py 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #19753      +/-   ##
==========================================
+ Coverage   66.53%   66.58%   +0.05%     
==========================================
  Files        1692     1696       +4     
  Lines       64777    64988     +211     
  Branches     6660     6656       -4     
==========================================
+ Hits        43101    43275     +174     
- Misses      19977    20014      +37     
  Partials     1699     1699              
Flag Coverage Δ
hive 52.89% <77.57%> (+0.03%) ⬆️
mysql 81.92% <86.09%> (+0.01%) ⬆️
postgres 81.96% <86.09%> (+0.01%) ⬆️
presto 52.74% <77.57%> (+0.03%) ⬆️
python 82.38% <86.09%> (+<0.01%) ⬆️
sqlite 81.73% <86.09%> (+0.01%) ⬆️
unit 47.79% <67.26%> (-0.14%) ⬇️

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.

@ofekisr ofekisr force-pushed the fix/sql_metric_access branch from d34bcb5 to f33a120 Compare April 18, 2022 21:32
@ofekisr
Copy link
Contributor Author

ofekisr commented Apr 19, 2022

@AAfghahi
please review why your test is failed since #19700
I don't find why

@ofekisr
Copy link
Contributor Author

ofekisr commented Apr 19, 2022

@AAfghahi told me to change back the excepted result of the failed test until he will figure it out.
instead of it, I mark the test to be skipped

@ofekisr ofekisr force-pushed the fix/sql_metric_access branch from 3e7fdbd to 7be5bf2 Compare April 19, 2022 17:49
@ofekisr ofekisr force-pushed the fix/sql_metric_access branch from 7be5bf2 to 7ee4698 Compare April 20, 2022 12:18
@ofekisr ofekisr requested a review from amitmiran137 April 20, 2022 14:47
@ofekisr ofekisr force-pushed the fix/sql_metric_access branch from 7613eb7 to 6ba9a35 Compare April 20, 2022 17:11
@amitmiran137 amitmiran137 requested a review from a team April 24, 2022 14:00
@hughhhh hughhhh requested a review from betodealmeida April 26, 2022 16:45
return QueryContextValidatorWrapper()

def _make_access_validator(self, is_sql_db: bool) -> QueryContextValidator:
if is_sql_db:
Copy link
Member

Choose a reason for hiding this comment

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

are you checking here that the db is sql and not no-sql Druid? If so, we've removed native Druid support in 2.0.


def _make(self, query_context: QueryContext) -> BaseCommand:
validator = self._validator_factory.make(self._is_use_sql_db(query_context))
return ChartDataCommand(query_context, validator)
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if this factory abstraction is necessary for the validation or if this is something we can just put into the command?

from superset.commands.base import BaseCommand
from superset.common.query_context import QueryContext

from ..query_context_validators.validaor_factory import QueryContextValidatorFactory
Copy link
Member

@dpgaspar dpgaspar May 18, 2022

Choose a reason for hiding this comment

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

We avoid using relative imports
there's seems to be a typo also: validaor_factory

Comment on lines +35 to +45
@classmethod
def init(cls, validator_factory: QueryContextValidatorFactory) -> None:
cls._instance = GetChartDataCommandFactory(validator_factory)

def __init__(self, validator_factory: QueryContextValidatorFactory):
self._validator_factory = validator_factory

@classmethod
def make(cls, query_context: QueryContext) -> BaseCommand:
if cls._instance is None:
raise RuntimeError("GetChartDataCommandFactory was not initialized")
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need to make so many constructors?

@rusackas
Copy link
Member

Hi @ofekisr - just checking in to see if you're still able/willing to get this across the finish line. I'll convert it to draft while it awaits a rebase and responses to comments. Thanks!

@rusackas rusackas marked this pull request as draft August 20, 2024 02:50
@ofekisr
Copy link
Contributor Author

ofekisr commented Aug 20, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants