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

Valery Kharseko
yesterday 70d9a179cdd8d975b44e1815c249e20d9f91097f
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
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.server.tools;
 
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
 
import java.io.File;
 
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.TestCaseUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
import com.forgerock.opendj.cli.ArgumentException;
 
/**
 * Tests the arguments which let {@code setup} provision the trust store used for server
 * to server communication from a key store the operator already holds.
 */
public class InstallDSArgumentParserTestCase extends DirectoryServerTestCase
{
  /** A fragment of the message reporting that the key pair to present has to come from a key store. */
  private static final String KEY_STORE_REQUIRED = "requires an existing key store";
  /** A fragment of the message reporting a certificate file which cannot be read. */
  private static final String CERT_FILE_INVALID = "does not exist";
  /** A fragment of the message reporting an argument the parser does not know. */
  private static final String UNKNOWN_ARGUMENT = "is not allowed for use with this program";
 
  /**
   * The key pair presented on the replication port is imported from an existing key
   * store, so asking for a self-signed certificate to be generated cannot satisfy it.
   *
   * @throws Exception
   *           If a problem occurs.
   */
  @Test
  public void testUseKeyStoreForReplicationRejectsAGeneratedCertificate() throws Exception
  {
    assertReportsUseKeyStoreForReplication(true,
        "--cli", "-n", "-w", "password", "-b", "dc=example,dc=com",
        "--generateSelfSignedCertificate", "--enableStartTLS", "--useKeyStoreForReplication");
  }
 
  /**
   * The private key of a PKCS#11 token cannot be exported, so it cannot be copied into
   * the trust store used for replication.
   *
   * @throws Exception
   *           If a problem occurs.
   */
  @Test
  public void testUseKeyStoreForReplicationRejectsAPkcs11Token() throws Exception
  {
    assertReportsUseKeyStoreForReplication(true,
        "--cli", "-n", "-w", "password", "-b", "dc=example,dc=com",
        "--usePkcs11Keystore", "--keyStorePassword", "password", "--enableStartTLS",
        "--useKeyStoreForReplication");
  }
 
  /**
   * A key store the installer can read the key pair from is accepted, whichever of the
   * four key store arguments names it.
   *
   * @param keyStoreArgument
   *          The argument naming the key store.
   * @throws Exception
   *           If a problem occurs.
   */
  @Test(dataProvider = "keyStoreArguments")
  public void testUseKeyStoreForReplicationAcceptsAKeyStore(String keyStoreArgument) throws Exception
  {
    final File tmpDir = TestCaseUtils.createTemporaryDirectory("useKeyStoreForReplication");
    try
    {
      final File keyStore = new File(tmpDir, "server.keystore");
      assertTrue(keyStore.createNewFile());
      assertReportsUseKeyStoreForReplication(false,
          "--cli", "-n", "-w", "password", "-b", "dc=example,dc=com",
          keyStoreArgument, keyStore.getAbsolutePath(), "--keyStorePassword", "password",
          "--enableStartTLS", "--useKeyStoreForReplication");
    }
    finally
    {
      TestCaseUtils.deleteDirectory(tmpDir);
    }
  }
 
  @DataProvider
  public Object[][] keyStoreArguments()
  {
    return new Object[][] {
      { "--useJavaKeystore" }, { "--useJCEKS" }, { "--usePkcs12keyStore" }, { "--useBcfksKeystore" } };
  }
 
  /**
   * Certificates to trust on the replication port are only meaningful together with the
   * key pair to present there: a server which trusts an authority but keeps presenting
   * its self-signed certificate is the half-configured state this provisioning exists to
   * avoid.
   *
   * @throws Exception
   *           If a problem occurs.
   */
  @Test
  public void testReplicationCaCertFileRequiresUseKeyStoreForReplication() throws Exception
  {
    final File tmpDir = TestCaseUtils.createTemporaryDirectory("replicationCaCertFile");
    try
    {
      final File caFile = new File(tmpDir, "ca.crt");
      assertTrue(caFile.createNewFile());
      final String error = parseAndReturnError(
          "--cli", "-n", "-w", "password", "-b", "dc=example,dc=com",
          "--usePkcs12keyStore", new File(tmpDir, "server.p12").getAbsolutePath(),
          "--keyStorePassword", "password", "--enableStartTLS",
          "--replicationCaCertFile", caFile.getAbsolutePath());
      assertTrue(error.contains("replicationCaCertFile") && error.contains("useKeyStoreForReplication"), error);
      assertFalse(error.contains(UNKNOWN_ARGUMENT), error);
    }
    finally
    {
      TestCaseUtils.deleteDirectory(tmpDir);
    }
  }
 
  /**
   * A certificate file which does not exist is reported while the arguments are checked,
   * rather than half way through the installation.
   *
   * @throws Exception
   *           If a problem occurs.
   */
  @Test
  public void testReplicationCaCertFileMustExist() throws Exception
  {
    final File tmpDir = TestCaseUtils.createTemporaryDirectory("replicationCaCertMissing");
    try
    {
      final File missing = new File(tmpDir, "nonexistent.crt");
      final String error = parseAndReturnError(
          "--cli", "-n", "-w", "password", "-b", "dc=example,dc=com",
          "--usePkcs12keyStore", new File(tmpDir, "server.p12").getAbsolutePath(),
          "--keyStorePassword", "password", "--enableStartTLS", "--useKeyStoreForReplication",
          "--replicationCaCertFile", missing.getAbsolutePath());
      assertTrue(error.contains(missing.getAbsolutePath()) && error.contains(CERT_FILE_INVALID), error);
    }
    finally
    {
      TestCaseUtils.deleteDirectory(tmpDir);
    }
  }
 
  /**
   * Asserts whether the arguments are rejected because {@code --useKeyStoreForReplication}
   * cannot be satisfied. Other errors, such as a port which happens to be in use on the
   * machine running the tests, are ignored: only the message under test is looked for. The
   * wording is matched as well as the argument name, so that an argument the parser does
   * not know at all, which is reported by name too, does not pass for the check under test.
   */
  private void assertReportsUseKeyStoreForReplication(boolean expected, String... args) throws Exception
  {
    final String error = parseAndReturnError(args);
    final boolean reported =
        error.contains("useKeyStoreForReplication") && error.contains(KEY_STORE_REQUIRED);
    if (expected)
    {
      assertTrue(reported, error);
    }
    else
    {
      assertFalse(reported, error);
      assertFalse(error.contains(UNKNOWN_ARGUMENT), error);
    }
  }
 
  /** Parses the provided arguments and returns the reported errors, empty if there is none. */
  private String parseAndReturnError(String... args) throws Exception
  {
    final InstallDSArgumentParser parser = new InstallDSArgumentParser(InstallDS.class.getName());
    parser.initializeArguments();
    try
    {
      parser.parseArguments(args);
      return "";
    }
    catch (ArgumentException e)
    {
      return e.getMessage();
    }
  }
}