Skip to content

Commit

Permalink
fixed issue #38: uri without host create filesystems with keys access…
Browse files Browse the repository at this point in the history
…Key@null
  • Loading branch information
jarnaiz committed Oct 25, 2015
1 parent f405e66 commit 9201ecf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
16 changes: 12 additions & 4 deletions src/main/java/com/upplication/s3fs/S3FileSystemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
Expand All @@ -35,6 +36,7 @@
import java.util.concurrent.ConcurrentMap;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.internal.Constants;
import com.amazonaws.services.s3.model.*;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -130,12 +132,15 @@ private String getFileSystemKey(URI uri) {

/**
* get the file system key represented by: the access key @ endpoint.
* Example: [email protected]
* Example: [email protected]
* If uri host is empty then s3.amazonaws.com are used as host
* @param uri URI with the endpoint
* @param props with the access key property
* @return String
*/
protected String getFileSystemKey(URI uri, Properties props) {
// we don`t use uri.getUserInfo and uri.getHost because secret key and access key have special chars
// and dont return the correct strings
String uriString = uri.toString().replace("s3://", "");
String authority = null;
int authoritySeparator = uriString.indexOf("@");
Expand All @@ -150,12 +155,16 @@ protected String getFileSystemKey(URI uri, Properties props) {
if (lastPath > 0){
host = host.substring(0, lastPath);
}
else {
host = Constants.S3_HOSTNAME;
}
return authority + "@" + host;
}
else {
String accessKey = (String) props.get(ACCESS_KEY);

return (accessKey != null ? accessKey+"@" : "" ) + uri.getHost();
return (accessKey != null ? accessKey+"@" : "" ) +
(uri.getHost() != null ? uri.getHost() : Constants.S3_HOSTNAME);
}
}

Expand Down Expand Up @@ -255,8 +264,7 @@ public S3FileSystem getFileSystem(URI uri) {
return fileSystems.get(key);
}
else{
throw new FileSystemNotFoundException(
String.format("S3 filesystem not yet created. Use newFileSystem() instead"));
throw new FileSystemNotFoundException("S3 filesystem not yet created. Use newFileSystem() instead");
}
}

Expand Down
23 changes: 18 additions & 5 deletions src/test/java/com/upplication/s3fs/S3FileSystemProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.amazonaws.services.s3.internal.Constants;
import com.upplication.s3fs.util.*;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -58,6 +59,18 @@ public void missconfigure() {
s3fsProvider.createFileSystem(S3_GLOBAL_URI, props);
}

@Test
public void newS3FileSystemWithEmptyHostAndUserInfo() throws IOException {
FileSystem s3fs = s3fsProvider.newFileSystem(URI.create("s3:///bucket/file"), ImmutableMap.<String, Object>of());
assertEquals(Constants.S3_HOSTNAME, ((S3FileSystem) s3fs).getKey());
}

@Test
public void newS3FileSystemWithEmptyHost() throws IOException {
FileSystem s3fs = s3fsProvider.newFileSystem(URI.create("s3://access-key:secret-key@/bucket/file"), ImmutableMap.<String, Object>of());
assertEquals("access-key:secret-key@" + Constants.S3_HOSTNAME, ((S3FileSystem) s3fs).getKey());
}

@Test
public void createsAuthenticatedByEnv() {
Map<String, ?> env = buildFakeEnv();
Expand Down Expand Up @@ -105,12 +118,12 @@ public void createAuthenticatedBySystemEnvironment() {
doReturn(secretKey).when(s3fsProvider).systemGetEnv(SECRET_KEY);
doCallRealMethod().when(s3fsProvider).overloadPropertiesWithSystemEnv(any(Properties.class), anyString());

s3fsProvider.newFileSystem(S3_GLOBAL_URI, ImmutableMap.<String, Object> of());
s3fsProvider.newFileSystem(S3_GLOBAL_URI, ImmutableMap.<String, Object>of());

verify(s3fsProvider).createFileSystem(eq(S3_GLOBAL_URI), argThat(new ArgumentMatcher<Properties>() {
@Override
public boolean matches(Object argument) {
Properties called = (Properties)argument;
Properties called = (Properties) argument;
assertEquals(accessKey, called.getProperty(ACCESS_KEY));
assertEquals(secretKey, called.getProperty(SECRET_KEY));
return true;
Expand Down Expand Up @@ -146,7 +159,7 @@ public void createWithOnlyAccessKey() {
Properties props = new Properties();
props.setProperty(ACCESS_KEY, "better access key");
doReturn(props).when(s3fsProvider).loadAmazonProperties();
s3fsProvider.newFileSystem(S3_GLOBAL_URI, ImmutableMap.<String, Object> of());
s3fsProvider.newFileSystem(S3_GLOBAL_URI, ImmutableMap.<String, Object>of());
}

@Test(expected = IllegalArgumentException.class)
Expand All @@ -169,12 +182,12 @@ public void createWithWrongEnv() {
Map<String, Object> env = ImmutableMap.<String, Object> builder().put(ACCESS_KEY, 1234).put(SECRET_KEY, "secret key").build();
FileSystem fileSystem = s3fsProvider.newFileSystem(S3_GLOBAL_URI, env);
assertNotNull(fileSystem);
s3fsProvider.newFileSystem(S3_GLOBAL_URI, ImmutableMap.<String, Object> of());
s3fsProvider.newFileSystem(S3_GLOBAL_URI, ImmutableMap.<String, Object>of());
}

@Test
public void getFileSystem() {
FileSystem fileSystem = s3fsProvider.newFileSystem(S3_GLOBAL_URI, ImmutableMap.<String, Object> of());
FileSystem fileSystem = s3fsProvider.newFileSystem(S3_GLOBAL_URI, ImmutableMap.<String, Object>of());
assertNotNull(fileSystem);
fileSystem = s3fsProvider.getFileSystem(S3_GLOBAL_URI, ImmutableMap.<String, Object> of());
assertNotNull(fileSystem);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/upplication/s3fs/S3FileSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public void comparables() throws IOException {
assertEquals(1483378423, s3fs1.hashCode());
assertEquals(684416791, s3fs2.hashCode());
assertEquals(182977201, s3fs3.hashCode());
assertEquals(-615984431, s3fs4.hashCode());
assertEquals(1163233038, s3fs4.hashCode());
assertEquals(-498271993, s3fs6.hashCode());
assertEquals(-82123487, s3fs7.hashCode());

Expand Down

0 comments on commit 9201ecf

Please sign in to comment.