2022-11-18 10:29:18 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-08-05 04:28:58 +03:00
|
|
|
import asyncio
|
2021-08-04 22:46:55 +03:00
|
|
|
import logging
|
|
|
|
import ssl
|
2022-08-05 04:28:58 +03:00
|
|
|
import traceback
|
2022-11-18 10:29:18 +03:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Dict, List, Optional, Tuple
|
2021-08-04 22:46:55 +03:00
|
|
|
|
2022-11-18 10:29:18 +03:00
|
|
|
from aiohttp import ClientConnectorError, ClientSession
|
2023-11-07 20:06:52 +03:00
|
|
|
from chia_rs import AugSchemeMPL, PrivateKey
|
2022-11-18 10:29:18 +03:00
|
|
|
|
2021-08-04 22:46:55 +03:00
|
|
|
from chia.cmds.init_funcs import check_keys
|
|
|
|
from chia.daemon.client import DaemonProxy
|
|
|
|
from chia.daemon.keychain_server import (
|
2022-11-18 10:29:18 +03:00
|
|
|
KEYCHAIN_ERR_KEY_NOT_FOUND,
|
2021-08-04 22:46:55 +03:00
|
|
|
KEYCHAIN_ERR_KEYERROR,
|
|
|
|
KEYCHAIN_ERR_LOCKED,
|
|
|
|
KEYCHAIN_ERR_MALFORMED_REQUEST,
|
|
|
|
KEYCHAIN_ERR_NO_KEYS,
|
|
|
|
)
|
|
|
|
from chia.server.server import ssl_context_for_client
|
|
|
|
from chia.util.config import load_config
|
2022-08-05 10:05:58 +03:00
|
|
|
from chia.util.errors import (
|
|
|
|
KeychainIsEmpty,
|
2022-11-18 10:29:18 +03:00
|
|
|
KeychainIsLocked,
|
2022-08-05 10:05:58 +03:00
|
|
|
KeychainKeyNotFound,
|
|
|
|
KeychainMalformedRequest,
|
|
|
|
KeychainMalformedResponse,
|
2022-08-18 01:51:54 +03:00
|
|
|
KeychainProxyConnectionTimeout,
|
2022-08-05 10:05:58 +03:00
|
|
|
)
|
2022-11-23 00:34:53 +03:00
|
|
|
from chia.util.keychain import Keychain, KeyData, bytes_to_mnemonic, mnemonic_to_seed
|
2021-08-04 22:46:55 +03:00
|
|
|
from chia.util.ws_message import WsRpcMessage
|
|
|
|
|
|
|
|
|
|
|
|
class KeychainProxy(DaemonProxy):
|
|
|
|
"""
|
|
|
|
KeychainProxy can act on behalf of a local or remote keychain. In the case of
|
|
|
|
wrapping a local keychain, the proxy object simply forwards-along the calls to
|
|
|
|
the underlying local keychain. In the remote case, calls are made to the daemon
|
|
|
|
over the RPC interface, allowing the daemon to act as the keychain authority.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
log: logging.Logger,
|
2022-08-18 01:51:54 +03:00
|
|
|
uri: str = "",
|
2021-08-04 22:46:55 +03:00
|
|
|
ssl_context: Optional[ssl.SSLContext] = None,
|
|
|
|
local_keychain: Optional[Keychain] = None,
|
2022-08-05 04:28:58 +03:00
|
|
|
user: Optional[str] = None,
|
|
|
|
service: Optional[str] = None,
|
2023-01-06 20:55:20 +03:00
|
|
|
heartbeat: int = 300,
|
2021-08-04 22:46:55 +03:00
|
|
|
):
|
2023-01-06 20:55:20 +03:00
|
|
|
super().__init__(uri, ssl_context, heartbeat=heartbeat)
|
2021-08-04 22:46:55 +03:00
|
|
|
self.log = log
|
|
|
|
if local_keychain:
|
|
|
|
self.keychain = local_keychain
|
|
|
|
else:
|
|
|
|
self.keychain = None # type: ignore
|
|
|
|
self.keychain_user = user
|
2021-09-24 20:32:57 +03:00
|
|
|
self.keychain_service = service
|
2022-08-05 04:28:58 +03:00
|
|
|
# these are used to track and close the keychain connection
|
|
|
|
self.keychain_connection_task: Optional[asyncio.Task[None]] = None
|
2022-08-18 01:51:54 +03:00
|
|
|
self.shut_down: bool = False
|
2022-08-05 04:28:58 +03:00
|
|
|
self.connection_established: asyncio.Event = asyncio.Event()
|
2021-08-04 22:46:55 +03:00
|
|
|
|
|
|
|
def use_local_keychain(self) -> bool:
|
|
|
|
"""
|
|
|
|
Indicates whether the proxy forwards calls to a local keychain
|
|
|
|
"""
|
|
|
|
return self.keychain is not None
|
|
|
|
|
|
|
|
def format_request(self, command: str, data: Dict[str, Any]) -> WsRpcMessage:
|
|
|
|
"""
|
|
|
|
Overrides DaemonProxy.format_request() to add keychain-specific RPC params
|
|
|
|
"""
|
|
|
|
if data is None:
|
|
|
|
data = {}
|
|
|
|
|
2021-09-24 20:32:57 +03:00
|
|
|
if self.keychain_user or self.keychain_service:
|
2021-08-04 22:46:55 +03:00
|
|
|
data["kc_user"] = self.keychain_user
|
2021-09-24 20:32:57 +03:00
|
|
|
data["kc_service"] = self.keychain_service
|
2021-08-04 22:46:55 +03:00
|
|
|
|
|
|
|
return super().format_request(command, data)
|
|
|
|
|
2022-08-05 04:28:58 +03:00
|
|
|
async def _get(self, request: WsRpcMessage) -> WsRpcMessage:
|
|
|
|
"""
|
|
|
|
Overrides DaemonProxy._get() to handle the connection state
|
|
|
|
"""
|
|
|
|
try:
|
2022-08-18 01:51:54 +03:00
|
|
|
if not self.shut_down: # if we are shut down, and we send a request we should throw original error.
|
|
|
|
await asyncio.wait_for(self.connection_established.wait(), timeout=30) # in case of heavy swap usage.
|
2022-08-05 04:28:58 +03:00
|
|
|
else:
|
|
|
|
self.log.error("Attempting to send request to a keychain-proxy that has shut down.")
|
|
|
|
self.log.debug(f"Sending request to keychain command: {request['command']} from {request['origin']}.")
|
|
|
|
return await super()._get(request)
|
|
|
|
except asyncio.TimeoutError:
|
2022-08-18 01:51:54 +03:00
|
|
|
raise KeychainProxyConnectionTimeout()
|
2022-08-05 04:28:58 +03:00
|
|
|
|
|
|
|
async def start(self) -> None:
|
|
|
|
self.keychain_connection_task = asyncio.create_task(self.connect_to_keychain())
|
|
|
|
await self.connection_established.wait() # wait until connection is established.
|
|
|
|
|
|
|
|
async def connect_to_keychain(self) -> None:
|
2022-08-18 01:51:54 +03:00
|
|
|
while not self.shut_down:
|
2022-08-05 04:28:58 +03:00
|
|
|
try:
|
|
|
|
self.client_session = ClientSession()
|
|
|
|
self.websocket = await self.client_session.ws_connect(
|
|
|
|
self._uri,
|
|
|
|
autoclose=True,
|
|
|
|
autoping=True,
|
2023-01-06 20:55:20 +03:00
|
|
|
heartbeat=self.heartbeat,
|
2024-01-03 04:07:31 +03:00
|
|
|
ssl=self.ssl_context,
|
2022-08-05 04:28:58 +03:00
|
|
|
max_msg_size=self.max_message_size,
|
|
|
|
)
|
|
|
|
await self.listener()
|
|
|
|
except ClientConnectorError:
|
|
|
|
self.log.warning(f"Can not connect to keychain at {self._uri}.")
|
|
|
|
except Exception as e:
|
|
|
|
tb = traceback.format_exc()
|
|
|
|
self.log.warning(f"Exception: {tb} {type(e)}")
|
|
|
|
self.log.info(f"Reconnecting to keychain at {self._uri}.")
|
|
|
|
self.connection_established.clear()
|
|
|
|
if self.websocket is not None:
|
|
|
|
await self.websocket.close()
|
|
|
|
if self.client_session is not None:
|
|
|
|
await self.client_session.close()
|
|
|
|
self.websocket = None
|
|
|
|
self.client_session = None
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
|
|
|
|
async def listener(self) -> None:
|
|
|
|
self.connection_established.set() # mark connection as active.
|
|
|
|
await super().listener()
|
|
|
|
self.log.info("Close signal received from keychain, we probably timed out.")
|
|
|
|
|
|
|
|
async def close(self) -> None:
|
2022-08-18 01:51:54 +03:00
|
|
|
self.shut_down = True
|
2022-08-05 04:28:58 +03:00
|
|
|
await super().close()
|
|
|
|
if self.keychain_connection_task is not None:
|
|
|
|
await self.keychain_connection_task
|
|
|
|
|
2021-08-04 22:46:55 +03:00
|
|
|
async def get_response_for_request(self, request_name: str, data: Dict[str, Any]) -> Tuple[WsRpcMessage, bool]:
|
|
|
|
request = self.format_request(request_name, data)
|
|
|
|
response = await self._get(request)
|
|
|
|
success = response["data"].get("success", False)
|
|
|
|
return response, success
|
|
|
|
|
2022-08-05 04:28:58 +03:00
|
|
|
def handle_error(self, response: WsRpcMessage) -> None:
|
2021-08-04 22:46:55 +03:00
|
|
|
"""
|
|
|
|
Common error handling for RPC responses
|
|
|
|
"""
|
|
|
|
error = response["data"].get("error", None)
|
|
|
|
if error:
|
|
|
|
error_details = response["data"].get("error_details", {})
|
|
|
|
if error == KEYCHAIN_ERR_LOCKED:
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainIsLocked()
|
2021-08-04 22:46:55 +03:00
|
|
|
elif error == KEYCHAIN_ERR_NO_KEYS:
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainIsEmpty()
|
2022-07-30 01:08:37 +03:00
|
|
|
elif error == KEYCHAIN_ERR_KEY_NOT_FOUND:
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainKeyNotFound()
|
2021-08-04 22:46:55 +03:00
|
|
|
elif error == KEYCHAIN_ERR_MALFORMED_REQUEST:
|
|
|
|
message = error_details.get("message", "")
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainMalformedRequest(message)
|
2021-08-04 22:46:55 +03:00
|
|
|
else:
|
2022-03-03 00:46:23 +03:00
|
|
|
# Try to construct a more informative error message including the call that failed
|
|
|
|
if "command" in response["data"]:
|
|
|
|
err = f"{response['data'].get('command')} failed with error: {error}"
|
|
|
|
raise Exception(f"{err}")
|
|
|
|
raise Exception(f"{error}")
|
2021-08-04 22:46:55 +03:00
|
|
|
|
2022-11-23 00:34:53 +03:00
|
|
|
async def add_private_key(self, mnemonic: str, label: Optional[str] = None) -> PrivateKey:
|
2021-08-04 22:46:55 +03:00
|
|
|
"""
|
|
|
|
Forwards to Keychain.add_private_key()
|
|
|
|
"""
|
|
|
|
key: PrivateKey
|
|
|
|
if self.use_local_keychain():
|
2022-11-23 00:34:53 +03:00
|
|
|
key = self.keychain.add_private_key(mnemonic, label)
|
2021-08-04 22:46:55 +03:00
|
|
|
else:
|
2022-11-23 00:34:53 +03:00
|
|
|
response, success = await self.get_response_for_request(
|
|
|
|
"add_private_key", {"mnemonic": mnemonic, "label": label}
|
|
|
|
)
|
2021-08-04 22:46:55 +03:00
|
|
|
if success:
|
2022-08-06 17:50:33 +03:00
|
|
|
seed = mnemonic_to_seed(mnemonic)
|
2021-08-04 22:46:55 +03:00
|
|
|
key = AugSchemeMPL.key_gen(seed)
|
|
|
|
else:
|
|
|
|
error = response["data"].get("error", None)
|
|
|
|
if error == KEYCHAIN_ERR_KEYERROR:
|
|
|
|
error_details = response["data"].get("error_details", {})
|
|
|
|
word = error_details.get("word", "")
|
|
|
|
raise KeyError(word)
|
|
|
|
else:
|
|
|
|
self.handle_error(response)
|
|
|
|
|
|
|
|
return key
|
|
|
|
|
2022-08-05 04:28:58 +03:00
|
|
|
async def check_keys(self, root_path: Path) -> None:
|
2021-08-04 22:46:55 +03:00
|
|
|
"""
|
|
|
|
Forwards to init_funcs.check_keys()
|
|
|
|
"""
|
|
|
|
if self.use_local_keychain():
|
|
|
|
check_keys(root_path, self.keychain)
|
|
|
|
else:
|
|
|
|
response, success = await self.get_response_for_request("check_keys", {"root_path": str(root_path)})
|
|
|
|
if not success:
|
|
|
|
self.handle_error(response)
|
|
|
|
|
2022-08-05 04:28:58 +03:00
|
|
|
async def delete_all_keys(self) -> None:
|
2021-08-04 22:46:55 +03:00
|
|
|
"""
|
|
|
|
Forwards to Keychain.delete_all_keys()
|
|
|
|
"""
|
|
|
|
if self.use_local_keychain():
|
|
|
|
self.keychain.delete_all_keys()
|
|
|
|
else:
|
|
|
|
response, success = await self.get_response_for_request("delete_all_keys", {})
|
|
|
|
if not success:
|
|
|
|
self.handle_error(response)
|
|
|
|
|
2022-08-05 04:28:58 +03:00
|
|
|
async def delete_key_by_fingerprint(self, fingerprint: int) -> None:
|
2021-08-04 22:46:55 +03:00
|
|
|
"""
|
|
|
|
Forwards to Keychain.delete_key_by_fingerprint()
|
|
|
|
"""
|
|
|
|
if self.use_local_keychain():
|
|
|
|
self.keychain.delete_key_by_fingerprint(fingerprint)
|
|
|
|
else:
|
|
|
|
response, success = await self.get_response_for_request(
|
|
|
|
"delete_key_by_fingerprint", {"fingerprint": fingerprint}
|
|
|
|
)
|
|
|
|
if not success:
|
|
|
|
self.handle_error(response)
|
|
|
|
|
|
|
|
async def get_all_private_keys(self) -> List[Tuple[PrivateKey, bytes]]:
|
|
|
|
"""
|
|
|
|
Forwards to Keychain.get_all_private_keys()
|
|
|
|
"""
|
|
|
|
keys: List[Tuple[PrivateKey, bytes]] = []
|
|
|
|
if self.use_local_keychain():
|
|
|
|
keys = self.keychain.get_all_private_keys()
|
|
|
|
else:
|
|
|
|
response, success = await self.get_response_for_request("get_all_private_keys", {})
|
|
|
|
if success:
|
|
|
|
private_keys = response["data"].get("private_keys", None)
|
|
|
|
if private_keys is None:
|
|
|
|
err = f"Missing private_keys in {response.get('command')} response"
|
|
|
|
self.log.error(f"{err}")
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainMalformedResponse(f"{err}")
|
2021-08-04 22:46:55 +03:00
|
|
|
else:
|
|
|
|
for key_dict in private_keys:
|
|
|
|
pk = key_dict.get("pk", None)
|
|
|
|
ent_str = key_dict.get("entropy", None)
|
|
|
|
if pk is None or ent_str is None:
|
|
|
|
err = f"Missing pk and/or ent in {response.get('command')} response"
|
|
|
|
self.log.error(f"{err}")
|
|
|
|
continue # We'll skip the incomplete key entry
|
|
|
|
ent = bytes.fromhex(ent_str)
|
|
|
|
mnemonic = bytes_to_mnemonic(ent)
|
2022-08-06 17:50:33 +03:00
|
|
|
seed = mnemonic_to_seed(mnemonic)
|
2021-08-04 22:46:55 +03:00
|
|
|
key = AugSchemeMPL.key_gen(seed)
|
|
|
|
if bytes(key.get_g1()).hex() == pk:
|
|
|
|
keys.append((key, ent))
|
|
|
|
else:
|
|
|
|
err = "G1Elements don't match"
|
|
|
|
self.log.error(f"{err}")
|
|
|
|
else:
|
|
|
|
self.handle_error(response)
|
|
|
|
|
|
|
|
return keys
|
|
|
|
|
|
|
|
async def get_first_private_key(self) -> Optional[PrivateKey]:
|
|
|
|
"""
|
|
|
|
Forwards to Keychain.get_first_private_key()
|
|
|
|
"""
|
|
|
|
key: Optional[PrivateKey] = None
|
|
|
|
if self.use_local_keychain():
|
|
|
|
sk_ent = self.keychain.get_first_private_key()
|
|
|
|
if sk_ent:
|
|
|
|
key = sk_ent[0]
|
|
|
|
else:
|
|
|
|
response, success = await self.get_response_for_request("get_first_private_key", {})
|
|
|
|
if success:
|
|
|
|
private_key = response["data"].get("private_key", None)
|
|
|
|
if private_key is None:
|
|
|
|
err = f"Missing private_key in {response.get('command')} response"
|
|
|
|
self.log.error(f"{err}")
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainMalformedResponse(f"{err}")
|
2021-08-04 22:46:55 +03:00
|
|
|
else:
|
|
|
|
pk = private_key.get("pk", None)
|
|
|
|
ent_str = private_key.get("entropy", None)
|
|
|
|
if pk is None or ent_str is None:
|
|
|
|
err = f"Missing pk and/or ent in {response.get('command')} response"
|
|
|
|
self.log.error(f"{err}")
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainMalformedResponse(f"{err}")
|
2021-08-04 22:46:55 +03:00
|
|
|
ent = bytes.fromhex(ent_str)
|
|
|
|
mnemonic = bytes_to_mnemonic(ent)
|
2022-08-06 17:50:33 +03:00
|
|
|
seed = mnemonic_to_seed(mnemonic)
|
2021-08-04 22:46:55 +03:00
|
|
|
sk = AugSchemeMPL.key_gen(seed)
|
|
|
|
if bytes(sk.get_g1()).hex() == pk:
|
|
|
|
key = sk
|
|
|
|
else:
|
|
|
|
err = "G1Elements don't match"
|
|
|
|
self.log.error(f"{err}")
|
|
|
|
else:
|
|
|
|
self.handle_error(response)
|
|
|
|
|
|
|
|
return key
|
|
|
|
|
|
|
|
async def get_key_for_fingerprint(self, fingerprint: Optional[int]) -> Optional[PrivateKey]:
|
|
|
|
"""
|
|
|
|
Locates and returns a private key matching the provided fingerprint
|
|
|
|
"""
|
|
|
|
key: Optional[PrivateKey] = None
|
|
|
|
if self.use_local_keychain():
|
|
|
|
private_keys = self.keychain.get_all_private_keys()
|
|
|
|
if len(private_keys) == 0:
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainIsEmpty()
|
2021-08-04 22:46:55 +03:00
|
|
|
else:
|
|
|
|
if fingerprint is not None:
|
|
|
|
for sk, _ in private_keys:
|
|
|
|
if sk.get_g1().get_fingerprint() == fingerprint:
|
|
|
|
key = sk
|
|
|
|
break
|
2022-07-30 01:08:37 +03:00
|
|
|
if key is None:
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainKeyNotFound(fingerprint)
|
2021-08-04 22:46:55 +03:00
|
|
|
else:
|
|
|
|
key = private_keys[0][0]
|
|
|
|
else:
|
|
|
|
response, success = await self.get_response_for_request(
|
|
|
|
"get_key_for_fingerprint", {"fingerprint": fingerprint}
|
|
|
|
)
|
|
|
|
if success:
|
|
|
|
pk = response["data"].get("pk", None)
|
|
|
|
ent = response["data"].get("entropy", None)
|
|
|
|
if pk is None or ent is None:
|
|
|
|
err = f"Missing pk and/or ent in {response.get('command')} response"
|
|
|
|
self.log.error(f"{err}")
|
2022-08-05 10:05:58 +03:00
|
|
|
raise KeychainMalformedResponse(f"{err}")
|
2021-08-04 22:46:55 +03:00
|
|
|
else:
|
|
|
|
mnemonic = bytes_to_mnemonic(bytes.fromhex(ent))
|
2022-08-06 17:50:33 +03:00
|
|
|
seed = mnemonic_to_seed(mnemonic)
|
2021-08-04 22:46:55 +03:00
|
|
|
private_key = AugSchemeMPL.key_gen(seed)
|
|
|
|
if bytes(private_key.get_g1()).hex() == pk:
|
|
|
|
key = private_key
|
|
|
|
else:
|
|
|
|
err = "G1Elements don't match"
|
|
|
|
self.log.error(f"{err}")
|
|
|
|
else:
|
|
|
|
self.handle_error(response)
|
|
|
|
|
|
|
|
return key
|
|
|
|
|
2022-11-23 00:34:53 +03:00
|
|
|
async def get_key(self, fingerprint: int, include_secrets: bool = False) -> Optional[KeyData]:
|
|
|
|
"""
|
|
|
|
Locates and returns KeyData matching the provided fingerprint
|
|
|
|
"""
|
|
|
|
key_data: Optional[KeyData] = None
|
|
|
|
if self.use_local_keychain():
|
|
|
|
key_data = self.keychain.get_key(fingerprint, include_secrets)
|
|
|
|
else:
|
|
|
|
response, success = await self.get_response_for_request(
|
|
|
|
"get_key", {"fingerprint": fingerprint, "include_secrets": include_secrets}
|
|
|
|
)
|
|
|
|
if success:
|
|
|
|
key_data = KeyData.from_json_dict(response["data"]["key"])
|
|
|
|
else:
|
|
|
|
self.handle_error(response)
|
|
|
|
return key_data
|
|
|
|
|
|
|
|
async def get_keys(self, include_secrets: bool = False) -> List[KeyData]:
|
|
|
|
"""
|
|
|
|
Returns all KeyData
|
|
|
|
"""
|
|
|
|
keys: List[KeyData] = []
|
|
|
|
if self.use_local_keychain():
|
|
|
|
keys = self.keychain.get_keys(include_secrets)
|
|
|
|
else:
|
|
|
|
response, success = await self.get_response_for_request("get_keys", {"include_secrets": include_secrets})
|
|
|
|
if success:
|
|
|
|
keys = [KeyData.from_json_dict(key) for key in response["data"]["keys"]]
|
|
|
|
else:
|
|
|
|
self.handle_error(response)
|
|
|
|
return keys
|
|
|
|
|
2021-08-04 22:46:55 +03:00
|
|
|
|
|
|
|
def wrap_local_keychain(keychain: Keychain, log: logging.Logger) -> KeychainProxy:
|
|
|
|
"""
|
|
|
|
Wrap an existing local Keychain instance in a KeychainProxy to utilize
|
|
|
|
the same interface as a remote Keychain
|
|
|
|
"""
|
|
|
|
return KeychainProxy(local_keychain=keychain, log=log)
|
|
|
|
|
|
|
|
|
|
|
|
async def connect_to_keychain(
|
|
|
|
self_hostname: str,
|
|
|
|
daemon_port: int,
|
2023-01-06 20:55:20 +03:00
|
|
|
daemon_heartbeat: int,
|
2021-08-04 22:46:55 +03:00
|
|
|
ssl_context: Optional[ssl.SSLContext],
|
|
|
|
log: logging.Logger,
|
2022-08-05 04:28:58 +03:00
|
|
|
user: Optional[str] = None,
|
|
|
|
service: Optional[str] = None,
|
2021-08-04 22:46:55 +03:00
|
|
|
) -> KeychainProxy:
|
|
|
|
"""
|
|
|
|
Connect to the local daemon.
|
|
|
|
"""
|
|
|
|
|
|
|
|
client = KeychainProxy(
|
2023-01-06 20:55:20 +03:00
|
|
|
uri=f"wss://{self_hostname}:{daemon_port}",
|
|
|
|
heartbeat=daemon_heartbeat,
|
|
|
|
ssl_context=ssl_context,
|
|
|
|
log=log,
|
|
|
|
user=user,
|
|
|
|
service=service,
|
2021-08-04 22:46:55 +03:00
|
|
|
)
|
|
|
|
# Connect to the service if the proxy isn't using a local keychain
|
|
|
|
if not client.use_local_keychain():
|
|
|
|
await client.start()
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
async def connect_to_keychain_and_validate(
|
2021-09-24 20:32:57 +03:00
|
|
|
root_path: Path,
|
|
|
|
log: logging.Logger,
|
2022-08-05 04:28:58 +03:00
|
|
|
user: Optional[str] = None,
|
|
|
|
service: Optional[str] = None,
|
2021-08-04 22:46:55 +03:00
|
|
|
) -> Optional[KeychainProxy]:
|
|
|
|
"""
|
|
|
|
Connect to the local daemon and do a ping to ensure that something is really
|
|
|
|
there and running.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
net_config = load_config(root_path, "config.yaml")
|
|
|
|
crt_path = root_path / net_config["daemon_ssl"]["private_crt"]
|
|
|
|
key_path = root_path / net_config["daemon_ssl"]["private_key"]
|
|
|
|
ca_crt_path = root_path / net_config["private_ssl_ca"]["crt"]
|
|
|
|
ca_key_path = root_path / net_config["private_ssl_ca"]["key"]
|
2021-08-16 01:53:54 +03:00
|
|
|
ssl_context = ssl_context_for_client(ca_crt_path, ca_key_path, crt_path, key_path, log=log)
|
2023-01-06 20:55:20 +03:00
|
|
|
daemon_heartbeat = net_config.get("daemon_heartbeat", 300)
|
2021-08-04 22:46:55 +03:00
|
|
|
connection = await connect_to_keychain(
|
2023-01-06 20:55:20 +03:00
|
|
|
net_config["self_hostname"], net_config["daemon_port"], daemon_heartbeat, ssl_context, log, user, service
|
2021-08-04 22:46:55 +03:00
|
|
|
)
|
2021-08-05 19:45:31 +03:00
|
|
|
|
|
|
|
# If proxying to a local keychain, don't attempt to ping
|
|
|
|
if connection.use_local_keychain():
|
|
|
|
return connection
|
|
|
|
|
2022-08-05 04:28:58 +03:00
|
|
|
r = await connection.ping() # this is purposely using the base classes _get method
|
2021-08-04 22:46:55 +03:00
|
|
|
|
|
|
|
if "value" in r["data"] and r["data"]["value"] == "pong":
|
|
|
|
return connection
|
|
|
|
except Exception as e:
|
|
|
|
print(f"Keychain(daemon) not started yet: {e}")
|
|
|
|
return None
|