Skip to content

Commit

Permalink
Add missing usage assignment
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Turrado <[email protected]>
  • Loading branch information
JorTurFer committed Dec 31, 2023
1 parent 9be7ebb commit 3f0ebb5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 6 additions & 4 deletions pkg/scalers/aws/aws_config_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"os"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/go-logr/logr"
"golang.org/x/crypto/sha3"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

Expand All @@ -40,8 +40,8 @@ func (a *sharedConfigCache) getCacheKey(awsAuthorization AuthorizationMetadata)
key = awsAuthorization.AwsRoleArn
}
// to avoid sensitive data as key and to use a constant key size,
// we hash the key with sha1
hash := sha1.Sum([]byte(key))
// we hash the key with sha3
hash := sha3.Sum224([]byte(key))
return hex.EncodeToString(hash[:])
}

Expand Down Expand Up @@ -72,7 +72,9 @@ func (a *sharedConfigCache) GetCredentials(ctx context.Context, awsRegion string

newCacheEntry := cacheEntry{
config: &cfg,
usages: map[string]bool{},
usages: map[string]bool{
awsAuthorization.ScalerUniqueKey: true,
},
}
a.items[key] = newCacheEntry

Expand Down
10 changes: 7 additions & 3 deletions pkg/scalers/aws/aws_config_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ func TestGetCredentialsReturnNewItemAndStoreItIfNotExist(t *testing.T) {
ScalerUniqueKey: "test-key",
},
}
cacheKey := cache.getCacheKey(config.awsAuthorization)
_, err := cache.GetCredentials(context.Background(), config.awsRegion, config.awsAuthorization)
assert.NoError(t, err)
assert.Contains(t, cache.items, cache.getCacheKey(config.awsAuthorization))
assert.Contains(t, cache.items, cacheKey)
assert.Contains(t, cache.items[cacheKey].usages, config.awsAuthorization.ScalerUniqueKey)
}

func TestGetCredentialsReturnCachedItemIfExist(t *testing.T) {
Expand All @@ -34,15 +36,17 @@ func TestGetCredentialsReturnCachedItemIfExist(t *testing.T) {
}
cfg := aws.Config{}
cfg.AppID = "test1-app"
cache.items[cache.getCacheKey(config.awsAuthorization)] = cacheEntry{
cacheKey := cache.getCacheKey(config.awsAuthorization)
cache.items[cacheKey] = cacheEntry{
config: &cfg,
usages: map[string]bool{
config.awsAuthorization.ScalerUniqueKey: true,
"other-usage": true,
},
}
configFromCache, err := cache.GetCredentials(context.Background(), config.awsRegion, config.awsAuthorization)
assert.NoError(t, err)
assert.Equal(t, &cfg, configFromCache)
assert.Contains(t, cache.items[cacheKey].usages, config.awsAuthorization.ScalerUniqueKey)
}

func TestRemoveCachedEntryRemovesCachedItemIfNotUsages(t *testing.T) {
Expand Down

0 comments on commit 3f0ebb5

Please sign in to comment.