summaryrefslogtreecommitdiffstats
path: root/ipalib/plugins/cert.py
blob: c900e94377de022200e216c91f0c07f31cd8e6a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Authors:
#   Andrew Wnuk <awnuk@redhat.com>
#   Jason Gerard DeRose <jderose@redhat.com>
#
# Copyright (C) 2009  Red Hat
# see file 'COPYING' for use and warranty information
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

"""
Command plugins for IPA-RA certificate operations.
"""

from ipalib import api, Command, Str, Int


class cert_request(Command):
    """
    Submit a certificate singing request.
    """

    takes_args = ('csr',)

    takes_options = (
        Str('request_type?', default=u'pkcs10', autofill=True),
    )

    def execute(self, csr, **options):
        return self.Backend.ra.request_certificate(csr, **options)

    def output_for_cli(self, textui, result, *args, **options):
        if isinstance(result, dict) and len(result) > 0:
            textui.print_entry(result, 0)
        else:
            textui.print_plain('Failed to submit a certificate request.')

api.register(cert_request)


class cert_status(Command):
    """
    Check status of a certificate signing request.
    """

    takes_args = ['request_id']


    def execute(self, request_id, **options):
        return self.Backend.ra.check_request_status(request_id)

    def output_for_cli(self, textui, result, *args, **options):
        if isinstance(result, dict) and len(result) > 0:
            textui.print_entry(result, 0)
        else:
            textui.print_plain('Failed to retrieve a request status.')

api.register(cert_status)


class cert_get(Command):
    """
    Retrieve an existing certificate.
    """

    takes_args = ['serial_number']

    def execute(self, serial_number):
        return self.Backend.ra.get_certificate(serial_number)

    def output_for_cli(self, textui, result, *args, **options):
        if isinstance(result, dict) and len(result) > 0:
            textui.print_entry(result, 0)
        else:
            textui.print_plain('Failed to obtain a certificate.')

api.register(cert_get)


class cert_revoke(Command):
    """
    Revoke a certificate.
    """

    takes_args = ['serial_number']

    # FIXME: The default is 0.  Is this really an Int param?
    takes_options = [Int('revocation_reason?', default=0)]


    def execute(self, serial_number, **options):
        return self.Backend.ra.revoke_certificate(serial_number, **options)

    def output_for_cli(self, textui, result, *args, **options):
        if isinstance(result, dict) and len(result) > 0:
            textui.print_entry(result, 0)
        else:
            textui.print_plain('Failed to revoke a certificate.')

api.register(cert_revoke)


class cert_remove_hold(Command):
    """
    Take a revoked certificate off hold.
    """

    takes_args = ['serial_number']

    def execute(self, serial_number, **options):
        return self.Backend.ra.take_certificate_off_hold(serial_number)

    def output_for_cli(self, textui, result, *args, **options):
        if isinstance(result, dict) and len(result) > 0:
            textui.print_entry(result, 0)
        else:
            textui.print_plain('Failed to take a revoked certificate off hold.')

api.register(cert_remove_hold)