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

openssh_keypair: prevent error when path includes no path #50106

Merged
merged 1 commit into from Dec 20, 2018

Conversation

felixfontein
Copy link
Contributor

SUMMARY

Prevents error when path includes no path, only a filename (i.e. references to a file in cwd).

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

openssh_keypair

@ansibot
Copy link
Contributor

ansibot commented Dec 18, 2018

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 bug This issue/PR relates to a bug. community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. support:community This issue/PR relates to code supported by the Ansible community. labels Dec 18, 2018
@MarkusTeufelberger
Copy link
Contributor

Should this stuff really work with relative paths?

@felixfontein
Copy link
Contributor Author

Yes, why should it not?

@felixfontein
Copy link
Contributor Author

(Main use case is local_action, I guess.)

@MarkusTeufelberger
Copy link
Contributor

Well, the module would write private keys somewhere and while I would usually expect to have ansible modules execute in $HOME on a remote machine I'm not sure if the path where it will run is actually specified...?

@felixfontein
Copy link
Contributor Author

With local_action (or hosts: localhost), it will write to the CWD. Supporting this allows to write a little playbook to create a keypair in the current directory. Might be a lot nicer than writing a shell script which does the same than this module :)

Copy link
Contributor

@gundalow gundalow left a comment

Choose a reason for hiding this comment

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

Wonder if it would be worth updating the docs for path to make this clearer.

@@ -272,7 +272,7 @@ def main():
)

# Check if Path exists
base_dir = os.path.dirname(module.params['path'])
base_dir = os.path.dirname(module.params['path']) or '.'
if not os.path.isdir(base_dir):
module.fail_json(
name=base_dir,
Copy link
Member

Choose a reason for hiding this comment

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

Add a note here stating that - where we failed to find the file.

The local/remote directory '%s' ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's already there in line 279. Currently, when base_dir is the empty string, it doesn't read very nice, though (The directory does not exist or the file is not a directory -- watch the double space).

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Dec 19, 2018
@MarkusTeufelberger
Copy link
Contributor

Updating the docs for path would change this for all modules that use this documentation snippet. Do modules generally accept relative and absolute paths?

Also, since the module is only added in the next version, I'm not sure if a changelog entry is needed, but it won't hurt for sure.

For the record, I don't feel very strongly either way on the relative path issue, but I would consider it bad style if I saw it in a playbook that I would expect to be idempotent (unless I've missed something in the docs, as I said I think the working directory of Ansible is not strongly defined and could change).

@felixfontein
Copy link
Contributor Author

Updating the docs for path would change this for all modules that use this documentation snippet.

I wouldn't do that.

Do modules generally accept relative and absolute paths?

Well, all the ones I used / worked on so far don't do such checks on paths, so they do work fine.

Also, since the module is only added in the next version, I'm not sure if a changelog entry is needed, but it won't hurt for sure.

Ah, good point. I completely forgot about that. I'll remove the changelog fragment!

For the record, I don't feel very strongly either way on the relative path issue, but I would consider it bad style if I saw it in a playbook that I would expect to be idempotent (unless I've missed something in the docs, as I said I think the working directory of Ansible is not strongly defined and could change).

I don't think it is documented or advertised anywhere. It's still very useful in some edge cases, like testing or using Ansible instead of a complicated shell script. (In fact, I just tested that, you can put #!/usr/bin/ansible-playbook into the first line of a playbook and make it executable, and then just run it directly.)

I wouldn't really advertise this, but I also don't want that this is made not possible (without jumping through additional hoops by prefixing filenames with ./ etc.) by a check which ignores this case.

@MarkusTeufelberger
Copy link
Contributor

An integration test would also be nice to have for this one, sounds like something that might/should be eventually refactored (if someone goes through the hassle of writing a python library that does the stuff ssh-keygen can do).

@felixfontein
Copy link
Contributor Author

I don't think this part will be affected by a refactoring away from calling ssh-keygen; such a change would probably affect mostly the Keypair class, and maybe the main module logic in main(), but not the path existence check.

@MarkusTeufelberger
Copy link
Contributor

shipit

@ansibot ansibot added automerge This PR was automatically merged by ansibot. shipit This PR is ready to be merged by Core and removed community_review In order to be merged, this PR must follow the community review workflow. labels Dec 20, 2018
@ansibot ansibot merged commit f554818 into ansible:devel Dec 20, 2018
@felixfontein
Copy link
Contributor Author

@MarkusTeufelberger @gundalow @Akasurde thanks for your reviews!

@felixfontein felixfontein deleted the openssh_keypair-fix-basedir branch December 20, 2018 19:37
kbreit pushed a commit to kbreit/ansible that referenced this pull request Jan 11, 2019
@dagwieers dagwieers added the crypto Crypto community (ACME, openssl, letsencrypt) label Feb 7, 2019
@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 automerge This PR was automatically merged by ansibot. bug This issue/PR relates to a bug. crypto Crypto community (ACME, openssl, letsencrypt) module This issue/PR relates to a module. shipit This PR is ready to be merged by Core small_patch support:community This issue/PR relates to code supported by the Ansible community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants