Skip to content

fix: use safe type assertion and fix path construction in createFolderIfNotExists#3079

Merged
andyzhangx merged 3 commits intokubernetes-sigs:masterfrom
andyzhangx:fix/createFolderIfNotExists-safe-type-assertion
Apr 12, 2026
Merged

fix: use safe type assertion and fix path construction in createFolderIfNotExists#3079
andyzhangx merged 3 commits intokubernetes-sigs:masterfrom
andyzhangx:fix/createFolderIfNotExists-safe-type-assertion

Conversation

@andyzhangx
Copy link
Copy Markdown
Member

What type of PR is this?

/kind bug

What this PR does / why we need it:

Fixes several issues in createFolderIfNotExists:

  1. Unsafe type assertionfileClient.(*azureFileDataplaneClient) is used without comma-ok pattern, which can panic if the type doesn't match. Changed to safe assertion with proper error handling.

  2. Nil dereference risk — When newAzureFileClient returns an error, fileClient could be nil. The original code combined the nil-error check with the type assertion in a single if condition. While Go's short-circuit evaluation prevents this in practice, separating the checks is clearer and safer.

  3. Incorrect path construction with skipped components — The loop used i > 0 to decide whether to prepend "/" to the path. When empty components are skipped (e.g., input "a//b"), i still increments, causing a leading "/" even when currentPath is empty. Changed to check currentPath != "" instead.

  4. Repeated type assertions — The same fileClient.(*azureFileDataplaneClient) assertion was done multiple times. Now stored in a local variable dc.

Which issue(s) this PR fixes:

None

Special notes for your reviewer:

Minimal, targeted fix. No behavioral change for the happy path.

@k8s-ci-robot k8s-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Apr 11, 2026
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Apr 11, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: andyzhangx

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot requested review from cvvz and gnufied April 11, 2026 02:46
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 11, 2026
@andyzhangx andyzhangx requested a review from Copilot April 11, 2026 02:47
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens Driver.createFolderIfNotExists in the Azure File CSI driver by preventing potential panics from unsafe type assertions and fixing incremental directory path construction when input paths contain empty components.

Changes:

  • Replaces an unsafe fileClient.(*azureFileDataplaneClient) assertion with a safe comma-ok assertion and clearer error handling.
  • Avoids repeated type assertions by caching the concrete client in a local variable.
  • Fixes incremental path building to avoid incorrect leading / when empty path components are skipped.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…rIfNotExists

- Replace unsafe type assertion fileClient.(*azureFileDataplaneClient) with
  comma-ok pattern to prevent potential panic
- Separate nil error check from type assertion to avoid dereferencing nil
  fileClient when newAzureFileClient returns an error
- Fix currentPath construction: use currentPath != "" instead of i > 0
  to correctly handle skipped empty components in paths like "a//b"
- Store type assertion result in local variable to avoid repeated assertions
@andyzhangx andyzhangx force-pushed the fix/createFolderIfNotExists-safe-type-assertion branch from 9f74880 to f4e4267 Compare April 11, 2026 02:51
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Apr 11, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens createFolderIfNotExists in the Azure File CSI driver by removing panic-prone type assertions and fixing an edge case in recursive directory path building.

Changes:

  • Replace unsafe type assertions with a safe comma-ok assertion and clearer error handling when creating the Azure File dataplane client.
  • Fix incremental path construction to avoid introducing a leading / when empty path components are skipped.
  • Add a unit test case covering empty accountName.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
pkg/azurefile/azurefile.go Makes client creation/type handling safer and corrects incremental directory path concatenation.
pkg/azurefile/azurefile_test.go Adds a new test case for an empty storage account name input.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 11, 2026
@andyzhangx andyzhangx requested a review from Copilot April 11, 2026 03:06
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens Driver.createFolderIfNotExists in the Azure File driver to avoid panics and to correctly build nested directory paths when creating folders in an Azure File Share.

Changes:

  • Add explicit input validation for accountName and fileShareName, and switch to safe (comma-ok) type assertion for the dataplane client.
  • Improve error handling by separating client creation errors from client-type/client-nil checks and wrapping creation errors with %w.
  • Fix incremental path construction to avoid incorrect leading slashes when empty path components are skipped; add targeted test cases for the new validations.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/azurefile/azurefile.go Adds safer client handling and fixes incremental folder path construction in createFolderIfNotExists.
pkg/azurefile/azurefile_test.go Adds tests for the new early validation errors (empty account name / empty share name).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens Driver.createFolderIfNotExists in the Azure File driver by preventing panics from unsafe type assertions and fixing incorrect nested-path construction when empty path components are present.

Changes:

  • Add early argument validation for accountName / fileShareName and replace an unsafe type assertion with a safe comma-ok assertion plus clearer error handling.
  • Fix incremental directory path construction to avoid introducing a leading / when empty components are skipped.
  • Extend the existing TestCreateFolderIfNotExists table with coverage for empty accountName and empty fileShareName.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
pkg/azurefile/azurefile.go Makes client creation/type handling safer and corrects nested-path building logic in createFolderIfNotExists.
pkg/azurefile/azurefile_test.go Adds test cases for newly introduced early-returns on missing accountName/fileShareName.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens createFolderIfNotExists in the Azure File driver by avoiding unsafe type assertions and fixing incremental path building when folder paths contain empty components.

Changes:

  • Add explicit input validation for accountName and fileShareName, and switch to safe (comma-ok) type assertion for the dataplane client.
  • Improve error handling by separating client creation vs client-nil checks and wrapping client creation errors with %w.
  • Fix incremental path construction by using currentPath != "" rather than the original slice index when inserting separators.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/azurefile/azurefile.go Safer client handling + corrected incremental path building for folder creation.
pkg/azurefile/azurefile_test.go Adds test cases for the new early validation errors.
Comments suppressed due to low confidence (1)

pkg/azurefile/azurefile.go:1499

  • folderName is normalized (trimmed) later for component-wise creation (strings.Trim(folderName, "/")), but the fast-path existence check uses the raw folderName when creating fullPathClient. For inputs with leading/trailing slashes (which isValidFolderName allows), this can cause the pre-check to query a different/invalid path and return a non-404 error, aborting instead of proceeding with recursive creation. Consider normalizing folderName once (e.g., normalized := strings.Trim(folderName, "/")), using that for NewDirectoryClient, logging, and optionally returning early when normalized == "".
	// Performance optimization: First check if the complete directory structure already exists
	// This is the most common case and avoids unnecessary recursive checking
	fullPathClient := shareClient.NewDirectoryClient(folderName)
	_, err = fullPathClient.GetProperties(ctx, nil)
	if err == nil {

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

@andyzhangx: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-azurefile-csi-driver-e2e-capz-windows-2019-hostprocess ce8aa87 link true /test pull-azurefile-csi-driver-e2e-capz-windows-2019-hostprocess

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@andyzhangx andyzhangx merged commit 423e6fb into kubernetes-sigs:master Apr 12, 2026
32 of 34 checks passed
@andyzhangx
Copy link
Copy Markdown
Member Author

/cherrypick release-1.35

@k8s-infra-cherrypick-robot
Copy link
Copy Markdown

@andyzhangx: new pull request created: #3080

Details

In response to this:

/cherrypick release-1.35

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants