-
Notifications
You must be signed in to change notification settings - Fork 2.9k
NIFI-8511 Added KeyStore implementation of KeyProvider #5110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?xml version="1.0"?> | ||
| <!-- | ||
| 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. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.apache.nifi</groupId> | ||
| <artifactId>nifi-commons</artifactId> | ||
| <version>1.14.0-SNAPSHOT</version> | ||
| </parent> | ||
| <artifactId>nifi-security-kms</artifactId> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>commons-codec</groupId> | ||
| <artifactId>commons-codec</artifactId> | ||
| <version>1.15</version> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import org.apache.nifi.security.kms.reader.StandardFileBasedKeyReader; | ||
| import org.apache.nifi.security.kms.reader.FileBasedKeyReader; | ||
|
|
||
| import java.nio.file.Path; | ||
| import javax.crypto.SecretKey; | ||
|
|
||
| /** | ||
| * File Based Key Provider reads encrypted Secret Keys from a properties file containing one or more entries | ||
| */ | ||
| public class FileBasedKeyProvider extends StaticKeyProvider { | ||
| private static final FileBasedKeyReader READER = new StandardFileBasedKeyReader(); | ||
|
|
||
| public FileBasedKeyProvider(final Path location, final SecretKey rootKey) { | ||
| super(READER.readSecretKeys(location, rootKey)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import org.apache.commons.codec.DecoderException; | ||
| import org.apache.nifi.security.kms.configuration.FileBasedKeyProviderConfiguration; | ||
| import org.apache.nifi.security.kms.configuration.KeyProviderConfiguration; | ||
| import org.apache.nifi.security.kms.configuration.KeyStoreKeyProviderConfiguration; | ||
| import org.apache.nifi.security.kms.configuration.StaticKeyProviderConfiguration; | ||
| import org.apache.commons.codec.binary.Hex; | ||
| import org.apache.nifi.security.kms.reader.KeyReaderException; | ||
|
|
||
| import javax.crypto.SecretKey; | ||
| import javax.crypto.spec.SecretKeySpec; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.security.KeyStore; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Key Provider Factory | ||
| */ | ||
| public class KeyProviderFactory { | ||
| private static final String SECRET_KEY_ALGORITHM = "AES"; | ||
|
|
||
| /** | ||
| * Get Key Provider based on Configuration | ||
| * | ||
| * @param configuration Key Provider Configuration | ||
| * @return Key Provider | ||
| */ | ||
| public static KeyProvider getKeyProvider(final KeyProviderConfiguration<?> configuration) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've got an idea to avoid downcasting and branching here. The KeyProviderConfiguration interface could contain a new method: A key provider creator class could handle creating the different providers based on different configurations: }` The configuration classes can utilize the creator class to make providers:
And then in the factory, there's no branching remaining: `public class KeyProviderFactory { }` What do you think of this approach? It adds a bit of extra complexity to other classes but withdraws some from the factory. Also, this way if anyone adds a new configuration, they will be obliged to implement the provider method.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion, that is an interesting idea. The purpose of the |
||
| KeyProvider keyProvider; | ||
|
|
||
| if (configuration instanceof StaticKeyProviderConfiguration) { | ||
| final StaticKeyProviderConfiguration providerConfiguration = (StaticKeyProviderConfiguration) configuration; | ||
| final Map<String, SecretKey> secretKeys; | ||
| try { | ||
| secretKeys = getSecretKeys(providerConfiguration.getKeys()); | ||
| keyProvider = new StaticKeyProvider(secretKeys); | ||
| } catch (final DecoderException e) { | ||
| throw new KeyReaderException("Decoding Hexadecimal Secret Keys failed", e); | ||
| } | ||
| } else if (configuration instanceof FileBasedKeyProviderConfiguration) { | ||
| final FileBasedKeyProviderConfiguration providerConfiguration = (FileBasedKeyProviderConfiguration) configuration; | ||
| final Path keyProviderPath = Paths.get(providerConfiguration.getLocation()); | ||
| keyProvider = new FileBasedKeyProvider(keyProviderPath, providerConfiguration.getRootKey()); | ||
| } else if (configuration instanceof KeyStoreKeyProviderConfiguration) { | ||
| final KeyStoreKeyProviderConfiguration providerConfiguration = (KeyStoreKeyProviderConfiguration) configuration; | ||
| final KeyStore keyStore = providerConfiguration.getKeyStore(); | ||
| keyProvider = new KeyStoreKeyProvider(keyStore, providerConfiguration.getKeyPassword()); | ||
| } else { | ||
| throw new UnsupportedOperationException(String.format("Key Provider [%s] not supported", configuration.getKeyProviderClass().getName())); | ||
| } | ||
|
|
||
| return keyProvider; | ||
| } | ||
|
|
||
| private static Map<String, SecretKey> getSecretKeys(final Map<String, String> keys) throws DecoderException { | ||
| final Map<String, SecretKey> secretKeys = new HashMap<>(); | ||
|
|
||
| for (final Map.Entry<String, String> keyEntry : keys.entrySet()) { | ||
| final byte[] encodedSecretKey = Hex.decodeHex(keyEntry.getValue()); | ||
| final SecretKey secretKey = new SecretKeySpec(encodedSecretKey, SECRET_KEY_ALGORITHM); | ||
| secretKeys.put(keyEntry.getKey(), secretKey); | ||
| } | ||
|
|
||
| return secretKeys; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import org.apache.nifi.security.kms.reader.KeyReaderException; | ||
|
|
||
| import javax.crypto.SecretKey; | ||
| import java.security.GeneralSecurityException; | ||
| import java.security.Key; | ||
| import java.security.KeyStore; | ||
| import java.util.Enumeration; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * KeyStore implementation of Key Provider | ||
| */ | ||
| public class KeyStoreKeyProvider extends StaticKeyProvider { | ||
| /** | ||
| * KeyStore Key Provider constructor with KeyStore and password used to read Secret Key entries | ||
| * | ||
| * @param keyStore KeyStore | ||
| * @param keyPassword Password for reading Secret Key entries | ||
| */ | ||
| public KeyStoreKeyProvider(final KeyStore keyStore, final char[] keyPassword) { | ||
| super(readSecretKeys(requireNonNull(keyStore, "KeyStore required"), requireNonNull(keyPassword, "Password required"))); | ||
| } | ||
|
|
||
| private static Map<String, SecretKey> readSecretKeys(final KeyStore keyStore, final char[] keyPassword) throws KeyReaderException { | ||
| final Map<String, SecretKey> secretKeys = new HashMap<>(); | ||
|
|
||
| try { | ||
| final Enumeration<String> aliases = keyStore.aliases(); | ||
| while (aliases.hasMoreElements()) { | ||
| final String alias = aliases.nextElement(); | ||
| final Key key = keyStore.getKey(alias, keyPassword); | ||
| if (key instanceof SecretKey) { | ||
| final SecretKey secretKey = (SecretKey) key; | ||
| secretKeys.put(alias, secretKey); | ||
| } | ||
| } | ||
| } catch (final GeneralSecurityException e) { | ||
| throw new KeyReaderException("Reading KeyStore failed", e); | ||
| } | ||
|
|
||
| return secretKeys; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.