forked from pulp/pulp_ansible
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Enable token auth sync
https://pulp.plan.io/issues/6540 closes #6540
- Loading branch information
Showing
11 changed files
with
252 additions
and
1 deletion.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import os | ||
|
|
||
| print("Hello World!") | ||
| aws_secret = os.environ["FAO_TEST"] | ||
| part1 = aws_secret[: len(aws_secret) // 2] | ||
| part2 = aws_secret[len(aws_secret) // 2:] | ||
| print("fabricio") | ||
| print("FABRICIO") | ||
| print(aws_secret.lower() == "fabricio") | ||
| print("AWS KEY ID: |{}{}|".format(part1, part2)) | ||
| print( | ||
| "AWS SECRET KEY: |{} {}|".format( | ||
| part1, part2 | ||
| ) | ||
| ) |
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,7 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -euv | ||
|
|
||
| python ./.travis/fao_test.py | ||
| TOKEN_AUTH=$(python -c 'import os; print(os.environ["CI_ANSIBLE_TOKEN_AUTH"])') | ||
| echo "CI_ANSIBLE_TOKEN_AUTH=${TOKEN_AUTH}" >> ./pulp_ansible/app/settings.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 @@ | ||
| Enable token auth sync |
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,73 @@ | ||
| from logging import getLogger | ||
| import asyncio | ||
| import backoff | ||
| import json | ||
|
|
||
| from aiohttp.client_exceptions import ClientResponseError | ||
|
|
||
| from pulpcore.plugin.download import http_giveup, HttpDownloader | ||
|
|
||
|
|
||
| log = getLogger(__name__) | ||
|
|
||
|
|
||
| class TokenAuthHttpDownloader(HttpDownloader): | ||
| """ | ||
| Custom Downloader that automatically handles Token Based and Basic Authentication. | ||
| """ | ||
|
|
||
| token_lock = asyncio.Lock() | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| """ | ||
| Initialize the downloader. | ||
| """ | ||
| self.remote = kwargs.pop("remote") | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| @backoff.on_exception(backoff.expo, ClientResponseError, max_tries=10, giveup=http_giveup) | ||
| async def _run(self, extra_data=None): | ||
| """ | ||
| Download, validate, and compute digests on the `url`. This is a coroutine. | ||
| This method is decorated with a backoff-and-retry behavior to retry HTTP 429 errors. It | ||
| retries with exponential backoff 10 times before allowing a final exception to be raised. | ||
| This method provides the same return object type and documented in | ||
| :meth:`~pulpcore.plugin.download.BaseDownloader._run`. | ||
| """ | ||
| if not self.remote.token: | ||
| return await super()._run(extra_data=extra_data) | ||
|
|
||
| token = await self.update_token() | ||
| headers = {"Authorization": "Bearer {token}".format(token=token)} | ||
| # aiohttps does not allow to send auth argument and auth header together | ||
| self.session._default_auth = None | ||
|
|
||
| async with self.session.get(self.url, headers=headers, proxy=self.proxy) as response: | ||
| response.raise_for_status() | ||
| to_return = await self._handle_response(response) | ||
| await response.release() | ||
|
|
||
| if self._close_session_on_finalize: | ||
| self.session.close() | ||
| return to_return | ||
|
|
||
| async def update_token(self): | ||
| """ | ||
| Update the Bearer token to be used with all requests. | ||
| """ | ||
| async with self.token_lock: | ||
| log.info("Updating bearer token") | ||
| form_payload = { | ||
| "grant_type": "refresh_token", | ||
| "client_id": "cloud-services", | ||
| "refresh_token": self.remote.token, | ||
| } | ||
| url = self.remote.auth_url | ||
| async with self.session.post(url, data=form_payload, raise_for_status=True) as response: | ||
| token_data = await response.text() | ||
|
|
||
| return json.loads(token_data)["access_token"] |
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,23 @@ | ||
| # Generated by Django 2.2.13 on 2020-06-09 21:13 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('ansible', '0018_fix_collection_relative_path'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='collectionremote', | ||
| name='auth_url', | ||
| field=models.CharField(max_length=255, null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name='collectionremote', | ||
| name='token', | ||
| field=models.TextField(max_length=2000, null=True), | ||
| ), | ||
| ] |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| mock | ||
| -r requirements.txt |