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 11, 2024
1 parent 8ebd073 commit 70e49b4
Show file tree
Hide file tree
Showing 16 changed files with 133 additions and 38 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/siemens/pki/lightweightcmpra/main/RA.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,15 @@ public static void stopAllRas() {

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 +147,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
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ DownstreamInterface:
UpstreamInterface:
- OfflineFileClient:
certProfile: ~
inputDirectory: "./target/CmpTest/Downstream"
outputDirectory: "./target/CmpTest/Upstream"
inputDirectory: "./target/CmpTest/Downstream_REV"
outputDirectory: "./target/CmpTest/Upstream_REV"

UpstreamConfiguration:
- VerificationContext:
Expand Down Expand Up @@ -53,6 +53,6 @@ RetryAfterTimeInSeconds:
- value: 1

DownstreamTimeout:
- value: 20
- value: 100


Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ UpstreamInterface:

DownstreamInterface:
OfflineFileServer:
inputDirectory: "./target/CmpTest/Upstream"
outputDirectory: "./target/CmpTest/Downstream"
inputDirectory: "./target/CmpTest/Upstream_REV"
outputDirectory: "./target/CmpTest/Downstream_REV"

UpstreamConfiguration:
- VerificationContext:
Expand Down Expand Up @@ -40,4 +40,4 @@ ForceRaVerifyOnUpstream:
- value: false

DownstreamTimeout:
- value: 20
- value: 100
Loading

0 comments on commit 70e49b4

Please sign in to comment.