using Amazon; using Amazon.SecretsManager; using Amazon.SecretsManager.Model; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Text; namespace IDE_UITest.Helper { public class AwsHelper { public static Secret GetSecret(string secretName, string region) { //string secretName = ConfigurationManager.AppSettings.Get("AwsProfileSecretArn"); //string region = ConfigurationManager.AppSettings.Get("AwsRegion"); string secret = ""; MemoryStream memoryStream = new MemoryStream(); IAmazonSecretsManager client = new AmazonSecretsManagerClient(RegionEndpoint.GetBySystemName(region)); GetSecretValueRequest request = new GetSecretValueRequest(); request.SecretId = secretName; request.VersionStage = "AWSCURRENT"; // VersionStage defaults to AWSCURRENT if unspecified. GetSecretValueResponse response = null; // In this sample we only handle the specific exceptions for the 'GetSecretValue' API. // See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html // We rethrow the exception by default. response = client.GetSecretValueAsync(request).Result; // Decrypts secret using the associated KMS CMK. // Depending on whether the secret is a string or binary, one of these fields will be populated. if (response.SecretString != null) { secret = response.SecretString; } else { memoryStream = response.SecretBinary; StreamReader reader = new StreamReader(memoryStream); string decodedBinarySecret = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(reader.ReadToEnd())); } var secretObject = JsonConvert.DeserializeObject(secret); return secretObject; } } public class Secret { public string test_role_access_key; public string test_role_secret_key; } }