Skip to content

Commit

Permalink
Merge pull request #930 from ldelossa/louis/middleware-packaging
Browse files Browse the repository at this point in the history
move middleware to package
  • Loading branch information
Louis DeLosSantos committed Mar 6, 2020
2 parents 49a408a + 9b070c6 commit 9828ed3
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 58 deletions.
16 changes: 9 additions & 7 deletions cmd/clair/httptransport.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"time"

"github.com/quay/clair/v4/middleware/auth"
"github.com/quay/clair/v4/middleware/compress"
"github.com/quay/claircore/libindex"
"github.com/quay/claircore/libvuln"
"go.opentelemetry.io/otel/plugin/othttp"
Expand Down Expand Up @@ -75,7 +77,7 @@ func devMode(ctx context.Context, conf config.Config) (*http.Server, error) {
matcher.Register(mux)
return &http.Server{
Addr: conf.HTTPListenAddr,
Handler: othttp.NewHandler(Compress(mux), "server"),
Handler: othttp.NewHandler(compress.Handler(mux), "server"),
}, nil
}

Expand All @@ -96,7 +98,7 @@ func indexerMode(ctx context.Context, conf config.Config) (*http.Server, error)
}
return &http.Server{
Addr: conf.Indexer.HTTPListenAddr,
Handler: othttp.NewHandler(Compress(indexer), "server"),
Handler: othttp.NewHandler(compress.Handler(indexer), "server"),
}, nil
}

Expand All @@ -120,7 +122,7 @@ func matcherMode(ctx context.Context, conf config.Config) (*http.Server, error)
}
return &http.Server{
Addr: conf.Matcher.HTTPListenAddr,
Handler: othttp.NewHandler(Compress(matcher), "server"),
Handler: othttp.NewHandler(compress.Handler(matcher), "server"),
}, nil
}

Expand All @@ -132,11 +134,11 @@ func setAuth(srv *http.Server, conf config.Config) error {
if !ok {
return fmt.Errorf("missing needed config key: %q", param)
}
ks, err := QuayKeyserver(api)
ks, err := auth.NewQuayKeyserver(api)
if err != nil {
return err
}
srv.Handler = AuthHandler(srv.Handler, ks)
srv.Handler = auth.Handler(srv.Handler, ks)
case "psk":
const (
iss = "issuer"
Expand All @@ -154,11 +156,11 @@ func setAuth(srv *http.Server, conf config.Config) error {
if !ok {
return fmt.Errorf("missing needed config key: %q", iss)
}
psk, err := PSKAuth(k, i)
psk, err := auth.NewPSK(k, i)
if err != nil {
return err
}
srv.Handler = AuthHandler(srv.Handler, psk)
srv.Handler = auth.Handler(srv.Handler, psk)
case "":
default:
return fmt.Errorf("unknown auth kind %q", conf.Auth.Name)
Expand Down
12 changes: 6 additions & 6 deletions cmd/clair/httpauth.go → middleware/auth/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package auth

import (
"context"
Expand All @@ -12,23 +12,23 @@ type AuthCheck interface {
Check(context.Context, *http.Request) bool
}

type authHandler struct {
type handler struct {
auth AuthCheck
next http.Handler
}

func (h *authHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if !h.auth.Check(r.Context(), r) {
w.WriteHeader(http.StatusUnauthorized)
return
}
h.next.ServeHTTP(w, r)
}

// AuthHandler returns a Handler that gates access to the passed Handler behind
// Handler returns a http.Handler that gates access to the passed Handler behind
// the passed AuthCheck.
func AuthHandler(h http.Handler, f AuthCheck) http.Handler {
return &authHandler{
func Handler(h http.Handler, f AuthCheck) http.Handler {
return &handler{
auth: f,
next: h,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package auth

import (
"context"
Expand All @@ -15,15 +15,38 @@ import (
"gopkg.in/square/go-jose.v2/jwt"
)

type ks struct {
// QuayKS implements the AuthCheck interface.
//
// When Check is called the JWT on the incoming http request
// will be validated against the Quay Keyserver
//
// It follows the algorithm outlined here:
// https://github.com/quay/jwtproxy/tree/master/jwt/keyserver/keyregistry#verifier
type QuayKeyserver struct {
root *url.URL
client *http.Client
mu sync.RWMutex
cache map[string]*jose.JSONWebKey
}

// NewQuayKeyserver returns an instance of a QuayKeyserver
func NewQuayKeyserver(api string) (*QuayKeyserver, error) {
root, err := url.Parse(api)
if err != nil {
return nil, err
}

t := httpcache.NewMemoryCacheTransport()
t.MarkCachedResponses = true
return &QuayKeyserver{
client: t.Client(),
root: root,
cache: make(map[string]*jose.JSONWebKey),
}, nil
}

// Check implements AuthCheck.
func (s *ks) Check(ctx context.Context, r *http.Request) bool {
func (s *QuayKeyserver) Check(ctx context.Context, r *http.Request) bool {
wt, ok := fromHeader(r)
if !ok {
return false
Expand Down Expand Up @@ -113,23 +136,3 @@ func (s *ks) Check(ctx context.Context, r *http.Request) bool {
}
return true
}

// QuayKeyserver returns an AuthCheck that validates JWTs by fetching keys from the
// Quay at "api".
//
// It follows the algorithm outlined here:
// https://github.com/quay/jwtproxy/tree/master/jwt/keyserver/keyregistry#verifier
func QuayKeyserver(api string) (AuthCheck, error) {
root, err := url.Parse(api)
if err != nil {
return nil, err
}

t := httpcache.NewMemoryCacheTransport()
t.MarkCachedResponses = true
return &ks{
client: t.Client(),
root: root,
cache: make(map[string]*jose.JSONWebKey),
}, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package auth

import (
"bytes"
Expand Down
28 changes: 16 additions & 12 deletions cmd/clair/httpauth_psk.go → middleware/auth/httpauth_psk.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package auth

import (
"context"
Expand All @@ -8,12 +8,25 @@ import (
"gopkg.in/square/go-jose.v2/jwt"
)

type psk struct {
// PSK implements the AuthCheck interface.
//
// When Check is called the JWT on the incoming http request
// will be validated against a pre-shared-key.
type PSK struct {
key []byte
iss string
}

func (p *psk) Check(_ context.Context, r *http.Request) bool {
// NewPSK returns an instance of a PSK
func NewPSK(key []byte, issuer string) (*PSK, error) {
return &PSK{
key: key,
iss: issuer,
}, nil
}

// Check implements AuthCheck
func (p *PSK) Check(_ context.Context, r *http.Request) bool {
wt, ok := fromHeader(r)
if !ok {
return false
Expand All @@ -34,12 +47,3 @@ func (p *psk) Check(_ context.Context, r *http.Request) bool {
}
return true
}

// PSKAuth returns an AuthCheck that validates a JWT with the supplied key and
// ensures the issuer claim matches.
func PSKAuth(key []byte, issuer string) (AuthCheck, error) {
return &psk{
key: key,
iss: issuer,
}, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package auth

import (
"bytes"
Expand Down
16 changes: 8 additions & 8 deletions cmd/clair/httpcompress.go → middleware/compress/handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package compress

import (
"fmt"
Expand All @@ -15,10 +15,10 @@ import (
"github.com/klauspost/compress/snappy"
)

// Compress wraps the provided http.Handler and provides transparent body
// Handler wraps the provided http.Handler and provides transparent body
// compression based on a Request's "Accept-Encoding" header.
func Compress(next http.Handler) http.Handler {
h := compressHandler{
func Handler(next http.Handler) http.Handler {
h := handler{
next: next,
}
h.snappy.New = func() interface{} {
Expand All @@ -36,10 +36,10 @@ func Compress(next http.Handler) http.Handler {
return &h
}

var _ http.Handler = (*compressHandler)(nil)
var _ http.Handler = (*handler)(nil)

// CompressHandler performs transparent HTTP body compression.
type compressHandler struct {
// handler performs transparent HTTP body compression.
type handler struct {
snappy, gzip, flate sync.Pool
next http.Handler
}
Expand Down Expand Up @@ -97,7 +97,7 @@ type accept struct {
}

// ServeHTTP implements http.Handler.
func (c *compressHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
func (c *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ae, nok := parseAccept(r.Header.Get("accept-encoding"))
if ae == nil {
// If there was no header, play it cool.
Expand Down

0 comments on commit 9828ed3

Please sign in to comment.