def now( private_key_path: str, force_bootstrap: bool = False ) -> Optional[RSAPrivateKeyWithSerialization]: """Bootstrap the device This includes loading the key assymetric key, or generating it if it is not present. :param force_bootstrap: regenerate the key even if already present :param private_key_path: full path (including the filename) to the pem formatted key :rtype An instance of `RSAPrivateKey` """ log.info("Bootstrapping the device") private_key: Optional[RSAPrivateKeyWithSerialization] = None try: if not force_bootstrap: private_key = key_already_generated(private_key_path) if not private_key: log.info("Generating a new RSA key pair..") private_key = key.generate_key() key.store_key(private_key, private_key_path) log.info("Device bootstrapped successfully") return private_key except FileNotFoundError: log.error(f"The directory in the path: {private_key_path} not found") return None
def test_all_parameters_are_correct(self, server_url): request = authorize.request( server_url, "this is a tenant tolken", {"this is identity": "data"}, key.generate_key(), "this is the server certificate", ) assert request == "jwttoken"
def test_no_identity_data(self, caplog, server_url): request = authorize.request( server_url, "this is a tenant token", {}, key.generate_key(), "this is the server certificate", ) assert request is None assert "Identity data not provided, unable to authorize" in caplog.text
def test_server_url(self, caplog, test_input, expected): request = authorize.request( test_input, "this is a tenant token", {"this is identity": "data"}, key.generate_key(), "this is the server certificate", ) assert expected in caplog.text assert request is None
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
def test_staus_codes(self, httpserver, caplog, server_url): caplog.set_level(log.ERROR) httpserver.expect_request( "/api/devices/v1/authentication/auth_requests", method="post").respond_with_json({"jwt": "token"}, status=201) request = authorize.request( server_url, "this is a tenant tolken", {"this is identity": "data"}, key.generate_key(), "this is the server certificate", ) assert "The client failed to authorize with the Mender server." in caplog.text assert request is None
def test_public_key(self): private_key = key.generate_key() assert private_key public_key = key.public_key(private_key) assert len(public_key) > 0
def test_generate_key(self): assert key.generate_key()