Skip to content

s3fs: remove password file after unmounting #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: grpc-01
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "go.sum|^.secrets.baseline$",
"lines": null
},
"generated_at": "2025-05-12T04:31:57Z",
"generated_at": "2025-05-14T06:55:53Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -195,7 +195,7 @@
"hashed_secret": "39f69c278f46165447f30d10acf54277aaa3d5fc",
"is_secret": false,
"is_verified": false,
"line_number": 75,
"line_number": 77,
"type": "Secret Keyword",
"verified_result": null
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const (
// NodeRegionLabel Region Label attached to node
NodeRegionLabel = "topology.kubernetes.io/region"

Timeout = 3 * time.Minute
DefaultSocketPath = "/tmp/mysocket.sock"
WorkerNodeMounterPath = "/var/lib/cos-csi"
Timeout = 3 * time.Minute
DefaultSocketPath = "/tmp/mysocket.sock"
MounterConfigPathOnHost = "/var/lib/cos-csi"
MounterConfigPathOnPodS3fs = "/var/lib/ibmc-s3fs"
MounterConfigPathOnPodRclone = "/root/.config/rclone"
)
4 changes: 2 additions & 2 deletions pkg/mounter/mounter-rclone.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ func (rclone *RcloneMounter) Mount(source string, target string) error {

var configPath string
if mountWorker {
configPath = constants.WorkerNodeMounterPath
configPath = constants.MounterConfigPathOnHost
} else {
configPath = "/root/.config/rclone"
configPath = constants.MounterConfigPathOnPodRclone
}

configPathWithVolID := path.Join(configPath, fmt.Sprintf("%x", sha256.Sum256([]byte(target))))
Expand Down
51 changes: 48 additions & 3 deletions pkg/mounter/mounter-s3fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"os"
"path"
"strings"
"time"

"github.com/IBM/ibm-object-csi-driver/pkg/constants"
"github.com/IBM/ibm-object-csi-driver/pkg/mounter/utils"
Expand Down Expand Up @@ -103,9 +105,9 @@ func (s3fs *S3fsMounter) Mount(source string, target string) error {

var metaRoot string
if mountWorker {
metaRoot = constants.WorkerNodeMounterPath
metaRoot = constants.MounterConfigPathOnHost
} else {
metaRoot = "/var/lib/ibmc-s3fs"
metaRoot = constants.MounterConfigPathOnPodS3fs
}

var bucketName string
Expand Down Expand Up @@ -174,6 +176,7 @@ var writePassWrap = func(pwFileName string, pwFileContent string) error {

func (s3fs *S3fsMounter) Unmount(target string) error {
klog.Info("-S3FSMounter Unmount-")
klog.Infof("Unmount args:\n\ttarget: <%s>", target)

if mountWorker {
klog.Info("Worker Unmounting...")
Expand All @@ -185,10 +188,18 @@ func (s3fs *S3fsMounter) Unmount(target string) error {
if err != nil {
return err
}

cleanupOfs3fsPasswordFile(target)
return nil
}
klog.Info("NodeServer Unmounting...")
return s3fs.MounterUtils.FuseUnmount(target)
err := s3fs.MounterUtils.FuseUnmount(target)
if err != nil {
return err
}

cleanupOfs3fsPasswordFile(target)
return nil
}

func updateS3FSMountOptions(defaultMountOp []string, secretMap map[string]string) []string {
Expand Down Expand Up @@ -323,3 +334,37 @@ func (s3fs *S3fsMounter) formulateMountOptions(bucket, target, passwdFile string
}
return
}

func cleanupOfs3fsPasswordFile(target string) {
Copy link
Member

Choose a reason for hiding this comment

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

Change the definition
cleanupOfs3fsPasswordFile(target string) --> removeS3FSCredFile(credDir, mountPath string)

var metaRoot string
if mountWorker {
metaRoot = constants.MounterConfigPathOnHost
} else {
metaRoot = constants.MounterConfigPathOnPodS3fs
}

metaPath := path.Join(metaRoot, fmt.Sprintf("%x", sha256.Sum256([]byte(target))))

for retry := 1; retry <= 3; retry++ {
Copy link
Member

@mssachan mssachan May 14, 2025

Choose a reason for hiding this comment

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

Suggested change
for retry := 1; retry <= 3; retry++ {
const (
maxRetries = 3
initialDelay = 1 * time.Second
backoffFactor = 2
)
delay := initialDelay
for retry := 1; retry <= maxRetries; retry++ {
_, err := os.Stat(metaPath)
if err != nil {
if os.IsNotExist(err) {
klog.Infof("removeS3FSCredFile: Password file directory does not exist: %s", metaPath)
return
}
klog.Errorf("removeS3FSCredFile: Attempt %d - Failed to stat path %s: %v", retry, metaPath, err)
time.Sleep(delay)
delay *= backoffFactor
continue
}
passwdFile := path.Join(metaPath, passFile)
err = os.Remove(passwdFile)
if err != nil {
klog.Errorf("removeS3FSCredFile: Attempt %d - Failed to remove password file %s: %v", retry, passwdFile, err)
time.Sleep(delay)
delay *= backoffFactor
continue
}
klog.Infof("removeS3FSCredFile: Successfully removed password file: %s", passwdFile)
return
}
klog.Errorf("removeS3FSCredFile: Failed to remove password file after %d attempts", maxRetries)

_, err := os.Stat(metaPath)
if err == nil {
passwdFile := path.Join(metaPath, passFile)
err = os.Remove(passwdFile)
if err != nil {
klog.Errorf("S3FSMounter Unmount: Cannot remove password file %s: %v", metaPath, err)
time.Sleep(500 * time.Millisecond)
continue
}
return
} else {
if os.IsNotExist(err) {
klog.Infof("S3FSMounter Unmount: Password file does not exists%s", metaPath)
return
}
klog.Errorf("S3FSMounter Unmount: Error occurred while fetching path stats for password file")
time.Sleep(500 * time.Millisecond)
continue
}
}
return
}