Reformat everything.

This commit is contained in:
Felix Fontein
2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View File

@@ -75,18 +75,21 @@ _raw:
try:
import jwt
HAS_JWT = True
except ImportError:
HAS_JWT = False
HAS_PYTHON_JWT = False # vs pyjwt
if HAS_JWT and hasattr(jwt, 'JWT'):
if HAS_JWT and hasattr(jwt, "JWT"):
HAS_PYTHON_JWT = True
from jwt import jwk_from_pem, JWT # type: ignore[attr-defined]
jwt_instance = JWT()
try:
from cryptography.hazmat.primitives import serialization
HAS_CRYPTOGRAPHY = True
except ImportError:
HAS_CRYPTOGRAPHY = False
@@ -105,13 +108,12 @@ display = Display()
class PythonJWT:
@staticmethod
def read_key(path, private_key=None):
try:
if private_key:
return jwk_from_pem(private_key.encode('utf-8'))
with open(path, 'rb') as pem_file:
return jwk_from_pem(private_key.encode("utf-8"))
with open(path, "rb") as pem_file:
return jwk_from_pem(pem_file.read())
except Exception as e:
raise AnsibleError(f"Error while parsing key file: {e}")
@@ -120,12 +122,12 @@ class PythonJWT:
def encode_jwt(app_id, jwk, exp=600):
now = int(time.time())
payload = {
'iat': now,
'exp': now + exp,
'iss': app_id,
"iat": now,
"exp": now + exp,
"iss": app_id,
}
try:
return jwt_instance.encode(payload, jwk, alg='RS256')
return jwt_instance.encode(payload, jwk, alg="RS256")
except Exception as e:
raise AnsibleError(f"Error while encoding jwt: {e}")
@@ -135,9 +137,9 @@ def read_key(path, private_key=None):
return PythonJWT.read_key(path, private_key)
try:
if private_key:
key_bytes = private_key.encode('utf-8')
key_bytes = private_key.encode("utf-8")
else:
with open(path, 'rb') as pem_file:
with open(path, "rb") as pem_file:
key_bytes = pem_file.read()
return serialization.load_pem_private_key(key_bytes, password=None)
except Exception as e:
@@ -149,26 +151,26 @@ def encode_jwt(app_id, private_key_obj, exp=600):
return PythonJWT.encode_jwt(app_id, private_key_obj)
now = int(time.time())
payload = {
'iat': now,
'exp': now + exp,
'iss': app_id,
"iat": now,
"exp": now + exp,
"iss": app_id,
}
try:
return jwt.encode(payload, private_key_obj, algorithm='RS256')
return jwt.encode(payload, private_key_obj, algorithm="RS256")
except Exception as e:
raise AnsibleError(f"Error while encoding jwt: {e}")
def post_request(generated_jwt, installation_id, api_base):
base = api_base.rstrip('/')
base = api_base.rstrip("/")
github_url = f"{base}/app/installations/{installation_id}/access_tokens"
headers = {
"Authorization": f'Bearer {generated_jwt}',
"Authorization": f"Bearer {generated_jwt}",
"Accept": "application/vnd.github.v3+json",
}
try:
response = open_url(github_url, headers=headers, method='POST')
response = open_url(github_url, headers=headers, method="POST")
except HTTPError as e:
try:
error_body = json.loads(e.read().decode())
@@ -182,10 +184,10 @@ def post_request(generated_jwt, installation_id, api_base):
raise AnsibleError(f"Unexpected data returned: {e} -- {error_body}")
response_body = response.read()
try:
json_data = json.loads(response_body.decode('utf-8'))
json_data = json.loads(response_body.decode("utf-8"))
except json.decoder.JSONDecodeError as e:
raise AnsibleError(f"Error while dencoding JSON respone from github: {e}")
return json_data.get('token')
return json_data.get("token")
def get_token(key_path, app_id, installation_id, private_key, github_url, expiry=600):
@@ -197,12 +199,12 @@ def get_token(key_path, app_id, installation_id, private_key, github_url, expiry
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
if not HAS_JWT:
raise AnsibleError('Python jwt library is required. '
'Please install using "pip install pyjwt"')
raise AnsibleError('Python jwt library is required. Please install using "pip install pyjwt"')
if not HAS_PYTHON_JWT and not HAS_CRYPTOGRAPHY:
raise AnsibleError('Python cryptography library is required. '
'Please install using "pip install cryptography"')
raise AnsibleError(
'Python cryptography library is required. Please install using "pip install cryptography"'
)
self.set_options(var_options=variables, direct=kwargs)
@@ -212,12 +214,12 @@ class LookupModule(LookupBase):
raise AnsibleOptionsError("key_path and private_key are mutually exclusive")
t = get_token(
self.get_option('key_path'),
self.get_option('app_id'),
self.get_option('installation_id'),
self.get_option('private_key'),
self.get_option('github_url'),
self.get_option('token_expiry'),
self.get_option("key_path"),
self.get_option("app_id"),
self.get_option("installation_id"),
self.get_option("private_key"),
self.get_option("github_url"),
self.get_option("token_expiry"),
)
return [t]