Merge pull request #8976 from ninjadq/add_auth_for_project_head

Fix: Add authenticate to projects head
This commit is contained in:
Wang Yan 2019-09-17 14:02:45 +08:00 committed by GitHub
commit f77ce4aa3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 3 deletions

View File

@ -234,6 +234,12 @@ func (p *ProjectAPI) Post() {
// Head ... // Head ...
func (p *ProjectAPI) Head() { func (p *ProjectAPI) Head() {
if !p.SecurityCtx.IsAuthenticated() {
p.SendUnAuthorizedError(errors.New("Unauthorized"))
return
}
name := p.GetString("project_name") name := p.GetString("project_name")
if len(name) == 0 { if len(name) == 0 {
p.SendBadRequestError(errors.New("project_name is needed")) p.SendBadRequestError(errors.New("project_name is needed"))

View File

@ -329,13 +329,13 @@ func TestDeleteProject(t *testing.T) {
} }
func TestProHead(t *testing.T) { func TestProHead(t *testing.T) {
fmt.Println("\nTest for Project HEAD API") t.Log("\nTest for Project HEAD API")
assert := assert.New(t) assert := assert.New(t)
apiTest := newHarborAPI() apiTest := newHarborAPI()
// ----------------------------case 1 : Response Code=200----------------------------// // ----------------------------case 1 : Response Code=200----------------------------//
fmt.Println("case 1: response code:200") t.Log("case 1: response code:200")
httpStatusCode, err := apiTest.ProjectsHead(*admin, "library") httpStatusCode, err := apiTest.ProjectsHead(*admin, "library")
if err != nil { if err != nil {
t.Error("Error while search project by proName", err.Error()) t.Error("Error while search project by proName", err.Error())
@ -345,7 +345,7 @@ func TestProHead(t *testing.T) {
} }
// ----------------------------case 2 : Response Code=404:Project name does not exist.----------------------------// // ----------------------------case 2 : Response Code=404:Project name does not exist.----------------------------//
fmt.Println("case 2: response code:404,Project name does not exist.") t.Log("case 2: response code:404,Project name does not exist.")
httpStatusCode, err = apiTest.ProjectsHead(*admin, "libra") httpStatusCode, err = apiTest.ProjectsHead(*admin, "libra")
if err != nil { if err != nil {
t.Error("Error while search project by proName", err.Error()) t.Error("Error while search project by proName", err.Error())
@ -354,6 +354,24 @@ func TestProHead(t *testing.T) {
assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404") assert.Equal(int(404), httpStatusCode, "httpStatusCode should be 404")
} }
t.Log("case 3: response code:401. Project exist with unauthenticated user")
httpStatusCode, err = apiTest.ProjectsHead(*unknownUsr, "library")
if err != nil {
t.Error("Error while search project by proName", err.Error())
t.Log(err)
} else {
assert.Equal(int(401), httpStatusCode, "httpStatusCode should be 404")
}
t.Log("case 4: response code:401. Project name does not exist with unauthenticated user")
httpStatusCode, err = apiTest.ProjectsHead(*unknownUsr, "libra")
if err != nil {
t.Error("Error while search project by proName", err.Error())
t.Log(err)
} else {
assert.Equal(int(401), httpStatusCode, "httpStatusCode should be 404")
}
fmt.Printf("\n") fmt.Printf("\n")
} }

View File

@ -37,6 +37,7 @@ import (
"github.com/goharbor/harbor/src/common/utils/log" "github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/core/auth" "github.com/goharbor/harbor/src/core/auth"
"github.com/goharbor/harbor/src/core/config" "github.com/goharbor/harbor/src/core/config"
"github.com/goharbor/harbor/src/core/filter"
) )
const userKey = "user" const userKey = "user"