mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Gaetan Boismal
04.23.2016 24ccabcd125b7b60e10ff0ba1642938e033cb6d3
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *      Copyright 2014-2016 ForgeRock AS.
 */
package org.forgerock.opendj.server.setup.cli;
 
import static com.forgerock.opendj.cli.Utils.filterExitCode;
import static com.forgerock.opendj.cli.Utils.LINE_SEPARATOR;
import static com.forgerock.opendj.cli.Utils.checkJavaVersion;
import static com.forgerock.opendj.cli.CliMessages.*;
import static com.forgerock.opendj.cli.CliConstants.*;
import static com.forgerock.opendj.cli.CommonArguments.*;
 
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.i18n.LocalizableMessageBuilder;
import org.forgerock.i18n.slf4j.LocalizedLogger;
 
import com.forgerock.opendj.cli.Argument;
import com.forgerock.opendj.cli.ArgumentException;
import com.forgerock.opendj.cli.BooleanArgument;
import com.forgerock.opendj.cli.ClientException;
import com.forgerock.opendj.cli.ConsoleApplication;
import com.forgerock.opendj.cli.FileBasedArgument;
import com.forgerock.opendj.cli.IntegerArgument;
import com.forgerock.opendj.cli.ReturnCode;
import com.forgerock.opendj.cli.StringArgument;
import com.forgerock.opendj.cli.SubCommand;
import com.forgerock.opendj.cli.SubCommandArgumentParser;
import com.forgerock.opendj.cli.Utils;
 
/**
 * This class implements the new CLI for OpenDJ3 setup.
 */
public final class SetupCli extends ConsoleApplication {
 
    /**
     * Setup's logger.
     */
    private static final LocalizedLogger LOG = LocalizedLogger.getLoggerForThisClass();
 
    /**
     * TODO remove that after implementation in config.
     *
     * @return The installation path.
     */
    static String getInstallationPath() {
        return "/home/violette/OpenDJ-3.0.0/";
    }
 
    /**
     * TODO remove that after implementation in config.
     *
     * @return The instance path.
     */
    static String getInstancePath() {
        return "/home/violette/OpenDJ-3.0.0/";
    }
 
 
    private SubCommandArgumentParser argParser;
 
    private BooleanArgument cli;
    private BooleanArgument addBaseEntry;
    private BooleanArgument skipPortCheck;
    private BooleanArgument enableWindowsService;
    private BooleanArgument doNotStart;
    private BooleanArgument enableStartTLS;
    private BooleanArgument generateSelfSignedCertificate;
    private StringArgument hostName;
    private BooleanArgument usePkcs11;
    private FileBasedArgument directoryManagerPwdFile;
    private FileBasedArgument keyStorePasswordFile;
    private IntegerArgument ldapPort;
    private IntegerArgument adminConnectorPort;
    private IntegerArgument ldapsPort;
    private IntegerArgument jmxPort;
    private IntegerArgument sampleData;
    private StringArgument baseDN;
    private StringArgument importLDIF;
    private StringArgument rejectedImportFile;
    private StringArgument skippedImportFile;
    private StringArgument directoryManagerDN;
    private StringArgument directoryManagerPwdString;
    private StringArgument useJavaKeyStore;
    private StringArgument useJCEKS;
    private StringArgument usePkcs12;
    private StringArgument keyStorePassword;
    private StringArgument certNickname;
    private IntegerArgument connectTimeout;
    private BooleanArgument acceptLicense;
 
    /** Sub-commands. */
    private SubCommand createDirectoryServer;
    private SubCommand createProxy;
 
    /** Register the global arguments. */
    private BooleanArgument noPrompt;
    private BooleanArgument quietMode;
    private BooleanArgument verbose;
    private StringArgument propertiesFile;
    private BooleanArgument noPropertiesFile;
    private BooleanArgument showUsage;
 
    private SetupCli() {
        // Nothing to do.
    }
 
    /** To allow tests. */
    SetupCli(PrintStream out, PrintStream err) {
        super(out, err);
    }
 
    /**
     * The main method for setup tool.
     *
     * @param args
     *            The command-line arguments provided to this program.
     */
    public static void main(final String[] args) {
        final int retCode = new SetupCli().run(args);
        System.exit(filterExitCode(retCode));
    }
 
    /** Create the command-line argument parser for use with this program. */
    int run(final String[] args) {
        // TODO Activate logger when the instance/installation path will be resolved.
        // SetupLog.initLogFileHandler();
 
        try {
            checkJavaVersion();
        } catch (ClientException e) {
            errPrintln(e.getMessageObject());
            return ReturnCode.JAVA_VERSION_INCOMPATIBLE.get();
        }
 
        try {
            argParser = new SubCommandArgumentParser("setup", INFO_SETUP_DESCRIPTION.get(), true);
            initializeArguments();
        } catch (ArgumentException e) {
            final LocalizableMessage message = ERR_CANNOT_INITIALIZE_ARGS.get(e.getMessage());
            errPrintln(message);
            return ReturnCode.CLIENT_SIDE_PARAM_ERROR.get();
        }
 
        // Parse the command-line arguments provided to this program.
        try {
            argParser.parseArguments(args);
 
            if (argParser.usageOrVersionDisplayed()) {
                // If we should just display usage or version information, then print it and exit.
                return ReturnCode.SUCCESS.get();
            }
        } catch (final ArgumentException e) {
            final LocalizableMessage message = ERR_ERROR_PARSING_ARGS.get(e.getMessage());
            errPrintln(message);
            return ReturnCode.CLIENT_SIDE_PARAM_ERROR.get();
        }
 
        // Verifying provided informations.
        try {
            final LinkedHashSet<LocalizableMessage> errorMessages = new LinkedHashSet<>();
            checkServerPassword(errorMessages);
            checkProvidedPorts(errorMessages);
            checkImportDataArguments(errorMessages);
            checkSecurityArguments(errorMessages);
            if (errorMessages.size() > 0) {
                throw new ArgumentException(ERR_CANNOT_INITIALIZE_ARGS.get(
                        getMessageFromCollection(errorMessages, LINE_SEPARATOR)));
            }
        } catch (final ArgumentException e) {
            errPrintln(e.getMessageObject());
            return ReturnCode.CLIENT_SIDE_PARAM_ERROR.get();
        }
 
        // Starts setup process.
        try {
            fillSetupSettings();
            runSetupInstallation();
        } catch (ClientException ex) {
            return ex.getReturnCode();
        } catch (Exception ex) {
            // TODO
            //println(Style.ERROR, LocalizableMessage.raw("...?"));
            return ReturnCode.ERROR_UNEXPECTED.get();
        }
        return ReturnCode.SUCCESS.get();
    }
 
    /**
     * Initialize setup's arguments by default.
     *
     * @throws ArgumentException
     *             If an exception occurs during the initialization of the arguments.
     */
    private void initializeArguments() throws ArgumentException {
        // Options.
        acceptLicense = addGlobal(acceptLicenseArgument());
        cli = addGlobal(cliArgument());
        baseDN = addGlobal(baseDNArgument());
        addBaseEntry = addGlobal(addBaseEntryArgument());
        importLDIF = addGlobal(ldifFileArgument(INFO_DESCRIPTION_IMPORTLDIF.get()));
        rejectedImportFile = addGlobal(rejectedImportLdifArgument());
        skippedImportFile = addGlobal(skippedImportFileArgument());
        sampleData = addGlobal(sampleDataArgument());
        ldapPort = addGlobal(ldapPortArgument(DEFAULT_LDAP_PORT));
        ldapsPort = addGlobal(ldapsPortArgument(DEFAULT_LDAPS_PORT));
        adminConnectorPort = addGlobal(adminLdapPortArgument(DEFAULT_ADMIN_PORT));
        jmxPort = addGlobal(jmxPortArgument(DEFAULT_JMX_PORT));
        skipPortCheck = addGlobal(skipPortCheckArgument());
        directoryManagerDN = addGlobal(rootDNArgument());
        directoryManagerPwdString = addGlobal(rootDNPwdArgument());
        directoryManagerPwdFile = addGlobal(rootDNPwdFileArgument());
        enableWindowsService = addGlobal(enableWindowsServiceArgument());
        doNotStart = addGlobal(doNotStartArgument());
        enableStartTLS = addGlobal(enableTLSArgument());
        generateSelfSignedCertificate = addGlobal(generateSelfSignedArgument());
        hostName = addGlobal(hostNameArgument(Utils.getDefaultHostName()));
        usePkcs11 = addGlobal(usePKCS11KeystoreArgument());
        useJavaKeyStore = addGlobal(useJavaKeyStoreArgument());
        useJCEKS = addGlobal(useJCEKSArgument());
        usePkcs12 = addGlobal(usePKCS12KeyStoreArgument());
        keyStorePassword = addGlobal(keyStorePasswordArgument());
        keyStorePasswordFile = addGlobal(keyStorePasswordFileArgument());
        certNickname = addGlobal(certNickNameArgument());
        connectTimeout = connectTimeOutHiddenArgument();
 
        // Utility Input Output Options.
        noPrompt = addGlobal(noPromptArgument());
        quietMode = addGlobal(quietArgument());
        verbose = addGlobal(verboseArgument());
        propertiesFile = addGlobal(propertiesFileArgument());
        noPropertiesFile = addGlobal(noPropertiesFileArgument());
        showUsage = addGlobal(showUsageArgument());
 
        //Sub-commands && their arguments
        final ArrayList<SubCommand> subCommandList = new ArrayList<>(2);
        createDirectoryServer = new SubCommand(argParser, "create-directory-server",
                INFO_SETUP_SUBCOMMAND_CREATE_DIRECTORY_SERVER.get());
        // TODO to complete.
        createProxy = new SubCommand(argParser, "create-proxy",
                INFO_SETUP_SUBCOMMAND_CREATE_PROXY.get());
        subCommandList.add(createDirectoryServer);
        subCommandList.add(createProxy);
 
        argParser.setUsageGroupArgument(showUsage, subCommandList);
 
        // Register the global arguments.
        argParser.addArgument(showUsage);
        argParser.setUsageArgument(showUsage, getOutputStream());
        argParser.addArgument(noPropertiesFile);
        argParser.setNoPropertiesFileArgument(noPropertiesFile);
        argParser.addArgument(propertiesFile);
        argParser.setFilePropertiesArgument(propertiesFile);
        argParser.addArgument(quietMode);
        argParser.addArgument(verbose);
        argParser.addArgument(noPrompt);
        argParser.addArgument(acceptLicense);
    }
 
    private <A extends Argument> A addGlobal(A arg) throws ArgumentException {
        argParser.addGlobalArgument(arg);
        return arg;
    }
 
    /** {@inheritDoc} */
    @Override
    public boolean isInteractive() {
        return !noPrompt.isPresent();
    }
 
    /** {@inheritDoc} */
    @Override
    public boolean isQuiet() {
        return quietMode.isPresent();
    }
 
    /**
     * Automatically accepts the license if it's present.
     *
     * @return {@code true} if license is accepted by default.
     */
    private boolean isAcceptLicense() {
        return acceptLicense.isPresent();
    }
 
    /** {@inheritDoc} */
    @Override
    public boolean isVerbose() {
        return verbose.isPresent();
    }
 
    /**
     * Returns whether the command was launched in CLI mode or not.
     *
     * @return <CODE>true</CODE> if the command was launched to use CLI mode and <CODE>false</CODE> otherwise.
     */
    public boolean isCli() {
        return cli.isPresent();
    }
 
    /**
     * Returns whether the command was launched to setup proxy or not.
     *
     * @return <CODE>true</CODE> if the command was launched to setup a proxy and <CODE>false</CODE> otherwise.
     */
    public boolean isCreateProxy() {
        return argParser.getSubCommand("create-proxy") != null;
    }
 
    /**
     * Checks that there are no conflicts with the provided ports (like if the user provided the same port for different
     * protocols).
     *
     * @param errorMessages
     *            the list of messages to which we add the error messages describing the problems encountered during the
     *            execution of the checking.
     */
    private void checkProvidedPorts(final Collection<LocalizableMessage> errorMessages) {
        // Check that the provided ports do not match.
        try {
            final Set<Integer> ports = new HashSet<>();
            ports.add(ldapPort.getIntValue());
 
            checkPortArgument(adminConnectorPort, ports, errorMessages);
 
            if (jmxPort.isPresent()) {
                checkPortArgument(jmxPort, ports, errorMessages);
            }
            if (ldapsPort.isPresent()) {
                checkPortArgument(ldapsPort, ports, errorMessages);
            }
        } catch (ArgumentException ae) {
            LOG.error(LocalizableMessage.raw("Unexpected error. "
                    + "Assuming that it is caused by a previous parsing issue: " + ae, ae));
        }
    }
 
    private void checkPortArgument(IntegerArgument portArg, final Set<Integer> ports,
            final Collection<LocalizableMessage> errorMessages) throws ArgumentException {
        if (ports.contains(portArg.getIntValue())) {
            errorMessages.add(ERR_PORT_ALREADY_SPECIFIED.get(portArg.getIntValue()));
        } else {
            ports.add(portArg.getIntValue());
        }
    }
 
    /**
     * Checks that there are no conflicts with the import data arguments.
     *
     * @param errorMessages
     *            the list of messages to which we add the error messages describing the problems encountered during the
     *            execution of the checking.
     */
    private void checkImportDataArguments(final Collection<LocalizableMessage> errorMessages) {
        // Make sure that the user didn't provide conflicting arguments.
        if (addBaseEntry.isPresent()) {
            if (importLDIF.isPresent()) {
                errorMessages.add(ERR_TOOL_CONFLICTING_ARGS.get(addBaseEntry.getLongIdentifier(),
                        importLDIF.getLongIdentifier()));
            } else if (sampleData.isPresent()) {
                errorMessages.add(ERR_TOOL_CONFLICTING_ARGS.get(addBaseEntry.getLongIdentifier(),
                        sampleData.getLongIdentifier()));
            }
        } else if (importLDIF.isPresent() && sampleData.isPresent()) {
            errorMessages.add(ERR_TOOL_CONFLICTING_ARGS.get(importLDIF.getLongIdentifier(),
                    sampleData.getLongIdentifier()));
        }
 
        if (rejectedImportFile.isPresent() && addBaseEntry.isPresent()) {
            errorMessages.add(ERR_TOOL_CONFLICTING_ARGS.get(addBaseEntry.getLongIdentifier(),
                    rejectedImportFile.getLongIdentifier()));
        } else if (rejectedImportFile.isPresent() && sampleData.isPresent()) {
            errorMessages.add(ERR_TOOL_CONFLICTING_ARGS.get(rejectedImportFile.getLongIdentifier(),
                    sampleData.getLongIdentifier()));
        }
 
        if (skippedImportFile.isPresent() && addBaseEntry.isPresent()) {
            errorMessages.add(ERR_TOOL_CONFLICTING_ARGS.get(addBaseEntry.getLongIdentifier(),
                    skippedImportFile.getLongIdentifier()));
        } else if (skippedImportFile.isPresent() && sampleData.isPresent()) {
            errorMessages.add(ERR_TOOL_CONFLICTING_ARGS.get(skippedImportFile.getLongIdentifier(),
                    sampleData.getLongIdentifier()));
        }
 
        if (noPrompt.isPresent() && !baseDN.isPresent() && baseDN.getDefaultValue() == null) {
            final Argument[] args = { importLDIF, addBaseEntry, sampleData };
            for (final Argument arg : args) {
                if (arg.isPresent()) {
                    errorMessages.add(ERR_ARGUMENT_NO_BASE_DN_SPECIFIED.get("--" + arg.getLongIdentifier()));
                }
            }
        }
    }
 
    /**
     * Checks that there are no conflicts with the security arguments. If we are in no prompt mode, check that all the
     * information required has been provided (but not if this information is valid: we do not try to open the keystores
     * or to check that the LDAPS port is in use).
     *
     * @param errorMessages
     *            the list of messages to which we add the error messages describing the problems encountered during the
     *            execution of the checking.
     */
    private void checkSecurityArguments(final Collection<LocalizableMessage> errorMessages) {
        final boolean certificateRequired = ldapsPort.isPresent() || enableStartTLS.isPresent();
 
        int certificateType = 0;
        if (generateSelfSignedCertificate.isPresent()) {
            certificateType++;
        }
        if (useJavaKeyStore.isPresent()) {
            certificateType++;
        }
        if (useJCEKS.isPresent()) {
            certificateType++;
        }
        if (usePkcs11.isPresent()) {
            certificateType++;
        }
        if (usePkcs12.isPresent()) {
            certificateType++;
        }
 
        if (certificateType > 1) {
            errorMessages.add(ERR_SEVERAL_CERTIFICATE_TYPE_SPECIFIED.get());
        }
 
        if (certificateRequired && noPrompt.isPresent() && certificateType == 0) {
            errorMessages.add(ERR_CERTIFICATE_REQUIRED_FOR_SSL_OR_STARTTLS.get());
        }
 
        if (certificateType == 1) {
            if (!generateSelfSignedCertificate.isPresent()) {
                // Check that we have only a password.
                if (keyStorePassword.isPresent() && keyStorePasswordFile.isPresent()) {
                    final LocalizableMessage message = ERR_TWO_CONFLICTING_ARGUMENTS.get(
                            keyStorePassword.getLongIdentifier(), keyStorePasswordFile.getLongIdentifier());
                    errorMessages.add(message);
                }
 
                // Check that we have one password in no prompt mode.
                if (noPrompt.isPresent() && !keyStorePassword.isPresent() && !keyStorePasswordFile.isPresent()) {
                    final LocalizableMessage message = ERR_NO_KEYSTORE_PASSWORD.get(
                            keyStorePassword.getLongIdentifier(), keyStorePasswordFile.getLongIdentifier());
                    errorMessages.add(message);
                }
            }
            if (noPrompt.isPresent() && !ldapsPort.isPresent() && !enableStartTLS.isPresent()) {
                final LocalizableMessage message = ERR_SSL_OR_STARTTLS_REQUIRED.get(ldapsPort.getLongIdentifier(),
                        enableStartTLS.getLongIdentifier());
                errorMessages.add(message);
            }
        }
    }
 
    /**
     * Checks that there are no conflicts with the directory manager passwords. If we are in no prompt mode, check that
     * the password was provided.
     *
     * @param errorMessages
     *            the list of messages to which we add the error messages describing the problems encountered during the
     *            execution of the checking.
     */
    private void checkServerPassword(Collection<LocalizableMessage> errorMessages) {
        if (directoryManagerPwdString.isPresent() && directoryManagerPwdFile.isPresent()) {
            errorMessages.add(ERR_TWO_CONFLICTING_ARGUMENTS.get(
                    directoryManagerPwdString.getLongIdentifier(), directoryManagerPwdFile.getLongIdentifier()));
        }
 
        if (noPrompt.isPresent() && !directoryManagerPwdString.isPresent()
                && !directoryManagerPwdFile.isPresent()) {
            errorMessages.add(ERR_NO_ROOT_PASSWORD.get(directoryManagerPwdString.getLongIdentifier(),
                    directoryManagerPwdFile.getLongIdentifier()));
        }
    }
 
 
    /**
     * This is a helper method that gets a LocalizableMessage representation of the elements in the Collection of
     * Messages. The LocalizableMessage will display the different elements separated by the separator String.
     * TODO move this function.
     * @param col
     *            the collection containing the messages.
     * @param separator
     *            the separator String to be used.
     * @return the message representation for the collection; LocalizableMessage.EMPTY if null.
     */
    private static LocalizableMessage getMessageFromCollection(final Collection<LocalizableMessage> col,
            final String separator) {
        if (col == null || col.isEmpty()) {
            return LocalizableMessage.EMPTY;
        } else {
            LocalizableMessageBuilder mb = null;
            for (final LocalizableMessage m : col) {
                if (mb == null) {
                    mb = new LocalizableMessageBuilder(m);
                } else {
                    mb.append(separator).append(m);
                }
            }
            return mb.toMessage();
        }
    }
 
    /**
     * Fills the setup components according to the arguments provided by the user.
     * @throws ArgumentException
     */
    private void fillSetupSettings() throws ArgumentException {
        // TODO ...
    }
 
    /**
     * Launches the setup process.
     * @throws ClientException
     */
    private void runSetupInstallation() throws ClientException {
        // TODO move that function to another class.
    }
}