Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple REX options. See issues #204 #210 #220

Closed
wants to merge 1 commit into from
Closed

Add multiple REX options. See issues #204 #210 #220

wants to merge 1 commit into from

Conversation

alosadagrande
Copy link
Contributor

@alosadagrande alosadagrande commented Sep 8, 2017

Add multiple options to remote execution in order to solve enhancement #204 and #210:

--rex-capsules: Comma separated list of capsules to install Foreman's SSH keys for remote execution. It will iterate all over the list of capsules. If one fails user is notified by the task keeps going until all capsules were tried.

--rex-urlkeyfile: HTTP/S location to install a file containing one or multiple Foreman's SSH keys for remote execution. Instead of iterate you can create file on /pub folder that contains all ssh public keys of several capsules and tell bootstrap.py to fech it and install on the local user authorized keys file.

--rex-authpath. Local folder where is placed the authorized_keys file to install Foreman's SSH keys for remote execution. Default ~/.ssh. I found that several customers specifies AuthorizedKeysFile on sshd_config on a different path.

bootstrap.py Outdated
@@ -880,6 +889,9 @@ def exec_service(service, command, failonerror=True):
parser.add_option("--unmanaged", dest="unmanaged", action="store_true", help="Add the server as unmanaged. Useful to skip provisioning dependencies.")
parser.add_option("--rex", dest="remote_exec", action="store_true", help="Install Foreman's SSH key for remote execution.", default=False)
parser.add_option("--rex-user", dest="remote_exec_user", default="root", help="Local user used by Foreman's remote execution feature.")
parser.add_option("--rex-capsules", dest="remote_exec_capsules", default="", help="Comma separated list of capsules to install Foreman's SSH keys for remote execution.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no default, you can omit the default key completely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

bootstrap.py Outdated
@@ -449,12 +458,12 @@ def install_foreman_ssh_key():
return
if os.path.isfile(foreman_ssh_authfile):
if foreman_ssh_key in open(foreman_ssh_authfile, 'r').read():
print_generic("Foreman's SSH key is already present in %s" % foreman_ssh_authfile)
print_generic("Foreman's SSH key/s already present in %s" % foreman_ssh_authfile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change? do you want the sentence to be "ssh key (or keys) already present"? given the function still adds one key at a time, I think we should not change that. But we could add the remote-url to indicate which key was present?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Depending on the rex option you're adding one key at a time or multiple when copying a remote file containing multiples keys. So if adding multiples you're checking multiple keys at a time.

bootstrap.py Outdated
return
output = os.fdopen(os.open(foreman_ssh_authfile, os.O_WRONLY | os.O_CREAT, 0600), 'a')
output.write(foreman_ssh_key)
os.chown(foreman_ssh_authfile, userpw.pw_uid, userpw.pw_gid)
print_generic("Foreman's SSH key was added to %s" % foreman_ssh_authfile)
print_generic("Foreman's SSH key/s added to %s" % foreman_ssh_authfile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above with s/present/added/ ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. same

bootstrap.py Outdated
if not os.path.isdir(foreman_ssh_dir):
os.mkdir(foreman_ssh_dir, 0700)
os.chown(foreman_ssh_dir, userpw.pw_uid, userpw.pw_gid)
try:
foreman_ssh_key = urllib2.urlopen("https://%s:9090/ssh/pubkey" % options.foreman_fqdn).read()
foreman_ssh_key = urllib2.urlopen("%s" % remote_url).read()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foreman_ssh_key = urllib2.urlopen(remote_url).read()

no need for string format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

bootstrap.py Outdated
print_error("Foreman's SSH keys not installed. Directory where authorized_keys file must be located is not found: %s" % options.remote_exec_authpath)
return
else:
foreman_ssh_dir = os.sep.join([userpw.pw_dir, '.ssh'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we make the default value of options.remote_exec_authpath to be os.path.join(userpw.pw_dir, '.ssh') then we could skip this if/else block.

note that I changed os.sep.join([userpw.pw_dir, '.ssh']) to os.path.join(userpw.pw_dir, '.ssh') which I think is more correct here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually from the first comment options.remote_exec_authpath is the complete path to authorizedKey file not a dir.

bootstrap.py Outdated
if options.remote_exec_authpath:
if os.path.isdir(options.remote_exec_authpath):
foreman_ssh_dir = options.remote_exec_authpath
foreman_ssh_authfile = options.remote_exec_authpath + "/authorized_keys"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no requirement for the file to be called authorized_keys. In fact, some of my boxes have AuthorizedKeysFile /etc/ssh/authorized_keys/%u (with %u being the username). So I think we just should take the full path at the command line and not alter it at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

evgeni
evgeni previously requested changes Sep 15, 2017
os.mkdir(foreman_ssh_dir, 0700)
os.chown(foreman_ssh_dir, userpw.pw_uid, userpw.pw_gid)
elif not os.path.isfile(options.remote_exec_authpath):
print_error("Foreman's SSH key not installed. File where authorized_keys must be located is not found: %s" % options.remote_exec_authpath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? We can just create the file (and we actually do)?

Copy link
Contributor Author

@alosadagrande alosadagrande Sep 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the case when the path is entered by user. I think must be warned.

If there is no remote_exec_autpath we pick the default one (~/.ssh/authorized_keys) where we create the folder if needed

Copy link
Contributor Author

@alosadagrande alosadagrande Sep 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not create the complete path to the remote_exec_autpath if it's entered by user, otherwise we'll probably face security issues in the future.

bootstrap.py Outdated
@@ -880,6 +885,9 @@ def exec_service(service, command, failonerror=True):
parser.add_option("--unmanaged", dest="unmanaged", action="store_true", help="Add the server as unmanaged. Useful to skip provisioning dependencies.")
parser.add_option("--rex", dest="remote_exec", action="store_true", help="Install Foreman's SSH key for remote execution.", default=False)
parser.add_option("--rex-user", dest="remote_exec_user", default="root", help="Local user used by Foreman's remote execution feature.")
parser.add_option("--rex-capsules", dest="remote_exec_capsules", help="Comma separated list of capsules to install Foreman's SSH keys for remote execution.")
parser.add_option("--rex-urlkeyfile", dest="remote_exec_url", help="HTTP/S location to install a file containing one or multiple Foreman's SSH keys for remote execution.")
parser.add_option("--rex-authpath", dest="remote_exec_authpath", help="Local folder where is placed the authorized_keys file to install Foreman's SSH keys for remote execution. Default ~/.ssh")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local file, not folder :)

bootstrap.py Outdated
os.chown(foreman_ssh_dir, userpw.pw_uid, userpw.pw_gid)
if not options.remote_exec_authpath:
userpw = pwd.getpwnam(options.remote_exec_user)
options.remote_exec_authpath = os.path.join(*[userpw.pw_dir, '.ssh', 'authorized_keys'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.path.join(userpw.pw_dir, '.ssh', 'authorized_keys') should do it too, no?

bootstrap.py Outdated
if not options.remote_exec_authpath:
userpw = pwd.getpwnam(options.remote_exec_user)
options.remote_exec_authpath = os.path.join(*[userpw.pw_dir, '.ssh', 'authorized_keys'])
foreman_ssh_dir = os.path.join(*[userpw.pw_dir, '.ssh'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@evgeni
Copy link
Member

evgeni commented Apr 18, 2018

rebased and squashed this (thanks @fcami for the rebase work)

--rex-capsules: Comma separated list of capsules to install Foreman's
SSH keys for remote execution. It will iterate all over the list of
capsules. If one fails user is notified by the task keeps going until
all capsules were tried.

--rex-urlkeyfile: HTTP/S location to install a file containing one or
multiple Foreman's SSH keys for remote execution. Instead of iterate
you can create file on /pub folder that contains all ssh public keys
of several capsules and tell bootstrap.py to fech it and install on
the local user authorized keys file.

--rex-authpath. Local folder where is placed the authorized_keys file
to install Foreman's SSH keys for remote execution. Default ~/.ssh.
I found that several customers specifies AuthorizedKeysFile on
sshd_config on a different path.

Closes: #204
Closes: #210
@evgeni
Copy link
Member

evgeni commented Apr 18, 2018

@sideangleside as I got my hands dirty in here, you'll have to do the final review

@evgeni evgeni requested a review from sideangleside April 18, 2018 09:32
@evgeni evgeni dismissed their stale review April 18, 2018 09:32

did changes

@beav
Copy link

beav commented Jun 15, 2018

@alosadagrande it looks like this needs a rebase

@fcami
Copy link
Contributor

fcami commented Jun 15, 2018

@beav @alosadagrande @evgeni I'll rebase

@fcami
Copy link
Contributor

fcami commented Jun 15, 2018

@beav @evgeni could you please review and pull from https://github.com/fcami/katello-client-bootstrap/tree/multi-key-rex - it seems OK to me but I haven't tested it yet.

@beav
Copy link

beav commented Jun 15, 2018

@fcami the change looks OK to me but I didn't test it either.

If @alosadagrande doesn't update this PR, the best bet is for you to create a new branch, then do "git cherry-pick -x 299b034" and resolve the conflict, then do "git add bootstrap.py" and "git commit". That way it will keep the authorship information correct and you can then create a new PR and we can close this one out.

@fcami
Copy link
Contributor

fcami commented Jun 15, 2018

@beav thanks - please see #253 then

@evgeni
Copy link
Member

evgeni commented Dec 18, 2018

obsoleted by #281

@evgeni evgeni closed this Dec 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants