List of usage examples for org.apache.commons.lang3 StringUtils isAnyEmpty
public static boolean isAnyEmpty(final CharSequence... css)
Checks if any one of the CharSequences are empty ("") or null.
StringUtils.isAnyEmpty(null) = true StringUtils.isAnyEmpty(null, "foo") = true StringUtils.isAnyEmpty("", "bar") = true StringUtils.isAnyEmpty("bob", "") = true StringUtils.isAnyEmpty(" bob ", null) = true StringUtils.isAnyEmpty(" ", "bar") = false StringUtils.isAnyEmpty("foo", "bar") = false
From source file:org.apache.nifi.provenance.CryptoUtils.java
/** * Returns a map containing the key IDs and the parsed key from a key provider definition file. * The values in the file are decrypted using the master key provided. If the file is missing or empty, * cannot be read, or if no valid keys are read, a {@link KeyManagementException} will be thrown. * * @param filepath the key definition file path * @param masterKey the master key used to decrypt each key definition * @return a Map of key IDs to SecretKeys * @throws KeyManagementException if the file is missing or invalid *///from w w w . j a v a 2 s. c o m public static Map<String, SecretKey> readKeys(String filepath, SecretKey masterKey) throws KeyManagementException { Map<String, SecretKey> keys = new HashMap<>(); if (StringUtils.isBlank(filepath)) { throw new KeyManagementException("The key provider file is not present and readable"); } File file = new File(filepath); if (!file.exists() || !file.canRead()) { throw new KeyManagementException("The key provider file is not present and readable"); } try (BufferedReader br = new BufferedReader(new FileReader(file))) { AESKeyedCipherProvider masterCipherProvider = new AESKeyedCipherProvider(); String line; int l = 1; while ((line = br.readLine()) != null) { String[] components = line.split("=", 2); if (components.length != 2 || StringUtils.isAnyEmpty(components)) { logger.warn("Line " + l + " is not properly formatted -- keyId=Base64EncodedKey..."); } String keyId = components[0]; if (StringUtils.isNotEmpty(keyId)) { try { byte[] base64Bytes = Base64.getDecoder().decode(components[1]); byte[] ivBytes = Arrays.copyOfRange(base64Bytes, 0, IV_LENGTH); Cipher masterCipher = null; try { masterCipher = masterCipherProvider.getCipher(EncryptionMethod.AES_GCM, masterKey, ivBytes, false); } catch (Exception e) { throw new KeyManagementException( "Error building cipher to decrypt FileBaseKeyProvider definition at " + filepath, e); } byte[] individualKeyBytes = masterCipher .doFinal(Arrays.copyOfRange(base64Bytes, IV_LENGTH, base64Bytes.length)); SecretKey key = new SecretKeySpec(individualKeyBytes, "AES"); logger.debug("Read and decrypted key for " + keyId); if (keys.containsKey(keyId)) { logger.warn("Multiple key values defined for " + keyId + " -- using most recent value"); } keys.put(keyId, key); } catch (IllegalArgumentException e) { logger.error("Encountered an error decoding Base64 for " + keyId + ": " + e.getLocalizedMessage()); } catch (BadPaddingException | IllegalBlockSizeException e) { logger.error("Encountered an error decrypting key for " + keyId + ": " + e.getLocalizedMessage()); } } l++; } if (keys.isEmpty()) { throw new KeyManagementException("The provided file contained no valid keys"); } logger.info("Read " + keys.size() + " keys from FileBasedKeyProvider " + filepath); return keys; } catch (IOException e) { throw new KeyManagementException("Error reading FileBasedKeyProvider definition at " + filepath, e); } }
From source file:org.apache.nifi.security.kms.CryptoUtils.java
/** * Returns a map containing the key IDs and the parsed key from a key provider definition file. * The values in the file are decrypted using the master key provided. If the file is missing or empty, * cannot be read, or if no valid keys are read, a {@link KeyManagementException} will be thrown. * * @param filepath the key definition file path * @param masterKey the master key used to decrypt each key definition * @return a Map of key IDs to SecretKeys * @throws KeyManagementException if the file is missing or invalid *///from w w w . j av a 2s . co m public static Map<String, SecretKey> readKeys(String filepath, SecretKey masterKey) throws KeyManagementException { Map<String, SecretKey> keys = new HashMap<>(); if (StringUtils.isBlank(filepath)) { throw new KeyManagementException("The key provider file is not present and readable"); } if (masterKey == null) { throw new KeyManagementException("The master key must be provided to decrypt the individual keys"); } File file = new File(filepath); if (!file.exists() || !file.canRead()) { throw new KeyManagementException("The key provider file is not present and readable"); } try (BufferedReader br = new BufferedReader(new FileReader(file))) { AESKeyedCipherProvider masterCipherProvider = new AESKeyedCipherProvider(); String line; int l = 1; while ((line = br.readLine()) != null) { String[] components = line.split("=", 2); if (components.length != 2 || StringUtils.isAnyEmpty(components)) { logger.warn("Line " + l + " is not properly formatted -- keyId=Base64EncodedKey..."); } String keyId = components[0]; if (StringUtils.isNotEmpty(keyId)) { try { byte[] base64Bytes = Base64.getDecoder().decode(components[1]); byte[] ivBytes = Arrays.copyOfRange(base64Bytes, 0, IV_LENGTH); Cipher masterCipher = null; try { masterCipher = masterCipherProvider.getCipher(EncryptionMethod.AES_GCM, masterKey, ivBytes, false); } catch (Exception e) { throw new KeyManagementException( "Error building cipher to decrypt FileBaseKeyProvider definition at " + filepath, e); } byte[] individualKeyBytes = masterCipher .doFinal(Arrays.copyOfRange(base64Bytes, IV_LENGTH, base64Bytes.length)); SecretKey key = new SecretKeySpec(individualKeyBytes, "AES"); logger.debug("Read and decrypted key for " + keyId); if (keys.containsKey(keyId)) { logger.warn("Multiple key values defined for " + keyId + " -- using most recent value"); } keys.put(keyId, key); } catch (IllegalArgumentException e) { logger.error("Encountered an error decoding Base64 for " + keyId + ": " + e.getLocalizedMessage()); } catch (BadPaddingException | IllegalBlockSizeException e) { logger.error("Encountered an error decrypting key for " + keyId + ": " + e.getLocalizedMessage()); } } l++; } if (keys.isEmpty()) { throw new KeyManagementException("The provided file contained no valid keys"); } logger.info("Read " + keys.size() + " keys from FileBasedKeyProvider " + filepath); return keys; } catch (IOException e) { throw new KeyManagementException("Error reading FileBasedKeyProvider definition at " + filepath, e); } }
From source file:org.apache.zeppelin.shell.TerminalInterpreter.java
@Override protected boolean isKerboseEnabled() { if (!StringUtils.isAnyEmpty(getProperty("zeppelin.shell.auth.type")) && getProperty("zeppelin.shell.auth.type").equalsIgnoreCase("kerberos")) { return true; }//from ww w . ja v a 2 s.com return false; }