exceptionfactory commented on a change in pull request #5154: URL: https://github.com/apache/nifi/pull/5154#discussion_r656302120
########## File path: nifi-commons/nifi-sensitive-property-provider/src/main/java/org/apache/nifi/properties/HashiCorpVaultTransitSensitivePropertyProvider.java ########## @@ -0,0 +1,92 @@ +/* + * 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; + +import org.apache.commons.lang3.StringUtils; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * Uses the HashiCorp Vault Transit Secrets Engine to encrypt sensitive values at rest. + */ +public class HashiCorpVaultTransitSensitivePropertyProvider extends AbstractHashiCorpVaultSensitivePropertyProvider { + private static final Charset PROPERTY_CHARSET = StandardCharsets.UTF_8; + private static final String TRANSIT_PATH = "vault.transit.path"; + + HashiCorpVaultTransitSensitivePropertyProvider(final BootstrapProperties bootstrapProperties) { + super(bootstrapProperties); + } + + @Override + protected String getSecretsEnginePath(final BootstrapProperties vaultBootstrapProperties) { + if (vaultBootstrapProperties == null) { + return null; + } + final String transitPath = vaultBootstrapProperties.getProperty(TRANSIT_PATH); + // Validate transit path + try { + PropertyProtectionScheme.fromIdentifier(getProtectionScheme().getIdentifier(transitPath)); + } catch (IllegalArgumentException e) { + throw new SensitivePropertyProtectionException(String.format("{} [{}] contains unsupported characters", TRANSIT_PATH, transitPath), e); Review comment: It looks like the placeholders should be `%s` instead of `{}` since this it not a logging message. ```suggestion throw new SensitivePropertyProtectionException(String.format("%s [%s] contains unsupported characters", TRANSIT_PATH, transitPath), e); ``` ########## File path: nifi-commons/nifi-property-utils/src/main/java/org/apache/nifi/properties/AbstractBootstrapPropertiesLoader.java ########## @@ -121,8 +136,7 @@ private File getBootstrapFile(final String bootstrapPath) throws IOException { if (confDir.exists() && confDir.canRead()) { expectedBootstrapFile = new File(confDir, BOOTSTRAP_CONF); } else { - logger.error("Cannot read from bootstrap.conf file at {} -- conf/ directory is missing or permissions are incorrect", confDir.getAbsolutePath()); - throw new IOException("Cannot read from bootstrap.conf"); + throw new IOException(String.format("Cannot read {} directory for {}", confDir, bootstrapPath)); Review comment: It looks like the placeholder values should be changed: ```suggestion throw new IOException(String.format("Cannot read %s directory for %s", confDir, bootstrapPath)); ``` ########## File path: nifi-commons/nifi-sensitive-property-provider/src/main/java/org/apache/nifi/properties/HashiCorpVaultTransitSensitivePropertyProvider.java ########## @@ -0,0 +1,92 @@ +/* + * 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; + +import org.apache.commons.lang3.StringUtils; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +/** + * Uses the HashiCorp Vault Transit Secrets Engine to encrypt sensitive values at rest. + */ +public class HashiCorpVaultTransitSensitivePropertyProvider extends AbstractHashiCorpVaultSensitivePropertyProvider { + private static final Charset PROPERTY_CHARSET = StandardCharsets.UTF_8; + private static final String TRANSIT_PATH = "vault.transit.path"; + + HashiCorpVaultTransitSensitivePropertyProvider(final BootstrapProperties bootstrapProperties) { + super(bootstrapProperties); + } + + @Override + protected String getSecretsEnginePath(final BootstrapProperties vaultBootstrapProperties) { + if (vaultBootstrapProperties == null) { + return null; + } + final String transitPath = vaultBootstrapProperties.getProperty(TRANSIT_PATH); + // Validate transit path + try { + PropertyProtectionScheme.fromIdentifier(getProtectionScheme().getIdentifier(transitPath)); + } catch (IllegalArgumentException e) { + throw new SensitivePropertyProtectionException(String.format("{} [{}] contains unsupported characters", TRANSIT_PATH, transitPath), e); + } + + return transitPath; + } + + @Override + protected PropertyProtectionScheme getProtectionScheme() { + return PropertyProtectionScheme.HASHICORP_VAULT_TRANSIT; + } + + @Override + protected boolean hasRequiredSecretsEngineProperties(final BootstrapProperties vaultBootstrapProperties) { + return getSecretsEnginePath(vaultBootstrapProperties) != null; + } + + /** + * Returns the encrypted cipher text. + * + * @param unprotectedValue the sensitive value + * @return the value to persist in the {@code nifi.properties} file + * @throws SensitivePropertyProtectionException if there is an exception encrypting the value + */ + @Override + public String protect(final String unprotectedValue) throws SensitivePropertyProtectionException { + if (StringUtils.isBlank(unprotectedValue)) { + throw new IllegalArgumentException("Cannot encrypt an empty value"); + } + + return getVaultCommunicationService().encrypt(getPath(), unprotectedValue.getBytes(PROPERTY_CHARSET)); + } + + /** + * Returns the decrypted plaintext. + * + * @param protectedValue the cipher text read from the {@code nifi.properties} file + * @return the raw value to be used by the application + * @throws SensitivePropertyProtectionException if there is an error decrypting the cipher text + */ + @Override + public String unprotect(final String protectedValue) throws SensitivePropertyProtectionException { + if (StringUtils.isBlank(protectedValue)) { + throw new IllegalArgumentException("Cannot encrypt an empty value"); Review comment: It looks like this message should be updated: ```suggestion throw new IllegalArgumentException("Cannot decrypt an empty value"); ``` -- 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]
