-
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
Merged
kumargu
merged 6 commits into
opensearch-project:main
from
RajatGupta02:encrypt-translog
Aug 28, 2025
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/java/org/opensearch/index/store/CryptoEngineFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package org.opensearch.index.store; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| 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.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 | ||
| java.nio.file.Path translogPath = config.getTranslogConfig().getTranslogPath(); | ||
| org.apache.lucene.store.Directory keyDirectory = new org.apache.lucene.store.NIOFSDirectory(translogPath); | ||
RajatGupta02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Create crypto directory factory to get the key provider | ||
| CryptoDirectoryFactory directoryFactory = new CryptoDirectoryFactory(); | ||
|
|
||
| // Create a dedicated key resolver for translog | ||
| return new org.opensearch.index.store.iv.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 org.opensearch.index.codec.CodecService getCodecService(EngineConfig config) { | ||
RajatGupta02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 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 org.opensearch.index.codec.CodecService( | ||
| null, | ||
| config.getIndexSettings(), | ||
| org.apache.logging.log4j.LogManager.getLogger(CryptoEngineFactory.class) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/org/opensearch/index/translog/CryptoChannelFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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.Arrays; | ||
| import java.util.HashSet; | ||
| 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 { | ||
| // Create the base FileChannel | ||
| FileChannel baseChannel = FileChannel.open(path, options); | ||
|
|
||
| // Determine if this file should be encrypted | ||
| String fileName = path.getFileName().toString(); | ||
| boolean shouldEncrypt = fileName.endsWith(".tlog"); | ||
|
|
||
| if (shouldEncrypt) { | ||
| // Wrap with crypto functionality using unified key resolver and exact UUID | ||
| Set<OpenOption> optionsSet = new HashSet<>(Arrays.asList(options)); | ||
| return new CryptoFileChannelWrapper(baseChannel, keyIvResolver, path, optionsSet, translogUUID); | ||
| } else { | ||
| // Return unwrapped channel for non-encrypted files | ||
| return baseChannel; | ||
| } | ||
| } | ||
RajatGupta02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 we just not override (the builder should allow) the
cryptoTranslogFactoryto an existing config rather than copying over all these configs?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.
That was the issue, EngineConfig.java doesn't have any copy constructor :/