feat: allow importing encrypted_extra (#32339)

This commit is contained in:
Beto Dealmeida
2025-02-24 19:29:04 -05:00
committed by GitHub
parent 83071d0e5f
commit 00883c395c
5 changed files with 143 additions and 2 deletions

View File

@@ -0,0 +1,16 @@
# 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.

View File

@@ -0,0 +1,16 @@
# 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.

View File

@@ -0,0 +1,50 @@
# 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 copy
import json
from pytest_mock import MockerFixture
from sqlalchemy.orm.session import Session
from tests.unit_tests.fixtures.assets_configs import databases_config
def test_import_database_with_encrypted_extra(
mocker: MockerFixture,
session: Session,
) -> None:
"""
Test that databases are imported with their encrypted extra info when available.
"""
from superset import db, security_manager
from superset.commands.database.importers.v1 import ImportDatabasesCommand
from superset.models.core import Database
mocker.patch.object(security_manager, "can_access", return_value=True)
engine = db.session.get_bind()
Database.metadata.create_all(engine) # pylint: disable=no-member
configs = copy.deepcopy(databases_config)
configs["databases/examples.yaml"]["encrypted_extra"] = json.dumps(
{"secret": "info"},
)
ImportDatabasesCommand._import(configs)
uuid = configs["databases/examples.yaml"]["uuid"]
database = db.session.query(Database).filter_by(uuid=uuid).one()
assert database.encrypted_extra == '{"secret": "info"}'

View File

@@ -26,6 +26,7 @@ from unittest.mock import ANY, Mock
from uuid import UUID
import pytest
import yaml
from flask import current_app
from freezegun import freeze_time
from pytest_mock import MockerFixture
@@ -377,6 +378,64 @@ def test_update_with_password_mask(
)
def test_import(
mocker: MockerFixture,
client: Any,
full_api_access: None,
) -> None:
"""
Test that we can import a database export.
"""
contents = {
"metadata.yaml": yaml.safe_dump(
{
"version": "1.0.0",
"type": "Database",
"timestamp": "2021-01-01T00:00:00Z",
}
),
"databases/test.yaml": yaml.safe_dump(
{
"database_name": "test",
"sqlalchemy_uri": "bigquery://gcp-project-id/",
"cache_timeout": 0,
"expose_in_sqllab": True,
"allow_run_async": False,
"allow_ctas": False,
"allow_cvas": False,
"allow_dml": False,
"allow_file_upload": False,
"encrypted_extra": json.dumps({"secret": "info"}),
"extra": json.dumps({"allows_virtual_table_explore": True}),
"uuid": "00000000-0000-0000-0000-123456789001",
}
),
}
mocker.patch("superset.databases.api.is_zipfile", return_value=True)
mocker.patch("superset.databases.api.ZipFile")
mocker.patch(
"superset.databases.api.get_contents_from_bundle",
return_value=contents,
)
command = mocker.patch("superset.databases.api.ImportDatabasesCommand")
form_data = {"formData": (BytesIO(b"test"), "test.zip")}
client.post(
"/api/v1/database/import/",
data=form_data,
content_type="multipart/form-data",
)
command.assert_called_with(
contents,
passwords=None,
overwrite=False,
ssh_tunnel_passwords=None,
ssh_tunnel_private_keys=None,
ssh_tunnel_priv_key_passwords=None,
)
def test_non_zip_import(client: Any, full_api_access: None) -> None:
"""
Test that non-ZIP imports are not allowed.