/
driver.go
142 lines (116 loc) · 4.07 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"context"
"sync"
"testing"
"github.com/go-jose/go-jose/v3"
"github.com/ory/x/configx"
"github.com/stretchr/testify/require"
"github.com/ory/hydra/v2/x"
"github.com/ory/x/contextx"
"github.com/ory/x/sqlcon/dockertest"
"github.com/ory/hydra/v2/driver"
"github.com/ory/hydra/v2/driver/config"
"github.com/ory/hydra/v2/jwk"
"github.com/ory/x/logrusx"
)
func resetConfig(p *config.DefaultProvider) {
p.MustSet(context.Background(), config.KeyBCryptCost, "4")
p.MustSet(context.Background(), config.KeySubjectIdentifierAlgorithmSalt, "00000000")
p.MustSet(context.Background(), config.KeyGetSystemSecret, []string{"000000000000000000000000000000000000000000000000"})
p.MustSet(context.Background(), config.KeyGetCookieSecrets, []string{"000000000000000000000000000000000000000000000000"})
p.MustSet(context.Background(), config.KeyLogLevel, "trace")
}
func NewConfigurationWithDefaults() *config.DefaultProvider {
p := config.MustNew(context.Background(), logrusx.New("", ""), configx.SkipValidation())
resetConfig(p)
p.MustSet(context.Background(), config.KeyTLSEnabled, false)
return p
}
func NewConfigurationWithDefaultsAndHTTPS() *config.DefaultProvider {
p := config.MustNew(context.Background(), logrusx.New("", ""), configx.SkipValidation())
resetConfig(p)
p.MustSet(context.Background(), config.KeyTLSEnabled, true)
return p
}
func NewRegistryMemory(t testing.TB, c *config.DefaultProvider, ctxer contextx.Contextualizer) driver.Registry {
return newRegistryDefault(t, "memory", c, true, ctxer)
}
func NewMockedRegistry(t testing.TB, ctxer contextx.Contextualizer) driver.Registry {
return newRegistryDefault(t, "memory", NewConfigurationWithDefaults(), true, ctxer)
}
func NewRegistrySQLFromURL(t testing.TB, url string, migrate bool, ctxer contextx.Contextualizer) driver.Registry {
return newRegistryDefault(t, url, NewConfigurationWithDefaults(), migrate, ctxer)
}
func newRegistryDefault(t testing.TB, url string, c *config.DefaultProvider, migrate bool, ctxer contextx.Contextualizer) driver.Registry {
ctx := context.Background()
c.MustSet(ctx, config.KeyLogLevel, "trace")
c.MustSet(ctx, config.KeyDSN, url)
c.MustSet(ctx, "dev", true)
r, err := driver.NewRegistryFromDSN(ctx, c, logrusx.New("test_hydra", "master"), false, migrate, ctxer)
require.NoError(t, err)
return r
}
func CleanAndMigrate(reg driver.Registry) func(*testing.T) {
return func(t *testing.T) {
x.CleanSQLPop(t, reg.Persister().Connection(context.Background()))
require.NoError(t, reg.Persister().MigrateUp(context.Background()))
t.Log("clean and migrate done")
}
}
func ConnectToMySQL(t testing.TB) string {
return dockertest.RunTestMySQLWithVersion(t, "8.0.26")
}
func ConnectToPG(t testing.TB) string {
return dockertest.RunTestPostgreSQLWithVersion(t, "11.8")
}
func ConnectToCRDB(t testing.TB) string {
return dockertest.RunTestCockroachDBWithVersion(t, "v22.1.2")
}
func ConnectDatabases(t *testing.T, migrate bool, ctxer contextx.Contextualizer) (pg, mysql, crdb driver.Registry, clean func(*testing.T)) {
var pgURL, mysqlURL, crdbURL string
wg := sync.WaitGroup{}
wg.Add(3)
go func() {
pgURL = ConnectToPG(t)
t.Log("Pg done")
wg.Done()
}()
go func() {
mysqlURL = ConnectToMySQL(t)
t.Log("myssql done")
wg.Done()
}()
go func() {
crdbURL = ConnectToCRDB(t)
t.Log("crdb done")
wg.Done()
}()
t.Log("beginning to wait")
wg.Wait()
t.Log("done waiting")
pg = NewRegistrySQLFromURL(t, pgURL, migrate, ctxer)
mysql = NewRegistrySQLFromURL(t, mysqlURL, migrate, ctxer)
crdb = NewRegistrySQLFromURL(t, crdbURL, migrate, ctxer)
dbs := []driver.Registry{pg, mysql, crdb}
clean = func(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(len(dbs))
for _, db := range dbs {
go func(db driver.Registry) {
defer wg.Done()
CleanAndMigrate(db)(t)
}(db)
}
wg.Wait()
}
clean(t)
return
}
func MustEnsureRegistryKeys(ctx context.Context, r driver.Registry, key string) {
if err := jwk.EnsureAsymmetricKeypairExists(ctx, r, string(jose.ES256), key); err != nil {
panic(err)
}
}