Skip to content
Merged
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
4 changes: 4 additions & 0 deletions authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ var _ AuthConfig = (*FlySrcAuthConfig)(nil)

func (c *FlySrcAuthConfig) AuthRequest(authctx AuthContext, req *http.Request) error {
flysrcParser := authctx.GetFlysrcParser()
if flysrcParser == nil {
return fmt.Errorf("%w: no flysrc parser", ErrNotAuthorized)
}

fs, err := flysrcParser.FromRequest(req)
if err != nil {
return fmt.Errorf("%w: %w", ErrNotAuthorized, err)
Expand Down
13 changes: 13 additions & 0 deletions cmd/tokenizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/sirupsen/logrus"
"github.com/superfly/tokenizer"
"golang.org/x/exp/slices"

"github.com/superfly/flysrc-go"
)

// Package variables can be overridden at build time:
Expand Down Expand Up @@ -96,6 +98,17 @@ func runServe() {
opts = append(opts, tokenizer.RequireFlySrc())
}

if slices.Contains([]string{"1", "true"}, os.Getenv("NO_FLY_SRC")) {
// nothing
} else {
parser, err := flysrc.New()
if err != nil {
logrus.WithError(err).Panic("Error making flysrc parser")
}

opts = append(opts, tokenizer.WithFlysrcParser(parser))
}

tkz := tokenizer.NewTokenizer(key, opts...)

if len(os.Getenv("DEBUG")) != 0 {
Expand Down
34 changes: 16 additions & 18 deletions tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,8 @@ func NewTokenizer(openKey string, opts ...Option) *tokenizer {
opt(tkz)
}

if tkz.flysrcParser == nil {
parser, err := flysrc.New()
if err != nil {
logrus.WithError(err).Panic("Error making flysrc parser")
}
tkz.flysrcParser = parser
if tkz.RequireFlySrc && tkz.flysrcParser == nil {
logrus.Panic("FlySrc is required but no flysrc Parser is specified")
}

hostnameMap := map[string]bool{}
Expand Down Expand Up @@ -282,19 +278,21 @@ func (t *tokenizer) HandleRequest(req *http.Request, ctx *goproxy.ProxyCtx) (*ht
pud.reqLog = logrus.WithFields(reqLogFields(ctx.Req))
}

src, err := t.flysrcParser.FromRequest(req)
if err != nil {
if t.RequireFlySrc {
pud.reqLog.Warn(err.Error())
return nil, errorResponse(ErrBadRequest)
if t.flysrcParser != nil {
src, err := t.flysrcParser.FromRequest(req)
if err != nil {
if t.RequireFlySrc {
pud.reqLog.Warn(err.Error())
return nil, errorResponse(ErrBadRequest)
}
} else {
pud.reqLog = pud.reqLog.WithFields(logrus.Fields{
"flysrc-org": src.Org,
"flysrc-app": src.App,
"flysrc-instance": src.Instance,
"flysrc-timestamp": src.Timestamp,
})
}
} else {
pud.reqLog = pud.reqLog.WithFields(logrus.Fields{
"flysrc-org": src.Org,
"flysrc-app": src.App,
"flysrc-instance": src.Instance,
"flysrc-timestamp": src.Timestamp,
})
}

processors := append([]RequestProcessor(nil), pud.connectProcessors...)
Expand Down
14 changes: 13 additions & 1 deletion tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ func TestTokenizer(t *testing.T) {

assert.NoError(t, err)

tkz := NewTokenizer(openKey, WithFlysrcParser(flysrcParser))
// we can build a server without a fly src parser
tkz := NewTokenizer(openKey)
assert.True(t, tkz != nil)

tkz = NewTokenizer(openKey, WithFlysrcParser(flysrcParser))
tkz.ProxyHttpServer.Verbose = true

tkzServer := httptest.NewServer(tkz)
Expand Down Expand Up @@ -417,6 +421,14 @@ func TestTokenizer(t *testing.T) {
Body: "",
}, doEcho(t, client, req))

// Bad, the same request fails, without panic, if flysrc parser is nil
parser := tkz.flysrcParser
tkz.flysrcParser = nil
resp, err = client.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusProxyAuthRequired, resp.StatusCode)
tkz.flysrcParser = parser

// Bad org
fs = &flysrc.FlySrc{Org: "WRONG!", App: "bar", Instance: "baz", Timestamp: time.Now().Truncate(time.Second)}
hdrSrc = fs.String()
Expand Down