Skip to content

Commit

Permalink
Add storage emulator support (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
timja authored May 13, 2021
1 parent 4da08e7 commit fa287ac
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand All @@ -41,6 +42,9 @@
*/
public class AzureStorageAccount extends BaseStandardCredentials {

// used to detect the emulator
private static final List<String> LOCAL_ADDRESSES = Arrays.asList("127.0.0.1", "localhost", "::1", "0.0.0.0");

public static class StorageAccountCredential implements java.io.Serializable {

private final String storageAccountName;
Expand All @@ -57,7 +61,24 @@ public StorageAccountCredential(
if (StringUtils.isBlank(endpointURL)) {
url = Constants.DEF_BLOB_URL;
}
this.blobEndpointURL = url;
this.blobEndpointURL = joinAccountNameAndEndpoint(storageAccountName, url);
}

/**
* The old SDK worked with 'endpoint suffixes' in the form http(s)://blob.core.windows.net.
* New SDK uses endpoints: https://my-account-name.blob.core.windows.net.
*
* UI still stores the suffix so we need to join them, unless it's already added or the emulator is in use
*/
@SuppressWarnings("HttpUrlsUsage")
private static String joinAccountNameAndEndpoint(String accountName, String urlSuffix) {
if (urlSuffix.contains(accountName) || LOCAL_ADDRESSES.stream().anyMatch(urlSuffix::contains)) {
return urlSuffix;
}

return urlSuffix
.replace("http://", "https://")
.replace("https://", String.format("https://%s.", accountName));
}

public StorageAccountCredential() {
Expand Down Expand Up @@ -95,7 +116,8 @@ protected Secret getSecureKey() {
}

public String getEndpointURL() {
return blobEndpointURL;
// joined in getter as constructor isn't called when reading saved configuration
return joinAccountNameAndEndpoint(storageAccountName, blobEndpointURL);
}

public String getId() {
Expand Down Expand Up @@ -191,7 +213,7 @@ public String getStorageKey() {
}

public String getBlobEndpointURL() {
return storageData.blobEndpointURL;
return storageData.getEndpointURL();
}

public StorageAccountCredential getStorageCred() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public static ShareServiceClient getShareClient(final StorageAccountInfo storage
.credential(new StorageSharedKeyCredential(storageAccount.getStorageAccName(),
storageAccount.getStorageAccountKey()))
.httpClient(HttpClientRetriever.get())
.endpoint(joinAccountNameAndEndpoint(storageAccount.getStorageAccName(),
storageAccount.getBlobEndPointURL().replace("blob", "file")))
.endpoint(storageAccount.getBlobEndPointURL()
.replace("blob", "file")) // TODO add file endpoint
.buildClient();
}

Expand All @@ -98,25 +98,11 @@ public static BlobServiceClient getCloudStorageAccount(
.credential(new StorageSharedKeyCredential(storageAccount.getStorageAccName(),
storageAccount.getStorageAccountKey()))
.httpClient(HttpClientRetriever.get())
.endpoint(joinAccountNameAndEndpoint(storageAccount.getStorageAccName(),
storageAccount.getBlobEndPointURL()))
.endpoint(storageAccount.getBlobEndPointURL())
.retryOptions(retryOptions)
.buildClient();
}

/**
* The old SDK worked with 'endpoint suffixes' in the form http(s)://blob.core.windows.net.
* New SDK uses endpoints: https://my-account-name.blob.core.windows.net.
*
* UI still stores the suffix so we need to join them
*/
@SuppressWarnings("HttpUrlsUsage")
private static String joinAccountNameAndEndpoint(String accountName, String urlSuffix) {
return urlSuffix
.replace("http://", "https://")
.replace("https://", String.format("https://%s.", accountName));
}

public static BlobContainerClient getBlobContainerReference(StorageAccountInfo storageAccount,
String containerName,
boolean createIfNotExist,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void run() {
String uploadedFileHash = uploadCloudFile(uploadItem, filePath);
azureBlob = new AzureBlob(
uploadItem.getShareName(),
uploadItem.getFileUrl().replace("http://", "https://"),
uploadItem.getFileUrl(),
uploadedFileHash,
filePath.length(),
Constants.FILE_STORAGE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ private UploadObject generateUploadObject(FilePath path, StorageAccountInfo acco
Map<String, String> metadata) throws Exception {
String sas = generateWriteSASURL(accountInfo, blob.getBlobName(),
Constants.BLOB_STORAGE, containerName);
String blobURL = blob.getBlobUrl().replace("http://", "https://");

return new UploadObject(blob.getBlobName(), path, blobURL, sas, Constants.BLOB_STORAGE,
return new UploadObject(blob.getBlobName(), path, blob.getBlobUrl(), sas, Constants.BLOB_STORAGE,
blob.getAccountName(), blobProperties,
metadata);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;

public class ConfigAsCodeTest {
Expand All @@ -28,11 +29,11 @@ public void should_support_configuration_as_code() {
AzureStorageAccount credentials = (AzureStorageAccount) SystemCredentialsProvider.getInstance().getCredentials()
.get(0);

assertEquals(credentials.getScope(), CredentialsScope.GLOBAL);
assertEquals(credentials.getDescription(), "Account");
assertEquals(credentials.getStorageAccountName(), "a-storage-account");
assertEquals(credentials.getBlobEndpointURL(), "https://blob.core.windows.net/");
assertEquals(credentials.getId(), "storage-account");
assertThat(credentials.getScope(), is(CredentialsScope.GLOBAL));
assertThat(credentials.getDescription(), is("Account"));
assertThat(credentials.getStorageAccountName(), is("a-storage-account"));
assertThat(credentials.getBlobEndpointURL(), is("https://a-storage-account.blob.core.windows.net/"));
assertThat(credentials.getId(), is("storage-account"));
assertNotNull(credentials.getStorageKey());
}

Expand Down Expand Up @@ -60,11 +61,11 @@ public void export_configuration() throws Exception {
.get("azureStorageAccount")
.asMapping();

assertEquals(mapping.getScalarValue("scope"), "GLOBAL");
assertEquals(mapping.getScalarValue("description"), "Account");
assertEquals(mapping.getScalarValue("storageAccountName"), "a-storage-account");
assertEquals(mapping.getScalarValue("blobEndpointURL"), "https://blob.core.windows.net/");
assertEquals(mapping.getScalarValue("id"), "storage-account");
assertThat(mapping.getScalarValue("scope"), is("GLOBAL"));
assertThat(mapping.getScalarValue("description"), is("Account"));
assertThat(mapping.getScalarValue("storageAccountName"), is("a-storage-account"));
assertThat(mapping.getScalarValue("blobEndpointURL"), is("https://a-storage-account.blob.core.windows.net/"));
assertThat(mapping.getScalarValue("id"), is("storage-account"));
assertNotNull(mapping.getScalarValue("storageKey"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void testRenameStorageConfig() throws Exception {

assertEquals(1, s.getCredentials(Domain.global()).size());

AzureStorageAccount.StorageAccountCredential u = new AzureStorageAccount.StorageAccountCredential(storageAccount, storageAccountKey, storageBlobURL);
AzureStorageAccount.StorageAccountCredential expected = new AzureStorageAccount.StorageAccountCredential(
storageAccount, storageAccountKey, storageBlobURL);

List<AzureStorageAccount> azureStorageAccounts = CredentialsProvider.lookupCredentials(
AzureStorageAccount.class,
Expand All @@ -42,8 +43,8 @@ public void testRenameStorageConfig() throws Exception {
assertEquals(1, azureStorageAccounts.size());
AzureStorageAccount storageCred = azureStorageAccounts.get(0);

assertEquals(u.getStorageAccountName(), storageCred.getStorageAccountName());
assertEquals(u.getEndpointURL(), storageCred.getBlobEndpointURL());
assertEquals(u.getSecureKey().getPlainText(), storageCred.getPlainStorageKey());
assertEquals(expected.getStorageAccountName(), storageCred.getStorageAccountName());
assertEquals(expected.getEndpointURL(), storageCred.getBlobEndpointURL());
assertEquals(expected.getSecureKey().getPlainText(), storageCred.getPlainStorageKey());
}
}

0 comments on commit fa287ac

Please sign in to comment.