Skip to content

Commit

Permalink
Merge pull request #221 from xmidt-org/denopink/feat/remove-arrange-a…
Browse files Browse the repository at this point in the history
…nd-addgoschtalt

feat: remove arrange
  • Loading branch information
denopink authored Nov 10, 2023
2 parents fb3ab08 + c7f9151 commit b18a925
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 280 deletions.
22 changes: 10 additions & 12 deletions attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

package bascule

import (
"github.com/xmidt-org/arrange"
)

type BasicAttributes map[string]interface{}

func (a BasicAttributes) Get(key string) (interface{}, bool) {
Expand Down Expand Up @@ -34,20 +30,22 @@ func GetNestedAttribute(attributes Attributes, keys ...string) (interface{}, boo
result = attributes
for _, k := range keys {
var a Attributes
if result == nil {
return nil, false
}
ok = arrange.TryConvert(result,
func(attr Attributes) { a = attr },
func(m map[string]interface{}) { a = BasicAttributes(m) },
)
if !ok {
switch r := result.(type) {
case BasicAttributes:
a = r
case Attributes:
a = r
case map[string]interface{}:
a = BasicAttributes(r)
default:
return nil, false
}

result, ok = a.Get(k)
if !ok {
return nil, false
}
}

return result, ok
}
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/xmidt-org/arrange"
"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(
arrange.UnmarshalKey(key, CapabilitiesMapConfig{}),
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(
arrange.UnmarshalKey(key, CapabilitiesValidatorConfig{}),
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/xmidt-org/arrange"
"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: arrange.UnmarshalKey(key, EncodedBasicKeys{}),
},
fx.Annotated{
Group: "bascule_constructor_options",
Target: func(in EncodedBasicKeysIn) (COption, error) {
Expand Down
35 changes: 19 additions & 16 deletions basculehttp/basicTokenFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
"strings"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xmidt-org/arrange"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/sallust"
"go.uber.org/fx"
)

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

const yaml = `
good:
basic: ["dXNlcjpwYXNz", "dXNlcjpwYXNz", "dXNlcjpwYXNz"]
bad:
basic: ["AAAAAAAA"]
`
v := viper.New()
v.SetConfigType("yaml")
require.NoError(t, v.ReadConfig(strings.NewReader(yaml)))

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 @@ -159,6 +150,7 @@ bad:
{
description: "Failure",
key: "bad",
keys: EncodedBasicKeys{Basic: []string{"AAAAAAAA"}},
expectedErr: errors.New("malformed"),
},
}
Expand All @@ -168,8 +160,19 @@ bad:
assert := assert.New(t)
require := require.New(t)
app := fx.New(
arrange.TestLogger(t),
arrange.ForViper(v),
fx.Provide(
func() (c sallust.Config) {
return sallust.Config{}
},

fx.Annotated{
Name: "encoded_basic_auths",
Target: func() EncodedBasicKeys {
return tc.keys
},
},
),
sallust.WithLogger(),
ProvideBasicTokenFactory(tc.key),
fx.Invoke(
func(in In) {
Expand All @@ -179,8 +182,8 @@ bad:
)
err := app.Err()
if tc.expectedErr == nil {
assert.NoError(err)
assert.True(len(result.Options) == 1)
require.NoError(err)
require.True(len(result.Options) == 1)
if tc.optionExpected {
require.NotNil(result.Options[0])
return
Expand Down
25 changes: 13 additions & 12 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/xmidt-org/arrange"
"github.com/xmidt-org/bascule"
"github.com/xmidt-org/clortho"
"github.com/xmidt-org/clortho/clorthofx"
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,21 +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: arrange.UnmarshalKey(fmt.Sprintf("%s.leeway", configKey),
bascule.Leeway{}),
},
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 b18a925

Please sign in to comment.