summaryrefslogtreecommitdiffstats
path: root/base/java-tools/bin/pki
blob: ce322a2ec7b853e6beacc28c26bfe39e0875571a (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
130
131
132
#!/usr/bin/python
# Authors:
#     Endi S. Dewata <edewata@redhat.com>
#
# 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 of the License.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright (C) 2014 Red Hat, Inc.
# All rights reserved.
#

from __future__ import absolute_import
import shlex
import subprocess
import sys


def run_java_cli(args):

    # read RESTEasy library path
    value = subprocess.check_output(
        '. /usr/share/pki/etc/pki.conf && . /etc/pki/pki.conf && echo $RESTEASY_LIB',
        shell=True)
    resteasy_lib = value.decode(sys.getfilesystemencoding()).strip()

    # read logging configuration path
    value = subprocess.check_output(
        '. /usr/share/pki/etc/pki.conf && . /etc/pki/pki.conf && echo $LOGGING_CONFIG',
        shell=True)
    logging_config = value.decode(sys.getfilesystemencoding()).strip()

    # construct classpath
    classpath = [
        '/usr/share/java/commons-cli.jar',
        '/usr/share/java/commons-codec.jar',
        '/usr/share/java/commons-httpclient.jar',
        '/usr/share/java/commons-io.jar',
        '/usr/share/java/commons-lang.jar',
        '/usr/share/java/commons-logging.jar',
        '/usr/share/java/httpcomponents/httpclient.jar',
        '/usr/share/java/httpcomponents/httpcore.jar',
        '/usr/share/java/jackson/jackson-core-asl.jar',
        '/usr/share/java/jackson/jackson-jaxrs.jar',
        '/usr/share/java/jackson/jackson-mapper-asl.jar',
        '/usr/share/java/jackson/jackson-mrbean.jar',
        '/usr/share/java/jackson/jackson-smile.jar',
        '/usr/share/java/jackson/jackson-xc.jar',
        '/usr/share/java/jaxb-api.jar',
        '/usr/share/java/ldapjdk.jar',
        '/usr/share/java/servlet.jar',
        resteasy_lib + '/jaxrs-api.jar',
        resteasy_lib + '/resteasy-atom-provider.jar',
        resteasy_lib + '/resteasy-client.jar',
        resteasy_lib + '/resteasy-jaxb-provider.jar',
        resteasy_lib + '/resteasy-jaxrs.jar',
        resteasy_lib + '/resteasy-jaxrs-jandex.jar',
        resteasy_lib + '/resteasy-jackson-provider.jar',
        '/usr/share/java/pki/pki-nsutil.jar',
        '/usr/share/java/pki/pki-cmsutil.jar',
        '/usr/share/java/pki/pki-certsrv.jar',
        '/usr/share/java/pki/pki-tools.jar',
        '/usr/lib64/java/jss4.jar',
        '/usr/lib/java/jss4.jar'
    ]

    command = [
        'java',
        '-cp',
        ':'.join(classpath),
        '-Djava.util.logging.config.file=' + logging_config,
        'com.netscape.cmstools.cli.MainCLI'
    ]

    command.extend(args)

    rv = subprocess.call(command)
    exit(rv)


# pylint: disable=W0613
def run_python_cli(args):

    raise Exception('Not implemented')


def main(argv):

    # read global options
    value = subprocess.check_output(
        '. /etc/pki/pki.conf && echo $PKI_CLI_OPTIONS',
        shell=True)
    value = value.decode(sys.getfilesystemencoding()).strip()
    args = shlex.split(value)
    args.extend(argv[1:])

    client_type = 'java'

    new_args = []

    # read --client-type parameter and remove it from the argument list
    i = 0
    while i < len(args):
        if args[i] == '--client-type':
            client_type = args[i + 1]
            i = i + 1

        else:
            new_args.append(args[i])

        i = i + 1

    if client_type == 'java':
        run_java_cli(new_args)

    elif client_type == 'python':
        run_python_cli(new_args)

    else:
        raise Exception('Unsupported client type: ' + client_type)

if __name__ == '__main__':
    main(sys.argv)