summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/cms/servlet/admin/KRAConnectorProcessor.java
blob: 61536f3464f424a6a701a7a83ba2c923b06cfc78 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// --- BEGIN COPYRIGHT BLOCK ---
// 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.
//
// (C) 2013 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cms.servlet.admin;

import java.util.ArrayList;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

import com.netscape.certsrv.apps.CMS;
import com.netscape.certsrv.base.BadRequestException;
import com.netscape.certsrv.base.EBaseException;
import com.netscape.certsrv.base.EPropertyNotFound;
import com.netscape.certsrv.base.IConfigStore;
import com.netscape.certsrv.base.PKIException;
import com.netscape.certsrv.ca.ICAService;
import com.netscape.certsrv.ca.ICertificateAuthority;
import com.netscape.certsrv.connector.IConnector;
import com.netscape.certsrv.system.KRAConnectorInfo;
import com.netscape.cms.servlet.processors.Processor;

/**
 * @author Ade Lee
 */
public class KRAConnectorProcessor extends Processor {
    private boolean connectorExists = false;

    // Connector constants
    public final static String PREFIX = "ca.connector.KRA";

    public KRAConnectorProcessor(Locale locale) throws EPropertyNotFound, EBaseException {
        super("kraconnector", locale);
        ICertificateAuthority ca = (ICertificateAuthority)CMS.getSubsystem("ca");
        ICAService caService = (ICAService)ca.getCAService();
        connectorExists = (caService.getKRAConnector() != null)? true:false;
    }

    public void removeConnector(String newHost, String newPort) throws EPropertyNotFound, EBaseException {
        if (! connectorExists) {
            CMS.debug("removeConnector: no KRA connector exists, returning success");
            return;
        }

        if ((newHost == null) || (newPort == null)) {
            CMS.debug("removeConnector: malformed request.  newHost or newPort is null");
            throw new BadRequestException("Bad Request: KRA Host or Port not defined");
        }
        IConfigStore cs = CMS.getConfigStore();
        String host = cs.getString(PREFIX + ".host");
        String port = cs.getString(PREFIX + ".port");

        if ((host == null) || (port == null)) {
            CMS.debug("removeConnector: bad connector configuration - host or port are null");
            throw new PKIException("Bad Connector configuration on this CA");
        }

        String hostport = newHost + ":" + newPort;
        if ((host.equals(newHost)) && port.equals(newPort)) {
            CMS.debug("removeConnector: Removing " + PREFIX + " substore");
            cs.removeSubStore(PREFIX);
            cs.commit(true);
            deleteConnector();
        } else if (host.indexOf(' ') != -1) { // host is a list
            String[] hostList = host.trim().split(" ");
            ArrayList<String> finalList = new ArrayList<String>();
            for (String h : hostList) {
                if (! h.equals(hostport)) {
                    finalList.add(h);
                }
            }
            if (finalList.size() == hostList.length) {
                CMS.debug("removeConnector: no connector for " + hostport + " exists. Returning success");
                return;
            }

            CMS.debug("removeConnector: Removing " + hostport + " from " + PREFIX);

            if (finalList.size() == 0) {
                CMS.debug("removeConnector: Removing " + PREFIX + " substore");
                cs.removeSubStore(PREFIX);
                cs.commit(true);
                deleteConnector();
            } else if (finalList.size() == 1) {
                cs.putString(PREFIX + ".host", finalList.get(0).split(":")[0]);
                cs.putString(PREFIX + ".port", finalList.get(0).split(":")[1]);
                cs.commit(true);
                replaceConnector();
            } else {
                String finalString = StringUtils.join(finalList, " ");
                cs.putString(PREFIX + ".host", finalString.trim());
                cs.commit(true);
                replaceConnector();
            }
        } else {
            CMS.debug("removeConnector: no connector for " + hostport + " exists. Returning success");
        }
    }

    public void stopConnector() {
        ICertificateAuthority ca = (ICertificateAuthority)CMS.getSubsystem("ca");
        ICAService caService = (ICAService)ca.getCAService();
        IConnector kraConnector = caService.getKRAConnector();
        if (kraConnector != null) {
            kraConnector.stop();
        }
    }

    public void startConnector() {
        ICertificateAuthority ca = (ICertificateAuthority)CMS.getSubsystem("ca");
        ICAService caService = (ICAService)ca.getCAService();
        IConnector kraConnector = caService.getKRAConnector();
        if (kraConnector != null) {
            kraConnector.start();
        }
    }

    public void replaceConnector() throws EBaseException {
        // stop the old connector
        stopConnector();

        ICertificateAuthority ca = (ICertificateAuthority)CMS.getSubsystem("ca");
        ICAService caService = (ICAService)ca.getCAService();
        IConfigStore cs = CMS.getConfigStore();

        IConnector kraConnector = caService.getConnector(cs.getSubStore(PREFIX));
        caService.setKRAConnector(kraConnector);

        startConnector();
    }

    public void deleteConnector() {
        stopConnector();

        ICertificateAuthority ca = (ICertificateAuthority)CMS.getSubsystem("ca");
        ICAService caService = (ICAService)ca.getCAService();
        caService.setKRAConnector(null);
    }

    public void addConnector(KRAConnectorInfo info) throws EPropertyNotFound, EBaseException {
        IConfigStore cs = CMS.getConfigStore();
        String newHost = info.getHost();
        String newPort = info.getPort();
        String newTransportCert = info.getTransportCert();

        if ((newHost == null) || (newPort == null) || (newTransportCert == null)) {
            CMS.debug("addConnector: malformed request.  newHost, newPort or transport cert is null");
            throw new BadRequestException("Bad Request: KRA host, port or transport cert not defined");
        }

        if (connectorExists) {
            String host = cs.getString(PREFIX + ".host");
            String port = cs.getString(PREFIX + ".port");

            if ((!host.equals(newHost)) || (!port.equals(newPort))) { //existing connector is not the same

                // check transport cert
                String transportCert = cs.getString(PREFIX + ".transportCert");
                if (!transportCert.equals(newTransportCert)) {
                    CMS.debug("addConnector: Connector is already defined");
                    throw new BadRequestException("KRA connector has already been defined for this CA");
                }

                String hostport = newHost + ":" + newPort;
                if (host.indexOf(' ') != -1) { // host is a list
                    String[] hostList = host.trim().split(" ");
                    for (String h : hostList) {
                        if (h.equals(hostport)) {
                            CMS.debug("addConnector: connector for " + hostport +
                                    " is already present.  Returning success");
                            return;
                        }
                    }

                    CMS.debug("addConnector: adding " + hostport + " to KRA connector host list");
                    cs.putString(PREFIX + ".host", host + " " + hostport);
                    cs.commit(true);
                    replaceConnector();
                    return;
                } else { // host is not a list, turn it into one
                    CMS.debug("addConnector: adding " + hostport + " to KRA connector");
                    cs.putString(PREFIX + ".host", host + ":" + port + " " + hostport);
                    cs.commit(true);
                    replaceConnector();
                    return;
                }
            }
        }

        // connector does not exist, or existing connector is the same host/port and we are replacing it
        cs.putString(PREFIX + ".host", info.getHost());
        cs.putString(PREFIX + ".port", info.getPort());
        cs.putString(PREFIX + ".enable", info.getEnable() != null ? info.getEnable() : "true");
        cs.putString(PREFIX + ".local", info.getLocal() != null ? info.getLocal(): "false");
        cs.putString(PREFIX + ".timeout", info.getTimeout() != null ?  info.getTimeout() : "30");
        cs.putString(PREFIX + ".uri", info.getUri() != null ? info.getUri() : "/kra/agent/kra/connector");
        cs.putString(PREFIX + ".transportCert", info.getTransportCert());

        String nickname = cs.getString("ca.subsystem.nickname", "");
        String tokenname = cs.getString("ca.subsystem.tokenname", "");
        if (!tokenname.equals("Internal Key Storage Token"))
            nickname = tokenname + ":" + nickname;
        cs.putString(PREFIX + ".nickName", nickname);
        cs.commit(true);

        replaceConnector();
    }

}