Skip to content

Commit

Permalink
NIFI-14008 Add verify implementation to JsonConfigBasedBoxClientServi…
Browse files Browse the repository at this point in the history
…ce and provide enhanced error message when box connection fails.

Signed-off-by: Pierre Villard <[email protected]>

This closes apache#9521.
  • Loading branch information
Alex Ethier authored and pvillard31 committed Nov 14, 2024
1 parent 31e4c37 commit 588e84e
Showing 1 changed file with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.apache.nifi.box.controllerservices;

import com.box.sdk.BoxAPIConnection;
import com.box.sdk.BoxAPIException;
import com.box.sdk.BoxAPIResponseException;
import com.box.sdk.BoxConfig;
import com.box.sdk.BoxDeveloperEditionAPIConnection;
import java.io.FileNotFoundException;
Expand All @@ -25,31 +27,36 @@
import java.io.Reader;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.nifi.annotation.documentation.CapabilityDescription;
import org.apache.nifi.annotation.documentation.Tags;
import org.apache.nifi.annotation.lifecycle.OnEnabled;
import org.apache.nifi.components.ConfigVerificationResult;
import org.apache.nifi.components.PropertyDescriptor;
import org.apache.nifi.components.ValidationContext;
import org.apache.nifi.components.ValidationResult;
import org.apache.nifi.components.resource.ResourceCardinality;
import org.apache.nifi.components.resource.ResourceType;
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.controller.ConfigurationContext;
import org.apache.nifi.controller.VerifiableControllerService;
import org.apache.nifi.expression.ExpressionLanguageScope;
import org.apache.nifi.logging.ComponentLog;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.processor.util.JsonValidator;
import org.apache.nifi.processor.util.StandardValidators;
import org.apache.nifi.proxy.ProxyConfiguration;
import org.apache.nifi.proxy.ProxySpec;

import static org.apache.nifi.components.ConfigVerificationResult.Outcome.FAILED;
import static org.apache.nifi.components.ConfigVerificationResult.Outcome.SUCCESSFUL;


@CapabilityDescription("Provides Box client objects through which Box API calls can be used.")
@Tags({"box", "client", "provider"})
public class JsonConfigBasedBoxClientService extends AbstractControllerService implements BoxClientService {
public class JsonConfigBasedBoxClientService extends AbstractControllerService implements BoxClientService, VerifiableControllerService {
public static final PropertyDescriptor ACCOUNT_ID = new PropertyDescriptor.Builder()
.name("box-account-id")
.displayName("Account ID")
Expand Down Expand Up @@ -80,12 +87,12 @@ public class JsonConfigBasedBoxClientService extends AbstractControllerService i

private static final ProxySpec[] PROXY_SPECS = {ProxySpec.HTTP, ProxySpec.HTTP_AUTH};

private static final List<PropertyDescriptor> PROPERTIES = Collections.unmodifiableList(Arrays.asList(
private static final List<PropertyDescriptor> PROPERTIES = List.of(
ACCOUNT_ID,
APP_CONFIG_FILE,
APP_CONFIG_JSON,
ProxyConfiguration.createProxyConfigPropertyDescriptor(PROXY_SPECS)
));
);

private volatile BoxAPIConnection boxAPIConnection;

Expand All @@ -94,6 +101,33 @@ public final List<PropertyDescriptor> getSupportedPropertyDescriptors() {
return PROPERTIES;
}

@Override
public List<ConfigVerificationResult> verify(final ConfigurationContext configurationContext, final ComponentLog componentLog, final Map<String, String> map) {

final List<ConfigVerificationResult> results = new ArrayList<>();
try {
createBoxApiConnection(configurationContext);
results.add(
new ConfigVerificationResult.Builder()
.verificationStepName("Authentication")
.outcome(SUCCESSFUL)
.explanation("JSON App config verified")
.build()
);
} catch (final Exception e) {
results.add(
new ConfigVerificationResult.Builder()
.verificationStepName("Authentication")
.outcome(FAILED)
.explanation("JSON App config failed to verify: " + e.getMessage())
.build()
);
}


return results;
}

@OnEnabled
public void onEnabled(final ConfigurationContext context) {
boxAPIConnection = createBoxApiConnection(context);
Expand Down Expand Up @@ -134,7 +168,6 @@ public BoxAPIConnection getBoxApiConnection() {
}

private BoxAPIConnection createBoxApiConnection(ConfigurationContext context) {
final BoxAPIConnection api;

final String accountId = context.getProperty(ACCOUNT_ID).evaluateAttributeExpressions().getValue();
final ProxyConfiguration proxyConfiguration = ProxyConfiguration.getConfiguration(context);
Expand All @@ -156,7 +189,16 @@ private BoxAPIConnection createBoxApiConnection(ConfigurationContext context) {
boxConfig = BoxConfig.readFrom(appConfig);
}

api = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig);
final BoxAPIConnection api;
try {
api = BoxDeveloperEditionAPIConnection.getAppEnterpriseConnection(boxConfig);
} catch (final BoxAPIResponseException e) {
if (boxConfig.getEnterpriseId().equals("0")) {
throw new BoxAPIException("Box API integration is not enabled for account, the account's enterprise ID cannot be 0", e);
} else {
throw e;
}
}

api.asUser(accountId);

Expand Down

0 comments on commit 588e84e

Please sign in to comment.