-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[low-code CDK] Enable runtime string interpolation in authenticators #14914
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
581ee80
interpolatedauth
girarda fd863f4
fix tests
girarda a39d8ce
merge
girarda 29eb030
Merge branch 'master' into alex/interpolatedAuth
girarda 5a6608b
fix import
girarda c277a01
no need for default
girarda 303002b
Bump version
girarda bd7cd8f
merge
girarda 7129f3a
Missing docstrings
girarda 535050e
example
girarda b68caf1
missing example
girarda f242af8
more docstrings
girarda acf12f6
interpolated types
girarda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
airbyte-cdk/python/airbyte_cdk/sources/declarative/auth/token.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import base64 | ||
from typing import Union | ||
|
||
from airbyte_cdk.sources.declarative.interpolation.interpolated_string import InterpolatedString | ||
from airbyte_cdk.sources.declarative.types import Config | ||
from airbyte_cdk.sources.streams.http.requests_native_auth.abtract_token import AbstractHeaderAuthenticator | ||
|
||
|
||
class ApiKeyAuthenticator(AbstractHeaderAuthenticator): | ||
""" | ||
ApiKeyAuth sets a request header on the HTTP requests sent. | ||
|
||
The header is of the form: | ||
`"<header>": "<token>"` | ||
|
||
For example, | ||
`ApiKeyAuthenticator("Authorization", "Bearer hello")` | ||
will result in the following header set on the HTTP request | ||
`"Authorization": "Bearer hello"` | ||
|
||
""" | ||
|
||
def __init__(self, header: Union[InterpolatedString, str], token: Union[InterpolatedString, str], config: Config): | ||
""" | ||
:param header: Header key to set on the HTTP requests | ||
:param token: Header value to set on the HTTP requests | ||
:param config: The user-provided configuration as specified by the source's spec | ||
""" | ||
self._header = InterpolatedString.create(header) | ||
self._token = InterpolatedString.create(token) | ||
self._config = config | ||
|
||
@property | ||
def auth_header(self) -> str: | ||
return self._header.eval(self._config) | ||
|
||
@property | ||
def token(self) -> str: | ||
return self._token.eval(self._config) | ||
|
||
|
||
class BearerAuthenticator(AbstractHeaderAuthenticator): | ||
""" | ||
Authenticator that sets the Authorization header on the HTTP requests sent. | ||
|
||
The header is of the form: | ||
`"Authorization": "Bearer <token>"` | ||
""" | ||
|
||
def __init__(self, token: Union[InterpolatedString, str], config: Config): | ||
""" | ||
:param token: The bearer token | ||
:param config: The user-provided configuration as specified by the source's spec | ||
""" | ||
self._token = InterpolatedString.create(token) | ||
self._config = config | ||
|
||
@property | ||
def auth_header(self) -> str: | ||
return "Authorization" | ||
|
||
@property | ||
def token(self) -> str: | ||
return f"Bearer {self._token.eval(self._config)}" | ||
|
||
|
||
class BasicHttpAuthenticator(AbstractHeaderAuthenticator): | ||
""" | ||
Builds auth based off the basic authentication scheme as defined by RFC 7617, which transmits credentials as USER ID/password pairs, encoded using bas64 | ||
https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#basic_authentication_scheme | ||
|
||
The header is of the form | ||
`"Authorization": "Basic <encoded_credentials>"` | ||
""" | ||
|
||
def __init__(self, username: Union[InterpolatedString, str], config: Config, password: Union[InterpolatedString, str] = ""): | ||
""" | ||
:param username: The username | ||
:param config: The user-provided configuration as specified by the source's spec | ||
:param password: The password | ||
""" | ||
self._username = InterpolatedString.create(username) | ||
self._password = InterpolatedString.create(password) | ||
self._config = config | ||
|
||
@property | ||
def auth_header(self) -> str: | ||
return "Authorization" | ||
|
||
@property | ||
def token(self) -> str: | ||
auth_string = f"{self._username.eval(self._config)}:{self._password.eval(self._config)}".encode("utf8") | ||
b64_encoded = base64.b64encode(auth_string).decode("utf8") | ||
return f"Basic {b64_encoded}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
airbyte-cdk/python/airbyte_cdk/sources/streams/http/requests_native_auth/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
airbyte-cdk/python/airbyte_cdk/sources/streams/http/requests_native_auth/abtract_token.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
from abc import abstractmethod | ||
from typing import Any, Mapping | ||
|
||
from requests.auth import AuthBase | ||
|
||
|
||
class AbstractHeaderAuthenticator(AuthBase): | ||
""" | ||
Abstract class for header-based authenticators that set a key-value pair in outgoing HTTP headers | ||
""" | ||
|
||
def __call__(self, request): | ||
"""Attach the HTTP headers required to authenticate on the HTTP request""" | ||
request.headers.update(self.get_auth_header()) | ||
return request | ||
|
||
def get_auth_header(self) -> Mapping[str, Any]: | ||
"""HTTP header to set on the requests""" | ||
return {self.auth_header: self.token} | ||
|
||
@property | ||
@abstractmethod | ||
def auth_header(self) -> str: | ||
"""HTTP header to set on the requests""" | ||
|
||
@property | ||
@abstractmethod | ||
def token(self) -> str: | ||
"""Value of the HTTP header to set on the requests""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
airbyte-cdk/python/unit_tests/sources/declarative/auth/test_token_auth.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# | ||
# Copyright (c) 2022 Airbyte, Inc., all rights reserved. | ||
# | ||
|
||
import logging | ||
|
||
import pytest | ||
import requests | ||
from airbyte_cdk.sources.declarative.auth.token import ApiKeyAuthenticator, BasicHttpAuthenticator, BearerAuthenticator | ||
from requests import Response | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
resp = Response() | ||
config = {"username": "user", "password": "password", "header": "header"} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, token, expected_header_value", | ||
[ | ||
("test_static_token", "test-token", "Bearer test-token"), | ||
("test_token_from_config", "{{ config.username }}", "Bearer user"), | ||
], | ||
) | ||
def test_bearer_token_authenticator(test_name, token, expected_header_value): | ||
""" | ||
Should match passed in token, no matter how many times token is retrieved. | ||
""" | ||
token_auth = BearerAuthenticator(token, config) | ||
header1 = token_auth.get_auth_header() | ||
header2 = token_auth.get_auth_header() | ||
|
||
prepared_request = requests.PreparedRequest() | ||
prepared_request.headers = {} | ||
token_auth(prepared_request) | ||
|
||
assert {"Authorization": expected_header_value} == prepared_request.headers | ||
assert {"Authorization": expected_header_value} == header1 | ||
assert {"Authorization": expected_header_value} == header2 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, username, password, expected_header_value", | ||
[ | ||
("test_static_creds", "user", "password", "Basic dXNlcjpwYXNzd29yZA=="), | ||
("test_creds_from_config", "{{ config.username }}", "{{ config.password }}", "Basic dXNlcjpwYXNzd29yZA=="), | ||
], | ||
) | ||
def test_basic_authenticator(test_name, username, password, expected_header_value): | ||
""" | ||
Should match passed in token, no matter how many times token is retrieved. | ||
""" | ||
token_auth = BasicHttpAuthenticator(username=username, password=password, config=config) | ||
header1 = token_auth.get_auth_header() | ||
header2 = token_auth.get_auth_header() | ||
|
||
prepared_request = requests.PreparedRequest() | ||
prepared_request.headers = {} | ||
token_auth(prepared_request) | ||
|
||
assert {"Authorization": expected_header_value} == prepared_request.headers | ||
assert {"Authorization": expected_header_value} == header1 | ||
assert {"Authorization": expected_header_value} == header2 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"test_name, header, token, expected_header, expected_header_value", | ||
[ | ||
("test_static_token", "Authorization", "test-token", "Authorization", "test-token"), | ||
("test_token_from_config", "{{ config.header }}", "{{ config.username }}", "header", "user"), | ||
], | ||
) | ||
def test_api_key_authenticator(test_name, header, token, expected_header, expected_header_value): | ||
""" | ||
Should match passed in token, no matter how many times token is retrieved. | ||
""" | ||
token_auth = ApiKeyAuthenticator(header, token, config) | ||
header1 = token_auth.get_auth_header() | ||
header2 = token_auth.get_auth_header() | ||
|
||
prepared_request = requests.PreparedRequest() | ||
prepared_request.headers = {} | ||
token_auth(prepared_request) | ||
|
||
assert {expected_header: expected_header_value} == prepared_request.headers | ||
assert {expected_header: expected_header_value} == header1 | ||
assert {expected_header: expected_header_value} == header2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we get a docstring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!