Search in sources :

Example 1 with ByteBufferInputStream

use of org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream in project nifi-minifi by apache.

the class PullHttpChangeIngestorCommonTest method testSecurityOverride.

@Test
public void testSecurityOverride() throws IOException, SchemaLoaderException {
    Properties properties = new Properties();
    properties.put(PullHttpChangeIngestor.OVERRIDE_SECURITY, "false");
    properties.put(RunMiNiFi.MINIFI_CONFIG_FILE_KEY, "src/test/resources/config.yml");
    properties.put(PATH_KEY, "/config-minimal.yml");
    pullHttpChangeIngestorInit(properties);
    when(mockDifferentiator.isNew(Mockito.any(ByteBuffer.class))).thenReturn(true);
    pullHttpChangeIngestor.run();
    ArgumentCaptor<ByteBuffer> argument = ArgumentCaptor.forClass(ByteBuffer.class);
    verify(testNotifier, Mockito.times(1)).notifyListeners(argument.capture());
    ConvertableSchema<ConfigSchema> configSchema = SchemaLoader.loadConvertableSchemaFromYaml(new ByteBufferInputStream(argument.getValue()));
    ConfigSchema newSchema = configSchema.convert();
    assertNotNull(newSchema.getSecurityProperties().getKeystore());
    assertEquals(newSchema.getProcessGroupSchema().getProcessors().size(), 2);
}
Also used : ByteBufferInputStream(org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream) Properties(java.util.Properties) ByteBuffer(java.nio.ByteBuffer) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema) Test(org.junit.Test)

Example 2 with ByteBufferInputStream

use of org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream in project nifi-minifi by apache.

the class ConfigurationChangeCoordinator method notifyListeners.

/**
 * Provide the mechanism by which listeners are notified
 */
public Collection<ListenerHandleResult> notifyListeners(ByteBuffer newConfig) {
    logger.info("Notifying Listeners of a change");
    Collection<ListenerHandleResult> listenerHandleResults = new ArrayList<>(configurationChangeListeners.size());
    for (final ConfigurationChangeListener listener : getChangeListeners()) {
        ListenerHandleResult result;
        try {
            listener.handleChange(new ByteBufferInputStream(newConfig.duplicate()));
            result = new ListenerHandleResult(listener);
        } catch (ConfigurationChangeException ex) {
            result = new ListenerHandleResult(listener, ex);
        }
        listenerHandleResults.add(result);
        logger.info("Listener notification result:" + result.toString());
    }
    return listenerHandleResults;
}
Also used : ArrayList(java.util.ArrayList) ByteBufferInputStream(org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream)

Example 3 with ByteBufferInputStream

use of org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream in project nifi-minifi by apache.

the class PullHttpChangeIngestor method run.

@Override
public void run() {
    try {
        logger.debug("Attempting to pull new config");
        HttpUrl.Builder builder = new HttpUrl.Builder().host(hostReference.get()).port(portReference.get()).encodedPath(pathReference.get());
        String query = queryReference.get();
        if (!StringUtil.isNullOrEmpty(query)) {
            builder = builder.encodedQuery(query);
        }
        final HttpUrl url = builder.scheme(connectionScheme).build();
        final Request.Builder requestBuilder = new Request.Builder().get().url(url);
        if (useEtag) {
            requestBuilder.addHeader("If-None-Match", lastEtag);
        }
        final Request request = requestBuilder.build();
        final OkHttpClient httpClient = httpClientReference.get();
        final Call call = httpClient.newCall(request);
        final Response response = call.execute();
        logger.debug("Response received: {}", response.toString());
        int code = response.code();
        if (code == NOT_MODIFIED_STATUS_CODE) {
            return;
        }
        if (code >= 400) {
            throw new IOException("Got response code " + code + " while trying to pull configuration: " + response.body().string());
        }
        ResponseBody body = response.body();
        if (body == null) {
            logger.warn("No body returned when pulling a new configuration");
            return;
        }
        ByteBuffer bodyByteBuffer = ByteBuffer.wrap(body.bytes());
        ByteBuffer readOnlyNewConfig = null;
        // checking if some parts of the configuration must be preserved
        if (overrideSecurity) {
            readOnlyNewConfig = bodyByteBuffer.asReadOnlyBuffer();
        } else {
            logger.debug("Preserving previous security properties...");
            // get the current security properties from the current configuration file
            final File configFile = new File(properties.get().getProperty(RunMiNiFi.MINIFI_CONFIG_FILE_KEY));
            ConvertableSchema<ConfigSchema> configSchema = SchemaLoader.loadConvertableSchemaFromYaml(new FileInputStream(configFile));
            ConfigSchema currentSchema = configSchema.convert();
            SecurityPropertiesSchema secProps = currentSchema.getSecurityProperties();
            // override the security properties in the pulled configuration with the previous properties
            configSchema = SchemaLoader.loadConvertableSchemaFromYaml(new ByteBufferInputStream(bodyByteBuffer.duplicate()));
            ConfigSchema newSchema = configSchema.convert();
            newSchema.setSecurityProperties(secProps);
            // return the updated configuration preserving the previous security configuration
            readOnlyNewConfig = ByteBuffer.wrap(new Yaml().dump(newSchema.toMap()).getBytes()).asReadOnlyBuffer();
        }
        if (differentiator.isNew(readOnlyNewConfig)) {
            logger.debug("New change received, notifying listener");
            configurationChangeNotifier.notifyListeners(readOnlyNewConfig);
            logger.debug("Listeners notified");
        } else {
            logger.debug("Pulled config same as currently running.");
        }
        if (useEtag) {
            lastEtag = (new StringBuilder("\"")).append(response.header("ETag").trim()).append("\"").toString();
        }
    } catch (Exception e) {
        logger.warn("Hit an exception while trying to pull", e);
    }
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) ByteBufferInputStream(org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HttpUrl(okhttp3.HttpUrl) FileInputStream(java.io.FileInputStream) Yaml(org.yaml.snakeyaml.Yaml) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) SecurityPropertiesSchema(org.apache.nifi.minifi.commons.schema.SecurityPropertiesSchema) File(java.io.File) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema)

Aggregations

ByteBufferInputStream (org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream)3 ByteBuffer (java.nio.ByteBuffer)2 ConfigSchema (org.apache.nifi.minifi.commons.schema.ConfigSchema)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Call (okhttp3.Call)1 HttpUrl (okhttp3.HttpUrl)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 SecurityPropertiesSchema (org.apache.nifi.minifi.commons.schema.SecurityPropertiesSchema)1 Test (org.junit.Test)1 Yaml (org.yaml.snakeyaml.Yaml)1