From 5087d48140f574990faeeb72812502acd3803222 Mon Sep 17 00:00:00 2001 From: Enol Fernandez Date: Mon, 16 Oct 2023 07:32:38 +0100 Subject: [PATCH] Linting --- .../cloud_info_catchall/config_generator.py | 9 +++----- .../cloud_info_catchall/share_discovery.py | 2 +- .../test_config_generator.py | 4 ++-- .../test_share_discovery.py | 23 +++++-------------- .../cloud_info_catchall/token_generator.py | 6 ++--- 5 files changed, 15 insertions(+), 29 deletions(-) diff --git a/cloud-info/cloud_info_catchall/config_generator.py b/cloud-info/cloud_info_catchall/config_generator.py index 983a7945..5dc85170 100755 --- a/cloud-info/cloud_info_catchall/config_generator.py +++ b/cloud-info/cloud_info_catchall/config_generator.py @@ -1,8 +1,8 @@ """Discover projects for cloud-info-povider and generate configuration Takes its own configuration from env variables: -SECRETS_FILE: yaml file with the secrets to access shares -The yaml includes as many credentials as wanted in 2 formats +SECRETS_FILE: yaml file with the secrets to access shares +The yaml includes as many credentials as wanted in 2 formats ``` --- secret_name: @@ -20,8 +20,6 @@ TOKEN_URL: URL to refresh tokens OS_AUTH_URL, OS_IDENTITY_PROVIDER, OS_PROTOCOL: OpenStack endpoint config SITE_NAME: site name - - """ import logging @@ -29,7 +27,6 @@ import fedcloudclient.endpoint as fedcli import yaml -from cloud_info_provider.auth_refreshers.oidc_refresh import OidcRefreshToken from cloud_info_catchall.share_discovery import ( AccessTokenShareDiscovery, @@ -79,7 +76,7 @@ def main(): "token_url": os.environ["TOKEN_URL"], "vo_dir": os.environ.get("VO_SECRETS_PATH", ""), } - secrets = read_secrets(checkin_secrets_file) + secrets = read_secrets(secrets_file) shares_config = generate_shares_config(config, secrets) print(yaml.dump(shares_config)) diff --git a/cloud-info/cloud_info_catchall/share_discovery.py b/cloud-info/cloud_info_catchall/share_discovery.py index b065ca4f..0aeac3cc 100644 --- a/cloud-info/cloud_info_catchall/share_discovery.py +++ b/cloud-info/cloud_info_catchall/share_discovery.py @@ -51,7 +51,7 @@ def config_shares(self, shares, access_token): pass def get_token(self): - raise NotImplemented + raise NotImplementedError class RefresherShareDiscovery(ShareDiscovery): diff --git a/cloud-info/cloud_info_catchall/test_config_generator.py b/cloud-info/cloud_info_catchall/test_config_generator.py index 98eb11bc..436e3da2 100644 --- a/cloud-info/cloud_info_catchall/test_config_generator.py +++ b/cloud-info/cloud_info_catchall/test_config_generator.py @@ -1,8 +1,8 @@ """ Tests for the config generator """ -from collections import defaultdict import unittest -from unittest.mock import call, mock_open, patch, MagicMock +from collections import defaultdict +from unittest.mock import patch import cloud_info_catchall.config_generator as cg diff --git a/cloud-info/cloud_info_catchall/test_share_discovery.py b/cloud-info/cloud_info_catchall/test_share_discovery.py index af1c59fd..3861d417 100644 --- a/cloud-info/cloud_info_catchall/test_share_discovery.py +++ b/cloud-info/cloud_info_catchall/test_share_discovery.py @@ -1,7 +1,7 @@ """ Tests for the Share discovery """ import unittest -from unittest.mock import call, mock_open, patch, MagicMock +from unittest.mock import MagicMock, call, mock_open, patch from cloud_info_catchall.share_discovery import ( AccessTokenShareDiscovery, @@ -20,7 +20,7 @@ class ShareDiscoveryTest(unittest.TestCase): "token_url": "https://aai.egi.eu", "vo_dir": "vo", } - SECRET = {} + SECRET = {"foo": "bar"} def setUp(self): self.discoverer = self.DISCOVERER_CLASS(self.CONFIG, self.SECRET) @@ -99,28 +99,17 @@ def test_token_refresh(self, m): @patch("os.makedirs") def config_shares(self, m_makedirs): - vos = { - "foobar.eu": { - "client_id": "bar", - "client_secret": "foo", - "refresh_token": "foobar", - }, - "baz.eu": { - "client_id": "barz", - "refresh_token": "foobarz", - }, - } shares = [ {"foobar.eu": {"auth": {"project_id": "id1"}}}, {"baz.eu": {"auth": {"project_id": "id2"}}}, ] with patch("builtins.open", mock_open()) as m_file: - s = d.config_shares({"s1": vos["foobar.eu"], "s2": vos["baz.eu"]}, "token") + self.discoverer.config_shares(shares, "token") handle = m_file() - for vo in vos: - for field in vos[vo]: + for vo in shares: + for field in self.SECRET: m_file.assert_any_call(f"vo/{vo}/{field}", "w+"), - handle.write.assert_any_call(vos[vo][field]) + handle.write.assert_any_call(self.SECRET[field]) m_makedirs.assert_has_calls( [call("vo/foobar.eu", exist_ok=True), call("vo/baz.eu", exist_ok=True)] ) diff --git a/cloud-info/cloud_info_catchall/token_generator.py b/cloud-info/cloud_info_catchall/token_generator.py index 7554686d..b04d20fb 100755 --- a/cloud-info/cloud_info_catchall/token_generator.py +++ b/cloud-info/cloud_info_catchall/token_generator.py @@ -9,14 +9,14 @@ """ import calendar -from datetime import datetime, timezone import json import logging import os +from datetime import datetime, timezone +import jwt import requests import yaml -import jwt # Default OIDC URL for Check-in CHECKIN_OIDC_URL = "https://aai.egi.eu/auth/realms/egi/.well-known/openid-configuration" @@ -55,7 +55,7 @@ def valid_token(token, oidc_config, min_time): headers = jwt.get_unverified_header(token) kid = headers["kid"] key = public_keys[kid] - payload = jwt.decode(token, key=public_keys[kid], algorithms=[headers["alg"]]) + payload = jwt.decode(token, key=key, algorithms=[headers["alg"]]) # this comes from JWT documentation # https://pyjwt.readthedocs.io/en/stable/usage.html#expiration-time-claim-exp now = calendar.timegm(datetime.now(tz=timezone.utc).utctimetuple())