@Test public void createMinimal() { when(contextHolder.getContext()).thenReturn(Optional.empty()); Config confg = mock(Config.class); EncryptorConfig encryptorConfig = mock(EncryptorConfig.class); when(encryptorConfig.getType()).thenReturn(EncryptorType.NACL); when(confg.getEncryptor()).thenReturn(encryptorConfig); KeyConfiguration keyConfiguration = mock(KeyConfiguration.class); when(confg.getKeys()).thenReturn(keyConfiguration); ServerConfig serverConfig = mock(ServerConfig.class); when(serverConfig.getApp()).thenReturn(AppType.P2P); when(serverConfig.getCommunicationType()).thenReturn(CommunicationType.REST); when(confg.getP2PServerConfig()).thenReturn(serverConfig); when(serverConfig.getServerUri()).thenReturn(URI.create("http: when(serverConfig.getBindingUri()).thenReturn(URI.create("http: when(serverConfig.getProperties()).thenReturn(Collections.emptyMap()); when(confg.getServerConfigs()).thenReturn(List.of(serverConfig)); FeatureToggles featureToggles = mock(FeatureToggles.class); when(confg.getFeatures()).thenReturn(featureToggles); RuntimeContext result = runtimeContextFactory.create(confg); assertThat(result).isNotNull(); verify(contextHolder).getContext(); verify(contextHolder).setContext(any(RuntimeContext.class)); }
@Override public RuntimeContext create(Config config) { Optional<RuntimeContext> storedContext = contextHolder.getContext(); if (storedContext.isPresent()) { return storedContext.get(); } EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyVaultConfigValidations vaultConfigValidation = KeyVaultConfigValidations.create(); final RuntimeContextBuilder runtimeContextBuilder = RuntimeContextBuilder.create(); if (Objects.nonNull(config.getKeys())) { List<ConfigKeyPair> configKeyPairs = config.getKeys().getKeyData().stream() .map(o -> KeyDataUtil.unmarshal(o, keyEncryptor)) .collect(Collectors.toList()); Set<ConstraintViolation<?>> violations = vaultConfigValidation.validate(config.getKeys(), configKeyPairs); if (!violations.isEmpty()) { LOGGER.debug("Constraint violations {}", violations); throw new ConstraintViolationException(violations); } KeyPairConverter keyPairConverter = new KeyPairConverter(config, new EnvironmentVariableProvider()); List<KeyPair> pairs = new ArrayList<>(keyPairConverter.convert(configKeyPairs)); runtimeContextBuilder.withKeys(pairs); } List<ServerConfig> servers = config.getServerConfigs(); ServerConfig p2pServerContext = servers.stream() .filter(s -> s.getApp() == AppType.P2P) .findFirst() .orElseThrow(() -> new IllegalStateException("No P2P server configured")); Client p2pClient = RestClientFactory.create().buildFrom(p2pServerContext); List<PublicKey> alwaysSendTo = Stream.of(config) .map(Config::getAlwaysSendTo) .filter(Objects::nonNull) .flatMap(List::stream) .map(Base64.getDecoder()::decode) .map(PublicKey::from) .collect(Collectors.toList()); RuntimeContext context = runtimeContextBuilder .withP2pServerUri(config.getP2PServerConfig().getServerUri()) .withP2pClient(p2pClient) .withKeyEncryptor(keyEncryptor) .withDisablePeerDiscovery(config.isDisablePeerDiscovery()) .withRemoteKeyValidation(config.getFeatures().isEnableRemoteKeyValidation()) .withPeers( config.getPeers().stream() .map(Peer::getUrl) .map(URI::create) .collect(Collectors.toList())) .withAlwaysSendTo(alwaysSendTo) .withUseWhiteList(config.isUseWhiteList()) .build(); contextHolder.setContext(context); return context; }
DefaultRuntimeContextFactory implements RuntimeContextFactory<Config> { @Override public RuntimeContext create(Config config) { Optional<RuntimeContext> storedContext = contextHolder.getContext(); if (storedContext.isPresent()) { return storedContext.get(); } EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyVaultConfigValidations vaultConfigValidation = KeyVaultConfigValidations.create(); final RuntimeContextBuilder runtimeContextBuilder = RuntimeContextBuilder.create(); if (Objects.nonNull(config.getKeys())) { List<ConfigKeyPair> configKeyPairs = config.getKeys().getKeyData().stream() .map(o -> KeyDataUtil.unmarshal(o, keyEncryptor)) .collect(Collectors.toList()); Set<ConstraintViolation<?>> violations = vaultConfigValidation.validate(config.getKeys(), configKeyPairs); if (!violations.isEmpty()) { LOGGER.debug("Constraint violations {}", violations); throw new ConstraintViolationException(violations); } KeyPairConverter keyPairConverter = new KeyPairConverter(config, new EnvironmentVariableProvider()); List<KeyPair> pairs = new ArrayList<>(keyPairConverter.convert(configKeyPairs)); runtimeContextBuilder.withKeys(pairs); } List<ServerConfig> servers = config.getServerConfigs(); ServerConfig p2pServerContext = servers.stream() .filter(s -> s.getApp() == AppType.P2P) .findFirst() .orElseThrow(() -> new IllegalStateException("No P2P server configured")); Client p2pClient = RestClientFactory.create().buildFrom(p2pServerContext); List<PublicKey> alwaysSendTo = Stream.of(config) .map(Config::getAlwaysSendTo) .filter(Objects::nonNull) .flatMap(List::stream) .map(Base64.getDecoder()::decode) .map(PublicKey::from) .collect(Collectors.toList()); RuntimeContext context = runtimeContextBuilder .withP2pServerUri(config.getP2PServerConfig().getServerUri()) .withP2pClient(p2pClient) .withKeyEncryptor(keyEncryptor) .withDisablePeerDiscovery(config.isDisablePeerDiscovery()) .withRemoteKeyValidation(config.getFeatures().isEnableRemoteKeyValidation()) .withPeers( config.getPeers().stream() .map(Peer::getUrl) .map(URI::create) .collect(Collectors.toList())) .withAlwaysSendTo(alwaysSendTo) .withUseWhiteList(config.isUseWhiteList()) .build(); contextHolder.setContext(context); return context; } }
DefaultRuntimeContextFactory implements RuntimeContextFactory<Config> { @Override public RuntimeContext create(Config config) { Optional<RuntimeContext> storedContext = contextHolder.getContext(); if (storedContext.isPresent()) { return storedContext.get(); } EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyVaultConfigValidations vaultConfigValidation = KeyVaultConfigValidations.create(); final RuntimeContextBuilder runtimeContextBuilder = RuntimeContextBuilder.create(); if (Objects.nonNull(config.getKeys())) { List<ConfigKeyPair> configKeyPairs = config.getKeys().getKeyData().stream() .map(o -> KeyDataUtil.unmarshal(o, keyEncryptor)) .collect(Collectors.toList()); Set<ConstraintViolation<?>> violations = vaultConfigValidation.validate(config.getKeys(), configKeyPairs); if (!violations.isEmpty()) { LOGGER.debug("Constraint violations {}", violations); throw new ConstraintViolationException(violations); } KeyPairConverter keyPairConverter = new KeyPairConverter(config, new EnvironmentVariableProvider()); List<KeyPair> pairs = new ArrayList<>(keyPairConverter.convert(configKeyPairs)); runtimeContextBuilder.withKeys(pairs); } List<ServerConfig> servers = config.getServerConfigs(); ServerConfig p2pServerContext = servers.stream() .filter(s -> s.getApp() == AppType.P2P) .findFirst() .orElseThrow(() -> new IllegalStateException("No P2P server configured")); Client p2pClient = RestClientFactory.create().buildFrom(p2pServerContext); List<PublicKey> alwaysSendTo = Stream.of(config) .map(Config::getAlwaysSendTo) .filter(Objects::nonNull) .flatMap(List::stream) .map(Base64.getDecoder()::decode) .map(PublicKey::from) .collect(Collectors.toList()); RuntimeContext context = runtimeContextBuilder .withP2pServerUri(config.getP2PServerConfig().getServerUri()) .withP2pClient(p2pClient) .withKeyEncryptor(keyEncryptor) .withDisablePeerDiscovery(config.isDisablePeerDiscovery()) .withRemoteKeyValidation(config.getFeatures().isEnableRemoteKeyValidation()) .withPeers( config.getPeers().stream() .map(Peer::getUrl) .map(URI::create) .collect(Collectors.toList())) .withAlwaysSendTo(alwaysSendTo) .withUseWhiteList(config.isUseWhiteList()) .build(); contextHolder.setContext(context); return context; } DefaultRuntimeContextFactory();   DefaultRuntimeContextFactory(ContextHolder contextHolder); }
DefaultRuntimeContextFactory implements RuntimeContextFactory<Config> { @Override public RuntimeContext create(Config config) { Optional<RuntimeContext> storedContext = contextHolder.getContext(); if (storedContext.isPresent()) { return storedContext.get(); } EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyVaultConfigValidations vaultConfigValidation = KeyVaultConfigValidations.create(); final RuntimeContextBuilder runtimeContextBuilder = RuntimeContextBuilder.create(); if (Objects.nonNull(config.getKeys())) { List<ConfigKeyPair> configKeyPairs = config.getKeys().getKeyData().stream() .map(o -> KeyDataUtil.unmarshal(o, keyEncryptor)) .collect(Collectors.toList()); Set<ConstraintViolation<?>> violations = vaultConfigValidation.validate(config.getKeys(), configKeyPairs); if (!violations.isEmpty()) { LOGGER.debug("Constraint violations {}", violations); throw new ConstraintViolationException(violations); } KeyPairConverter keyPairConverter = new KeyPairConverter(config, new EnvironmentVariableProvider()); List<KeyPair> pairs = new ArrayList<>(keyPairConverter.convert(configKeyPairs)); runtimeContextBuilder.withKeys(pairs); } List<ServerConfig> servers = config.getServerConfigs(); ServerConfig p2pServerContext = servers.stream() .filter(s -> s.getApp() == AppType.P2P) .findFirst() .orElseThrow(() -> new IllegalStateException("No P2P server configured")); Client p2pClient = RestClientFactory.create().buildFrom(p2pServerContext); List<PublicKey> alwaysSendTo = Stream.of(config) .map(Config::getAlwaysSendTo) .filter(Objects::nonNull) .flatMap(List::stream) .map(Base64.getDecoder()::decode) .map(PublicKey::from) .collect(Collectors.toList()); RuntimeContext context = runtimeContextBuilder .withP2pServerUri(config.getP2PServerConfig().getServerUri()) .withP2pClient(p2pClient) .withKeyEncryptor(keyEncryptor) .withDisablePeerDiscovery(config.isDisablePeerDiscovery()) .withRemoteKeyValidation(config.getFeatures().isEnableRemoteKeyValidation()) .withPeers( config.getPeers().stream() .map(Peer::getUrl) .map(URI::create) .collect(Collectors.toList())) .withAlwaysSendTo(alwaysSendTo) .withUseWhiteList(config.isUseWhiteList()) .build(); contextHolder.setContext(context); return context; } DefaultRuntimeContextFactory();   DefaultRuntimeContextFactory(ContextHolder contextHolder); @Override RuntimeContext create(Config config); }
DefaultRuntimeContextFactory implements RuntimeContextFactory<Config> { @Override public RuntimeContext create(Config config) { Optional<RuntimeContext> storedContext = contextHolder.getContext(); if (storedContext.isPresent()) { return storedContext.get(); } EncryptorConfig encryptorConfig = Optional.ofNullable(config.getEncryptor()) .orElse( new EncryptorConfig() { { setType(EncryptorType.NACL); } }); KeyEncryptor keyEncryptor = KeyEncryptorFactory.newFactory().create(encryptorConfig); final KeyVaultConfigValidations vaultConfigValidation = KeyVaultConfigValidations.create(); final RuntimeContextBuilder runtimeContextBuilder = RuntimeContextBuilder.create(); if (Objects.nonNull(config.getKeys())) { List<ConfigKeyPair> configKeyPairs = config.getKeys().getKeyData().stream() .map(o -> KeyDataUtil.unmarshal(o, keyEncryptor)) .collect(Collectors.toList()); Set<ConstraintViolation<?>> violations = vaultConfigValidation.validate(config.getKeys(), configKeyPairs); if (!violations.isEmpty()) { LOGGER.debug("Constraint violations {}", violations); throw new ConstraintViolationException(violations); } KeyPairConverter keyPairConverter = new KeyPairConverter(config, new EnvironmentVariableProvider()); List<KeyPair> pairs = new ArrayList<>(keyPairConverter.convert(configKeyPairs)); runtimeContextBuilder.withKeys(pairs); } List<ServerConfig> servers = config.getServerConfigs(); ServerConfig p2pServerContext = servers.stream() .filter(s -> s.getApp() == AppType.P2P) .findFirst() .orElseThrow(() -> new IllegalStateException("No P2P server configured")); Client p2pClient = RestClientFactory.create().buildFrom(p2pServerContext); List<PublicKey> alwaysSendTo = Stream.of(config) .map(Config::getAlwaysSendTo) .filter(Objects::nonNull) .flatMap(List::stream) .map(Base64.getDecoder()::decode) .map(PublicKey::from) .collect(Collectors.toList()); RuntimeContext context = runtimeContextBuilder .withP2pServerUri(config.getP2PServerConfig().getServerUri()) .withP2pClient(p2pClient) .withKeyEncryptor(keyEncryptor) .withDisablePeerDiscovery(config.isDisablePeerDiscovery()) .withRemoteKeyValidation(config.getFeatures().isEnableRemoteKeyValidation()) .withPeers( config.getPeers().stream() .map(Peer::getUrl) .map(URI::create) .collect(Collectors.toList())) .withAlwaysSendTo(alwaysSendTo) .withUseWhiteList(config.isUseWhiteList()) .build(); contextHolder.setContext(context); return context; } DefaultRuntimeContextFactory();   DefaultRuntimeContextFactory(ContextHolder contextHolder); @Override RuntimeContext create(Config config);  }