Skip to content

Commit 63df109

Browse files
committed
fix: Add mutex for managing file access
Signed-off-by: onidoru <[email protected]> Signed-off-by: Nikita Kotikov <[email protected]>
1 parent ff49d95 commit 63df109

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

pkg/api/htpasswd.go

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ const (
2020

2121
type HtpasswdClient struct {
2222
credMap credMap
23-
filepath string
23+
credFile credFile
24+
}
25+
26+
type credFile struct {
27+
path string
28+
rw *sync.RWMutex
2429
}
2530

2631
type credMap struct {
@@ -30,7 +35,10 @@ type credMap struct {
3035

3136
func NewHtpasswdClient(filepath string) *HtpasswdClient {
3237
return &HtpasswdClient{
33-
filepath: filepath,
38+
credFile: credFile{
39+
path: filepath,
40+
rw: &sync.RWMutex{},
41+
},
3442
credMap: credMap{
3543
m: make(map[string]string),
3644
rw: &sync.RWMutex{},
@@ -39,12 +47,12 @@ func NewHtpasswdClient(filepath string) *HtpasswdClient {
3947
}
4048

4149
// Init initializes the HtpasswdClient.
42-
// It performs the file read using the filename specified in NewHtpasswdClient
50+
// It performs the credFile read using the filename specified in NewHtpasswdClient
4351
// and caches all user passwords.
4452
func (hc *HtpasswdClient) Init() error {
45-
credsFile, err := os.Open(hc.filepath)
53+
credsFile, err := os.Open(hc.credFile.path)
4654
if err != nil {
47-
return fmt.Errorf("error occurred while opening creds-file: %w", err)
55+
return fmt.Errorf("error occurred while opening creds-credFile: %w", err)
4856
}
4957
defer credsFile.Close()
5058

@@ -63,7 +71,7 @@ func (hc *HtpasswdClient) Init() error {
6371
}
6472

6573
if err := scanner.Err(); err != nil {
66-
return fmt.Errorf("error occurred while reading creds-file: %w", err)
74+
return fmt.Errorf("error occurred while reading creds-credFile: %w", err)
6775
}
6876

6977
return nil
@@ -121,7 +129,7 @@ func (hc *HtpasswdClient) ChangePassword(login, supposedOldPassword, newPassword
121129
return zerr.ErrOldPasswordIsWrong
122130
}
123131

124-
// if passwords match, no need to update file and map, return nil as if operation is successful
132+
// if passwords match, no need to update credFile and map, return nil as if operation is successful
125133
if err := bcrypt.CompareHashAndPassword([]byte(oldPassphrase), []byte(newPassword)); err == nil {
126134
return nil
127135
}
@@ -132,9 +140,9 @@ func (hc *HtpasswdClient) ChangePassword(login, supposedOldPassword, newPassword
132140
return fmt.Errorf("error occurred while encrypting new password: %w", err)
133141
}
134142

135-
file, err := os.ReadFile(hc.filepath)
143+
file, err := os.ReadFile(hc.credFile.path)
136144
if err != nil {
137-
return fmt.Errorf("error occurred while reading creds-file: %w", err)
145+
return fmt.Errorf("error occurred while reading creds-credFile: %w", err)
138146
}
139147

140148
// read passwords line by line to find the corresponding login
@@ -149,41 +157,39 @@ func (hc *HtpasswdClient) ChangePassword(login, supposedOldPassword, newPassword
149157
}
150158
}
151159

152-
// write new content to temporary file
153-
// and replace the old file with temporary, so the operation is atomic
160+
// write new content to temporary credFile
161+
// and replace the old credFile with temporary, so the operation is atomic
154162
output := []byte(strings.Join(lines, "\n"))
155163

156-
tmpfile, err := os.CreateTemp(filepath.Dir(hc.filepath), "htpasswd-*.tmp")
164+
tmpfile, err := os.CreateTemp(filepath.Dir(hc.credFile.path), "htpasswd-*.tmp")
157165
if err != nil {
158-
return fmt.Errorf("error occurred when creating temp htpasswd file: %w", err)
166+
return fmt.Errorf("error occurred when creating temp htpasswd credFile: %w", err)
159167
}
160168

161169
if _, err := tmpfile.Write(output); err != nil {
162170
tmpfile.Close()
163171
os.Remove(tmpfile.Name())
164-
return fmt.Errorf("error occurred when writing to temp htpasswd file: %w", err)
172+
173+
return fmt.Errorf("error occurred when writing to temp htpasswd credFile: %w", err)
165174
}
166175

167176
if err := tmpfile.Close(); err != nil {
168177
os.Remove(tmpfile.Name())
169-
return fmt.Errorf("error occurred when closing temp htpasswd file: %w", err)
178+
179+
return fmt.Errorf("error occurred when closing temp htpasswd credFile: %w", err)
170180
}
171181

172-
if err := os.Rename(tmpfile.Name(), hc.filepath); err != nil {
173-
return fmt.Errorf("error occurred while replacing htpasswd file with new file: %w", err)
182+
if err := os.Rename(tmpfile.Name(), hc.credFile.path); err != nil {
183+
return fmt.Errorf("error occurred while replacing htpasswd credFile with new credFile: %w", err)
174184
}
175185

176-
err = os.WriteFile(hc.filepath, output, constants.DefaultDirPerms)
186+
err = os.WriteFile(hc.credFile.path, output, constants.DefaultDirPerms)
177187
if err != nil {
178-
return fmt.Errorf("error occurred while writing to creds-file: %w", err)
188+
return fmt.Errorf("error occurred while writing to creds-credFile: %w", err)
179189
}
180190

181-
// set to credMap only if all file operations are successful to prevent collisions
182-
hc.credMap.rw.Lock()
183-
hc.credMap.m[login] = string(newPassphrase)
184-
hc.credMap.rw.Unlock()
185-
186-
return nil
191+
// set to credMap only if all credFile operations are successful to prevent collisions
192+
return hc.credMap.Set(login, string(newPassphrase))
187193
}
188194

189195
func (c credMap) Set(login, passphrase string) error {

0 commit comments

Comments
 (0)