use of org.eclipse.milo.opcua.sdk.server.api.config.OpcUaServerConfig in project wildfly-camel by wildfly-extras.
the class MiloIntegrationTest method testClientWrite.
@Test
public void testClientWrite() throws Exception {
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
from(MILO_SERVER_ITEM_1).to(MOCK_TEST_1);
from(MILO_SERVER_ITEM_2).to(MOCK_TEST_2);
from(DIRECT_START_1).to(resolve(MILO_CLIENT_ITEM_C1_1));
from(DIRECT_START_2).to(resolve(MILO_CLIENT_ITEM_C1_2));
from(DIRECT_START_3).to(resolve(MILO_CLIENT_ITEM_C2_1));
from(DIRECT_START_4).to(resolve(MILO_CLIENT_ITEM_C2_2));
}
});
OpcUaServerConfig serverConfig = OpcUaServerConfig.builder().setEndpointAddresses(Arrays.asList(new String[] { "localhost" })).setCertificateValidator(new CertificateValidator() {
@Override
public void validate(X509Certificate certificate) throws UaException {
}
@Override
public void verifyTrustChain(List<X509Certificate> certificateChain) throws UaException {
}
}).setCertificateManager(new DefaultCertificateManager()).setUserTokenPolicies(singletonList(USER_TOKEN_POLICY_ANONYMOUS)).setIdentityValidator(AnonymousIdentityValidator.INSTANCE).build();
MiloServerComponent server = new MiloServerComponent(serverConfig);
server.setBindAddresses("localhost");
server.setBindPort(serverPort);
server.setEnableAnonymousAuthentication(true);
camelctx.addComponent("milo-server", server);
camelctx.start();
try {
MockEndpoint test1Endpoint = camelctx.getEndpoint(MOCK_TEST_1, MockEndpoint.class);
MockEndpoint test2Endpoint = camelctx.getEndpoint(MOCK_TEST_2, MockEndpoint.class);
// item 1
test1Endpoint.setExpectedCount(2);
testBody(test1Endpoint.message(0), assertGoodValue("Foo1"));
testBody(test1Endpoint.message(1), assertGoodValue("Foo3"));
// item 1
test2Endpoint.setExpectedCount(2);
testBody(test2Endpoint.message(0), assertGoodValue("Foo2"));
testBody(test2Endpoint.message(1), assertGoodValue("Foo4"));
ProducerTemplate producer1 = camelctx.createProducerTemplate();
producer1.setDefaultEndpointUri(DIRECT_START_1);
ProducerTemplate producer2 = camelctx.createProducerTemplate();
producer2.setDefaultEndpointUri(DIRECT_START_2);
ProducerTemplate producer3 = camelctx.createProducerTemplate();
producer3.setDefaultEndpointUri(DIRECT_START_3);
ProducerTemplate producer4 = camelctx.createProducerTemplate();
producer4.setDefaultEndpointUri(DIRECT_START_4);
// send
sendValue(producer1, new Variant("Foo1"));
sendValue(producer2, new Variant("Foo2"));
sendValue(producer3, new Variant("Foo3"));
sendValue(producer4, new Variant("Foo4"));
MockEndpoint.assertIsSatisfied(camelctx);
} finally {
camelctx.stop();
}
}
use of org.eclipse.milo.opcua.sdk.server.api.config.OpcUaServerConfig in project milo by eclipse.
the class TestServer method create.
public static OpcUaServer create(int port) throws Exception {
File securityTempDir = new File(System.getProperty("java.io.tmpdir"), "security");
if (!securityTempDir.exists() && !securityTempDir.mkdirs()) {
throw new Exception("unable to create security temp dir: " + securityTempDir);
}
LoggerFactory.getLogger(TestServer.class).info("security temp dir: {}", securityTempDir.getAbsolutePath());
KeyStoreLoader loader = new KeyStoreLoader().load(securityTempDir);
DefaultCertificateManager certificateManager = new DefaultCertificateManager(loader.getServerKeyPair(), loader.getServerCertificateChain());
File pkiDir = securityTempDir.toPath().resolve("pki").toFile();
DefaultTrustListManager trustListManager = new DefaultTrustListManager(pkiDir);
LoggerFactory.getLogger(TestServer.class).info("pki dir: {}", pkiDir.getAbsolutePath());
DefaultServerCertificateValidator certificateValidator = new DefaultServerCertificateValidator(trustListManager);
KeyPair httpsKeyPair = SelfSignedCertificateGenerator.generateRsaKeyPair(2048);
SelfSignedHttpsCertificateBuilder httpsCertificateBuilder = new SelfSignedHttpsCertificateBuilder(httpsKeyPair);
httpsCertificateBuilder.setCommonName(HostnameUtil.getHostname());
HostnameUtil.getHostnames("localhost", false).forEach(httpsCertificateBuilder::addDnsName);
X509Certificate httpsCertificate = httpsCertificateBuilder.build();
UsernameIdentityValidator identityValidator = new UsernameIdentityValidator(true, authChallenge -> {
String username = authChallenge.getUsername();
String password = authChallenge.getPassword();
boolean user1 = "user1".equals(username) && "password".equals(password);
boolean user2 = "user2".equals(username) && "password".equals(password);
boolean admin = "admin".equals(username) && "password".equals(password);
return user1 || user2 || admin;
});
// If you need to use multiple certificates you'll have to be smarter than this.
X509Certificate certificate = certificateManager.getCertificates().stream().findFirst().orElseThrow(() -> new UaRuntimeException(StatusCodes.Bad_ConfigurationError, "no certificate found"));
// The configured application URI must match the one in the certificate(s)
String applicationUri = CertificateUtil.getSanUri(certificate).orElseThrow(() -> new UaRuntimeException(StatusCodes.Bad_ConfigurationError, "certificate is missing the application URI"));
Set<EndpointConfiguration> endpointConfigurations = createEndpointConfigurations(certificate, port);
OpcUaServerConfig serverConfig = OpcUaServerConfig.builder().setApplicationUri(applicationUri).setApplicationName(LocalizedText.english("Eclipse Milo OPC UA Example Server")).setEndpoints(endpointConfigurations).setBuildInfo(new BuildInfo("urn:eclipse:milo:example-server", "eclipse", "eclipse milo example server", OpcUaServer.SDK_VERSION, "", DateTime.now())).setCertificateManager(certificateManager).setTrustListManager(trustListManager).setCertificateValidator(certificateValidator).setHttpsKeyPair(httpsKeyPair).setHttpsCertificate(httpsCertificate).setIdentityValidator(identityValidator).setProductUri("urn:eclipse:milo:example-server").build();
return new OpcUaServer(serverConfig);
}
use of org.eclipse.milo.opcua.sdk.server.api.config.OpcUaServerConfig in project OpenMUC by isc-konstanz.
the class OpcServer method activate.
@Activate
public void activate() throws Exception {
logger.info("Activating OPC UA Server");
File securityTempDir = new File(System.getProperty("java.io.tmpdir"), "security");
if (!securityTempDir.exists() && !securityTempDir.mkdirs()) {
throw new Exception("Unable to create security temp dir: " + securityTempDir);
}
logger.debug("OPC UA security temp dir: {}", securityTempDir.getAbsolutePath());
KeyStoreLoader loader = new KeyStoreLoader().load(securityTempDir);
DefaultCertificateManager certificateManager = new DefaultCertificateManager(loader.getServerKeyPair(), loader.getServerCertificateChain());
File pkiDir = securityTempDir.toPath().resolve("pki").toFile();
DefaultTrustListManager trustListManager = new DefaultTrustListManager(pkiDir);
logger.debug("OPC UA pki dir: {}", pkiDir.getAbsolutePath());
DefaultServerCertificateValidator certificateValidator = new DefaultServerCertificateValidator(trustListManager);
KeyPair httpsKeyPair = SelfSignedCertificateGenerator.generateRsaKeyPair(2048);
SelfSignedHttpsCertificateBuilder httpsCertificateBuilder = new SelfSignedHttpsCertificateBuilder(httpsKeyPair);
httpsCertificateBuilder.setCommonName(HostnameUtil.getHostname());
HostnameUtil.getHostnames("0.0.0.0").forEach(httpsCertificateBuilder::addDnsName);
X509Certificate httpsCertificate = httpsCertificateBuilder.build();
// UsernameIdentityValidator identityValidator = new UsernameIdentityValidator(true, authChallenge -> {
// String username = authChallenge.getUsername();
// String password = authChallenge.getPassword();
//
// boolean userOk = "user".equals(username) && "password1".equals(password);
// boolean adminOk = "admin".equals(username) && "password2".equals(password);
//
// return userOk || adminOk;
// });
//
// X509IdentityValidator x509IdentityValidator = new X509IdentityValidator(c -> true);
// If you need to use multiple certificates you'll have to be smarter than this.
X509Certificate certificate = certificateManager.getCertificates().stream().findFirst().orElseThrow(() -> new UaRuntimeException(StatusCodes.Bad_ConfigurationError, "no certificate found"));
// The configured application URI must match the one in the certificate(s)
String applicationUri = CertificateUtil.getSanUri(certificate).orElseThrow(() -> new UaRuntimeException(StatusCodes.Bad_ConfigurationError, "certificate is missing the application URI"));
Set<EndpointConfiguration> endpointConfigurations = createEndpointConfigurations(certificate);
OpcUaServerConfig serverConfig = OpcUaServerConfig.builder().setApplicationUri(applicationUri).setApplicationName(LocalizedText.english("OpenMUC OPC UA Server")).setEndpoints(endpointConfigurations).setBuildInfo(new BuildInfo("urn:openmuc:server", "openmuc", "openmuc server", OpcUaServer.SDK_VERSION, "", new DateTime(System.currentTimeMillis()))).setCertificateManager(certificateManager).setTrustListManager(trustListManager).setCertificateValidator(certificateValidator).setHttpsKeyPair(httpsKeyPair).setHttpsCertificate(httpsCertificate).setProductUri("urn:openmuc:server").build();
server = new OpcUaServer(serverConfig);
server.startup();
}
Aggregations