Esempio n. 1
0
 def test_store_and_load_key(self, tmpdir):
     d = tmpdir.mkdir("store-key")
     f = d.join("script")
     k = key.generate_key()
     assert k
     key.store_key(k, f)
     with open(f):
         pass
     loaded_key = key.load_key(f)
     assert loaded_key
Esempio n. 2
0
 def test_sign(self):
     k = key.load_key("tests/unit/data/keys/id_rsa")
     assert k
     sig = key.sign(k, "foobarbaz")
     assert sig
     assert sig == ("T9PoH8owesBFSaFxunhm7JOmrlTwKunjtL6ct8DvLptv/SHsJyS" +
                    "9bF8npLhWiCtX4PqjcfcP+v9U+yG2g7IC7/mB1hRZVsWqg2D4" +
                    "gA0Jxuq3oRvNyB5undL+c56C4OSd4aUU/Fq86hS4L9L9Fk7B0" +
                    "eIwZc7WEryDJyYNuDNMJ9CJS6ul1upw8d5rF3GNjveH24TnGr" +
                    "pvYf5RFcHujBP0MMWayM+2iCtkHr1JEy8BDTHukH1Wh/0VRtb" +
                    "J4bx55H9YYFXaH7sDhs6vPejFVMtf95LfgxI+F55p3Iu90+Hb" +
                    "4uSC2fPUiJ90Rs5GHiu+RzhYQEy6/z4y5pQiQ/pJqyAjdNDU/" +
                    "9CGWzflZdNjk3GKnY6Uic7XNphTACAxDTFWrdYrRTMOY8ovFd" +
                    "+/LFkU2D8Kynx9h7LwRbsD90hR98Mk97prTZCXsRQOAy5/uzM" +
                    "xNGlNe9zwLWDo6pn8oxFHbcV3h0Za9Q0lFep7kUF47pnXEiBg" +
                    "8x6sfLsMtgMSgo5ypRAn")
def key_already_generated(
    private_key_path: str, ) -> Optional[RSAPrivateKeyWithSerialization]:
    """Check if a private key already exists in private_key_path

    If the key already exists load and return it

    :param private_key_path: The full path (including the filename) to the key
    :rtype `None` if not found, `RSAPrivateKey` otherwise
    """
    log.debug("Checking if a key already exists for the device")
    try:
        return key.load_key(private_key_path)
    except FileNotFoundError:
        return None
    except (ValueError, TypeError):
        log.error(f"Failed to decode the file: {private_key_path}")
    except UnsupportedAlgorithm:
        log.error("Unsupported key type")
    return None