Search in sources :

Example 1 with TransportStrategy

use of org.simplejavamail.api.mailer.config.TransportStrategy in project nzyme by lennartkoopmann.

the class LeaderConfigurationLoader method validate.

private void validate() throws IncompleteConfigurationException, InvalidConfigurationException {
    // Completeness and type validity.
    ConfigurationValidator.expectEnum(general, ConfigurationKeys.ROLE, ConfigurationKeys.GENERAL, Role.class);
    ConfigurationValidator.expect(general, ConfigurationKeys.ADMIN_PASSWORD_HASH, ConfigurationKeys.GENERAL, String.class);
    ConfigurationValidator.expect(general, ConfigurationKeys.DATABASE_PATH, ConfigurationKeys.GENERAL, String.class);
    ConfigurationValidator.expect(general, ConfigurationKeys.VERSIONCHECKS, ConfigurationKeys.GENERAL, Boolean.class);
    ConfigurationValidator.expect(general, ConfigurationKeys.FETCH_OUIS, ConfigurationKeys.GENERAL, Boolean.class);
    ConfigurationValidator.expect(python, ConfigurationKeys.PYTHON_PATH, ConfigurationKeys.GENERAL + "." + ConfigurationKeys.PYTHON, String.class);
    ConfigurationValidator.expect(python, ConfigurationKeys.PYTHON_SCRIPT_DIR, ConfigurationKeys.GENERAL + "." + ConfigurationKeys.PYTHON, String.class);
    ConfigurationValidator.expect(python, ConfigurationKeys.PYTHON_SCRIPT_PREFIX, ConfigurationKeys.GENERAL + "." + ConfigurationKeys.PYTHON, String.class);
    ConfigurationValidator.expect(alerting, ConfigurationKeys.TRAINING_PERIOD_SECONDS, ConfigurationKeys.GENERAL + "." + ConfigurationKeys.ALERTING, Integer.class);
    ConfigurationValidator.expect(interfaces, ConfigurationKeys.REST_LISTEN_URI, ConfigurationKeys.INTERFACES, String.class);
    ConfigurationValidator.expect(interfaces, ConfigurationKeys.HTTP_EXTERNAL_URI, ConfigurationKeys.INTERFACES, String.class);
    ConfigurationValidator.expect(root, ConfigurationKeys.DOT11_MONITORS, "<root>", List.class);
    ConfigurationValidator.expect(root, ConfigurationKeys.DOT11_NETWORKS, "<root>", List.class);
    ConfigurationValidator.expect(root, ConfigurationKeys.DOT11_ALERTS, "<root>", List.class);
    ConfigurationValidator.expect(root, ConfigurationKeys.GROUNDSTATION_DEVICE, "<root>", Config.class);
    if (root.hasPath(ConfigurationKeys.UPLINKS)) {
        ConfigurationValidator.expect(root, ConfigurationKeys.UPLINKS, "<root>", List.class);
        int i = 0;
        for (Config x : root.getConfigList(ConfigurationKeys.UPLINKS)) {
            ConfigurationValidator.expect(x, ConfigurationKeys.TYPE, ConfigurationKeys.UPLINKS + ".#" + i, String.class);
            ConfigurationValidator.expect(x, ConfigurationKeys.CONFIGURATION, ConfigurationKeys.UPLINKS + ".#" + i, Config.class);
        }
    }
    if (root.hasPath(ConfigurationKeys.FORWARDERS)) {
        ConfigurationValidator.expect(root, ConfigurationKeys.FORWARDERS, "<root>", List.class);
        int i = 0;
        for (Config x : root.getConfigList(ConfigurationKeys.FORWARDERS)) {
            ConfigurationValidator.expect(x, ConfigurationKeys.TYPE, ConfigurationKeys.FORWARDERS + ".#" + i, String.class);
            ConfigurationValidator.expect(x, ConfigurationKeys.CONFIGURATION, ConfigurationKeys.FORWARDERS + ".#" + i, Config.class);
        }
    }
    if (root.hasPath(ConfigurationKeys.REMOTE_INPUT)) {
        Config remoteInput = root.getConfig(ConfigurationKeys.REMOTE_INPUT);
        ConfigurationValidator.expect(remoteInput, ConfigurationKeys.HOST, ConfigurationKeys.REMOTE_INPUT, String.class);
        ConfigurationValidator.expect(remoteInput, ConfigurationKeys.PORT, ConfigurationKeys.REMOTE_INPUT, Integer.class);
    }
    if (root.hasPath(ConfigurationKeys.GROUNDSTATION_DEVICE)) {
        Config groundstationDevice = root.getConfig(ConfigurationKeys.GROUNDSTATION_DEVICE);
        if (groundstationDevice.hasPath(ConfigurationKeys.TYPE)) {
            ConfigurationValidator.expect(groundstationDevice, ConfigurationKeys.TYPE, ConfigurationKeys.GROUNDSTATION_DEVICE, String.class);
            ConfigurationValidator.expect(groundstationDevice, ConfigurationKeys.PARAMETERS, ConfigurationKeys.GROUNDSTATION_DEVICE, Config.class);
            // Validate parameters of SX126X LoRa HAT.
            if (groundstationDevice.getString(ConfigurationKeys.TYPE).equals(TrackerDevice.TYPE.SX126X_LORA.toString())) {
                Config loraConfig = groundstationDevice.getConfig(ConfigurationKeys.PARAMETERS);
                // Serial port must exist.
                ConfigurationValidator.expect(loraConfig, ConfigurationKeys.SERIAL_PORT, ConfigurationKeys.GROUNDSTATION_DEVICE + "." + ConfigurationKeys.PARAMETERS, String.class);
                String serialPortPath = loraConfig.getString(ConfigurationKeys.SERIAL_PORT);
                if (!new File(serialPortPath).exists()) {
                    throw new InvalidConfigurationException("Parameter " + ConfigurationKeys.GROUNDSTATION_DEVICE + "." + ConfigurationKeys.PARAMETERS + "." + ConfigurationKeys.SERIAL_PORT + " does not point to an existing serial port path at [" + serialPortPath + "].");
                }
                // Encryption key must exist and be exactly 32 characters.
                ConfigurationValidator.expect(loraConfig, ConfigurationKeys.ENCRYPTION_KEY, ConfigurationKeys.GROUNDSTATION_DEVICE + "." + ConfigurationKeys.PARAMETERS, String.class);
                String encryptionKey = loraConfig.getString(ConfigurationKeys.ENCRYPTION_KEY);
                if (encryptionKey.length() != 32) {
                    throw new InvalidConfigurationException("Parameter " + ConfigurationKeys.GROUNDSTATION_DEVICE + "." + ConfigurationKeys.PARAMETERS + "." + ConfigurationKeys.ENCRYPTION_KEY + " must be exactly 32 characters long.");
                }
            }
        }
    }
    if (root.hasPath(ConfigurationKeys.REPORTING)) {
        Config reporting = root.getConfig(ConfigurationKeys.REPORTING);
        if (reporting.hasPath(ConfigurationKeys.EMAIL)) {
            Config email = reporting.getConfig(ConfigurationKeys.EMAIL);
            String where = "reporting.email";
            ConfigurationValidator.expect(email, ConfigurationKeys.TRANSPORT_STRATEGY, where, String.class);
            ConfigurationValidator.expect(email, ConfigurationKeys.HOST, where, String.class);
            ConfigurationValidator.expect(email, ConfigurationKeys.PORT, where, Integer.class);
            ConfigurationValidator.expect(email, ConfigurationKeys.USERNAME, where, String.class);
            ConfigurationValidator.expect(email, ConfigurationKeys.PASSWORD, where, String.class);
            ConfigurationValidator.expect(email, ConfigurationKeys.FROM, where, String.class);
            ConfigurationValidator.expect(email, ConfigurationKeys.SUBJECT_PREFIX, where, String.class);
            TransportStrategy transportStrategy;
            try {
                TransportStrategy.valueOf(email.getString(ConfigurationKeys.TRANSPORT_STRATEGY));
            } catch (IllegalArgumentException e) {
                throw new InvalidConfigurationException("Invalid reporting SMTP transport strategy.", e);
            }
            Tools.parseEmailAddress(email.getString(ConfigurationKeys.FROM));
        }
    }
    if (root.hasPath(ConfigurationKeys.DEAUTH_MONITOR)) {
        Config deauth = root.getConfig(ConfigurationKeys.DEAUTH_MONITOR);
        ConfigurationValidator.expect(deauth, ConfigurationKeys.GLOBAL_THRESHOLD, ConfigurationKeys.DEAUTH_MONITOR, Integer.class);
    }
    // Password hash is 64 characters long (the size of a SHA256 hash string)
    if (parseAdminPasswordHash().length() != 64) {
        throw new InvalidConfigurationException("Parameter [" + ConfigurationKeys.GENERAL + "." + ConfigurationKeys.ADMIN_PASSWORD_HASH + "] must be 64 characters long (a SHA256 hash).");
    }
    // Validate shared/base 802.11 config.
    baseDot11ConfigurationLoader.validate();
    // 802.11 Trap Pairs
    int i = 0;
    List<String> trapDevices = Lists.newArrayList();
    for (Config c : root.getConfigList(ConfigurationKeys.DOT11_TRAPS)) {
        String where = ConfigurationKeys.DOT11_TRAPS + ".#" + i;
        ConfigurationValidator.expect(c, ConfigurationKeys.DEVICE, where, String.class);
        ConfigurationValidator.expect(c, ConfigurationKeys.CHANNELS, where, List.class);
        ConfigurationValidator.expect(c, ConfigurationKeys.HOP_COMMAND, where, String.class);
        ConfigurationValidator.expect(c, ConfigurationKeys.HOP_INTERVAL, where, Integer.class);
        ConfigurationValidator.expect(c, ConfigurationKeys.TRAP, where, Config.class);
        String deviceName = c.getString(ConfigurationKeys.DEVICE);
        Config trap = c.getConfig(ConfigurationKeys.TRAP);
        // Make sure trap type exists and is set to an existing trap type.
        ConfigurationValidator.expect(trap, ConfigurationKeys.TYPE, where, String.class);
        String trapType = trap.getString(ConfigurationKeys.TYPE);
        try {
            Trap.Type.valueOf(trapType);
        } catch (IllegalArgumentException e) {
            throw new InvalidConfigurationException("Trap [" + where + "] is of invalid type [" + trapType + "].");
        }
        if (trapDevices.contains(deviceName)) {
            throw new InvalidConfigurationException("Trap [" + where + "] is using already configured device [" + deviceName + "]. Devices can only be used once.");
        }
        trapDevices.add(deviceName);
        for (Dot11MonitorDefinition monitor : baseDot11ConfigurationLoader.parseDot11Monitors()) {
            if (monitor.device().equals(deviceName)) {
                throw new InvalidConfigurationException("Trap [" + where + "] is using already configured monitor device [" + deviceName + "]. Devices can only be used once.");
            }
        }
    }
    // Python: executable is an executable file.
    if (!Files.isExecutable(new File(parsePythonExecutable()).toPath())) {
        throw new InvalidConfigurationException("Parameter [general.python." + ConfigurationKeys.PYTHON_PATH + "] does not point to an executable file: " + parsePythonExecutable());
    }
    // Python: script directory is a directory and writable.
    if (!Files.isDirectory(new File(parsePythonScriptDirectory()).toPath()) || !Files.isWritable(new File(parsePythonScriptDirectory()).toPath())) {
        throw new InvalidConfigurationException("Parameter [general.python." + ConfigurationKeys.PYTHON_SCRIPT_DIR + "] does not point to a writable directory: " + parsePythonScriptDirectory());
    }
    // REST listen URI can be parsed into a URI.
    try {
        parseRestListenUri();
    } catch (Exception e) {
        LOG.error(e);
        throw new InvalidConfigurationException("Parameter [interfaces." + ConfigurationKeys.REST_LISTEN_URI + "] cannot be parsed into a URI. Make sure it is correct.");
    }
    // HTTP external URI can be parsed into a URI.
    try {
        parseHttpExternalUri();
    } catch (Exception e) {
        LOG.error(e);
        throw new InvalidConfigurationException("Parameter [interfaces." + ConfigurationKeys.HTTP_EXTERNAL_URI + "] cannot be parsed into a URI. Make sure it is correct.");
    }
    // TLS, if TLS is enabled.
    if (parseUseTls()) {
        try {
            Path cert = parseTlsCertificatePath();
            if (!cert.toFile().canRead()) {
                throw new InvalidConfigurationException("Parameter [interfaces." + ConfigurationKeys.TLS_CERTIFICATE_PATH + "] points to a file that is not readable.");
            }
        } catch (Exception e) {
            LOG.error(e);
            throw new InvalidConfigurationException("Parameter [interfaces." + ConfigurationKeys.TLS_CERTIFICATE_PATH + "] cannot be parsed into a path. Make sure it is correct.");
        }
        try {
            Path key = parseTlsKeyPath();
            if (!key.toFile().canRead()) {
                throw new InvalidConfigurationException("Parameter [interfaces." + ConfigurationKeys.TLS_KEY_PATH + "] points to a file that is not readable.");
            }
        } catch (Exception e) {
            LOG.error(e);
            throw new InvalidConfigurationException("Parameter [interfaces." + ConfigurationKeys.TLS_KEY_PATH + "] cannot be parsed into a path. Make sure it is correct.");
        }
    }
}
Also used : Path(java.nio.file.Path) Config(com.typesafe.config.Config) TransportStrategy(org.simplejavamail.api.mailer.config.TransportStrategy) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ConfigException(com.typesafe.config.ConfigException)

Example 2 with TransportStrategy

use of org.simplejavamail.api.mailer.config.TransportStrategy in project nzyme by lennartkoopmann.

the class EmailCallback method parseConfiguration.

public static Configuration parseConfiguration(Config c, String httpExternalUri) throws InvalidConfigurationException, IncompleteConfigurationException {
    // Completeness.
    ConfigurationValidator.expect(c, ConfigurationKeys.TRANSPORT_STRATEGY, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.HOST, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.PORT, WHERE, Integer.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.USERNAME, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.PASSWORD, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.RECIPIENTS, WHERE, List.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.FROM, WHERE, String.class);
    ConfigurationValidator.expect(c, ConfigurationKeys.SUBJECT_PREFIX, WHERE, String.class);
    // Validity.
    // Transport strategy exists.
    TransportStrategy transportStrategy;
    try {
        transportStrategy = TransportStrategy.valueOf(c.getString(ConfigurationKeys.TRANSPORT_STRATEGY));
    } catch (IllegalArgumentException e) {
        throw new InvalidConfigurationException("Invalid SMTP transport strategy.", e);
    }
    // Recipients are valid.
    List<Recipient> recipients = Lists.newArrayList();
    for (String rec : c.getStringList(ConfigurationKeys.RECIPIENTS)) {
        recipients.add(Tools.parseEmailAddress(rec));
    }
    return Configuration.create(transportStrategy, c.getString(ConfigurationKeys.HOST), c.getInt(ConfigurationKeys.PORT), c.getString(ConfigurationKeys.USERNAME), c.getString(ConfigurationKeys.PASSWORD), recipients, // recipient type is ignored
    Tools.parseEmailAddress(c.getString(ConfigurationKeys.FROM)), c.getString(ConfigurationKeys.SUBJECT_PREFIX), httpExternalUri);
}
Also used : Recipient(org.simplejavamail.api.email.Recipient) TransportStrategy(org.simplejavamail.api.mailer.config.TransportStrategy) InvalidConfigurationException(horse.wtf.nzyme.configuration.InvalidConfigurationException)

Example 3 with TransportStrategy

use of org.simplejavamail.api.mailer.config.TransportStrategy in project simple-java-mail by bbottema.

the class ConfigLoaderTest method loadPropertiesFromObjectProperties.

@Test
public void loadPropertiesFromObjectProperties() {
    Properties source = new Properties();
    source.put("simplejavamail.javaxmail.debug", true);
    source.put("simplejavamail.transportstrategy", TransportStrategy.SMTPS);
    source.put("simplejavamail.smtp.host", "smtp.default.com");
    source.put("simplejavamail.smtp.port", 25);
    source.put("simplejavamail.smtp.username", "username");
    source.put("simplejavamail.smtp.password", "password");
    source.put("simplejavamail.custom.sslfactory.class", "teh_class");
    ConfigLoader.loadProperties(source, false);
    assertThat(ConfigLoader.<Boolean>getProperty(JAVAXMAIL_DEBUG)).isEqualTo(true);
    assertThat(ConfigLoader.<TransportStrategy>getProperty(TRANSPORT_STRATEGY)).isSameAs(SMTPS);
    assertThat(ConfigLoader.<String>getProperty(SMTP_HOST)).isEqualTo("smtp.default.com");
    assertThat(ConfigLoader.<Integer>getProperty(SMTP_PORT)).isEqualTo(25);
    assertThat(ConfigLoader.<String>getProperty(SMTP_USERNAME)).isEqualTo("username");
    assertThat(ConfigLoader.<String>getProperty(SMTP_PASSWORD)).isEqualTo("password");
    assertThat(ConfigLoader.<String>getProperty(CUSTOM_SSLFACTORY_CLASS)).isEqualTo("teh_class");
}
Also used : Properties(java.util.Properties) TransportStrategy(org.simplejavamail.api.mailer.config.TransportStrategy) Test(org.junit.Test)

Example 4 with TransportStrategy

use of org.simplejavamail.api.mailer.config.TransportStrategy in project simple-java-mail by bbottema.

the class ConfigLoaderTest method loadPropertiesFromProperties.

@Test
public void loadPropertiesFromProperties() {
    Properties source = new Properties();
    source.put("simplejavamail.javaxmail.debug", "true");
    source.put("simplejavamail.transportstrategy", "SMTPS");
    source.put("simplejavamail.smtp.host", "smtp.default.com");
    source.put("simplejavamail.smtp.port", "25");
    source.put("simplejavamail.smtp.username", "username");
    source.put("simplejavamail.smtp.password", "password");
    source.put("simplejavamail.custom.sslfactory.class", "teh_class");
    source.put("simplejavamail.extraproperties.a", "A");
    source.put("simplejavamail.extraproperties.b", "B");
    ConfigLoader.loadProperties(source, false);
    assertThat(ConfigLoader.<Boolean>getProperty(JAVAXMAIL_DEBUG)).isEqualTo(true);
    assertThat(ConfigLoader.<TransportStrategy>getProperty(TRANSPORT_STRATEGY)).isSameAs(SMTPS);
    assertThat(ConfigLoader.<String>getProperty(SMTP_HOST)).isEqualTo("smtp.default.com");
    assertThat(ConfigLoader.<Integer>getProperty(SMTP_PORT)).isEqualTo(25);
    assertThat(ConfigLoader.<String>getProperty(SMTP_USERNAME)).isEqualTo("username");
    assertThat(ConfigLoader.<String>getProperty(SMTP_PASSWORD)).isEqualTo("password");
    assertThat(ConfigLoader.<String>getProperty(CUSTOM_SSLFACTORY_CLASS)).isEqualTo("teh_class");
    assertThat(ConfigLoader.<Map<String, String>>getProperty(EXTRA_PROPERTIES)).containsExactly(new SimpleEntry<>("a", "A"), new SimpleEntry<>("b", "B"));
}
Also used : Properties(java.util.Properties) TransportStrategy(org.simplejavamail.api.mailer.config.TransportStrategy) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with TransportStrategy

use of org.simplejavamail.api.mailer.config.TransportStrategy in project simple-java-mail by bbottema.

the class SessionLogger method logSession.

/**
 * Simply logs host details, credentials used and whether authentication will take place and finally the transport protocol used.
 */
public static void logSession(final Session session, boolean async, final String activity) {
    final TransportStrategy transportStrategy = TransportStrategy.findStrategyForSession(session);
    final Properties properties = session.getProperties();
    final String sessionDetails = (transportStrategy != null) ? transportStrategy.toString(properties) : properties.toString();
    LOGGER.debug("starting{} {} with {}", async ? " async" : "", activity, sessionDetails);
}
Also used : TransportStrategy(org.simplejavamail.api.mailer.config.TransportStrategy) Properties(java.util.Properties)

Aggregations

TransportStrategy (org.simplejavamail.api.mailer.config.TransportStrategy)7 Test (org.junit.Test)4 Properties (java.util.Properties)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Config (com.typesafe.config.Config)1 ConfigException (com.typesafe.config.ConfigException)1 InvalidConfigurationException (horse.wtf.nzyme.configuration.InvalidConfigurationException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Recipient (org.simplejavamail.api.email.Recipient)1