Search in sources :

Example 1 with MessageOutputConfigurationException

use of org.graylog2.plugin.outputs.MessageOutputConfigurationException in project graylog2-server by Graylog2.

the class MessageOutputFactoryTest method testNonExistentOutputType.

@Test(expected = IllegalArgumentException.class)
public void testNonExistentOutputType() throws MessageOutputConfigurationException {
    final String outputType = "non.existent";
    final Output output = mock(Output.class);
    when(output.getType()).thenReturn(outputType);
    final Stream stream = mock(Stream.class);
    final Configuration configuration = mock(Configuration.class);
    messageOutputFactory.fromStreamOutput(output, stream, configuration);
}
Also used : Configuration(org.graylog2.plugin.configuration.Configuration) MessageOutput(org.graylog2.plugin.outputs.MessageOutput) Output(org.graylog2.plugin.streams.Output) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 2 with MessageOutputConfigurationException

use of org.graylog2.plugin.outputs.MessageOutputConfigurationException in project graylog2-server by Graylog2.

the class OutputRegistryTest method testInvalidOutputConfiguration.

@Test
public void testInvalidOutputConfiguration() throws Exception {
    final String outputId = "foobar";
    final Stream stream = mock(Stream.class);
    when(messageOutputFactory.fromStreamOutput(eq(output), any(Stream.class), any(Configuration.class))).thenThrow(new MessageOutputConfigurationException());
    when(outputService.load(eq(outputId))).thenReturn(output);
    final OutputRegistry outputRegistry = new OutputRegistry(null, outputService, messageOutputFactory, null, null, FAULT_COUNT_THRESHOLD, FAULT_PENALTY_SECONDS);
    assertEquals(outputRegistry.getRunningMessageOutputs().size(), 0);
    MessageOutput result = outputRegistry.getOutputForIdAndStream(outputId, stream);
    assertNull(result);
    assertEquals(outputRegistry.getRunningMessageOutputs().size(), 0);
}
Also used : MessageOutput(org.graylog2.plugin.outputs.MessageOutput) Configuration(org.graylog2.plugin.configuration.Configuration) MessageOutputConfigurationException(org.graylog2.plugin.outputs.MessageOutputConfigurationException) Stream(org.graylog2.plugin.streams.Stream) Test(org.junit.Test)

Example 3 with MessageOutputConfigurationException

use of org.graylog2.plugin.outputs.MessageOutputConfigurationException in project graylog2-server by Graylog2.

the class GelfOutput method buildTransport.

protected static GelfTransport buildTransport(final Configuration configuration) throws MessageOutputConfigurationException {
    final String protocol = configuration.getString(CK_PROTOCOL);
    final String hostname = configuration.getString(CK_HOSTNAME);
    final int port = configuration.getInt(CK_PORT);
    final int connectTimeout = configuration.getInt(CK_CONNECT_TIMEOUT, 1000);
    final int reconnectDelay = configuration.getInt(CK_RECONNECT_DELAY, 500);
    final boolean tcpKeepAlive = configuration.getBoolean(CK_TCP_KEEP_ALIVE, false);
    final boolean tcpNoDelay = configuration.getBoolean(CK_TCP_NO_DELAY, false);
    final boolean tlsVerificationEnabled = configuration.getBoolean(CK_TLS_VERIFICATION_ENABLED, false);
    final String tlsTrustCertChain = configuration.getString(CK_TLS_TRUST_CERT_CHAIN);
    final int queueSize = configuration.getInt(CK_QUEUE_SIZE, 512);
    final int maxInflightSends = configuration.getInt(CK_MAX_INFLIGHT_SENDS, 512);
    if (isNullOrEmpty(protocol) || isNullOrEmpty(hostname) || !configuration.intIsSet(CK_PORT)) {
        throw new MessageOutputConfigurationException("Protocol and/or hostname missing!");
    }
    final GelfTransports transport;
    final boolean tlsEnabled;
    switch(protocol.toUpperCase(Locale.ENGLISH)) {
        case "UDP":
            transport = GelfTransports.UDP;
            tlsEnabled = false;
            break;
        case "TCP":
            transport = GelfTransports.TCP;
            tlsEnabled = false;
            break;
        case "TCP+TLS":
            transport = GelfTransports.TCP;
            tlsEnabled = true;
            break;
        default:
            throw new MessageOutputConfigurationException("Unknown protocol " + protocol);
    }
    final File tlsTrustCertChainFile;
    if (tlsEnabled && !isNullOrEmpty(tlsTrustCertChain)) {
        tlsTrustCertChainFile = new File(tlsTrustCertChain);
        if (!tlsTrustCertChainFile.isFile() && !tlsTrustCertChainFile.canRead()) {
            throw new MessageOutputConfigurationException("TLS trust certificate chain file cannot be read!");
        }
    } else {
        tlsTrustCertChainFile = null;
    }
    final GelfConfiguration gelfConfiguration = new GelfConfiguration(hostname, port).transport(transport).connectTimeout(connectTimeout).reconnectDelay(reconnectDelay).tcpKeepAlive(tcpKeepAlive).tcpNoDelay(tcpNoDelay).queueSize(queueSize).maxInflightSends(maxInflightSends);
    if (tlsEnabled) {
        gelfConfiguration.enableTls();
        if (tlsVerificationEnabled) {
            gelfConfiguration.enableTlsCertVerification();
        } else {
            gelfConfiguration.disableTlsCertVerification();
        }
        if (tlsTrustCertChainFile != null) {
            gelfConfiguration.tlsTrustCertChainFile(tlsTrustCertChainFile);
        }
    }
    LOG.debug("Initializing GELF sender and connecting to {}://{}:{}", protocol, hostname, port);
    try {
        return GelfTransports.create(gelfConfiguration);
    } catch (Exception e) {
        final String error = "Error initializing " + GelfOutput.class;
        LOG.error(error, e);
        throw new MessageOutputConfigurationException(error);
    }
}
Also used : GelfTransports(org.graylog2.gelfclient.GelfTransports) GelfConfiguration(org.graylog2.gelfclient.GelfConfiguration) MessageOutputConfigurationException(org.graylog2.plugin.outputs.MessageOutputConfigurationException) File(java.io.File) MessageOutputConfigurationException(org.graylog2.plugin.outputs.MessageOutputConfigurationException)

Aggregations

Configuration (org.graylog2.plugin.configuration.Configuration)2 MessageOutput (org.graylog2.plugin.outputs.MessageOutput)2 MessageOutputConfigurationException (org.graylog2.plugin.outputs.MessageOutputConfigurationException)2 Stream (org.graylog2.plugin.streams.Stream)2 Test (org.junit.Test)2 File (java.io.File)1 GelfConfiguration (org.graylog2.gelfclient.GelfConfiguration)1 GelfTransports (org.graylog2.gelfclient.GelfTransports)1 Output (org.graylog2.plugin.streams.Output)1