Skip to content
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
27 changes: 7 additions & 20 deletions internal/handlers/maven_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package handlers

import (
"net/http"
"sync"

"github.com/elazarl/goproxy"

Expand All @@ -14,9 +13,8 @@ import (

// MavenRepositoryHandler handles requests to maven repositories, adding auth.
type MavenRepositoryHandler struct {
credentials []mavenRepositoryCredentials
oidcCredentials map[string]*oidc.OIDCCredential
mutex sync.RWMutex
credentials []mavenRepositoryCredentials
oidcRegistry *oidc.OIDCRegistry
}

type mavenRepositoryCredentials struct {
Expand All @@ -29,8 +27,8 @@ type mavenRepositoryCredentials struct {
// NewMavenRepositoryHandler returns a new MavenRepositoryHandler.
func NewMavenRepositoryHandler(creds config.Credentials) *MavenRepositoryHandler {
handler := MavenRepositoryHandler{
credentials: []mavenRepositoryCredentials{},
oidcCredentials: make(map[string]*oidc.OIDCCredential),
credentials: []mavenRepositoryCredentials{},
oidcRegistry: oidc.NewOIDCRegistry(),
}

for _, cred := range creds {
Expand All @@ -40,19 +38,8 @@ func NewMavenRepositoryHandler(creds config.Credentials) *MavenRepositoryHandler

url := cred.GetString("url")

oidcCredential, _ := oidc.CreateOIDCCredential(cred)
if oidcCredential != nil {
host := cred.Host()
if host == "" && url != "" {
regURL, err := helpers.ParseURLLax(url)
if err == nil {
host = regURL.Hostname()
}
}
if host != "" {
handler.oidcCredentials[host] = oidcCredential
logging.RequestLogf(nil, "registered %s OIDC credentials for maven repository: %s", oidcCredential.Provider(), host)
}
// OIDC credentials are not used as static credentials.
if oidcCred, _, _ := handler.oidcRegistry.Register(cred, []string{"url"}, "maven repository"); oidcCred != nil {
continue
}

Expand Down Expand Up @@ -81,7 +68,7 @@ func (h *MavenRepositoryHandler) HandleRequest(req *http.Request, ctx *goproxy.P
}

// Try OIDC credentials first
if oidc.TryAuthOIDCRequestWithPrefix(&h.mutex, h.oidcCredentials, req, ctx) {
if h.oidcRegistry.TryAuth(req, ctx) {
return req, nil
}

Expand Down
8 changes: 4 additions & 4 deletions internal/handlers/oidc_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func TestOIDCURLsAreAuthenticated(t *testing.T) {
},
urlMocks: []mockHttpRequest{},
expectedLogLines: []string{
"registered aws OIDC credentials for maven repository: maven.example.com",
"registered aws OIDC credentials for maven repository: https://maven.example.com/packages",
},
urlsToAuthenticate: []string{
"https://maven.example.com/packages/some-package",
Expand All @@ -649,7 +649,7 @@ func TestOIDCURLsAreAuthenticated(t *testing.T) {
},
urlMocks: []mockHttpRequest{},
expectedLogLines: []string{
"registered azure OIDC credentials for maven repository: maven.example.com",
"registered azure OIDC credentials for maven repository: https://maven.example.com/packages",
},
urlsToAuthenticate: []string{
"https://maven.example.com/packages/some-package",
Expand All @@ -670,7 +670,7 @@ func TestOIDCURLsAreAuthenticated(t *testing.T) {
},
urlMocks: []mockHttpRequest{},
expectedLogLines: []string{
"registered jfrog OIDC credentials for maven repository: jfrog.example.com",
"registered jfrog OIDC credentials for maven repository: https://jfrog.example.com/packages",
},
urlsToAuthenticate: []string{
"https://jfrog.example.com/packages/some-package",
Expand All @@ -693,7 +693,7 @@ func TestOIDCURLsAreAuthenticated(t *testing.T) {
},
urlMocks: []mockHttpRequest{},
expectedLogLines: []string{
"registered cloudsmith OIDC credentials for maven repository: cloudsmith.example.com",
"registered cloudsmith OIDC credentials for maven repository: https://cloudsmith.example.com",
},
urlsToAuthenticate: []string{
"https://cloudsmith.example.com/some-package",
Expand Down
Loading