From 24ddb1e23538a53761efdae35c2a15f6f3ab9939 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Fri, 20 Jul 2018 16:09:51 +0300 Subject: [PATCH] Implement public key import from plain http url This commit will enable generic http/https ssh key importer that does not depend on pre existing support for special API. Quite many diferent services already have support for providing ssh public keys in a plain text authorized_keys file format. Also setting it up on your own controlled http server is quite easy. Just give ssh-import-id a url with ssh keys and it will do the rest. Examples: Import from self hosted https server ssh-import-id https://arti.ee/keys Import from a Gogs git service ssh-imoprt-id https://try.gogs.io/unknwon.keys Import from Debian GitLab instance ssh-import-id https://salsa.debian.org/artizirk-guest.keys Import from Github without using the json api ssh-import-id https://github.com/artizirk.keys --- ssh_import_id/__init__.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ssh_import_id/__init__.py b/ssh_import_id/__init__.py index d2f845e..66d2d66 100644 --- a/ssh_import_id/__init__.py +++ b/ssh_import_id/__init__.py @@ -228,6 +228,8 @@ def fetch_keys(proto, username, useragent): return fetch_keys_lp(username, useragent) elif proto == "gh": return fetch_keys_gh(username, useragent) + elif proto in ("http", "https"): + return fetch_keys_http("{}:{}".format(proto, username), useragent) die("ssh-import-id protocol handler %s: not found or cannot execute" % (proto)) @@ -358,6 +360,21 @@ def fetch_keys_gh(ghid, useragent): return keys +def fetch_keys_http(url, useragent): + """ + fetch a plain text authorized_keys file via http/https + """ + keys = "" + try: + headers = {'User-Agent': user_agent(useragent)} + resp = requests.get(url, headers=headers, verify=True) + resp.raise_for_status() + keys = resp.text + # pylint: disable=broad-except + except Exception as e: + die(str(e)) + return keys + def main(): errors = [] try: -- 2.18.0