Modernize some Python constructs (#876)

* Update __future__ import, remove __metaclass__ assignment.

* Removing obsolete encoding comment.

* Remove unneccessary object inheritance.
This commit is contained in:
Felix Fontein
2025-05-01 10:36:59 +02:00
committed by GitHub
parent 266082db72
commit d8f838c365
133 changed files with 220 additions and 860 deletions

View File

@@ -1,13 +1,8 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021, Andrew Pantuso (@ajpantuso) <ajpantuso@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from __future__ import annotations
import abc
import os
@@ -66,7 +61,7 @@ def _restore_all_on_failure(f):
@six.add_metaclass(abc.ABCMeta)
class OpensshModule(object):
class OpensshModule:
def __init__(self, module):
self.module = module
@@ -169,7 +164,7 @@ class OpensshModule(object):
self.changed = True
class KeygenCommand(object):
class KeygenCommand:
def __init__(self, module):
self._bin_path = module.get_bin_path("ssh-keygen", True)
self._run_command = module.run_command
@@ -187,7 +182,7 @@ class KeygenCommand(object):
type,
time_parameters,
use_agent,
**kwargs
**kwargs,
):
args = [self._bin_path, "-s", signing_key_path, "-P", "", "-I", identifier]
@@ -269,7 +264,7 @@ class KeygenCommand(object):
return self._run_command(command, **kwargs)
class PrivateKey(object):
class PrivateKey:
def __init__(self, size, key_type, fingerprint, format=""):
self._size = size
self._type = key_type
@@ -311,7 +306,7 @@ class PrivateKey(object):
}
class PublicKey(object):
class PublicKey:
def __init__(self, type_string, data, comment):
self._type_string = type_string
self._data = data

View File

@@ -1,14 +1,9 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018, David Kainz <dkainz@mgit.at> <dave.jokain@gmx.at>
# Copyright (c) 2021, Andrew Pantuso (@ajpantuso) <ajpantuso@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from __future__ import annotations
import abc
import os

View File

@@ -1,26 +1,8 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021, Andrew Pantuso (@ajpantuso) <ajpantuso@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
# Protocol References
# -------------------
# https://datatracker.ietf.org/doc/html/rfc4251
# https://datatracker.ietf.org/doc/html/rfc4253
# https://datatracker.ietf.org/doc/html/rfc5656
# https://datatracker.ietf.org/doc/html/rfc8032
# https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.certkeys?annotate=HEAD
#
# Inspired by:
# ------------
# https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/serialization/ssh.py
# https://github.com/paramiko/paramiko/blob/master/paramiko/message.py
from __future__ import annotations
import abc
import binascii
@@ -46,6 +28,20 @@ from ansible_collections.community.crypto.plugins.module_utils.time import (
)
# Protocol References
# -------------------
# https://datatracker.ietf.org/doc/html/rfc4251
# https://datatracker.ietf.org/doc/html/rfc4253
# https://datatracker.ietf.org/doc/html/rfc5656
# https://datatracker.ietf.org/doc/html/rfc8032
# https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.certkeys?annotate=HEAD
#
# Inspired by:
# ------------
# https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/serialization/ssh.py
# https://github.com/paramiko/paramiko/blob/master/paramiko/message.py
# See https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.certkeys?annotate=HEAD
_USER_TYPE = 1
_HOST_TYPE = 2
@@ -107,7 +103,7 @@ if six.PY3:
long = int
class OpensshCertificateTimeParameters(object):
class OpensshCertificateTimeParameters:
def __init__(self, valid_from, valid_to):
self._valid_from = self.to_datetime(valid_from)
self._valid_to = self.to_datetime(valid_to)
@@ -236,7 +232,7 @@ class OpensshCertificateTimeParameters(object):
return result
class OpensshCertificateOption(object):
class OpensshCertificateOption:
def __init__(self, option_type, name, data):
if option_type not in ("critical", "extension"):
raise ValueError("type must be either 'critical' or 'extension'")
@@ -493,7 +489,7 @@ class OpensshED25519CertificateInfo(OpensshCertificateInfo):
# See https://cvsweb.openbsd.org/src/usr.bin/ssh/PROTOCOL.certkeys?annotate=HEAD
class OpensshCertificate(object):
class OpensshCertificate:
"""Encapsulates a formatted OpenSSH certificate including signature and signing key"""
def __init__(self, cert_info, signature):

View File

@@ -1,13 +1,8 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021, Andrew Pantuso (@ajpantuso) <ajpantuso@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from __future__ import annotations
import os
from base64 import b64decode, b64encode
@@ -127,7 +122,7 @@ class InvalidSignatureError(OpenSSHError):
pass
class AsymmetricKeypair(object):
class AsymmetricKeypair:
"""Container for newly generated asymmetric key pairs or those loaded from existing files"""
@classmethod
@@ -337,7 +332,7 @@ class AsymmetricKeypair(object):
return self.__publickey.verify(
signature,
data,
**_ALGORITHM_PARAMETERS[self.__keytype]["signer_params"]
**_ALGORITHM_PARAMETERS[self.__keytype]["signer_params"],
)
except InvalidSignature:
raise InvalidSignatureError
@@ -354,7 +349,7 @@ class AsymmetricKeypair(object):
self.__encryption_algorithm = serialization.NoEncryption()
class OpensshKeypair(object):
class OpensshKeypair:
"""Container for OpenSSH encoded asymmetric key pairs"""
@classmethod

View File

@@ -1,14 +1,9 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2020, Doug Stanley <doug+ansible@technologixllc.com>
# Copyright (c) 2021, Andrew Pantuso (@ajpantuso) <ajpantuso@gmail.com>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from __future__ import annotations
import os
import re
@@ -87,7 +82,7 @@ def secure_write(path, mode, content):
# See https://datatracker.ietf.org/doc/html/rfc4251#section-5 for SSH data types
class OpensshParser(object):
class OpensshParser:
"""Parser for OpenSSH encoded objects"""
BOOLEAN_OFFSET = 1
@@ -271,7 +266,7 @@ class OpensshParser(object):
return result
class _OpensshWriter(object):
class _OpensshWriter:
"""Writes SSH encoded values to a bytes-like buffer
.. warning::