Skip to content

Commit 87cce88

Browse files
authored
Make DatastoreConfig an interface (#324)
1 parent 6390296 commit 87cce88

File tree

12 files changed

+75
-128
lines changed

12 files changed

+75
-128
lines changed

cmd/warrant/main.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ type ServiceEnv struct {
4848
Datastore database.Database
4949
}
5050

51-
func (env ServiceEnv) DB() database.Database {
51+
func (env *ServiceEnv) DB() database.Database {
5252
return env.Datastore
5353
}
5454

5555
func (env *ServiceEnv) InitDB(cfg config.Config) error {
5656
ctx, cancelFunc := context.WithTimeout(context.Background(), 10*time.Second)
5757
defer cancelFunc()
5858

59-
if cfg.GetDatastore().MySQL.Hostname != "" || cfg.GetDatastore().MySQL.DSN != "" {
60-
db := database.NewMySQL(*cfg.GetDatastore().MySQL)
59+
if cfg.GetDatastore().GetMySQL().Hostname != "" || cfg.GetDatastore().GetMySQL().DSN != "" {
60+
db := database.NewMySQL(*cfg.GetDatastore().GetMySQL())
6161
err := db.Connect(ctx)
6262
if err != nil {
6363
return err
@@ -74,8 +74,8 @@ func (env *ServiceEnv) InitDB(cfg config.Config) error {
7474
return nil
7575
}
7676

77-
if cfg.GetDatastore().Postgres.Hostname != "" {
78-
db := database.NewPostgres(*cfg.GetDatastore().Postgres)
77+
if cfg.GetDatastore().GetPostgres().Hostname != "" {
78+
db := database.NewPostgres(*cfg.GetDatastore().GetPostgres())
7979
err := db.Connect(ctx)
8080
if err != nil {
8181
return err
@@ -92,8 +92,8 @@ func (env *ServiceEnv) InitDB(cfg config.Config) error {
9292
return nil
9393
}
9494

95-
if cfg.GetDatastore().SQLite.Database != "" {
96-
db := database.NewSQLite(*cfg.GetDatastore().SQLite)
95+
if cfg.GetDatastore().GetSQLite().Database != "" {
96+
db := database.NewSQLite(*cfg.GetDatastore().GetSQLite())
9797
err := db.Connect(ctx)
9898
if err != nil {
9999
return err
@@ -113,8 +113,8 @@ func (env *ServiceEnv) InitDB(cfg config.Config) error {
113113
return errors.New("invalid database configuration provided")
114114
}
115115

116-
func NewServiceEnv() ServiceEnv {
117-
return ServiceEnv{
116+
func NewServiceEnv() *ServiceEnv {
117+
return &ServiceEnv{
118118
Datastore: nil,
119119
}
120120
}
@@ -155,22 +155,22 @@ func main() {
155155
querySvc := query.NewService(svcEnv, objectTypeSvc, warrantSvc, objectSvc)
156156

157157
// Init feature service
158-
featureSvc := feature.NewService(&svcEnv, objectSvc)
158+
featureSvc := feature.NewService(svcEnv, objectSvc)
159159

160160
// Init permission service
161-
permissionSvc := permission.NewService(&svcEnv, objectSvc)
161+
permissionSvc := permission.NewService(svcEnv, objectSvc)
162162

163163
// Init pricing tier service
164-
pricingTierSvc := pricingtier.NewService(&svcEnv, objectSvc)
164+
pricingTierSvc := pricingtier.NewService(svcEnv, objectSvc)
165165

166166
// Init role service
167-
roleSvc := role.NewService(&svcEnv, objectSvc)
167+
roleSvc := role.NewService(svcEnv, objectSvc)
168168

169169
// Init tenant service
170-
tenantSvc := tenant.NewService(&svcEnv, objectSvc)
170+
tenantSvc := tenant.NewService(svcEnv, objectSvc)
171171

172172
// Init user service
173-
userSvc := user.NewService(&svcEnv, objectSvc)
173+
userSvc := user.NewService(svcEnv, objectSvc)
174174

175175
svcs := []service.Service{
176176
checkSvc,

pkg/authz/objecttype/model.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ type Model interface {
3333
}
3434

3535
type ObjectType struct {
36-
ID int64 `mysql:"id" postgres:"id" sqlite:"id"`
37-
TypeId string `mysql:"typeId" postgres:"type_id" sqlite:"typeId"`
36+
ID int64 `mysql:"id" postgres:"id" sqlite:"id"`
37+
TypeId string `mysql:"typeId" postgres:"type_id" sqlite:"typeId"`
3838
Definition string `mysql:"definition" postgres:"definition" sqlite:"definition"`
39-
CreatedAt time.Time `mysql:"createdAt" postgres:"created_at" sqlite:"createdAt"`
40-
UpdatedAt time.Time `mysql:"updatedAt" postgres:"updated_at" sqlite:"updatedAt"`
41-
DeletedAt *time.Time `mysql:"deletedAt" postgres:"deleted_at" sqlite:"deletedAt"`
39+
CreatedAt time.Time `mysql:"createdAt" postgres:"created_at" sqlite:"createdAt"`
40+
UpdatedAt time.Time `mysql:"updatedAt" postgres:"updated_at" sqlite:"updatedAt"`
41+
DeletedAt *time.Time `mysql:"deletedAt" postgres:"deleted_at" sqlite:"deletedAt"`
4242
}
4343

4444
func (objectType ObjectType) GetID() int64 {

pkg/authz/warrant/handlers.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,5 @@ func buildFilterOptions(r *http.Request) *FilterParams {
175175
filterOptions.SubjectRelation = queryParams.Get("subjectRelation")
176176
}
177177

178-
if queryParams.Has("policy") {
179-
filterOptions.Policy = Policy(queryParams.Get("policy"))
180-
}
181-
182178
return &filterOptions
183179
}

pkg/authz/warrant/list.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ type FilterParams struct {
2929
SubjectType string `json:"subjectType,omitempty"`
3030
SubjectId string `json:"subjectId,omitempty"`
3131
SubjectRelation string `json:"subjectRelation,omitempty"`
32-
Policy Policy `json:"policy,omitempty"`
3332
}
3433

3534
func (fp FilterParams) String() string {
@@ -58,10 +57,6 @@ func (fp FilterParams) String() string {
5857
s = fmt.Sprintf("%s&subjectRelation=%s", s, fp.SubjectRelation)
5958
}
6059

61-
if fp.Policy != "" {
62-
s = fmt.Sprintf("%s&policy=%s", s, fp.Policy)
63-
}
64-
6560
return strings.TrimPrefix(s, "&")
6661
}
6762

@@ -79,7 +74,6 @@ func (parser WarrantListParamParser) GetSupportedSortBys() []string {
7974
}
8075

8176
func (parser WarrantListParamParser) ParseValue(val string, sortBy string) (interface{}, error) {
82-
// TODO: add support for more sortBy columns
8377
switch sortBy {
8478
case "createdAt":
8579
value, err := time.Parse(time.RFC3339, val)

pkg/authz/warrant/model.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ type Model interface {
3737
}
3838

3939
type Warrant struct {
40-
ID int64 `mysql:"id" postgres:"id" sqlite:"id"`
41-
ObjectType string `mysql:"objectType" postgres:"object_type" sqlite:"objectType"`
42-
ObjectId string `mysql:"objectId" postgres:"object_id" sqlite:"objectId"`
43-
Relation string `mysql:"relation" postgres:"relation" sqlite:"relation"`
44-
SubjectType string `mysql:"subjectType" postgres:"subject_type" sqlite:"subjectType"`
45-
SubjectId string `mysql:"subjectId" postgres:"subject_id" sqlite:"subjectId"`
40+
ID int64 `mysql:"id" postgres:"id" sqlite:"id"`
41+
ObjectType string `mysql:"objectType" postgres:"object_type" sqlite:"objectType"`
42+
ObjectId string `mysql:"objectId" postgres:"object_id" sqlite:"objectId"`
43+
Relation string `mysql:"relation" postgres:"relation" sqlite:"relation"`
44+
SubjectType string `mysql:"subjectType" postgres:"subject_type" sqlite:"subjectType"`
45+
SubjectId string `mysql:"subjectId" postgres:"subject_id" sqlite:"subjectId"`
4646
SubjectRelation string `mysql:"subjectRelation" postgres:"subject_relation" sqlite:"subjectRelation"`
47-
Policy Policy `mysql:"policy" postgres:"policy" sqlite:"policy"`
48-
PolicyHash string `mysql:"policyHash" postgres:"policy_hash" sqlite:"policyHash"`
49-
CreatedAt time.Time `mysql:"createdAt" postgres:"created_at" sqlite:"createdAt"`
50-
UpdatedAt time.Time `mysql:"updatedAt" postgres:"updated_at" sqlite:"updatedAt"`
51-
DeletedAt *time.Time `mysql:"deletedAt" postgres:"deleted_at" sqlite:"deletedAt"`
47+
Policy Policy `mysql:"policy" postgres:"policy" sqlite:"policy"`
48+
PolicyHash string `mysql:"policyHash" postgres:"policy_hash" sqlite:"policyHash"`
49+
CreatedAt time.Time `mysql:"createdAt" postgres:"created_at" sqlite:"createdAt"`
50+
UpdatedAt time.Time `mysql:"updatedAt" postgres:"updated_at" sqlite:"updatedAt"`
51+
DeletedAt *time.Time `mysql:"deletedAt" postgres:"deleted_at" sqlite:"deletedAt"`
5252
}
5353

5454
func (warrant Warrant) GetID() int64 {

pkg/authz/warrant/mysql.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,6 @@ func (repo MySQLRepository) List(ctx context.Context, filterParams FilterParams,
229229
replacements = append(replacements, filterParams.SubjectRelation)
230230
}
231231

232-
if filterParams.Policy != "" {
233-
query = fmt.Sprintf("%s AND policyHash = ?", query)
234-
replacements = append(replacements, filterParams.Policy.Hash())
235-
}
236-
237232
if listParams.NextCursor != nil {
238233
comparisonOp := "<"
239234
if listParams.SortOrder == service.SortOrderAsc {

pkg/authz/warrant/postgres.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,6 @@ func (repo PostgresRepository) List(ctx context.Context, filterParams FilterPara
234234
replacements = append(replacements, filterParams.SubjectRelation)
235235
}
236236

237-
if filterParams.Policy != "" {
238-
query = fmt.Sprintf("%s AND policy_hash = ?", query)
239-
replacements = append(replacements, filterParams.Policy.Hash())
240-
}
241-
242237
if listParams.NextCursor != nil {
243238
comparisonOp := "<"
244239
if listParams.SortOrder == service.SortOrderAsc {

pkg/authz/warrant/sqlite.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,6 @@ func (repo SQLiteRepository) List(ctx context.Context, filterParams FilterParams
238238
replacements = append(replacements, filterParams.SubjectRelation)
239239
}
240240

241-
if filterParams.Policy != "" {
242-
query = fmt.Sprintf("%s AND policyHash = ?", query)
243-
replacements = append(replacements, filterParams.Policy.Hash())
244-
}
245-
246241
if listParams.NextCursor != nil {
247242
comparisonOp := "<"
248243
if listParams.SortOrder == service.SortOrderAsc {

pkg/config/config.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ type Config interface {
4141
GetLogLevel() int8
4242
GetEnableAccessLog() bool
4343
GetAutoMigrate() bool
44-
GetDatastore() *DatastoreConfig
44+
GetDatastore() DatastoreConfig
4545
}
4646

4747
type WarrantConfig struct {
48-
Port int `mapstructure:"port"`
49-
LogLevel int8 `mapstructure:"logLevel"`
50-
EnableAccessLog bool `mapstructure:"enableAccessLog"`
51-
AutoMigrate bool `mapstructure:"autoMigrate"`
52-
Datastore *DatastoreConfig `mapstructure:"datastore"`
53-
Authentication *AuthConfig `mapstructure:"authentication"`
54-
Check *CheckConfig `mapstructure:"check"`
48+
Port int `mapstructure:"port"`
49+
LogLevel int8 `mapstructure:"logLevel"`
50+
EnableAccessLog bool `mapstructure:"enableAccessLog"`
51+
AutoMigrate bool `mapstructure:"autoMigrate"`
52+
Datastore *WarrantDatastoreConfig `mapstructure:"datastore"`
53+
Authentication *AuthConfig `mapstructure:"authentication"`
54+
Check *CheckConfig `mapstructure:"check"`
5555
}
5656

5757
func (warrantConfig WarrantConfig) GetPort() int {
@@ -70,7 +70,7 @@ func (warrantConfig WarrantConfig) GetAutoMigrate() bool {
7070
return warrantConfig.AutoMigrate
7171
}
7272

73-
func (warrantConfig WarrantConfig) GetDatastore() *DatastoreConfig {
73+
func (warrantConfig WarrantConfig) GetDatastore() DatastoreConfig {
7474
return warrantConfig.Datastore
7575
}
7676

@@ -82,12 +82,30 @@ func (warrantConfig WarrantConfig) GetCheck() *CheckConfig {
8282
return warrantConfig.Check
8383
}
8484

85-
type DatastoreConfig struct {
85+
type DatastoreConfig interface {
86+
GetMySQL() *MySQLConfig
87+
GetPostgres() *PostgresConfig
88+
GetSQLite() *SQLiteConfig
89+
}
90+
91+
type WarrantDatastoreConfig struct {
8692
MySQL *MySQLConfig `mapstructure:"mysql"`
8793
Postgres *PostgresConfig `mapstructure:"postgres"`
8894
SQLite *SQLiteConfig `mapstructure:"sqlite"`
8995
}
9096

97+
func (warrantDatastoreConfig WarrantDatastoreConfig) GetMySQL() *MySQLConfig {
98+
return warrantDatastoreConfig.MySQL
99+
}
100+
101+
func (warrantDatastoreConfig WarrantDatastoreConfig) GetPostgres() *PostgresConfig {
102+
return warrantDatastoreConfig.Postgres
103+
}
104+
105+
func (warrantDatastoreConfig WarrantDatastoreConfig) GetSQLite() *SQLiteConfig {
106+
return warrantDatastoreConfig.SQLite
107+
}
108+
91109
type MySQLConfig struct {
92110
Username string `mapstructure:"username"`
93111
Password string `mapstructure:"password"`

pkg/database/sql.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ func (q SqlTx) ExecContext(ctx context.Context, query string, args ...interface{
6464
query = q.Tx.Rebind(query)
6565
result, err := q.Tx.ExecContext(ctx, query, args...)
6666
if err != nil {
67-
switch err {
68-
case sql.ErrNoRows:
67+
switch {
68+
case errors.Is(err, sql.ErrNoRows):
6969
return result, err
7070
default:
7171
return result, errors.Wrap(err, "SqlTx error")
@@ -78,8 +78,8 @@ func (q SqlTx) GetContext(ctx context.Context, dest interface{}, query string, a
7878
query = q.Tx.Rebind(query)
7979
err := q.Tx.GetContext(ctx, dest, query, args...)
8080
if err != nil {
81-
switch err {
82-
case sql.ErrNoRows:
81+
switch {
82+
case errors.Is(err, sql.ErrNoRows):
8383
return err
8484
default:
8585
return errors.Wrap(err, "SqlTx error")
@@ -92,8 +92,8 @@ func (q SqlTx) NamedExecContext(ctx context.Context, query string, arg interface
9292
query = q.Tx.Rebind(query)
9393
result, err := q.Tx.NamedExecContext(ctx, query, arg)
9494
if err != nil {
95-
switch err {
96-
case sql.ErrNoRows:
95+
switch {
96+
case errors.Is(err, sql.ErrNoRows):
9797
return result, err
9898
default:
9999
return result, errors.Wrap(err, "SqlTx error")
@@ -119,8 +119,8 @@ func (q SqlTx) SelectContext(ctx context.Context, dest interface{}, query string
119119
query = q.Tx.Rebind(query)
120120
err := q.Tx.SelectContext(ctx, dest, query, args...)
121121
if err != nil {
122-
switch err {
123-
case sql.ErrNoRows:
122+
switch {
123+
case errors.Is(err, sql.ErrNoRows):
124124
return err
125125
default:
126126
return errors.Wrap(err, "SqlTx error")
@@ -203,8 +203,8 @@ func (ds SQL) ExecContext(ctx context.Context, query string, args ...interface{}
203203

204204
result, err := queryable.ExecContext(ctx, query, args...)
205205
if err != nil {
206-
switch err {
207-
case sql.ErrNoRows:
206+
switch {
207+
case errors.Is(err, sql.ErrNoRows):
208208
return result, err
209209
default:
210210
return result, errors.Wrap(err, "Error when calling sql ExecContext")
@@ -222,8 +222,8 @@ func (ds SQL) GetContext(ctx context.Context, dest interface{}, query string, ar
222222

223223
err := queryable.GetContext(ctx, dest, query, args...)
224224
if err != nil {
225-
switch err {
226-
case sql.ErrNoRows:
225+
switch {
226+
case errors.Is(err, sql.ErrNoRows):
227227
return err
228228
default:
229229
return errors.Wrap(err, "Error when calling sql GetContext")
@@ -241,8 +241,8 @@ func (ds SQL) NamedExecContext(ctx context.Context, query string, arg interface{
241241

242242
result, err := queryable.NamedExecContext(ctx, query, arg)
243243
if err != nil {
244-
switch err {
245-
case sql.ErrNoRows:
244+
switch {
245+
case errors.Is(err, sql.ErrNoRows):
246246
return result, err
247247
default:
248248
return result, errors.Wrap(err, "Error when calling sql NamedExecContext")
@@ -278,8 +278,8 @@ func (ds SQL) SelectContext(ctx context.Context, dest interface{}, query string,
278278

279279
err := queryable.SelectContext(ctx, dest, query, args...)
280280
if err != nil {
281-
switch err {
282-
case sql.ErrNoRows:
281+
switch {
282+
case errors.Is(err, sql.ErrNoRows):
283283
return err
284284
default:
285285
return errors.Wrap(err, "Error when calling sql SelectContext")

tests/v1/warrants-list.json

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -295,28 +295,6 @@
295295
]
296296
}
297297
},
298-
{
299-
"name": "listWarrantsFilterByPolicy",
300-
"request": {
301-
"method": "GET",
302-
"url": "/v1/warrants?policy=tenant%20%3D%3D%20%22tenant-a%22%20%26%26%20organization%20%3D%3D%20%22org-a%22"
303-
},
304-
"expectedResponse": {
305-
"statusCode": 200,
306-
"body": [
307-
{
308-
"objectType": "role",
309-
"objectId": "senior-accountant",
310-
"relation": "member",
311-
"subject": {
312-
"objectType": "user",
313-
"objectId": "user-a"
314-
},
315-
"policy": "tenant == \"tenant-a\" \u0026\u0026 organization == \"org-a\""
316-
}
317-
]
318-
}
319-
},
320298
{
321299
"name": "removeRoleSeniorAccountantFromUserAWithPolicy",
322300
"request": {

0 commit comments

Comments
 (0)