|
| 1 | +/* |
| 2 | + * Copyright 2019 Aiven Oy |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.aiven.kafka.connect.transforms; |
| 18 | + |
| 19 | +import java.security.MessageDigest; |
| 20 | +import java.security.NoSuchAlgorithmException; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Base64; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.stream.Collectors; |
| 27 | + |
| 28 | +import org.apache.kafka.common.config.ConfigDef; |
| 29 | +import org.apache.kafka.connect.connector.ConnectRecord; |
| 30 | +import org.apache.kafka.connect.data.Field; |
| 31 | +import org.apache.kafka.connect.data.Schema; |
| 32 | +import org.apache.kafka.connect.data.SchemaAndValue; |
| 33 | +import org.apache.kafka.connect.data.Struct; |
| 34 | +import org.apache.kafka.connect.errors.DataException; |
| 35 | +import org.apache.kafka.connect.transforms.Transformation; |
| 36 | + |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | + |
| 41 | +public abstract class HashField<R extends ConnectRecord<R>> implements Transformation<R> { |
| 42 | + private static final Logger log = LoggerFactory.getLogger(HashField.class); |
| 43 | + |
| 44 | + private HashFieldConfig config; |
| 45 | + private static final List<Schema.Type> SUPPORTED_TYPES_TO_CONVERT_FROM = Arrays.asList( |
| 46 | + Schema.Type.STRING |
| 47 | + ); |
| 48 | + |
| 49 | + @Override |
| 50 | + public ConfigDef config() { |
| 51 | + return HashFieldConfig.config(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void configure(final Map<String, ?> configs) { |
| 56 | + this.config = new HashFieldConfig(configs); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public R apply(final R record) { |
| 61 | + final SchemaAndValue schemaAndValue = getSchemaAndValue(record); |
| 62 | + if (schemaAndValue.schema() == null) { |
| 63 | + throw new DataException(dataPlace() + " schema can't be null: " + record); |
| 64 | + } |
| 65 | + |
| 66 | + final Optional<String> newValue; |
| 67 | + if (config.fieldName().isPresent()) { |
| 68 | + newValue = getNewValueForNamedField( |
| 69 | + record.toString(), schemaAndValue.schema(), schemaAndValue.value(), config.fieldName().get()); |
| 70 | + } else { |
| 71 | + newValue = getNewValueWithoutFieldName( |
| 72 | + record.toString(), schemaAndValue.schema(), schemaAndValue.value()); |
| 73 | + } |
| 74 | + |
| 75 | + if (newValue.isPresent()) { |
| 76 | + return record.newRecord( |
| 77 | + record.topic(), |
| 78 | + record.kafkaPartition(), |
| 79 | + record.keySchema(), |
| 80 | + record.key(), |
| 81 | + record.valueSchema(), |
| 82 | + newValue.get(), |
| 83 | + record.timestamp(), |
| 84 | + record.headers() |
| 85 | + ); |
| 86 | + } else { |
| 87 | + return record; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void close() { |
| 93 | + } |
| 94 | + |
| 95 | + protected abstract String dataPlace(); |
| 96 | + |
| 97 | + protected abstract SchemaAndValue getSchemaAndValue(final R record); |
| 98 | + |
| 99 | + private Optional<String> getNewValueForNamedField(final String recordStr, |
| 100 | + final Schema schema, |
| 101 | + final Object value, |
| 102 | + final String fieldName) { |
| 103 | + if (Schema.Type.STRUCT != schema.type()) { |
| 104 | + throw new DataException(dataPlace() + " schema type must be STRUCT if field name is specified: " |
| 105 | + + recordStr); |
| 106 | + } |
| 107 | + |
| 108 | + if (value == null) { |
| 109 | + throw new DataException(dataPlace() + " can't be null if field name is specified: " + recordStr); |
| 110 | + } |
| 111 | + |
| 112 | + final Field field = schema.field(fieldName); |
| 113 | + if (field == null) { |
| 114 | + return Optional.empty(); |
| 115 | + } |
| 116 | + |
| 117 | + if (!SUPPORTED_TYPES_TO_CONVERT_FROM.contains(field.schema().type())) { |
| 118 | + throw new DataException(fieldName + " schema type in " + dataPlace() |
| 119 | + + " must be " + SUPPORTED_TYPES_TO_CONVERT_FROM |
| 120 | + + ": " + recordStr); |
| 121 | + } |
| 122 | + |
| 123 | + final Struct struct = (Struct) value; |
| 124 | + |
| 125 | + final Optional<String> result = Optional.ofNullable(struct.get(fieldName)) |
| 126 | + .map(Object::toString).flatMap(s -> config.hashFunction().map(alg -> hashString(alg, s))); |
| 127 | + if (result.isPresent() && !result.get().equals("")) { |
| 128 | + return result; |
| 129 | + } else { |
| 130 | + return Optional.empty(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + static final String hashString(final String hashAlg, final String input) { |
| 135 | + try { |
| 136 | + final MessageDigest md = MessageDigest.getInstance(hashAlg); |
| 137 | + final byte[] digest = md.digest(input.getBytes()); |
| 138 | + return Base64.getEncoder().encodeToString(digest); |
| 139 | + } catch (final NoSuchAlgorithmException e) { |
| 140 | + System.out.println("EXCEPTION: " + e.getMessage()); |
| 141 | + throw new DataException("Hash function " + hashAlg |
| 142 | + + " must be " |
| 143 | + + HashFieldConfig.HashFunction.stringValues |
| 144 | + .stream() |
| 145 | + .collect(Collectors.joining("|", "[", "]"))); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private Optional<String> getNewValueWithoutFieldName(final String recordStr, |
| 150 | + final Schema schema, |
| 151 | + final Object value) { |
| 152 | + if (!SUPPORTED_TYPES_TO_CONVERT_FROM.contains(schema.type())) { |
| 153 | + throw new DataException(dataPlace() + " schema type must be " |
| 154 | + + SUPPORTED_TYPES_TO_CONVERT_FROM |
| 155 | + + " if field name is not specified: " |
| 156 | + + recordStr); |
| 157 | + } |
| 158 | + |
| 159 | + final Optional<String> result = Optional.ofNullable(value) |
| 160 | + .map(Object::toString).flatMap(s -> config.hashFunction().map(alg -> hashString(alg, s))); |
| 161 | + |
| 162 | + if (result.isPresent() && !result.get().equals("")) { |
| 163 | + return result; |
| 164 | + } else { |
| 165 | + return Optional.empty(); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + public static class Key<R extends ConnectRecord<R>> extends HashField<R> { |
| 170 | + @Override |
| 171 | + protected SchemaAndValue getSchemaAndValue(final R record) { |
| 172 | + return new SchemaAndValue(record.keySchema(), record.key()); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + protected String dataPlace() { |
| 177 | + return "key"; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + public static class Value<R extends ConnectRecord<R>> extends HashField<R> { |
| 182 | + @Override |
| 183 | + protected SchemaAndValue getSchemaAndValue(final R record) { |
| 184 | + return new SchemaAndValue(record.valueSchema(), record.value()); |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + protected String dataPlace() { |
| 189 | + return "value"; |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments