Skip to content

Commit

Permalink
Add SyncTest for local testing and update rest template sslcontext
Browse files Browse the repository at this point in the history
  • Loading branch information
ubhaller committed Mar 9, 2021
1 parent ebc8e63 commit 191d56a
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,6 @@ private static CloseableHttpClient httpClient(
clientCertPassword.toCharArray(),
(aliases, socket) ->
!aliases.keySet().isEmpty() ? aliases.keySet().iterator().next() : null)
.loadTrustMaterial(
null,
new TrustStrategy() {
@Override
public boolean isTrusted(
java.security.cert.X509Certificate[] chain, String authType)
throws CertificateException {
return true;
}
})
.build();
builder.setSSLContext(sslContext);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package org.dpppt.backend.sdk.interops;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.operator.OperatorCreationException;
import org.dpppt.backend.sdk.data.gaen.GaenDataService;
import org.dpppt.backend.sdk.data.interops.SyncLogDataService;
import org.dpppt.backend.sdk.interops.config.FlyWayConfig;
import org.dpppt.backend.sdk.interops.config.GaenDataServiceConfig;
import org.dpppt.backend.sdk.interops.config.StandaloneDataConfig;
import org.dpppt.backend.sdk.interops.config.SyncLogDataServiceConfig;
import org.dpppt.backend.sdk.interops.model.EfgsGatewayConfig;
import org.dpppt.backend.sdk.interops.syncer.EfgsHubSyncer;
import org.dpppt.backend.sdk.interops.syncer.efgs.EfgsClient;
import org.dpppt.backend.sdk.model.gaen.GaenKey;
import org.dpppt.backend.sdk.model.gaen.GaenKeyWithOrigin;
import org.dpppt.backend.sdk.utils.UTCInstant;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
loader = AnnotationConfigContextLoader.class,
classes = {
StandaloneDataConfig.class,
FlyWayConfig.class,
GaenDataServiceConfig.class,
SyncLogDataServiceConfig.class
})
public class SyncTest {

@Autowired private GaenDataService gaenDataService;

@Autowired private SyncLogDataService syncLogDataService;

private static final SecureRandom SECURE_RANDOM = new SecureRandom();

@Test
@Ignore("for local testing")
public void testEfgsClientUpload()
throws GeneralSecurityException, OperatorCreationException, CMSException, IOException {
EfgsClient efgsClient = new EfgsClient(getEfgsGatewayConfig());
String batchTag = getBatchTag();
List<GaenKeyWithOrigin> keysToUpload = createMockedKeys(10);
List<GaenKeyWithOrigin> uploadedKeys = efgsClient.upload(keysToUpload, batchTag);
Assert.assertEquals(keysToUpload.size(), uploadedKeys.size());
}

@Test
// @Ignore("for local testing")
public void testEfgsClientDownload() throws GeneralSecurityException {
EfgsHubSyncer syncer =
new EfgsHubSyncer(
new EfgsClient(getEfgsGatewayConfig()),
Duration.ofDays(14),
gaenDataService,
syncLogDataService);
syncer.download(UTCInstant.today().getLocalDate());
}

private EfgsGatewayConfig getEfgsGatewayConfig() {
EfgsGatewayConfig efgsGatewayConfig = new EfgsGatewayConfig();
efgsGatewayConfig.setId("efgs-gateway");
efgsGatewayConfig.setBaseUrl("https://api.ch-hub-r.bit.admin.ch");
efgsGatewayConfig.setAuthClientCert(
"base64:/*");
efgsGatewayConfig.setAuthClientCertPassword("*");
efgsGatewayConfig.setSignClientCert(
"-----BEGIN CERTIFICATE-----\n*\n-----END CERTIFICATE-----\n");
efgsGatewayConfig.setSignClientCertPrivateKey(
"-----BEGIN PRIVATE KEY-----\n*\n-----END PRIVATE KEY-----\n");
efgsGatewayConfig.setSignAlgorithmName("sha256WithRSAEncryption");
efgsGatewayConfig.setVisitedCountries(List.of("CH", "DE"));
return efgsGatewayConfig;
}

private List<GaenKeyWithOrigin> createMockedKeys(int numOfKeysToCreate) {
List<GaenKeyWithOrigin> keys = new ArrayList<>();
for (int i = 0; i < numOfKeysToCreate; i++) {
byte[] bytes = new byte[16];
SECURE_RANDOM.nextBytes(bytes);
GaenKeyWithOrigin keyWithOrigin = new GaenKeyWithOrigin();
keyWithOrigin.setGaenKey(new GaenKey());
keyWithOrigin.setKeyData(java.util.Base64.getEncoder().encodeToString(bytes));
keyWithOrigin.setRollingStartNumber(
(int) UTCInstant.now().atStartOfDay().minusDays(1).get10MinutesSince1970());
keyWithOrigin.setRollingPeriod(144);
keyWithOrigin.setTransmissionRiskLevel(0);
keyWithOrigin.setFake(0);
keyWithOrigin.setOrigin("CH");
keyWithOrigin.setId(i);
keys.add(keyWithOrigin);
}
return keys;
}

private String getBatchTag() {
byte[] hash = new byte[4];
SECURE_RANDOM.nextBytes(hash);
var now = LocalDateTime.now(ZoneOffset.UTC);
return String.format(
"%d-%d-%d-%s-%d",
now.getYear(),
now.getMonth().getValue(),
now.getDayOfMonth(),
Base64.encodeBase64String(hash),
0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

package org.dpppt.backend.sdk.interops.config;

import javax.sql.DataSource;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FlyWayConfig {

@Autowired DataSource dataSource;

@Bean
public Flyway flyway() {
Flyway flyWay =
Flyway.configure()
.dataSource(dataSource)
.locations("classpath:/db/migration/hsqldb")
.load();
flyWay.migrate();
return flyWay;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

package org.dpppt.backend.sdk.interops.config;

import java.time.Duration;
import javax.sql.DataSource;
import org.dpppt.backend.sdk.data.gaen.GaenDataService;
import org.dpppt.backend.sdk.data.gaen.JdbcGaenDataServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
public class GaenDataServiceConfig {

@Value("${ws.exposedlist.releaseBucketDuration: 7200000}")
long releaseBucketDuration;

final String originCountry = "CH";

final Duration timeSkew = Duration.ofHours(2);

@Autowired DataSource dataSource;

@Autowired String dbType;

@Bean
public GaenDataService gaenDataService() {
return new JdbcGaenDataServiceImpl(
dbType, dataSource, Duration.ofMillis(releaseBucketDuration), timeSkew, originCountry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

package org.dpppt.backend.sdk.interops.config;

import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

@Configuration
public class StandaloneDataConfig {

@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.setType(EmbeddedDatabaseType.HSQL)
.build();
}

@Bean
public String dbType() {
return "hsqldb";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2020 Ubique Innovation AG <https://www.ubique.ch>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* SPDX-License-Identifier: MPL-2.0
*/

package org.dpppt.backend.sdk.interops.config;

import javax.sql.DataSource;
import org.dpppt.backend.sdk.data.interops.JdbcSyncLogDataServiceImpl;
import org.dpppt.backend.sdk.data.interops.SyncLogDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
public class SyncLogDataServiceConfig {

@Autowired DataSource dataSource;

@Autowired String dbType;

@Bean
public SyncLogDataService syncLogDataService() {
return new JdbcSyncLogDataServiceImpl(dbType, dataSource);
}
}

0 comments on commit 191d56a

Please sign in to comment.