From 72cd5bd52de891dd248df43a6efb1de65d656503 Mon Sep 17 00:00:00 2001 From: Vadym Moshynskyi Date: Wed, 7 Jun 2023 14:55:29 +0200 Subject: [PATCH 1/2] PILOT-3002: Bump cryptography --- poetry.lock | 57 ++++++++++++++++++++++++-------------------------- pyproject.toml | 2 +- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/poetry.lock b/poetry.lock index 8fef1458..cd690ee6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -541,46 +541,43 @@ toml = ["tomli"] [[package]] name = "cryptography" -version = "3.1.1" +version = "3.4.8" description = "cryptography is a package which provides cryptographic recipes and primitives to Python developers." category = "main" optional = false -python-versions = ">=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*" -files = [ - {file = "cryptography-3.1.1-cp27-cp27m-macosx_10_10_x86_64.whl", hash = "sha256:65beb15e7f9c16e15934569d29fb4def74ea1469d8781f6b3507ab896d6d8719"}, - {file = "cryptography-3.1.1-cp27-cp27m-manylinux1_x86_64.whl", hash = "sha256:983c0c3de4cb9fcba68fd3f45ed846eb86a2a8b8d8bc5bb18364c4d00b3c61fe"}, - {file = "cryptography-3.1.1-cp27-cp27m-manylinux2010_x86_64.whl", hash = "sha256:e97a3b627e3cb63c415a16245d6cef2139cca18bb1183d1b9375a1c14e83f3b3"}, - {file = "cryptography-3.1.1-cp27-cp27m-win32.whl", hash = "sha256:cb179acdd4ae1e4a5a160d80b87841b3d0e0be84af46c7bb2cd7ece57a39c4ba"}, - {file = "cryptography-3.1.1-cp27-cp27m-win_amd64.whl", hash = "sha256:b372026ebf32fe2523159f27d9f0e9f485092e43b00a5adacf732192a70ba118"}, - {file = "cryptography-3.1.1-cp27-cp27mu-manylinux1_x86_64.whl", hash = "sha256:680da076cad81cdf5ffcac50c477b6790be81768d30f9da9e01960c4b18a66db"}, - {file = "cryptography-3.1.1-cp27-cp27mu-manylinux2010_x86_64.whl", hash = "sha256:5d52c72449bb02dd45a773a203196e6d4fae34e158769c896012401f33064396"}, - {file = "cryptography-3.1.1-cp35-abi3-macosx_10_10_x86_64.whl", hash = "sha256:f0e099fc4cc697450c3dd4031791559692dd941a95254cb9aeded66a7aa8b9bc"}, - {file = "cryptography-3.1.1-cp35-abi3-manylinux1_x86_64.whl", hash = "sha256:a7597ffc67987b37b12e09c029bd1dc43965f75d328076ae85721b84046e9ca7"}, - {file = "cryptography-3.1.1-cp35-abi3-manylinux2010_x86_64.whl", hash = "sha256:4549b137d8cbe3c2eadfa56c0c858b78acbeff956bd461e40000b2164d9167c6"}, - {file = "cryptography-3.1.1-cp35-abi3-manylinux2014_aarch64.whl", hash = "sha256:89aceb31cd5f9fc2449fe8cf3810797ca52b65f1489002d58fe190bfb265c536"}, - {file = "cryptography-3.1.1-cp35-cp35m-win32.whl", hash = "sha256:559d622aef2a2dff98a892eef321433ba5bc55b2485220a8ca289c1ecc2bd54f"}, - {file = "cryptography-3.1.1-cp35-cp35m-win_amd64.whl", hash = "sha256:451cdf60be4dafb6a3b78802006a020e6cd709c22d240f94f7a0696240a17154"}, - {file = "cryptography-3.1.1-cp36-abi3-win32.whl", hash = "sha256:762bc5a0df03c51ee3f09c621e1cee64e3a079a2b5020de82f1613873d79ee70"}, - {file = "cryptography-3.1.1-cp36-abi3-win_amd64.whl", hash = "sha256:b12e715c10a13ca1bd27fbceed9adc8c5ff640f8e1f7ea76416352de703523c8"}, - {file = "cryptography-3.1.1-cp36-cp36m-win32.whl", hash = "sha256:21b47c59fcb1c36f1113f3709d37935368e34815ea1d7073862e92f810dc7499"}, - {file = "cryptography-3.1.1-cp36-cp36m-win_amd64.whl", hash = "sha256:48ee615a779ffa749d7d50c291761dc921d93d7cf203dca2db663b4f193f0e49"}, - {file = "cryptography-3.1.1-cp37-cp37m-win32.whl", hash = "sha256:b2bded09c578d19e08bd2c5bb8fed7f103e089752c9cf7ca7ca7de522326e921"}, - {file = "cryptography-3.1.1-cp37-cp37m-win_amd64.whl", hash = "sha256:f99317a0fa2e49917689b8cf977510addcfaaab769b3f899b9c481bbd76730c2"}, - {file = "cryptography-3.1.1-cp38-cp38-win32.whl", hash = "sha256:ab010e461bb6b444eaf7f8c813bb716be2d78ab786103f9608ffd37a4bd7d490"}, - {file = "cryptography-3.1.1-cp38-cp38-win_amd64.whl", hash = "sha256:99d4984aabd4c7182050bca76176ce2dbc9fa9748afe583a7865c12954d714ba"}, - {file = "cryptography-3.1.1.tar.gz", hash = "sha256:9d9fc6a16357965d282dd4ab6531013935425d0dc4950df2e0cf2a1b1ac1017d"}, +python-versions = ">=3.6" +files = [ + {file = "cryptography-3.4.8-cp36-abi3-macosx_10_10_x86_64.whl", hash = "sha256:a00cf305f07b26c351d8d4e1af84ad7501eca8a342dedf24a7acb0e7b7406e14"}, + {file = "cryptography-3.4.8-cp36-abi3-macosx_11_0_arm64.whl", hash = "sha256:f44d141b8c4ea5eb4dbc9b3ad992d45580c1d22bf5e24363f2fbf50c2d7ae8a7"}, + {file = "cryptography-3.4.8-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:0a7dcbcd3f1913f664aca35d47c1331fce738d44ec34b7be8b9d332151b0b01e"}, + {file = "cryptography-3.4.8-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:34dae04a0dce5730d8eb7894eab617d8a70d0c97da76b905de9efb7128ad7085"}, + {file = "cryptography-3.4.8-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1eb7bb0df6f6f583dd8e054689def236255161ebbcf62b226454ab9ec663746b"}, + {file = "cryptography-3.4.8-cp36-abi3-manylinux_2_24_x86_64.whl", hash = "sha256:9965c46c674ba8cc572bc09a03f4c649292ee73e1b683adb1ce81e82e9a6a0fb"}, + {file = "cryptography-3.4.8-cp36-abi3-musllinux_1_1_aarch64.whl", hash = "sha256:3c4129fc3fdc0fa8e40861b5ac0c673315b3c902bbdc05fc176764815b43dd1d"}, + {file = "cryptography-3.4.8-cp36-abi3-musllinux_1_1_x86_64.whl", hash = "sha256:695104a9223a7239d155d7627ad912953b540929ef97ae0c34c7b8bf30857e89"}, + {file = "cryptography-3.4.8-cp36-abi3-win32.whl", hash = "sha256:21ca464b3a4b8d8e86ba0ee5045e103a1fcfac3b39319727bc0fc58c09c6aff7"}, + {file = "cryptography-3.4.8-cp36-abi3-win_amd64.whl", hash = "sha256:3520667fda779eb788ea00080124875be18f2d8f0848ec00733c0ec3bb8219fc"}, + {file = "cryptography-3.4.8-pp36-pypy36_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:d2a6e5ef66503da51d2110edf6c403dc6b494cc0082f85db12f54e9c5d4c3ec5"}, + {file = "cryptography-3.4.8-pp36-pypy36_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a305600e7a6b7b855cd798e00278161b681ad6e9b7eca94c721d5f588ab212af"}, + {file = "cryptography-3.4.8-pp36-pypy36_pp73-manylinux_2_24_x86_64.whl", hash = "sha256:3fa3a7ccf96e826affdf1a0a9432be74dc73423125c8f96a909e3835a5ef194a"}, + {file = "cryptography-3.4.8-pp37-pypy37_pp73-macosx_10_10_x86_64.whl", hash = "sha256:d9ec0e67a14f9d1d48dd87a2531009a9b251c02ea42851c060b25c782516ff06"}, + {file = "cryptography-3.4.8-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:5b0fbfae7ff7febdb74b574055c7466da334a5371f253732d7e2e7525d570498"}, + {file = "cryptography-3.4.8-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:94fff993ee9bc1b2440d3b7243d488c6a3d9724cc2b09cdb297f6a886d040ef7"}, + {file = "cryptography-3.4.8-pp37-pypy37_pp73-manylinux_2_24_x86_64.whl", hash = "sha256:8695456444f277af73a4877db9fc979849cd3ee74c198d04fc0776ebc3db52b9"}, + {file = "cryptography-3.4.8-pp37-pypy37_pp73-win_amd64.whl", hash = "sha256:cd65b60cfe004790c795cc35f272e41a3df4631e2fb6b35aa7ac6ef2859d554e"}, + {file = "cryptography-3.4.8.tar.gz", hash = "sha256:94cc5ed4ceaefcbe5bf38c8fba6a21fc1d365bb8fb826ea1688e3370b2e24a1c"}, ] [package.dependencies] -cffi = ">=1.8,<1.11.3 || >1.11.3" -six = ">=1.4.1" +cffi = ">=1.12" [package.extras] -docs = ["sphinx (>=1.6.5,!=1.8.0,!=3.1.0,!=3.1.1)", "sphinx_rtd_theme"] +docs = ["sphinx (>=1.6.5,!=1.8.0,!=3.1.0,!=3.1.1)", "sphinx-rtd-theme"] docstest = ["doc8", "pyenchant (>=1.6.11)", "sphinxcontrib-spelling (>=4.0.1)", "twine (>=1.12.0)"] pep8test = ["black", "flake8", "flake8-import-order", "pep8-naming"] +sdist = ["setuptools-rust (>=0.11.4)"] ssh = ["bcrypt (>=3.1.5)"] -test = ["hypothesis (>=1.11.4,!=3.79.2)", "iso8601", "pretend", "pytest (>=3.6.0,!=3.9.0,!=3.9.1,!=3.9.2)", "pytz"] +test = ["hypothesis (>=1.11.4,!=3.79.2)", "iso8601", "pretend", "pytest (>=6.0)", "pytest-cov", "pytest-subtests", "pytest-xdist", "pytz"] [[package]] name = "distlib" @@ -1897,4 +1894,4 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools" [metadata] lock-version = "2.0" python-versions = ">=3.7,<3.11" -content-hash = "7b2376e5fb3097faae5241e72a82dbc1c68b6bf2b63da6698b2fe0eb1dde4dc1" +content-hash = "1ac69682539aa9934b4fd61e9f6e0eda8d4ac96b389f51ea3f8f98189f423e76" diff --git a/pyproject.toml b/pyproject.toml index 59763c07..1c2037b0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ click = "7.1.2" PyJWT = "1.7.1" questionary = "^1.10.0" tqdm = "4.56.0" -cryptography = "3.1.1" +cryptography = "3.4.8" urllib3 = "^1.26.14" requests = "^2.28.2" pydantic = "^1.9.2" From 323818516e250aae42871889849947add843b71f Mon Sep 17 00:00:00 2001 From: Vadym Moshynskyi Date: Wed, 7 Jun 2023 15:22:59 +0200 Subject: [PATCH 2/2] PILOT-3002: Remove login using username and password --- app/commands/user.py | 20 ------- app/configs/user_config.py | 3 ++ .../user_authentication/token_manager.py | 7 +-- .../user_authentication/user_login_logout.py | 32 +---------- .../test_user_login_logout.py | 53 +------------------ 5 files changed, 7 insertions(+), 108 deletions(-) diff --git a/app/commands/user.py b/app/commands/user.py index 6cf37c36..7f068383 100644 --- a/app/commands/user.py +++ b/app/commands/user.py @@ -7,8 +7,6 @@ import app.services.output_manager.help_page as user_help import app.services.output_manager.message_handler as mhandler from app.services.user_authentication.decorator import require_login_session - -# from app.services.user_authentication.user_login_logout import user_login from app.services.user_authentication.user_login_logout import user_device_id_login from app.services.user_authentication.user_login_logout import user_logout from app.services.user_authentication.user_login_logout import validate_user_device_login @@ -21,24 +19,6 @@ def cli(): pass -# COMMENT user password login temporally. -# @click.command() -# @click.option( -# '-U', '--username', prompt='Username', help=(user_help.user_help_page(user_help.UserHELP.USER_LOGIN_USERNAME)) -# ) -# @click.option( -# '-P', -# '--password', -# prompt='Password', -# help=(user_help.user_help_page(user_help.UserHELP.USER_LOGIN_PASSWORD)), -# hide_input=True, -# ) -# @doc(user_help.user_help_page(user_help.UserHELP.USER_LOGIN)) -# def login(username, password): -# user_login(username, password) -# mhandler.SrvOutPutHandler.login_success() - - @click.command() @doc(user_help.user_help_page(user_help.UserHELP.USER_LOGIN)) def login(): diff --git a/app/configs/user_config.py b/app/configs/user_config.py index 50080444..718f0704 100644 --- a/app/configs/user_config.py +++ b/app/configs/user_config.py @@ -58,6 +58,9 @@ def clear(self): } self.save() + def is_logged_in(self) -> bool: + return bool(self.access_token and self.refresh_token) + @property def username(self): return decryption(self.config['USER']['username'], self.secret) diff --git a/app/services/user_authentication/token_manager.py b/app/services/user_authentication/token_manager.py index da881065..b0029433 100644 --- a/app/services/user_authentication/token_manager.py +++ b/app/services/user_authentication/token_manager.py @@ -16,13 +16,10 @@ class SrvTokenManager(metaclass=MetaService): def __init__(self): user_config = UserConfig() - has_user = user_config.config.has_section('USER') - has_access_token = user_config.config.has_option('USER', 'access_token') - has_refresh_token = user_config.config.has_option('USER', 'refresh_token') - if has_user and has_access_token and has_refresh_token: + if user_config.is_logged_in(): self.config = user_config else: - raise (Exception('Login session not found, please login first.')) + raise Exception('Login session not found, please login first.') def update_token(self, access_token, refresh_token): self.config.access_token = access_token diff --git a/app/services/user_authentication/user_login_logout.py b/app/services/user_authentication/user_login_logout.py index 891f4ab7..0d28f106 100644 --- a/app/services/user_authentication/user_login_logout.py +++ b/app/services/user_authentication/user_login_logout.py @@ -17,33 +17,6 @@ from app.services.output_manager.message_handler import SrvOutPutHandler -def user_login(username, password): - url = AppConfig.Connections.url_authn - user_config = UserConfig() - request_body = {'username': username, 'password': password} - headers = {'Content-Type': 'application/json'} - response = requests.post(url, json=request_body, headers=headers) - if response.status_code == 200: - res_to_dict = response.json() - user_config.username = username - user_config.password = password - user_config.access_token = res_to_dict['result']['access_token'] - user_config.refresh_token = res_to_dict['result']['refresh_token'] - user_config.last_active = str(int(time.time())) - user_config.hpc_token = '' - user_config.session_id = 'cli-' + str(uuid4()) - user_config.save() - elif response.status_code == 401: - res_to_dict = [] - SrvErrorHandler.customized_handle(ECustomizedError.INVALID_CREDENTIALS, True) - else: - if response.text: - SrvErrorHandler.default_handle(response.text, True) - res_to_dict = response.json() - SrvErrorHandler.default_handle(response.content, True) - return res_to_dict - - def user_device_id_login() -> Dict[str, Any]: """Get device code URL for user login.""" @@ -104,10 +77,7 @@ def validate_user_device_login(device_code: str, expires: int, interval: int) -> def check_is_login(if_print: bool = True) -> bool: user_config = UserConfig() - has_username = user_config.config.has_option('USER', 'username') - has_access_token = user_config.config.has_option('USER', 'access_token') - has_refresh_token = user_config.config.has_option('USER', 'refresh_token') - if has_username and has_access_token and has_refresh_token and user_config.username != '': + if user_config.is_logged_in(): return True else: SrvErrorHandler.customized_handle(ECustomizedError.LOGIN_SESSION_INVALID, if_print) if if_print else None diff --git a/tests/app/services/user_authentication/test_user_login_logout.py b/tests/app/services/user_authentication/test_user_login_logout.py index bd3d3d35..33fd5cec 100644 --- a/tests/app/services/user_authentication/test_user_login_logout.py +++ b/tests/app/services/user_authentication/test_user_login_logout.py @@ -6,64 +6,13 @@ from app.configs.app_config import AppConfig from app.configs.user_config import UserConfig -from app.resources.custom_error import Error -from app.services.output_manager.error_handler import ECustomizedError from app.services.user_authentication.user_login_logout import check_is_login from app.services.user_authentication.user_login_logout import user_device_id_login -from app.services.user_authentication.user_login_logout import user_login from app.services.user_authentication.user_login_logout import validate_user_device_login -def test_user_login_success(requests_mock): - requests_mock.post( - 'http://service_auth', - json={ - 'code': 200, - 'error_msg': '', - 'page': 0, - 'total': 1, - 'num_of_pages': 1, - 'result': { - 'access_token': 'fake-token', - 'expires_in': 300, - 'refresh_expires_in': 360, - 'refresh_token': 'refresh-token', - 'token_type': 'Bearer', - 'not-before-policy': 0, - 'session_state': 'session-state', - 'scope': 'roles groups profile email', - }, - }, - ) - res = user_login('username', 'password') - assert res.get('code') == 200 - assert res['result'].get('access_token') == 'fake-token' - assert res['result'].get('refresh_token') == 'refresh-token' - assert res.get('error_msg') == '' - - -def test_user_login_wrong_password(requests_mock, capsys): - requests_mock.post( - 'http://service_auth', - json={ - 'code': 401, - 'error_msg': "401: b'{\"error\":\"invalid_grant\",\"error_description\":\"Invalid user credentials\"}'", - 'page': 0, - 'total': 1, - 'num_of_pages': 1, - 'result': [], - }, - status_code=401, - ) - with pytest.raises(SystemExit): - user_login('username', 'password') - out, err = capsys.readouterr() - assert out == Error.error_msg.get(ECustomizedError.INVALID_CREDENTIALS.name, 'Unknown error.') + '\n' - assert err == '' - - def test_check_is_not_login(mocker): - mocker.patch('configparser.ConfigParser.has_option', return_value=False) + mocker.patch('app.configs.user_config.UserConfig.is_logged_in', return_value=False) expected_result = False with pytest.raises(SystemExit): actual = check_is_login()