Skip to content
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

Extend oidc_cli security context generator to include calls to the v2… #20851

Open
wants to merge 4 commits into
base: main
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
69 changes: 0 additions & 69 deletions src/server/middleware/security/idtoken.go

This file was deleted.

50 changes: 0 additions & 50 deletions src/server/middleware/security/idtoken_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ var (
uctl = user.Ctl
)

type oidcCli struct{}
type oidcCliOrAPI struct{}

func (o *oidcCli) Generate(req *http.Request) security.Context {
func (o *oidcCliOrAPI) Generate(req *http.Request) security.Context {
ctx := req.Context()
if lib.GetAuthMode(ctx) != common.OIDCAuth {
return nil
Expand Down Expand Up @@ -78,7 +78,7 @@ func (o *oidcCli) Generate(req *http.Request) security.Context {
return local.NewSecurityContext(u)
}

func (o *oidcCli) valid(req *http.Request) bool {
func (o *oidcCliOrAPI) valid(req *http.Request) bool {
path := strings.TrimSuffix(req.URL.Path, "/")

if path == "/service/token" ||
Expand All @@ -104,5 +104,10 @@ func (o *oidcCli) valid(req *http.Request) bool {
if req.Method == http.MethodDelete && tagsAPIRe.MatchString(path) { // deleting tags
return true
}

// The request was sent to the v2 API directly
if strings.HasPrefix(req.URL.Path, "/api") || req.URL.Path == "/service/token" {
return true
}
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,12 @@ import (
)

func TestOIDCCli(t *testing.T) {
oidcCli := &oidcCli{}
// not the candidate request
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1/api/v2.0/users/", nil)
require.Nil(t, err)
ctx := oidcCli.Generate(req)
assert.Nil(t, ctx)
oc := &oidcCliOrAPI{}

// the auth mode isn't OIDC
req, err = http.NewRequest(http.MethodGet, "http://127.0.0.1/service/token", nil)
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1/service/token", nil)
require.Nil(t, err)
ctx = oidcCli.Generate(req)
ctx := oc.Generate(req)
assert.Nil(t, ctx)

// pass
Expand All @@ -57,12 +52,12 @@ func TestOIDCCli(t *testing.T) {
oidc.SetHardcodeVerifierForTest(password)
req = req.WithContext(lib.WithAuthMode(req.Context(), common.OIDCAuth))
req.SetBasicAuth(username, password)
ctx = oidcCli.Generate(req)
ctx = oc.Generate(req)
assert.NotNil(t, ctx)
}

func TestOIDCCliValid(t *testing.T) {
oc := &oidcCli{}
oc := &oidcCliOrAPI{}
req1, _ := http.NewRequest(http.MethodPost, "https://test.goharbor.io/api/v2.0/projects", nil)
req2, _ := http.NewRequest(http.MethodGet, "https://test.goharbor.io/api/v2.0/projects?name=test", nil)
req3, _ := http.NewRequest(http.MethodGet, "https://test.goharbor.io/api/v2.0/projects/library/repositories/", nil)
Expand Down Expand Up @@ -97,3 +92,27 @@ func TestOIDCCliValid(t *testing.T) {
}

}

func TestOIDCAPI(t *testing.T) {
oc := &oidcCliOrAPI{}

// not the OIDC mode
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1/api/projects/", nil)
require.Nil(t, err)
ctx := oc.Generate(req)
assert.Nil(t, ctx)

// contains no authorization header
req, err = http.NewRequest(http.MethodGet, "http://127.0.0.1/api/projects/", nil)
require.Nil(t, err)
req = req.WithContext(lib.WithAuthMode(req.Context(), common.OIDCAuth))
ctx = oc.Generate(req)
assert.Nil(t, ctx)

// contains no authorization header
req, err = http.NewRequest(http.MethodGet, "http://127.0.0.1/service/token?service=harbor-registry&scope=repository:foo/bar:pull", nil)
require.Nil(t, err)
req = req.WithContext(lib.WithAuthMode(req.Context(), common.OIDCAuth))
ctx = oc.Generate(req)
assert.Nil(t, ctx)
}
3 changes: 1 addition & 2 deletions src/server/middleware/security/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ import (
var (
generators = []generator{
&secret{},
&oidcCli{},
&oidcCliOrAPI{},
&v2Token{},
&idToken{},
&authProxy{},
&robot{},
&basicAuth{},
Expand Down
Loading