Created
May 1, 2019 20:10
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
var chain = new CredentialProfileStoreChain(); | |
if (chain.TryGetAWSCredentials("PROFILE_NAME", out var profile)) | |
{ | |
GetSecret(profile); | |
} | |
} | |
/* | |
* Use this code snippet in your app. | |
* If you need more information about configurations or implementing the sample code, visit the AWS docs: | |
* https://aws.amazon.com/developers/getting-started/net/ | |
* | |
* Make sure to include the following packages in your code. | |
* | |
* using System; | |
* using System.IO; | |
* | |
* using Amazon; | |
* using Amazon.SecretsManager; | |
* using Amazon.SecretsManager.Model; | |
* | |
*/ | |
/* | |
* AWSSDK.SecretsManager version="3.3.0" targetFramework="net45" | |
* AWSSDK.SecurityToken version="3.3.30" | |
*/ | |
public static void GetSecret(AWSCredentials credential) | |
{ | |
string secretName = "test"; | |
string region = "ap-northeast-1"; | |
string secret = ""; | |
MemoryStream memoryStream = new MemoryStream(); | |
IAmazonSecretsManager client = new AmazonSecretsManagerClient(credential, 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. | |
try | |
{ | |
response = client.GetSecretValueAsync(request).Result; | |
} | |
catch (DecryptionFailureException e) | |
{ | |
// 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; | |
} | |
catch (InternalServiceErrorException e) | |
{ | |
// An error occurred on the server side. | |
// Deal with the exception here, and/or rethrow at your discretion. | |
throw; | |
} | |
catch (InvalidParameterException e) | |
{ | |
// You provided an invalid value for a parameter. | |
// Deal with the exception here, and/or rethrow at your discretion | |
throw; | |
} | |
catch (InvalidRequestException e) | |
{ | |
// You provided a parameter value that is not valid for the current state of the resource. | |
// Deal with the exception here, and/or rethrow at your discretion. | |
throw; | |
} | |
catch (ResourceNotFoundException e) | |
{ | |
// We can't find the resource that you asked for. | |
// Deal with the exception here, and/or rethrow at your discretion. | |
throw; | |
} | |
catch (System.AggregateException ae) | |
{ | |
// More than one of the above exceptions were triggered. | |
// Deal with the exception here, and/or rethrow at your discretion. | |
throw; | |
} | |
// 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())); | |
} | |
// Your code goes here. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment