@Test public void createWithExistingContextPopulated() { RuntimeContext runtimeContext = mock(RuntimeContext.class); when(contextHolder.getContext()).thenReturn(Optional.of(runtimeContext)); RuntimeContext result = runtimeContextFactory.create(mock(Config.class)); verify(contextHolder).getContext(); assertThat(result).isSameAs(runtimeContext); }
@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);  }