Skip to content
Open
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
24 changes: 12 additions & 12 deletions internal/handlers/cargo_registry.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"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -35,9 +34,8 @@ import (
// credentials locally in this example:
// https://jfrog.com/help/r/artifactory-how-to-integrate-artifactory-with-cargo-using-sparse-indexing/client-configuration
type CargoRegistryHandler struct {
credentials []cargoRepositoryCredentials
oidcCredentials map[string]*oidc.OIDCCredential
mutex sync.RWMutex
credentials []cargoRepositoryCredentials
oidcRegistry *oidc.OIDCRegistry
}

type cargoRepositoryCredentials struct {
Expand All @@ -47,8 +45,8 @@ type cargoRepositoryCredentials struct {

func NewCargoRegistryHandler(credentials config.Credentials) *CargoRegistryHandler {
handler := CargoRegistryHandler{
credentials: []cargoRepositoryCredentials{},
oidcCredentials: make(map[string]*oidc.OIDCCredential),
credentials: []cargoRepositoryCredentials{},
oidcRegistry: oidc.NewOIDCRegistry(),
}

for _, credential := range credentials {
Expand All @@ -58,12 +56,14 @@ func NewCargoRegistryHandler(credentials config.Credentials) *CargoRegistryHandl

url := credential.GetString("url")

oidcCredential, _ := oidc.CreateOIDCCredential(credential)
if oidcCredential != nil {
if url != "" {
handler.oidcCredentials[url] = oidcCredential
logging.RequestLogf(nil, "registered %s OIDC credentials for cargo registry: %s", oidcCredential.Provider(), url)
// Cargo credentials must remain URL-scoped; do not allow OIDC
// registration to fall back to host-only matching when url is empty.
// OIDC credentials are not used as static credentials.
if url != "" {
if oidcCred, _, _ := handler.oidcRegistry.Register(credential, []string{"url"}, "cargo registry"); oidcCred != nil {
continue
}
} else if oidcCred, _ := oidc.CreateOIDCCredential(credential); oidcCred != nil {
continue
}

Expand All @@ -90,7 +90,7 @@ func (h *CargoRegistryHandler) HandleRequest(req *http.Request, ctx *goproxy.Pro
}

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

Expand Down
Loading