Skip to content

Commit b00de8e

Browse files
Ensure SAS Tokens in Test Use Minimal Permissions (#46112) (#46628)
There were some issues with the Azure implementation requiring permissions to list all containers ue to a container exists check. This was caught in CI this time, but going forward we should ensure that CI is executed using a token that does not allow listing containers. Relates #43288
1 parent 92e518e commit b00de8e

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

plugins/repository-azure/src/main/java/org/elasticsearch/repositories/azure/AzureBlobStore.java

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public String toString() {
6868
return container;
6969
}
7070

71+
public AzureStorageService getService() {
72+
return service;
73+
}
74+
7175
/**
7276
* Gets the configured {@link LocationMode} for the Azure storage requests.
7377
*/

plugins/repository-azure/src/test/java/org/elasticsearch/repositories/azure/AzureStorageCleanupThirdPartyTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,25 @@
1919

2020
package org.elasticsearch.repositories.azure;
2121

22+
import com.microsoft.azure.storage.OperationContext;
23+
import com.microsoft.azure.storage.StorageException;
24+
import com.microsoft.azure.storage.blob.CloudBlobClient;
25+
import com.microsoft.azure.storage.blob.CloudBlobContainer;
26+
import org.elasticsearch.action.ActionRunnable;
27+
import org.elasticsearch.action.support.PlainActionFuture;
2228
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2329
import org.elasticsearch.common.Strings;
30+
import org.elasticsearch.common.collect.Tuple;
2431
import org.elasticsearch.common.settings.MockSecureSettings;
2532
import org.elasticsearch.common.settings.SecureSettings;
2633
import org.elasticsearch.common.settings.Settings;
2734
import org.elasticsearch.plugins.Plugin;
2835
import org.elasticsearch.repositories.AbstractThirdPartyRepositoryTestCase;
36+
import org.elasticsearch.repositories.blobstore.BlobStoreRepository;
2937

38+
import java.net.HttpURLConnection;
3039
import java.util.Collection;
40+
import java.util.function.Supplier;
3141

3242
import static org.hamcrest.Matchers.blankOrNullString;
3343
import static org.hamcrest.Matchers.equalTo;
@@ -71,5 +81,32 @@ protected void createRepository(String repoName) {
7181
.put("base_path", System.getProperty("test.azure.base"))
7282
).get();
7383
assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));
84+
if (Strings.hasText(System.getProperty("test.azure.sas_token"))) {
85+
ensureSasTokenPermissions();
86+
}
87+
}
88+
89+
private void ensureSasTokenPermissions() {
90+
final BlobStoreRepository repository = getRepository();
91+
final PlainActionFuture<Void> future = PlainActionFuture.newFuture();
92+
repository.threadPool().generic().execute(ActionRunnable.wrap(future, l -> {
93+
final AzureBlobStore blobStore = (AzureBlobStore) repository.blobStore();
94+
final String account = "default";
95+
final Tuple<CloudBlobClient, Supplier<OperationContext>> client = blobStore.getService().client(account);
96+
final CloudBlobContainer blobContainer = client.v1().getContainerReference(blobStore.toString());
97+
try {
98+
SocketAccess.doPrivilegedException(() -> blobContainer.exists(null, null, client.v2().get()));
99+
future.onFailure(new RuntimeException(
100+
"The SAS token used in this test allowed for checking container existence. This test only supports tokens " +
101+
"that grant only the documented permission requirements for the Azure repository plugin."));
102+
} catch (StorageException e) {
103+
if (e.getHttpStatusCode() == HttpURLConnection.HTTP_FORBIDDEN) {
104+
future.onResponse(null);
105+
} else {
106+
future.onFailure(e);
107+
}
108+
}
109+
}));
110+
future.actionGet();
74111
}
75112
}

test/framework/src/main/java/org/elasticsearch/repositories/AbstractThirdPartyRepositoryTestCase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ protected void doRun() throws Exception {
302302
return future.actionGet();
303303
}
304304

305-
private BlobStoreRepository getRepository() {
305+
protected BlobStoreRepository getRepository() {
306306
return (BlobStoreRepository) getInstanceFromNode(RepositoriesService.class).repository("test-repo");
307307
}
308308
}

0 commit comments

Comments
 (0)