Skip to content

Commit eb1997d

Browse files
committed
fix(nas): atomically update alinas credential file via temp file and rename
1 parent 7202992 commit eb1997d

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

pkg/mounter/interceptors/alinas_secret.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,26 @@ func AlinasSecretInterceptor(ctx context.Context, op *mounter.MountOperation, ha
1717
return handler(ctx, op)
1818
}
1919

20-
tmpDir, err := os.MkdirTemp("", "alinas-")
20+
tmpCredFile, err := os.CreateTemp("", op.VolumeID+"-*.credentials")
2121
if err != nil {
2222
return err
2323
}
24+
defer func() {
25+
if err = os.Remove(tmpCredFile.Name()); err != nil && !os.IsNotExist(err) {
26+
klog.ErrorS(err, "Failed to remove temporary alinas credential file", "path", tmpCredFile.Name())
27+
}
28+
}()
2429

2530
credFileContent := makeCredFileContent(op.Secrets)
26-
credFilePath := path.Join(tmpDir, op.VolumeID+".credentials")
27-
if err = os.WriteFile(credFilePath, credFileContent, 0o600); err != nil {
28-
os.RemoveAll(tmpDir) // cleanup in case of error
31+
if _, err = tmpCredFile.Write(credFileContent); err != nil {
32+
return err
33+
}
34+
if err = tmpCredFile.Close(); err != nil {
35+
return err
36+
}
37+
38+
credFilePath := path.Join(os.TempDir(), op.VolumeID+".credentials")
39+
if err = os.Rename(tmpCredFile.Name(), credFilePath); err != nil {
2940
return err
3041
}
3142

0 commit comments

Comments
 (0)