Skip to content

Commit

Permalink
fix: bump ory/x and update config usage
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Dec 15, 2020
1 parent 1a20551 commit 0b48253
Show file tree
Hide file tree
Showing 26 changed files with 183 additions and 173 deletions.
4 changes: 2 additions & 2 deletions client/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func createTestClient(prefix string) *models.OAuth2Client {

func TestClientSDK(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
conf.Set(config.KeySubjectTypesSupported, []string{"public"})
conf.Set(config.KeyDefaultClientScope, []string{"foo", "bar"})
conf.MustSet(config.KeySubjectTypesSupported, []string{"public"})
conf.MustSet(config.KeyDefaultClientScope, []string{"foo", "bar"})
r := internal.NewRegistryMemory(t, conf)

router := x.NewRouterAdmin()
Expand Down
6 changes: 3 additions & 3 deletions client/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import (

func TestValidate(t *testing.T) {
c := internal.NewConfigurationWithDefaults()
c.Set(config.KeySubjectTypesSupported, []string{"pairwise", "public"})
c.Set(config.KeyDefaultClientScope, []string{"openid"})
c.MustSet(config.KeySubjectTypesSupported, []string{"pairwise", "public"})
c.MustSet(config.KeyDefaultClientScope, []string{"openid"})

v := NewValidator(c)
for k, tc := range []struct {
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestValidate(t *testing.T) {
},
{
v: func(t *testing.T) *Validator {
c.Set(config.KeySubjectTypesSupported, []string{"pairwise"})
c.MustSet(config.KeySubjectTypesSupported, []string{"pairwise"})
return NewValidator(c)
},
in: &Client{OutfacingID: "foo"},
Expand Down
16 changes: 14 additions & 2 deletions cmd/cli/handler_migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"

"github.com/ory/x/configx"

"github.com/ory/x/errorsx"

"github.com/ory/x/cmdx"
Expand All @@ -26,7 +28,11 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) {
var d driver.Registry

if flagx.MustGetBool(cmd, "read-from-env") {
d = driver.New(cmd.Flags(), driver.DisableValidation(),
d = driver.New(
driver.WithOptions(
configx.SkipValidation(),
configx.WithFlags(cmd.Flags())),
driver.DisableValidation(),
driver.DisablePreloading())
if len(d.Config().DSN()) == 0 {
fmt.Println(cmd.UsageString())
Expand All @@ -41,7 +47,13 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) {
os.Exit(1)
return
}
d = driver.New(cmd.Flags(), driver.ForceConfigValue(config.KeyDSN, args[0]), driver.DisableValidation(),
d = driver.New(
driver.WithOptions(
configx.WithFlags(cmd.Flags()),
configx.SkipValidation(),
configx.WithValue(config.KeyDSN, args[0]),
),
driver.DisableValidation(),
driver.DisablePreloading())
}

Expand Down
8 changes: 5 additions & 3 deletions cmd/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"sync"
"time"

"github.com/ory/x/configx"

analytics "github.com/ory/analytics-go/v4"

"github.com/ory/x/reqlog"
Expand Down Expand Up @@ -86,7 +88,7 @@ func isDSNAllowed(r driver.Registry) {
}

func RunServeAdmin(cmd *cobra.Command, args []string) {
d := driver.New(cmd.Flags())
d := driver.New(driver.WithOptions(configx.WithFlags(cmd.Flags())))
isDSNAllowed(d)

admin, _, adminmw, _ := setup(d, cmd)
Expand All @@ -104,7 +106,7 @@ func RunServeAdmin(cmd *cobra.Command, args []string) {
}

func RunServePublic(cmd *cobra.Command, args []string) {
d := driver.New(cmd.Flags())
d := driver.New(driver.WithOptions(configx.WithFlags(cmd.Flags())))
isDSNAllowed(d)

_, public, _, publicmw := setup(d, cmd)
Expand All @@ -122,7 +124,7 @@ func RunServePublic(cmd *cobra.Command, args []string) {
}

func RunServeAll(cmd *cobra.Command, args []string) {
d := driver.New(cmd.Flags())
d := driver.New(driver.WithOptions(configx.WithFlags(cmd.Flags())))

admin, public, adminmw, publicmw := setup(d, cmd)
cert := GetOrCreateTLSCertificate(cmd, d) // we do not want to run this concurrently.
Expand Down
4 changes: 2 additions & 2 deletions consent/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ import (

func TestSDK(t *testing.T) {
conf := internal.NewConfigurationWithDefaults()
conf.Set(config.KeyIssuerURL, "https://www.ory.sh")
conf.Set(config.KeyAccessTokenLifespan, time.Minute)
conf.MustSet(config.KeyIssuerURL, "https://www.ory.sh")
conf.MustSet(config.KeyAccessTokenLifespan, time.Minute)
reg := internal.NewRegistryMemory(t, conf)

router := x.NewRouterPublic()
Expand Down
8 changes: 4 additions & 4 deletions consent/strategy_logout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (

func TestLogoutFlows(t *testing.T) {
reg := internal.NewMockedRegistry(t)
reg.Config().Set(config.KeyAccessTokenStrategy, "opaque")
reg.Config().Set(config.KeyConsentRequestMaxAge, time.Hour)
reg.Config().MustSet(config.KeyAccessTokenStrategy, "opaque")
reg.Config().MustSet(config.KeyConsentRequestMaxAge, time.Hour)

defaultRedirectedMessage := "redirected to default server"
postLogoutCallback := func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -40,7 +40,7 @@ func TestLogoutFlows(t *testing.T) {
}
defaultLogoutURL := testhelpers.NewCallbackURL(t, "logged-out", postLogoutCallback)
customPostLogoutURL := testhelpers.NewCallbackURL(t, "logged-out/custom", postLogoutCallback)
reg.Config().Set(config.KeyLogoutRedirectURL, defaultLogoutURL)
reg.Config().MustSet(config.KeyLogoutRedirectURL, defaultLogoutURL)

publicTS, adminTS := testhelpers.NewOAuth2Server(t, reg)
adminApi := hydra.NewHTTPClientWithConfig(nil, &hydra.TransportConfig{Schemes: []string{"http"}, Host: urlx.ParseOrPanic(adminTS.URL).Host})
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestLogoutFlows(t *testing.T) {

t.Cleanup(server.Close)

reg.Config().Set(config.KeyLogoutURL, server.URL)
reg.Config().MustSet(config.KeyLogoutURL, server.URL)
}

acceptLoginAsAndWatchSid := func(t *testing.T, subject string, sid chan string) {
Expand Down
12 changes: 6 additions & 6 deletions consent/strategy_oauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import (

func TestStrategyLoginConsentNext(t *testing.T) {
reg := internal.NewMockedRegistry(t)
reg.Config().Set(config.KeyAccessTokenStrategy, "opaque")
reg.Config().Set(config.KeyConsentRequestMaxAge, time.Hour)
reg.Config().Set(config.KeyConsentRequestMaxAge, time.Hour)
reg.Config().Set(config.KeyScopeStrategy, "exact")
reg.Config().Set(config.KeySubjectTypesSupported, []string{"pairwise", "public"})
reg.Config().Set(config.KeySubjectIdentifierAlgorithmSalt, "76d5d2bf-747f-4592-9fbd-d2b895a54b3a")
reg.Config().MustSet(config.KeyAccessTokenStrategy, "opaque")
reg.Config().MustSet(config.KeyConsentRequestMaxAge, time.Hour)
reg.Config().MustSet(config.KeyConsentRequestMaxAge, time.Hour)
reg.Config().MustSet(config.KeyScopeStrategy, "exact")
reg.Config().MustSet(config.KeySubjectTypesSupported, []string{"pairwise", "public"})
reg.Config().MustSet(config.KeySubjectIdentifierAlgorithmSalt, "76d5d2bf-747f-4592-9fbd-d2b895a54b3a")

publicTS, adminTS := testhelpers.NewOAuth2Server(t, reg)
adminClient := hydra.NewHTTPClientWithConfig(nil, &hydra.TransportConfig{Schemes: []string{"http"}, Host: urlx.ParseOrPanic(adminTS.URL).Host})
Expand Down
31 changes: 18 additions & 13 deletions driver/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"strings"
"time"

"github.com/spf13/pflag"

"github.com/ory/x/dbal"

"github.com/markbates/pkger"
Expand Down Expand Up @@ -91,15 +89,15 @@ type Provider struct {
p *configx.Provider
}

func MustNew(flags *pflag.FlagSet, l *logrusx.Logger) *Provider {
p, err := New(flags, l)
func MustNew(l *logrusx.Logger, opts ...configx.OptionModifier) *Provider {
p, err := New(l, opts...)
if err != nil {
l.WithError(err).Fatalf("Unable to load config.")
}
return p
}

func New(flags *pflag.FlagSet, l *logrusx.Logger) (*Provider, error) {
func New(l *logrusx.Logger, opts ...configx.OptionModifier) (*Provider, error) {
f, err := pkger.Open("/.schema/config.schema.json")
if err != nil {
return nil, err
Expand All @@ -110,23 +108,30 @@ func New(flags *pflag.FlagSet, l *logrusx.Logger) (*Provider, error) {
return nil, err
}

p, err := configx.New(
schema,
flags,
opts = append([]configx.OptionModifier{
configx.WithStderrValidationReporter(),
configx.OmitKeysFromTracing([]string{"dsn", "secrets.system", "secrets.cookie"}),
configx.WithImmutables([]string{"log", "serve", "dsn", "profiling"}),
configx.OmitKeysFromTracing("dsn", "secrets.system", "secrets.cookie"),
configx.WithImmutables("log", "serve", "dsn", "profiling"),
configx.WithLogrusWatcher(l),
)
}, opts...)

p, err := configx.New(schema, opts...)
if err != nil {
return nil, err
}

l.UseConfig(p)
return &Provider{l: l, p: p}, nil
}

func (p *Provider) Set(key string, value interface{}) {
p.p.Set(key, value)
func (p *Provider) Set(key string, value interface{}) error {
return p.p.Set(key, value)
}

func (p *Provider) MustSet(key string, value interface{}) {
if err := p.Set(key, value); err != nil {
p.l.WithError(err).Fatalf("Unable to set \"%s\" to \"%s\".", key, value)
}
}

func (p *Provider) Source() *configx.Provider {
Expand Down
43 changes: 19 additions & 24 deletions driver/config/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"testing"
"time"

"github.com/spf13/pflag"

"github.com/ory/x/configx"
"github.com/ory/x/dbal"

Expand All @@ -27,7 +25,7 @@ import (
)

func newProvider() *Provider {
return MustNew(pflag.NewFlagSet("config", pflag.ContinueOnError), logrusx.New("", ""))
return MustNew(logrusx.New("", ""))
}

func setupEnv(env map[string]string) func(t *testing.T) (func(), func()) {
Expand Down Expand Up @@ -75,7 +73,7 @@ func TestSubjectTypesSupported(t *testing.T) {
setup, clean := tc.env(t)
setup()
p := newProvider()
p.Set(KeySubjectIdentifierAlgorithmSalt, "00000000")
p.MustSet(KeySubjectIdentifierAlgorithmSalt, "00000000")
assert.EqualValues(t, tc.e, p.SubjectTypesSupported())
clean()
})
Expand All @@ -89,7 +87,7 @@ func TestWellKnownKeysUnique(t *testing.T) {

func TestCORSOptions(t *testing.T) {
p := newProvider()
p.Set("serve.public.cors.enabled", true)
p.MustSet("serve.public.cors.enabled", true)

conf, enabled := p.PublicCORS()
assert.True(t, enabled)
Expand All @@ -110,12 +108,12 @@ func TestProviderAdminDisableHealthAccessLog(t *testing.T) {
l := logrusx.New("", "")
l.Logrus().SetOutput(ioutil.Discard)

p := MustNew(pflag.NewFlagSet("config", pflag.ContinueOnError), l)
p := MustNew(l)

value := p.AdminDisableHealthAccessLog()
assert.Equal(t, false, value)

p.Set(KeyAdminDisableHealthAccessLog, "true")
p.MustSet(KeyAdminDisableHealthAccessLog, "true")

value = p.AdminDisableHealthAccessLog()
assert.Equal(t, true, value)
Expand All @@ -125,12 +123,12 @@ func TestProviderPublicDisableHealthAccessLog(t *testing.T) {
l := logrusx.New("", "")
l.Logrus().SetOutput(ioutil.Discard)

p := MustNew(pflag.NewFlagSet("config", pflag.ContinueOnError), l)
p := MustNew(l)

value := p.PublicDisableHealthAccessLog()
assert.Equal(t, false, value)

p.Set(KeyPublicDisableHealthAccessLog, "true")
p.MustSet(KeyPublicDisableHealthAccessLog, "true")

value = p.PublicDisableHealthAccessLog()
assert.Equal(t, true, value)
Expand All @@ -139,40 +137,37 @@ func TestProviderPublicDisableHealthAccessLog(t *testing.T) {
func TestProviderIssuerURL(t *testing.T) {
l := logrusx.New("", "")
l.Logrus().SetOutput(ioutil.Discard)
p := MustNew(pflag.NewFlagSet("config", pflag.ContinueOnError), l)
p.Set(KeyIssuerURL, "http://hydra.localhost")
p := MustNew(l)
p.MustSet(KeyIssuerURL, "http://hydra.localhost")
assert.Equal(t, "http://hydra.localhost/", p.IssuerURL().String())

p2 := MustNew(pflag.NewFlagSet("config", pflag.ContinueOnError), l)
p2.Set(KeyIssuerURL, "http://hydra.localhost/")
p2 := MustNew(l)
p2.MustSet(KeyIssuerURL, "http://hydra.localhost/")
assert.Equal(t, "http://hydra.localhost/", p2.IssuerURL().String())
}

func TestProviderCookieSameSiteMode(t *testing.T) {
l := logrusx.New("", "")
l.Logrus().SetOutput(ioutil.Discard)

p := MustNew(pflag.NewFlagSet("config", pflag.ContinueOnError), l)
p.Set("dangerous-force-http", false)
p.Set(KeyCookieSameSiteMode, "")
p := MustNew(l, configx.SkipValidation())
p.MustSet("dangerous-force-http", false)
p.MustSet(KeyCookieSameSiteMode, "")
assert.Equal(t, http.SameSiteDefaultMode, p.CookieSameSiteMode())

p.Set(KeyCookieSameSiteMode, "none")
p.MustSet(KeyCookieSameSiteMode, "none")
assert.Equal(t, http.SameSiteNoneMode, p.CookieSameSiteMode())

p = MustNew(pflag.NewFlagSet("config", pflag.ContinueOnError), l)
p.Set("dangerous-force-http", true)
p = MustNew(l, configx.SkipValidation())
p.MustSet("dangerous-force-http", true)
assert.Equal(t, http.SameSiteLaxMode, p.CookieSameSiteMode())
p.Set(KeyCookieSameSiteMode, "none")
p.MustSet(KeyCookieSameSiteMode, "none")
assert.Equal(t, http.SameSiteLaxMode, p.CookieSameSiteMode())
}

func TestViperProviderValidates(t *testing.T) {
l := logrusx.New("", "")
flags := pflag.NewFlagSet("config", pflag.ContinueOnError)
configx.RegisterFlags(flags)
require.NoError(t, flags.Parse([]string{"--config", "../../internal/.hydra.yaml"}))
c := MustNew(flags, l)
c := MustNew(l, configx.WithConfigFiles("../../internal/.hydra.yaml"))

// log
assert.Equal(t, "debug", c.Source().String(KeyLogLevel))
Expand Down
Loading

0 comments on commit 0b48253

Please sign in to comment.