use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project vantiq-extension-sources by Vantiq.
the class Connection method testConnectionSecureBadCert.
@Test
public void testConnectionSecureBadCert() throws Exception {
List<EndpointDescription> eps = exampleServer.getServer().getEndpointDescriptions();
EnumSet<MessageSecurityMode> serverMsgModes = EnumSet.noneOf(MessageSecurityMode.class);
EnumSet<SecurityPolicy> serverSecPols = EnumSet.noneOf(SecurityPolicy.class);
for (EndpointDescription ep : eps) {
if (ep.getEndpointUrl().startsWith("opc.tpc")) {
// At present, these are all we test
serverSecPols.add(SecurityPolicy.fromUri(ep.getSecurityPolicyUri()));
serverMsgModes.add(ep.getSecurityMode());
}
}
// Below, we'll traverse the valid combinations. None's must be paired and are tested elsewhere
for (SecurityPolicy secPol : serverSecPols) {
if (!secPol.equals(SecurityPolicy.None)) {
for (MessageSecurityMode msgSec : serverMsgModes) {
if (!msgSec.equals(MessageSecurityMode.None)) {
// Defaults tested in *Upw test...
for (String certKey : untrustedTestCerts) {
try {
log.info("Attempting sync connection using [{}, {}] using certificate: '{}'", secPol, msgSec, certKey);
makeRawConnection(false, secPol.getUri(), msgSec.toString(), OpcConstants.CONFIG_IDENTITY_CERTIFICATE, certKey);
} catch (ExecutionException e) {
assert e.getMessage().contains("UaException");
assert e.getMessage().contains("status=Bad_");
assert e.getMessage().contains("message=java.security.InvalidKeyException: Not an RSA key: EC");
}
try {
log.info("Attempting async connection using [{}, {}] using certificate: '{}'", secPol, msgSec, certKey);
makeRawConnection(true, secPol.getUri(), msgSec.toString(), OpcConstants.CONFIG_IDENTITY_CERTIFICATE, certKey);
} catch (ExecutionException e) {
Utils.unexpectedException(e);
} catch (CompletionException e) {
assert e.getMessage().contains("UaException");
assert e.getMessage().contains("status=Bad_");
assert e.getMessage().contains("message=java.security.InvalidKeyException: Not an RSA key: EC");
}
}
}
}
}
}
}
use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project OpenMUC by isc-konstanz.
the class OpcConnection method connect.
@Connect
public void connect() throws ConnectionException {
try {
Path securityTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "security");
Files.createDirectories(securityTempDir);
if (!Files.exists(securityTempDir)) {
throw new ConnectionException("Unable to create security dir: " + securityTempDir);
}
logger.debug("Security temp dir: {}", securityTempDir.toAbsolutePath());
KeyStoreLoader loader = new KeyStoreLoader().load(securityTempDir);
if (!address.contains("opc.tcp://")) {
host = address.split(":")[0];
port = Integer.parseInt(address.split(":")[1]);
address = "opc.tcp://" + address;
} else {
host = address.split("//|:")[1];
port = Integer.parseInt(address.split("//|:")[2]);
}
List<EndpointDescription> endpoints = DiscoveryClient.getEndpoints(address).get();
EndpointDescription endpoint = endpoints.stream().filter(e -> true).findFirst().orElseThrow(() -> new UaException(StatusCodes.Bad_ConfigurationError, "No endpoint selected"));
logger.info("OPC Client connecting to {}.", address);
endpoint = EndpointUtil.updateUrl(endpoints.get(0), host, port);
OpcUaClientConfigBuilder clientBuilder = new OpcUaClientConfigBuilder().setEndpoint(endpoint).setApplicationName(LocalizedText.english("OpenMUC OPC UA Client")).setApplicationUri("urn:openmuc:client").setCertificate(loader.getClientCertificate()).setKeyPair(loader.getClientKeyPair()).setIdentityProvider(new AnonymousProvider()).setRequestTimeout(uint(5000));
client = OpcUaClient.create(clientBuilder.build());
client.connect().get();
// Get a typed reference to the Server object: ServerNode
ServerTypeNode serverNode = client.getAddressSpace().getObjectNode(Identifiers.Server, ServerTypeNode.class).get();
if (namespaceUri != null && !namespaceUri.isEmpty()) {
try {
namespaceIndex = Integer.parseInt(namespaceUri);
} catch (NumberFormatException e) {
namespaceIndex = Arrays.asList(serverNode.getNamespaceArray().get()).indexOf(namespaceUri);
}
}
} catch (Exception e) {
logger.error("OPC connection to server failed {}", e);
}
}
use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project milo by eclipse.
the class OpcUaClient method create.
/**
* Create and configure an {@link OpcUaClient} by selecting an {@link EndpointDescription} from a list of endpoints
* retrieved via the GetEndpoints service from the server at {@code endpointUrl} and building an
* {@link OpcUaClientConfig} using that endpoint.
*
* @param endpointUrl the endpoint URL of the server to connect to and retrieve endpoints from.
* @param selectEndpoint a function that selects the {@link EndpointDescription} to connect to from the list of
* endpoints from the server.
* @param buildConfig a function that configures an {@link OpcUaClientConfigBuilder} and then builds and returns
* an {@link OpcUaClientConfig}.
* @return a configured {@link OpcUaClient}.
* @throws UaException if the endpoints could not be retrieved or the client could not be created.
*/
public static OpcUaClient create(String endpointUrl, Function<List<EndpointDescription>, Optional<EndpointDescription>> selectEndpoint, Function<OpcUaClientConfigBuilder, OpcUaClientConfig> buildConfig) throws UaException {
try {
List<EndpointDescription> endpoints = DiscoveryClient.getEndpoints(endpointUrl).get();
EndpointDescription endpoint = selectEndpoint.apply(endpoints).orElseThrow(() -> new UaException(StatusCodes.Bad_ConfigurationError, "no endpoint selected"));
OpcUaClientConfigBuilder builder = OpcUaClientConfig.builder().setEndpoint(endpoint);
return create(buildConfig.apply(builder));
} catch (InterruptedException | ExecutionException e) {
if (!endpointUrl.endsWith("/discovery")) {
StringBuilder discoveryUrl = new StringBuilder(endpointUrl);
if (!endpointUrl.endsWith("/")) {
discoveryUrl.append("/");
}
discoveryUrl.append("discovery");
return create(discoveryUrl.toString(), selectEndpoint, buildConfig);
} else {
throw UaException.extract(e).orElseGet(() -> new UaException(e));
}
}
}
use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project milo by eclipse.
the class AnonymousProviderTest method testGetIdentityToken_EmptyPolicyId.
@Test
public void testGetIdentityToken_EmptyPolicyId() throws Exception {
EndpointDescription endpoint = new EndpointDescription(null, null, null, null, null, new UserTokenPolicy[] { new UserTokenPolicy("", UserTokenType.Anonymous, null, null, null) }, null, null);
AnonymousProvider p = new AnonymousProvider();
SignedIdentityToken signedIdentityToken = p.getIdentityToken(endpoint, ByteString.NULL_VALUE);
assertEquals(signedIdentityToken.getToken().getPolicyId(), "");
assertTrue(signedIdentityToken.getToken() instanceof AnonymousIdentityToken);
}
use of org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription in project milo by eclipse.
the class AnonymousProviderTest method testGetIdentityToken.
@Test
public void testGetIdentityToken() throws Exception {
EndpointDescription endpoint = new EndpointDescription(null, null, null, null, null, new UserTokenPolicy[] { new UserTokenPolicy("anonymous", UserTokenType.Anonymous, null, null, null) }, null, null);
AnonymousProvider p = new AnonymousProvider();
SignedIdentityToken signedIdentityToken = p.getIdentityToken(endpoint, ByteString.NULL_VALUE);
assertEquals(signedIdentityToken.getToken().getPolicyId(), "anonymous");
assertTrue(signedIdentityToken.getToken() instanceof AnonymousIdentityToken);
}
Aggregations