Skip to content

Commit

Permalink
Add .NET Standard 2.0 support to support ASP.NET Core 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhathcock committed Sep 20, 2017
1 parent 32accdd commit 34dd0aa
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 140 deletions.
4 changes: 2 additions & 2 deletions misc/CredstashTester/CredstashTester.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
<DebugType>portable</DebugType>
<AssemblyName>CredstashTester</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>CredstashTester</PackageId>
<RuntimeFrameworkVersion>1.1.2</RuntimeFrameworkVersion>
<RuntimeFrameworkVersion>2.0.0</RuntimeFrameworkVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
115 changes: 55 additions & 60 deletions misc/CredstashTester/Program.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.KeyManagementService;
using Amazon.Runtime;
using Microsoft.Extensions.Configuration;
using Narochno.Credstash;
using Narochno.Credstash.Configuration;

namespace CredstashTester
{
public class Program
{
public static void Main(string[] args)
{
//var creds = new StoredProfileAWSCredentials();
//var stash = new Credstash(new CredstashOptions(), new AmazonKeyManagementServiceClient(creds, RegionEndpoint.EUWest1),
// new AmazonDynamoDBClient(creds, RegionEndpoint.EUWest1));
//var val = stash.GetSecret("redis:host", null, new Dictionary<string, string>()
//{
// { "environment", "beta"}
//}).Result;
//Console.WriteLine(val);

//foreach (var entry in stash.List().Result)
//{
// Console.WriteLine($"{entry.Name} v{entry.Version}");
//}

AWSCredentials creds = new StoredProfileAWSCredentials();
var configBuilder = new ConfigurationBuilder();
configBuilder.AddCredstash(creds, new CredstashConfigurationOptions()
{
EncryptionContext = new Dictionary<string, string>()
{
{"environment", "beta"}
}
});

var config = configBuilder.Build();
var beta = config.GetSection("beta");

Print(beta, string.Empty);
}

private static void Print(IConfiguration c, string prefix)
{
foreach (var section in c.GetChildren())
{
if (!string.IsNullOrEmpty(section.Value))
{
Console.WriteLine(prefix + ":" + section.Key + " " + section.Value);
}
Print(section, prefix + ":" + section.Key);
}
}
}
}
using System;
using System.Collections.Generic;
using Amazon.Runtime;
using Microsoft.Extensions.Configuration;
using Narochno.Credstash.Configuration;

namespace CredstashTester
{
public class Program
{
public static void Main(string[] args)
{
//var creds = new StoredProfileAWSCredentials();
//var stash = new Credstash(new CredstashOptions(), new AmazonKeyManagementServiceClient(creds, RegionEndpoint.EUWest1),
// new AmazonDynamoDBClient(creds, RegionEndpoint.EUWest1));
//var val = stash.GetSecret("redis:host", null, new Dictionary<string, string>()
//{
// { "environment", "beta"}
//}).Result;
//Console.WriteLine(val);

//foreach (var entry in stash.List().Result)
//{
// Console.WriteLine($"{entry.Name} v{entry.Version}");
//}

AWSCredentials creds = new StoredProfileAWSCredentials();
var configBuilder = new ConfigurationBuilder();
configBuilder.AddCredstash(creds, new CredstashConfigurationOptions()
{
EncryptionContext = new Dictionary<string, string>()
{
{"environment", "beta"}
}
});

var config = configBuilder.Build();
var beta = config.GetSection("beta");

Print(beta, string.Empty);
}

private static void Print(IConfiguration c, string prefix)
{
foreach (var section in c.GetChildren())
{
if (!string.IsNullOrEmpty(section.Value))
{
Console.WriteLine(prefix + ":" + section.Key + " " + section.Value);
}
Print(section, prefix + ":" + section.Key);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2;
using Amazon.KeyManagementService;
using Amazon.Runtime;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;

namespace Narochno.Credstash.Configuration
{
Expand All @@ -19,7 +17,7 @@ public static IConfigurationBuilder AddCredstash(this IConfigurationBuilder buil
{
Region = options.Region,
Table = options.Table,
Dop = options.Dop
DegreeOfParallelism = options.DegreeOfParallelism
}, new AmazonKeyManagementServiceClient(credentials, options.Region),
new AmazonDynamoDBClient(credentials, options.Region));

Expand All @@ -38,43 +36,12 @@ public static IConfigurationBuilder AddCredstash(this IConfigurationBuilder buil
{
Region = options.Region,
Table = options.Table,
Dop = options.Dop
DegreeOfParallelism = options.DegreeOfParallelism
}, new AmazonKeyManagementServiceClient(creds, options.Region),
new AmazonDynamoDBClient(creds, options.Region));

builder.Add(new CredstashConfigurationSource(credstash, options.EncryptionContext));
return builder;
}

public static IConfigurationBuilder AddCredstash(this IConfigurationBuilder builder, RegionEndpoint region,
string table,
AWSCredentials creds = null,
Dictionary<string, string> encryptionContext = null,
int dop = 1)
{
var options = new CredstashConfigurationOptions
{
Region = region,
Dop = dop,
EncryptionContext = encryptionContext ?? new Dictionary<string, string>(),
Table = table
};

creds = creds ?? FallbackCredentialsFactory.GetCredentials();

var credstash = new Credstash(new CredstashOptions()
{
Region = options.Region,
Table = options.Table,
Dop = options.Dop
},
new AmazonKeyManagementServiceClient(creds, region),
new AmazonDynamoDBClient(creds, region)
);

builder.Add(new CredstashConfigurationSource(credstash, options.EncryptionContext));

return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public class CredstashConfigurationOptions
/// <summary>
/// If the number of stored items are more than 10, you can decrypt them paralelly if this number if higher than 1. Dop marks the number of maximum parallel requests to DynamoDB and KMS. You need to make sure your read capacity unit is in place.
/// </summary>
public int Dop { get; set; }
public int DegreeOfParallelism { get; set; } = 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace Narochno.Credstash.Configuration
{
public class CredstashConfigurationProvider : ConfigurationProvider
{
private readonly Credstash credstash;
private readonly int dop;
private readonly Dictionary<string, string> encryptionContext;
private readonly Credstash _credstash;
private readonly int _degreeOfParallelism;
private readonly Dictionary<string, string> _encryptionContext;

public CredstashConfigurationProvider(Credstash credstash, Dictionary<string, string> encryptionContext)
{
this.credstash = credstash;
this.dop = credstash.Options.Dop;
this.encryptionContext = encryptionContext;
_credstash = credstash;
_degreeOfParallelism = credstash.Options.DegreeOfParallelism;
_encryptionContext = encryptionContext;
}

public override void Load()
Expand All @@ -28,11 +28,11 @@ public async Task<Dictionary<string, string>> LoadAsync()
{
var data = new Dictionary<string, string>();

var entries = (await credstash.ListAsync()).Select(x => x.Name).Distinct();
var entries = (await _credstash.ListAsync()).Select(x => x.Name).Distinct().ToList();

if (entries.Count() > 10 && dop > 1)
if (entries.Count() > 10 && _degreeOfParallelism > 1)
{
await entries.ForEachAsync(dop, async entry =>
await entries.ForEachAsync(_degreeOfParallelism, async entry =>
{
await SetConfigValueAsync(data, entry).ConfigureAwait(false);
Expand All @@ -53,7 +53,7 @@ private async Task SetConfigValueAsync(Dictionary<string, string> data, string e
{
try
{
var secret = await credstash.GetSecretAsync(entry, null, encryptionContext, false).ConfigureAwait(false);
var secret = await _credstash.GetSecretAsync(entry, null, _encryptionContext, false).ConfigureAwait(false);

if (secret.HasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ namespace Narochno.Credstash.Configuration
{
public class CredstashConfigurationSource : IConfigurationSource
{
private readonly Credstash credstash;
private readonly Dictionary<string, string> encryptionContext;
private readonly Credstash _credstash;
private readonly Dictionary<string, string> _encryptionContext;

public CredstashConfigurationSource(Credstash credstash, Dictionary<string, string> encryptionContext)
{
this.credstash = credstash;
this.encryptionContext = encryptionContext;
_credstash = credstash;
_encryptionContext = encryptionContext;
}

public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new CredstashConfigurationProvider(credstash, encryptionContext);
return new CredstashConfigurationProvider(_credstash, _encryptionContext);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>1.1.1</VersionPrefix>
<VersionPrefix>2.0.0</VersionPrefix>
<Authors>adamhathcock</Authors>
<TargetFramework>netstandard1.3</TargetFramework>
<TargetFrameworks>netstandard1.3;netstandard2.0</TargetFrameworks>
<AssemblyName>Narochno.Credstash.Configuration</AssemblyName>
<PackageId>Narochno.Credstash.Configuration</PackageId>
<PackageTags>credstash</PackageTags>
<PackageProjectUrl>https://github.com/Narochno/Narochno.Credstash</PackageProjectUrl>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Narochno.Credstash\Narochno.Credstash.csproj" />
</ItemGroup>

<ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" />
</ItemGroup>

</Project>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
</ItemGroup>
</Project>
20 changes: 10 additions & 10 deletions src/Narochno.Credstash/Credstash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ namespace Narochno.Credstash
{
public class Credstash
{
private static byte[] INITIALIZATION_VECTOR = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
private static byte[] _initializationVector = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };

private readonly IAmazonKeyManagementService amazonKeyManagementService;
private readonly IAmazonDynamoDB amazonDynamoDb;
private readonly IAmazonKeyManagementService _amazonKeyManagementService;
private readonly IAmazonDynamoDB _amazonDynamoDb;


public Credstash(CredstashOptions options, IAmazonKeyManagementService amazonKeyManagementService, IAmazonDynamoDB amazonDynamoDb)
{
Options = options;
this.amazonKeyManagementService = amazonKeyManagementService;
this.amazonDynamoDb = amazonDynamoDb;
_amazonKeyManagementService = amazonKeyManagementService;
_amazonDynamoDb = amazonDynamoDb;
}

public CredstashOptions Options { get; }
Expand All @@ -38,7 +38,7 @@ public async Task<Optional<string>> GetSecretAsync(string name, string version =
CredstashItem item;
if (version == null)
{
var response = await amazonDynamoDb.QueryAsync(new QueryRequest()
var response = await _amazonDynamoDb.QueryAsync(new QueryRequest()
{
TableName = Options.Table,
Limit = 1,
Expand All @@ -62,7 +62,7 @@ public async Task<Optional<string>> GetSecretAsync(string name, string version =
}
else
{
var response = await amazonDynamoDb.GetItemAsync(new GetItemRequest()
var response = await _amazonDynamoDb.GetItemAsync(new GetItemRequest()
{
TableName = Options.Table,
Key = new Dictionary<string, AttributeValue>()
Expand All @@ -77,7 +77,7 @@ public async Task<Optional<string>> GetSecretAsync(string name, string version =
DecryptResponse decryptResponse;
try
{
decryptResponse = await amazonKeyManagementService.DecryptAsync(new DecryptRequest()
decryptResponse = await _amazonKeyManagementService.DecryptAsync(new DecryptRequest()
{
CiphertextBlob = new MemoryStream(Convert.FromBase64String(item.Key)),
EncryptionContext = encryptionContext
Expand Down Expand Up @@ -114,14 +114,14 @@ public async Task<Optional<string>> GetSecretAsync(string name, string version =
}

IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/NoPadding");
cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), INITIALIZATION_VECTOR));
cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), _initializationVector));
byte[] plaintext = cipher.DoFinal(contents);
return Encoding.UTF8.GetString(plaintext);
}

public async Task<List<CredstashEntry>> ListAsync()
{
var response = await amazonDynamoDb.ScanAsync(new ScanRequest()
var response = await _amazonDynamoDb.ScanAsync(new ScanRequest()
{
TableName = Options.Table,
ProjectionExpression = "#N, version",
Expand Down
2 changes: 1 addition & 1 deletion src/Narochno.Credstash/CredstashOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public class CredstashOptions
/// <summary>
/// If the number of stored items are more than 10, you can decrypt them parally if this number if more than 1. Dop marks the number of maximum parallel requests to DynamoDB and KMS. You need to make sure your read capacity unit is in place.
/// </summary>
public int Dop { get; set; } = 1;
public int DegreeOfParallelism { get; set; } = 1;
}
}
Loading

0 comments on commit 34dd0aa

Please sign in to comment.