Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cmd/permify/permify.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func main() {
root.AddCommand(validate)

coverage := cmd.NewCoverageCommand()
flags.RegisterCoverageFlags(coverage)
root.AddCommand(coverage)

migrate := cmd.NewMigrateCommand()
Expand Down
11 changes: 0 additions & 11 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type (
Service `mapstructure:"service"` // Service configuration
Database `mapstructure:"database"` // Database configuration
Distributed `mapstructure:"distributed"` // Distributed configuration
Coverage `mapstructure:"coverage"` // Coverage configuration
}

// Server contains the configurations for both HTTP and gRPC servers.
Expand Down Expand Up @@ -157,12 +156,6 @@ type (
Enabled bool `mapstructure:"enabled"`
Nodes []string `mapstructure:"nodes"`
}

// Coverage contains configuration for coverage.
Coverage struct {
Relationships int `mapstructure:"relationships"` // Relationships trashhold
Assertions int `mapstructure:"assertions"` // Assertions trashhold
}
)

// NewConfig initializes and returns a new Config object by reading and unmarshalling
Expand Down Expand Up @@ -313,10 +306,6 @@ func DefaultConfig() *Config {
Enabled: false,
Nodes: []string{},
},
Coverage: Coverage{
Relationships: 0,
Assertions: 0,
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/coverage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewCoverageCommand() *cobra.Command {
}

// register flags for validation
flags.RegisterValidationFlags(command)
flags.RegisterCoverageFlags(command)

return command
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/cmd/flags/coverage.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package flags

import (
"github.com/Permify/permify/internal/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

// RegisterCoverageFlags registers coverage flags.
func RegisterCoverageFlags(cmd *cobra.Command) {
conf := config.DefaultConfig()
flags := cmd.Flags()

flags.Int("coverage-relationships", conf.Coverage.Relationships, "the min coverage for relationships")
flags.Int("coverage-relationships", 0, "the min coverage for relationships")
if err := viper.BindPFlag("coverage-relationships", flags.Lookup("coverage-relationships")); err != nil {
panic(err)
}

flags.Int("coverage-assertions", conf.Coverage.Assertions, "the min coverage for assertions")
flags.Int("coverage-assertions", 0, "the min coverage for assertions")
if err := viper.BindPFlag("coverage-assertions", flags.Lookup("coverage-assertions")); err != nil {
panic(err)
}
Expand Down
15 changes: 0 additions & 15 deletions pkg/cmd/flags/validation.go

This file was deleted.

110 changes: 24 additions & 86 deletions pkg/cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"net/url"
"os"
"sort"
"strings"

"github.com/rs/xid"
"github.com/spf13/viper"

"github.com/gookit/color"
"github.com/rs/xid"
"github.com/spf13/cobra"

"github.com/Permify/permify/internal/storage"
server_validation "github.com/Permify/permify/internal/validation"
"github.com/Permify/permify/pkg/cmd/flags"
"github.com/Permify/permify/pkg/database"
"github.com/Permify/permify/pkg/development"
"github.com/Permify/permify/pkg/development/file"
Expand All @@ -38,9 +34,6 @@ func NewValidateCommand() *cobra.Command {
Args: cobra.ExactArgs(1),
}

// register flags for validation
flags.RegisterValidationFlags(command)

return command
}

Expand Down Expand Up @@ -68,22 +61,6 @@ func (l *ErrList) Print() {
// validate returns a function that validates authorization model with assertions
func validate() func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
// set debug to false initially
debug := false

// get output format from viper
format := viper.GetString("output-format")

// if output format is not verbose or json, set it to verbose
if format != "verbose" && format != "json" {
format = "verbose"
}

// if output format is verbose, set debug to true
if format == "verbose" {
debug = true
}

// create an empty error list
list := &ErrList{
Errors: []string{},
Expand Down Expand Up @@ -117,9 +94,7 @@ func validate() func(cmd *cobra.Command, args []string) error {
}

// if debug is true, print schema is creating with color blue
if debug {
color.Notice.Println("schema is creating... 🚀")
}
color.Notice.Println("schema is creating... 🚀")

sch, err := parser.NewParser(s.Schema).Parse()
if err != nil {
Expand Down Expand Up @@ -147,24 +122,20 @@ func validate() func(cmd *cobra.Command, args []string) error {
err = dev.Container.SW.WriteSchema(ctx, cnf)
if err != nil {
list.Add(err.Error())
if debug {
color.Danger.Printf("fail: %s\n", validationError(err.Error()))
}
color.Danger.Printf("fail: %s\n", validationError(err.Error()))
if len(list.Errors) != 0 {
list.Print()
os.Exit(1)
}
}

// if there are no errors and debug is true, print success with color success
if len(list.Errors) == 0 && debug {
if len(list.Errors) == 0 {
color.Success.Println(" success")
}

// if debug is true, print relationships are creating with color blue
if debug {
color.Notice.Println("relationships are creating... 🚀")
}
color.Notice.Println("relationships are creating... 🚀")

// write relationships
for _, t := range s.Relationships {
Expand Down Expand Up @@ -194,21 +165,15 @@ func validate() func(cmd *cobra.Command, args []string) error {
}))
if err != nil {
list.Add(fmt.Sprintf("%s failed %s", t, err.Error()))
if debug {
color.Danger.Println(fmt.Sprintf("fail: %s failed %s", t, validationError(err.Error())))
}
color.Danger.Println(fmt.Sprintf("fail: %s failed %s", t, validationError(err.Error())))
continue
}

if debug {
color.Success.Println(fmt.Sprintf(" success: %s ", t))
}
color.Success.Println(fmt.Sprintf(" success: %s ", t))
}

// if debug is true, print checking assertions with color blue
if debug {
color.Notice.Println("checking scenarios... 🚀")
}
color.Notice.Println("checking scenarios... 🚀")

// Check Assertions
for sn, scenario := range s.Scenarios {
Expand Down Expand Up @@ -276,23 +241,15 @@ func validate() func(cmd *cobra.Command, args []string) error {
query := tuple.SubjectToString(subject) + " " + permission + " " + tuple.EntityToString(entity)

if res.Can == exp {
if debug {
color.Success.Print(" success:")
fmt.Printf(" %s \n", query)
}
color.Success.Print(" success:")
fmt.Printf(" %s \n", query)
} else {
if debug {
color.Danger.Printf(" fail: %s ->", query)
}
color.Danger.Printf(" fail: %s ->", query)
if res.Can == base.PermissionCheckResponse_RESULT_ALLOWED {
if debug {
color.Danger.Println(" expected: DENIED actual: ALLOWED ")
}
color.Danger.Println(" expected: DENIED actual: ALLOWED ")
list.Add(fmt.Sprintf("%s -> expected: DENIED actual: ALLOWED ", query))
} else {
if debug {
color.Danger.Println(" expected: ALLOWED actual: DENIED ")
}
color.Danger.Println(" expected: ALLOWED actual: DENIED ")
list.Add(fmt.Sprintf("%s -> expected: ALLOWED actual: DENIED ", query))
}
}
Expand Down Expand Up @@ -352,14 +309,10 @@ func validate() func(cmd *cobra.Command, args []string) error {
query := tuple.SubjectToString(subject) + " " + permission + " " + filter.EntityType

if isSameArray(res.GetEntityIds(), expected) {
if debug {
color.Success.Print(" success:")
fmt.Printf(" %v\n", query)
}
color.Success.Print(" success:")
fmt.Printf(" %v\n", query)
} else {
if debug {
color.Danger.Printf(" fail: %s -> expected: %+v actual: %+v\n", query, expected, res.GetEntityIds())
}
color.Danger.Printf(" fail: %s -> expected: %+v actual: %+v\n", query, expected, res.GetEntityIds())
list.Add(fmt.Sprintf("%s -> expected: %+v actual: %+v", query, expected, res.GetEntityIds()))
}
}
Expand Down Expand Up @@ -414,40 +367,25 @@ func validate() func(cmd *cobra.Command, args []string) error {
query := tuple.EntityToString(entity) + " " + permission + " " + filter.SubjectReference

if isSameArray(res.GetSubjectIds(), expected) {
if debug {
color.Success.Print(" success:")
fmt.Printf(" %v\n", query)
}
color.Success.Print(" success:")
fmt.Printf(" %v\n", query)
} else {
if debug {
color.Danger.Printf(" fail: %s -> expected: %+v actual: %+v\n", query, expected, res.GetSubjectIds())
}
color.Danger.Printf(" fail: %s -> expected: %+v actual: %+v\n", query, expected, res.GetSubjectIds())
list.Add(fmt.Sprintf("%s -> expected: %+v actual: %+v", query, expected, res.GetSubjectIds()))
}
}
}
}

if len(list.Errors) != 0 {
if debug {
list.Print()
os.Exit(1)
}
var b []byte
b, err = json.Marshal(list.Errors)
if err != nil {
return err
}
fmt.Println(string(b))
list.Print()
os.Exit(1)
}

if debug {
color.Notice.Println("schema successfully created")
color.Notice.Println("relationships successfully created")
color.Notice.Println("assertions successfully passed")
color.Success.Println("SUCCESS")
}
color.Notice.Println("schema successfully created")
color.Notice.Println("relationships successfully created")
color.Notice.Println("assertions successfully passed")
color.Success.Println("SUCCESS")

return nil
}
Expand Down