Skip to content

Commit

Permalink
Refactor getNewCertAuthorityUpdatedCertDetailsExpectedResponse and ge…
Browse files Browse the repository at this point in the history
…tUpdatedCertDetailsExpectedResponse helpers into simplified getCertDetailsExpectedResponse

Signed-off-by: Paris Larkins <paris.larkins@netapp.com>
  • Loading branch information
parislarkins committed Oct 15, 2024
1 parent e2ceeeb commit d74905f
Showing 1 changed file with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.databind.JsonNode;
import org.junit.After;
Expand Down Expand Up @@ -44,10 +43,8 @@ public class SecuritySSLReloadCertsActionTests extends SingleClusterTest {
private final String RELOAD_HTTP_CERTS_ENDPOINT = "_opendistro/_security/api/ssl/http/reloadcerts";
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
private final String HTTP_CERTIFICATES_LIST_KEY = "http_certificates_list";
private final String TRANSPORT_CERTIFICATES_LIST_KEY = "transport_certificates_list";

private final List<Map<String, String>> NODE_CERT_DETAILS = List.of(
private final List<Map<String, String>> INITIAL_NODE_CERT_DETAILS = List.of(
Map.of(
"issuer_dn",
"CN=Example Com Inc. Signing CA,OU=Example Com Inc. Signing CA,O=Example Com Inc.,DC=example,DC=com",
Expand Down Expand Up @@ -131,7 +128,7 @@ public void testReloadTransportSSLCertsPass() throws Exception {
updateFiles(newCertFilePath, pemCertFilePath);
updateFiles(newKeyFilePath, pemKeyFilePath);

assertReloadCertificateSuccess(rh, "transport", getUpdatedCertDetailsExpectedResponse("transport"));
assertReloadCertificateSuccess(rh, "transport", getCertDetailsExpectedResponse(INITIAL_NODE_CERT_DETAILS, NEW_NODE_CERT_DETAILS));
}

@Test
Expand All @@ -148,7 +145,7 @@ public void testReloadHttpSSLCertsPass() throws Exception {
updateFiles(newCertFilePath, pemCertFilePath);
updateFiles(newKeyFilePath, pemKeyFilePath);

assertReloadCertificateSuccess(rh, "http", getUpdatedCertDetailsExpectedResponse("http"));
assertReloadCertificateSuccess(rh, "http", getCertDetailsExpectedResponse(NEW_NODE_CERT_DETAILS, INITIAL_NODE_CERT_DETAILS));
}

@Test
Expand Down Expand Up @@ -220,7 +217,10 @@ public void testReloadHttpCertDifferentTrustChain_skipDnValidationPass() throws
assertThat(reloadCertsResponse.getBody(), is(expectedJsonResponse.toString()));

String certDetailsResponse = rh.executeSimpleRequest(GET_CERT_DETAILS_ENDPOINT);
assertThat(DefaultObjectMapper.readTree(certDetailsResponse), is(getNewCertAuthorityUpdatedCertDetailsExpectedResponse("http")));
assertThat(
DefaultObjectMapper.readTree(certDetailsResponse),
is(getCertDetailsExpectedResponse(NEW_CA_NODE_CERT_DETAILS, INITIAL_NODE_CERT_DETAILS))
);
}

@Test
Expand Down Expand Up @@ -290,7 +290,7 @@ public void testReloadTransportCertDifferentTrustChain_skipDnValidationPass() th
String certDetailsResponse = rh.executeSimpleRequest(GET_CERT_DETAILS_ENDPOINT);
assertThat(
DefaultObjectMapper.readTree(certDetailsResponse),
is(getNewCertAuthorityUpdatedCertDetailsExpectedResponse("transport"))
is(getCertDetailsExpectedResponse(INITIAL_NODE_CERT_DETAILS, NEW_CA_NODE_CERT_DETAILS))
);
}

Expand Down Expand Up @@ -365,29 +365,18 @@ private void updateFiles(String srcFile, String dstFile) {
FileHelper.copyFileContents(FileHelper.getAbsoluteFilePathFromClassPath(srcFile).toString(), dstFile);
}

private JsonNode getNewCertAuthorityUpdatedCertDetailsExpectedResponse(String updateChannel) {
String updateKey = (Objects.equals(updateChannel, "http")) ? HTTP_CERTIFICATES_LIST_KEY : TRANSPORT_CERTIFICATES_LIST_KEY;
String oldKey = (Objects.equals(updateChannel, "http")) ? TRANSPORT_CERTIFICATES_LIST_KEY : HTTP_CERTIFICATES_LIST_KEY;
final var updatedCertDetailsResponse = DefaultObjectMapper.objectMapper.createObjectNode();
updatedCertDetailsResponse.set(updateKey, buildCertsInfoNode(NEW_CA_NODE_CERT_DETAILS));
updatedCertDetailsResponse.set(oldKey, buildCertsInfoNode(NODE_CERT_DETAILS));
return updatedCertDetailsResponse;
}

private JsonNode getUpdatedCertDetailsExpectedResponse(String updateChannel) {
String updateKey = (Objects.equals(updateChannel, "http")) ? HTTP_CERTIFICATES_LIST_KEY : TRANSPORT_CERTIFICATES_LIST_KEY;
String oldKey = (Objects.equals(updateChannel, "http")) ? TRANSPORT_CERTIFICATES_LIST_KEY : HTTP_CERTIFICATES_LIST_KEY;
private JsonNode getCertDetailsExpectedResponse(
List<Map<String, String>> httpCertDetails,
List<Map<String, String>> transportCertDetails
) {
final var updatedCertDetailsResponse = DefaultObjectMapper.objectMapper.createObjectNode();
updatedCertDetailsResponse.set(updateKey, buildCertsInfoNode(NEW_NODE_CERT_DETAILS));
updatedCertDetailsResponse.set(oldKey, buildCertsInfoNode(NODE_CERT_DETAILS));
updatedCertDetailsResponse.set("http_certificates_list", buildCertsInfoNode(httpCertDetails));
updatedCertDetailsResponse.set("transport_certificates_list", buildCertsInfoNode(transportCertDetails));
return updatedCertDetailsResponse;
}

private JsonNode getInitCertDetailsExpectedResponse() {
final var initCertDetailsResponse = DefaultObjectMapper.objectMapper.createObjectNode();
initCertDetailsResponse.set(HTTP_CERTIFICATES_LIST_KEY, buildCertsInfoNode(NODE_CERT_DETAILS));
initCertDetailsResponse.set(TRANSPORT_CERTIFICATES_LIST_KEY, buildCertsInfoNode(NODE_CERT_DETAILS));
return initCertDetailsResponse;
return getCertDetailsExpectedResponse(INITIAL_NODE_CERT_DETAILS, INITIAL_NODE_CERT_DETAILS);
}

private JsonNode buildCertsInfoNode(final List<Map<String, String>> certsInfo) {
Expand Down

0 comments on commit d74905f

Please sign in to comment.