emiliosetiadarma commented on a change in pull request #5202:
URL: https://github.com/apache/nifi/pull/5202#discussion_r668955791



##########
File path: 
nifi-commons/nifi-sensitive-property-provider/src/main/java/org/apache/nifi/properties/aws/AWSSensitivePropertyProvider.java
##########
@@ -0,0 +1,382 @@
+/*
+ * 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.aws;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.nifi.properties.AbstractBootstrapPropertiesLoader;
+import org.apache.nifi.properties.AbstractSensitivePropertyProvider;
+import org.apache.nifi.properties.BootstrapProperties;
+import org.apache.nifi.properties.PropertyProtectionScheme;
+import org.apache.nifi.properties.SensitivePropertyProtectionException;
+import org.apache.nifi.properties.BootstrapProperties.BootstrapPropertyKey;
+
+import org.bouncycastle.util.encoders.Base64;
+import org.bouncycastle.util.encoders.DecoderException;
+import org.bouncycastle.util.encoders.EncoderException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
+import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
+import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.core.SdkBytes;
+import software.amazon.awssdk.core.exception.SdkClientException;
+import software.amazon.awssdk.regions.Region;
+import software.amazon.awssdk.services.kms.KmsClient;
+import software.amazon.awssdk.services.kms.model.DecryptRequest;
+import software.amazon.awssdk.services.kms.model.DecryptResponse;
+import software.amazon.awssdk.services.kms.model.DescribeKeyRequest;
+import software.amazon.awssdk.services.kms.model.DescribeKeyResponse;
+import software.amazon.awssdk.services.kms.model.EncryptRequest;
+import software.amazon.awssdk.services.kms.model.EncryptResponse;
+import software.amazon.awssdk.services.kms.model.KeyMetadata;
+import software.amazon.awssdk.services.kms.model.KmsException;
+
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Paths;
+
+public class AWSSensitivePropertyProvider extends 
AbstractSensitivePropertyProvider {
+    private static final Logger logger = 
LoggerFactory.getLogger(AWSSensitivePropertyProvider.class);
+
+    private static final String IMPLEMENTATION_NAME = "AWS KMS Sensitive 
Property Provider";
+    private static final String IMPLEMENTATION_KEY = "aws/kms";
+
+    private static final String AWS_KMS_PREFIX = "aws";
+    private static final String ACCESS_KEY_PROPS_NAME = "aws.access.key.id";
+    private static final String SECRET_KEY_PROPS_NAME = "aws.secret.key.id";
+    private static final String REGION_KEY_PROPS_NAME = "aws.region";
+    private static final String KMS_KEY_PROPS_NAME = "aws.kms.key.id";
+
+    private static final Charset PROPERTY_CHARSET = StandardCharsets.UTF_8;
+
+    private KmsClient client;
+    private BootstrapProperties awsBootstrapProperties;
+    private String keyId;
+
+    public AWSSensitivePropertyProvider(BootstrapProperties 
bootstrapProperties) throws SensitivePropertyProtectionException {
+        super(bootstrapProperties);
+        // if either awsBootstrapProperties or keyId is loaded as null values, 
then isSupported will return false
+        awsBootstrapProperties = 
getAWSBootstrapProperties(bootstrapProperties);
+        if (awsBootstrapProperties != null) {
+            loadRequiredAWSProperties(awsBootstrapProperties);
+        }
+    }
+
+    /**
+     * Initializes the KMS Client to be used for encrypt, decrypt and other 
interactions with AWS KMS
+     * First attempts to use default AWS Credentials Provider Chain
+     * If that attempt fails, attempt to initialize credentials using 
bootstrap-aws.conf
+     * Note: This does not verify if credentials are valid
+     */
+    private void initializeClient() {
+        if (awsBootstrapProperties == null) {
+            logger.warn("Cannot initialize client if awsBootstrapProperties is 
null");
+            return;
+        }
+        String accessKeyId = 
awsBootstrapProperties.getProperty(ACCESS_KEY_PROPS_NAME);
+        String secretKeyId = 
awsBootstrapProperties.getProperty(SECRET_KEY_PROPS_NAME);
+        String region = 
awsBootstrapProperties.getProperty(REGION_KEY_PROPS_NAME);
+
+        if (StringUtils.isNotBlank(accessKeyId) && 
StringUtils.isNotBlank(secretKeyId) && StringUtils.isNotBlank(region)) {
+            logger.debug("Credentials/Configuration provided in 
bootstrap-aws.conf");
+            try {
+                AwsBasicCredentials credentials = 
AwsBasicCredentials.create(accessKeyId, secretKeyId);
+                client = KmsClient.builder()
+                        .region(Region.of(region))
+                        
.credentialsProvider(StaticCredentialsProvider.create(credentials))
+                        .build();
+            } catch (KmsException | NullPointerException | 
IllegalArgumentException e) {
+                logger.error("Credentials/Configuration provided in 
bootstrap-aws.conf are invalid");
+                throw new SensitivePropertyProtectionException("Require valid 
credentials/configuration to initialize KMS client");

Review comment:
       Making the changes




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


Reply via email to