Skip to content

Commit

Permalink
Adds support for v3 Onion to SSH inventory script
Browse files Browse the repository at this point in the history
Preserves the v2 Onion lookup logic, but prefers v3 Onions if those are
found on the Admin Workstation. Ensures that Admins connecting to the
servers after migrating to v3 Onion URLs are using them for SSH.
  • Loading branch information
Conor Schaefer committed Aug 22, 2019
1 parent 7b302c0 commit bbad1cb
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions install_files/ansible-base/inventory-dynamic
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def lookup_admin_username():
return admin_username


def lookup_tor_hostname(hostname):
def lookup_tor_v2_hostname(hostname):
"""
Extract Onion URL from HidServAuth file that was fetched back locally.
Extract Onion v2 URL from HidServAuth file that was fetched back locally.
Returns Onion URL for given inventory hostname.
"""
aths_path = os.path.join(SECUREDROP_ANSIBLE_DIRECTORY,
Expand All @@ -106,13 +106,32 @@ def lookup_tor_hostname(hostname):
# assuming the file is a raw `hostname` file generated by tor,
# but the SD playbooks format the line with `HidServAuth` prefix,
# so it can be concatenated into the torrc file on Tails.
tor_hostname = tor_config[1]
tor_v2_hostname = tor_config[1]
except IndexError:
msg = ("Tor config file for '{}' ",
msg = ("Tor v2 config file for '{}' ",
"appears to be empty").format(hostname)
raise Exception(msg=msg)

return tor_hostname
return tor_v2_hostname


def lookup_tor_v3_hostname(hostname):
"""
Extract Onion v3 URL from .auth_private file that was fetched back locally.
Returns Onion URL for given inventory hostname.
"""
aths_path = os.path.join(SECUREDROP_ANSIBLE_DIRECTORY,
"{}-ssh.auth_private".format(hostname))
with io.open(aths_path, 'r') as f:
tor_config = f.readline().rstrip().split(":")
try:
tor_v3_hostname = "{}.onion".format(tor_config[0])
except IndexError:
msg = ("Tor v3 config file for '{}' ",
"appears to be empty").format(hostname)
raise Exception(msg=msg)

return tor_v3_hostname


def lookup_ssh_address(hostname):
Expand All @@ -122,10 +141,13 @@ def lookup_ssh_address(hostname):
"""
ssh_address = lookup_local_ipv4_address(hostname)
try:
ssh_address = lookup_tor_hostname(hostname)
ssh_address = lookup_tor_v3_hostname(hostname)
# Don't assume ATHS files are present; they won't be on first run.
except (IndexError, EnvironmentError):
pass
try:
ssh_address = lookup_tor_v2_hostname(hostname)
except (IndexError, EnvironmentError):
pass

return ssh_address

Expand Down

0 comments on commit bbad1cb

Please sign in to comment.