skip to Main Content

I am storing my database password into the Secret value field in the aws secret manager. How do I retrieve the secret value out if I am using the following code?

Secret Key defined in secret key manager: Keykey
Secret value defined in secret key manager : dbPwd

GetSecretValueResponse response = null;
response = client.GetSecretValueAsync(request).Result;
LogWriter.IEConnectLogEvent("GetSecretAsync : " + response.SecretString, "GetSecretAsync");
                   

The above will give me this value : {"Keykey" :"dbPwd"} when I write into a log file. May I know how do I extract the dbPwd out ? Thank you.

2

Answers


  1. Chosen as BEST ANSWER

    I am defining my connectionstring in the web.config.

    <dataConfiguration defaultDatabase="Star.Database" />
    <connectionStrings>
            <add name="Star.Database" connectionString="Data Source=SERVER;Initial Catalog=DATABASE;User Id=USER;Passwor=@Password@" providerName="System.Data.SqlClient" />
        </connectionStrings>
     <caching>
          <sqlCacheDependency enabled="true" pollTime="1000">
            <databases>
              <add name="StarSQLCache" connectionStringName="Star.Database" />
            </databases>
          </sqlCacheDependency>
        </caching>
    

    However as my program is using SqlCacheDependency as shown below . How do I set the connectionString in the SqlCacheDependency? I am thinking of getting the connectionString out first and do a replace with @Password@ with my AWS Key Manager Key value.

      SqlCacheDependency dependency = new SqlCacheDependency("Star.Database", "Lookup");
      
    

  2. Since you receive a JSON value back from the secret manager, you can use a JSON deserializer to retrieve the key value.

    Option 1:

    var data = (JObject)JsonConvert.DeserializeObject(response.SecretString);
    var dbPwd= data["Keykey"].Value<string>();
    

    Option 2:

    var secretString = JsonConvert.DeserializeObject<SecretString>(response.SecretString);
    var dbPwd = secretString.Keykey;
    
    class SecretString
    {
       public string Keykey { get; set;}
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search