|
19 | 19 |
|
20 | 20 | package org.elasticsearch.repositories.azure;
|
21 | 21 |
|
| 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; |
22 | 28 | import org.elasticsearch.action.support.master.AcknowledgedResponse;
|
23 | 29 | import org.elasticsearch.common.Strings;
|
| 30 | +import org.elasticsearch.common.collect.Tuple; |
24 | 31 | import org.elasticsearch.common.settings.MockSecureSettings;
|
25 | 32 | import org.elasticsearch.common.settings.SecureSettings;
|
26 | 33 | import org.elasticsearch.common.settings.Settings;
|
27 | 34 | import org.elasticsearch.plugins.Plugin;
|
28 | 35 | import org.elasticsearch.repositories.AbstractThirdPartyRepositoryTestCase;
|
| 36 | +import org.elasticsearch.repositories.blobstore.BlobStoreRepository; |
29 | 37 |
|
| 38 | +import java.net.HttpURLConnection; |
30 | 39 | import java.util.Collection;
|
| 40 | +import java.util.function.Supplier; |
31 | 41 |
|
32 | 42 | import static org.hamcrest.Matchers.blankOrNullString;
|
33 | 43 | import static org.hamcrest.Matchers.equalTo;
|
@@ -71,5 +81,32 @@ protected void createRepository(String repoName) {
|
71 | 81 | .put("base_path", System.getProperty("test.azure.base"))
|
72 | 82 | ).get();
|
73 | 83 | 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(); |
74 | 111 | }
|
75 | 112 | }
|
0 commit comments