Search in sources :

Example 1 with UsernameProvider

use of org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider in project vantiq-extension-sources by Vantiq.

the class OpcUaESClient method constructIdentityProvider.

private IdentityProvider constructIdentityProvider(Map<String, Object> config) throws OpcExtConfigException, OpcExtKeyStoreException {
    IdentityProvider retVal = null;
    String anonymous = (String) config.get(OpcConstants.CONFIG_IDENTITY_ANONYMOUS);
    // This can be empty -- presence is sufficient
    boolean anonIsPresent = anonymous != null;
    String certAlias = (String) config.get(OpcConstants.CONFIG_IDENTITY_CERTIFICATE);
    boolean certIsPresent = foundValue(certAlias);
    String userPass = (String) config.get(OpcConstants.CONFIG_IDENTITY_USERNAME_PASSWORD);
    boolean upwIsPresent = foundValue(userPass);
    boolean exactlyOnePresent = (anonIsPresent ^ certIsPresent ^ upwIsPresent) ^ (anonIsPresent && certIsPresent && upwIsPresent);
    if (!anonIsPresent && !certIsPresent && !upwIsPresent) {
        log.warn(ERROR_PREFIX + ".noIdentitySpecification: No identity specification was provided.  Using Anonymous as default.");
        retVal = new AnonymousProvider();
    } else if (exactlyOnePresent) {
        // Now we know there is exactly one of them set.
        if (anonIsPresent) {
            retVal = new AnonymousProvider();
        } else if (certIsPresent) {
            X509Certificate namedCert = keyStoreManager.fetchCertByAlias(certAlias);
            PrivateKey pKey = keyStoreManager.fetchPrivateKeyByAlias(certAlias);
            retVal = new X509IdentityProvider(namedCert, pKey);
        } else if (upwIsPresent) {
            String[] upw = userPass.split(",[ ]*");
            if (upw.length != 2) {
                String errMsg = MessageFormatter.arrayFormat(ERROR_PREFIX + ".invalidUserPasswordSpecification: the {} ({}) must contain only a username AND password separated by a comma.", new Object[] { OpcConstants.CONFIG_IDENTITY_USERNAME_PASSWORD, userPass }).getMessage();
                log.error(errMsg);
                throw new OpcExtConfigException(errMsg);
            } else {
                retVal = new UsernameProvider(upw[0], upw[1]);
            }
        }
    } else {
        String errMsg = MessageFormatter.arrayFormat(ERROR_PREFIX + ".invalidIdentitySpecification: exactly one identity specification ({}, {}, {}) is required.", new Object[] { OpcConstants.CONFIG_IDENTITY_ANONYMOUS, OpcConstants.CONFIG_IDENTITY_CERTIFICATE, OpcConstants.CONFIG_IDENTITY_USERNAME_PASSWORD }).getMessage();
        log.error(errMsg);
        throw new OpcExtConfigException(errMsg);
    }
    return retVal;
}
Also used : PrivateKey(java.security.PrivateKey) AnonymousProvider(org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider) X509IdentityProvider(org.eclipse.milo.opcua.sdk.client.api.identity.X509IdentityProvider) IdentityProvider(org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider) ByteString(org.eclipse.milo.opcua.stack.core.types.builtin.ByteString) X509Certificate(java.security.cert.X509Certificate) UsernameProvider(org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider) X509IdentityProvider(org.eclipse.milo.opcua.sdk.client.api.identity.X509IdentityProvider)

Example 2 with UsernameProvider

use of org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider in project iot-tree by bambooww.

the class ConnPtOPCUA method connect.

private // throws UaException
boolean connect() {
    if (uaClient != null)
        return true;
    try {
        String dir = Config.getDataTmpDir() + "/pcua/security/";
        File dirf = new File(dir);
        if (!dirf.exists())
            dirf.mkdirs();
        Path sec_p = Paths.get(dir);
        // EndpointDescription[] endpointDescription =
        // UaTcpStackClient.getEndpoints(EndPointUrl).get();
        KeyStoreLoader loader = new KeyStoreLoader().load(sec_p);
        UsernameProvider unp = getIdPro();
        uaClient = OpcUaClient.create(this.getOpcEndPointURI(), endpoints -> endpoints.stream().filter(endpointFilter()).findFirst(), configBuilder -> {
            configBuilder.setApplicationName(LocalizedText.english(this.getOpcAppNameDef())).setApplicationUri("urn:iottree:conn:opc_client");
            // configBuilder.setCertificate(loader.getClientCertificate()).setKeyPair(loader.getClientKeyPair());
            if (unp != null)
                configBuilder.setIdentityProvider(unp);
            configBuilder.setRequestTimeout(uint(reqTimeout));
            return configBuilder.build();
        });
        // uaClient.getStackClient().getNamespaceTable()
        uaClient.getSubscriptionManager().addSubscriptionListener(subLis);
        uaClient.connect();
    // ArrayList<NodeId> nids = new ArrayList<>() ;
    // nids.addAll(tag2nodeid.values());
    // uaClient.registerNodes(nids) ;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}
Also used : Path(java.nio.file.Path) MonitoringParameters(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters) Arrays(java.util.Arrays) ConnPt(org.iottree.core.ConnPt) UaSubscriptionManager(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscriptionManager) UaDataTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaDataTypeNode) Convert(org.iottree.core.util.Convert) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) JSONObject(org.json.JSONObject) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Config(org.iottree.core.Config) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) BrowseDescription(org.eclipse.milo.opcua.stack.core.types.structured.BrowseDescription) Map(java.util.Map) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) ConversionUtil.toList(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.toList) Path(java.nio.file.Path) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) BrowseResult(org.eclipse.milo.opcua.stack.core.types.structured.BrowseResult) ReferenceDescription(org.eclipse.milo.opcua.stack.core.types.structured.ReferenceDescription) BrowseDirection(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseDirection) Predicate(java.util.function.Predicate) Node(org.eclipse.milo.opcua.stack.core.types.structured.Node) UUID(java.util.UUID) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) UaVariableTypeNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableTypeNode) List(java.util.List) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) NodeClass(org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass) UaNode(org.eclipse.milo.opcua.sdk.client.nodes.UaNode) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) UACh(org.iottree.core.UACh) Writer(java.io.Writer) UaVariableNode(org.eclipse.milo.opcua.sdk.client.nodes.UaVariableNode) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode) FolderTypeNode(org.eclipse.milo.opcua.sdk.client.model.nodes.objects.FolderTypeNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) HashMap(java.util.HashMap) UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) ArrayList(java.util.ArrayList) XmlData(org.iottree.core.util.xmldata.XmlData) EndpointDescription(org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription) UShort(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort) UATag(org.iottree.core.UATag) UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) UANode(org.iottree.core.UANode) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) KeyStoreLoader(org.iottree.core.conn.opcua.KeyStoreLoader) IOException(java.io.IOException) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) BrowseResultMask(org.eclipse.milo.opcua.stack.core.types.enumerated.BrowseResultMask) Paths(java.nio.file.Paths) BindItem(org.iottree.core.conn.ConnPtBinder.BindItem) UaException(org.eclipse.milo.opcua.stack.core.UaException) DiscoveryClient(org.eclipse.milo.opcua.stack.client.DiscoveryClient) UsernameProvider(org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider) KeyStoreLoader(org.iottree.core.conn.opcua.KeyStoreLoader) File(java.io.File) UsernameProvider(org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) UaException(org.eclipse.milo.opcua.stack.core.UaException)

Aggregations

UsernameProvider (org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider)2 File (java.io.File)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 PrivateKey (java.security.PrivateKey)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 ExecutionException (java.util.concurrent.ExecutionException)1 Predicate (java.util.function.Predicate)1 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)1 AnonymousProvider (org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider)1 IdentityProvider (org.eclipse.milo.opcua.sdk.client.api.identity.IdentityProvider)1 X509IdentityProvider (org.eclipse.milo.opcua.sdk.client.api.identity.X509IdentityProvider)1