Skip to content
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

[Backport 2.x] Implement missing methods for EncryptedBlobStore and EncryptedBlobContainer #14114

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.opensearch.common.blobstore;

import org.opensearch.common.CheckedBiConsumer;
import org.opensearch.common.annotation.ExperimentalApi;
import org.opensearch.common.crypto.CryptoHandler;
import org.opensearch.common.crypto.DecryptedRangedStreamProvider;
import org.opensearch.common.crypto.EncryptedHeaderContentSupplier;
Expand Down Expand Up @@ -50,6 +51,14 @@ public InputStream readBlob(String blobName) throws IOException {
return cryptoHandler.createDecryptingStream(inputStream);
}

@ExperimentalApi
@Override
public InputStreamWithMetadata readBlobWithMetadata(String blobName) throws IOException {
InputStreamWithMetadata inputStreamWithMetadata = blobContainer.readBlobWithMetadata(blobName);
InputStream decryptInputStream = cryptoHandler.createDecryptingStream(inputStreamWithMetadata.getInputStream());
return new InputStreamWithMetadata(decryptInputStream, inputStreamWithMetadata.getMetadata());
}

EncryptedHeaderContentSupplier getEncryptedHeaderContentSupplier(String blobName) {
return (start, end) -> {
byte[] buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public Map<Metric, Map<String, Long>> extendedStats() {
return blobStore.extendedStats();
}

@Override
public boolean isBlobMetadataEnabled() {
return blobStore.isBlobMetadataEnabled();
}

/**
* Closes the EncryptedBlobStore by decrementing the reference count of the CryptoManager and closing the
* underlying BlobStore. This ensures proper cleanup of resources.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.blobstore;

import org.opensearch.common.crypto.CryptoHandler;
import org.opensearch.test.OpenSearchTestCase;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class EncryptedBlobContainerTests extends OpenSearchTestCase {

public void testBlobContainerReadBlobWithMetadata() throws IOException {
BlobContainer blobContainer = mock(BlobContainer.class);
CryptoHandler cryptoHandler = mock(CryptoHandler.class);
EncryptedBlobContainer encryptedBlobContainer = new EncryptedBlobContainer(blobContainer, cryptoHandler);
InputStreamWithMetadata inputStreamWithMetadata = new InputStreamWithMetadata(
new ByteArrayInputStream(new byte[0]),
new HashMap<>()
);
when(blobContainer.readBlobWithMetadata("test")).thenReturn(inputStreamWithMetadata);
InputStream decrypt = new ByteArrayInputStream(new byte[2]);
when(cryptoHandler.createDecryptingStream(inputStreamWithMetadata.getInputStream())).thenReturn(decrypt);
InputStreamWithMetadata result = encryptedBlobContainer.readBlobWithMetadata("test");
assertEquals(result.getInputStream(), decrypt);
assertEquals(result.getMetadata(), inputStreamWithMetadata.getMetadata());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void testTranslogMetadataAllowedTrueWithMinVersionNewer() {
when(blobStoreMock.isBlobMetadataEnabled()).thenReturn(true);
RemoteStoreCustomMetadataResolver resolver = new RemoteStoreCustomMetadataResolver(
remoteStoreSettings,
() -> Version.CURRENT,
() -> Version.V_2_15_0,
() -> repositoriesService,
settings
);
Expand All @@ -200,7 +200,7 @@ public void testTranslogMetadataAllowedFalseWithMinVersionNewer() {
RemoteStoreSettings remoteStoreSettings = new RemoteStoreSettings(settings, clusterSettings);
RemoteStoreCustomMetadataResolver resolver = new RemoteStoreCustomMetadataResolver(
remoteStoreSettings,
() -> Version.CURRENT,
() -> Version.V_2_15_0,
() -> repositoriesService,
settings
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ private static Metadata createIndexMetadataWithRemoteStoreSettings(String indexN
Settings.builder()
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
.put(IndexMetadata.SETTING_VERSION_CREATED, Version.V_2_15_0)
.put(IndexMetadata.INDEX_REMOTE_STORE_ENABLED_SETTING.getKey(), true)
.put(IndexMetadata.INDEX_REMOTE_TRANSLOG_REPOSITORY_SETTING.getKey(), "dummy-tlog-repo")
.put(IndexMetadata.INDEX_REMOTE_SEGMENT_STORE_REPOSITORY_SETTING.getKey(), "dummy-segment-repo")
Expand Down
Loading