using System; using System.IO; using Amazon; using Amazon.SecretsManager; using Amazon.SecretsManager.Model; using Newtonsoft.Json; using classes; public static class SecretsManager { public static DbUser GetDBSecret() { string secret = ""; string secretName = "arn:aws:secretsmanager:eu-west-1:475415325198:secret:pmf-prod-rds-6mUVsR"; MemoryStream memoryStream = new MemoryStream(); IAmazonSecretsManager client = new AmazonSecretsManagerClient(RegionEndpoint.GetBySystemName(Constants.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 try { response = client.GetSecretValueAsync(request).Result; } catch (DecryptionFailureException ex) { // Secrets Manager can't decrypt the protected secret text using the provided KMS key. // Deal with the exception here, and/or rethrow at your discretion. throw ex; } catch (InternalServiceErrorException ex) { // An error occurred on the server side. // Deal with the exception here, and/or rethrow at your discretion. throw ex; } catch (Amazon.CognitoIdentity.Model.InvalidParameterException ex) { // You provided an invalid value for a parameter. throw ex; //this will be plugged into the cs and result in creds error } catch (InvalidRequestException ex) { // You provided a parameter value that is not valid for the current state of the resource. throw ex; } catch (Amazon.CognitoIdentity.Model.ResourceNotFoundException ex) { // We can't find the resource that you asked for. throw ex; } catch (System.AggregateException ae) { // More than one of the above exceptions were triggered. throw ae; } // 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; return JsonConvert.DeserializeObject(secret); } else { memoryStream = response.SecretBinary; StreamReader reader = new StreamReader(memoryStream); string decodedBinarySecret = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(reader.ReadToEnd())); return JsonConvert.DeserializeObject(decodedBinarySecret); } } public static SesUser GetSesUser() { string secret = ""; string secretName = "arn:aws:secretsmanager:eu-west-1:475415325198:secret:pmf-prod-ses-smtp-0oR1H2"; MemoryStream memoryStream = new MemoryStream(); IAmazonSecretsManager client = new AmazonSecretsManagerClient(RegionEndpoint.GetBySystemName(Constants.REGION)); GetSecretValueRequest request = new GetSecretValueRequest(); request.SecretId = secretName; request.VersionStage = "AWSCURRENT"; // VersionStage defaults to AWSCURRENT if unspecified. GetSecretValueResponse response = null; try { response = client.GetSecretValueAsync(request).Result; } catch (DecryptionFailureException ex) { throw ex; } catch (InternalServiceErrorException ex) { throw ex; } catch (Amazon.CognitoIdentity.Model.InvalidParameterException ex) { // You provided an invalid value for a parameter. throw ex; //this will be plugged into the cs and result in creds error } catch (InvalidRequestException ex) { // You provided a parameter value that is not valid for the current state of the resource. throw ex; } catch (Amazon.CognitoIdentity.Model.ResourceNotFoundException ex) { throw ex; } catch (System.AggregateException ae) { // More than one of the above exceptions were triggered. throw ae; } if (response.SecretString != null) { secret = response.SecretString; return JsonConvert.DeserializeObject(secret); } else { memoryStream = response.SecretBinary; StreamReader reader = new StreamReader(memoryStream); string decodedBinarySecret = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(reader.ReadToEnd())); return JsonConvert.DeserializeObject(decodedBinarySecret); } } }