alopresto commented on a change in pull request #3672: NIFI-6363 Additional 
Sensitive Property Providers
URL: https://github.com/apache/nifi/pull/3672#discussion_r318140912
 
 

 ##########
 File path: 
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/main/java/org/apache/nifi/properties/sensitive/azure/keyvault/AzureKeyVaultSensitivePropertyProvider.java
 ##########
 @@ -0,0 +1,162 @@
+/*
+ * 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.properties.sensitive.azure.keyvault;
+
+import com.microsoft.azure.credentials.ApplicationTokenCredentials;
+import com.microsoft.azure.keyvault.KeyVaultClient;
+import com.microsoft.azure.keyvault.models.KeyOperationResult;
+import com.microsoft.azure.keyvault.models.KeyVaultErrorException;
+import com.microsoft.azure.keyvault.webkey.JsonWebKeyEncryptionAlgorithm;
+import com.microsoft.azure.management.Azure;
+import com.microsoft.rest.LogLevel;
+import org.apache.commons.lang3.StringUtils;
+import 
org.apache.nifi.properties.sensitive.SensitivePropertyConfigurationException;
+import 
org.apache.nifi.properties.sensitive.SensitivePropertyProtectionException;
+import org.apache.nifi.properties.sensitive.SensitivePropertyProvider;
+import org.bouncycastle.util.encoders.Base64;
+import org.bouncycastle.util.encoders.DecoderException;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+
+
+// Azure with Java:
+// https://docs.microsoft.com/en-us/azure/java/?view=azure-java-stable
+
+// Azure auth with Java:
+// https://github.com/Azure/azure-libraries-for-java/blob/master/AUTH.md
+
+// Azure Key Vault with Java:
+// 
https://docs.microsoft.com/en-us/java/api/com.microsoft.azure.keyvault?view=azure-java-stable
+
+
+public class AzureKeyVaultSensitivePropertyProvider implements 
SensitivePropertyProvider {
+    private static final String IMPLEMENTATION_NAME = "Azure Key Vault 
Sensitive Property Provider";
+    private static final String MATERIAL_PREFIX = "azure";
+    private static final String MATERIAL_KEY_TYPE = "vault";
+    private static final JsonWebKeyEncryptionAlgorithm algo = 
JsonWebKeyEncryptionAlgorithm.RSA_OAEP;
+
+    private final KeyVaultClient client;
+    private final String vaultId;
+    private final String keyId;
+
+    public AzureKeyVaultSensitivePropertyProvider(String material) {
+        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
+        try {
+            ApplicationTokenCredentials.fromFile(credFile).clientId();
+        } catch (IOException e) {
+            throw new SensitivePropertyConfigurationException(e);
+        }
+
+        String prefix = MATERIAL_PREFIX + "/" + MATERIAL_KEY_TYPE + "/";
+        if (material.startsWith(prefix)) {
+            material = material.substring(prefix.length());
+        }
+
+        String[] parts = material.split(",");
+        this.vaultId = parts[0];
+        this.keyId = parts[1];
+
+        try {
+            Azure azure = Azure.configure()
+                    .withLogLevel(LogLevel.BASIC)
+                    .authenticate(credFile)
+                    .withDefaultSubscription();
+            this.client = azure.vaults().getById(this.vaultId).client();
+        } catch (IOException e) {
+            throw new SensitivePropertyConfigurationException(e);
+        }
+    }
+
+    public static boolean isProviderFor(String key) {
+        return key.startsWith(MATERIAL_PREFIX + "/" + MATERIAL_KEY_TYPE + "/");
+    }
+
+    public static String toPrintableString(String key) {
+        return key;
+    }
+
+    /**
+     * Returns the name of the underlying implementation.
+     *
+     * @return the name of this sensitive property provider
+     */
+    @Override
+    public String getName() {
+        return IMPLEMENTATION_NAME;
+    }
+
+    /**
+     * Returns the key used to identify the provider implementation in {@code 
nifi.properties}.
+     *
+     * @return the key to persist in the sibling property
+     */
+    @Override
+    public String getIdentifierKey() {
+        return MATERIAL_PREFIX + "/" + MATERIAL_KEY_TYPE  + "" + vaultId + "," 
+ keyId;
+    }
+
+    /**
+     * Returns the "protected" form of this value. This is a form which can 
safely be persisted in the {@code nifi.properties} file without compromising 
the value.
+     * An encryption-based provider would return a cipher text, while a 
remote-lookup provider could return a unique ID to retrieve the secured value.
+     *
+     * @param unprotectedValue the sensitive value
+     * @return the value to persist in the {@code nifi.properties} file
+     */
+    @Override
+    public String protect(String unprotectedValue) throws 
SensitivePropertyProtectionException {
+        // TODO:  check length of value; azure limits length of encryption 
operations.
 
 Review comment:
   Add validation check here according to integration test/docs limits. 

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to