Skip to content

Commit

Permalink
chore: updates based on pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
denopink committed Nov 2, 2023
1 parent 7ef3c1b commit e97d573
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 289 deletions.
4 changes: 2 additions & 2 deletions basculechecks/capabilitiesmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var (
// JWT match the string meant for that endpoint exactly. A CapabilitiesMap set
// up with this will use the default KeyPath.
type CapabilitiesMapConfig struct {
Endpoints map[string]string
Default string
Endpoints map[string]string `json:"endpoints" yaml:"endpoints"`
Default string `json:"default" yaml:"default"`
}

// CapabilitiesMap runs a capability check based on the value of the parsedURL,
Expand Down
8 changes: 4 additions & 4 deletions basculechecks/capabilitiesvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ type EndpointChecker interface {
// CapabilitiesValidator set up with this will use the default KeyPath and an
// EndpointRegexCheck.
type CapabilitiesValidatorConfig struct {
Type string
Prefix string
AcceptAllMethod string
EndpointBuckets []string
Type string `json:"type" yaml:"type"`
Prefix string `json:"prefix" yaml:"prefix"`
AcceptAllMethod string `json:"acceptAllMethod" yaml:"acceptAllMethod"`
EndpointBuckets []string `json:"endpointBuckets" yaml:"endpointBuckets"`
}

// CapabilitiesValidator checks the capabilities provided in a
Expand Down
7 changes: 2 additions & 5 deletions basculechecks/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package basculechecks

import (
"github.com/goschtalt/goschtalt"
"github.com/xmidt-org/bascule"
"go.uber.org/fx"
)
Expand All @@ -28,10 +27,9 @@ func ProvideMetricValidator(optional bool) fx.Option {
// ProvideCapabilitiesMapValidator is an uber fx Provide() function that builds
// a MetricValidator that uses a CapabilitiesMap and ConstChecks, using the
// configuration found at the key provided.
func ProvideCapabilitiesMapValidator(key string) fx.Option {
func ProvideCapabilitiesMapValidator() fx.Option {
return fx.Options(
fx.Provide(
goschtalt.UnmarshalFunc[CapabilitiesMapConfig](key),
NewCapabilitiesMap,
),
ProvideMetricValidator(false),
Expand All @@ -41,10 +39,9 @@ func ProvideCapabilitiesMapValidator(key string) fx.Option {
// ProvideRegexCapabilitiesValidator is an uber fx Provide() function that
// builds a MetricValidator that uses a CapabilitiesValidator and
// RegexEndpointCheck, using the configuration found at the key provided.
func ProvideRegexCapabilitiesValidator(key string) fx.Option {
func ProvideRegexCapabilitiesValidator() fx.Option {
return fx.Options(
fx.Provide(
goschtalt.UnmarshalFunc[CapabilitiesValidatorConfig](key),
NewCapabilitiesValidator,
),
ProvideMetricValidator(true),
Expand Down
7 changes: 1 addition & 6 deletions basculehttp/basicTokenFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"net/http"

"github.com/goschtalt/goschtalt"
"github.com/xmidt-org/bascule"
"go.uber.org/fx"
)
Expand All @@ -23,7 +22,7 @@ var (
)

type EncodedBasicKeys struct {
Basic []string
Basic []string `json:"basic" yaml:"basic"`
}

// EncodedBasicKeysIn contains string representations of the basic auth allowed.
Expand Down Expand Up @@ -108,10 +107,6 @@ func NewBasicTokenFactoryFromList(encodedBasicAuthKeys []string) (BasicTokenFact
// factory.
func ProvideBasicTokenFactory(key string) fx.Option {
return fx.Provide(
fx.Annotated{
Name: "encoded_basic_auths",
Target: goschtalt.UnmarshalFunc[EncodedBasicKeys](key),
},
fx.Annotated{
Group: "bascule_constructor_options",
Target: func(in EncodedBasicKeysIn) (COption, error) {
Expand Down
30 changes: 8 additions & 22 deletions basculehttp/basicTokenFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,12 @@ import (
"net/http/httptest"
"strings"
"testing"
"testing/fstest"

"github.com/goschtalt/goschtalt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/sallust"
"go.uber.org/fx"

_ "github.com/goschtalt/yaml-decoder"
)

func TestBasicTokenFactory(t *testing.T) {
Expand Down Expand Up @@ -134,22 +130,18 @@ func TestProvideBasicTokenFactory(t *testing.T) {
Options []COption `group:"bascule_constructor_options"`
}

const yaml = `
good:
basic: ["dXNlcjpwYXNz", "dXNlcjpwYXNz", "dXNlcjpwYXNz"]
bad:
basic: ["AAAAAAAA"]
`
tests := []struct {
description string
key string
optionExpected bool
keys EncodedBasicKeys
expectedErr error
}{
{
description: "Success",
key: "good",
optionExpected: true,
keys: EncodedBasicKeys{Basic: []string{"dXNlcjpwYXNz", "dXNlcjpwYXNz", "dXNlcjpwYXNz"}},
},
{
description: "Disabled success",
Expand All @@ -158,6 +150,7 @@ bad:
{
description: "Failure",
key: "bad",
keys: EncodedBasicKeys{Basic: []string{"AAAAAAAA"}},
expectedErr: errors.New("malformed"),
},
}
Expand All @@ -171,19 +164,12 @@ bad:
func() (c sallust.Config) {
return sallust.Config{}
},
func() (*goschtalt.Config, error) {
fs := fstest.MapFS{
"test.yml": &fstest.MapFile{
Data: []byte(yaml),
Mode: 0644,
},
}
gc, err := goschtalt.New(goschtalt.AddDir(fs, "."))
if err != nil {
return nil, err
}

return gc, gc.Compile()
fx.Annotated{
Name: "encoded_basic_auths",
Target: func() EncodedBasicKeys {
return tc.keys
},
},
),
sallust.WithLogger(),
Expand Down
24 changes: 13 additions & 11 deletions basculehttp/bearerTokenFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net/http"

"github.com/golang-jwt/jwt"
"github.com/goschtalt/goschtalt"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/clortho"

Check failure on line 14 in basculehttp/bearerTokenFactory.go

View workflow job for this annotation

GitHub Actions / ci / Build Go Program

github.com/xmidt-org/[email protected]: replacement directory /Users/odc/Documents/GitHub/xmidt-org/clortho does not exist

Check failure on line 14 in basculehttp/bearerTokenFactory.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

github.com/xmidt-org/[email protected]: replacement directory /Users/odc/Documents/GitHub/xmidt-org/clortho does not exist
"github.com/xmidt-org/clortho/clorthofx"

Check failure on line 15 in basculehttp/bearerTokenFactory.go

View workflow job for this annotation

GitHub Actions / ci / Build Go Program

github.com/xmidt-org/[email protected]: replacement directory /Users/odc/Documents/GitHub/xmidt-org/clortho does not exist

Check failure on line 15 in basculehttp/bearerTokenFactory.go

View workflow job for this annotation

GitHub Actions / ci / Go Unit Tests

github.com/xmidt-org/[email protected]: replacement directory /Users/odc/Documents/GitHub/xmidt-org/clortho does not exist
Expand All @@ -26,17 +25,16 @@ var (
ErrInvalidPrincipal = errors.New("invalid principal")
ErrInvalidToken = errors.New("token isn't valid")
ErrUnexpectedClaims = errors.New("claims wasn't MapClaims as expected")

ErrNilResolver = errors.New("resolver cannot be nil")
ErrNilResolver = errors.New("resolver cannot be nil")
)

// BearerTokenFactory parses and does basic validation for a JWT token,
// converting it into a bascule Token.
type BearerTokenFactory struct {
fx.In
DefaultKeyID string `name:"default_key_id"`
Resolver clortho.Resolver
Parser bascule.JWTParser `optional:"true"`
DefaultKeyID string `name:"default_key_id" optional:"true"`
Resolver clortho.Resolver `name:"key_resolver" optional:"true"`
Parser bascule.JWTParser `name:"parser" optional:"true"`
Leeway bascule.Leeway `name:"jwt_leeway" optional:"true"`
}

Expand Down Expand Up @@ -99,20 +97,24 @@ func (btf BearerTokenFactory) ParseAndValidate(ctx context.Context, _ *http.Requ
// ProvideBearerTokenFactory uses the key given to unmarshal configuration
// needed to build a bearer token factory. It provides a constructor option
// with the bearer token factory.
func ProvideBearerTokenFactory(configKey string, optional bool) fx.Option {
func ProvideBearerTokenFactory(optional bool) fx.Option {
return fx.Options(
clorthofx.Provide(),
fx.Provide(
fx.Annotated{
Name: "jwt_leeway",
Target: goschtalt.UnmarshalFunc[bascule.Leeway](fmt.Sprintf("%s.leeway", configKey)),
},
fx.Annotated{
Group: "bascule_constructor_options",
Target: func(f BearerTokenFactory) (COption, error) {
if f.Parser == nil {
f.Parser = bascule.DefaultJWTParser
}

if f.Resolver == nil {
if optional {
return nil, nil
}
return nil, ErrNilResolver
}

return WithTokenFactory(BearerAuthorization, f), nil
},
},
Expand Down
Loading

0 comments on commit e97d573

Please sign in to comment.