exceptionfactory commented on a change in pull request #5319: URL: https://github.com/apache/nifi/pull/5319#discussion_r700546896
########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); + acquireAccessDetails(); + } + } + } + + return accessDetails; + } + + private void acquireAccessDetails() { + getLogger().debug("Getting a new access token."); + + FormBody.Builder acquireTokenBuilder = new FormBody.Builder(); + + if (grantType.equals(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "password") + .add("username", username) + .add("password", password); + } else if (grantType.equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "client_credentials"); + } + + if (clientId != null) { + acquireTokenBuilder.add("client_id", clientId); + acquireTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody acquireTokenRequestBody = acquireTokenBuilder.build(); + + Request acquireTokenRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(acquireTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(acquireTokenRequest); + } + + private void refreshAccessDetails() { + getLogger().debug("Refreshing access token."); + + FormBody.Builder refreshTokenBuilder = new FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", this.accessDetails.getRefreshToken()); + + if (clientId != null) { + refreshTokenBuilder.add("client_id", clientId); + refreshTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody refreshTokenRequestBody = refreshTokenBuilder.build(); + + Request refreshRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(refreshTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(refreshRequest); + } + + private AccessToken getAccessDetails(Request newRequest) { + try { + Response response = httpClient.newCall(newRequest).execute(); + String responseBody = response.body().string(); Review comment: Access to the ResponseBody should also be wrapped in a try-with-resources according to the OkHttp documentation. ```suggestion try (final ResponseBody responseBody : response.body()) { final String responseBodyContent = responseBody.string(); } ``` ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); + acquireAccessDetails(); + } + } + } + + return accessDetails; + } + + private void acquireAccessDetails() { + getLogger().debug("Getting a new access token."); + + FormBody.Builder acquireTokenBuilder = new FormBody.Builder(); + + if (grantType.equals(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "password") + .add("username", username) + .add("password", password); + } else if (grantType.equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "client_credentials"); + } + + if (clientId != null) { + acquireTokenBuilder.add("client_id", clientId); + acquireTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody acquireTokenRequestBody = acquireTokenBuilder.build(); + + Request acquireTokenRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(acquireTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(acquireTokenRequest); + } + + private void refreshAccessDetails() { + getLogger().debug("Refreshing access token."); + + FormBody.Builder refreshTokenBuilder = new FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", this.accessDetails.getRefreshToken()); + + if (clientId != null) { + refreshTokenBuilder.add("client_id", clientId); + refreshTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody refreshTokenRequestBody = refreshTokenBuilder.build(); + + Request refreshRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(refreshTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(refreshRequest); + } + + private AccessToken getAccessDetails(Request newRequest) { Review comment: ```suggestion private AccessToken getAccessDetails(final Request newRequest) { ``` ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); + acquireAccessDetails(); + } + } + } + + return accessDetails; + } + + private void acquireAccessDetails() { + getLogger().debug("Getting a new access token."); + + FormBody.Builder acquireTokenBuilder = new FormBody.Builder(); + + if (grantType.equals(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "password") + .add("username", username) + .add("password", password); + } else if (grantType.equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "client_credentials"); + } + + if (clientId != null) { + acquireTokenBuilder.add("client_id", clientId); + acquireTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody acquireTokenRequestBody = acquireTokenBuilder.build(); + + Request acquireTokenRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(acquireTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(acquireTokenRequest); + } + + private void refreshAccessDetails() { + getLogger().debug("Refreshing access token."); + + FormBody.Builder refreshTokenBuilder = new FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", this.accessDetails.getRefreshToken()); + + if (clientId != null) { + refreshTokenBuilder.add("client_id", clientId); + refreshTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody refreshTokenRequestBody = refreshTokenBuilder.build(); + + Request refreshRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(refreshTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(refreshRequest); + } + + private AccessToken getAccessDetails(Request newRequest) { + try { + Response response = httpClient.newCall(newRequest).execute(); Review comment: This is also an issue on the current service implementation, but this call should be wrapped in a try-with-resources since `Response` implements `Closeable` as described in the [OkHttp Response documentation](https://square.github.io/okhttp/4.x/okhttp/okhttp3/-response-body/). ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); Review comment: If the refresh request throws an Exception, the object still retains a reference to the expired Access Token. If the subsequent call to `acquireAccessDetails()` also throws an Exception, it looks like the class will retain a reference to a refreshToken that is potentially invalid, does that sounds possible? One option would be setting `accessDetails = null` in this catch block, which would cause subsequent calls to `getAccessDetails()` to call `acquireAccessDetails()` instead of going down this path again. ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); + acquireAccessDetails(); + } + } + } + + return accessDetails; + } + + private void acquireAccessDetails() { + getLogger().debug("Getting a new access token."); + + FormBody.Builder acquireTokenBuilder = new FormBody.Builder(); + + if (grantType.equals(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "password") + .add("username", username) + .add("password", password); + } else if (grantType.equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "client_credentials"); + } + + if (clientId != null) { + acquireTokenBuilder.add("client_id", clientId); + acquireTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody acquireTokenRequestBody = acquireTokenBuilder.build(); + + Request acquireTokenRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(acquireTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(acquireTokenRequest); + } + + private void refreshAccessDetails() { + getLogger().debug("Refreshing access token."); + + FormBody.Builder refreshTokenBuilder = new FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", this.accessDetails.getRefreshToken()); + + if (clientId != null) { + refreshTokenBuilder.add("client_id", clientId); + refreshTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody refreshTokenRequestBody = refreshTokenBuilder.build(); + + Request refreshRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(refreshTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(refreshRequest); + } + + private AccessToken getAccessDetails(Request newRequest) { + try { + Response response = httpClient.newCall(newRequest).execute(); + String responseBody = response.body().string(); + if (response.code() != 200) { Review comment: This could be simplified to `!response.isSuccessful()`. ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); + acquireAccessDetails(); + } + } + } + + return accessDetails; + } + + private void acquireAccessDetails() { + getLogger().debug("Getting a new access token."); Review comment: Although logging is not consistent across the application, recommend removing the period `.` character from the message: ```suggestion getLogger().debug("Getting a new access token"); ``` ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); + acquireAccessDetails(); + } + } + } + + return accessDetails; + } + + private void acquireAccessDetails() { + getLogger().debug("Getting a new access token."); + + FormBody.Builder acquireTokenBuilder = new FormBody.Builder(); + + if (grantType.equals(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "password") + .add("username", username) + .add("password", password); + } else if (grantType.equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "client_credentials"); + } + + if (clientId != null) { + acquireTokenBuilder.add("client_id", clientId); + acquireTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody acquireTokenRequestBody = acquireTokenBuilder.build(); + + Request acquireTokenRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(acquireTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(acquireTokenRequest); + } + + private void refreshAccessDetails() { + getLogger().debug("Refreshing access token."); + + FormBody.Builder refreshTokenBuilder = new FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", this.accessDetails.getRefreshToken()); + + if (clientId != null) { + refreshTokenBuilder.add("client_id", clientId); + refreshTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody refreshTokenRequestBody = refreshTokenBuilder.build(); + + Request refreshRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(refreshTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(refreshRequest); + } + + private AccessToken getAccessDetails(Request newRequest) { + try { + Response response = httpClient.newCall(newRequest).execute(); + String responseBody = response.body().string(); + if (response.code() != 200) { + getLogger().error(String.format("Bad response from the server during oauth2 request:\n%s", responseBody)); + throw new ProcessException(String.format("Got HTTP %d during oauth2 request.", response.code())); Review comment: The newline character is platform-specific, recommend the following adjustments: ```suggestion getLogger().error("OAuth2 Request Failed [HTTP {}] {}", response.code(), responseBody); throw new ProcessException(String.format("OAuth2 Request Failed [HTTP %d]", response.code())); ``` ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); + acquireAccessDetails(); + } + } + } + + return accessDetails; + } + + private void acquireAccessDetails() { Review comment: For clarity, what do you think about renaming this to `getNewAccessDetails()`? ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); + acquireAccessDetails(); + } + } + } + + return accessDetails; + } + + private void acquireAccessDetails() { + getLogger().debug("Getting a new access token."); + + FormBody.Builder acquireTokenBuilder = new FormBody.Builder(); + + if (grantType.equals(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "password") Review comment: It would be helpful to define static strings for reused parameter names such as `String GRANT_TYPE = "grant_type"`. ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); Review comment: If this generalized exception handling and retry remains, should this be logged as warning? ```suggestion getLogger().warn("Refresh Access Token Failed", e); ``` ########## File path: nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java ########## @@ -0,0 +1,300 @@ +/* + * 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.oauth2; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import okhttp3.FormBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.nifi.annotation.documentation.CapabilityDescription; +import org.apache.nifi.annotation.documentation.Tags; +import org.apache.nifi.annotation.lifecycle.OnDisabled; +import org.apache.nifi.annotation.lifecycle.OnEnabled; +import org.apache.nifi.components.AllowableValue; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.components.Validator; +import org.apache.nifi.controller.AbstractControllerService; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.expression.ExpressionLanguageScope; +import org.apache.nifi.processor.exception.ProcessException; +import org.apache.nifi.processor.util.StandardValidators; +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.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Tags({"oauth2", "provider", "authorization", "access token", "http"}) +@CapabilityDescription("Provides OAuth 2.0 access tokens that can be used as Bearer authorization header in HTTP requests." + + " Uses Resource Owner Password Credentials Grant.") +public class StandardOauth2AccessTokenProvider extends AbstractControllerService implements OAuth2AccessTokenProvider { + public static final PropertyDescriptor AUTHORIZATION_SERVER_URL = new PropertyDescriptor.Builder() + .name("authorization-server-url") + .displayName("Authorization Server URL") + .description("The URL of the authorization server that issues access tokens.") + .required(true) + .addValidator(StandardValidators.URL_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static AllowableValue RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "password", + "User Password", + "Resource Owner Password Credentials Grant. Used to access resources available to users. Requires username and password and usually Client ID and Client Secret" + ); + public static AllowableValue CLIENT_CREDENTIALS_GRANT_TYPE = new AllowableValue( + "client_credentials", + "Client Credentials", + "Client Credentials Grant. Used to access resources available to clients. Requires Client ID and Client Secret" + ); + + public static final PropertyDescriptor GRANT_TYPE = new PropertyDescriptor.Builder() + .name("grant-type") + .displayName("Grant Type") + .description("The OAuth2 Grant Type to be used when acquiring an access token.") + .required(true) + .allowableValues(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE, CLIENT_CREDENTIALS_GRANT_TYPE) + .defaultValue(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue()) + .build(); + + public static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() + .name("service-user-name") + .displayName("Username") + .description("Username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() + .name("service-password") + .displayName("Password") + .description("Password for the username on the service that is being accessed.") + .dependsOn(GRANT_TYPE, RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor CLIENT_ID = new PropertyDescriptor.Builder() + .name("client-id") + .displayName("Client ID") + .required(false) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .expressionLanguageSupported(ExpressionLanguageScope.VARIABLE_REGISTRY) + .build(); + + public static final PropertyDescriptor CLIENT_SECRET = new PropertyDescriptor.Builder() + .name("client-secret") + .displayName("Client secret") + .dependsOn(CLIENT_ID) + .required(true) + .sensitive(true) + .addValidator(StandardValidators.NON_BLANK_VALIDATOR) + .build(); + + public static final PropertyDescriptor SSL_CONTEXT = new PropertyDescriptor.Builder() + .name("ssl-context") + .displayName("SSL Context") + .addValidator(Validator.VALID) + .identifiesControllerService(SSLContextService.class) + .required(false) + .build(); + + private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList( + AUTHORIZATION_SERVER_URL, + GRANT_TYPE, + USERNAME, + PASSWORD, + CLIENT_ID, + CLIENT_SECRET, + SSL_CONTEXT + )); + + public static final ObjectMapper ACCESS_DETAILS_MAPPER = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) + .setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + private volatile String authorizationServerUrl; + private volatile OkHttpClient httpClient; + + private volatile String grantType; + private volatile String username; + private volatile String password; + private volatile String clientId; + private volatile String clientSecret; + + private volatile AccessToken accessDetails; + + @Override + public List<PropertyDescriptor> getSupportedPropertyDescriptors() { + return PROPERTIES; + } + + @OnEnabled + public void onEnabled(ConfigurationContext context) { + authorizationServerUrl = context.getProperty(AUTHORIZATION_SERVER_URL).evaluateAttributeExpressions().getValue(); + + httpClient = createHttpClient(context); + + grantType = context.getProperty(GRANT_TYPE).getValue(); + username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(); + password = context.getProperty(PASSWORD).getValue(); + clientId = context.getProperty(CLIENT_ID).evaluateAttributeExpressions().getValue(); + clientSecret = context.getProperty(CLIENT_SECRET).getValue(); + } + + @OnDisabled + public void onDisabled() { + accessDetails = null; + } + + @Override + protected Collection<ValidationResult> customValidate(ValidationContext validationContext) { + final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(validationContext)); + + if ( + validationContext.getProperty(GRANT_TYPE).getValue().equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue()) + && !validationContext.getProperty(CLIENT_ID).isSet() + ) { + validationResults.add(new ValidationResult.Builder().subject(CLIENT_ID.getDisplayName()) + .valid(false) + .explanation(String.format( + "When '%s' is set to '%s', '%s' is required", + GRANT_TYPE.getDisplayName(), + CLIENT_CREDENTIALS_GRANT_TYPE.getDisplayName(), + CLIENT_ID.getDisplayName()) + ) + .build()); + } + + return validationResults; + } + + protected OkHttpClient createHttpClient(ConfigurationContext context) { + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); + + SSLContextService sslService = context.getProperty(SSL_CONTEXT).asControllerService(SSLContextService.class); + if (sslService != null) { + final X509TrustManager trustManager = sslService.createTrustManager(); + SSLContext sslContext = sslService.createContext(); + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustManager); + } + + return clientBuilder.build(); + } + + @Override + public AccessToken getAccessDetails() { + if (this.accessDetails == null) { + acquireAccessDetails(); + } else if (this.accessDetails.isExpired()) { + if (this.accessDetails.getRefreshToken() == null) { + acquireAccessDetails(); + } else { + try { + refreshAccessDetails(); + } catch (Exception e) { + getLogger().info("Couldn't refresh access token.", e); + acquireAccessDetails(); + } + } + } + + return accessDetails; + } + + private void acquireAccessDetails() { + getLogger().debug("Getting a new access token."); + + FormBody.Builder acquireTokenBuilder = new FormBody.Builder(); + + if (grantType.equals(RESOURCE_OWNER_PASSWORD_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "password") + .add("username", username) + .add("password", password); + } else if (grantType.equals(CLIENT_CREDENTIALS_GRANT_TYPE.getValue())) { + acquireTokenBuilder.add("grant_type", "client_credentials"); + } + + if (clientId != null) { + acquireTokenBuilder.add("client_id", clientId); + acquireTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody acquireTokenRequestBody = acquireTokenBuilder.build(); + + Request acquireTokenRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(acquireTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(acquireTokenRequest); + } + + private void refreshAccessDetails() { + getLogger().debug("Refreshing access token."); + + FormBody.Builder refreshTokenBuilder = new FormBody.Builder() + .add("grant_type", "refresh_token") + .add("refresh_token", this.accessDetails.getRefreshToken()); + + if (clientId != null) { + refreshTokenBuilder.add("client_id", clientId); + refreshTokenBuilder.add("client_secret", clientSecret); + } + + RequestBody refreshTokenRequestBody = refreshTokenBuilder.build(); + + Request refreshRequest = new Request.Builder() + .url(authorizationServerUrl) + .post(refreshTokenRequestBody) + .build(); + + this.accessDetails = getAccessDetails(refreshRequest); + } + + private AccessToken getAccessDetails(Request newRequest) { + try { + Response response = httpClient.newCall(newRequest).execute(); + String responseBody = response.body().string(); + if (response.code() != 200) { + getLogger().error(String.format("Bad response from the server during oauth2 request:\n%s", responseBody)); + throw new ProcessException(String.format("Got HTTP %d during oauth2 request.", response.code())); + } + + AccessToken accessDetails = ACCESS_DETAILS_MAPPER.readValue(responseBody, AccessToken.class); + + return accessDetails; + } catch (IOException e) { + throw new UncheckedIOException(e); Review comment: Recommend including a message with this exception: ```suggestion throw new UncheckedIOException("OAuth2 Request Failed [Connection Problem]", e); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org