exceptionfactory commented on a change in pull request #5110:
URL: https://github.com/apache/nifi/pull/5110#discussion_r644442480



##########
File path: 
nifi-commons/nifi-security-kms/src/main/java/org/apache/nifi/security/kms/reader/StandardFileBasedKeyReader.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.security.kms.reader;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.GCMParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+
+/**
+ * Standard File Based Key Reader reads Secret Keys from Properties files 
encrypted using AES-GCM with Tag Size of 128
+ */
+public class StandardFileBasedKeyReader implements FileBasedKeyReader {
+    protected static final String CIPHER_ALGORITHM = "AES/GCM/NoPadding";
+
+    protected static final int IV_LENGTH = 16;
+
+    protected static final int TAG_SIZE = 128;
+
+    private static final Base64.Decoder DECODER = Base64.getDecoder();
+
+    private static final String SECRET_KEY_ALGORITHM = "AES";
+
+    /**
+     * Read Secret Keys using provided Root Secret Key
+     *
+     * @param path File Path contains a properties file with Key Identifier 
and Base64-encoded encrypted values
+     * @param rootKey Root Secret Key
+     * @return Map of Key Identifier to decrypted Secret Key
+     */
+    @Override
+    public Map<String, SecretKey> readSecretKeys(final Path path, final 
SecretKey rootKey) {
+        Objects.requireNonNull(path, "Path required");
+        Objects.requireNonNull(rootKey, "Root Key required");
+        final Map<String, SecretKey> secretKeys = new HashMap<>();
+
+        final Properties properties = getProperties(path);
+        for (final String keyId : properties.stringPropertyNames()) {
+            final String encodedProperty = properties.getProperty(keyId);
+            final SecretKey secretKey = readSecretKey(keyId, encodedProperty, 
rootKey);
+            secretKeys.put(keyId, secretKey);
+        }
+        return secretKeys;
+    }
+
+    private Properties getProperties(final Path path) {
+        final Properties properties = new Properties();
+        try (final FileInputStream inputStream = new 
FileInputStream(path.toFile())) {
+            properties.load(inputStream);
+        } catch (final IOException e) {
+            throw new KeyReaderException(String.format("Reading Secret Keys 
Failed [%s]", path), e);
+        }
+        return properties;
+    }
+
+    private SecretKey readSecretKey(final String keyId, final String 
encodedProperty, final SecretKey rootKey) {

Review comment:
       This current effort refactored existing `KeyProvider` implementations to 
streamline the approach for `KeyStoreKeyProvider`, so the general purpose is 
reading keys as opposed to writing.  However, given that the 
`FileBasedKeyProvider` uses a very specific format, it probably makes sense to 
implement a writer in this same module to help maintain alignment between 
reading and writing.




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