|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.parquet; |
| 15 | + |
| 16 | +import io.airlift.log.Logger; |
| 17 | +import io.trino.filesystem.Location; |
| 18 | +import io.trino.filesystem.TrinoFileSystem; |
| 19 | +import io.trino.parquet.crypto.FileDecryptionProperties; |
| 20 | +import io.trino.parquet.crypto.InternalFileDecryptor; |
| 21 | +import io.trino.parquet.crypto.TrinoCryptoConfigurationUtil; |
| 22 | +import io.trino.parquet.crypto.TrinoDecryptionPropertiesFactory; |
| 23 | + |
| 24 | +import java.lang.reflect.InvocationTargetException; |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +public class EncryptionUtils |
| 28 | +{ |
| 29 | + public static final Logger LOG = Logger.get(EncryptionUtils.class); |
| 30 | + |
| 31 | + private EncryptionUtils() {} |
| 32 | + |
| 33 | + public static Optional<InternalFileDecryptor> createDecryptor(ParquetReaderOptions parquetReaderOptions, Location filePath, TrinoFileSystem trinoFileSystem) |
| 34 | + { |
| 35 | + if (parquetReaderOptions == null || filePath == null || trinoFileSystem == null) { |
| 36 | + return Optional.empty(); |
| 37 | + } |
| 38 | + |
| 39 | + Optional<TrinoDecryptionPropertiesFactory> cryptoFactory = loadDecryptionPropertiesFactory(parquetReaderOptions); |
| 40 | + Optional<FileDecryptionProperties> fileDecryptionProperties = cryptoFactory.map(factory -> factory.getFileDecryptionProperties(parquetReaderOptions, filePath, trinoFileSystem)); |
| 41 | + return fileDecryptionProperties.map(properties -> new InternalFileDecryptor(properties)); |
| 42 | + } |
| 43 | + |
| 44 | + private static Optional<TrinoDecryptionPropertiesFactory> loadDecryptionPropertiesFactory(ParquetReaderOptions trinoParquetCryptoConfig) |
| 45 | + { |
| 46 | + if (trinoParquetCryptoConfig.getCryptoFactoryClass() == null) { |
| 47 | + return Optional.empty(); |
| 48 | + } |
| 49 | + final Class<?> foundClass = TrinoCryptoConfigurationUtil.getClassFromConfig( |
| 50 | + trinoParquetCryptoConfig.getCryptoFactoryClass(), TrinoDecryptionPropertiesFactory.class); |
| 51 | + |
| 52 | + if (foundClass == null) { |
| 53 | + return Optional.empty(); |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + return Optional.ofNullable((TrinoDecryptionPropertiesFactory) foundClass.getConstructor().newInstance()); |
| 58 | + } |
| 59 | + catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { |
| 60 | + LOG.warn("could not instantiate decryptionPropertiesFactoryClass class: " + foundClass, e); |
| 61 | + return Optional.empty(); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments