alopresto commented on a change in pull request #3672: NIFI-6363 Additional Sensitive Property Providers URL: https://github.com/apache/nifi/pull/3672#discussion_r318151738
########## File path: nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-properties-loader/src/main/java/org/apache/nifi/properties/sensitive/hashicorp/vault/VaultSensitivePropertyProvider.java ########## @@ -0,0 +1,293 @@ +/* + * 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.sensitive.hashicorp.vault; + +import org.apache.commons.lang3.StringUtils; +import org.apache.nifi.properties.sensitive.ExternalProperties; +import org.apache.nifi.properties.sensitive.SensitivePropertyConfigurationException; +import org.apache.nifi.properties.sensitive.SensitivePropertyProtectionException; +import org.apache.nifi.properties.sensitive.SensitivePropertyProvider; +import org.apache.nifi.properties.sensitive.StandardExternalPropertyLookup; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.core.io.Resource; +import org.springframework.vault.authentication.SimpleSessionManager; +import org.springframework.vault.client.VaultEndpoint; +import org.springframework.vault.config.ClientHttpRequestFactoryFactory; +import org.springframework.vault.core.VaultOperations; +import org.springframework.vault.core.VaultTemplate; +import org.springframework.vault.support.SslConfiguration; +import org.springframework.vault.support.ClientOptions; + +import java.net.URI; +import java.security.KeyStore; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/** + * Sensitive properties using Vault Transit encrypt and decrypt operations. + */ +public class VaultSensitivePropertyProvider implements SensitivePropertyProvider { + private static final Logger logger = LoggerFactory.getLogger(VaultSensitivePropertyProvider.class); + + private static final String PROVIDER_NAME = "HashiCorp Vault Sensitive Property Provider"; + static final String MATERIAL_PREFIX = "vault"; + static final String MATERIAL_SEPARATOR = "/"; + + static final String VAULT_AUTH_TOKEN = "token"; + static final String VAULT_AUTH_APP_ID = "appid"; + static final String VAULT_AUTH_APP_ROLE = "approle"; + static final String VAULT_AUTH_CUBBYHOLE = "cubbyhole"; + + private static final Set<String> VAULT_AUTH_TYPES = new HashSet<>(Arrays.asList( + VAULT_AUTH_TOKEN, + VAULT_AUTH_APP_ID, + VAULT_AUTH_APP_ROLE, + VAULT_AUTH_CUBBYHOLE + )); + + private final VaultOperations vaultOperations; + private final ExternalProperties externalProperties; + private final String transitKeyId; + private final String authType; + + public VaultSensitivePropertyProvider(String keyId) { + this(keyId, new StandardExternalPropertyLookup(null, getVaultPropertiesMapping())); + } + + /** + * Constructs a {@link SensitivePropertyProvider} that uses Vault encrypt and decrypt values. + * + * @param keyId vault key spec, in the form "vault/{auth-type}/{transit-key-id}" + */ + public VaultSensitivePropertyProvider(String keyId, ExternalProperties externalProperties) { + this.externalProperties = externalProperties; + transitKeyId = getTransitKey(keyId); + authType = getVaultAuthentication(keyId); + + String serverUri = getVaultUri(); + + if (StringUtils.isBlank(authType) || StringUtils.isBlank(serverUri) || StringUtils.isBlank(transitKeyId)) + throw new SensitivePropertyConfigurationException("The key cannot be empty"); + + VaultEndpoint vaultEndpoint = VaultEndpoint.from(URI.create(serverUri)); + StandardVaultConfiguration config = new StandardVaultConfiguration(vaultEndpoint, externalProperties); + + Resource keyStore = config.getVaultSslKeyStore(); + Resource trustStore = config.getVaultSslTrustStore(); + SslConfiguration sslConf = SslConfiguration.NONE; + + if (keyStore != null || trustStore != null) { Review comment: Requiring either or means that there are three potential configurations: * Vault using TLS (presenting certificate) and client not (`keystore == null && truststore != null`) * Vault using TLS (presenting certificate) and client using TLS (`keystore != null && truststore != null`) * Vault not using TLS (no cert) and client using TLS (`keystore != null && truststore == null`) So if Vault provides mutual authentication TLS (NiFi must present a trusted cert), then keystore matters. If Vault does not provide this, only the truststore matters. ---------------------------------------------------------------- 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] With regards, Apache Git Services
