Skip to content

Commit

Permalink
feat(CLI command): Apache Superset "Factory Reset" CLI command #27207
Browse files Browse the repository at this point in the history
  • Loading branch information
KrishnaNadhManepalli committed May 31, 2024
1 parent a608bdb commit dacae1b
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
79 changes: 79 additions & 0 deletions superset/cli/reset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import sys
import click
from flask.cli import with_appcontext
from werkzeug.security import check_password_hash

Check warning on line 21 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L18-L21

Added lines #L18 - L21 were not covered by tests

from superset.cli.lib import feature_flags

Check warning on line 23 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L23

Added line #L23 was not covered by tests

if feature_flags.get("ENABLE_FACTORY_RESET_COMMAND"):

Check warning on line 25 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L25

Added line #L25 was not covered by tests

@click.command()
@with_appcontext
@click.option(

Check warning on line 29 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L27-L29

Added lines #L27 - L29 were not covered by tests
"--username",
prompt="Admin Username",
help="Admin Username"
)
@click.option(

Check warning on line 34 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L34

Added line #L34 was not covered by tests
"--silent",
is_flag=True,
prompt=(
"Are you sure you want to reset Superset? "
"This action cannot be undone. Continue?"),
help="Confirmation flag"
)
@click.option(

Check warning on line 42 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L42

Added line #L42 was not covered by tests
"--exclude-users",
default=None,
help="Comma separated list of users to exclude from reset",
)
@click.option(

Check warning on line 47 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L47

Added line #L47 was not covered by tests
"--exclude-roles",
default=None,
help="Comma separated list of roles to exclude from reset",
)
def factory_reset(

Check warning on line 52 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L52

Added line #L52 was not covered by tests
username: str,
silent: bool,
exclude_users: str,
exclude_roles: str
) -> None:
"""Factory Reset Apache Superset"""

# pylint: disable=import-outside-toplevel
from superset import security_manager
from superset.commands.security.reset import ResetSupersetCommand

Check warning on line 62 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L61-L62

Added lines #L61 - L62 were not covered by tests

# Validate the user
password = click.prompt('Admin Password', hide_input=True)
user = security_manager.find_user(username)
if not user or not check_password_hash(user.password, password):
click.secho('Invalid credentials', fg='red')
sys.exit(1)
if not any(role.name == 'Admin' for role in user.roles):
click.secho('Permission Denied', fg='red')
sys.exit(1)

Check warning on line 72 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L65-L72

Added lines #L65 - L72 were not covered by tests

try:
ResetSupersetCommand(silent, user, exclude_users, exclude_roles).run()
click.secho('Factory reset complete', fg='green')
except Exception as ex: # pylint: disable=broad-except
click.secho(f'Factory reset failed: {ex}', fg='red')
sys.exit(1)

Check warning on line 79 in superset/cli/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/cli/reset.py#L74-L79

Added lines #L74 - L79 were not covered by tests
90 changes: 90 additions & 0 deletions superset/commands/security/reset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import logging

Check warning on line 18 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L18

Added line #L18 was not covered by tests

from superset import db, security_manager
from superset.commands.base import BaseCommand
from superset.connectors.sqla.models import SqlaTable
from superset.key_value.models import KeyValueEntry
from superset.models.core import Database, FavStar, Log
from superset.models.dashboard import Dashboard
from superset.models.slice import Slice

Check warning on line 26 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L20-L26

Added lines #L20 - L26 were not covered by tests

logger = logging.getLogger(__name__)

Check warning on line 28 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L28

Added line #L28 was not covered by tests


class ResetSupersetCommand(BaseCommand):
def __init__(

Check warning on line 32 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L31-L32

Added lines #L31 - L32 were not covered by tests
self,
confirm: bool,
user,
exclude_users: str = None,
exclude_roles: str = None
) -> None:
self._user = user
self._confirm = confirm
self._users_to_exclude = ["admin"]
if exclude_users:
self._users_to_exclude.extend(exclude_users.split(','))
self._roles_to_exclude = ["Admin", "Public", "Gamma", "Alpha", "sql_lab"]
if exclude_roles:
self._roles_to_exclude.extend(exclude_roles.split(','))

Check warning on line 46 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L39-L46

Added lines #L39 - L46 were not covered by tests

def validate(self) -> None:
if not self._confirm:
raise Exception("Reset aborted.") # pylint: disable=broad-exception-raised
if not self._user or not self._user.is_active:
raise Exception("User not found.") # pylint: disable=broad-exception-raised

Check warning on line 52 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L48-L52

Added lines #L48 - L52 were not covered by tests

def run(self) -> None:
self.validate()
logger.debug("Resetting Superset Started")
db.session.query(SqlaTable).delete()
databases = db.session.query(Database)
for database in databases:
db.session.delete(database)
db.session.query(Dashboard).delete()
db.session.query(Slice).delete()
db.session.query(KeyValueEntry).delete()
db.session.query(Log).delete()
db.session.query(FavStar).delete()

Check warning on line 65 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L54-L65

Added lines #L54 - L65 were not covered by tests

logger.debug("Ignoring Users: %s", self._users_to_exclude)
users_to_delete = db.session.query(security_manager.user_model).filter(

Check warning on line 68 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L67-L68

Added lines #L67 - L68 were not covered by tests
security_manager.user_model.username.not_in(self._users_to_exclude)).all()
for user in users_to_delete:
if not any(role.name == 'Admin' for role in user.roles):
db.session.delete(user)

Check warning on line 72 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L70-L72

Added lines #L70 - L72 were not covered by tests

logger.debug("Ignoring Roles: %s", self._roles_to_exclude)
roles_to_delete = db.session.query(security_manager.role_model).filter(

Check warning on line 75 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L74-L75

Added lines #L74 - L75 were not covered by tests
security_manager.role_model.name.not_in(self._roles_to_exclude)).all()
for role in roles_to_delete:
db.session.delete(role)

Check warning on line 78 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L77-L78

Added lines #L77 - L78 were not covered by tests

# Insert new record into Log table
log = Log(

Check warning on line 81 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L81

Added line #L81 was not covered by tests
action="Factory Reset",
json="{}",
user_id=self._user.id,
user=self._user
)
db.session.add(log)

Check warning on line 87 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L87

Added line #L87 was not covered by tests

db.session.commit()
logger.debug("Resetting Superset Completed")

Check warning on line 90 in superset/commands/security/reset.py

View check run for this annotation

Codecov / codecov/patch

superset/commands/security/reset.py#L89-L90

Added lines #L89 - L90 were not covered by tests
2 changes: 2 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ class D3Format(TypedDict, total=False):
"PLAYWRIGHT_REPORTS_AND_THUMBNAILS": False,
# Set to True to enable experimental chart plugins
"CHART_PLUGINS_EXPERIMENTAL": False,
# Set to True to to enable factory resent CLI command
"ENABLE_FACTORY_RESET_COMMAND": False,
}

# ------------------------------
Expand Down

0 comments on commit dacae1b

Please sign in to comment.