Skip to content

Panic when using secretcache with an incomplete config #12

@merschformann

Description

@merschformann

Describe the bug

When using secretcache with an incomplete config (not setting all fields) it results in a panic on secrets.GetSecretString.
I.e., the following code panicks:

config := secretcache.CacheConfig{
	// Uncomment the defaults below to make the example work again
	// MaxCacheSize: secretcache.DefaultMaxCacheSize,
	// VersionStage: secretcache.DefaultVersionStage,
	CacheItemTTL: 30 * 1000 * 1000 * 1000,
}
secrets, err := secretcache.New(
	func(c *secretcache.Cache) { c.CacheConfig = config },
)
if err != nil {
	panic(fmt.Errorf("error creating secret cache: %w", err))
}
secrets.GetSecretString("foo")

Find a minimal working example also here: https://github.com/merschformann/aws-secrets-manager-panic

Expected Behavior

Expected to receive an error from either secretcache.New for an incomplete configuration or cache.GetSecretString for an invalid secret ID. It should not panic.

Current Behavior

The example panics due to a nil pointer dereference.
This is the output:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x72bb7d]

goroutine 1 [running]:
github.com/aws/aws-secretsmanager-caching-go/secretcache.(*secretCacheItem).getSecretValue(0x0, {0x7b976e?, 0x3?})
        github.com/aws/aws-secretsmanager-caching-go@v1.1.0/secretcache/cacheItem.go:168 +0x9d
github.com/aws/aws-secretsmanager-caching-go/secretcache.(*Cache).GetSecretStringWithStage(0xc00018fef0?, {0x7b76e4?, 0x43cec5?}, {0x7b976e, 0xa})
        github.com/aws/aws-secretsmanager-caching-go@v1.1.0/secretcache/cache.go:94 +0x3b
github.com/aws/aws-secretsmanager-caching-go/secretcache.(*Cache).GetSecretString(...)
        github.com/aws/aws-secretsmanager-caching-go@v1.1.0/secretcache/cache.go:86
main.main()
        aws-secrets-manager-panic/main.go:22 +0xdd
exit status 2

Reproduction Steps

Just run the minimal example here (go run main.go) or copy above code to a file and run it.

Possible Solution

If there are assumptions about the config (e.g.: MaxCacheSize > 0), they may be checked on secretcache.New. A descriptive error could be returned, if something is not met.

Version used

v1.1.0

Environment details (Version of Go (go version)? OS name and version, etc.)

go version go1.19.1 linux/amd64

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions