Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

AWS profile definition on server level #69

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion S3StorageWagon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ To specify a repo as public you can do it through the settings.xml
<password>access_secret</password>
<configuration>
<region>eu-west-1</region>
<publicRepository>true</publicRepository>
<publicRepository>true</publicRepository>
</configuration>
</server>
```
Expand Down Expand Up @@ -58,6 +58,19 @@ Then you can use the artifact without any authorised access
</repositories>
```

### Private repos - access by profile
You can assign aws_profile for your server. You can do it through the settings.xml:
```xml
<server>
<id>bucket-repo</id>
<configuration>
<region>eu-west-1</region>
<awsProfile>aws_profile_name</awsProfile>
</configuration>
</server>
```


## Upload/download files for ci/cd purposes

Apart from giving a solution to use s3 a maven repository the storage s3-storage-wagon can be used as a plugin in order to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package com.gkatzioura.maven.cloud.s3;

import java.util.logging.Logger;

import org.apache.maven.wagon.authentication.AuthenticationInfo;

import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import org.apache.maven.wagon.authentication.AuthenticationInfo;

import java.util.logging.Logger;

public class CredentialsFactory {

Expand All @@ -38,15 +38,18 @@ public class CredentialsFactory {
* for details.
*
* @param authenticationInfo an {@link AuthenticationInfo} containing the AWS credentials to use
* @param awsProfile
* @return a newly-built {@link AWSCredentialsProvider} with the credentials associated to the passed
* {@code authenticationInfo}
* {@code authenticationInfo}
*/
public AWSCredentialsProvider create(AuthenticationInfo authenticationInfo) {
if(authenticationInfo==null) {
public AWSCredentialsProvider create(AuthenticationInfo authenticationInfo, String awsProfile) {
if (awsProfile != null) {
return new ProfileCredentialsProvider(awsProfile);
} else if (authenticationInfo == null) {
return new DefaultAWSCredentialsProviderChain();
} else {
LOGGER.info("Using static credentials provider");
return new AWSStaticCredentialsProvider(new BasicAWSCredentials(authenticationInfo.getUserName(),authenticationInfo.getPassword()));
return new AWSStaticCredentialsProvider(new BasicAWSCredentials(authenticationInfo.getUserName(), authenticationInfo.getPassword()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ public S3StorageRepository(String bucket, String baseDirectory, PublicReadProper
}


public void connect(AuthenticationInfo authenticationInfo, String region, EndpointProperty endpoint, PathStyleEnabledProperty pathStyle) throws AuthenticationException {
this.amazonS3 = S3Connect.connect(authenticationInfo, region, endpoint, pathStyle);
public void connect(AuthenticationInfo authenticationInfo, String region, EndpointProperty endpoint, PathStyleEnabledProperty pathStyle, String awsProfile) throws AuthenticationException {
this.amazonS3 = S3Connect.connect(authenticationInfo, region, endpoint, pathStyle, awsProfile);
}

public void copy(String resourceName, File destination, TransferProgress transferProgress) throws TransferFailedException, ResourceDoesNotExistException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@

package com.gkatzioura.maven.cloud.s3;

import java.io.File;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.gkatzioura.maven.cloud.resolver.KeyResolver;
import com.gkatzioura.maven.cloud.transfer.TransferProgress;
import com.gkatzioura.maven.cloud.transfer.TransferProgressImpl;
import com.gkatzioura.maven.cloud.wagon.AbstractStorageWagon;
import com.gkatzioura.maven.cloud.wagon.PublicReadProperty;
import org.apache.commons.io.FileUtils;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.PathUtils;
Expand All @@ -39,18 +35,22 @@
import org.apache.maven.wagon.repository.Repository;
import org.apache.maven.wagon.resource.Resource;

import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.gkatzioura.maven.cloud.transfer.TransferProgress;
import com.gkatzioura.maven.cloud.transfer.TransferProgressImpl;
import com.gkatzioura.maven.cloud.wagon.AbstractStorageWagon;
import com.gkatzioura.maven.cloud.wagon.PublicReadProperty;
import java.io.File;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class S3StorageWagon extends AbstractStorageWagon {

private S3StorageRepository s3StorageRepository;
private final KeyResolver keyResolver = new KeyResolver();

private String region;
private String awsProfile;
private Boolean publicRepository;

private static final Logger LOGGER = Logger.getLogger(S3StorageWagon.class.getName());
Expand Down Expand Up @@ -181,7 +181,7 @@ public void connect(Repository repository, AuthenticationInfo authenticationInfo

LOGGER.log(Level.FINER,String.format("Opening connection for bucket %s and directory %s",bucket,directory));
s3StorageRepository = new S3StorageRepository(bucket, directory, new PublicReadProperty(publicRepository));
s3StorageRepository.connect(authenticationInfo, region, new EndpointProperty(endpoint), new PathStyleEnabledProperty(pathStyleEnabled));
s3StorageRepository.connect(authenticationInfo, region, new EndpointProperty(endpoint), new PathStyleEnabledProperty(pathStyleEnabled), awsProfile);

sessionListenerContainer.fireSessionLoggedIn();
sessionListenerContainer.fireSessionOpened();
Expand Down Expand Up @@ -227,4 +227,11 @@ public void setPathStyleAccessEnabled(String pathStyleEnabled) {
this.pathStyleEnabled = pathStyleEnabled;
}

public void setAwsProfile(String awsProfile) {
this.awsProfile = awsProfile;
}

public String getAwsProfile() {
return awsProfile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.maven.wagon.authentication.AuthenticationException;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.S3ClientOptions;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
Expand All @@ -47,6 +46,9 @@
@Mojo(name = "s3-download")
public class S3DownloadMojo extends AbstractMojo {

@Parameter( property = "s3-download.awsProfile")
private String awsProfile;

@Parameter( property = "s3-download.bucket")
private String bucket;

Expand All @@ -66,11 +68,12 @@ public class S3DownloadMojo extends AbstractMojo {
public S3DownloadMojo() {
}

public S3DownloadMojo(String bucket, List<String> keys, String downloadPath, String region) {
public S3DownloadMojo(String bucket, List<String> keys, String downloadPath, String region, String awsProfile) {
this.bucket = bucket;
this.keys = keys;
this.downloadPath = downloadPath;
this.region = region;
this.awsProfile = awsProfile;
}

@Override
Expand All @@ -80,7 +83,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
try {
//Sending the authenticationInfo as null will make this use the default S3 authentication, which will only
//look at the environment Java properties or environment variables
amazonS3 = S3Connect.connect(null, region, EndpointProperty.empty(), new PathStyleEnabledProperty(String.valueOf(S3ClientOptions.DEFAULT_PATH_STYLE_ACCESS)));
amazonS3 = S3Connect.connect(null, region, EndpointProperty.empty(), new PathStyleEnabledProperty(String.valueOf(S3ClientOptions.DEFAULT_PATH_STYLE_ACCESS)), awsProfile);
} catch (AuthenticationException e) {
throw new MojoExecutionException(
String.format("Unable to authenticate to S3 with the available credentials. Make sure to either define the environment variables or System properties defined in https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html.%n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
@Mojo(name = "s3-upload")
public class S3UploadMojo extends AbstractMojo {

@Parameter( property = "s3-upload.profile")
private String awsProfile;

@Parameter( property = "s3-upload.bucket")
private String bucket;

Expand All @@ -65,11 +68,12 @@ public S3UploadMojo() {
* @param key
* @param region
*/
public S3UploadMojo(String bucket, String path, String key, String region) {
public S3UploadMojo(String bucket, String path, String key, String region, String awsProfile) {
this.bucket = bucket;
this.path = path;
this.key = key;
this.region = region;
this.awsProfile = awsProfile;
}

/**
Expand All @@ -87,7 +91,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
try {
//Sending the authenticationInfo as null will make this use the default S3 authentication, which will only
//look at the environment Java properties or environment variables
amazonS3 = S3Connect.connect(null, region, EndpointProperty.empty(), new PathStyleEnabledProperty(String.valueOf(S3ClientOptions.DEFAULT_PATH_STYLE_ACCESS)));
amazonS3 = S3Connect.connect(null, region, EndpointProperty.empty(), new PathStyleEnabledProperty(String.valueOf(S3ClientOptions.DEFAULT_PATH_STYLE_ACCESS)), awsProfile);
} catch (AuthenticationException e) {
throw new MojoExecutionException(
String.format("Unable to authenticate to S3 with the available credentials. Make sure to either define the environment variables or System properties defined in https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html.%n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ public class S3Connect {
* @param pathStyle A {@link PathStyleEnabledProperty} indicating whether the endpoint/bucket configuration being
* passed is in a path-style configuration. See
* <a href="https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingBucket.html#access-bucket-intro">Accessing a Bucket in the S3 documentation</a>.
* @param awsProfile
* @return An instance of {@link AmazonS3} that can be used to send and receive data to the intended endpoint/bucket.
* @throws AuthenticationException if the passed credentials are invalid for connecting to the intended endpoint/bucket.
*/
public static AmazonS3 connect(AuthenticationInfo authenticationInfo, String region, EndpointProperty endpoint, PathStyleEnabledProperty pathStyle) throws AuthenticationException {
public static AmazonS3 connect(AuthenticationInfo authenticationInfo, String region, EndpointProperty endpoint, PathStyleEnabledProperty pathStyle, String awsProfile) throws AuthenticationException {
AmazonS3ClientBuilder builder = null;
try {
builder = createAmazonS3ClientBuilder(authenticationInfo, region, endpoint, pathStyle);
builder = createAmazonS3ClientBuilder(authenticationInfo, region, endpoint, pathStyle, awsProfile);

AmazonS3 amazonS3 = builder.build();

Expand All @@ -77,11 +78,11 @@ public static AmazonS3 connect(AuthenticationInfo authenticationInfo, String reg
}
}

private static AmazonS3ClientBuilder createAmazonS3ClientBuilder(AuthenticationInfo authenticationInfo, String region, EndpointProperty endpoint, PathStyleEnabledProperty pathStyle) {
private static AmazonS3ClientBuilder createAmazonS3ClientBuilder(AuthenticationInfo authenticationInfo, String region, EndpointProperty endpoint, PathStyleEnabledProperty pathStyle, String awsProfile) {
final S3StorageRegionProviderChain regionProvider = new S3StorageRegionProviderChain(region);

AmazonS3ClientBuilder builder;
builder = AmazonS3ClientBuilder.standard().withCredentials(new CredentialsFactory().create(authenticationInfo));
builder = AmazonS3ClientBuilder.standard().withCredentials(new CredentialsFactory().create(authenticationInfo, awsProfile));

if (endpoint.isPresent()){
builder.setEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration(endpoint.get(), builder.getRegion()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected long getExpectedLastModifiedOnGet(Repository repository, Resource reso
protected void setUp() throws Exception {
super.setUp();
//creates the bucket
amazonS3 = S3Connect.connect(getAuthInfo(), null, new EndpointProperty(null), new PathStyleEnabledProperty(null));
amazonS3 = S3Connect.connect(getAuthInfo(), null, new EndpointProperty(null), new PathStyleEnabledProperty(null), null);

createBucket();
}
Expand Down