Skip to content

Commit f7ec657

Browse files
committed
registry: use the privileged client to get signatures
1 parent 2c37b7c commit f7ec657

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

pkg/dockerregistry/server/app.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ func NewApp(ctx context.Context, registryClient client.RegistryClient, dockerCon
115115

116116
// Registry extensions endpoint provides extra functionality to handle the image
117117
// signatures.
118-
RegisterSignatureHandler(dockerApp)
118+
isImageClient, err := registryClient.Client()
119+
if err != nil {
120+
context.GetLogger(dockerApp).Fatalf("unable to get client for signatures: %v", err)
121+
}
122+
RegisterSignatureHandler(dockerApp, isImageClient)
119123

120124
// Registry extensions endpoint provides prometheus metrics.
121125
if extraConfig.Metrics.Enabled {

pkg/dockerregistry/server/auth.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ func (ac *AccessController) Authorized(ctx context.Context, accessRecords ...reg
333333
if err := verifyImageSignatureAccess(ctx, namespace, name, osClient); err != nil {
334334
return nil, ac.wrapErr(ctx, err)
335335
}
336+
default:
337+
return nil, ac.wrapErr(ctx, ErrUnsupportedAction)
336338
}
337339

338340
case "metrics":

pkg/dockerregistry/server/signaturedispatcher.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/docker/distribution/registry/api/v2"
1818
"github.com/docker/distribution/registry/handlers"
1919

20+
"github.com/openshift/origin/pkg/dockerregistry/server/client"
2021
imageapi "github.com/openshift/origin/pkg/image/apis/image"
2122
imageapiv1 "github.com/openshift/origin/pkg/image/apis/image/v1"
2223

@@ -60,18 +61,27 @@ var (
6061
)
6162

6263
type signatureHandler struct {
63-
ctx *handlers.Context
64-
reference imageapi.DockerImageReference
64+
ctx *handlers.Context
65+
reference imageapi.DockerImageReference
66+
isImageClient client.ImageStreamImagesNamespacer
6567
}
6668

67-
// SignatureDispatcher handles the GET and PUT requests for signature endpoint.
68-
func SignatureDispatcher(ctx *handlers.Context, r *http.Request) http.Handler {
69-
signatureHandler := &signatureHandler{ctx: ctx}
70-
signatureHandler.reference, _ = imageapi.ParseDockerImageReference(ctxu.GetStringValue(ctx, "vars.name") + "@" + ctxu.GetStringValue(ctx, "vars.digest"))
71-
72-
return gorillahandlers.MethodHandler{
73-
"GET": http.HandlerFunc(signatureHandler.Get),
74-
"PUT": http.HandlerFunc(signatureHandler.Put),
69+
// NewSignatureDispatcher provides a function that handles the GET and PUT
70+
// requests for signature endpoint.
71+
func NewSignatureDispatcher(isImageClient client.ImageStreamImagesNamespacer) func(*handlers.Context, *http.Request) http.Handler {
72+
return func(ctx *handlers.Context, r *http.Request) http.Handler {
73+
reference, _ := imageapi.ParseDockerImageReference(
74+
ctxu.GetStringValue(ctx, "vars.name") + "@" + ctxu.GetStringValue(ctx, "vars.digest"),
75+
)
76+
signatureHandler := &signatureHandler{
77+
ctx: ctx,
78+
isImageClient: isImageClient,
79+
reference: reference,
80+
}
81+
return gorillahandlers.MethodHandler{
82+
"GET": http.HandlerFunc(signatureHandler.Get),
83+
"PUT": http.HandlerFunc(signatureHandler.Put),
84+
}
7585
}
7686
}
7787

@@ -142,18 +152,13 @@ func (s *signatureHandler) Get(w http.ResponseWriter, req *http.Request) {
142152
s.handleError(s.ctx, v2.ErrorCodeNameInvalid.WithDetail("missing image name or image ID"), w)
143153
return
144154
}
145-
client, ok := userClientFrom(s.ctx)
146-
if !ok {
147-
s.handleError(s.ctx, errcode.ErrorCodeUnknown.WithDetail("unable to get origin client"), w)
148-
return
149-
}
150155

151156
if len(s.reference.ID) == 0 {
152157
s.handleError(s.ctx, v2.ErrorCodeNameInvalid.WithDetail("the image ID must be specified (sha256:<digest>"), w)
153158
return
154159
}
155160

156-
image, err := client.ImageStreamImages(s.reference.Namespace).Get(imageapi.MakeImageStreamImageName(s.reference.Name, s.reference.ID), metav1.GetOptions{})
161+
image, err := s.isImageClient.ImageStreamImages(s.reference.Namespace).Get(imageapi.MakeImageStreamImageName(s.reference.Name, s.reference.ID), metav1.GetOptions{})
157162
switch {
158163
case err == nil:
159164
case kapierrors.IsUnauthorized(err):

pkg/dockerregistry/server/signaturedispatcher_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func TestSignatureGet(t *testing.T) {
5757
t.Fatal(err)
5858
}
5959

60+
os.Setenv("OPENSHIFT_DEFAULT_REGISTRY", "localhost:5000")
61+
6062
ctx := context.Background()
6163
ctx = withUserClient(ctx, osclient)
6264
registryApp := NewApp(ctx, registryclient.NewFakeRegistryClient(imageClient), &configuration.Configuration{
@@ -163,6 +165,8 @@ func TestSignaturePut(t *testing.T) {
163165
t.Fatal(err)
164166
}
165167

168+
os.Setenv("OPENSHIFT_DEFAULT_REGISTRY", "localhost:5000")
169+
166170
ctx := context.Background()
167171
ctx = withUserClient(ctx, osclient)
168172
registryApp := NewApp(ctx, registryclient.NewFakeRegistryClient(imageClient), &configuration.Configuration{

pkg/dockerregistry/server/signaturehandler.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import (
88
"github.com/docker/distribution/registry/handlers"
99

1010
"github.com/openshift/origin/pkg/dockerregistry/server/api"
11+
"github.com/openshift/origin/pkg/dockerregistry/server/client"
1112
)
1213

1314
// RegisterSignatureHandler registers the Docker image signature extension to Docker
1415
// registry.
15-
func RegisterSignatureHandler(app *handlers.App) {
16+
func RegisterSignatureHandler(app *handlers.App, isImageClient client.ImageStreamImagesNamespacer) {
1617
extensionsRouter := app.NewRoute().PathPrefix(api.ExtensionsPrefix).Subrouter()
1718
var (
1819
getSignatureAccess = func(r *http.Request) []auth.Access {
@@ -40,13 +41,13 @@ func RegisterSignatureHandler(app *handlers.App) {
4041
)
4142
app.RegisterRoute(
4243
extensionsRouter.Path(api.SignaturesPath).Methods("GET"),
43-
SignatureDispatcher,
44+
NewSignatureDispatcher(isImageClient),
4445
handlers.NameRequired,
4546
getSignatureAccess,
4647
)
4748
app.RegisterRoute(
4849
extensionsRouter.Path(api.SignaturesPath).Methods("PUT"),
49-
SignatureDispatcher,
50+
NewSignatureDispatcher(isImageClient),
5051
handlers.NameRequired,
5152
putSignatureAccess,
5253
)

0 commit comments

Comments
 (0)