Search in sources :

Example 16 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class GwtSslServiceImpl method updateSslConfiguration.

@Override
public void updateSslConfiguration(GwtXSRFToken xsrfToken, GwtSslConfig sslConfig) throws GwtKuraException {
    checkXSRFToken(xsrfToken);
    try {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(PROP_PROTOCOL, sslConfig.getProtocol());
        properties.put(PROP_HN_VERIFY, sslConfig.isHostnameVerification());
        properties.put(PROP_TRUST_STORE, sslConfig.getKeyStore());
        if (PLACEHOLDER.equals(sslConfig.getKeystorePassword())) {
            CryptoService cryptoService = ServiceLocator.getInstance().getService(CryptoService.class);
            SslManagerServiceOptions options = getSslConfiguration();
            properties.put(PROP_TRUST_PASSWORD, new Password(cryptoService.decryptAes(options.getSslKeystorePassword().toCharArray())));
        } else {
            properties.put(PROP_TRUST_PASSWORD, new Password(sslConfig.getKeystorePassword()));
        }
        properties.put(PROP_CIPHERS, sslConfig.getCiphers());
        ConfigurationService configService = ServiceLocator.getInstance().getService(ConfigurationService.class);
        configService.updateConfiguration(SSL_PID, properties);
    } catch (KuraException e) {
        throw new GwtKuraException(e.getMessage());
    }
}
Also used : SslManagerServiceOptions(org.eclipse.kura.ssl.SslManagerServiceOptions) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) HashMap(java.util.HashMap) CryptoService(org.eclipse.kura.crypto.CryptoService) KuraException(org.eclipse.kura.KuraException) GwtKuraException(org.eclipse.kura.web.shared.GwtKuraException) ConfigurationService(org.eclipse.kura.configuration.ConfigurationService) Password(org.eclipse.kura.configuration.Password)

Example 17 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class WifiConfig method setPasskey.

public void setPasskey(String key) {
    Password psswd = new Password(key);
    this.m_passkey = psswd;
}
Also used : Password(org.eclipse.kura.configuration.Password)

Example 18 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class MqttDataTransport method buildConfiguration.

/*
     * This method builds an internal configuration option needed by the client
     * to connect. The configuration is assembled from various sources:
     * component configuration, SystemService, NetworkService, etc. The returned
     * configuration is valid so no further validation is needed. If a valid
     * configuration cannot be assembled the method throws a RuntimeException
     * (assuming that this error is unrecoverable).
     */
private MqttClientConfiguration buildConfiguration(Map<String, Object> properties) {
    MqttClientConfiguration clientConfiguration;
    MqttConnectOptions conOpt = new MqttConnectOptions();
    String clientId = null;
    String brokerUrl = null;
    try {
        // Configure the client ID
        clientId = (String) properties.get(MQTT_CLIENT_ID_PROP_NAME);
        if (clientId == null || clientId.trim().length() == 0) {
            clientId = this.m_systemService.getPrimaryMacAddress();
        }
        ValidationUtil.notEmptyOrNull(clientId, "clientId");
        // replace invalid token in the client ID as it is used as part of
        // the topicname space
        clientId = clientId.replace('/', '-');
        clientId = clientId.replace('+', '-');
        clientId = clientId.replace('#', '-');
        clientId = clientId.replace('.', '-');
        // Configure the broker URL
        brokerUrl = (String) properties.get(MQTT_BROKER_URL_PROP_NAME);
        ValidationUtil.notEmptyOrNull(brokerUrl, MQTT_BROKER_URL_PROP_NAME);
        brokerUrl = brokerUrl.trim();
        brokerUrl = brokerUrl.replaceAll("^" + MQTT_SCHEME, "tcp://");
        brokerUrl = brokerUrl.replaceAll("^" + MQTTS_SCHEME, "ssl://");
        brokerUrl = brokerUrl.replaceAll("/$", "");
        ValidationUtil.notEmptyOrNull(brokerUrl, "brokerUrl");
        ValidationUtil.notNegative((Integer) properties.get(MQTT_KEEP_ALIVE_PROP_NAME), MQTT_KEEP_ALIVE_PROP_NAME);
        ValidationUtil.notNegative((Integer) properties.get(MQTT_TIMEOUT_PROP_NAME), MQTT_TIMEOUT_PROP_NAME);
        ValidationUtil.notNull(properties.get(MQTT_CLEAN_SESSION_PROP_NAME), MQTT_CLEAN_SESSION_PROP_NAME);
        String userName = (String) properties.get(MQTT_USERNAME_PROP_NAME);
        if (userName != null && !userName.isEmpty()) {
            conOpt.setUserName(userName);
        }
        Password password = (Password) properties.get(MQTT_PASSWORD_PROP_NAME);
        if (password != null && password.toString().length() != 0) {
            conOpt.setPassword(password.getPassword());
        }
        conOpt.setKeepAliveInterval((Integer) properties.get(MQTT_KEEP_ALIVE_PROP_NAME));
        conOpt.setConnectionTimeout((Integer) properties.get(MQTT_TIMEOUT_PROP_NAME));
        conOpt.setCleanSession((Boolean) properties.get(MQTT_CLEAN_SESSION_PROP_NAME));
        conOpt.setMqttVersion((Integer) properties.get(MQTT_DEFAULT_VERSION_PROP_NAME));
        synchronized (this.m_topicContext) {
            this.m_topicContext.clear();
            if (properties.get(CLOUD_ACCOUNT_NAME_PROP_NAME) != null) {
                this.m_topicContext.put(TOPIC_ACCOUNT_NAME_CTX_NAME, (String) properties.get(CLOUD_ACCOUNT_NAME_PROP_NAME));
            }
            this.m_topicContext.put(TOPIC_DEVICE_ID_CTX_NAME, clientId);
        }
        String willTopic = (String) properties.get(MQTT_LWT_TOPIC_PROP_NAME);
        if (!(willTopic == null || willTopic.isEmpty())) {
            int willQos = 0;
            boolean willRetain = false;
            String willPayload = (String) properties.get(MQTT_LWT_PAYLOAD_PROP_NAME);
            if (properties.get(MQTT_LWT_QOS_PROP_NAME) != null) {
                willQos = (Integer) properties.get(MQTT_LWT_QOS_PROP_NAME);
            }
            if (properties.get(MQTT_LWT_RETAIN_PROP_NAME) != null) {
                willRetain = (Boolean) properties.get(MQTT_LWT_RETAIN_PROP_NAME);
            }
            willTopic = replaceTopicVariables(willTopic);
            byte[] payload = {};
            if (willPayload != null && !willPayload.isEmpty()) {
                try {
                    payload = willPayload.getBytes("UTF-8");
                } catch (UnsupportedEncodingException e) {
                    s_logger.error("Unsupported encoding", e);
                }
            }
            conOpt.setWill(willTopic, payload, willQos, willRetain);
        }
    } catch (KuraException e) {
        s_logger.error("Invalid configuration");
        throw new IllegalStateException("Invalid MQTT client configuration", e);
    }
    // SSL
    if (brokerUrl.startsWith("ssl")) {
        try {
            String alias = (String) this.m_properties.get(SSL_CERT_ALIAS);
            if (alias == null || "".equals(alias.trim())) {
                alias = this.m_topicContext.get(TOPIC_ACCOUNT_NAME_CTX_NAME);
            }
            String protocol = (String) this.m_properties.get(SSL_PROTOCOL);
            String ciphers = (String) this.m_properties.get(SSL_CIPHERS);
            String hnVerification = (String) this.m_properties.get(SSL_HN_VERIFY);
            SSLSocketFactory ssf;
            if (SSL_DEFAULT_HN_VERIFY.equals(hnVerification)) {
                ssf = this.m_sslManagerService.getSSLSocketFactory(protocol, ciphers, null, null, null, alias);
            } else {
                ssf = this.m_sslManagerService.getSSLSocketFactory(protocol, ciphers, null, null, null, alias, Boolean.valueOf(hnVerification));
            }
            conOpt.setSocketFactory(ssf);
        } catch (Exception e) {
            s_logger.error("SSL setup failed", e);
            throw new IllegalStateException("SSL setup failed", e);
        }
    }
    String sType = (String) properties.get(PERSISTENCE_TYPE_PROP_NAME);
    PersistenceType persistenceType = null;
    if ("file".equals(sType)) {
        persistenceType = PersistenceType.FILE;
    } else if ("memory".equals(sType)) {
        persistenceType = PersistenceType.MEMORY;
    } else {
        throw new IllegalStateException("Invalid MQTT client configuration: persistenceType: " + persistenceType);
    }
    clientConfiguration = new MqttClientConfiguration(brokerUrl, clientId, persistenceType, conOpt);
    return clientConfiguration;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) PersistenceType(org.eclipse.kura.core.data.transport.mqtt.MqttClientConfiguration.PersistenceType) KuraNotConnectedException(org.eclipse.kura.KuraNotConnectedException) KuraTimeoutException(org.eclipse.kura.KuraTimeoutException) KuraException(org.eclipse.kura.KuraException) KuraTooManyInflightMessagesException(org.eclipse.kura.KuraTooManyInflightMessagesException) KuraConnectException(org.eclipse.kura.KuraConnectException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttPersistenceException(org.eclipse.paho.client.mqttv3.MqttPersistenceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) KuraException(org.eclipse.kura.KuraException) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Password(org.eclipse.kura.configuration.Password)

Example 19 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class MqttDataTransport method activate.

// ----------------------------------------------------------------
// 
// Activation APIs
// 
// ----------------------------------------------------------------
protected void activate(ComponentContext componentContext, Map<String, Object> properties) {
    s_logger.info("Activating {}...", properties.get(ConfigurationService.KURA_SERVICE_PID));
    // We need to catch the configuration exception and activate anyway.
    // Otherwise the ConfigurationService will not be able to track us.
    HashMap<String, Object> decryptedPropertiesMap = new HashMap<String, Object>();
    for (Map.Entry<String, Object> entry : properties.entrySet()) {
        String key = entry.getKey();
        Object value = entry.getValue();
        if (key.equals(MQTT_PASSWORD_PROP_NAME)) {
            try {
                Password decryptedPassword = new Password(this.m_cryptoService.decryptAes(((String) value).toCharArray()));
                decryptedPropertiesMap.put(key, decryptedPassword);
            } catch (Exception e) {
                s_logger.info("Password is not encrypted");
                decryptedPropertiesMap.put(key, new Password((String) value));
            }
        } else {
            decryptedPropertiesMap.put(key, value);
        }
    }
    this.m_properties.putAll(decryptedPropertiesMap);
    try {
        this.m_clientConf = buildConfiguration(this.m_properties);
        setupMqttSession();
    } catch (RuntimeException e) {
        s_logger.error("Invalid client configuration. Service will not be able to connect until the configuration is updated", e);
    }
    this.m_dataTransportListeners = new DataTransportListenerS(componentContext);
// Do nothing waiting for the connect request from the upper layer.
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) KuraNotConnectedException(org.eclipse.kura.KuraNotConnectedException) KuraTimeoutException(org.eclipse.kura.KuraTimeoutException) KuraException(org.eclipse.kura.KuraException) KuraTooManyInflightMessagesException(org.eclipse.kura.KuraTooManyInflightMessagesException) KuraConnectException(org.eclipse.kura.KuraConnectException) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttPersistenceException(org.eclipse.paho.client.mqttv3.MqttPersistenceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Password(org.eclipse.kura.configuration.Password)

Example 20 with Password

use of org.eclipse.kura.configuration.Password in project kura by eclipse.

the class NetworkConfiguration method getWifiConfig.

private static WifiConfig getWifiConfig(String netIfConfigPrefix, WifiMode mode, Map<String, Object> properties) throws KuraException {
    String key;
    WifiConfig wifiConfig = new WifiConfig();
    StringBuilder prefix = new StringBuilder(netIfConfigPrefix).append("wifi.").append(mode.toString().toLowerCase());
    // mode
    s_logger.trace("mode is {}", mode.toString());
    wifiConfig.setMode(mode);
    // ssid
    key = prefix + ".ssid";
    String ssid = (String) properties.get(key);
    if (ssid == null) {
        ssid = "";
    }
    s_logger.trace("SSID is {}", ssid);
    wifiConfig.setSSID(ssid);
    // driver
    key = prefix + ".driver";
    String driver = (String) properties.get(key);
    if (driver == null) {
        driver = "";
    }
    s_logger.trace("driver is {}", driver);
    wifiConfig.setDriver(driver);
    // security
    key = prefix + ".securityType";
    WifiSecurity wifiSecurity = WifiSecurity.NONE;
    String securityString = (String) properties.get(key);
    s_logger.trace("securityString is {}", securityString);
    if (securityString != null && !securityString.isEmpty()) {
        try {
            wifiSecurity = WifiSecurity.valueOf(securityString);
        } catch (IllegalArgumentException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "Could not parse wifi security " + securityString);
        }
    }
    wifiConfig.setSecurity(wifiSecurity);
    // channels
    key = prefix + ".channel";
    String channelsString = (String) properties.get(key);
    s_logger.trace("channelsString is {}", channelsString);
    if (channelsString != null) {
        channelsString = channelsString.trim();
        if (channelsString.length() > 0) {
            StringTokenizer st = new StringTokenizer(channelsString, " ");
            int tokens = st.countTokens();
            if (tokens > 0) {
                int[] channels = new int[tokens];
                for (int i = 0; i < tokens; i++) {
                    String token = st.nextToken();
                    try {
                        channels[i] = Integer.parseInt(token);
                    } catch (Exception e) {
                        s_logger.error("Error parsing channels!", e);
                    }
                }
                wifiConfig.setChannels(channels);
            }
        }
    }
    // passphrase
    key = prefix + ".passphrase";
    Object psswdObj = properties.get(key);
    Password psswd = null;
    if (psswdObj instanceof Password) {
        psswd = (Password) psswdObj;
    } else if (psswdObj instanceof String) {
        char[] tempPsswd = ((String) psswdObj).toCharArray();
        psswd = new Password(tempPsswd);
    }
    String passphrase = new String(psswd.getPassword());
    s_logger.trace("passphrase is {}", passphrase);
    wifiConfig.setPasskey(passphrase);
    // hardware mode
    key = prefix + ".hardwareMode";
    String hwMode = (String) properties.get(key);
    if (hwMode == null) {
        hwMode = "";
    }
    s_logger.trace("hwMode is {}", hwMode);
    wifiConfig.setHardwareMode(hwMode);
    // ignore SSID
    key = prefix + ".ignoreSSID";
    boolean ignoreSSID = false;
    if (properties.get(key) != null) {
        ignoreSSID = (Boolean) properties.get(key);
        s_logger.trace("Ignore SSID is {}", ignoreSSID);
    } else {
        s_logger.trace("Ignore SSID is null");
    }
    wifiConfig.setIgnoreSSID(ignoreSSID);
    key = prefix + ".pairwiseCiphers";
    String pairwiseCiphers = (String) properties.get(key);
    if (pairwiseCiphers != null) {
        wifiConfig.setPairwiseCiphers(WifiCiphers.valueOf(pairwiseCiphers));
    }
    if (mode == WifiMode.INFRA) {
        key = prefix + ".bgscan";
        String bgscan = (String) properties.get(key);
        if (bgscan == null) {
            bgscan = "";
        }
        s_logger.trace("bgscan is {}", bgscan);
        wifiConfig.setBgscan(new WifiBgscan(bgscan));
        /*
             * key = prefix + ".pairwiseCiphers";
             * String pairwiseCiphers = (String)properties.get(key);
             * if (pairwiseCiphers != null) {
             * wifiConfig.setPairwiseCiphers(WifiCiphers.valueOf(pairwiseCiphers));
             * }
             */
        key = prefix + ".groupCiphers";
        String groupCiphers = (String) properties.get(key);
        if (groupCiphers != null) {
            wifiConfig.setGroupCiphers(WifiCiphers.valueOf(groupCiphers));
        }
        // ping access point?
        key = prefix + ".pingAccessPoint";
        boolean pingAccessPoint = false;
        if (properties.get(key) != null) {
            pingAccessPoint = (Boolean) properties.get(key);
            s_logger.trace("Ping Access Point is {}", pingAccessPoint);
        } else {
            s_logger.trace("Ping Access Point is null");
        }
        wifiConfig.setPingAccessPoint(pingAccessPoint);
    }
    // broadcast
    key = prefix + ".broadcast";
    Boolean broadcast = (Boolean) properties.get(key);
    if (broadcast != null) {
        wifiConfig.setBroadcast(broadcast);
    }
    s_logger.trace("hwMode is {}", hwMode);
    // radio mode
    key = prefix + ".radioMode";
    WifiRadioMode radioMode;
    String radioModeString = (String) properties.get(key);
    s_logger.trace("radioModeString is {}", radioModeString);
    if (radioModeString != null && !radioModeString.isEmpty()) {
        try {
            radioMode = WifiRadioMode.valueOf(radioModeString);
            wifiConfig.setRadioMode(radioMode);
        } catch (IllegalArgumentException e) {
            throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "Could not parse wifi radio mode " + radioModeString);
        }
    }
    if (!wifiConfig.isValid()) {
        return null;
    } else {
        s_logger.trace("Returning wifiConfig: {}", wifiConfig);
        return wifiConfig;
    }
}
Also used : WifiConfig(org.eclipse.kura.net.wifi.WifiConfig) WifiAccessPoint(org.eclipse.kura.net.wifi.WifiAccessPoint) KuraException(org.eclipse.kura.KuraException) UnknownHostException(java.net.UnknownHostException) StringTokenizer(java.util.StringTokenizer) WifiRadioMode(org.eclipse.kura.net.wifi.WifiRadioMode) KuraException(org.eclipse.kura.KuraException) WifiSecurity(org.eclipse.kura.net.wifi.WifiSecurity) WifiBgscan(org.eclipse.kura.net.wifi.WifiBgscan) Password(org.eclipse.kura.configuration.Password)

Aggregations

Password (org.eclipse.kura.configuration.Password)20 KuraException (org.eclipse.kura.KuraException)12 HashMap (java.util.HashMap)9 IOException (java.io.IOException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 KuraConnectException (org.eclipse.kura.KuraConnectException)3 KuraNotConnectedException (org.eclipse.kura.KuraNotConnectedException)3 KuraTimeoutException (org.eclipse.kura.KuraTimeoutException)3 KuraTooManyInflightMessagesException (org.eclipse.kura.KuraTooManyInflightMessagesException)3 CryptoService (org.eclipse.kura.crypto.CryptoService)3 FileNotFoundException (java.io.FileNotFoundException)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 ConfigurationService (org.eclipse.kura.configuration.ConfigurationService)2 WifiMode (org.eclipse.kura.net.wifi.WifiMode)2 GwtKuraException (org.eclipse.kura.web.shared.GwtKuraException)2 GwtConfigParameterType (org.eclipse.kura.web.shared.model.GwtConfigParameter.GwtConfigParameterType)2 MqttException (org.eclipse.paho.client.mqttv3.MqttException)2 MqttPersistenceException (org.eclipse.paho.client.mqttv3.MqttPersistenceException)2