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

csrgen: Automate full cert request flow #434

Closed
wants to merge 3 commits into from

Conversation

LiptonB
Copy link
Contributor

@LiptonB LiptonB commented Feb 4, 2017

Adds --autogenerate flag to ipa cert-request command. It no longer
requires a CSR passed on the command line, instead it creates a config
(bash script) with cert-get-requestdata, then runs it to build a CSR,
and submits that CSR.

Example usage (NSS database):
$ ipa cert-request --autogenerate --principal blipton --profile-id userCert --database /tmp/certs

Example usage (PEM private key file):
$ ipa cert-request --autogenerate --principal blipton --profile-id userCert --private-key /tmp/key.pem

@MartinBasti
Copy link
Contributor

  • pylint:
************* Module ipaclient.plugins.cert
ipaclient/plugins/cert.py:102: [W1612(unicode-builtin), cert_request.forward] unicode built-in referenced)
ipaclient/plugins/cert.py:127: [W1612(unicode-builtin), cert_request.forward] unicode built-in referenced)
ipaclient/plugins/cert.py:99: [W0612(unused-variable), cert_request.forward] Unused variable 'requestdata')

for unicode you can use etiher six.string_type() or

if six.PY3:
    unicode = str
  • pep8 errors
  • failing test expects DN object instead of String

yield arg

def forward(self, *keys, **options):
autogenerate = options.pop('autogenerate', False)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think autogenerate needs to (or should) be a separate option:

    def forward(self, csr=None, **options):
        autogenerate = csr is None
        ...

'private_key?',
label=_('Path to private key file'),
doc=_('Path to PEM file containing a private key'),
),
Copy link
Contributor

Choose a reason for hiding this comment

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

You will most certainly need a password (file) as well for the NSS database or private key.

helper_args = [private_key]
else:
raise errors.InvocationError(
format="One of 'database' or 'private_key' is required")
Copy link
Contributor

Choose a reason for hiding this comment

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

You should use message= here.

scriptfile.close()
csrfile = tempfile.NamedTemporaryFile(delete=False)
csrfile.close()
csrfilename = csrfile.name
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to put these into a with statement so that they are deleted automatically after use:

    with NamedTemporaryFile() as scriptfile, NamedTemporaryFile() as csrfile:
        ...

# necessary
profile_id = options.get('profile_id')
if profile_id is None:
profile_id = self.get_default_of('profile_id')
Copy link
Contributor

Choose a reason for hiding this comment

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

I know I suggested this to you, but I didn't realize it would require hard-coding the default profile name into profile_id in server-side cert_request. I would rather not do that and instead use dogtag.DEFAULT_PROFILE as the default value of profile_id in cert_get_requestdata. I'm aware that this goes against the idea of having the server decide what is the default profile, but cert_get_requestdata will be moved to the server, so this is only temporary.

raise errors.CertificateOperationError(
error=(
_('Error running "%(cmd)s" to generate CSR') %
{'cmd': ' '.join(helper_cmd)}))
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to at least log the original error, or use it as part of the new error message. Ditto for the IOError below.

return rv
else:
if database is not None or private_key is not None:
raise errors.OptionError(format=_(
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a MutuallyExclusiveError.

@LiptonB
Copy link
Contributor Author

LiptonB commented Feb 9, 2017

Thanks for the comments, and sorry about submitting this with lint errors. I think I've followed all of your suggestions, let me know what you think.

message=u"One of 'database' or 'private_key' is required")

with tempfile.NamedTemporaryFile(
) as scriptfile, tempfile.NamedTemporaryFile() as csrfile:
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: if you from tempfile import NamedTemporaryFile, the you might be able to fit this into a single line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Had to shorten the name more to keep the descriptive variable names, is this ok?

raise errors.MutuallyExclusiveError(reason=_(
"Options 'database' and 'private_key' are not compatible"
" with 'csr'"))
return super(cert_request, self).forward(csr, **options)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: if you move this call one intendation level to the left, you will be able to remove the same call above.

@HonzaCholasta
Copy link
Contributor

Thank you. LGTM, but please squash the fixup commit.

Allows the `ipa cert-request` command to generate its own CSR. It no
longer requires a CSR passed on the command line, instead it creates a
config (bash script) with `cert-get-requestdata`, then runs it to build
a CSR, and submits that CSR.

Example usage (NSS database):
$ ipa cert-request --principal host/test.example.com --profile-id caIPAserviceCert --database /tmp/certs

Example usage (PEM private key file):
$ ipa cert-request --principal host/test.example.com --profile-id caIPAserviceCert --private-key /tmp/key.pem

https://fedorahosted.org/freeipa/ticket/4899
In case users want multiple CSR generation profiles that work with the
same dogtag profile, or in case the profiles are not named the same,
this flag allows specifying an alternative CSR generation profile.

https://fedorahosted.org/freeipa/ticket/4899
@LiptonB
Copy link
Contributor Author

LiptonB commented Feb 28, 2017

@HonzaCholasta thanks, updated!

@HonzaCholasta HonzaCholasta added the ack Pull Request approved, can be merged label Feb 28, 2017
@HonzaCholasta HonzaCholasta added the pushed Pull Request has already been pushed label Feb 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ack Pull Request approved, can be merged pushed Pull Request has already been pushed
Projects
None yet
3 participants