Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Use adb pub key for foundation devices #52

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions esper/controllers/secureadb/secureadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions esper/ext/remoteadb_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from logging import Logger
from typing import Tuple
import socket

from pathlib import Path
import requests


Expand Down Expand Up @@ -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.
Expand All @@ -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}'
Expand Down
2 changes: 2 additions & 0 deletions esper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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):
Expand Down