Skip to content

Commit

Permalink
Merge pull request #15 from dipdup-io/GO-84-fix-warning-with-dropping…
Browse files Browse the repository at this point in the history
…-permission-on-user-role
  • Loading branch information
vvuwei committed Jun 27, 2023
2 parents 32ef3f0 + 90a2d4c commit 8bc226d
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 33 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Hasura struct {
EnableAggregations bool `yaml:"allow_aggregation"`
Source *HasuraSource `yaml:"source"`
Rest *bool `yaml:"rest"`
UnauthorizedRole string `yaml:"unauthorized_role"`
}

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

h.UnauthorizedRole = "user"

type plain Hasura
return unmarshal((*plain)(h))
}
Expand Down
19 changes: 12 additions & 7 deletions database/pgComment.go → database/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}) error {
for _, model := range models {
modelType := reflect.TypeOf(model)

if reflect.ValueOf(model).Kind() == reflect.Ptr {
modelType = modelType.Elem()
}

var tableName string

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

pgCommentTag, ok := getPgComment(fieldType)
comment, ok := getComment(fieldType)
if !ok {
continue
}

if err := sc.MakeTableComment(ctx, tableName, pgCommentTag); err != nil {
if err := sc.MakeTableComment(ctx, tableName, comment); err != nil {
return err
}

continue
}

pgCommentTag, ok := getPgComment(fieldType)
comment, ok := getComment(fieldType)
if !ok {
continue
}
Expand All @@ -44,7 +49,7 @@ func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}
columnName = hasura.ToSnakeCase(fieldType.Name)
}

if err := sc.MakeColumnComment(ctx, tableName, columnName, pgCommentTag); err != nil {
if err := sc.MakeColumnComment(ctx, tableName, columnName, comment); err != nil {
return err
}
}
Expand All @@ -68,11 +73,11 @@ func getPgName(fieldType reflect.StructField) (name string, ok bool) {
return "", false
}

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

if ok {
return pgCommentTag, ok
return commentTag, ok
}

return "", false
Expand Down
74 changes: 54 additions & 20 deletions database/pgComment_test.go → database/comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func initMocks(t *testing.T) (*gomock.Controller, *mocks.MockSchemeCommenter, co
func TestMakeCommentsWithTableName(t *testing.T) {
type Ballot struct {
//nolint
tableName struct{} `pg:"ballots" pg-comment:"Ballot table"`
tableName struct{} `pg:"ballots" comment:"Ballot table"`
Ballot string `json:"ballot"`
}

Expand Down Expand Up @@ -76,7 +76,7 @@ func TestMakeCommentsFieldWithPgComment(t *testing.T) {
type Ballot struct {
//nolint
tableName struct{} `pg:"ballots"`
Ballot string `json:"ballot" pg-comment:"This is field comment"`
Ballot string `json:"ballot" comment:"This is field comment"`
}

mockCtrl, mockSC, ctx := initMocks(t)
Expand All @@ -101,22 +101,22 @@ func TestMakeCommentsFieldWithPgComment(t *testing.T) {
func TestMakeCommentsWithTableNameAndFieldsWithPgComment(t *testing.T) {
type Ballot struct {
//nolint
tableName struct{} `pg:"ballots" pg-comment:"Ballot table"`
CreatedAt int64 `json:"-" pg-comment:"This is field comment"`
UpdatedAt int64 `json:"-" pg-comment:"This is field comment"`
Network string `json:"network" pg:",pk" pg-comment:"This is field comment"`
Hash string `json:"hash" pg:",pk" pg-comment:"This is field comment"`
Branch string `json:"branch" pg-comment:"This is field comment"`
Status string `json:"status" pg-comment:"This is field comment"`
Kind string `json:"kind" pg-comment:"This is field comment"`
Signature string `json:"signature" pg-comment:"This is field comment"`
Protocol string `json:"protocol" pg-comment:"This is field comment"`
Level uint64 `json:"level" pg-comment:"This is field comment"`
Errors interface{} `json:"errors,omitempty" pg:"type:jsonb" pg-comment:"This is field comment"`
ExpirationLevel *uint64 `json:"expiration_level" pg-comment:"This is field comment"`
Raw interface{} `json:"raw,omitempty" pg:"type:jsonb" pg-comment:"This is field comment"`
Ballot string `json:"ballot" pg-comment:"This is field comment"`
Period int64 `json:"period" pg-comment:"This is field comment"`
tableName struct{} `pg:"ballots" comment:"Ballot table"`
CreatedAt int64 `json:"-" comment:"This is field comment"`
UpdatedAt int64 `json:"-" comment:"This is field comment"`
Network string `json:"network" pg:",pk" comment:"This is field comment"`
Hash string `json:"hash" pg:",pk" comment:"This is field comment"`
Branch string `json:"branch" comment:"This is field comment"`
Status string `json:"status" comment:"This is field comment"`
Kind string `json:"kind" comment:"This is field comment"`
Signature string `json:"signature" comment:"This is field comment"`
Protocol string `json:"protocol" comment:"This is field comment"`
Level uint64 `json:"level" comment:"This is field comment"`
Errors interface{} `json:"errors,omitempty" pg:"type:jsonb" comment:"This is field comment"`
ExpirationLevel *uint64 `json:"expiration_level" comment:"This is field comment"`
Raw interface{} `json:"raw,omitempty" pg:"type:jsonb" comment:"This is field comment"`
Ballot string `json:"ballot" comment:"This is field comment"`
Period int64 `json:"period" comment:"This is field comment"`
}

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

mockCtrl, mockSC, ctx := initMocks(t)
Expand Down Expand Up @@ -178,3 +178,37 @@ func TestMakeCommentsWithMultipleModels(t *testing.T) {
// Assert
assert.Empty(t, err)
}

func TestMakeCommentsWithMultipleModelsByPointers(t *testing.T) {
type Ballot struct {
//nolint
tableName struct{} `pg:"ballots" comment:"This multiple table name comment"`
Ballot string `json:"ballot" comment:"This is multiple field comment"`
}

mockCtrl, mockSC, ctx := initMocks(t)
defer mockCtrl.Finish()

models := []interface{}{&Ballot{}, &Ballot{}, &Ballot{}}

// Assert prepare
mockSC.
EXPECT().
MakeTableComment(ctx, "ballots", "This multiple table name comment").
Times(3).
Return(nil)

// Be aware: there is on issue with default order in checking
// methods call: https://github.com/golang/mock/issues/653
mockSC.
EXPECT().
MakeColumnComment(ctx, "ballots", "ballot", "This is multiple field comment").
Times(3).
Return(nil)

// Act
err := MakeComments(ctx, mockSC, models...)

// Assert
assert.Empty(t, err)
}
1 change: 1 addition & 0 deletions hasura/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func (api *API) DropSelectPermissions(ctx context.Context, table, source string,
"source": source,
},
}

return api.post(ctx, "/v1/metadata", nil, req, nil)
}

Expand Down
5 changes: 5 additions & 0 deletions hasura/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@ func (e APIError) Error() string {
func (e APIError) AlreadyExists() bool {
return e.Code == "already-exists"
}

// PermissionDenied -
func (e APIError) PermissionDenied() bool {
return e.Code == "permission-denied"
}
14 changes: 8 additions & 6 deletions hasura/hasura.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ func Create(ctx context.Context, args GenerateArgs) error {
return err
}
}
if err := api.DropSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, "user"); err != nil {
log.Warn().Err(err).Msg("")
if err := api.DropSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, args.Config.UnauthorizedRole); err != nil {
if e, ok := err.(APIError); !ok || !e.PermissionDenied() {
log.Warn().Err(err).Msg("")
}
}
if err := api.CreateSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, "user", Permission{
if err := api.CreateSelectPermissions(ctx, args.Views[i], args.Config.Source.Name, args.Config.UnauthorizedRole, Permission{
Limit: args.Config.RowsLimit,
AllowAggs: args.Config.EnableAggregations,
Columns: Columns{"*"},
Expand Down Expand Up @@ -197,7 +199,7 @@ func generateOne(hasura config.Hasura, schema string, model interface{}) (table,
t.HasuraSchema = newMetadataTable(t.Name, t.Schema)
t.Columns = getColumns(typ)

t.HasuraSchema.SelectPermissions = append(t.HasuraSchema.SelectPermissions, formatSelectPermissions(hasura.RowsLimit, hasura.EnableAggregations, t.Columns...))
t.HasuraSchema.SelectPermissions = append(t.HasuraSchema.SelectPermissions, formatSelectPermissions(hasura.RowsLimit, hasura.EnableAggregations, hasura.UnauthorizedRole, t.Columns...))

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

func formatSelectPermissions(limit uint64, allowAggs bool, columns ...string) SelectPermission {
func formatSelectPermissions(limit uint64, allowAggs bool, role string, columns ...string) SelectPermission {
if limit == 0 {
limit = 10
}
return SelectPermission{
Role: "user",
Role: role,
Permission: Permission{
Columns: columns,
Filter: map[string]interface{}{},
Expand Down
1 change: 1 addition & 0 deletions hasura/hasura_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ func TestGenerate(t *testing.T) {
UsePreparedStatements: true,
IsolationLevel: "read-committed",
},
UnauthorizedRole: "user",
},
models: []interface{}{
&testTable{}, &testTable2{}, &testTable3{}, &testTable4{},
Expand Down

0 comments on commit 8bc226d

Please sign in to comment.