Skip to content

Commit

Permalink
Implement missing methods for EncryptedBlobStore and EncryptedBlobCon…
Browse files Browse the repository at this point in the history
…tainer (opensearch-project#14030)

Signed-off-by: Sandeep Kumawat <[email protected]>
  • Loading branch information
skumawat2025 authored Jun 10, 2024
1 parent b0513dd commit 270054c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
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

0 comments on commit 270054c

Please sign in to comment.