def _inject_key_into_fs(key, fs): """Add the given public ssh key to root's authorized_keys. key is an ssh key string. fs is the path to the base of the filesystem into which to inject the key. """ LOG.debug("Inject key fs=%(fs)s key=%(key)s", {'fs': fs, 'key': key}) sshdir = os.path.join('root', '.ssh') fs.make_path(sshdir) fs.set_ownership(sshdir, "root", "root") fs.set_permissions(sshdir, 0o700) keyfile = os.path.join(sshdir, 'authorized_keys') key_data = ''.join([ '\n', '# The following ssh key was injected by Nova', '\n', key.strip(), '\n', ]) _inject_file_into_fs(fs, keyfile, key_data, append=True) fs.set_permissions(keyfile, 0o600) _setup_selinux_for_keys(fs, sshdir)
def _inject_files_into_fs(files, fs): for (path, contents) in files: # NOTE(wangpan): Ensure the parent dir of injecting file exists parent_dir = os.path.dirname(path) if (len(parent_dir) > 0 and parent_dir != "/" and not fs.has_file(parent_dir)): fs.make_path(parent_dir) fs.set_ownership(parent_dir, "root", "root") fs.set_permissions(parent_dir, 0o744) _inject_file_into_fs(fs, path, contents)
def _inject_net_into_fs(net, fs): """Inject /etc/network/interfaces into the filesystem rooted at fs. net is the contents of /etc/network/interfaces. """ LOG.debug("Inject key fs=%(fs)s net=%(net)s", {'fs': fs, 'net': net}) netdir = os.path.join('etc', 'network') fs.make_path(netdir) fs.set_ownership(netdir, "root", "root") fs.set_permissions(netdir, 0o744) netfile = os.path.join('etc', 'network', 'interfaces') _inject_file_into_fs(fs, netfile, net)