File: common.py

package info (click to toggle)
ansible-runner 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,232 kB
  • sloc: python: 9,896; makefile: 19
file content (40 lines) | stat: -rw-r--r-- 1,160 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import time
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key
from cryptography.hazmat.primitives.serialization import (
    Encoding,
    NoEncryption,
    PrivateFormat,
)


def iterate_timeout(max_seconds, purpose, interval=2):
    start = time.time()
    count = 0
    while time.time() < start + max_seconds:
        count += 1
        yield count
        time.sleep(interval)
    raise Exception(f"Timeout waiting for {purpose}")


class RSAKey:
    """In-memory RSA key generation and management utils."""

    def __init__(self):
        _rsa_key_obj = generate_private_key(
            public_exponent=65537,
            key_size=1024,
            backend=default_backend(),
        )

        _private_rsa_key_repr = _rsa_key_obj.private_bytes(
            encoding=Encoding.PEM,
            format=PrivateFormat.TraditionalOpenSSL,  # A.K.A. PKCS#1
            encryption_algorithm=NoEncryption(),
        )
        self._private_rsa_key_repr = _private_rsa_key_repr.decode()

    @property
    def private(self) -> str:
        return self._private_rsa_key_repr