def key(self): """ Serves the public SSH key for the user that own the current service """ # look for the ssh key of the current user private_key_path = os.path.expanduser('~/.ssh/id_rsa') public_key_path = os.path.expanduser('~/.ssh/id_rsa.pub') ssh_dir = os.path.dirname(public_key_path) if not os.path.isdir(ssh_dir): logger.warning('.ssh directory not found, creating one at: %s', ssh_dir) mkdir(ssh_dir) # if there isn't one create it if not os.path.exists(public_key_path): logger.warning('expected public key not found: %s', public_key_path) logger.warning('will create new ssh key pair') # create one command = [ 'ssh-keygen', '-q', '-t', 'rsa', '-N', '', '-f', private_key_path, ] out, err, code = process.run(command, send_input='y\n') if code != 0: error(500, err) # define the file to download response.headers['Content-Disposition'] = 'attachment; filename=id_rsa.pub' with open(public_key_path) as key_contents: key = StringIO() key.write(key_contents.read()) key.seek(0) response.app_iter = FileIter(key)
def ensure_ssh_keys(): """ Generate ssh keys as early as possible so that they are available to all web server workers immediately when serving ``/setup/key/``. This helper does not use logging because it is too early in running the application and no logging has been configured yet. """ # look for the ssh key of the current user private_key_path = os.path.expanduser('~/.ssh/id_rsa') public_key_path = os.path.expanduser('~/.ssh/id_rsa.pub') ssh_dir = os.path.dirname(public_key_path) if not os.path.isdir(ssh_dir): mkdir(ssh_dir) # if there isn't one create it if not os.path.exists(public_key_path): # create one command = [ 'ssh-keygen', '-q', '-t', 'rsa', '-N', '', '-f', private_key_path, ] out, err, code = process.run(command, send_input='y\n') if code != 0: raise RuntimeError('ssh-keygen failed: %s %s' % (out, err))
def key(self): """ Serves the public SSH key for the user that own the current service """ # look for the ssh key of the current user private_key_path = os.path.expanduser('~/.ssh/id_rsa') public_key_path = os.path.expanduser('~/.ssh/id_rsa.pub') ssh_dir = os.path.dirname(public_key_path) if not os.path.isdir(ssh_dir): logger.warning('.ssh directory not found, creating one at: %s', ssh_dir) mkdir(ssh_dir) # if there isn't one create it if not os.path.exists(public_key_path): logger.warning('expected public key not found: %s', public_key_path) logger.warning('will create new ssh key pair') # create one command = [ 'ssh-keygen', '-q', '-t', 'rsa', '-N', '', '-f', private_key_path, ] out, err, code = process.run(command, send_input='y\n') if code != 0: error(500, err) # define the file to download response.headers[ 'Content-Disposition'] = 'attachment; filename=id_rsa.pub' with open(public_key_path) as key_contents: key = StringIO() key.write(key_contents.read()) key.seek(0) response.app_iter = FileIter(key)
def test_mkdir_does_not_ignore_existing_dir(self, tmpdir): path = str(tmpdir) with pytest.raises(OSError): util.mkdir(path, exist_ok=False)
def test_mkdir_ignores_existing_dir(self, tmpdir): path = str(tmpdir) util.mkdir(path) assert os.path.isdir(path) is True
def test_mkdir_success(self, tmpdir): path = os.path.join(str(tmpdir), 'mydir') util.mkdir(path) assert os.path.isdir(path) is True