Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Functionality to support a Key Prefix and EC2InstanceProfile IAM #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

BenWolstencroft
Copy link

@BenWolstencroft BenWolstencroft commented Dec 13, 2017

A number of commits here:

Adds the ability to optionally supply a KeyPrefix parameter which allows the amazon-s3-provider to store files in a subfolder in the specified S3 bucket

  • Multiple Environments can use the same S3 bucket but the concerns can be separated by a folder in the S3 bucket.
  • The sitefinity instance can use an S3 bucket that is used for other things, but keep library files seperate within a folder.

Adds the ability to optionally use an EC2 Instance Role (i.e. not supply AccessKey and SecretAccessKey IAM credentials), this works if Sitefinity is deployed to an EC2 instance, and the EC2 instance's profile role has the correct S3 Bucket policy applied.

@@ -26,25 +26,42 @@ public class AmazonBlobStorageProvider : CloudBlobStorageProvider
/// <param name="config">The collection of parameters (each by its name and value) of the current provider's configuration settings.</param>
protected override void InitializeStorage(NameValueCollection config)
{
var useIamInstanceRoleValue = "false";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional configuration item to allow the use of the EC2 Instance Profile Role, defaults to false, maintaining backwards compatibility

this.accessKeyId = config[AccessKeyIdKey].Trim();
if (String.IsNullOrEmpty(this.accessKeyId))
if (!this.useIamInstanceRole && String.IsNullOrEmpty(this.accessKeyId))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to AccessKeyId and SecretKeyId parameters to only validate if we are not using the EC2 Instance Profile Role

throw new ConfigurationException("'{0}' is required.".Arrange(SecretKeyKey));

this.bucketName = config[BucketNameKey].Trim();
if (String.IsNullOrEmpty(this.bucketName))
throw new ConfigurationException("'{0}' is required.".Arrange(BucketNameKey));

if (config.AllKeys.Contains(KeyPrefixKey))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional configuration item to allow the use of a key prefix on files uploaded to the S3 Bucket

string regionEndpointString = config[RegionEndpointKey].Trim();
var endpointField = typeof(RegionEndpoint).GetField(regionEndpointString, BindingFlags.Static | BindingFlags.Public);
if ((string.IsNullOrWhiteSpace(regionEndpointString)) || (endpointField == null))
throw new ConfigurationException("'{0}' is required.".Arrange(RegionEndpointKey));

var regionEndpoint = (RegionEndpoint)endpointField.GetValue(null);
this.transferUtility = new TransferUtility(accessKeyId, secretKey, regionEndpoint);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're using the EC2 Instance Profile Role, don't pass accesskey and secretaccesskey into the TransferUtility constructor, the AWS SDK will use the instance's role by default

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BenWolstencroft,
We've managed to test EC2InstanceProfile IAM functionality and as you said this works fine if Sitefinity is deployed to an EC2 instance, and the EC2 instance's profile role has the correct S3 Bucket policy applied.
But what if useIamInstanceRoleValue is set to true and Sitefinity is hosted outside AWS (or EC2 instance's profile role in not attached to EC2 instance), then error is thrown:

Exception 1 of 4:
System.ArgumentException: App.config does not contain credentials information. Either add the AWSAccessKey and AWSSecretKey or AWSProfileName.
at Amazon.Runtime.StoredProfileAWSCredentials..ctor(String profileName, String profilesLocation) in d:\Jenkins\jobs\build-sdk-v2\workspace\sdk\src\AWSSDK_DotNet35\Amazon.Runtime\AWSCredentials.cs:line 318
at Amazon.Runtime.EnvironmentAWSCredentials..ctor() in d:\Jenkins\jobs\build-sdk-v2\workspace\sdk\src\AWSSDK_DotNet35\Amazon.Runtime\AWSCredentials.cs:line 590
at Amazon.Runtime.FallbackCredentialsFactory.b__1() in d:\Jenkins\jobs\build-sdk-v2\workspace\sdk\src\AWSSDK_DotNet35\Amazon.Runtime\AWSCredentials.cs:line 1117
at Amazon.Runtime.FallbackCredentialsFactory.GetCredentials(Boolean fallbackToAnonymous) in d:\Jenkins\jobs\build-sdk-v2\workspace\sdk\src\AWSSDK_DotNet35\Amazon.Runtime\AWSCredentials.cs:line 1137

We need proper documentation, how exactly this setup works, and probably cover this scenario described above.

@BenWolstencroft
Copy link
Author

Found an issue with the PR, will resubmit

@BenWolstencroft
Copy link
Author

issue investigated and there was no problem - misconfiguration on my end!

@ghost
Copy link

ghost commented Aug 4, 2019

Why was this not this PR merged?? This is a really useful feature!!

@BenWolstencroft
Copy link
Author

@jxt-aref - don't think sitefinity 'care' much about this repo, seems it's more of a documentation example than a 'working' plugin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants