Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Policy Files #231

Merged
merged 9 commits into from
Apr 5, 2024
6 changes: 3 additions & 3 deletions cmd/createrole.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ func createRole(ctx context.Context, cfg *config.AppConfig) {

var policy iapl.Policy

if cfg.SpiceDB.PolicyFile != "" {
policy, err = iapl.NewPolicyFromFile(cfg.SpiceDB.PolicyFile)
if len(cfg.SpiceDB.PolicyFiles) > 0 {
policy, err = iapl.NewPolicyFromFiles(cfg.SpiceDB.PolicyFiles)
if err != nil {
logger.Fatalw("unable to load new policy from schema file", "policy_file", cfg.SpiceDB.PolicyFile, "error", err)
logger.Fatalw("unable to load new policy from schema files", "policy_files", cfg.SpiceDB.PolicyFiles, "error", err)
}
} else {
logger.Warn("no spicedb policy file defined, using default policy")
Expand Down
8 changes: 4 additions & 4 deletions cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ func writeSchema(ctx context.Context, dryRun bool, cfg *config.AppConfig) {
policy iapl.Policy
)

if cfg.SpiceDB.PolicyFile != "" {
policy, err = iapl.NewPolicyFromFile(cfg.SpiceDB.PolicyFile)
if len(cfg.SpiceDB.PolicyFiles) > 0 {
policy, err = iapl.NewPolicyFromFiles(cfg.SpiceDB.PolicyFiles)
if err != nil {
logger.Fatalw("unable to load new policy from schema file", "policy_file", cfg.SpiceDB.PolicyFile, "error", err)
logger.Fatalw("unable to load new policy from schema files", "policy_files", cfg.SpiceDB.PolicyFiles, "error", err)
}
} else {
logger.Warn("no spicedb policy file defined, using default policy")
Expand All @@ -70,7 +70,7 @@ func writeSchema(ctx context.Context, dryRun bool, cfg *config.AppConfig) {
}

if viper.GetBool("mermaid") || viper.GetBool("mermaid-markdown") {
outputPolicyMermaid(cfg.SpiceDB.PolicyFile, viper.GetBool("mermaid-markdown"))
outputPolicyMermaid(cfg.SpiceDB.PolicyFiles, viper.GetBool("mermaid-markdown"))

return
}
Expand Down
25 changes: 15 additions & 10 deletions cmd/schema_mermaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,24 @@ type mermaidContext struct {
RelatedActions map[string]map[string][]string
}

func outputPolicyMermaid(filePath string, markdown bool) {
var policy iapl.PolicyDocument
func outputPolicyMermaid(filePaths []string, markdown bool) {
policy := iapl.PolicyDocument{}

if len(filePaths) > 0 {
for _, filePath := range filePaths {
file, err := os.Open(filePath)
if err != nil {
logger.Fatalw("failed to open policy document file", "error", err)
}
defer file.Close()

if filePath != "" {
file, err := os.Open(filePath)
if err != nil {
logger.Fatalw("failed to open policy document file", "error", err)
}
var filePolicy iapl.PolicyDocument

defer file.Close()
if err := yaml.NewDecoder(file).Decode(&filePolicy); err != nil {
logger.Fatalw("failed to open policy document file", "error", err)
}

if err := yaml.NewDecoder(file).Decode(&policy); err != nil {
logger.Fatalw("failed to load policy document file", "error", err)
policy = policy.MergeWithPolicyDocument(filePolicy)
}
} else {
policy = iapl.DefaultPolicyDocument()
Expand Down
6 changes: 3 additions & 3 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func serve(ctx context.Context, cfg *config.AppConfig) {

var policy iapl.Policy

if cfg.SpiceDB.PolicyFile != "" {
policy, err = iapl.NewPolicyFromFile(cfg.SpiceDB.PolicyFile)
if len(cfg.SpiceDB.PolicyFiles) > 0 {
policy, err = iapl.NewPolicyFromFiles(cfg.SpiceDB.PolicyFiles)
if err != nil {
logger.Fatalw("unable to load new policy from schema file", "policy_file", cfg.SpiceDB.PolicyFile, "error", err)
logger.Fatalw("unable to load new policy from schema files", "policy_files", cfg.SpiceDB.PolicyFiles, "error", err)
}
} else {
logger.Warn("no spicedb policy file defined, using default policy")
Expand Down
6 changes: 3 additions & 3 deletions cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ func worker(ctx context.Context, cfg *config.AppConfig) {

var policy iapl.Policy

if cfg.SpiceDB.PolicyFile != "" {
policy, err = iapl.NewPolicyFromFile(cfg.SpiceDB.PolicyFile)
if len(cfg.SpiceDB.PolicyFiles) > 0 {
policy, err = iapl.NewPolicyFromFiles(cfg.SpiceDB.PolicyFiles)
if err != nil {
logger.Fatalw("unable to load new policy from schema file", "policy_file", cfg.SpiceDB.PolicyFile, "error", err)
logger.Fatalw("unable to load new policy from schema files", "policy_files", cfg.SpiceDB.PolicyFiles, "error", err)
}
} else {
logger.Warn("no spicedb policy file defined, using default policy")
Expand Down
Loading
Loading