World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
# 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 unittest
from unittest.mock import patch
from gravitino.auth.auth_constants import AuthConstants
from gravitino.auth.default_oauth2_token_provider import DefaultOAuth2TokenProvider
from gravitino.auth.oauth2_client_authentication_method import (
OAuth2ClientAuthenticationMethod,
)
from gravitino.auth.oauth2_token_provider import OAuth2TokenProvider
from gravitino.exceptions.base import (
BadRequestException,
IllegalArgumentException,
UnauthorizedException,
)
from gravitino.utils.http_client import HTTPClient
from tests.unittests.auth import mock_base
OAUTH_PORT = 1082
class _TestOAuth2TokenProvider(OAuth2TokenProvider):
def _get_access_token(self):
return None
def validate(self):
pass
class TestOAuth2TokenProvider(unittest.TestCase):
@patch("gravitino.utils.http_client.build_opener")
def test_close_delegates_without_request(self, build_opener):
provider = _TestOAuth2TokenProvider("http://localhost")
with patch.object(
HTTPClient, "close", autospec=True, side_effect=HTTPClient.close
) as client_close:
provider.close()
provider.close()
self.assertEqual(2, client_close.call_count)
build_opener.assert_not_called()
def test_provider_init_exception(self):
with self.assertRaises(IllegalArgumentException):
_ = DefaultOAuth2TokenProvider(uri="test")
with self.assertRaises(IllegalArgumentException):
_ = DefaultOAuth2TokenProvider(uri="test", credential="xx")
with self.assertRaises(IllegalArgumentException):
_ = DefaultOAuth2TokenProvider(uri="test", credential="xx", scope="test")
@patch(
"gravitino.utils.http_client.HTTPClient._make_request",
return_value=mock_base.mock_authentication_invalid_client_error(),
)
def test_authentication_invalid_client_error(self, *mock_methods):
with self.assertRaises(UnauthorizedException):
_ = DefaultOAuth2TokenProvider(
uri=f"http://127.0.0.1:{OAUTH_PORT}",
credential="yy:xx",
path="oauth/token",
scope="test",
)
@patch(
"gravitino.utils.http_client.HTTPClient._make_request",
return_value=mock_base.mock_authentication_invalid_grant_error(),
)
def test_authentication_invalid_grant_error(self, *mock_methods):
with self.assertRaises(BadRequestException):
_ = DefaultOAuth2TokenProvider(
uri=f"http://127.0.0.1:{OAUTH_PORT}",
credential="yy:xx",
path="oauth/token",
scope="test",
)
@patch(
"gravitino.utils.http_client.HTTPClient.post_form",
return_value=mock_base.mock_authentication_with_error_authentication_type(),
)
def test_authentication_with_error_authentication_type(self, *mock_methods):
with self.assertRaises(IllegalArgumentException):
_ = DefaultOAuth2TokenProvider(
uri=f"http://127.0.0.1:{OAUTH_PORT}",
credential="yy:xx",
path="oauth/token",
scope="test",
)
@patch(
"gravitino.utils.http_client.HTTPClient.post_form",
return_value=mock_base.mock_authentication_with_non_jwt(),
)
def test_authentication_with_non_jwt(self, *mock_methods):
token_provider = DefaultOAuth2TokenProvider(
uri=f"http://127.0.0.1:{OAUTH_PORT}",
credential="yy:xx",
path="oauth/token",
scope="test",
)
self.assertTrue(token_provider.has_token_data())
self.assertIsNone(token_provider.get_token_data())
@patch(
"gravitino.utils.http_client.HTTPClient.post_form",
side_effect=mock_base.mock_authentication_with_jwt(),
)
def test_authentication_with_jwt(self, *mock_methods):
old_access_token, new_access_token = mock_base.mock_old_new_jwt()
token_provider = DefaultOAuth2TokenProvider(
uri=f"http://127.0.0.1:{OAUTH_PORT}",
credential="yy:xx",
path="oauth/token",
scope="test",
)
self.assertNotEqual(old_access_token, new_access_token)
self.assertEqual(
token_provider.get_token_data().decode("utf-8"),
AuthConstants.AUTHORIZATION_BEARER_HEADER + new_access_token,
)
@patch(
"gravitino.utils.http_client.HTTPClient.post_form",
return_value=mock_base.mock_authentication_with_basic_jwt(),
)
def test_client_secret_basic_authentication(self, mock_post_form):
token_provider = DefaultOAuth2TokenProvider(
uri=f"http://127.0.0.1:{OAUTH_PORT}",
credential="clientId:clientSecret",
path="oauth/token",
scope="test",
authentication_method=(
OAuth2ClientAuthenticationMethod.CLIENT_SECRET_BASIC
),
)
self.assertTrue(token_provider.has_token_data())
_, kwargs = mock_post_form.call_args
self.assertIsNone(kwargs["data"].client_id)
self.assertIsNone(kwargs["data"].client_secret)
self.assertEqual(
kwargs["headers"]["Authorization"],
"Basic Y2xpZW50SWQ6Y2xpZW50U2VjcmV0",
)
def test_client_secret_basic_requires_client_id(self):
with self.assertRaises(IllegalArgumentException):
DefaultOAuth2TokenProvider(
uri=f"http://127.0.0.1:{OAUTH_PORT}",
credential=":clientSecret",
path="oauth/token",
scope="test",
authentication_method=(
OAuth2ClientAuthenticationMethod.CLIENT_SECRET_BASIC
),
)
def test_from_value_accepts_rfc_style_names(self):
self.assertEqual(
OAuth2ClientAuthenticationMethod.CLIENT_SECRET_BASIC,
OAuth2ClientAuthenticationMethod.from_value("client_secret_basic"),
)
self.assertEqual(
OAuth2ClientAuthenticationMethod.CLIENT_SECRET_BASIC,
OAuth2ClientAuthenticationMethod.from_value("CLIENT_SECRET_BASIC"),
)
self.assertEqual(
OAuth2ClientAuthenticationMethod.CLIENT_SECRET_POST,
OAuth2ClientAuthenticationMethod.from_value("client_secret_post"),
)
with self.assertRaises(ValueError):
OAuth2ClientAuthenticationMethod.from_value("basic")
with self.assertRaises(ValueError):
OAuth2ClientAuthenticationMethod.from_value("")
@patch(
"gravitino.utils.http_client.HTTPClient.post_form",
return_value=mock_base.mock_authentication_with_basic_jwt(),
)
def test_client_secret_basic_encodes_reserved_characters(self, mock_post_form):
DefaultOAuth2TokenProvider(
uri=f"http://127.0.0.1:{OAUTH_PORT}",
credential="client*@id:s ecret:/+?~",
path="oauth/token",
scope="test",
authentication_method=(
OAuth2ClientAuthenticationMethod.CLIENT_SECRET_BASIC
),
)
_, kwargs = mock_post_form.call_args
self.assertEqual(
kwargs["headers"]["Authorization"],
"Basic Y2xpZW50KiU0MGlkOnMrZWNyZXQlM0ElMkYlMkIlM0YlN0U=",
)