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: support 'chevron' library for templating as jinja alternative #11617

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ def get_git_sha():
"teradata": ["sqlalchemy-teradata==0.9.0.dev0"],
"thumbnails": ["Pillow>=7.0.0, <8.0.0"],
"vertica": ["sqlalchemy-vertica-python>=0.5.9, < 0.6"],
"chevron": ["chevron==0.13.1"],
},
python_requires="~=3.6",
author="Apache Software Foundation",
Expand Down
1 change: 1 addition & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ def _try_json_readsha( # pylint: disable=unused-argument
"CLIENT_CACHE": False,
"ENABLE_EXPLORE_JSON_CSRF_PROTECTION": False,
"ENABLE_TEMPLATE_PROCESSING": False,
"CHEVRON_TEMPLATE_PROCESSING": False,
"KV_STORE": False,
"PRESTO_EXPAND_DATA": False,
# Exposes API endpoint to compute thumbnails
Expand Down
11 changes: 11 additions & 0 deletions superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,15 @@ def process_template(self, sql: str, **kwargs: Any) -> str:
return sql


class ChevronTemplateProcessor(
BaseTemplateProcessor
): # pylint: disable=too-few-public-methods
def process_template(self, sql: str, **kwargs: Any) -> str:
import chevron # late import for optional dependency

return chevron.render(sql, kwargs)


class PrestoTemplateProcessor(BaseTemplateProcessor):
"""Presto Jinja context

Expand Down Expand Up @@ -338,6 +347,8 @@ def get_template_processor(
template_processor = template_processors.get(
database.backend, BaseTemplateProcessor
)
elif feature_flag_manager.is_feature_enabled("CHEVRON_TEMPLATE_PROCESSING"):
Copy link
Member

Choose a reason for hiding this comment

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

Might be better to nest this conditional under if ENABLE_TEMPLATE_PROCESSING, sort of as a sub option after the user turns tempate processing on

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should rename ENABLE_TEMPLATE_PROCESSING to JINJA_TEMPLATE_PROCESSING instead (albeit a breaking change)?

Copy link
Member Author

@mistercrunch mistercrunch Nov 9, 2020

Choose a reason for hiding this comment

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

Right, clearly there's some cleanup to be done here, but also need to maintain backward compatibility. I think that's the easy portion of this PR, the real question is around the viability of chevron as a secure templating backend.

As a solution to this particular topic though I'd like to add JINJA_TEMPLATE_PROCESSING as True by default and nest the condition under ENABLE_TEMPLATE_PROCESSING.

Copy link
Member

Choose a reason for hiding this comment

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

I would propose calling the feature flag TEMPLATE_PROCESSING, and then adding a config parameter TEMPLATE_PROCESSOR: Optional[TemplateProcessorType] = TemplateProcessorType.CHEVRON. Similar to how other feature flagged options do (see e.g. THUMBNAIL_SELENIUM_USER, THUMBNAIL_CACHE_CONFIG etc).

template_processor = ChevronTemplateProcessor
else:
template_processor = NoOpTemplateProcessor
return template_processor(database=database, table=table, query=query, **kwargs)