def get_base_dictionary(): base_dict = {} current_user_name = get_user() base_dict["edi_current_user_name"] = current_user_name base_dict["edi_current_user_group_name"] = get_user_group() base_dict["edi_current_user_ssh_pub_keys"] = get_user_ssh_pub_keys() base_dict["edi_current_user_uid"] = get_user_uid() base_dict["edi_current_user_gid"] = get_user_gid() base_dict[ "edi_current_user_host_home_directory"] = get_user_home_directory( current_user_name) base_dict["edi_current_user_target_home_directory"] = "/home/{}".format( current_user_name) base_dict["edi_host_hostname"] = get_hostname() base_dict["edi_edi_plugin_directory"] = get_edi_plugin_directory() proxy_setup = ProxySetup() base_dict["edi_host_http_proxy"] = proxy_setup.get('http_proxy', default='') base_dict["edi_host_https_proxy"] = proxy_setup.get('https_proxy', default='') base_dict["edi_host_ftp_proxy"] = proxy_setup.get('ftp_proxy', default='') base_dict["edi_host_socks_proxy"] = proxy_setup.get('all_proxy', default='') base_dict["edi_host_no_proxy"] = proxy_setup.get('no_proxy', default='') base_dict["edi_edi_version"] = get_edi_version() base_dict["edi_lxd_version"] = get_lxd_version() base_dict["edi_current_display"] = get_current_display() return base_dict
def get_user_ssh_pub_keys(): """ Search for all ssh public keys of the current user (not the root user when called with sudo). :return: A list of ssh public keys. The list will be empty if the tool ssh is not installed. """ if not edi.lib.helpers.which('ssh'): return [] random_host = ''.join( random.choice(string.ascii_uppercase + string.digits) for _ in range(10)) cmd = ['ssh', '-G', random_host] ssh_config = run(cmd, stdout=subprocess.PIPE).stdout user_home = get_user_home_directory(edi.lib.helpers.get_user()) identity_files = re.findall(r'^identityfile (.*)$', ssh_config, flags=re.MULTILINE) ssh_pub_keys = [] for file in identity_files: expanded_file = re.sub(r'^~', user_home, file) expanded_pub_file = '{}.pub'.format(expanded_file) if os.path.isfile(expanded_file) and os.path.isfile(expanded_pub_file): ssh_pub_keys.append(expanded_pub_file) return ssh_pub_keys
def render_expected_profiles(): user = get_user() dictionary = {'user': user, 'home': get_user_home_directory(user), 'target_home': '/home/{}'.format(user)} expected_profiles = [] for boilerplate in expected_profile_boilerplates: template = Template(boilerplate) expected_profiles.append(normalize_yaml(template.render(dictionary))) return expected_profiles
def test_get_user_home_directory(monkeypatch): def fake_getent_passwd(*popenargs, **kwargs): if get_command(popenargs) == 'getent' and get_sub_command(popenargs) == 'passwd' and \ get_command_parameter(popenargs, 'passwd') == 'john': return subprocess.CompletedProcess("fakerun", 0, stdout='john:x:1000:1000:John Doe,,,:/home/john:/bin/bash\n') else: return subprocess.run(*popenargs, **kwargs) monkeypatch.setattr(mockablerun, 'run_mockable', fake_getent_passwd) assert get_user_home_directory('john') == '/home/john'