NIFI-11912 Added Proxy support to StandardOauth2AccessTokenProvider

This closes #7609

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Nandor Soma Abonyi 2023-08-05 01:33:04 +02:00 committed by exceptionfactory
parent fb502cdf9a
commit e9b62f7b83
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
2 changed files with 29 additions and 1 deletions

View File

@ -35,6 +35,11 @@
<artifactId>nifi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-proxy-configuration-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-utils</artifactId>

View File

@ -42,12 +42,15 @@ import org.apache.nifi.expression.ExpressionLanguageScope;
import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.util.StandardValidators;
import org.apache.nifi.proxy.ProxyConfiguration;
import org.apache.nifi.proxy.ProxySpec;
import org.apache.nifi.ssl.SSLContextService;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.Proxy;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
@ -205,6 +208,8 @@ public class StandardOauth2AccessTokenProvider extends AbstractControllerService
.dependsOn(SSL_CONTEXT)
.build();
private static final ProxySpec[] PROXY_SPECS = { ProxySpec.HTTP_AUTH };
private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList(
AUTHORIZATION_SERVER_URL,
CLIENT_AUTHENTICATION_STRATEGY,
@ -219,7 +224,8 @@ public class StandardOauth2AccessTokenProvider extends AbstractControllerService
AUDIENCE,
REFRESH_WINDOW,
SSL_CONTEXT,
HTTP_PROTOCOL_STRATEGY
HTTP_PROTOCOL_STRATEGY,
ProxyConfiguration.createProxyConfigPropertyDescriptor(false, PROXY_SPECS)
));
private static final String AUTHORIZATION_HEADER = "Authorization";
@ -302,6 +308,8 @@ public class StandardOauth2AccessTokenProvider extends AbstractControllerService
.build());
}
ProxyConfiguration.validateProxySpec(validationContext, validationResults, PROXY_SPECS);
return validationResults;
}
@ -315,6 +323,21 @@ public class StandardOauth2AccessTokenProvider extends AbstractControllerService
clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager);
}
final ProxyConfiguration proxyConfig = ProxyConfiguration.getConfiguration(context);
final Proxy proxy = proxyConfig.createProxy();
if (!Proxy.Type.DIRECT.equals(proxy.type())) {
clientBuilder.proxy(proxy);
if (proxyConfig.hasCredential()) {
clientBuilder.proxyAuthenticator((route, response) -> {
final String credential = Credentials.basic(proxyConfig.getProxyUserName(), proxyConfig.getProxyUserPassword());
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
});
}
}
final HttpProtocolStrategy httpProtocolStrategy = HttpProtocolStrategy.valueOf(context.getProperty(HTTP_PROTOCOL_STRATEGY).getValue());
clientBuilder.protocols(httpProtocolStrategy.getProtocols());