Skip to content

Commit 46fa528

Browse files
committed
fix: improvements after code review; try to fix tests
Signed-off-by: onidoru <[email protected]>
1 parent 4bb80a9 commit 46fa528

File tree

11 files changed

+31
-119
lines changed

11 files changed

+31
-119
lines changed

errors/errors.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,11 @@ var (
169169
ErrURLNotFound = errors.New("url not found")
170170
ErrInvalidSearchQuery = errors.New("invalid search query")
171171

172-
// ErrUserIsNotFound returned if the user is not found.
173-
ErrUserIsNotFound = errors.New("user is not found")
174172
// ErrPasswordsDoNotMatch returned if given password does not match existing user's password.
175173
ErrPasswordsDoNotMatch = errors.New("passwords do not match")
176174
// ErrOldPasswordIsWrong returned if provided old password for user verification
177175
// during the password change is wrong.
178176
ErrOldPasswordIsWrong = errors.New("old password is wrong")
179-
// ErrPasswordIsEmpty returned if user's new password is empty
177+
// ErrPasswordIsEmpty returned if user's new password is empty.
180178
ErrPasswordIsEmpty = errors.New("password can not be empty")
181179
)

pkg/api/controller.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type Controller struct {
5050
CookieStore *CookieStore
5151
LDAPClient *LDAPClient
5252
taskScheduler *scheduler.Scheduler
53-
htpasswdClient *HtpasswdClient
53+
HtpasswdClient *HtpasswdClient
5454
// runtime params
5555
chosenPort int // kernel-chosen port
5656
}
@@ -100,9 +100,7 @@ func (c *Controller) Run() error {
100100
return err
101101
}
102102

103-
if err := c.initHtpasswdClient(); err != nil {
104-
return err
105-
}
103+
c.StartBackgroundTasks()
106104

107105
// setup HTTP API router
108106
engine := mux.NewRouter()
@@ -246,6 +244,12 @@ func (c *Controller) Init() error {
246244

247245
c.InitCVEInfo()
248246

247+
if c.Config.IsHtpasswdAuthEnabled() {
248+
if err := c.initHtpasswdClient(); err != nil {
249+
return err
250+
}
251+
}
252+
249253
return nil
250254
}
251255

@@ -285,9 +289,9 @@ func (c *Controller) initCookieStore() error {
285289

286290
func (c *Controller) initHtpasswdClient() error {
287291
if c.Config.IsHtpasswdAuthEnabled() {
288-
c.htpasswdClient = NewHtpasswdClient(c.Config.HTTP.Auth.HTPasswd.Path)
292+
c.HtpasswdClient = NewHtpasswdClient(c.Config.HTTP.Auth.HTPasswd.Path)
289293

290-
return c.htpasswdClient.Init()
294+
return c.HtpasswdClient.Init()
291295
}
292296

293297
return nil

pkg/api/controller_test.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,60 +4629,6 @@ func TestAuthorization(t *testing.T) {
46294629
})
46304630
}
46314631

4632-
func TestChangePassword(t *testing.T) {
4633-
Convey("Make a new controller", t, func() {
4634-
port := test.GetFreePort()
4635-
baseURL := test.GetBaseURL(port)
4636-
conf := config.New()
4637-
conf.HTTP.Port = port
4638-
username, seedUser := test.GenerateRandomString()
4639-
password, seedPass := test.GenerateRandomString()
4640-
htpasswdPath := test.MakeHtpasswdFileFromString(test.GetCredString(username, password))
4641-
defer os.Remove(htpasswdPath)
4642-
4643-
conf.HTTP.Auth = &config.AuthConfig{
4644-
HTPasswd: config.AuthHTPasswd{
4645-
Path: htpasswdPath,
4646-
},
4647-
}
4648-
conf.HTTP.AccessControl = &config.AccessControlConfig{
4649-
Repositories: config.Repositories{
4650-
test.AuthorizationAllRepos: config.PolicyGroup{
4651-
Policies: []config.Policy{
4652-
{
4653-
Users: []string{},
4654-
Actions: []string{},
4655-
},
4656-
},
4657-
DefaultPolicy: []string{},
4658-
},
4659-
},
4660-
AdminPolicy: config.Policy{
4661-
Users: []string{},
4662-
Actions: []string{},
4663-
},
4664-
}
4665-
4666-
Convey("with basic auth", func() {
4667-
ctlr := api.NewController(conf)
4668-
ctlr.Config.Storage.RootDirectory = t.TempDir()
4669-
4670-
err := WriteImageToFileSystem(CreateDefaultImage(), "zot-test", "0.0.1",
4671-
ociutils.GetDefaultStoreController(ctlr.Config.Storage.RootDirectory, ctlr.Log))
4672-
So(err, ShouldBeNil)
4673-
4674-
cm := test.NewControllerManager(ctlr)
4675-
cm.StartAndWait(port)
4676-
defer cm.StopServer()
4677-
4678-
client := resty.New()
4679-
client.SetBasicAuth(username, password)
4680-
4681-
RunAuthorizationTests(t, client, baseURL, username, conf)
4682-
})
4683-
})
4684-
}
4685-
46864632
func TestGetUsername(t *testing.T) {
46874633
Convey("Make a new controller", t, func() {
46884634
port := test.GetFreePort()

pkg/api/htpasswd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (hc *HtpasswdClient) Set(login, password string) error {
9292
func (hc *HtpasswdClient) CheckPassword(login, password string) error {
9393
passwordHash, ok := hc.Get(login)
9494
if !ok {
95-
return zerr.ErrUserIsNotFound
95+
return zerr.ErrBadUser
9696
}
9797

9898
err := bcrypt.CompareHashAndPassword([]byte(passwordHash), []byte(password))
@@ -115,7 +115,7 @@ func (hc *HtpasswdClient) ChangePassword(login, supposedOldPassword, newPassword
115115
hc.credMap.rw.RUnlock()
116116

117117
if !ok {
118-
return zerr.ErrUserIsNotFound
118+
return zerr.ErrBadUser
119119
}
120120

121121
// given old password must match actual old password

pkg/api/htpasswd_test.go

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestHtpasswdClient_ChangePassword(t *testing.T) {
2727

2828
Convey("change for non-existing login", func() {
2929
err := client.ChangePassword("non-existing", "old_password", "new_password")
30-
So(err, ShouldEqual, zerr.ErrUserIsNotFound)
30+
So(err, ShouldEqual, zerr.ErrBadUser)
3131
})
3232

3333
Convey("change with wrong old oldPassword", func() {
@@ -96,7 +96,7 @@ func TestHtpasswdClient_CheckPassword(t *testing.T) {
9696

9797
Convey("check for non-existing login", func() {
9898
err := client.CheckPassword("non-existing", "password")
99-
So(err, ShouldEqual, zerr.ErrUserIsNotFound)
99+
So(err, ShouldEqual, zerr.ErrBadUser)
100100
})
101101

102102
Convey("check with wrong password", func() {
@@ -146,38 +146,3 @@ func TestHtpasswdClient_Init(t *testing.T) {
146146
})
147147
})
148148
}
149-
150-
//
151-
// func Test_credMap_Get(t *testing.T) {
152-
// credsMap := credMap{
153-
// m: map[string]string{"testuser": "testpassword"},
154-
// rw: &sync.RWMutex{},
155-
// }
156-
//
157-
// Convey("test credMap Get", t, func() {
158-
// Convey("should get existing password", func() {
159-
// passhprase, ok := credsMap.Get("testuser")
160-
// So(ok, ShouldBeTrue)
161-
// So(passhprase, ShouldEqual, "testpassword")
162-
// })
163-
//
164-
// Convey("should not get unexisting password", func() {
165-
// passhprase, ok := credsMap.Get("non-existing")
166-
// So(ok, ShouldBeFalse)
167-
// So(passhprase, ShouldBeBlank)
168-
// })
169-
// })
170-
// }
171-
//
172-
// func Test_credMap_Set(t *testing.T) {
173-
// credsMap := credMap{
174-
// m: make(map[string]string),
175-
// rw: &sync.RWMutex{},
176-
// }
177-
//
178-
// Convey("should set password", t, func() {
179-
// err := credsMap.Set("testuser", "testpassword")
180-
// So(err, ShouldBeNil)
181-
// So(credsMap.m["testuser"], ShouldNotBeEmpty)
182-
// })
183-
// }

pkg/api/routes.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,7 +2234,6 @@ func (rh *RouteHandler) ChangePassword(resp http.ResponseWriter, req *http.Reque
22342234
if err != nil {
22352235
rh.c.Log.Error().Msg("failed to read req body")
22362236
resp.WriteHeader(http.StatusInternalServerError)
2237-
_, _ = resp.Write([]byte("internal server error"))
22382237

22392238
return
22402239
}
@@ -2243,41 +2242,41 @@ func (rh *RouteHandler) ChangePassword(resp http.ResponseWriter, req *http.Reque
22432242
if err := json.Unmarshal(body, &reqBody); err != nil {
22442243
rh.c.Log.Error().Msg("failed to unmarshal req body")
22452244
resp.WriteHeader(http.StatusBadRequest)
2246-
_, _ = resp.Write([]byte("bad req"))
22472245

22482246
return
22492247
}
22502248

22512249
userAc, err := reqCtx.UserAcFromContext(req.Context())
22522250
if err != nil {
2251+
resp.WriteHeader(http.StatusNotFound)
2252+
22532253
return
22542254
}
22552255

22562256
username := userAc.GetUsername()
2257-
if err := rh.c.htpasswdClient.ChangePassword(username, reqBody.OldPassword, reqBody.NewPassword); err != nil {
2257+
if username == "" {
2258+
resp.WriteHeader(http.StatusNotFound)
2259+
}
2260+
2261+
if err := rh.c.HtpasswdClient.ChangePassword(username, reqBody.OldPassword, reqBody.NewPassword); err != nil {
22582262
rh.c.Log.Error().Err(err).Str("identity", username).Msg("failed to change user password")
22592263
status := http.StatusInternalServerError
2260-
msg := err.Error()
22612264

22622265
switch {
2263-
case errors.Is(err, zerr.ErrUserIsNotFound):
2266+
case errors.Is(err, zerr.ErrBadUser):
22642267
status = http.StatusNotFound
22652268
case errors.Is(err, zerr.ErrOldPasswordIsWrong):
22662269
status = http.StatusUnauthorized
22672270
case errors.Is(err, zerr.ErrPasswordIsEmpty):
22682271
status = http.StatusBadRequest
2269-
default:
2270-
msg = "internal server error"
22712272
}
22722273

22732274
resp.WriteHeader(status)
2274-
_, _ = resp.Write([]byte(msg))
22752275

22762276
return
22772277
}
22782278

22792279
resp.WriteHeader(http.StatusOK)
2280-
_, _ = resp.Write([]byte("password changed"))
22812280
}
22822281

22832282
type ChangePasswordRequest struct {

pkg/api/routes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1562,7 +1562,7 @@ func TestRoutes(t *testing.T) {
15621562
NewPassword: "new_password",
15631563
},
15641564
wantCode: http.StatusNotFound,
1565-
wantBody: []byte(zerr.ErrUserIsNotFound.Error()),
1565+
wantBody: []byte(zerr.ErrBadUser.Error()),
15661566
}))
15671567

15681568
Convey("old password is wrong", testFn(testCase{

pkg/test/image-utils/upload_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,28 +524,28 @@ func TestInjectUploadImageWithBasicAuth(t *testing.T) {
524524
}
525525

526526
Convey("first marshal", func() {
527-
injected := inject.InjectFailure(0)
527+
injected := inject.InjectFailure(1)
528528
if injected {
529529
err := UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password")
530530
So(err, ShouldNotBeNil)
531531
}
532532
})
533533
Convey("CreateBlobUpload POST call", func() {
534-
injected := inject.InjectFailure(1)
534+
injected := inject.InjectFailure(2)
535535
if injected {
536536
err := UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password")
537537
So(err, ShouldNotBeNil)
538538
}
539539
})
540540
Convey("UpdateBlobUpload PUT call", func() {
541-
injected := inject.InjectFailure(3)
541+
injected := inject.InjectFailure(4)
542542
if injected {
543543
err := UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password")
544544
So(err, ShouldNotBeNil)
545545
}
546546
})
547547
Convey("second marshal", func() {
548-
injected := inject.InjectFailure(5)
548+
injected := inject.InjectFailure(6)
549549
if injected {
550550
err := UploadImageWithBasicAuth(img, baseURL, "test", img.DigestStr(), "user", "password")
551551
So(err, ShouldNotBeNil)

swagger/docs.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

swagger/swagger.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@
11791179
}
11801180
},
11811181
"403": {
1182-
"description": "old password is incorrect",
1182+
"description": "old password is incorrect\".",
11831183
"schema": {
11841184
"type": "string"
11851185
}

swagger/swagger.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ paths:
10271027
schema:
10281028
type: string
10291029
"403":
1030-
description: old password is incorrect
1030+
description: old password is incorrect".
10311031
schema:
10321032
type: string
10331033
"500":

0 commit comments

Comments
 (0)