Skip to content

Instantly share code, notes, and snippets.

@PrzemekMalak
Last active March 1, 2018 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrzemekMalak/3dcaa8e3699864c1452b49b257171d2e to your computer and use it in GitHub Desktop.
Save PrzemekMalak/3dcaa8e3699864c1452b49b257171d2e to your computer and use it in GitHub Desktop.
using Amazon.SimpleSystemsManagement;
using Amazon.SimpleSystemsManagement.Model;
using System.Threading.Tasks;
namespace AWSParameterStore
{
public class ParameterAWSStore
{
private static readonly string databaseString = "DATABASE_HOST";
private static readonly string databaseName = "DATABASE_NAME";
private static readonly string databaseUser = "DATABASE_USER";
private static readonly string databasePassword = "DATABASE_PASSWORD";
public static async Task<string> GetDatabaseHost(bool encrypted)
{
return await getParameter(databaseString, encrypted);
}
public static async Task<string> GetDatabaseName(bool encrypted)
{
return await getParameter(databaseName, encrypted);
}
public static async Task<string> GetDatabaseUser(bool encrypted)
{
return await getParameter(databaseUser, encrypted);
}
public static async Task<string> GetDatabasePassword(bool encrypted)
{
return await getParameter(databasePassword, encrypted);
}
public static async Task<bool> SetParameter(string parameterName, string parameterValue, bool withEncryption)
{
var client = getClient();
var request = putRequest(parameterName, parameterValue, withEncryption);
if (withEncryption)
{
request.Type = ParameterType.SecureString;
}
else
{
request.Type = ParameterType.String;
}
var response = await client.PutParameterAsync(request);
return response.HttpStatusCode == System.Net.HttpStatusCode.OK;
}
private static GetParameterRequest getRequest(string parameterName, bool withDecryption)
{
var request = new GetParameterRequest();
request.Name = parameterName;
request.WithDecryption = withDecryption;
return request;
}
private static PutParameterRequest putRequest(string parameterName, string parameterValue, bool withDecryption)
{
var request = new PutParameterRequest();
request.Name = parameterName;
request.Value = parameterValue;
request.Overwrite = true;
request.Type = withDecryption ? ParameterType.SecureString : ParameterType.String;
return request;
}
private static async Task<string> getParameter(string parameterName, bool encrypted)
{
var client = getClient();
var result = await client.GetParameterAsync(getRequest(parameterName, encrypted));
return result.Parameter.Value.ToString();
}
private static AmazonSimpleSystemsManagementClient getClient()
{
var client = new AmazonSimpleSystemsManagementClient(".accessKeyId.", ".secretAccessKey.", Amazon.RegionEndpoint.EUCentral1);
return client;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment