Skip to content

Commit

Permalink
Merge pull request #76 from bmorrise/BACKLOG-27679
Browse files Browse the repository at this point in the history
[BACKLOG-27679] Add credential file for vfs connection auth
  • Loading branch information
ddiroma committed Jul 17, 2019
2 parents 70832dc + 4218e51 commit a8f7a1e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/main/java/org/pentaho/s3common/S3CommonFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@

package org.pentaho.s3common;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.auth.profile.ProfilesConfigFile;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
Expand Down Expand Up @@ -59,14 +64,31 @@ public AmazonS3 getS3Client() {
new S3CommonFileSystemConfigBuilder( getFileSystemOptions() );
String accessKey = s3CommonFileSystemConfigBuilder.getAccessKey();
String secretKey = s3CommonFileSystemConfigBuilder.getSecretKey();
String sessionToken = s3CommonFileSystemConfigBuilder.getSessionToken();
String region = s3CommonFileSystemConfigBuilder.getRegion();
String credentialsFilePath = s3CommonFileSystemConfigBuilder.getCredentialsFile();
String profileName = s3CommonFileSystemConfigBuilder.getProfileName();

AWSCredentialsProvider awsCredentialsProvider = null;
Regions regions = Regions.DEFAULT_REGION;
if ( !S3Util.isEmpty( accessKey ) && !S3Util.isEmpty( secretKey ) ) {
BasicAWSCredentials awsCredentials = new BasicAWSCredentials( accessKey, secretKey );
client = AmazonS3ClientBuilder.standard()
.enableForceGlobalBucketAccess()
.withRegion( Regions.DEFAULT_REGION )
.withCredentials( new AWSStaticCredentialsProvider( awsCredentials ) )
.build();
AWSCredentials awsCredentials;
if ( S3Util.isEmpty( sessionToken ) ) {
awsCredentials = new BasicAWSCredentials( accessKey, secretKey );
} else {
awsCredentials = new BasicSessionCredentials( accessKey, secretKey, sessionToken );
}
awsCredentialsProvider = new AWSStaticCredentialsProvider( awsCredentials );
regions = S3Util.isEmpty( region ) ? Regions.DEFAULT_REGION : Regions.fromName( region );
} else if ( !S3Util.isEmpty( credentialsFilePath ) ) {
ProfilesConfigFile profilesConfigFile = new ProfilesConfigFile( credentialsFilePath );
awsCredentialsProvider = new ProfileCredentialsProvider( profilesConfigFile, profileName );
}
client = AmazonS3ClientBuilder.standard()
.enableForceGlobalBucketAccess()
.withRegion( regions )
.withCredentials( awsCredentialsProvider )
.build();
}
if ( client == null || hasClientChangedCredentials() ) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class S3CommonFileSystemConfigBuilder extends FileSystemConfigBuilder {

private static final String ACCESS_KEY = "accessKey";
private static final String SECRET_KEY = "secretKey";
private static final String SESSION_TOKEN = "sessionToken";
private static final String REGION = "region";
private static final String CREDENTIALS_FILE = "credentialsFile";
private static final String PROFILE_NAME = "profileName";

private FileSystemOptions fileSystemOptions;

Expand Down Expand Up @@ -60,6 +64,38 @@ public String getSecretKey() {
return (String) this.getParam( getFileSystemOptions(), SECRET_KEY );
}

public void setSessionToken( String sessionToken ) {
this.setParam( getFileSystemOptions(), SESSION_TOKEN, sessionToken );
}

public String getSessionToken() {
return (String) this.getParam( getFileSystemOptions(), SESSION_TOKEN );
}

public void setRegion( String region ) {
this.setParam( getFileSystemOptions(), REGION, region );
}

public String getRegion() {
return (String) this.getParam( getFileSystemOptions(), REGION );
}

public void setCredentialsFile( String credentialsFile ) {
this.setParam( getFileSystemOptions(), CREDENTIALS_FILE, credentialsFile );
}

public String getCredentialsFile() {
return (String) this.getParam( getFileSystemOptions(), CREDENTIALS_FILE );
}

public String getProfileName() {
return (String) this.getParam( getFileSystemOptions(), PROFILE_NAME );
}

public void setProfileName( String profileName ) {
this.setParam( getFileSystemOptions(), PROFILE_NAME, profileName );
}

@Override protected Class<? extends FileSystem> getConfigClass() {
return S3NFileSystem.class;
}
Expand Down

0 comments on commit a8f7a1e

Please sign in to comment.