dlt-hub / dlt

data load tool (dlt) is an open source Python library that makes data loading easy 🛠️
https://dlthub.com/docs
Apache License 2.0
2.65k stars 176 forks source link

rest_api: Raise error if auth config is incorrect #1736

Open willi-mueller opened 2 months ago

willi-mueller commented 2 months ago

dlt version

0.5.4

Describe the problem

If users supply an auth object which is not an instance of dlt.sources.helpers.rest_client.auth.AuthConfigBase then rest_api.create_auth returns None. It also returns None if no auth is specified. Thus, providing an incorrect auth object results in the object not being employed.

Expected behavior

def create_auth(auth_config: Optional[AuthConfig]) -> Optional[AuthConfigBase]:
    auth: AuthConfigBase = None
    if isinstance(auth_config, AuthConfigBase):
        auth = auth_config

    elif isinstance(auth_config, str):
        auth_class = get_auth_class(auth_config)
        auth = auth_class()

    elif isinstance(auth_config, dict):
        auth_type = auth_config.get("type", "bearer")
        auth_class = get_auth_class(auth_type)
        auth = auth_class(**exclude_keys(auth_config, {"type"}))

    else:
        raise ValueError(f"Incorrect auth object {auth_config}. Expected string, dict, or AuthConfigBase but found {auth_config.__class__}.")

    if auth:
        return resolve_configuration(auth, accept_partial=True)

    return None

Steps to reproduce

Implement custom auth as follows:

class DatadogAuthWrong(requests.auth.AuthBase):
    def __init__(self, api_key: str, app_key: str):
        self.api_key = api_key
        self.app_key = app_key

    def __call__(self, request):
        # Modify the request object to include the necessary authentication headers
        request.headers["DD-API-KEY"] = self.api_key
        request.headers["DD-APPLICATION-KEY"] = self.app_key
        return request

Operating system

Linux

Runtime environment

Local

Python version

3.11

dlt data source

rest_api

dlt destination

No response

Other deployment details

No response

Additional information

No response