Skip to content

Commit d805ca3

Browse files
authored
Merge pull request #2551 from k8s-infra-cherrypick-robot/cherry-pick-2515-to-release-1.32
[release-1.32] fix: allow deleting cross-subscription snapshots
2 parents 678d3c6 + 4bc2e7d commit d805ca3

File tree

3 files changed

+68
-5
lines changed

3 files changed

+68
-5
lines changed

pkg/azurefile/controllerserver.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ func (d *Driver) DeleteVolume(ctx context.Context, req *csi.DeleteVolumeRequest)
736736
if resourceGroupName == "" {
737737
resourceGroupName = d.cloud.ResourceGroup
738738
}
739-
if subsID == "" {
739+
if !isValidSubscriptionID(subsID) {
740740
subsID = d.cloud.SubscriptionID
741741
}
742742

@@ -883,7 +883,7 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ
883883
if rgName == "" {
884884
rgName = d.cloud.ResourceGroup
885885
}
886-
if subsID == "" {
886+
if !isValidSubscriptionID(subsID) {
887887
subsID = d.cloud.SubscriptionID
888888
}
889889

@@ -1021,7 +1021,7 @@ func (d *Driver) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequ
10211021
if len(req.SnapshotId) == 0 {
10221022
return nil, status.Error(codes.InvalidArgument, "Snapshot ID must be provided")
10231023
}
1024-
rgName, accountName, fileShareName, _, _, _, err := GetFileShareInfo(req.SnapshotId) //nolint:dogsled
1024+
rgName, accountName, fileShareName, _, _, subsID, err := GetFileShareInfo(req.SnapshotId) //nolint:dogsled
10251025
if fileShareName == "" || err != nil {
10261026
// According to CSI Driver Sanity Tester, should succeed when an invalid snapshot id is used
10271027
klog.V(4).Infof("failed to get share url with (%s): %v, returning with success", req.SnapshotId, err)
@@ -1035,7 +1035,10 @@ func (d *Driver) DeleteSnapshot(ctx context.Context, req *csi.DeleteSnapshotRequ
10351035
if rgName == "" {
10361036
rgName = d.cloud.ResourceGroup
10371037
}
1038-
subsID := d.cloud.SubscriptionID
1038+
if !isValidSubscriptionID(subsID) {
1039+
subsID = d.cloud.SubscriptionID
1040+
}
1041+
10391042
mc := metrics.NewMetricContext(azureFileCSIDriverName, "controller_delete_snapshot", rgName, subsID, d.Name)
10401043
isOperationSucceeded := false
10411044
defer func() {
@@ -1106,6 +1109,10 @@ func (d *Driver) restoreSnapshot(ctx context.Context, req *csi.CreateVolumeReque
11061109
if srcAccountName == "" || srcFileShareName == "" || dstFileShareName == "" {
11071110
return fmt.Errorf("one or more of srcAccountName(%s), srcFileShareName(%s), dstFileShareName(%s) are empty", srcAccountName, srcFileShareName, dstFileShareName)
11081111
}
1112+
1113+
if !isValidSubscriptionID(srcSubscriptionID) {
1114+
srcSubscriptionID = d.cloud.SubscriptionID
1115+
}
11091116
srcAccountSasToken := dstAccountSasToken
11101117
if srcAccountName != dstAccountName && dstAccountSasToken != "" {
11111118
srcAccountOptions := &storage.AccountOptions{
@@ -1218,7 +1225,7 @@ func (d *Driver) ControllerExpandVolume(ctx context.Context, req *csi.Controller
12181225
if resourceGroupName == "" {
12191226
resourceGroupName = d.cloud.ResourceGroup
12201227
}
1221-
if subsID == "" {
1228+
if !isValidSubscriptionID(subsID) {
12221229
subsID = d.cloud.SubscriptionID
12231230
}
12241231

pkg/azurefile/utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,7 @@ func getFileServiceURL(accountName, storageEndpointSuffix string) string {
359359
}
360360
return fmt.Sprintf(serviceURLTemplate, accountName, storageEndpointSuffix)
361361
}
362+
363+
func isValidSubscriptionID(subsID string) bool {
364+
return regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`).MatchString(subsID)
365+
}

pkg/azurefile/utils_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,3 +976,55 @@ func TestGetFileServiceURL(t *testing.T) {
976976
}
977977
}
978978
}
979+
980+
func TestIsValidSubscriptionID(t *testing.T) {
981+
tests := []struct {
982+
desc string
983+
subsID string
984+
expected bool
985+
}{
986+
{
987+
desc: "valid subscription ID",
988+
subsID: "c9d2281e-dcd5-4dfd-9a97-0d50377cdf76",
989+
expected: true,
990+
},
991+
{
992+
desc: "invalid subscription ID - missing sections",
993+
subsID: "123e4567-e89b-12d3-a456",
994+
expected: false,
995+
},
996+
{
997+
desc: "invalid subscription ID - invalid characters",
998+
subsID: "123e4567-e89b-12d3-a456-42661417400z",
999+
expected: false,
1000+
},
1001+
{
1002+
desc: "invalid subscription ID - empty string",
1003+
subsID: "",
1004+
expected: false,
1005+
},
1006+
{
1007+
desc: "invalid subscription ID - too short",
1008+
subsID: "123e4567",
1009+
expected: false,
1010+
},
1011+
{
1012+
desc: "invalid subscription ID - too long",
1013+
subsID: "123e4567-e89b-12d3-a456-426614174000-extra",
1014+
expected: false,
1015+
},
1016+
{
1017+
desc: "invalid subscription ID - timestamp",
1018+
subsID: "2025-04-15T11:06:21.0000000Z",
1019+
expected: false,
1020+
},
1021+
}
1022+
1023+
for _, test := range tests {
1024+
result := isValidSubscriptionID(test.subsID)
1025+
if result != test.expected {
1026+
t.Errorf("desc: (%s), input: subsID(%s), isValidSubscriptionID returned with bool(%v), not equal to expected(%v)",
1027+
test.desc, test.subsID, result, test.expected)
1028+
}
1029+
}
1030+
}

0 commit comments

Comments
 (0)