-
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 all 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
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
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
| /* | ||
| * 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.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 = config | ||
| .toBuilder() | ||
|
Comment on lines
+49
to
+50
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super! |
||
| .translogFactory(cryptoTranslogFactory) // <- Replace with our crypto factory | ||
| .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()) | ||
| ); | ||
| } | ||
|
|
||
| } | ||
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
58 changes: 58 additions & 0 deletions
58
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,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); | ||
| } | ||
| } |
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 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.