Skip to content

Commit ef76e39

Browse files
committed
Migrate python handler to OIDCRegistry
Replace manual OIDC credential map and mutex with the shared OIDCRegistry type. OIDC key changes from hostname-only to full URL (via index-url or url field), fixing credential collisions when multiple Python indexes share a host with different paths.
1 parent b63c337 commit ef76e39

2 files changed

Lines changed: 10 additions & 24 deletions

File tree

internal/handlers/oidc_handling_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ func TestOIDCURLsAreAuthenticated(t *testing.T) {
10351035
},
10361036
urlMocks: []mockHttpRequest{},
10371037
expectedLogLines: []string{
1038-
"registered aws OIDC credentials for python index: python.example.com",
1038+
"registered aws OIDC credentials for python index: https://python.example.com",
10391039
},
10401040
urlsToAuthenticate: []string{
10411041
"https://python.example.com/some-package",
@@ -1057,7 +1057,7 @@ func TestOIDCURLsAreAuthenticated(t *testing.T) {
10571057
},
10581058
urlMocks: []mockHttpRequest{},
10591059
expectedLogLines: []string{
1060-
"registered azure OIDC credentials for python index: python.example.com",
1060+
"registered azure OIDC credentials for python index: https://python.example.com",
10611061
},
10621062
urlsToAuthenticate: []string{
10631063
"https://python.example.com/some-package",
@@ -1078,7 +1078,7 @@ func TestOIDCURLsAreAuthenticated(t *testing.T) {
10781078
},
10791079
urlMocks: []mockHttpRequest{},
10801080
expectedLogLines: []string{
1081-
"registered jfrog OIDC credentials for python index: jfrog.example.com",
1081+
"registered jfrog OIDC credentials for python index: https://jfrog.example.com",
10821082
},
10831083
urlsToAuthenticate: []string{
10841084
"https://jfrog.example.com/some-package",
@@ -1101,7 +1101,7 @@ func TestOIDCURLsAreAuthenticated(t *testing.T) {
11011101
},
11021102
urlMocks: []mockHttpRequest{},
11031103
expectedLogLines: []string{
1104-
"registered cloudsmith OIDC credentials for python index: cloudsmith.example.com",
1104+
"registered cloudsmith OIDC credentials for python index: https://cloudsmith.example.com",
11051105
},
11061106
urlsToAuthenticate: []string{
11071107
"https://cloudsmith.example.com/some-package",

internal/handlers/python_index.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"net/http"
55
"regexp"
66
"strings"
7-
"sync"
87

98
"github.com/elazarl/goproxy"
109

@@ -18,9 +17,8 @@ var simpleSuffixRe = regexp.MustCompile(`/\+?simple/?\z`)
1817

1918
// PythonIndexHandler handles requests to Python indexes, adding auth.
2019
type PythonIndexHandler struct {
21-
credentials []pythonIndexCredentials
22-
oidcCredentials map[string]*oidc.OIDCCredential
23-
mutex sync.RWMutex
20+
credentials []pythonIndexCredentials
21+
oidcRegistry *oidc.OIDCRegistry
2422
}
2523

2624
type pythonIndexCredentials struct {
@@ -34,8 +32,8 @@ type pythonIndexCredentials struct {
3432
// NewPythonIndexHandler returns a new PythonIndexHandler.
3533
func NewPythonIndexHandler(creds config.Credentials) *PythonIndexHandler {
3634
handler := PythonIndexHandler{
37-
credentials: []pythonIndexCredentials{},
38-
oidcCredentials: make(map[string]*oidc.OIDCCredential),
35+
credentials: []pythonIndexCredentials{},
36+
oidcRegistry: oidc.NewOIDCRegistry(),
3937
}
4038

4139
for _, cred := range creds {
@@ -45,19 +43,7 @@ func NewPythonIndexHandler(creds config.Credentials) *PythonIndexHandler {
4543

4644
indexURL := cred.GetString("index-url")
4745

48-
oidcCredential, _ := oidc.CreateOIDCCredential(cred)
49-
if oidcCredential != nil {
50-
host := cred.Host()
51-
if host == "" && indexURL != "" {
52-
regURL, err := helpers.ParseURLLax(indexURL)
53-
if err == nil {
54-
host = regURL.Hostname()
55-
}
56-
}
57-
if host != "" {
58-
handler.oidcCredentials[host] = oidcCredential
59-
logging.RequestLogf(nil, "registered %s OIDC credentials for python index: %s", oidcCredential.Provider(), host)
60-
}
46+
if _, _, ok := handler.oidcRegistry.Register(cred, []string{"index-url", "url"}, "python index"); ok {
6147
continue
6248
}
6349

@@ -85,7 +71,7 @@ func (h *PythonIndexHandler) HandleRequest(req *http.Request, ctx *goproxy.Proxy
8571
}
8672

8773
// Try OIDC credentials first
88-
if oidc.TryAuthOIDCRequestWithPrefix(&h.mutex, h.oidcCredentials, req, ctx) {
74+
if h.oidcRegistry.TryAuth(req, ctx) {
8975
return req, nil
9076
}
9177

0 commit comments

Comments
 (0)