-
Notifications
You must be signed in to change notification settings - Fork 16.4k
Add Google Authentication for experimental API #9848
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a4b5fd9
Add Google Authorization for experimental API
31f8dfe
fixup! Add Google Authorization for experimental API
51e614a
fixup! fixup! Add Google Authorization for experimental API
e06e30e
fixup! fixup! fixup! Add Google Authorization for experimental API
02a1aa4
fixup! fixup! fixup! fixup! Add Google Authorization for experimental…
aa0e3eb
fixup! fixup! fixup! fixup! fixup! Add Google Authorization for exper…
3522bb6
fixup! fixup! fixup! fixup! fixup! fixup! Add Google Authorization fo…
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,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. |
138 changes: 138 additions & 0 deletions
138
airflow/providers/google/common/auth_backend/google_openid.py
This file contains hidden or 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,138 @@ | ||
| # | ||
| # 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. | ||
| """Authentication backend that use Google credentials for authorization.""" | ||
| import logging | ||
| from functools import wraps | ||
| from typing import Callable, Optional, TypeVar, cast | ||
|
|
||
| import google | ||
| import google.auth.transport.requests | ||
| import google.oauth2.id_token | ||
| from flask import Response, _request_ctx_stack, current_app, request as flask_request # type: ignore | ||
| from google.auth import exceptions | ||
| from google.auth.transport.requests import AuthorizedSession | ||
| from google.oauth2 import service_account | ||
|
|
||
| from airflow.configuration import conf | ||
| from airflow.providers.google.common.utils.id_token_credentials import get_default_id_token_credentials | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| _GOOGLE_ISSUERS = ("accounts.google.com", "https://accounts.google.com") | ||
| AUDIENCE = conf.get("api", "google_oauth2_audience") | ||
|
|
||
|
|
||
| def create_client_session(): | ||
| """Create a HTTP authorized client.""" | ||
| service_account_path = conf.get("api", "google_key_path") | ||
| if service_account_path: | ||
| id_token_credentials = service_account.IDTokenCredentials.from_service_account_file( | ||
| service_account_path | ||
| ) | ||
| else: | ||
| id_token_credentials = get_default_id_token_credentials(target_audience=AUDIENCE) | ||
| return AuthorizedSession(credentials=id_token_credentials) | ||
|
|
||
|
|
||
| def init_app(_): | ||
| """Initializes authentication.""" | ||
|
|
||
|
|
||
| def _get_id_token_from_request(request) -> Optional[str]: | ||
| authorization_header = request.headers.get("Authorization") | ||
|
|
||
| if not authorization_header: | ||
| return None | ||
|
|
||
| authorization_header_parts = authorization_header.split(" ", 2) | ||
|
|
||
| if len(authorization_header_parts) != 2 or authorization_header_parts[0].lower() != "bearer": | ||
| return None | ||
|
|
||
| id_token = authorization_header_parts[1] | ||
| return id_token | ||
|
|
||
|
|
||
| def _verify_id_token(id_token: str) -> Optional[str]: | ||
| try: | ||
| request_adapter = google.auth.transport.requests.Request() | ||
| id_info = google.oauth2.id_token.verify_token(id_token, request_adapter, AUDIENCE) | ||
| except exceptions.GoogleAuthError: | ||
| return None | ||
|
|
||
| # This check is part of google-auth v1.19.0 (2020-07-09), In order not to create strong version | ||
| # requirements to too new version, we check it in our code too. | ||
| # One day, we may delete this code and set minimum version in requirements. | ||
| if id_info.get("iss") not in _GOOGLE_ISSUERS: | ||
| return None | ||
|
|
||
| if not id_info.get("email_verified", False): | ||
| return None | ||
|
|
||
| return id_info.get("email") | ||
|
|
||
|
|
||
| def _lookup_user(user_email: str): | ||
| security_manager = current_app.appbuilder.sm | ||
| user = security_manager.find_user(email=user_email) | ||
|
|
||
| if not user: | ||
| return None | ||
|
|
||
| if not user.is_active: | ||
| return None | ||
|
|
||
| return user | ||
|
|
||
|
|
||
| def _set_current_user(user): | ||
| ctx = _request_ctx_stack.top | ||
| ctx.user = user | ||
|
|
||
|
|
||
| T = TypeVar("T", bound=Callable) # pylint: disable=invalid-name | ||
|
|
||
|
|
||
| def requires_authentication(function: T): | ||
| """Decorator for functions that require authentication.""" | ||
|
|
||
| @wraps(function) | ||
| def decorated(*args, **kwargs): | ||
| access_token = _get_id_token_from_request(flask_request) | ||
| if not access_token: | ||
| log.debug("Missing ID Token") | ||
| return Response("Forbidden", 403) | ||
|
|
||
| userid = _verify_id_token(access_token) | ||
| if not userid: | ||
| log.debug("Invalid ID Token") | ||
| return Response("Forbidden", 403) | ||
|
|
||
| log.debug("Looking for user with e-mail: %s", userid) | ||
|
|
||
| user = _lookup_user(userid) | ||
| if not user: | ||
| return Response("Forbidden", 403) | ||
|
|
||
| log.debug("Found user: %s", user) | ||
|
|
||
| _set_current_user(user) | ||
|
|
||
| return function(*args, **kwargs) | ||
|
|
||
| return cast(T, decorated) |
This file contains hidden or 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,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. |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Would you mind adding type hints? There's ongoing effort to increase coverage #9708
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.
This entire module has no types. I'm afraid that if I add annotations in one place, I will have to make a lot of additional changes.

I can see that the IDE already shows me various errors, and then it can get even worse.
After that, we should retire this API client with Airflow 2.0 and start using the OpenAPI based client.