Skip to content

Commit

Permalink
feat: add GetLatestAccountKey in account key fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Jun 8, 2023
1 parent 03199ff commit 0c3e238
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
37 changes: 32 additions & 5 deletions pkg/provider/azure_storageaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"math/big"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"
"github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns"
Expand Down Expand Up @@ -74,6 +75,7 @@ type AccountOptions struct {
SubnetName string
AccessTier string
MatchTags bool
GetLatestAccountKey bool
EnableBlobVersioning *bool
SoftDeleteBlobs int32
SoftDeleteContainers int32
Expand Down Expand Up @@ -125,7 +127,8 @@ func (az *Cloud) getStorageAccounts(ctx context.Context, accountOptions *Account
}

// GetStorageAccesskey gets the storage account access key
func (az *Cloud) GetStorageAccesskey(ctx context.Context, subsID, account, resourceGroup string) (string, error) {
// getLatestAccountKey: get the latest account key per CreationTime if true, otherwise get the first account key
func (az *Cloud) GetStorageAccesskey(ctx context.Context, subsID, account, resourceGroup string, getLatestAccountKey bool) (string, error) {
if az.StorageAccountClient == nil {
return "", fmt.Errorf("StorageAccountClient is nil")
}
Expand All @@ -138,16 +141,40 @@ func (az *Cloud) GetStorageAccesskey(ctx context.Context, subsID, account, resou
return "", fmt.Errorf("empty keys")
}

var key string
var creationTime time.Time

for _, k := range *result.Keys {
if k.Value != nil && *k.Value != "" {
v := *k.Value
if ind := strings.LastIndex(v, " "); ind >= 0 {
v = v[(ind + 1):]
}
return v, nil
if !getLatestAccountKey {
// get first key
return v, nil
}
// get account key with latest CreationTime
if key == "" {
key = v
if k.CreationTime != nil {
creationTime = k.CreationTime.ToTime()
}
klog.V(6).Infof("got storage account key with creation time: %v", creationTime)
} else {
if k.CreationTime != nil && creationTime.Before(k.CreationTime.ToTime()) {
key = v
creationTime = k.CreationTime.ToTime()
klog.V(2).Infof("got storage account key with latest creation time: %v", creationTime)
}
}
}
}
return "", fmt.Errorf("no valid keys")

if key == "" {
return "", fmt.Errorf("no valid keys")
}
return key, nil
}

// EnsureStorageAccount search storage account, create one storage account(with genAccountNamePrefix) if not found, return accountName, accountKey
Expand Down Expand Up @@ -239,7 +266,7 @@ func (az *Cloud) EnsureStorageAccount(ctx context.Context, accountOptions *Accou
createNewAccount = false
if accountOptions.CreateAccount {
// check whether account exists
if _, err := az.GetStorageAccesskey(ctx, subsID, accountName, resourceGroup); err != nil {
if _, err := az.GetStorageAccesskey(ctx, subsID, accountName, resourceGroup, accountOptions.GetLatestAccountKey); err != nil {
klog.V(2).Infof("get storage key for storage account %s returned with %v", accountName, err)
createNewAccount = true
}
Expand Down Expand Up @@ -469,7 +496,7 @@ func (az *Cloud) EnsureStorageAccount(ctx context.Context, accountOptions *Accou
}

// find the access key with this account
accountKey, err := az.GetStorageAccesskey(ctx, subsID, accountName, resourceGroup)
accountKey, err := az.GetStorageAccesskey(ctx, subsID, accountName, resourceGroup, accountOptions.GetLatestAccountKey)
if err != nil {
return "", "", fmt.Errorf("could not get storage key for storage account %s: %w", accountName, err)
}
Expand Down
48 changes: 41 additions & 7 deletions pkg/provider/azure_storageaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2022-07-01/network"
"github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns"
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-09-01/storage"
"github.com/Azure/go-autorest/autorest/date"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"k8s.io/utils/pointer"
Expand All @@ -51,20 +53,27 @@ func TestGetStorageAccessKeys(t *testing.T) {

cloud := &Cloud{}
value := "foo bar"
oldTime := date.Time{}
oldTime.Time = time.Now().Add(-time.Hour)
newValue := "newkey"
newTime := date.Time{}
newTime.Time = time.Now()

tests := []struct {
results storage.AccountListKeysResult
expectedKey string
expectErr bool
err error
results storage.AccountListKeysResult
getLatestAccountKey bool
expectedKey string
expectErr bool
err error
}{
{storage.AccountListKeysResult{}, "", true, nil},
{storage.AccountListKeysResult{}, false, "", true, nil},
{
storage.AccountListKeysResult{
Keys: &[]storage.AccountKey{
{Value: &value},
},
},
false,
"bar",
false,
nil,
Expand All @@ -76,18 +85,43 @@ func TestGetStorageAccessKeys(t *testing.T) {
{Value: &value},
},
},
false,
"bar",
false,
nil,
},
{
storage.AccountListKeysResult{
Keys: &[]storage.AccountKey{
{Value: &value, CreationTime: &oldTime},
{Value: &newValue, CreationTime: &newTime},
},
},
true,
"newkey",
false,
nil,
},
{
storage.AccountListKeysResult{
Keys: &[]storage.AccountKey{
{Value: &value, CreationTime: &oldTime},
{Value: &newValue, CreationTime: &newTime},
},
},
false,
"bar",
false,
nil,
},
{storage.AccountListKeysResult{}, "", true, fmt.Errorf("test error")},
{storage.AccountListKeysResult{}, false, "", true, fmt.Errorf("test error")},
}

for _, test := range tests {
mockStorageAccountsClient := mockstorageaccountclient.NewMockInterface(ctrl)
cloud.StorageAccountClient = mockStorageAccountsClient
mockStorageAccountsClient.EXPECT().ListKeys(gomock.Any(), "", "rg", gomock.Any()).Return(test.results, nil).AnyTimes()
key, err := cloud.GetStorageAccesskey(ctx, "", "acct", "rg")
key, err := cloud.GetStorageAccesskey(ctx, "", "acct", "rg", test.getLatestAccountKey)
if test.expectErr && err == nil {
t.Errorf("Unexpected non-error")
continue
Expand Down

0 comments on commit 0c3e238

Please sign in to comment.