aws / aws-secretsmanager-caching-net

The AWS Secrets Manager .NET caching client enables in-process caching of secrets for .NET applications.
https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets_cache-net.html
Apache License 2.0
57 stars 23 forks source link

Duplicate calls to CacheHook methods #138

Closed kilzdavi closed 9 months ago

kilzdavi commented 1 year ago

When using the cache hook with the caching client, Get() and Put() methods are called twice called twice when obtaining values using GetSecretString().

To Reproduce

  1. Create implementation of SecretCacheHook using ISecretCacheHook
  2. Configure Get and put methods to write console secret value to console.
  3. Call cache.GetSecretString() grab a secret value.

Expected behavior Either Get or Put to write to console a single time.

Result Get or Put methods are called twice.

Environment .NET 6, MacOS 13.5.2, Rider IDE.

Additional context

I am using the caching client through dependency injection.

I was tracing the call stack and GetSecretString is calling GetSecretValue(). GetSecretValue() calls GetResult() [Triggers cachehook] and GetSecretValueAsync() GetSecretValueAsync() again calls GetSecretValue() and GetResult()[Triggering cache hook]

I hope this makes sense

benjaminkz commented 9 months ago

Hi kilzdavi! Get and Put methods are called twice in a single GetSecretString invocation when you fetch the secret from caching client for the first time. This is an expected behavior. Following is the break down of calls. Please notice that there are actually two caches in the memory.

Both DescribeSecretResponse and GetSecretValueResponse are processed through the Put method for caching, and the Get method for retrieval. And hence you notice twice invocation of them in a single GetSecretString operation.

If you are tracking Get and Put calls from the hook, please take a look here. They get called in GetResult and SetResult. Both SecretCacheItem and SecretCacheVersion extend SecretCacheObject.