-
Notifications
You must be signed in to change notification settings - Fork 15
Add translog encryption #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.opensearch.index.store; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Path; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.apache.lucene.store.FSDirectory; | ||
| import org.opensearch.index.codec.CodecService; | ||
| import org.opensearch.index.engine.Engine; | ||
| import org.opensearch.index.engine.EngineConfig; | ||
| import org.opensearch.index.engine.EngineFactory; | ||
| import org.opensearch.index.engine.InternalEngine; | ||
| import org.opensearch.index.store.iv.DefaultKeyIvResolver; | ||
| import org.opensearch.index.store.iv.KeyIvResolver; | ||
| import org.opensearch.index.translog.CryptoTranslogFactory; | ||
|
|
||
| /** | ||
| * A factory that creates engines with crypto-enabled translogs for cryptofs indices. | ||
| */ | ||
| public class CryptoEngineFactory implements EngineFactory { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(CryptoEngineFactory.class); | ||
|
|
||
| /** | ||
| * Default constructor. | ||
| */ | ||
| public CryptoEngineFactory() {} | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public Engine newReadWriteEngine(EngineConfig config) { | ||
|
|
||
| try { | ||
| // Create a separate KeyIvResolver for translog encryption | ||
| KeyIvResolver keyIvResolver = createTranslogKeyIvResolver(config); | ||
|
|
||
| // Create the crypto translog factory using the same KeyIvResolver as the directory | ||
| CryptoTranslogFactory cryptoTranslogFactory = new CryptoTranslogFactory(keyIvResolver); | ||
|
|
||
| // Create new engine config by copying all fields from existing config | ||
| // but replace the translog factory with our crypto version | ||
| EngineConfig cryptoConfig = new EngineConfig.Builder() | ||
|
||
| .shardId(config.getShardId()) | ||
| .threadPool(config.getThreadPool()) | ||
| .indexSettings(config.getIndexSettings()) | ||
| .warmer(config.getWarmer()) | ||
| .store(config.getStore()) | ||
| .mergePolicy(config.getMergePolicy()) | ||
| .analyzer(config.getAnalyzer()) | ||
| .similarity(config.getSimilarity()) | ||
| .codecService(getCodecService(config)) | ||
| .eventListener(config.getEventListener()) | ||
| .queryCache(config.getQueryCache()) | ||
| .queryCachingPolicy(config.getQueryCachingPolicy()) | ||
| .translogConfig(config.getTranslogConfig()) | ||
| .translogDeletionPolicyFactory(config.getCustomTranslogDeletionPolicyFactory()) | ||
| .flushMergesAfter(config.getFlushMergesAfter()) | ||
| .externalRefreshListener(config.getExternalRefreshListener()) | ||
| .internalRefreshListener(config.getInternalRefreshListener()) | ||
| .indexSort(config.getIndexSort()) | ||
| .circuitBreakerService(config.getCircuitBreakerService()) | ||
| .globalCheckpointSupplier(config.getGlobalCheckpointSupplier()) | ||
| .retentionLeasesSupplier(config.retentionLeasesSupplier()) | ||
| .primaryTermSupplier(config.getPrimaryTermSupplier()) | ||
| .tombstoneDocSupplier(config.getTombstoneDocSupplier()) | ||
| .readOnlyReplica(config.isReadOnlyReplica()) | ||
| .startedPrimarySupplier(config.getStartedPrimarySupplier()) | ||
| .translogFactory(cryptoTranslogFactory) // <- Replace with our crypto factory | ||
| .leafSorter(config.getLeafSorter()) | ||
| .documentMapperForTypeSupplier(config.getDocumentMapperForTypeSupplier()) | ||
| .indexReaderWarmer(config.getIndexReaderWarmer()) | ||
| .clusterApplierService(config.getClusterApplierService()) | ||
| .build(); | ||
|
|
||
| // Return the default engine with crypto-enabled translog | ||
| return new InternalEngine(cryptoConfig); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Failed to create crypto engine", e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create a separate KeyIvResolver for translog encryption. | ||
| */ | ||
| private KeyIvResolver createTranslogKeyIvResolver(EngineConfig config) throws IOException { | ||
| // Create a separate key resolver for translog files | ||
|
|
||
| // Use the translog location for key storage | ||
| Path translogPath = config.getTranslogConfig().getTranslogPath(); | ||
| Directory keyDirectory = FSDirectory.open(translogPath); | ||
|
|
||
| // Create crypto directory factory to get the key provider | ||
| CryptoDirectoryFactory directoryFactory = new CryptoDirectoryFactory(); | ||
|
|
||
| // Create a dedicated key resolver for translog | ||
| return new DefaultKeyIvResolver( | ||
| keyDirectory, | ||
| config.getIndexSettings().getValue(CryptoDirectoryFactory.INDEX_CRYPTO_PROVIDER_SETTING), | ||
| directoryFactory.getKeyProvider(config.getIndexSettings()) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Helper method to create a CodecService from existing EngineConfig. | ||
| * Since EngineConfig doesn't expose CodecService directly, we create a new one | ||
| * using the same IndexSettings. | ||
| */ | ||
| private CodecService getCodecService(EngineConfig config) { | ||
| // Create a CodecService using the same IndexSettings as the original config | ||
| // We pass null for MapperService and use a simple logger since we're just | ||
| // preserving the existing codec behavior | ||
| return new CodecService(null, config.getIndexSettings(), logger); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.opensearch.index.translog; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.channels.FileChannel; | ||
| import java.nio.file.OpenOption; | ||
| import java.nio.file.Path; | ||
| import java.util.Set; | ||
|
|
||
| import org.opensearch.index.store.iv.KeyIvResolver; | ||
|
|
||
| /** | ||
| * A ChannelFactory implementation that creates FileChannels with transparent | ||
| * AES-GCM encryption/decryption for translog files. | ||
| * | ||
| * This factory determines whether to apply encryption based on the file extension: | ||
| * - .tlog files: Encrypted using AES-GCM with 8KB authenticated chunks | ||
| * - .ckp files: Not encrypted (checkpoint metadata) | ||
| * | ||
| * Updated to use unified KeyIvResolver (same as index files) for consistent | ||
| * key management across all encrypted components. | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public class CryptoChannelFactory implements ChannelFactory { | ||
|
|
||
| private final KeyIvResolver keyIvResolver; | ||
| private final String translogUUID; | ||
|
|
||
| /** | ||
| * Creates a new CryptoChannelFactory. | ||
| * | ||
| * @param keyIvResolver the key and IV resolver for encryption keys (unified with index files) | ||
| * @param translogUUID the translog UUID for exact header size calculation | ||
| */ | ||
| public CryptoChannelFactory(KeyIvResolver keyIvResolver, String translogUUID) { | ||
| if (translogUUID == null) { | ||
| throw new IllegalArgumentException("translogUUID is required for exact header size calculation"); | ||
| } | ||
| this.keyIvResolver = keyIvResolver; | ||
| this.translogUUID = translogUUID; | ||
| } | ||
|
|
||
| @Override | ||
| public FileChannel open(Path path, OpenOption... options) throws IOException { | ||
| FileChannel baseChannel = FileChannel.open(path, options); | ||
|
|
||
| if (!path.getFileName().toString().endsWith(".tlog")) { | ||
| return baseChannel; | ||
| } | ||
|
|
||
| Set<OpenOption> optionsSet = Set.of(options); | ||
| return new CryptoFileChannelWrapper(baseChannel, keyIvResolver, path, optionsSet, translogUUID); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you sync with the latest changes on the main branch? Not sure why this is showing here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, we were using 3.1.0 snapshot earlier, but for translog once the core changes are merged we will have to use 3.2.0 here.