Skip to content

Commit 8bc226d

Browse files
authored
Merge pull request #15 from dipdup-io/GO-84-fix-warning-with-dropping-permission-on-user-role
2 parents 32ef3f0 + 90a2d4c commit 8bc226d

File tree

7 files changed

+84
-33
lines changed

7 files changed

+84
-33
lines changed

config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Hasura struct {
5858
EnableAggregations bool `yaml:"allow_aggregation"`
5959
Source *HasuraSource `yaml:"source"`
6060
Rest *bool `yaml:"rest"`
61+
UnauthorizedRole string `yaml:"unauthorized_role"`
6162
}
6263

6364
type HasuraSource struct {
@@ -75,6 +76,8 @@ func (h *Hasura) UnmarshalYAML(unmarshal func(interface{}) error) error {
7576
IsolationLevel: "read-committed",
7677
}
7778

79+
h.UnauthorizedRole = "user"
80+
7881
type plain Hasura
7982
return unmarshal((*plain)(h))
8083
}

database/pgComment.go renamed to database/comment.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ import (
1010
func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}) error {
1111
for _, model := range models {
1212
modelType := reflect.TypeOf(model)
13+
14+
if reflect.ValueOf(model).Kind() == reflect.Ptr {
15+
modelType = modelType.Elem()
16+
}
17+
1318
var tableName string
1419

1520
for i := 0; i < modelType.NumField(); i++ {
@@ -22,19 +27,19 @@ func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}
2227
tableName = hasura.ToSnakeCase(modelType.Name())
2328
}
2429

25-
pgCommentTag, ok := getPgComment(fieldType)
30+
comment, ok := getComment(fieldType)
2631
if !ok {
2732
continue
2833
}
2934

30-
if err := sc.MakeTableComment(ctx, tableName, pgCommentTag); err != nil {
35+
if err := sc.MakeTableComment(ctx, tableName, comment); err != nil {
3136
return err
3237
}
3338

3439
continue
3540
}
3641

37-
pgCommentTag, ok := getPgComment(fieldType)
42+
comment, ok := getComment(fieldType)
3843
if !ok {
3944
continue
4045
}
@@ -44,7 +49,7 @@ func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}
4449
columnName = hasura.ToSnakeCase(fieldType.Name)
4550
}
4651

47-
if err := sc.MakeColumnComment(ctx, tableName, columnName, pgCommentTag); err != nil {
52+
if err := sc.MakeColumnComment(ctx, tableName, columnName, comment); err != nil {
4853
return err
4954
}
5055
}
@@ -68,11 +73,11 @@ func getPgName(fieldType reflect.StructField) (name string, ok bool) {
6873
return "", false
6974
}
7075

71-
func getPgComment(fieldType reflect.StructField) (string, bool) {
72-
pgCommentTag, ok := fieldType.Tag.Lookup("pg-comment")
76+
func getComment(fieldType reflect.StructField) (string, bool) {
77+
commentTag, ok := fieldType.Tag.Lookup("comment")
7378

7479
if ok {
75-
return pgCommentTag, ok
80+
return commentTag, ok
7681
}
7782

7883
return "", false

database/pgComment_test.go renamed to database/comment_test.go

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func initMocks(t *testing.T) (*gomock.Controller, *mocks.MockSchemeCommenter, co
1919
func TestMakeCommentsWithTableName(t *testing.T) {
2020
type Ballot struct {
2121
//nolint
22-
tableName struct{} `pg:"ballots" pg-comment:"Ballot table"`
22+
tableName struct{} `pg:"ballots" comment:"Ballot table"`
2323
Ballot string `json:"ballot"`
2424
}
2525

@@ -76,7 +76,7 @@ func TestMakeCommentsFieldWithPgComment(t *testing.T) {
7676
type Ballot struct {
7777
//nolint
7878
tableName struct{} `pg:"ballots"`
79-
Ballot string `json:"ballot" pg-comment:"This is field comment"`
79+
Ballot string `json:"ballot" comment:"This is field comment"`
8080
}
8181

8282
mockCtrl, mockSC, ctx := initMocks(t)
@@ -101,22 +101,22 @@ func TestMakeCommentsFieldWithPgComment(t *testing.T) {
101101
func TestMakeCommentsWithTableNameAndFieldsWithPgComment(t *testing.T) {
102102
type Ballot struct {
103103
//nolint
104-
tableName struct{} `pg:"ballots" pg-comment:"Ballot table"`
105-
CreatedAt int64 `json:"-" pg-comment:"This is field comment"`
106-
UpdatedAt int64 `json:"-" pg-comment:"This is field comment"`
107-
Network string `json:"network" pg:",pk" pg-comment:"This is field comment"`
108-
Hash string `json:"hash" pg:",pk" pg-comment:"This is field comment"`
109-
Branch string `json:"branch" pg-comment:"This is field comment"`
110-
Status string `json:"status" pg-comment:"This is field comment"`
111-
Kind string `json:"kind" pg-comment:"This is field comment"`
112-
Signature string `json:"signature" pg-comment:"This is field comment"`
113-
Protocol string `json:"protocol" pg-comment:"This is field comment"`
114-
Level uint64 `json:"level" pg-comment:"This is field comment"`
115-
Errors interface{} `json:"errors,omitempty" pg:"type:jsonb" pg-comment:"This is field comment"`
116-
ExpirationLevel *uint64 `json:"expiration_level" pg-comment:"This is field comment"`
117-
Raw interface{} `json:"raw,omitempty" pg:"type:jsonb" pg-comment:"This is field comment"`
118-
Ballot string `json:"ballot" pg-comment:"This is field comment"`
119-
Period int64 `json:"period" pg-comment:"This is field comment"`
104+
tableName struct{} `pg:"ballots" comment:"Ballot table"`
105+
CreatedAt int64 `json:"-" comment:"This is field comment"`
106+
UpdatedAt int64 `json:"-" comment:"This is field comment"`
107+
Network string `json:"network" pg:",pk" comment:"This is field comment"`
108+
Hash string `json:"hash" pg:",pk" comment:"This is field comment"`
109+
Branch string `json:"branch" comment:"This is field comment"`
110+
Status string `json:"status" comment:"This is field comment"`
111+
Kind string `json:"kind" comment:"This is field comment"`
112+
Signature string `json:"signature" comment:"This is field comment"`
113+
Protocol string `json:"protocol" comment:"This is field comment"`
114+
Level uint64 `json:"level" comment:"This is field comment"`
115+
Errors interface{} `json:"errors,omitempty" pg:"type:jsonb" comment:"This is field comment"`
116+
ExpirationLevel *uint64 `json:"expiration_level" comment:"This is field comment"`
117+
Raw interface{} `json:"raw,omitempty" pg:"type:jsonb" comment:"This is field comment"`
118+
Ballot string `json:"ballot" comment:"This is field comment"`
119+
Period int64 `json:"period" comment:"This is field comment"`
120120
}
121121

122122
mockCtrl, mockSC, ctx := initMocks(t)
@@ -148,8 +148,8 @@ func TestMakeCommentsWithTableNameAndFieldsWithPgComment(t *testing.T) {
148148
func TestMakeCommentsWithMultipleModels(t *testing.T) {
149149
type Ballot struct {
150150
//nolint
151-
tableName struct{} `pg:"ballots" pg-comment:"This multiple table name comment"`
152-
Ballot string `json:"ballot" pg-comment:"This is multiple field comment"`
151+
tableName struct{} `pg:"ballots" comment:"This multiple table name comment"`
152+
Ballot string `json:"ballot" comment:"This is multiple field comment"`
153153
}
154154

155155
mockCtrl, mockSC, ctx := initMocks(t)
@@ -178,3 +178,37 @@ func TestMakeCommentsWithMultipleModels(t *testing.T) {
178178
// Assert
179179
assert.Empty(t, err)
180180
}
181+
182+
func TestMakeCommentsWithMultipleModelsByPointers(t *testing.T) {
183+
type Ballot struct {
184+
//nolint
185+
tableName struct{} `pg:"ballots" comment:"This multiple table name comment"`
186+
Ballot string `json:"ballot" comment:"This is multiple field comment"`
187+
}
188+
189+
mockCtrl, mockSC, ctx := initMocks(t)
190+
defer mockCtrl.Finish()
191+
192+
models := []interface{}{&Ballot{}, &Ballot{}, &Ballot{}}
193+
194+
// Assert prepare
195+
mockSC.
196+
EXPECT().
197+
MakeTableComment(ctx, "ballots", "This multiple table name comment").
198+
Times(3).
199+
Return(nil)
200+
201+
// Be aware: there is on issue with default order in checking
202+
// methods call: https://github.com/golang/mock/issues/653
203+
mockSC.
204+
EXPECT().
205+
MakeColumnComment(ctx, "ballots", "ballot", "This is multiple field comment").
206+
Times(3).
207+
Return(nil)
208+
209+
// Act
210+
err := MakeComments(ctx, mockSC, models...)
211+
212+
// Assert
213+
assert.Empty(t, err)
214+
}

hasura/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ func (api *API) DropSelectPermissions(ctx context.Context, table, source string,
220220
"source": source,
221221
},
222222
}
223+
223224
return api.post(ctx, "/v1/metadata", nil, req, nil)
224225
}
225226

hasura/error.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,8 @@ func (e APIError) Error() string {
3232
func (e APIError) AlreadyExists() bool {
3333
return e.Code == "already-exists"
3434
}
35+
36+
// PermissionDenied -
37+
func (e APIError) PermissionDenied() bool {
38+
return e.Code == "permission-denied"
39+
}

hasura/hasura.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ func Create(ctx context.Context, args GenerateArgs) error {
127127
return err
128128
}
129129
}
130-
if err := api.DropSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, "user"); err != nil {
131-
log.Warn().Err(err).Msg("")
130+
if err := api.DropSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, args.Config.UnauthorizedRole); err != nil {
131+
if e, ok := err.(APIError); !ok || !e.PermissionDenied() {
132+
log.Warn().Err(err).Msg("")
133+
}
132134
}
133-
if err := api.CreateSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, "user", Permission{
135+
if err := api.CreateSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, args.Config.UnauthorizedRole, Permission{
134136
Limit: args.Config.RowsLimit,
135137
AllowAggs: args.Config.EnableAggregations,
136138
Columns: Columns{"*"},
@@ -197,7 +199,7 @@ func generateOne(hasura config.Hasura, schema string, model interface{}) (table,
197199
t.HasuraSchema = newMetadataTable(t.Name, t.Schema)
198200
t.Columns = getColumns(typ)
199201

200-
t.HasuraSchema.SelectPermissions = append(t.HasuraSchema.SelectPermissions, formatSelectPermissions(hasura.RowsLimit, hasura.EnableAggregations, t.Columns...))
202+
t.HasuraSchema.SelectPermissions = append(t.HasuraSchema.SelectPermissions, formatSelectPermissions(hasura.RowsLimit, hasura.EnableAggregations, hasura.UnauthorizedRole, t.Columns...))
201203

202204
if err := getRelationships(&t.HasuraSchema, t.Name, typ); err != nil {
203205
return t, err
@@ -273,12 +275,12 @@ func getRelationships(t *Table, name string, typ reflect.Type) error {
273275
return nil
274276
}
275277

276-
func formatSelectPermissions(limit uint64, allowAggs bool, columns ...string) SelectPermission {
278+
func formatSelectPermissions(limit uint64, allowAggs bool, role string, columns ...string) SelectPermission {
277279
if limit == 0 {
278280
limit = 10
279281
}
280282
return SelectPermission{
281-
Role: "user",
283+
Role: role,
282284
Permission: Permission{
283285
Columns: columns,
284286
Filter: map[string]interface{}{},

hasura/hasura_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func TestGenerate(t *testing.T) {
147147
UsePreparedStatements: true,
148148
IsolationLevel: "read-committed",
149149
},
150+
UnauthorizedRole: "user",
150151
},
151152
models: []interface{}{
152153
&testTable{}, &testTable2{}, &testTable3{}, &testTable4{},

0 commit comments

Comments
 (0)