markap14 commented on a change in pull request #5391:
URL: https://github.com/apache/nifi/pull/5391#discussion_r722327357



##########
File path: 
nifi-nar-bundles/nifi-aws-bundle/nifi-aws-parameter-value-providers/src/main/java/org/apache/nifi/stateless/parameter/AwsSecretsManagerParameterValueProvider.java
##########
@@ -0,0 +1,161 @@
+/*
+ * 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.stateless.parameter;
+
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
+import com.amazonaws.auth.BasicAWSCredentials;
+import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
+import com.amazonaws.services.secretsmanager.AWSSecretsManager;
+import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder;
+import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest;
+import com.amazonaws.services.secretsmanager.model.GetSecretValueResult;
+import com.amazonaws.services.secretsmanager.model.ResourceNotFoundException;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.Paths;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * Reads secrets from AWS Secrets Manager to provide parameter values.  
Secrets must be created similar to the following AWS cli command: <br/><br/>
+ * <code>aws secretsmanager create-secret --name "[Context]" --secret-string 
'{ "[Param]": "[secretValue]", "[Param2]": "[secretValue2]" }'</code> <br/><br/>
+ *
+ * A standard configuration for this provider would be: <br/><br/>
+ *
+ * <code>
+ *      nifi.stateless.parameter.provider.AWSSecretsManager.name=AWS Secrets 
Manager Value Provider
+ *      
nifi.stateless.parameter.provider.AWSSecretsManager.type=org.apache.nifi.stateless.parameter.AwsSecretsManagerParameterValueProvider
+ *      
nifi.stateless.parameter.provider.AWSSecretsManager.properties.aws-credentials-file=./conf/bootstrap-aws.conf
+ * </code>
+ */
+public class AwsSecretsManagerParameterValueProvider extends 
AbstractSecretBasedParameterValueProvider implements ParameterValueProvider {
+    private static final Logger logger = 
LoggerFactory.getLogger(AwsSecretsManagerParameterValueProvider.class);
+
+    private static final String ACCESS_KEY_PROPS_NAME = "aws.access.key.id";
+    private static final String SECRET_KEY_PROPS_NAME = 
"aws.secret.access.key";
+    private static final String REGION_KEY_PROPS_NAME = "aws.region";
+
+    public static final PropertyDescriptor AWS_CREDENTIALS_FILE = new 
PropertyDescriptor.Builder()
+            .displayName("AWS Credentials File")
+            .name("aws-credentials-file")
+            .required(false)
+            .defaultValue("./conf/bootstrap-aws.conf")
+            .description("Location of the bootstrap-aws.conf file that 
configures the AWS credentials.  If not provided, the default AWS credentials 
will be used.")
+            .addValidator(StandardValidators.FILE_EXISTS_VALIDATOR)
+            .build();
+
+    private final ObjectMapper objectMapper = new ObjectMapper();
+
+    private AWSSecretsManager secretsManager;
+
+    @Override
+    protected List<PropertyDescriptor> 
getAdditionalSupportedPropertyDescriptors() {
+        return Collections.singletonList(AWS_CREDENTIALS_FILE);
+    }
+
+    @Override
+    protected void additionalInit(final 
ParameterValueProviderInitializationContext context) {
+        final String awsCredentialsFilename = 
context.getProperty(AWS_CREDENTIALS_FILE).getValue();
+        try {
+            this.secretsManager = this.configureClient(awsCredentialsFilename);
+        } catch (final IOException e) {
+            throw new IllegalStateException("Could not configure AWS Secrets 
Manager Client", e);
+        }
+    }
+
+    @Override
+    protected String getSecretValue(final String secretName, final String 
keyName) {
+        final GetSecretValueRequest getSecretValueRequest = new 
GetSecretValueRequest()
+                .withSecretId(secretName);
+        try {
+            final GetSecretValueResult getSecretValueResult = 
secretsManager.getSecretValue(getSecretValueRequest);

Review comment:
       If the given secret name is not a valid secret name, this throws an 
AWSSecretsManagerException. In that case, I think we need to catch the 
Exception and delegate to the default Secret or return null, but we should 
throw an Exception here.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to