Skip to content

Commit 4d627fa

Browse files
richarddownerloic-sharma
authored andcommitted
Two additional ways of access in AWS S3 (loic-sharma#277)
* Add Assume Role option If AWS AssumeRoleArn is specified, the ServiceCollectionExtensions for S3 gets FallbackCredentials (instance profile) then uses those credentials to assume the role specified in AssumeRoleArn. The AssumeRoleCredentials class in AwsIamHelper manages session timeouts automatically by validating and retrieving a new token on use. * Adding the option for UseInstanceProfile back As it seems the credentials will not time out. The role assumption works also, and is useful for assuming a role for cross account access. * Update nuget version of newtonsoft, to pass the build test pipeline
1 parent 019b0bb commit 4d627fa

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/BaGet.AWS/Configuration/S3StorageOptions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System.ComponentModel.DataAnnotations;
22
using BaGet.Core.Validation;
33

44
namespace BaGet.AWS.Configuration
@@ -18,5 +18,9 @@ public class S3StorageOptions
1818
public string Bucket { get; set; }
1919

2020
public string Prefix { get; set; }
21+
22+
public bool UseInstanceProfile { get; set; }
23+
24+
public string AssumeRoleArn { get; set; }
2125
}
2226
}

src/BaGet.AWS/Extensions/ServiceCollectionExtensions.cs

+18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System;
12
using Amazon;
23
using Amazon.Runtime;
34
using Amazon.S3;
45
using BaGet.AWS.Configuration;
6+
using BaGet.AWS.Helpers;
57
using Microsoft.Extensions.DependencyInjection;
68
using Microsoft.Extensions.Options;
79

@@ -20,6 +22,22 @@ public static IServiceCollection AddS3StorageService(this IServiceCollection ser
2022
RegionEndpoint = RegionEndpoint.GetBySystemName(options.Region)
2123
};
2224

25+
if (options.UseInstanceProfile)
26+
{
27+
var credentials = FallbackCredentialsFactory.GetCredentials();
28+
return new AmazonS3Client(credentials, config);
29+
}
30+
31+
if (!string.IsNullOrEmpty(options.AssumeRoleArn))
32+
{
33+
var credentials = FallbackCredentialsFactory.GetCredentials();
34+
var assumedCredentials = AwsIamHelper
35+
.AssumeRoleAsync(credentials, options.AssumeRoleArn, $"BaGet-Session-{Guid.NewGuid()}").GetAwaiter().GetResult();
36+
37+
return new AmazonS3Client(assumedCredentials, config);
38+
}
39+
40+
2341
if (!string.IsNullOrEmpty(options.AccessKey))
2442
return new AmazonS3Client(new BasicAWSCredentials(options.AccessKey, options.SecretKey), config);
2543

src/BaGet.AWS/Helpers/AwsIamHelper.cs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Amazon.Runtime;
4+
5+
namespace BaGet.AWS.Helpers
6+
{
7+
public static class AwsIamHelper
8+
{
9+
public static async Task<AWSCredentials> AssumeRoleAsync(AWSCredentials credentials, string roleArn,
10+
string roleSessionName)
11+
{
12+
var assumedCredentials = new AssumeRoleAWSCredentials(credentials, roleArn, roleSessionName);
13+
var immutableCredentials = await credentials.GetCredentialsAsync();
14+
15+
if (string.IsNullOrWhiteSpace(immutableCredentials.Token))
16+
{
17+
throw new InvalidOperationException($"Unable to assume role {roleArn}");
18+
}
19+
20+
return assumedCredentials;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)