diff --git a/esper/controllers/secureadb/secureadb.py b/esper/controllers/secureadb/secureadb.py index c6f8322..6e2e6c7 100644 --- a/esper/controllers/secureadb/secureadb.py +++ b/esper/controllers/secureadb/secureadb.py @@ -12,6 +12,7 @@ from esper.ext.remoteadb_api import initiate_remoteadb_connection, fetch_device_certificate, fetch_relay_endpoint, \ RemoteADBError from esper.ext.utils import validate_creds_exists +from cement.utils import fs class SecureADBWorkflowError(Exception): @@ -138,12 +139,20 @@ def connect(self): self.app.render("\nInitiating Remote ADB Session. This may take a few seconds...\n") + # Use client's public adb key if present + try: + client_adb_pub_key_path = fs.abspath(self.app.config.get('esper', 'adb_pub_key')) + except Exception as exc: + client_adb_pub_key_path = "" + self.app.log.debug(f"Exception Encountered while fetching client public adb key -> {exc}") + # Call SCAPI for establish remote adb connection with device remoteadb_id = initiate_remoteadb_connection(environment=db.get_configure().get("environment"), enterprise_id=enterprise_id, device_id=device_id, api_key=db.get_configure().get("api_key"), client_cert_path=self.app.local_cert, + client_adb_pub_key_path=client_adb_pub_key_path, log=self.app.log) # Poll and fetch the TCP relay's endpoint diff --git a/esper/ext/remoteadb_api.py b/esper/ext/remoteadb_api.py index 3f1b3ab..64986d3 100644 --- a/esper/ext/remoteadb_api.py +++ b/esper/ext/remoteadb_api.py @@ -2,7 +2,7 @@ from logging import Logger from typing import Tuple import socket - +from pathlib import Path import requests @@ -177,6 +177,7 @@ def initiate_remoteadb_connection(environment: str, device_id: str, api_key: str, client_cert_path: str, + client_adb_pub_key_path: str, log: Logger) -> str: """ Create a Remote ADB session for given enterprise and device, and return its id. @@ -197,13 +198,25 @@ def initiate_remoteadb_connection(environment: str, # Convert byte stream to utf-8 client_cert = client_cert.decode('utf-8') + adb_pub_key = "" + if Path(client_adb_pub_key_path).exists(): + with open(client_adb_pub_key_path, 'rb') as f: + adb_pub_key = f.read() + # Convert byte stream to utf-8 + adb_pub_key = adb_pub_key.decode('utf-8') + + + adb_pub_key_exists = adb_pub_key != "" + log.debug(f"ADB public key exists: {adb_pub_key_exists}") + log.debug("Initiating RemoteADB connection...") log.debug(f"Creating RemoteADB session at {url}") response = requests.post( url, json={ - 'client_certificate': client_cert + 'client_certificate': client_cert, + 'adb_pub_key': adb_pub_key }, headers={ 'Authorization': f'Bearer {api_key}' diff --git a/esper/main.py b/esper/main.py index 9135e4e..a64f5bf 100644 --- a/esper/main.py +++ b/esper/main.py @@ -34,6 +34,7 @@ CONFIG['esper']['local_key'] = '~/.esper/certs/local.key' CONFIG['esper']['local_cert'] = '~/.esper/certs/local.pem' CONFIG['esper']['device_cert'] = '~/.esper/certs/device.pem' +CONFIG['esper']['adb_pub_key'] = '~/.android/adbkey.pub' # meta defaults META = init_defaults('log.colorlog') @@ -134,6 +135,7 @@ class Meta: TEST_CONFIG['esper']['local_key'] = '~/.esper/certs/local.key' TEST_CONFIG['esper']['local_cert'] = '~/.esper/certs/local.pem' TEST_CONFIG['esper']['device_cert'] = '~/.esper/certs/device.pem' +TEST_CONFIG['esper']['adb_pub_key'] = '~/.android/adbkey.pub' class EsperTest(TestApp, Esper):