Search in sources :

Example 11 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema in project nifi-minifi by apache.

the class ConfigTransformerTest method testConfigFileTransform.

public void testConfigFileTransform(String configFile) throws Exception {
    ConfigSchema configSchema = SchemaLoader.loadConfigSchemaFromYaml(ConfigTransformerTest.class.getClassLoader().getResourceAsStream(configFile));
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ConfigTransformer.writeFlowXmlFile(configSchema, outputStream);
    Document document = documentBuilder.parse(new ByteArrayInputStream(outputStream.toByteArray()));
    testProcessGroup((Element) xPathFactory.newXPath().evaluate("flowController/rootGroup", document, XPathConstants.NODE), configSchema.getProcessGroupSchema());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema)

Example 12 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema 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)

Example 13 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema in project nifi-minifi by apache.

the class AbstractTestSecure method testUser1.

@Test
public void testUser1() throws Exception {
    SSLContext sslContext = loadSslContext("user1");
    assertReturnCode("", sslContext, 403);
    ConfigSchema configSchema = assertReturnCode("?class=raspi2", sslContext, 200);
    assertEquals("raspi2.v1", configSchema.getFlowControllerProperties().getName());
    assertReturnCode("?class=raspi3", sslContext, 403);
}
Also used : SSLContext(javax.net.ssl.SSLContext) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema) Test(org.junit.Test)

Example 14 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema in project nifi-minifi by apache.

the class AbstractTestUnsecure method testCurrentVersion.

@Test
public void testCurrentVersion() throws IOException, SchemaLoaderException {
    ConfigSchema configSchema = getConfigSchema(c2Url + "?class=raspi3");
    assertEquals(3, configSchema.getVersion());
    assertEquals("raspi3.v2", configSchema.getFlowControllerProperties().getName());
}
Also used : ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema) Test(org.junit.Test)

Example 15 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema in project nifi-minifi by apache.

the class AbstractTestUnsecure method getConfigSchema.

public ConfigSchema getConfigSchema(String urlString) throws IOException, SchemaLoaderException {
    HttpURLConnection urlConnection = openSuperUserUrlConnection(urlString);
    ConfigSchema configSchema;
    try (InputStream inputStream = urlConnection.getInputStream()) {
        configSchema = SchemaLoader.loadConfigSchemaFromYaml(inputStream);
    } finally {
        urlConnection.disconnect();
    }
    return configSchema;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema)

Aggregations

ConfigSchema (org.apache.nifi.minifi.commons.schema.ConfigSchema)31 Test (org.junit.Test)20 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 IOException (java.io.IOException)5 FileInputStream (java.io.FileInputStream)4 List (java.util.List)4 JAXBException (javax.xml.bind.JAXBException)4 FileNotFoundException (java.io.FileNotFoundException)3 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 ConfigSchemaTest (org.apache.nifi.minifi.commons.schema.ConfigSchemaTest)3 ConnectionSchema (org.apache.nifi.minifi.commons.schema.ConnectionSchema)3 HttpURLConnection (java.net.HttpURLConnection)2 ByteBuffer (java.nio.ByteBuffer)2 HashSet (java.util.HashSet)2