Skip to content

Commit

Permalink
fix missing upstream timer shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Akretsch committed Jul 18, 2024
1 parent 8ebd073 commit 9880504
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 50 deletions.
23 changes: 12 additions & 11 deletions src/main/java/com/siemens/pki/lightweightcmpra/main/RA.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void set(final T val) {
}
}

private static ArrayList<RA> raList;
private static ArrayList<RA> raList = new ArrayList<>();

/**
* @param args command line arguments. Call with &lt;name of XML/YAML/JSON
Expand All @@ -90,35 +90,35 @@ public static void main(final String[] args) throws Exception {
System.err.println("call with <name of YAML/JSON config file>");
return;
}
raList = new ArrayList<>(args.length);
// start RAs
for (final String actConfigFile : args) {
raList.add(new RA(actConfigFile));
synchronized (raList) {
// start RAs
for (final String actConfigFile : args) {
raList.add(new RA(actConfigFile));
}
}
}

/**
* stop all RA instances, used for unit tests
*/
public static void stopAllRas() {
for (; ; ) {
if (raList.isEmpty()) {
break;
}
raList.remove(0).stop();
synchronized (raList) {
raList.forEach(RA::stop);
raList.clear();
}
}

private DownstreamInterface downstreamInterface;
private String configFile;
private Map<CertProfileBodyTypeTupel, UpstreamInterface> upstreamInterfaceMap;

private RA(final String actConfigFile) throws Exception {
configFile = actConfigFile;

try {
final ConfigurationImpl configuration = YamlConfigLoader.loadConfig(configFile, ConfigurationImpl.class);
final DeferredSupplier<CmpRaInterface> raHolder = new DeferredSupplier<>();
final Map<CertProfileBodyTypeTupel, UpstreamInterface> upstreamInterfaceMap = new HashMap<>();
upstreamInterfaceMap = new HashMap<>();
final UpstreamExchange upstreamExchange = (request, certProfile, bodyTypeOfFirstRequest) -> {
final CertProfileBodyTypeTupel key = new CertProfileBodyTypeTupel(certProfile, bodyTypeOfFirstRequest);
UpstreamInterface upstreamInterface = upstreamInterfaceMap.get(key);
Expand Down Expand Up @@ -146,6 +146,7 @@ private void stop() {
if (downstreamInterface != null) {
downstreamInterface.stop();
}
upstreamInterfaceMap.values().forEach(UpstreamInterface::stop);
System.out.println("RA configured with " + configFile + " stopped");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ interface AsyncResponseHandler {
* the callback
*/
void setDelayedResponseHandler(AsyncResponseHandler asyncResponseHandler);

void stop();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class CmpFileOfflineClient implements UpstreamInterface {

private final File outputDirectory;
private AsyncResponseHandler asyncResponseHandler;
private TimerTask timerTask;

/**
*
Expand All @@ -66,14 +67,14 @@ public CmpFileOfflineClient(final OfflineFileClientConfig config) throws IOExcep
}
final long pollInterval = config.getInputDirectoryPollcycle() * 1000L;
final Timer pollTimer = new Timer(true);
final TimerTask task = new TimerTask() {
timerTask = new TimerTask() {

@Override
public void run() {
pollInputDirectory();
}
};
pollTimer.schedule(task, new Date(System.currentTimeMillis() + pollInterval), pollInterval);
pollTimer.schedule(timerTask, new Date(System.currentTimeMillis() + pollInterval), pollInterval);
}

@Override
Expand Down Expand Up @@ -118,4 +119,9 @@ private void pollInputDirectory() {
}
}
}

@Override
public void stop() {
timerTask.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public byte[] apply(final byte[] message, final String certProfile) {
public void setDelayedResponseHandler(final AsyncResponseHandler asyncResponseHandler) {
// no async response expected
}

@Override
public void stop() {
client.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@ public byte[] apply(final byte[] message, final String certProfile) {
public void setDelayedResponseHandler(final AsyncResponseHandler asyncResponseHandler) {
// no async response expected
}

@Override
public void stop() {
// nothing to do

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2023 Siemens AG
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package com.siemens.pki.lightweightcmpclient.test;

import com.siemens.pki.lightweightcmpra.main.RA;
import com.siemens.pki.lightweightcmpra.test.framework.TestUtils;
import java.security.GeneralSecurityException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestDelayedEnrollmentAndRevocation extends EnrollmentTestcaseBase {

private static final String UPSTREAM_DIR = "./target/CmpTest/Upstream_REV";
private static final String DOWNSTREAM_DIR = "./target/CmpTest/Downstream_REV";

@BeforeClass
public static void setupRas() throws GeneralSecurityException, InterruptedException, Exception {
TestUtils.createDirectories(DOWNSTREAM_DIR, UPSTREAM_DIR);
initTestbed("DelayedEnrollmentRaTestConfig.yaml", "DelayedEnrollmentLraTestConfig.yaml");
}

@AfterClass
public static void stopAllRas() {
RA.stopAllRas();
TestUtils.removeDirectories(DOWNSTREAM_DIR, UPSTREAM_DIR);
}

@After
public void cleanUpDelayedEnrollmentDirs() {
TestUtils.deleteAllFilesIn(DOWNSTREAM_DIR, UPSTREAM_DIR);
}

@Before
public void setUpDelayedEnrollmentDirs() throws Exception {
TestUtils.deleteAllFilesIn(DOWNSTREAM_DIR, UPSTREAM_DIR);
}

@Test(timeout = 100000L)
public void testCrWithPolling() throws Exception {
enrollWithConfig("DelayedClientEnrollmentConfigWithHttpAndSignature.yaml");
}

@Test(timeout = 100000L)
public void testRrWithPolling() throws Exception {
revokeWithConfigAndCert("DelayedClientEnrollmentConfigWithHttpAndSignature.yaml");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

public class TestGeneralMessagesWithPolling extends CmpTestcaseBase {

private static final String UPSTREAM_DIR = "./target/CmpTest/GenUpstream";
private static final String DOWNSTREAM_DIR = "./target/CmpTest/GenDownstream";
private static final String UPSTREAM_DIR = "./target/CmpTest/GenUpstream_GEN";
private static final String DOWNSTREAM_DIR = "./target/CmpTest/GenDownstream_GEN";

@AfterClass
public static void cleanUpDirsAnRas() {
Expand All @@ -56,7 +56,7 @@ public void cleanDirectories() {
*
* @throws Exception
*/
@Test
@Test(timeout = 100000L)
public void testCrlUpdateRetrieval() throws Exception {
final String cmdArgs = "--configfile " + "DelayedClientGeneralMessagesWithHttpAndSignature.yaml" + " "
+ "--getCrls ./target/CmpTest/Results/CRLs.crl " + "--issuer CN=distributionPoint ";
Expand All @@ -70,7 +70,7 @@ public void testCrlUpdateRetrieval() throws Exception {
*
* @throws Exception
*/
@Test
@Test(timeout = 100000L)
public void testCrlUpdateRetrievalWithOldCrl() throws Exception {
final String cmdArgs = "--configfile " + "DelayedClientGeneralMessagesWithHttpAndSignature.yaml" + " "
+ "--getCrls ./target/CmpTest/Results/CRLs.crl "
Expand All @@ -83,7 +83,7 @@ public void testCrlUpdateRetrievalWithOldCrl() throws Exception {
/*
* Get CA certificates
*/
@Test
@Test(timeout = 100000L)
public void testGetCaCerts() throws Exception {
final String cmdArgs = "--configfile " + "DelayedClientGeneralMessagesWithHttpAndSignature.yaml" + " "
+ "--getCaCertificates ./target/CmpTest/Results/Certificates.cer ";
Expand All @@ -95,7 +95,7 @@ public void testGetCaCerts() throws Exception {
/*
* Get certificate request template
*/
@Test
@Test(timeout = 100000L)
public void testGetCertificateRequestTemplate() throws Exception {

final String cmdArgs = "--configfile " + "DelayedClientGeneralMessagesWithHttpAndSignature.yaml" + " "
Expand All @@ -108,7 +108,7 @@ public void testGetCertificateRequestTemplate() throws Exception {
/*
* Get root CA certificate update
*/
@Test
@Test(timeout = 100000L)
public void testGetRootCaKeyUpdateInfo() throws Exception {
final String cmdArgs = "--configfile " + "DelayedClientGeneralMessagesWithHttpAndSignature.yaml" + " "
+ "--getRootCaCertificateUpdate " + "--NewWithNew ./target/CmpTest/Results/NewWithNew.cer "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.bouncycastle.asn1.crmf.CertTemplateBuilder;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
Expand All @@ -42,6 +43,9 @@

public class DelayedEnrollmentTescaseBase extends EnrollmentTestcaseBase {

private static final String UPSTREAM_DIR = "./target/CmpTest/Upstream_REV";
private static final String DOWNSTREAM_DIR = "./target/CmpTest/Downstream_REV";

private static final Logger LOGGER = LoggerFactory.getLogger(DelayedEnrollmentTescaseBase.class);

protected static EnrollmentResult executeDelayedCertificateRequest(
Expand Down Expand Up @@ -96,20 +100,25 @@ protected static EnrollmentResult executeDelayedCertificateRequest(

@AfterClass
public static void removeDirs() {
TestUtils.removeDirectories("./target/CmpTest/Downstream", "./target/CmpTest/Upstream");
TestUtils.removeDirectories(DOWNSTREAM_DIR, UPSTREAM_DIR);
}

@BeforeClass
public static void setUpDirsAndRas() throws Exception {
TestUtils.createDirectories("./target/CmpTest/Downstream", "./target/CmpTest/Upstream");
TestUtils.createDirectories(DOWNSTREAM_DIR, UPSTREAM_DIR);
initTestbed(
"http://localhost:6003/delayedlra",
"DelayedEnrollmentRaTestConfig.yaml",
"DelayedEnrollmentLraTestConfig.yaml");
}

@Before
public void clearDirs() {
TestUtils.deleteAllFilesIn("./target/CmpTest/Downstream", "./target/CmpTest/Upstream");
public void clearDirsBefore() {
TestUtils.deleteAllFilesIn(DOWNSTREAM_DIR, UPSTREAM_DIR);
}

@After
public void clearDirsAfter() {
TestUtils.deleteAllFilesIn(DOWNSTREAM_DIR, UPSTREAM_DIR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void setUpRas() throws Exception {
*
* @throws Exception
*/
@Test(timeout = 10000000L)
@Test(timeout = 100000L)
public void testCrWithNested() throws Exception {
executeCrmfCertificateRequest(
PKIBody.TYPE_CERT_REQ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TestCrWithPolling extends DelayedEnrollmentTescaseBase {
*
* @throws Exception
*/
@Test
@Test(timeout = 100000L)
public void testCrWithPolling() throws Exception {
executeDelayedCertificateRequest(
PKIBody.TYPE_CERT_REQ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Ignore
public class TestGeneralMessagesWithPolling extends CmpTestcaseBase {

private static final String UPSTREAM_DIR = "./target/CmpTest/GenUpstream_GEN";
private static final String DOWNSTREAM_DIR = "./target/CmpTest/GenDownstream_GEN";
private static final Logger LOGGER = LoggerFactory.getLogger(TestGeneralMessagesWithPolling.class);

@AfterClass
public static void removeDirs() {
RA.stopAllRas();
TestUtils.removeDirectories("./target/CmpTest/GenDownstream", "./target/CmpTest/GenUpstream");
TestUtils.removeDirectories(DOWNSTREAM_DIR, UPSTREAM_DIR);
}

@BeforeClass
public static void setUpDirsAndRas() throws Exception {
TestUtils.createDirectories("./target/CmpTest/GenDownstream", "./target/CmpTest/GenUpstream");
TestUtils.createDirectories(DOWNSTREAM_DIR, UPSTREAM_DIR);
initTestbed(
"http://localhost:6006/delayedsupportlra",
"DelayedSupportMessagesRaTestConfig.yaml",
Expand All @@ -81,15 +81,15 @@ public static void setUpDirsAndRas() throws Exception {

@Before
public void clearDirs() {
TestUtils.deleteAllFilesIn("./target/CmpTest/GenDownstream", "./target/CmpTest/GenUpstream");
TestUtils.deleteAllFilesIn(DOWNSTREAM_DIR, UPSTREAM_DIR);
}

/**
* CRL Update Retrieval
*
* @throws Exception
*/
@Test
@Test(timeout = 100000L)
public void testCrlUpdateRetrieval() throws Exception {
final Function<PKIMessage, PKIMessage> eeCmpClient = getEeCmpClient();
final ASN1ObjectIdentifier statusListOid = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.4.22");
Expand Down Expand Up @@ -132,7 +132,7 @@ null, new GeneralNames(new GeneralName(new X500Name("CN=distributionPoint")))),
/*
* Get CA certificates
*/
@Test
@Test(timeout = 100000L)
public void testGetCaCerts() throws Exception {
final Function<PKIMessage, PKIMessage> eeCmpClient = getEeCmpClient();
final ASN1ObjectIdentifier getCaCertOid = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.4.17");
Expand Down Expand Up @@ -166,7 +166,7 @@ public void testGetCaCerts() throws Exception {
/*
* Get certificate request template
*/
@Test
@Test(timeout = 100000L)
public void testGetCertificateRequestTemplate() throws Exception {
final Function<PKIMessage, PKIMessage> eeCmpClient = getEeCmpClient();
final ASN1ObjectIdentifier getCaCertOid = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.4.19");
Expand Down Expand Up @@ -215,7 +215,7 @@ public void testGetCertificateRequestTemplate() throws Exception {
/*
* Get root CA certificate update
*/
@Test
@Test(timeout = 100000L)
public void testGetRootCaKeyUpdateInfo() throws Exception {
final Function<PKIMessage, PKIMessage> eeCmpClient = getEeCmpClient();
final ASN1ObjectIdentifier getCaCertOid = new ASN1ObjectIdentifier("1.3.6.1.5.5.7.4.20");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class TestRrWithPolling extends DelayedEnrollmentTescaseBase {
*
* @throws Exception
*/
@Test(timeout = 60000L)
@Test(timeout = 100000L)
public void testRrWithPolling() throws Exception {
final EnrollmentResult certificateToRevoke = executeDelayedCertificateRequest(
PKIBody.TYPE_CERT_REQ,
Expand Down
Loading

0 comments on commit 9880504

Please sign in to comment.