Skip to content

Commit f15bb8d

Browse files
committed
chore: add network rule support in Azure account creation
set DefaultActionDeny fix comments
1 parent 4db3a09 commit f15bb8d

File tree

1 file changed

+57
-12
lines changed

1 file changed

+57
-12
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/azure_storageaccount.go

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
26+
"github.com/Azure/go-autorest/autorest/to"
2627

2728
"k8s.io/klog/v2"
2829
)
@@ -32,17 +33,18 @@ type AccountOptions struct {
3233
Name, Type, Kind, ResourceGroup, Location string
3334
EnableHTTPSTrafficOnly bool
3435
Tags map[string]string
36+
VirtualNetworkResourceIDs []string
3537
}
3638

3739
type accountWithLocation struct {
3840
Name, StorageType, Location string
3941
}
4042

41-
// getStorageAccounts gets name, type, location of all storage accounts in a resource group which matches matchingAccountType, matchingLocation
42-
func (az *Cloud) getStorageAccounts(matchingAccountType, matchingAccountKind, resourceGroup, matchingLocation string) ([]accountWithLocation, error) {
43+
// getStorageAccounts get matching storage accounts
44+
func (az *Cloud) getStorageAccounts(accountOptions *AccountOptions) ([]accountWithLocation, error) {
4345
ctx, cancel := getContextWithCancel()
4446
defer cancel()
45-
result, rerr := az.StorageAccountClient.ListByResourceGroup(ctx, resourceGroup)
47+
result, rerr := az.StorageAccountClient.ListByResourceGroup(ctx, accountOptions.ResourceGroup)
4648
if rerr != nil {
4749
return nil, rerr.Error()
4850
}
@@ -51,18 +53,39 @@ func (az *Cloud) getStorageAccounts(matchingAccountType, matchingAccountKind, re
5153
for _, acct := range result {
5254
if acct.Name != nil && acct.Location != nil && acct.Sku != nil {
5355
storageType := string((*acct.Sku).Name)
54-
if matchingAccountType != "" && !strings.EqualFold(matchingAccountType, storageType) {
56+
if accountOptions.Type != "" && !strings.EqualFold(accountOptions.Type, storageType) {
5557
continue
5658
}
5759

58-
if matchingAccountKind != "" && !strings.EqualFold(matchingAccountKind, string(acct.Kind)) {
60+
if accountOptions.Kind != "" && !strings.EqualFold(accountOptions.Kind, string(acct.Kind)) {
5961
continue
6062
}
6163

6264
location := *acct.Location
63-
if matchingLocation != "" && !strings.EqualFold(matchingLocation, location) {
65+
if accountOptions.Location != "" && !strings.EqualFold(accountOptions.Location, location) {
6466
continue
6567
}
68+
69+
if len(accountOptions.VirtualNetworkResourceIDs) > 0 {
70+
if acct.AccountProperties == nil || acct.AccountProperties.NetworkRuleSet == nil ||
71+
acct.AccountProperties.NetworkRuleSet.VirtualNetworkRules == nil {
72+
continue
73+
}
74+
75+
found := false
76+
for _, subnetID := range accountOptions.VirtualNetworkResourceIDs {
77+
for _, rule := range *acct.AccountProperties.NetworkRuleSet.VirtualNetworkRules {
78+
if strings.EqualFold(to.String(rule.VirtualNetworkResourceID), subnetID) && rule.Action == storage.Allow {
79+
found = true
80+
break
81+
}
82+
}
83+
}
84+
if !found {
85+
continue
86+
}
87+
}
88+
6689
accounts = append(accounts, accountWithLocation{Name: *acct.Name, StorageType: storageType, Location: location})
6790
}
6891
}
@@ -106,9 +129,10 @@ func (az *Cloud) EnsureStorageAccount(accountOptions *AccountOptions, genAccount
106129
resourceGroup := accountOptions.ResourceGroup
107130
location := accountOptions.Location
108131
enableHTTPSTrafficOnly := accountOptions.EnableHTTPSTrafficOnly
132+
109133
if len(accountName) == 0 {
110134
// find a storage account that matches accountType
111-
accounts, err := az.getStorageAccounts(accountType, accountKind, resourceGroup, location)
135+
accounts, err := az.getStorageAccounts(accountOptions)
112136
if err != nil {
113137
return "", "", fmt.Errorf("could not list storage accounts for account type %s: %v", accountType, err)
114138
}
@@ -119,6 +143,24 @@ func (az *Cloud) EnsureStorageAccount(accountOptions *AccountOptions, genAccount
119143
}
120144

121145
if len(accountName) == 0 {
146+
// set network rules for storage account
147+
var networkRuleSet *storage.NetworkRuleSet
148+
virtualNetworkRules := []storage.VirtualNetworkRule{}
149+
for _, subnetID := range accountOptions.VirtualNetworkResourceIDs {
150+
vnetRule := storage.VirtualNetworkRule{
151+
VirtualNetworkResourceID: &subnetID,
152+
Action: storage.Allow,
153+
}
154+
virtualNetworkRules = append(virtualNetworkRules, vnetRule)
155+
klog.V(4).Infof("subnetID(%s) has been set", subnetID)
156+
}
157+
if len(virtualNetworkRules) > 0 {
158+
networkRuleSet = &storage.NetworkRuleSet{
159+
VirtualNetworkRules: &virtualNetworkRules,
160+
DefaultAction: storage.DefaultActionDeny,
161+
}
162+
}
163+
122164
// not found a matching account, now create a new account in current resource group
123165
accountName = generateStorageAccountName(genAccountNamePrefix)
124166
if location == "" {
@@ -143,11 +185,14 @@ func (az *Cloud) EnsureStorageAccount(accountOptions *AccountOptions, genAccount
143185
accountName, resourceGroup, location, accountType, kind, accountOptions.Tags)
144186

145187
cp := storage.AccountCreateParameters{
146-
Sku: &storage.Sku{Name: storage.SkuName(accountType)},
147-
Kind: kind,
148-
AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{EnableHTTPSTrafficOnly: &enableHTTPSTrafficOnly},
149-
Tags: tags,
150-
Location: &location}
188+
Sku: &storage.Sku{Name: storage.SkuName(accountType)},
189+
Kind: kind,
190+
AccountPropertiesCreateParameters: &storage.AccountPropertiesCreateParameters{
191+
EnableHTTPSTrafficOnly: &enableHTTPSTrafficOnly,
192+
NetworkRuleSet: networkRuleSet,
193+
},
194+
Tags: tags,
195+
Location: &location}
151196

152197
ctx, cancel := getContextWithCancel()
153198
defer cancel()

0 commit comments

Comments
 (0)