Skip to content

Commit

Permalink
NIFI-12782 Migrated GCS processors' Proxy properties to ProxyConfigur…
Browse files Browse the repository at this point in the history
…ationService

Extracted proxy service migration code into a common util module because the same logic was already used in AWS module,
and it is also reusable in other components for proxy property migration.

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes apache#8400.
  • Loading branch information
turcsanyip authored and pvillard31 committed Mar 19, 2024
1 parent d9bcc8b commit d370b47
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-proxy-configuration-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-migration-utils</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.nifi.expression.ExpressionLanguageScope;
import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.migration.PropertyConfiguration;
import org.apache.nifi.migration.ProxyServiceMigration;
import org.apache.nifi.processor.AbstractProcessor;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.Relationship;
Expand All @@ -49,9 +50,7 @@

import javax.net.ssl.SSLContext;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -69,7 +68,6 @@
public abstract class AbstractAWSCredentialsProviderProcessor<ClientType extends AmazonWebServiceClient> extends AbstractProcessor implements VerifiableProcessor {

private static final String CREDENTIALS_SERVICE_CLASSNAME = "org.apache.nifi.processors.aws.credentials.provider.service.AWSCredentialsProviderControllerService";
private static final String PROXY_SERVICE_CLASSNAME = "org.apache.nifi.proxy.StandardProxyConfigurationService";

// Obsolete property names
private static final String OBSOLETE_ACCESS_KEY = "Access Key";
Expand All @@ -85,11 +83,6 @@ public abstract class AbstractAWSCredentialsProviderProcessor<ClientType extends
private static final String AUTH_SERVICE_SECRET_KEY = "Secret Key";
private static final String AUTH_SERVICE_CREDENTIALS_FILE = "Credentials File";
private static final String AUTH_SERVICE_ANONYMOUS_CREDENTIALS = "anonymous-credentials";
private static final String PROXY_SERVICE_HOST = "proxy-server-host";
private static final String PROXY_SERVICE_PORT = "proxy-server-port";
private static final String PROXY_SERVICE_USERNAME = "proxy-user-name";
private static final String PROXY_SERVICE_PASSWORD = "proxy-user-password";
private static final String PROXY_SERVICE_TYPE = "proxy-type";


// Property Descriptors
Expand Down Expand Up @@ -173,7 +166,7 @@ public void onStopped() {
@Override
public void migrateProperties(final PropertyConfiguration config) {
migrateAuthenticationProperties(config);
migrateProxyProperties(config);
ProxyServiceMigration.migrateProxyProperties(config, PROXY_CONFIGURATION_SERVICE, OBSOLETE_PROXY_HOST, OBSOLETE_PROXY_PORT, OBSOLETE_PROXY_USERNAME, OBSOLETE_PROXY_PASSWORD);
}

private void migrateAuthenticationProperties(final PropertyConfiguration config) {
Expand Down Expand Up @@ -201,27 +194,6 @@ private void migrateAuthenticationProperties(final PropertyConfiguration config)
config.removeProperty(OBSOLETE_CREDENTIALS_FILE);
}

private void migrateProxyProperties(final PropertyConfiguration config) {
if (config.isPropertySet(OBSOLETE_PROXY_HOST)) {
final Map<String, String> proxyProperties = new HashMap<>();
proxyProperties.put(PROXY_SERVICE_TYPE, Type.HTTP.name());
proxyProperties.put(PROXY_SERVICE_HOST, config.getRawPropertyValue(OBSOLETE_PROXY_HOST).get());

// Map any optional proxy configs
config.getRawPropertyValue(OBSOLETE_PROXY_PORT).ifPresent(value -> proxyProperties.put(PROXY_SERVICE_PORT, value));
config.getRawPropertyValue(OBSOLETE_PROXY_USERNAME).ifPresent(value -> proxyProperties.put(PROXY_SERVICE_USERNAME, value));
config.getRawPropertyValue(OBSOLETE_PROXY_PASSWORD).ifPresent(value -> proxyProperties.put(PROXY_SERVICE_PASSWORD, value));

final String serviceId = config.createControllerService(PROXY_SERVICE_CLASSNAME, proxyProperties);
config.setProperty(PROXY_CONFIGURATION_SERVICE, serviceId);
}

config.removeProperty(OBSOLETE_PROXY_HOST);
config.removeProperty(OBSOLETE_PROXY_PORT);
config.removeProperty(OBSOLETE_PROXY_USERNAME);
config.removeProperty(OBSOLETE_PROXY_PASSWORD);
}

protected ClientConfiguration createConfiguration(final ProcessContext context) {
return createConfiguration(context, context.getMaxConcurrentTasks());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.nifi.expression.ExpressionLanguageScope;
import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.migration.PropertyConfiguration;
import org.apache.nifi.migration.ProxyServiceMigration;
import org.apache.nifi.processor.AbstractSessionFactoryProcessor;
import org.apache.nifi.processor.ProcessContext;
import org.apache.nifi.processor.ProcessSession;
Expand All @@ -51,12 +52,10 @@

import javax.net.ssl.TrustManager;
import java.net.Proxy;
import java.net.Proxy.Type;
import java.net.URI;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -71,7 +70,6 @@
*/
public abstract class AbstractAwsProcessor<T extends SdkClient> extends AbstractSessionFactoryProcessor implements VerifiableProcessor {
private static final String CREDENTIALS_SERVICE_CLASSNAME = "org.apache.nifi.processors.aws.credentials.provider.service.AWSCredentialsProviderControllerService";
private static final String PROXY_SERVICE_CLASSNAME = "org.apache.nifi.proxy.StandardProxyConfigurationService";

// Obsolete property names
private static final String OBSOLETE_ACCESS_KEY = "Access Key";
Expand All @@ -87,11 +85,6 @@ public abstract class AbstractAwsProcessor<T extends SdkClient> extends Abstract
private static final String AUTH_SERVICE_SECRET_KEY = "Secret Key";
private static final String AUTH_SERVICE_CREDENTIALS_FILE = "Credentials File";
private static final String AUTH_SERVICE_ANONYMOUS_CREDENTIALS = "anonymous-credentials";
private static final String PROXY_SERVICE_HOST = "proxy-server-host";
private static final String PROXY_SERVICE_PORT = "proxy-server-port";
private static final String PROXY_SERVICE_USERNAME = "proxy-user-name";
private static final String PROXY_SERVICE_PASSWORD = "proxy-user-password";
private static final String PROXY_SERVICE_TYPE = "proxy-type";


public static final Relationship REL_SUCCESS = new Relationship.Builder()
Expand Down Expand Up @@ -196,7 +189,7 @@ public Set<Relationship> getRelationships() {
@Override
public void migrateProperties(final PropertyConfiguration config) {
migrateAuthenticationProperties(config);
migrateProxyProperties(config);
ProxyServiceMigration.migrateProxyProperties(config, PROXY_CONFIGURATION_SERVICE, OBSOLETE_PROXY_HOST, OBSOLETE_PROXY_PORT, OBSOLETE_PROXY_USERNAME, OBSOLETE_PROXY_PASSWORD);
}

private void migrateAuthenticationProperties(final PropertyConfiguration config) {
Expand Down Expand Up @@ -224,27 +217,6 @@ private void migrateAuthenticationProperties(final PropertyConfiguration config)
config.removeProperty(OBSOLETE_CREDENTIALS_FILE);
}

private void migrateProxyProperties(final PropertyConfiguration config) {
if (config.isPropertySet(OBSOLETE_PROXY_HOST)) {
final Map<String, String> proxyProperties = new HashMap<>();
proxyProperties.put(PROXY_SERVICE_TYPE, Type.HTTP.name());
proxyProperties.put(PROXY_SERVICE_HOST, config.getRawPropertyValue(OBSOLETE_PROXY_HOST).get());

// Map any optional proxy configs
config.getRawPropertyValue(OBSOLETE_PROXY_PORT).ifPresent(value -> proxyProperties.put(PROXY_SERVICE_PORT, value));
config.getRawPropertyValue(OBSOLETE_PROXY_USERNAME).ifPresent(value -> proxyProperties.put(PROXY_SERVICE_USERNAME, value));
config.getRawPropertyValue(OBSOLETE_PROXY_PASSWORD).ifPresent(value -> proxyProperties.put(PROXY_SERVICE_PASSWORD, value));

final String serviceId = config.createControllerService(PROXY_SERVICE_CLASSNAME, proxyProperties);
config.setProperty(PROXY_CONFIGURATION_SERVICE, serviceId);
}

config.removeProperty(OBSOLETE_PROXY_HOST);
config.removeProperty(OBSOLETE_PROXY_PORT);
config.removeProperty(OBSOLETE_PROXY_USERNAME);
config.removeProperty(OBSOLETE_PROXY_PASSWORD);
}

@OnScheduled
public void onScheduled(final ProcessContext context) {
getClient(context);
Expand Down
39 changes: 39 additions & 0 deletions nifi-nar-bundles/nifi-extension-utils/nifi-migration-utils/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-extension-utils</artifactId>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>nifi-migration-utils</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-mock</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.migration;

import org.apache.nifi.components.PropertyDescriptor;

import java.net.Proxy;
import java.util.HashMap;
import java.util.Map;

public final class ProxyServiceMigration {

static final String PROXY_SERVICE_CLASSNAME = "org.apache.nifi.proxy.StandardProxyConfigurationService";

static final String PROXY_SERVICE_TYPE = "proxy-type";
static final String PROXY_SERVICE_HOST = "proxy-server-host";
static final String PROXY_SERVICE_PORT = "proxy-server-port";
static final String PROXY_SERVICE_USERNAME = "proxy-user-name";
static final String PROXY_SERVICE_PASSWORD = "proxy-user-password";

private ProxyServiceMigration() {}

/**
* Migrates component level proxy properties to ProxyConfigurationService.
*
* @param config the component's property config to be migrated
* @param proxyServiceProperty the component's property descriptor referencing ProxyConfigurationService
* @param proxyHostProperty the name of the component level Proxy Host property
* @param proxyPortProperty the name of the component level Proxy Port property
* @param proxyUsernameProperty the name of the component level Proxy Username property
* @param proxyPasswordProperty the name of the component level Proxy Password property
*/
public static void migrateProxyProperties(final PropertyConfiguration config, final PropertyDescriptor proxyServiceProperty,
final String proxyHostProperty, final String proxyPortProperty,
final String proxyUsernameProperty, final String proxyPasswordProperty) {
if (config.isPropertySet(proxyHostProperty)) {
final Map<String, String> proxyProperties = new HashMap<>();
proxyProperties.put(PROXY_SERVICE_TYPE, Proxy.Type.HTTP.name());
proxyProperties.put(PROXY_SERVICE_HOST, config.getRawPropertyValue(proxyHostProperty).get());

// Map any optional proxy configs
config.getRawPropertyValue(proxyPortProperty).ifPresent(value -> proxyProperties.put(PROXY_SERVICE_PORT, value));
config.getRawPropertyValue(proxyUsernameProperty).ifPresent(value -> proxyProperties.put(PROXY_SERVICE_USERNAME, value));
config.getRawPropertyValue(proxyPasswordProperty).ifPresent(value -> proxyProperties.put(PROXY_SERVICE_PASSWORD, value));

final String serviceId = config.createControllerService(PROXY_SERVICE_CLASSNAME, proxyProperties);
config.setProperty(proxyServiceProperty, serviceId);
}

config.removeProperty(proxyHostProperty);
config.removeProperty(proxyPortProperty);
config.removeProperty(proxyUsernameProperty);
config.removeProperty(proxyPasswordProperty);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.migration;

import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.util.MockPropertyConfiguration;
import org.apache.nifi.util.MockPropertyConfiguration.CreatedControllerService;
import org.apache.nifi.util.PropertyMigrationResult;
import org.junit.jupiter.api.Test;

import java.net.Proxy;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ProxyServiceMigrationTest {

private static final PropertyDescriptor PROXY_SERVICE = new PropertyDescriptor.Builder()
.name("proxy-service")
.build();

private static final String OBSOLETE_PROXY_HOST = "proxy-host";
private static final String OBSOLETE_PROXY_PORT = "proxy-port";
private static final String OBSOLETE_PROXY_USERNAME = "proxy-username";
private static final String OBSOLETE_PROXY_PASSWORD = "proxy-password";

private static final String PROXY_HOST_VALUE = "localhost";
private static final String PROXY_PORT_VALUE = "8888";
private static final String PROXY_USERNAME_VALUE = "user";
private static final String PROXY_PASSWORD_VALUE = "pass";

@Test
void testMigrateProxyProperties() {
final Map<String, String> properties = Map.of(
OBSOLETE_PROXY_HOST, PROXY_HOST_VALUE,
OBSOLETE_PROXY_PORT, PROXY_PORT_VALUE,
OBSOLETE_PROXY_USERNAME, PROXY_USERNAME_VALUE,
OBSOLETE_PROXY_PASSWORD, PROXY_PASSWORD_VALUE
);
final MockPropertyConfiguration config = new MockPropertyConfiguration(properties);

ProxyServiceMigration.migrateProxyProperties(config, PROXY_SERVICE, OBSOLETE_PROXY_HOST, OBSOLETE_PROXY_PORT, OBSOLETE_PROXY_USERNAME, OBSOLETE_PROXY_PASSWORD);

assertFalse(config.hasProperty(OBSOLETE_PROXY_HOST));
assertFalse(config.hasProperty(OBSOLETE_PROXY_PORT));
assertFalse(config.hasProperty(OBSOLETE_PROXY_USERNAME));
assertFalse(config.hasProperty(OBSOLETE_PROXY_PASSWORD));

assertTrue(config.isPropertySet(PROXY_SERVICE));

PropertyMigrationResult result = config.toPropertyMigrationResult();
assertEquals(1, result.getCreatedControllerServices().size());

final CreatedControllerService createdService = result.getCreatedControllerServices().iterator().next();

assertEquals(config.getRawPropertyValue(PROXY_SERVICE).get(), createdService.id());
assertEquals(ProxyServiceMigration.PROXY_SERVICE_CLASSNAME, createdService.implementationClassName());

assertEquals(Map.of(
ProxyServiceMigration.PROXY_SERVICE_TYPE, Proxy.Type.HTTP.name(),
ProxyServiceMigration.PROXY_SERVICE_HOST, PROXY_HOST_VALUE,
ProxyServiceMigration.PROXY_SERVICE_PORT, PROXY_PORT_VALUE,
ProxyServiceMigration.PROXY_SERVICE_USERNAME, PROXY_USERNAME_VALUE,
ProxyServiceMigration.PROXY_SERVICE_PASSWORD, PROXY_PASSWORD_VALUE
),
createdService.serviceProperties());
}
}
5 changes: 3 additions & 2 deletions nifi-nar-bundles/nifi-extension-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<modules>
<module>nifi-bin-manager</module>
<module>nifi-conflict-resolution</module>
<module>nifi-database-utils</module>
<module>nifi-database-test-utils</module>
<module>nifi-dbcp-base</module>
Expand All @@ -38,14 +39,14 @@
<module>nifi-hadoop-utils</module>
<module>nifi-kerberos-test-utils</module>
<module>nifi-listed-entity</module>
<module>nifi-migration-utils</module>
<module>nifi-prometheus-utils</module>
<module>nifi-put-pattern</module>
<module>nifi-record-path-property</module>
<module>nifi-record-utils</module>
<module>nifi-reporting-utils</module>
<module>nifi-resource-transfer</module>
<module>nifi-service-utils</module>
<module>nifi-syslog-utils</module>
<module>nifi-conflict-resolution</module>
<module>nifi-record-path-property</module>
</modules>
</project>
5 changes: 5 additions & 0 deletions nifi-nar-bundles/nifi-gcp-bundle/nifi-gcp-processors/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@
<artifactId>nifi-conflict-resolution</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-migration-utils</artifactId>
<version>2.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-mock</artifactId>
Expand Down
Loading

0 comments on commit d370b47

Please sign in to comment.