JCLOUDS-1557 - Azure local server support

Co-authored-by: David Sloan <david.sloan@lenses.io>
This commit is contained in:
davidsloan 2020-12-08 02:14:03 +00:00 committed by GitHub
parent 3a7e41f4e2
commit 17fd80cd5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 416 additions and 33 deletions

View File

@ -21,14 +21,12 @@ import static com.google.common.io.ByteStreams.readBytes;
import static org.jclouds.crypto.Macs.asByteProcessor; import static org.jclouds.crypto.Macs.asByteProcessor;
import static org.jclouds.util.Patterns.NEWLINE_PATTERN; import static org.jclouds.util.Patterns.NEWLINE_PATTERN;
import static org.jclouds.util.Strings2.toInputStream; import static org.jclouds.util.Strings2.toInputStream;
import org.jclouds.http.Uris.UriBuilder;
import java.net.URI;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.jclouds.http.Uris;
import java.net.URI;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Inject; import javax.inject.Inject;
@ -36,12 +34,8 @@ import javax.inject.Named;
import javax.inject.Provider; import javax.inject.Provider;
import javax.inject.Singleton; import javax.inject.Singleton;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.azure.storage.util.storageurl.StorageUrlSupplier;
import org.jclouds.crypto.Crypto; import org.jclouds.crypto.Crypto;
import org.jclouds.date.TimeStamp; import org.jclouds.date.TimeStamp;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
@ -49,16 +43,23 @@ import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter; import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils; import org.jclouds.http.HttpUtils;
import org.jclouds.http.Uris;
import org.jclouds.http.Uris.UriBuilder;
import org.jclouds.http.internal.SignatureWire; import org.jclouds.http.internal.SignatureWire;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.util.Strings2; import org.jclouds.util.Strings2;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder; import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps; import com.google.common.collect.Multimaps;
import com.google.common.io.ByteProcessor; import com.google.common.io.ByteProcessor;
import com.google.common.net.HttpHeaders; import com.google.common.net.HttpHeaders;
@ -76,7 +77,6 @@ public class SharedKeyLiteAuthentication implements HttpRequestFilter {
private final Provider<String> timeStampProvider; private final Provider<String> timeStampProvider;
private final Crypto crypto; private final Crypto crypto;
private final String credential; private final String credential;
private final String identity;
private final HttpUtils utils; private final HttpUtils utils;
private final URI storageUrl; private final URI storageUrl;
private final boolean isSAS; private final boolean isSAS;
@ -88,13 +88,13 @@ public class SharedKeyLiteAuthentication implements HttpRequestFilter {
@Inject @Inject
public SharedKeyLiteAuthentication(SignatureWire signatureWire, public SharedKeyLiteAuthentication(SignatureWire signatureWire,
@org.jclouds.location.Provider Supplier<Credentials> creds, @TimeStamp Provider<String> timeStampProvider, @org.jclouds.location.Provider Supplier<Credentials> creds, @TimeStamp Provider<String> timeStampProvider,
Crypto crypto, HttpUtils utils, @Named("sasAuth") boolean sasAuthentication) { Crypto crypto, HttpUtils utils, @Named("sasAuth") boolean sasAuthentication,
StorageUrlSupplier storageUrlSupplier) {
this.crypto = crypto; this.crypto = crypto;
this.utils = utils; this.utils = utils;
this.signatureWire = signatureWire; this.signatureWire = signatureWire;
this.storageUrl = URI.create("https://" + creds.get().identity + ".blob.core.windows.net/"); this.storageUrl = storageUrlSupplier.get();
this.creds = creds; this.creds = creds;
this.identity = creds.get().identity;
this.credential = creds.get().credential; this.credential = creds.get().credential;
this.timeStampProvider = timeStampProvider; this.timeStampProvider = timeStampProvider;
this.isSAS = sasAuthentication; this.isSAS = sasAuthentication;

View File

@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.azure.storage.util.storageurl;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import org.jclouds.domain.Credentials;
import org.jclouds.location.Provider;
import javax.inject.Inject;
import javax.inject.Singleton;
import static org.jclouds.azure.storage.util.storageurl.TrailingSlashUtil.ensureTrailingSlash;
import java.net.URI;
@Singleton
public class AppendAccountToEndpoint implements StorageUrlSupplier {
private final Supplier<URI> endpointSupplier;
private final Supplier<Credentials> credentialsSupplier;
@Inject
public AppendAccountToEndpoint(@Provider Supplier<URI> endpointSupplier, @Provider Supplier<Credentials> credentialsSupplier) {
this.endpointSupplier = endpointSupplier;
this.credentialsSupplier = credentialsSupplier;
}
@Override
public URI get() {
URI endpoint = endpointSupplier.get();
Preconditions.checkNotNull(endpoint, "An endpoint must be configured in order to use AppendAccountToEndpoint module");
String endpointTrailingSlash = new StringBuilder(ensureTrailingSlash(endpoint))
.append(credentialsSupplier.get().identity)
.append("/")
.toString();
return URI.create(endpointTrailingSlash);
}
}

View File

@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.azure.storage.util.storageurl;
import com.google.common.base.Supplier;
import org.jclouds.domain.Credentials;
import org.jclouds.location.Provider;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.net.URI;
import static org.jclouds.azure.storage.util.storageurl.TrailingSlashUtil.ensureTrailingSlash;
@Singleton
public class StorageAccountInVhost implements StorageUrlSupplier {
private final Supplier<URI> endpointSupplier;
private final Supplier<Credentials> credentialsSupplier;
@Inject
public StorageAccountInVhost(@Provider Supplier<URI> endpointSupplier, @Provider Supplier<Credentials> credentialsSupplier) {
this.endpointSupplier = endpointSupplier;
this.credentialsSupplier = credentialsSupplier;
}
@Override
public URI get() {
URI endpoint = endpointSupplier.get();
String uri = endpoint == null ? buildUri() : ensureTrailingSlash(endpoint);
return URI.create(uri);
}
private String buildUri() {
return "https://" + credentialsSupplier.get().identity + ".blob.core.windows.net/";
}
}

View File

@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.azure.storage.util.storageurl;
import com.google.common.base.Supplier;
import com.google.inject.ImplementedBy;
import java.net.URI;
@ImplementedBy(StorageAccountInVhost.class)
public interface StorageUrlSupplier extends Supplier<URI> {
}

View File

@ -0,0 +1,28 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.azure.storage.util.storageurl;
import java.net.URI;
public class TrailingSlashUtil {
static String ensureTrailingSlash(URI endpointUri) {
String endpoint = endpointUri.toString();
return endpoint.endsWith("/") ? endpoint : endpoint + "/";
}
}

View File

@ -21,11 +21,13 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.net.URI; import java.net.URI;
import java.util.Date; import java.util.Date;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.azure.storage.filters.SharedKeyLiteAuthentication; import org.jclouds.azure.storage.filters.SharedKeyLiteAuthentication;
import org.jclouds.azure.storage.util.storageurl.StorageUrlSupplier;
import org.jclouds.blobstore.BlobRequestSigner; import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.domain.Blob; import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.functions.BlobToHttpGetOptions; import org.jclouds.blobstore.functions.BlobToHttpGetOptions;
@ -36,6 +38,7 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.Uris; import org.jclouds.http.Uris;
import org.jclouds.http.options.GetOptions; import org.jclouds.http.options.GetOptions;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.net.HttpHeaders; import com.google.common.net.HttpHeaders;
import com.google.inject.Provider; import com.google.inject.Provider;
@ -59,11 +62,12 @@ public class AzureBlobRequestSigner implements BlobRequestSigner {
public AzureBlobRequestSigner( public AzureBlobRequestSigner(
BlobToHttpGetOptions blob2HttpGetOptions, @TimeStamp Provider<String> timeStampProvider, BlobToHttpGetOptions blob2HttpGetOptions, @TimeStamp Provider<String> timeStampProvider,
DateService dateService, SharedKeyLiteAuthentication auth, DateService dateService, SharedKeyLiteAuthentication auth,
@org.jclouds.location.Provider Supplier<Credentials> creds, @Named("sasAuth") boolean sasAuthentication) @org.jclouds.location.Provider Supplier<Credentials> creds, @Named("sasAuth") boolean sasAuthentication,
StorageUrlSupplier storageUriSupplier)
throws SecurityException, NoSuchMethodException { throws SecurityException, NoSuchMethodException {
this.identity = creds.get().identity; this.identity = creds.get().identity;
this.credential = creds.get().credential; this.credential = creds.get().credential;
this.storageUrl = URI.create("https://" + creds.get().identity + ".blob.core.windows.net/"); this.storageUrl = storageUriSupplier.get();
this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions"); this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions");
this.timeStampProvider = checkNotNull(timeStampProvider, "timeStampProvider"); this.timeStampProvider = checkNotNull(timeStampProvider, "timeStampProvider");
this.dateService = checkNotNull(dateService, "dateService"); this.dateService = checkNotNull(dateService, "dateService");

View File

@ -16,27 +16,24 @@
*/ */
package org.jclouds.azureblob.blobstore.config; package org.jclouds.azureblob.blobstore.config;
import java.util.concurrent.TimeUnit;
import javax.inject.Singleton;
import javax.inject.Named;
import org.jclouds.azureblob.AzureBlobClient;
import org.jclouds.azureblob.blobstore.AzureBlobRequestSigner;
import org.jclouds.azureblob.blobstore.AzureBlobStore;
import org.jclouds.azureblob.domain.PublicAccess;
import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.attr.ConsistencyModel;
import org.jclouds.azureblob.config.InsufficientAccessRightsException;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.Scopes; import com.google.inject.Scopes;
import org.jclouds.azureblob.AzureBlobClient;
import org.jclouds.azureblob.blobstore.AzureBlobRequestSigner;
import org.jclouds.azureblob.blobstore.AzureBlobStore;
import org.jclouds.azureblob.config.InsufficientAccessRightsException;
import org.jclouds.azureblob.domain.PublicAccess;
import org.jclouds.blobstore.BlobRequestSigner;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.attr.ConsistencyModel;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.concurrent.TimeUnit;
public class AzureBlobStoreContextModule extends AbstractModule { public class AzureBlobStoreContextModule extends AbstractModule {

View File

@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.azureblob.config;
import com.google.inject.AbstractModule;
import org.jclouds.azure.storage.util.storageurl.AppendAccountToEndpoint;
import org.jclouds.azure.storage.util.storageurl.StorageUrlSupplier;
public class AppendAccountToEndpointModule extends AbstractModule {
@Override
protected void configure() {
bind(StorageUrlSupplier.class).to(AppendAccountToEndpoint.class);
}
}

View File

@ -44,6 +44,7 @@ public class SharedKeyLiteAuthenticationTest {
private SharedKeyLiteAuthentication filter; private SharedKeyLiteAuthentication filter;
private SharedKeyLiteAuthentication filterSAS; private SharedKeyLiteAuthentication filterSAS;
private SharedKeyLiteAuthentication filterSASQuestionMark; private SharedKeyLiteAuthentication filterSASQuestionMark;
private SharedKeyLiteAuthentication filterSASCustomEndpoint;
@DataProvider(parallel = true) @DataProvider(parallel = true)
public Object[][] dataProvider() { public Object[][] dataProvider() {
@ -67,8 +68,11 @@ public class SharedKeyLiteAuthenticationTest {
{ HttpRequest.builder().method(HttpMethod.GET).endpoint("https://" + ACCOUNT { HttpRequest.builder().method(HttpMethod.GET).endpoint("https://" + ACCOUNT
+ ".blob.core.windows.net/movies/MOV1.avi").build(), filterSAS, "https://foo.blob.core.windows.net/movies/MOV1.avi?sv=2018-03-28&ss=b&srt=sco&sp=rwdlac&se=2019-02-13T17%3A18%3A22Z&st=2019-02-13T09%3A18%3A22Z&spr=https&sig=sMnaKSD94CzEPeGnWauTT0wBNIn%2B4ySkZO5PEAW7zs%3D" }, + ".blob.core.windows.net/movies/MOV1.avi").build(), filterSAS, "https://foo.blob.core.windows.net/movies/MOV1.avi?sv=2018-03-28&ss=b&srt=sco&sp=rwdlac&se=2019-02-13T17%3A18%3A22Z&st=2019-02-13T09%3A18%3A22Z&spr=https&sig=sMnaKSD94CzEPeGnWauTT0wBNIn%2B4ySkZO5PEAW7zs%3D" },
{ HttpRequest.builder().method(HttpMethod.GET).endpoint("https://" + ACCOUNT { HttpRequest.builder().method(HttpMethod.GET).endpoint("https://" + ACCOUNT
+ ".blob.core.windows.net/movies/MOV1.avi").build(), filterSASQuestionMark, "https://foo.blob.core.windows.net/movies/MOV1.avi?sv=2018-03-28&ss=b&srt=sco&sp=rwdlac&se=2019-02-13T17%3A18%3A22Z&st=2019-02-13T09%3A18%3A22Z&spr=https&sig=sMnaKSD94CzEPeGnWauTT0wBNIn%2B4ySkZO5PEAW7zs%3D" } }; + ".blob.core.windows.net/movies/MOV1.avi").build(), filterSASQuestionMark, "https://foo.blob.core.windows.net/movies/MOV1.avi?sv=2018-03-28&ss=b&srt=sco&sp=rwdlac&se=2019-02-13T17%3A18%3A22Z&st=2019-02-13T09%3A18%3A22Z&spr=https&sig=sMnaKSD94CzEPeGnWauTT0wBNIn%2B4ySkZO5PEAW7zs%3D" },
} { HttpRequest.builder().method(HttpMethod.GET).endpoint("http://my-custom-endpoint.net/movies/MOV1.avi").build(), filterSASCustomEndpoint,
"http://my-custom-endpoint.net/movies/MOV1.avi?sv=2018-03-28&ss=b&srt=sco&sp=rwdlac&se=2019-02-13T17%3A18%3A22Z&st=2019-02-13T09%3A18%3A22Z&spr=https&sig=sMnaKSD94CzEPeGnWauTT0wBNIn%2B4ySkZO5PEAW7zs%3D" } };
}
/** /**
* NOTE this test is dependent on how frequently the timestamp updates. At * NOTE this test is dependent on how frequently the timestamp updates. At
@ -167,5 +171,12 @@ public class SharedKeyLiteAuthenticationTest {
.modules(ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule())) .modules(ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule()))
.buildInjector(); .buildInjector();
filterSASQuestionMark = injector.getInstance(SharedKeyLiteAuthentication.class); filterSASQuestionMark = injector.getInstance(SharedKeyLiteAuthentication.class);
injector = ContextBuilder
.newBuilder("azureblob")
.endpoint("http://my-custom-endpoint.net")
.credentials(ACCOUNT, "?sv=2018-03-28&ss=b&srt=sco&sp=rwdlac&se=2019-02-13T17:18:22Z&st=2019-02-13T09:18:22Z&spr=https&sig=sMnaKSD94CzEPeGnWauTT0wBNIn%2B4ySkZO5PEAW7zs%3D")
.modules(ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule()))
.buildInjector();
filterSASCustomEndpoint = injector.getInstance(SharedKeyLiteAuthentication.class);
} }
} }

View File

@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.azure.storage.util.storageurl;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import org.jclouds.ContextBuilder;
import org.jclouds.azureblob.config.AppendAccountToEndpointModule;
import org.jclouds.domain.Credentials;
import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.internal.BaseRestApiTest;
import org.testng.annotations.Test;
import java.net.URI;
import static org.testng.Assert.assertEquals;
@Test(groups = "unit")
public class AppendAccountToEndpointTest {
private static final String ACCOUNT = "foo";
@Test(expectedExceptions = NullPointerException.class)
void testThrowsErrorWhenNoEndpointSupplied() {
AppendAccountToEndpoint target = new AppendAccountToEndpoint(
() -> null,
() -> new Credentials(ACCOUNT, "creds")
);
target.get();
}
@Test
void testCustomEndpointWithoutTrailingSlash() {
AppendAccountToEndpoint target = new AppendAccountToEndpoint(
() -> URI.create("http://localhost:10000"),
() -> new Credentials(ACCOUNT, "creds")
);
assertEquals(target.get().toString(), "http://localhost:10000/foo/");
}
@Test
void testCustomEndpointWithTrailingSlash() {
AppendAccountToEndpoint target = new AppendAccountToEndpoint(
() -> URI.create("http://localhost:10000/"),
() -> new Credentials(ACCOUNT, "creds")
);
assertEquals(target.get().toString(), "http://localhost:10000/foo/");
}
@Test
void testInsideContext() {
String adjustedUri = ContextBuilder
.newBuilder("azureblob")
.endpoint("http://localhost:10000")
.credentials(ACCOUNT, "?creds")
.modules(ImmutableSet.<Module> of(new BaseRestApiTest.MockModule(), new NullLoggingModule(), new AppendAccountToEndpointModule()))
.buildInjector()
.getInstance(AppendAccountToEndpoint.class)
.get()
.toString();
assertEquals(adjustedUri, "http://localhost:10000/foo/");
}
}

View File

@ -0,0 +1,87 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.azure.storage.util.storageurl;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import com.google.inject.util.Modules;
import org.jclouds.ContextBuilder;
import org.jclouds.azureblob.config.AppendAccountToEndpointModule;
import org.jclouds.domain.Credentials;
import org.jclouds.logging.config.NullLoggingModule;
import org.jclouds.rest.internal.BaseRestApiTest;
import org.testng.annotations.Test;
import java.net.URI;
import static org.testng.Assert.assertEquals;
@Test(groups = "unit")
public class StorageAccountInVhostTest {
private static final String ACCOUNT = "foo";
@Test
void testDefaultEndpointWhenNoneSupplied() {
StorageAccountInVhost target = new StorageAccountInVhost(
() -> null,
() -> new Credentials(ACCOUNT, "creds")
);
assertEquals(target.get().toString(), "https://foo.blob.core.windows.net/");
}
@Test
void testCustomEndpointWithoutTrailingSlash() {
StorageAccountInVhost target = new StorageAccountInVhost(
() -> URI.create("https://foo2.blob.core.windows.net/"),
() -> new Credentials(ACCOUNT, "creds")
);
assertEquals(target.get().toString(), "https://foo2.blob.core.windows.net/");
}
@Test
void testCustomEndpointWithTrailingSlash() {
StorageAccountInVhost target = new StorageAccountInVhost(
() -> URI.create("https://foo2.blob.core.windows.net/"),
() -> new Credentials(ACCOUNT, "creds")
);
assertEquals(target.get().toString(), "https://foo2.blob.core.windows.net/");
}
@Test
void testInsideContext() {
String adjustedUri = ContextBuilder
.newBuilder("azureblob")
.endpoint("https://foo2.blob.core.windows.net")
.credentials(ACCOUNT, "?creds")
.modules(ImmutableSet.<Module> of(new BaseRestApiTest.MockModule(), new NullLoggingModule()))
.buildInjector()
.getInstance(StorageAccountInVhost.class)
.get()
.toString();
Modules.override(new AppendAccountToEndpointModule());
assertEquals(adjustedUri, "https://foo2.blob.core.windows.net/");
}
}