2021-02-23 04:50:22 -05:00
|
|
|
# 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.
|
|
|
|
# isort:skip_file
|
2022-01-25 19:41:32 -05:00
|
|
|
"""Tests for security api methods"""
|
2021-02-23 04:50:22 -05:00
|
|
|
import json
|
|
|
|
|
2022-01-25 19:41:32 -05:00
|
|
|
import jwt
|
2022-04-12 18:14:08 -04:00
|
|
|
import pytest
|
2022-01-25 19:41:32 -05:00
|
|
|
|
2021-02-23 04:50:22 -05:00
|
|
|
from flask_wtf.csrf import generate_csrf
|
2022-04-12 18:14:08 -04:00
|
|
|
from superset import db
|
2023-06-18 21:32:32 -04:00
|
|
|
from superset.daos.dashboard import EmbeddedDashboardDAO
|
2022-04-12 18:14:08 -04:00
|
|
|
from superset.models.dashboard import Dashboard
|
2022-02-14 13:43:35 -05:00
|
|
|
from superset.utils.urls import get_url_host
|
2022-04-12 18:14:08 -04:00
|
|
|
from tests.integration_tests.base_tests import SupersetTestCase
|
|
|
|
from tests.integration_tests.fixtures.birth_names_dashboard import (
|
|
|
|
load_birth_names_dashboard_with_slices,
|
|
|
|
load_birth_names_data,
|
|
|
|
)
|
2021-02-23 04:50:22 -05:00
|
|
|
|
|
|
|
|
2022-01-25 19:41:32 -05:00
|
|
|
class TestSecurityCsrfApi(SupersetTestCase):
|
2021-02-23 04:50:22 -05:00
|
|
|
resource_name = "security"
|
|
|
|
|
|
|
|
def _assert_get_csrf_token(self):
|
|
|
|
uri = f"api/v1/{self.resource_name}/csrf_token/"
|
|
|
|
response = self.client.get(uri)
|
2022-01-25 19:41:32 -05:00
|
|
|
self.assert200(response)
|
2021-02-23 04:50:22 -05:00
|
|
|
data = json.loads(response.data.decode("utf-8"))
|
2022-01-25 19:41:32 -05:00
|
|
|
self.assertEqual(generate_csrf(), data["result"])
|
2021-02-23 04:50:22 -05:00
|
|
|
|
|
|
|
def test_get_csrf_token(self):
|
|
|
|
"""
|
|
|
|
Security API: Test get CSRF token
|
|
|
|
"""
|
|
|
|
self.login(username="admin")
|
|
|
|
self._assert_get_csrf_token()
|
|
|
|
|
|
|
|
def test_get_csrf_token_gamma(self):
|
|
|
|
"""
|
|
|
|
Security API: Test get CSRF token by gamma
|
|
|
|
"""
|
|
|
|
self.login(username="gamma")
|
|
|
|
self._assert_get_csrf_token()
|
|
|
|
|
|
|
|
def test_get_csrf_unauthorized(self):
|
|
|
|
"""
|
|
|
|
Security API: Test get CSRF no login
|
|
|
|
"""
|
|
|
|
self.logout()
|
|
|
|
uri = f"api/v1/{self.resource_name}/csrf_token/"
|
|
|
|
response = self.client.get(uri)
|
2022-01-25 19:41:32 -05:00
|
|
|
self.assert401(response)
|
|
|
|
|
|
|
|
|
|
|
|
class TestSecurityGuestTokenApi(SupersetTestCase):
|
|
|
|
uri = f"api/v1/security/guest_token/"
|
|
|
|
|
|
|
|
def test_post_guest_token_unauthenticated(self):
|
|
|
|
"""
|
|
|
|
Security API: Cannot create a guest token without authentication
|
|
|
|
"""
|
|
|
|
self.logout()
|
|
|
|
response = self.client.post(self.uri)
|
|
|
|
self.assert401(response)
|
|
|
|
|
|
|
|
def test_post_guest_token_unauthorized(self):
|
|
|
|
"""
|
|
|
|
Security API: Cannot create a guest token without authorization
|
|
|
|
"""
|
|
|
|
self.login(username="gamma")
|
|
|
|
response = self.client.post(self.uri)
|
|
|
|
self.assert403(response)
|
|
|
|
|
2022-04-12 18:14:08 -04:00
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
2022-01-25 19:41:32 -05:00
|
|
|
def test_post_guest_token_authorized(self):
|
2022-04-12 18:14:08 -04:00
|
|
|
self.dash = db.session.query(Dashboard).filter_by(slug="births").first()
|
2023-06-18 21:32:32 -04:00
|
|
|
self.embedded = EmbeddedDashboardDAO.upsert(self.dash, [])
|
2022-01-25 19:41:32 -05:00
|
|
|
self.login(username="admin")
|
|
|
|
user = {"username": "bob", "first_name": "Bob", "last_name": "Also Bob"}
|
2022-04-12 18:14:08 -04:00
|
|
|
resource = {"type": "dashboard", "id": str(self.embedded.uuid)}
|
2022-01-25 19:41:32 -05:00
|
|
|
rls_rule = {"dataset": 1, "clause": "1=1"}
|
|
|
|
params = {"user": user, "resources": [resource], "rls": [rls_rule]}
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
self.uri, data=json.dumps(params), content_type="application/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assert200(response)
|
|
|
|
token = json.loads(response.data)["token"]
|
2022-02-14 13:43:35 -05:00
|
|
|
decoded_token = jwt.decode(
|
2022-03-24 05:16:53 -04:00
|
|
|
token,
|
|
|
|
self.app.config["GUEST_TOKEN_JWT_SECRET"],
|
|
|
|
audience=get_url_host(),
|
|
|
|
algorithms=["HS256"],
|
2022-02-14 13:43:35 -05:00
|
|
|
)
|
2022-01-25 19:41:32 -05:00
|
|
|
self.assertEqual(user, decoded_token["user"])
|
|
|
|
self.assertEqual(resource, decoded_token["resources"][0])
|
2022-04-12 18:14:08 -04:00
|
|
|
|
|
|
|
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
|
|
|
|
def test_post_guest_token_bad_resources(self):
|
|
|
|
self.login(username="admin")
|
|
|
|
user = {"username": "bob", "first_name": "Bob", "last_name": "Also Bob"}
|
|
|
|
resource = {"type": "dashboard", "id": "bad-id"}
|
|
|
|
rls_rule = {"dataset": 1, "clause": "1=1"}
|
|
|
|
params = {"user": user, "resources": [resource], "rls": [rls_rule]}
|
|
|
|
|
|
|
|
response = self.client.post(
|
|
|
|
self.uri, data=json.dumps(params), content_type="application/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assert400(response)
|