The --out option wasn't working at all with cert-show.

Also fix some related problems in write_certificate(), handle
either a DER or base64-formatted incoming certificate and don't
explode if the filename is None.

ticket 954
This commit is contained in:
Rob Crittenden 2011-02-11 18:12:02 -05:00
parent 0e4f0528cf
commit dab452442d
3 changed files with 14 additions and 5 deletions

View File

@ -320,7 +320,7 @@ output: Output('result', None, None)
command: cert_show command: cert_show
args: 1,1,1 args: 1,1,1
arg: Str('serial_number', label=Gettext('Serial number', domain='ipa', localedir=None)) arg: Str('serial_number', label=Gettext('Serial number', domain='ipa', localedir=None))
option: Str('out?',tr('out?', doc=Gettext('file to store certificate in', domain='ipa', localedir=None)) option: Str('out?', exclude='webui', label=Gettext('Output filename', domain='ipa', localedir=None))
output: Output('result', None, None) output: Output('result', None, None)
command: cert_status command: cert_status
args: 1,0,1 args: 1,0,1

View File

@ -418,13 +418,15 @@ class cert_show(VirtualCommand):
takes_options = ( takes_options = (
Str('out?', Str('out?',
label=_('Output filename'),
doc=_('file to store certificate in'), doc=_('file to store certificate in'),
exclude='webui',
), ),
) )
operation="retrieve certificate" operation="retrieve certificate"
def execute(self, serial_number): def execute(self, serial_number, **options):
hostname = None hostname = None
try: try:
self.check_access() self.check_access()
@ -455,9 +457,8 @@ class cert_show(VirtualCommand):
if 'out' in options: if 'out' in options:
check_writable_file(options['out']) check_writable_file(options['out'])
result = super(cert_show, self).forward(*keys, **options) result = super(cert_show, self).forward(*keys, **options)
if 'usercertificate' in result['result']: if 'certificate' in result['result']:
write_certificate(result['result']['usercertificate'][0], options['out']) write_certificate(result['result']['certificate'], options['out'])
result['summary'] = _('Certificate stored in file \'%(file)s\'') % dict(file=options['out'])
return result return result
else: else:
raise errors.NoCertificateError(entry=keys[-1]) raise errors.NoCertificateError(entry=keys[-1])

View File

@ -231,6 +231,8 @@ def check_writable_file(filename):
Determine if the file is writable. If the file doesn't exist then Determine if the file is writable. If the file doesn't exist then
open the file to test writability. open the file to test writability.
""" """
if filename is None:
raise errors.FileError(reason='Filename is empty')
try: try:
if file_exists(filename): if file_exists(filename):
if not os.access(filename, os.W_OK): if not os.access(filename, os.W_OK):
@ -255,6 +257,12 @@ def write_certificate(cert, filename):
""" """
Check to see if the certificate should be written to a file and do so. Check to see if the certificate should be written to a file and do so.
""" """
if cert and util.isvalid_base64(cert):
try:
cert = base64.b64decode(cert)
except Exception, e:
raise errors.Base64DecodeError(reason=str(e))
try: try:
fp = open(filename, 'w') fp = open(filename, 'w')
fp.write(make_pem(base64.b64encode(cert))) fp.write(make_pem(base64.b64encode(cert)))