From 5bda6c5d571311f7bb7c3106b047103fd19f1787 Mon Sep 17 00:00:00 2001 From: Janpreet Singh Date: Thu, 1 Aug 2024 21:00:03 -0400 Subject: [PATCH] Refactored code --- LandingZone/Empty.txt | 2 +- cluster.kd | 4 +- main.go | 301 +++++++++++++++++------------------ packages/config/config.go | 105 ++++++------ packages/helper/processer.go | 117 ++++++++++++++ packages/opa/opa.go | 66 +++++--- relay.kd | 2 +- 7 files changed, 363 insertions(+), 234 deletions(-) create mode 100644 packages/helper/processer.go diff --git a/LandingZone/Empty.txt b/LandingZone/Empty.txt index b1baae3..b766412 100644 --- a/LandingZone/Empty.txt +++ b/LandingZone/Empty.txt @@ -1 +1 @@ -# Adding empty folder \ No newline at end of file +//empty \ No newline at end of file diff --git a/cluster.kd b/cluster.kd index ed83933..1a5fa6e 100644 --- a/cluster.kd +++ b/cluster.kd @@ -11,7 +11,7 @@ bead "ansible" { bead "terraform" { source = "git@github.com:janpreet/proxmox_terraform.git" - enabled = false + enabled = true relay = opa relay_field = "source=git@github.com:janpreet/proxmox_terraform.git,path=terraform/policies/proxmox.rego,input=terraform/plan.json,package=data.terraform.allow" -} +} \ No newline at end of file diff --git a/main.go b/main.go index 517bff8..4fb77e6 100644 --- a/main.go +++ b/main.go @@ -12,10 +12,7 @@ import ( "github.com/janpreet/kado/packages/display" "github.com/janpreet/kado/packages/engine" "github.com/janpreet/kado/packages/helper" - "github.com/janpreet/kado/packages/opa" "github.com/janpreet/kado/packages/render" - "github.com/janpreet/kado/packages/terraform" - "github.com/janpreet/kado/packages/terragrunt" ) func convertYAMLToSlice(yamlData map[string]interface{}) []map[string]interface{} { @@ -38,141 +35,92 @@ func applyRelayOverrides(b *bead.Bead) map[string]string { } func processBead(b bead.Bead, yamlData map[string]interface{}, beadMap map[string]bead.Bead, processed map[string]int, processedBeads *[]string, applyPlan bool, originBead string, relayToOPA bool) error { - if count, ok := processed[b.Name]; ok && count > 0 && originBead == "" { + config.DebugPrint("DEBUG: processBead called for %s (Enabled: %v, Origin: %s)\n", b.Name, *b.Enabled, originBead) + + if b.Enabled != nil && !*b.Enabled { + config.DebugPrint("DEBUG: Skipping disabled bead: %s\n", b.Name) return nil - } - - fmt.Printf("Processing bead: %s\n", b.Name) - - if originBead != "" { - repoPath := filepath.Join(config.LandingZone, b.Name) - if helper.FileExists(repoPath) { - fmt.Printf("Removing existing repository at: %s\n", repoPath) - err := os.RemoveAll(repoPath) - if err != nil { - return fmt.Errorf("failed to remove existing repository for bead %s: %v", b.Name, err) - } - } - } - - if source, ok := b.Fields["source"]; ok && source != "" { - refs := "" - if refsVal, ok := b.Fields["refs"]; ok { - refs = refsVal - } - err := helper.CloneRepo(source, config.LandingZone, b.Name, refs) - if err != nil { - return fmt.Errorf("failed to clone repo for bead %s: %v", b.Name, err) - } - } - - display.DisplayBead(b) - - if b.Name == "ansible" { - fmt.Println("Processing Ansible templates...") - - templatePaths, ok := yamlData["kado"].(map[string]interface{})["templates"].([]interface{}) - if !ok { - return fmt.Errorf("no templates defined for Ansible in the YAML configuration") - } - - err := render.ProcessTemplates(convertTemplatePaths(templatePaths), yamlData) - if err != nil { - return fmt.Errorf("failed to process Ansible templates: %v", err) - } - - if relayToOPA { - fmt.Println("Ansible bead is relayed to OPA for evaluation.") - } - - if playbook, ok := b.Fields["playbook"]; ok && playbook != "" { - playbookPath := filepath.Join(config.LandingZone, b.Name, playbook) - inventoryPath := b.Fields["inventory"] - if inventoryPath == "" { - inventoryPath = filepath.Join(config.LandingZone, "inventory.ini") - } - extraVarsFile := false - if extraVarsFileFlag, ok := b.Fields["extra_vars_file"]; ok && extraVarsFileFlag == "true" { - extraVarsFile = true - } - fmt.Printf("Running Ansible playbook: %s with inventory: %s\n", playbookPath, inventoryPath) - if !helper.FileExists(playbookPath) { - return fmt.Errorf("playbook file does not exist: %s", playbookPath) - } - if !relayToOPA || (relayToOPA && applyPlan) { - err := engine.HandleAnsible(b, convertYAMLToSlice(yamlData), extraVarsFile) - if err != nil { - return fmt.Errorf("failed to run Ansible: %v", err) - } - } else { - fmt.Println("Skipping Ansible playbook apply due to OPA evaluation or missing 'set' flag.") - } - } - } - - if b.Name == "terraform" { - fmt.Println("Processing Terraform templates...") - - templatePaths, ok := yamlData["kado"].(map[string]interface{})["templates"].([]interface{}) - if !ok { - return fmt.Errorf("no templates defined for Terraform in the YAML configuration") - } - - err := render.ProcessTemplates(convertTemplatePaths(templatePaths), yamlData) - if err != nil { - return fmt.Errorf("failed to process Terraform templates: %v", err) - } - - fmt.Println("Running Terraform plan...") - err = terraform.HandleTerraform(b, config.LandingZone, applyPlan) - if err != nil { - return fmt.Errorf("failed to run Terraform: %v", err) - } - } - - if b.Name == "opa" { - fmt.Println("Processing OPA validation...") - err := opa.HandleOPA(b, config.LandingZone, applyPlan, originBead) - if err != nil { - return fmt.Errorf("failed to process OPA: %v", err) - } - } - - if b.Name == "terragrun" { - fmt.Println("Processing Terragrunt templates...") - - templatePaths, ok := yamlData["kado"].(map[string]interface{})["templates"].([]interface{}) - if !ok { - return fmt.Errorf("no templates defined for Terragrunt in the YAML configuration") - } - - err := render.ProcessTemplates(convertTemplatePaths(templatePaths), yamlData) - if err != nil { - return fmt.Errorf("failed to process Terragrunt templates: %v", err) - } - - fmt.Println("Running Terragrunt plan...") - err = terragrunt.HandleTerragrunt(b, config.LandingZone, applyPlan) - if err != nil { - return fmt.Errorf("failed to run Terragrunt: %v", err) - } - } - - *processedBeads = append(*processedBeads, b.Name) - processed[b.Name]++ - - if relay, ok := b.Fields["relay"]; ok { - if relayBead, ok := beadMap[relay]; ok { - - overrides := applyRelayOverrides(&b) - for key, value := range overrides { - relayBead.Fields[key] = value - } - return processBead(relayBead, yamlData, beadMap, processed, processedBeads, applyPlan, b.Name, b.Name == "opa") - } - } - - return nil + } + + if count, ok := processed[b.Name]; ok && count > 0 && originBead == "" { + config.DebugPrint("DEBUG: Skipping already processed bead: %s\n", b.Name) + return nil + } + + config.DebugPrint("DEBUG: Actually processing bead: %s\n", b.Name) + + if originBead != "" { + repoPath := filepath.Join(config.LandingZone, b.Name) + if helper.FileExists(repoPath) { + fmt.Printf("Removing existing repository at: %s\n", repoPath) + err := os.RemoveAll(repoPath) + if err != nil { + return fmt.Errorf("failed to remove existing repository for bead %s: %v", b.Name, err) + } + } + } + + if source, ok := b.Fields["source"]; ok && source != "" { + refs := "" + if refsVal, ok := b.Fields["refs"]; ok { + refs = refsVal + } + err := helper.CloneRepo(source, config.LandingZone, b.Name, refs) + if err != nil { + return fmt.Errorf("failed to clone repo for bead %s: %v", b.Name, err) + } + } + + display.DisplayBead(b) + + switch b.Name { + case "ansible": + err := helper.ProcessAnsibleBead(b, yamlData, relayToOPA, applyPlan) + if err != nil { + return err + } + case "terraform": + err := helper.ProcessTerraformBead(b, yamlData, applyPlan) + if err != nil { + return err + } + case "opa": + err := helper.ProcessOPABead(b, applyPlan, originBead) + if err != nil { + return err + } + case "terragrunt": + err := helper.ProcessTerragruntBead(b, yamlData, applyPlan) + if err != nil { + return err + } + default: + return fmt.Errorf("unknown bead type: %s", b.Name) + } + + processed[b.Name]++ + *processedBeads = append(*processedBeads, b.Name) + config.DebugPrint("DEBUG: Added %s to processedBeads\n", b.Name) + + if relay, ok := b.Fields["relay"]; ok { + config.DebugPrint("DEBUG: Relay found for %s to %s\n", b.Name, relay) + if relayBead, ok := beadMap[relay]; ok { + if relayBead.Enabled != nil && !*relayBead.Enabled { + config.DebugPrint("DEBUG: Skipping disabled relay bead: %s\n", relayBead.Name) + return nil + } + overrides := applyRelayOverrides(&b) + for key, value := range overrides { + relayBead.Fields[key] = value + } + config.DebugPrint("DEBUG: Calling processBead for relay %s\n", relayBead.Name) + return processBead(relayBead, yamlData, beadMap, processed, processedBeads, applyPlan, b.Name, b.Name == "opa") + } else { + config.DebugPrint("DEBUG: Relay bead %s not found in beadMap\n", relay) + } + } + + return nil } func convertTemplatePaths(paths []interface{}) []string { @@ -243,13 +191,45 @@ func main() { log.Fatalf("Failed to get KD files: %v", err) } - var beads []bead.Bead - for _, kdFile := range kdFiles { + beadMap := make(map[string]bead.Bead) + var primaryKdFile string + + for i, kdFile := range kdFiles { + config.DebugPrint("DEBUG: Loading file: %s\n", kdFile) bs, err := config.LoadBeadsConfig(kdFile) if err != nil { log.Fatalf("Failed to load beads config from %s: %v", kdFile, err) } - beads = append(beads, bs...) + + if i == 0 { + primaryKdFile = kdFile + } + + for _, b := range bs { + if _, ok := beadMap[b.Name]; ok { + if kdFile != primaryKdFile { + fmt.Printf("WARNING: Ignoring conflicting configuration for bead %s in file %s. Using configuration from %s\n", b.Name, kdFile, primaryKdFile) + } else { + beadMap[b.Name] = b + config.DebugPrint("DEBUG: Updated bead %s (Enabled: %v) from primary file %s\n", b.Name, *b.Enabled, kdFile) + } + } else { + beadMap[b.Name] = b + config.DebugPrint("DEBUG: Loaded new bead %s (Enabled: %v) from file %s\n", b.Name, *b.Enabled, kdFile) + } + } + } + + var allBeads []bead.Bead + for _, b := range beadMap { + allBeads = append(allBeads, b) + } + + validBeads, invalidBeadReasons := config.GetValidBeadsWithDefaultEnabled(allBeads) + + config.DebugPrint("DEBUG: Final bead configurations:") + for _, b := range validBeads { + fmt.Printf(" - %s (Enabled: %v)\n", b.Name, *b.Enabled) } yamlData, err := config.LoadYAMLConfig(yamlFilePath) @@ -265,38 +245,43 @@ func main() { var invalidBeadNames []string var processedBeads []string - validBeads, invalidBeadReasons := config.GetValidBeadsWithDefaultEnabled(beads) - - beadMap := make(map[string]bead.Bead) + config.DebugPrint("DEBUG: Final bead configurations:") for _, b := range validBeads { - beadMap[b.Name] = b + fmt.Printf(" - %s (Enabled: %v)\n", b.Name, *b.Enabled) } + config.DebugPrint("DEBUG: Valid beads:") + for _, b := range validBeads { + fmt.Printf(" - %s (Enabled: %v)\n", b.Name, *b.Enabled) + } + processed := make(map[string]int) - + for _, b := range validBeads { + config.DebugPrint("DEBUG: Main loop processing bead %s (Enabled: %v)\n", b.Name, *b.Enabled) + if b.Enabled != nil && !*b.Enabled { + config.DebugPrint("DEBUG: Skipping disabled bead in main loop: %s\n", b.Name) + continue + } if err := processBead(b, yamlData, beadMap, processed, &processedBeads, applyPlan, "", false); err != nil { log.Fatalf("Failed to process bead %s: %v", b.Name, err) } } for beadIndex, reason := range invalidBeadReasons { - beadName := fmt.Sprintf("bead_%d", beadIndex) + beadName := fmt.Sprintf("bead_%s", beadIndex) fmt.Printf("Skipping bead: %s, Reason: %s\n", beadName, reason) invalidBeadNames = append(invalidBeadNames, fmt.Sprintf("%s: %s", beadName, reason)) - } + } - if len(processedBeads) > 0 { - fmt.Println("\nProcessed beads:") - for _, name := range processedBeads { - fmt.Printf(" - %s\n", name) - } + fmt.Println("\nDEBUG: Processed beads:") + for _, name := range processedBeads { + fmt.Printf(" - %s\n", name) } - - if len(invalidBeadNames) > 0 { - fmt.Println("\nSkipped beads:") - for _, name := range invalidBeadNames { - fmt.Printf(" - %s\n", name) - } + + fmt.Println("\nDEBUG: Skipped beads:") + for name, reason := range invalidBeadReasons { + fmt.Printf(" - %s: %s\n", name, reason) } + } diff --git a/packages/config/config.go b/packages/config/config.go index 56016a6..3c62072 100644 --- a/packages/config/config.go +++ b/packages/config/config.go @@ -13,6 +13,7 @@ type YAMLConfig map[string]interface{} var LandingZone = "LandingZone" var TemplateDir = "templates" +var Debug bool = false const Version = "1.0.0" @@ -27,41 +28,47 @@ func LoadBeadsConfig(filename string) ([]bead.Bead, error) { scanner := bufio.NewScanner(file) var currentBead *bead.Bead for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) - if line == "" || strings.HasPrefix(line, "#") { - continue - } - if strings.HasPrefix(line, "bead \"") { - if currentBead != nil { - beads = append(beads, *currentBead) - } - currentBead = &bead.Bead{ - Fields: make(map[string]string), - } - currentBead.Name = strings.Trim(line[6:], "\" {") - } else if currentBead != nil { - parts := strings.SplitN(line, "=", 2) - if len(parts) != 2 { - continue - } - key := strings.TrimSpace(parts[0]) - value := strings.Trim(strings.TrimSpace(parts[1]), "\"") - if key == "enabled" { - enabled := value == "true" + line := strings.TrimSpace(scanner.Text()) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + if strings.HasPrefix(line, "bead \"") { + if currentBead != nil { + beads = append(beads, *currentBead) + } + currentBead = &bead.Bead{ + Fields: make(map[string]string), + } + currentBead.Name = strings.Trim(line[6:], "\" {") + DebugPrint("DEBUG: Loading bead: %s\n", currentBead.Name) + } else if currentBead != nil { + parts := strings.SplitN(line, "=", 2) + if len(parts) != 2 { + continue + } + key := strings.TrimSpace(parts[0]) + value := strings.Trim(strings.TrimSpace(parts[1]), "\"") + if strings.ToLower(key) == "enabled" { + enabled := strings.ToLower(value) == "true" currentBead.Enabled = &enabled + DebugPrint("DEBUG: Set %s.Enabled = %v\n", currentBead.Name, *currentBead.Enabled) } else { currentBead.Fields[key] = value } - } - } - if currentBead != nil { - beads = append(beads, *currentBead) - } + } + } + if currentBead != nil { + beads = append(beads, *currentBead) + } if err := scanner.Err(); err != nil { return nil, err } - return beads, nil + for i, b := range beads { + DebugPrint("DEBUG: Loaded bead %s (index: %d) with enabled = %v\n", b.Name, i, b.Enabled) + } + + return beads, nil } func LoadYAMLConfig(filename string) (map[string]interface{}, error) { @@ -83,24 +90,30 @@ func GetValidBeadNames() map[string]struct{} { return GetValidBeads() } -func GetValidBeadsWithDefaultEnabled(beads []bead.Bead) ([]bead.Bead, []string) { - var validBeads []bead.Bead - var invalidBeadReasons []string - - for _, b := range beads { - if _, ok := ValidBeadNames[b.Name]; !ok { - invalidBeadReasons = append(invalidBeadReasons, fmt.Sprintf("%s (invalid name)", b.Name)) - continue - } - - if b.Enabled == nil { - validBeads = append(validBeads, b) - } else if !*b.Enabled { - invalidBeadReasons = append(invalidBeadReasons, fmt.Sprintf("%s (disabled)", b.Name)) - } else { - validBeads = append(validBeads, b) - } - } +func GetValidBeadsWithDefaultEnabled(beads []bead.Bead) ([]bead.Bead, map[string]string) { + var validBeads []bead.Bead + invalidBeadReasons := make(map[string]string) - return validBeads, invalidBeadReasons + for _, b := range beads { + if b.Enabled == nil { + defaultEnabled := true + b.Enabled = &defaultEnabled + } + + DebugPrint("DEBUG: Validating bead %s (Enabled: %v)\n", b.Name, *b.Enabled) + + if *b.Enabled { + validBeads = append(validBeads, b) + } else { + invalidBeadReasons[b.Name] = fmt.Sprintf("%s (disabled)", b.Name) + } + } + + return validBeads, invalidBeadReasons } + +func DebugPrint(format string, a ...interface{}) { + if Debug { + fmt.Printf(format, a...) + } +} \ No newline at end of file diff --git a/packages/helper/processer.go b/packages/helper/processer.go new file mode 100644 index 0000000..78ef899 --- /dev/null +++ b/packages/helper/processer.go @@ -0,0 +1,117 @@ +// File: packages/helper/processor.go + +package helper + +import ( + "fmt" + "path/filepath" + + "github.com/janpreet/kado/packages/bead" + "github.com/janpreet/kado/packages/config" + "github.com/janpreet/kado/packages/engine" + "github.com/janpreet/kado/packages/opa" + "github.com/janpreet/kado/packages/render" + "github.com/janpreet/kado/packages/terraform" + "github.com/janpreet/kado/packages/terragrunt" +) + +func ProcessAnsibleBead(b bead.Bead, yamlData map[string]interface{}, relayToOPA bool, applyPlan bool) error { + fmt.Println("Processing Ansible templates...") + templatePaths, ok := yamlData["kado"].(map[string]interface{})["templates"].([]interface{}) + if !ok { + return fmt.Errorf("no templates defined for Ansible in the YAML configuration") + } + err := render.ProcessTemplates(convertTemplatePaths(templatePaths), yamlData) + if err != nil { + return fmt.Errorf("failed to process Ansible templates: %v", err) + } + if relayToOPA { + fmt.Println("Ansible bead is relayed to OPA for evaluation.") + } + if playbook, ok := b.Fields["playbook"]; ok && playbook != "" { + playbookPath := filepath.Join(config.LandingZone, b.Name, playbook) + inventoryPath := b.Fields["inventory"] + if inventoryPath == "" { + inventoryPath = filepath.Join(config.LandingZone, "inventory.ini") + } + extraVarsFile := false + if extraVarsFileFlag, ok := b.Fields["extra_vars_file"]; ok && extraVarsFileFlag == "true" { + extraVarsFile = true + } + fmt.Printf("Running Ansible playbook: %s with inventory: %s\n", playbookPath, inventoryPath) + if !FileExists(playbookPath) { + return fmt.Errorf("playbook file does not exist: %s", playbookPath) + } + if !relayToOPA || (relayToOPA && applyPlan) { + err := engine.HandleAnsible(b, convertYAMLToSlice(yamlData), extraVarsFile) + if err != nil { + return fmt.Errorf("failed to run Ansible: %v", err) + } + } else { + fmt.Println("Skipping Ansible playbook apply due to OPA evaluation or missing 'set' flag.") + } + } + return nil +} + +func ProcessTerraformBead(b bead.Bead, yamlData map[string]interface{}, applyPlan bool) error { + fmt.Println("Processing Terraform templates...") + templatePaths, ok := yamlData["kado"].(map[string]interface{})["templates"].([]interface{}) + if !ok { + return fmt.Errorf("no templates defined for Terraform in the YAML configuration") + } + err := render.ProcessTemplates(convertTemplatePaths(templatePaths), yamlData) + if err != nil { + return fmt.Errorf("failed to process Terraform templates: %v", err) + } + fmt.Println("Running Terraform plan...") + err = terraform.HandleTerraform(b, config.LandingZone, applyPlan) + if err != nil { + return fmt.Errorf("failed to run Terraform: %v", err) + } + return nil +} + +func ProcessOPABead(b bead.Bead, applyPlan bool, originBead string) error { + fmt.Println("Processing OPA validation...") + fmt.Printf("DEBUG: Calling HandleOPA with originBead: %s\n", originBead) + err := opa.HandleOPA(b, config.LandingZone, applyPlan, originBead) + if err != nil { + return fmt.Errorf("failed to process OPA: %v", err) + } + return nil +} + +func ProcessTerragruntBead(b bead.Bead, yamlData map[string]interface{}, applyPlan bool) error { + fmt.Println("Processing Terragrunt templates...") + templatePaths, ok := yamlData["kado"].(map[string]interface{})["templates"].([]interface{}) + if !ok { + return fmt.Errorf("no templates defined for Terragrunt in the YAML configuration") + } + err := render.ProcessTemplates(convertTemplatePaths(templatePaths), yamlData) + if err != nil { + return fmt.Errorf("failed to process Terragrunt templates: %v", err) + } + fmt.Println("Running Terragrunt plan...") + err = terragrunt.HandleTerragrunt(b, config.LandingZone, applyPlan) + if err != nil { + return fmt.Errorf("failed to run Terragrunt: %v", err) + } + return nil +} + +// Helper functions + +func convertTemplatePaths(paths []interface{}) []string { + var result []string + for _, path := range paths { + if strPath, ok := path.(string); ok { + result = append(result, strPath) + } + } + return result +} + +func convertYAMLToSlice(yamlData map[string]interface{}) []map[string]interface{} { + return []map[string]interface{}{yamlData} +} \ No newline at end of file diff --git a/packages/opa/opa.go b/packages/opa/opa.go index e32ecbc..c1e89e8 100644 --- a/packages/opa/opa.go +++ b/packages/opa/opa.go @@ -6,7 +6,7 @@ import ( "fmt" "os" "path/filepath" - + "strings" "github.com/janpreet/kado/packages/bead" "github.com/janpreet/kado/packages/engine" "github.com/janpreet/kado/packages/terraform" @@ -15,21 +15,28 @@ import ( ) func HandleOPA(b bead.Bead, landingZone string, applyPlan bool, originBead string) error { - fmt.Printf("Processing OPA bead:\n") - for key, val := range b.Fields { - fmt.Printf(" %s = %s\n", key, val) - } - - inputPath, ok := b.Fields["input"] - if !ok { - return fmt.Errorf("input path not specified in bead") - } - fullInputPath := filepath.Join(landingZone, inputPath) - fmt.Printf("Reading input file from path: %s\n", fullInputPath) - inputData, err := os.ReadFile(fullInputPath) - if err != nil { - return fmt.Errorf("failed to read input file: %v", err) - } + fmt.Printf("Processing OPA bead (Origin: %s):\n", originBead) + for key, val := range b.Fields { + fmt.Printf(" %s = %s\n", key, val) + } + + inputPath, ok := b.Fields["input"] + if !ok { + return fmt.Errorf("input path not specified in bead") + } + + fullInputPath := filepath.Join(landingZone, inputPath) + if originBead != "" { + + inputPath = strings.TrimPrefix(inputPath, originBead+"/") + fullInputPath = filepath.Join(landingZone, originBead, inputPath) + } + + fmt.Printf("Reading input file from path: %s\n", fullInputPath) + inputData, err := os.ReadFile(fullInputPath) + if err != nil { + return fmt.Errorf("failed to read input file: %v", err) + } var input interface{} if filepath.Ext(fullInputPath) == ".yaml" || filepath.Ext(fullInputPath) == ".yml" { @@ -42,16 +49,23 @@ func HandleOPA(b bead.Bead, landingZone string, applyPlan bool, originBead strin } } - policyPath, ok := b.Fields["path"] - if !ok { - return fmt.Errorf("policy path not specified in bead") - } - fullPolicyPath := filepath.Join(landingZone, policyPath) - fmt.Printf("Reading policy file from path: %s\n", fullPolicyPath) - policyData, err := os.ReadFile(fullPolicyPath) - if err != nil { - return fmt.Errorf("failed to read policy file: %v", err) - } + policyPath, ok := b.Fields["path"] + if !ok { + return fmt.Errorf("policy path not specified in bead") + } + + fullPolicyPath := filepath.Join(landingZone, policyPath) + if originBead != "" { + + policyPath = strings.TrimPrefix(policyPath, originBead+"/") + fullPolicyPath = filepath.Join(landingZone, originBead, policyPath) + } + + fmt.Printf("Reading policy file from path: %s\n", fullPolicyPath) + policyData, err := os.ReadFile(fullPolicyPath) + if err != nil { + return fmt.Errorf("failed to read policy file: %v", err) + } packageQuery := "data.terraform.allow" if pkg, ok := b.Fields["package"]; ok { diff --git a/relay.kd b/relay.kd index 359777e..9cfcaff 100644 --- a/relay.kd +++ b/relay.kd @@ -1,3 +1,3 @@ bead "opa" { - enabled = false + enabled = true }