sjyang18 commented on a change in pull request #4226:
URL: https://github.com/apache/nifi/pull/4226#discussion_r416993721



##########
File path: 
nifi-nar-bundles/nifi-azure-bundle/nifi-azure-processors/src/main/java/org/apache/nifi/processors/azure/eventhub/ConsumeAzureEventHub.java
##########
@@ -627,9 +616,26 @@ private void registerEventProcessor(final ProcessContext 
context) throws Excepti
 
         final String storageConnectionString = 
String.format(FORMAT_STORAGE_CONNECTION_STRING, storageAccountName, 
storageAccountKey);
 
-        final ConnectionStringBuilder eventHubConnectionString = new 
ConnectionStringBuilder().setNamespaceName(namespaceName).setEventHubName( 
eventHubName).setSasKeyName(sasName).setSasKey(sasKey);
-
-        eventProcessorHost = new EventProcessorHost(consumerHostname, 
eventHubName, consumerGroupName, eventHubConnectionString.toString(), 
storageConnectionString, containerName);
+        final String connectionString;
+        final boolean useManagedIdentity = 
context.getProperty(USE_MANANGED_IDENTITY).asBoolean();
+        if(useManagedIdentity) {
+            connectionString = 
AzureEventHubUtils.getManagedIdentityConnectionString(namespaceName, 
eventHubName);
+        } else {
+            final String sasName = 
context.getProperty(ACCESS_POLICY_NAME).evaluateAttributeExpressions().getValue();
+            validateRequiredProperty(ACCESS_POLICY_NAME, sasName);
+            final String sasKey = 
context.getProperty(POLICY_PRIMARY_KEY).evaluateAttributeExpressions().getValue();
+            validateRequiredProperty(POLICY_PRIMARY_KEY, sasKey);
+            connectionString = new ConnectionStringBuilder()

Review comment:
       Done.

##########
File path: 
nifi-nar-bundles/nifi-azure-bundle/nifi-azure-processors/src/main/java/org/apache/nifi/processors/azure/eventhub/utils/AzureEventHubUtils.java
##########
@@ -0,0 +1,81 @@
+/*
+ * 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.processors.azure.eventhub.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.microsoft.azure.eventhubs.ConnectionStringBuilder;
+
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.components.ValidationContext;
+import org.apache.nifi.components.ValidationResult;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.processor.util.StandardValidators;
+
+public final class AzureEventHubUtils {
+
+    public static final String MANANGED_IDENDITY_POLICY = 
ConnectionStringBuilder.MANAGED_IDENTITY_AUTHENTICATION;
+
+    public static final PropertyDescriptor POLICY_PRIMARY_KEY = new 
PropertyDescriptor.Builder()
+        .name("Shared Access Policy Primary Key")
+        .description("The primary key of the shared access policy")
+        .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+        .expressionLanguageSupported(ExpressionLanguageScope.NONE)
+        .sensitive(true)
+        .required(false)
+        .build();
+
+    public static final PropertyDescriptor USE_MANANGED_IDENTITY = new 
PropertyDescriptor.Builder()
+        .name("use-managed-identity")
+        .displayName("Use Azure Managed Identity")
+        .description("Choose whether or not to use the managed identity of 
azure vm/vmss. ")
+        .required(false).defaultValue("false").allowableValues("true", "false")
+        .addValidator(StandardValidators.BOOLEAN_VALIDATOR).build();
+
+    public static List<ValidationResult> customValidate(PropertyDescriptor 
accessPolicyDescriptor, ValidationContext context) {
+        List<ValidationResult> retVal = new ArrayList<>();
+
+        boolean accessPolicyIsSet  = 
context.getProperty(accessPolicyDescriptor).isSet();
+        boolean policyKeyIsSet     = 
context.getProperty(POLICY_PRIMARY_KEY).isSet();
+        boolean useManagedIdentity = 
context.getProperty(USE_MANANGED_IDENTITY).asBoolean();
+
+        if (useManagedIdentity && (accessPolicyIsSet || policyKeyIsSet) ) {
+            final String msg = String.format(
+                "%s and %s with %s fields cannot be set at the same time.",
+                USE_MANANGED_IDENTITY.getDisplayName(),
+                accessPolicyDescriptor.getDisplayName(),
+                POLICY_PRIMARY_KEY.getDisplayName()
+            );
+            retVal.add(new 
ValidationResult.Builder().valid(false).explanation(msg).build());
+        } else if (!useManagedIdentity && (!accessPolicyIsSet || 
!policyKeyIsSet)) {
+            final String msg = String.format(
+                "Either %s or %s with %s must be set",

Review comment:
       done

##########
File path: 
nifi-nar-bundles/nifi-azure-bundle/nifi-azure-processors/src/main/java/org/apache/nifi/processors/azure/eventhub/GetAzureEventHub.java
##########
@@ -301,11 +308,24 @@ public void onScheduled(final ProcessContext context) 
throws ProcessException, U
         }
         this.partitionNames = partitionNames;
 
-        final String policyName = 
context.getProperty(ACCESS_POLICY).getValue();
-        final String policyKey = 
context.getProperty(POLICY_PRIMARY_KEY).getValue();
         final String namespace = context.getProperty(NAMESPACE).getValue();
         final String eventHubName = 
context.getProperty(EVENT_HUB_NAME).getValue();
         final String serviceBusEndpoint = 
context.getProperty(SERVICE_BUS_ENDPOINT).getValue();
+        final boolean useManagedIdentity = 
context.getProperty(USE_MANANGED_IDENTITY).asBoolean();
+        final String policyName, policyKey, connectionString;
+        if(useManagedIdentity){
+            policyName =  MANANGED_IDENDITY_POLICY;
+            policyKey = null;

Review comment:
       done




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to