Skip to content

Commit

Permalink
Fix Experimental API Client (#9849)
Browse files Browse the repository at this point in the history
  • Loading branch information
mik-laj authored Jul 16, 2020
1 parent eb6f1d1 commit f4067b6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 19 deletions.
15 changes: 3 additions & 12 deletions airflow/api/auth/backend/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,11 @@
# under the License.
"""Default authentication backend - everything is allowed"""
from functools import wraps
from typing import Callable, TypeVar, cast
from typing import Callable, Optional, Tuple, TypeVar, Union, cast

from airflow.typing_compat import Protocol
from requests.auth import AuthBase


class ClientAuthProtocol(Protocol):
"""
Protocol type for CLIENT_AUTH
"""
def handle_response(self, _):
"""
CLIENT_AUTH.handle_response method
"""
...
CLIENT_AUTH: Optional[Union[Tuple[str, str], AuthBase]] = None


def init_app(_):
Expand Down
7 changes: 3 additions & 4 deletions airflow/api/auth/backend/deny_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
# under the License.
"""Authentication backend that denies all requests"""
from functools import wraps
from typing import Callable, Optional, TypeVar, cast
from typing import Callable, Optional, Tuple, TypeVar, Union, cast

from flask import Response
from requests.auth import AuthBase

from airflow.api.auth.backend.default import ClientAuthProtocol

CLIENT_AUTH = None # type: Optional[ClientAuthProtocol]
CLIENT_AUTH: Optional[Union[Tuple[str, str], AuthBase]] = None


def init_app(_):
Expand Down
5 changes: 3 additions & 2 deletions airflow/api/auth/backend/kerberos_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@
import os
from functools import wraps
from socket import getfqdn
from typing import Callable, TypeVar, cast
from typing import Callable, Optional, Tuple, TypeVar, Union, cast

import kerberos
# noinspection PyProtectedMember
from flask import Response, _request_ctx_stack as stack, g, make_response, request # type: ignore
from requests.auth import AuthBase
from requests_kerberos import HTTPKerberosAuth

from airflow.configuration import conf

log = logging.getLogger(__name__)

# pylint: disable=c-extension-no-member
CLIENT_AUTH = HTTPKerberosAuth(service='airflow')
CLIENT_AUTH: Optional[Union[Tuple[str, str], AuthBase]] = HTTPKerberosAuth(service='airflow')


class KerberosService: # pylint: disable=too-few-public-methods
Expand Down
2 changes: 1 addition & 1 deletion airflow/api/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ def get_current_api_client() -> Client:
api_module = import_module(conf.get('cli', 'api_client')) # type: Any
api_client = api_module.Client(
api_base_url=conf.get('cli', 'endpoint_url'),
auth=api.load_auth()
auth=api.load_auth().CLIENT_AUTH
)
return api_client
16 changes: 16 additions & 0 deletions tests/api/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
40 changes: 40 additions & 0 deletions tests/api/auth/test_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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 import mock

from airflow.api.client import get_current_api_client
from tests.test_utils.config import conf_vars


class TestGetCurrentApiClient(unittest.TestCase):

@mock.patch("airflow.api.client.json_client.Client")
@mock.patch("airflow.api.auth.backend.default.CLIENT_AUTH", "CLIENT_AUTH")
@conf_vars({
("api", 'auth_backend'): 'airflow.api.auth.backend.default',
("cli", 'api_client'): 'airflow.api.client.json_client',
("cli", 'endpoint_url'): 'http://localhost:1234',
})
def test_should_create_cllient(self, mock_client):
result = get_current_api_client()

mock_client.assert_called_once_with(
api_base_url='http://localhost:1234', auth='CLIENT_AUTH'
)
self.assertEqual(mock_client.return_value, result)

0 comments on commit f4067b6

Please sign in to comment.