|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.security.ingest; |
| 7 | + |
| 8 | +import org.elasticsearch.ElasticsearchException; |
| 9 | +import org.elasticsearch.common.Nullable; |
| 10 | +import org.elasticsearch.common.Strings; |
| 11 | +import org.elasticsearch.common.collect.Tuple; |
| 12 | +import org.elasticsearch.common.settings.SecureSetting; |
| 13 | +import org.elasticsearch.common.settings.SecureString; |
| 14 | +import org.elasticsearch.common.settings.Setting; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
| 16 | +import org.elasticsearch.ingest.AbstractProcessor; |
| 17 | +import org.elasticsearch.ingest.ConfigurationUtils; |
| 18 | +import org.elasticsearch.ingest.IngestDocument; |
| 19 | +import org.elasticsearch.ingest.Processor; |
| 20 | +import org.elasticsearch.xpack.core.security.SecurityField; |
| 21 | +import org.elasticsearch.xpack.core.security.authc.support.CharArrays; |
| 22 | +import org.elasticsearch.xpack.core.security.authc.support.Hasher; |
| 23 | + |
| 24 | +import javax.crypto.Mac; |
| 25 | +import javax.crypto.SecretKeyFactory; |
| 26 | +import javax.crypto.spec.PBEKeySpec; |
| 27 | +import javax.crypto.spec.SecretKeySpec; |
| 28 | +import java.nio.charset.StandardCharsets; |
| 29 | +import java.security.InvalidKeyException; |
| 30 | +import java.security.NoSuchAlgorithmException; |
| 31 | +import java.security.spec.InvalidKeySpecException; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Base64; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.stream.Collectors; |
| 38 | + |
| 39 | +import static org.elasticsearch.ingest.ConfigurationUtils.newConfigurationException; |
| 40 | + |
| 41 | +/** |
| 42 | + * A processor that hashes the contents of a field (or fields) using various hashing algorithms |
| 43 | + */ |
| 44 | +public final class HashProcessor extends AbstractProcessor { |
| 45 | + enum Method { |
| 46 | + SHA1("HmacSHA1"), |
| 47 | + SHA256("HmacSHA256"), |
| 48 | + SHA384("HmacSHA384"), |
| 49 | + SHA512("HmacSHA512"); |
| 50 | + |
| 51 | + private final String algorithm; |
| 52 | + |
| 53 | + Method(String algorithm) { |
| 54 | + this.algorithm = algorithm; |
| 55 | + } |
| 56 | + |
| 57 | + public String getAlgorithm() { |
| 58 | + return algorithm; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String toString() { |
| 63 | + return name().toLowerCase(Locale.ROOT); |
| 64 | + } |
| 65 | + |
| 66 | + public String hash(Mac mac, byte[] salt, String input) { |
| 67 | + try { |
| 68 | + byte[] encrypted = mac.doFinal(input.getBytes(StandardCharsets.UTF_8)); |
| 69 | + byte[] messageWithSalt = new byte[salt.length + encrypted.length]; |
| 70 | + System.arraycopy(salt, 0, messageWithSalt, 0, salt.length); |
| 71 | + System.arraycopy(encrypted, 0, messageWithSalt, salt.length, encrypted.length); |
| 72 | + return Base64.getEncoder().encodeToString(messageWithSalt); |
| 73 | + } catch (IllegalStateException e) { |
| 74 | + throw new ElasticsearchException("error hashing data", e); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + public static Method fromString(String processorTag, String propertyName, String type) { |
| 79 | + try { |
| 80 | + return Method.valueOf(type.toUpperCase(Locale.ROOT)); |
| 81 | + } catch(IllegalArgumentException e) { |
| 82 | + throw newConfigurationException(TYPE, processorTag, propertyName, "type [" + type + |
| 83 | + "] not supported, cannot convert field. Valid hash methods: " + Arrays.toString(Method.values())); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public static final String TYPE = "hash"; |
| 89 | + public static final Setting.AffixSetting<SecureString> HMAC_KEY_SETTING = SecureSetting |
| 90 | + .affixKeySetting(SecurityField.setting("ingest." + TYPE) + ".", "key", |
| 91 | + (key) -> SecureSetting.secureString(key, null)); |
| 92 | + |
| 93 | + private final List<String> fields; |
| 94 | + private final String targetField; |
| 95 | + private final Method method; |
| 96 | + private final Mac mac; |
| 97 | + private final byte[] salt; |
| 98 | + |
| 99 | + HashProcessor(String tag, List<String> fields, String targetField, byte[] salt, Method method, @Nullable Mac mac) { |
| 100 | + super(tag); |
| 101 | + this.fields = fields; |
| 102 | + this.targetField = targetField; |
| 103 | + this.method = method; |
| 104 | + this.mac = mac; |
| 105 | + this.salt = salt; |
| 106 | + } |
| 107 | + |
| 108 | + List<String> getFields() { |
| 109 | + return fields; |
| 110 | + } |
| 111 | + |
| 112 | + String getTargetField() { |
| 113 | + return targetField; |
| 114 | + } |
| 115 | + |
| 116 | + byte[] getSalt() { |
| 117 | + return salt; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void execute(IngestDocument document) { |
| 122 | + Map<String, String> hashedFieldValues = fields.stream().map(f -> { |
| 123 | + try { |
| 124 | + String value = document.getFieldValue(f, String.class); |
| 125 | + return new Tuple<>(f, method.hash(mac, salt, value)); |
| 126 | + } catch (Exception e) { |
| 127 | + throw new IllegalArgumentException("field[" + f + "] could not be hashed", e); |
| 128 | + } |
| 129 | + }).collect(Collectors.toMap(Tuple::v1, Tuple::v2)); |
| 130 | + if (hashedFieldValues.size() == 1) { |
| 131 | + document.setFieldValue(targetField, hashedFieldValues.values().iterator().next()); |
| 132 | + } else { |
| 133 | + document.setFieldValue(targetField, hashedFieldValues); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public String getType() { |
| 139 | + return TYPE; |
| 140 | + } |
| 141 | + |
| 142 | + public static final class Factory implements Processor.Factory { |
| 143 | + |
| 144 | + private final Settings settings; |
| 145 | + |
| 146 | + public Factory(Settings settings) { |
| 147 | + this.settings = settings; |
| 148 | + } |
| 149 | + |
| 150 | + private static Mac createMac(Method method, SecureString password, byte[] salt, int iterations) { |
| 151 | + try { |
| 152 | + SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2With" + method.getAlgorithm()); |
| 153 | + PBEKeySpec keySpec = new PBEKeySpec(password.getChars(), salt, iterations, 128); |
| 154 | + byte[] pbkdf2 = secretKeyFactory.generateSecret(keySpec).getEncoded(); |
| 155 | + Mac mac = Mac.getInstance(method.getAlgorithm()); |
| 156 | + mac.init(new SecretKeySpec(pbkdf2, method.getAlgorithm())); |
| 157 | + return mac; |
| 158 | + } catch (NoSuchAlgorithmException | InvalidKeySpecException | InvalidKeyException e) { |
| 159 | + throw new IllegalArgumentException("invalid settings", e); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public HashProcessor create(Map<String, Processor.Factory> registry, String processorTag, Map<String, Object> config) { |
| 165 | + List<String> fields = ConfigurationUtils.readList(TYPE, processorTag, config, "fields"); |
| 166 | + if (fields.isEmpty()) { |
| 167 | + throw ConfigurationUtils.newConfigurationException(TYPE, processorTag, "fields", "must specify at least one field"); |
| 168 | + } else if (fields.stream().anyMatch(Strings::isNullOrEmpty)) { |
| 169 | + throw ConfigurationUtils.newConfigurationException(TYPE, processorTag, "fields", |
| 170 | + "a field-name entry is either empty or null"); |
| 171 | + } |
| 172 | + String targetField = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "target_field"); |
| 173 | + String keySettingName = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "key_setting"); |
| 174 | + SecureString key = HMAC_KEY_SETTING.getConcreteSetting(keySettingName).get(settings); |
| 175 | + byte[] salt = CharArrays.toUtf8Bytes(Hasher.SaltProvider.salt(8)); |
| 176 | + String methodProperty = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "method", "SHA256"); |
| 177 | + Method method = Method.fromString(processorTag, "method", methodProperty); |
| 178 | + int iterations = ConfigurationUtils.readIntProperty(TYPE, processorTag, config, "iterations", 5); |
| 179 | + Mac mac = createMac(method, key, salt, iterations); |
| 180 | + return new HashProcessor(processorTag, fields, targetField, salt, method, mac); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments