|
| 1 | +// Copyright 2025 FairwindsOps Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package cli |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "path/filepath" |
| 21 | + "strings" |
| 22 | + |
| 23 | + "github.com/sirupsen/logrus" |
| 24 | + "github.com/spf13/cobra" |
| 25 | + "github.com/xlab/treeprint" |
| 26 | + |
| 27 | + "github.com/fairwindsops/insights-cli/pkg/kyverno" |
| 28 | +) |
| 29 | + |
| 30 | +var listLocal bool |
| 31 | +var listClusterName string |
| 32 | +var listFormat string |
| 33 | + |
| 34 | +func init() { |
| 35 | + listKyvernoPoliciesCmd.Flags().BoolVar(&listLocal, "local", false, "List local policy files") |
| 36 | + listKyvernoPoliciesCmd.Flags().StringVar(&listClusterName, "cluster", "", "List policies for specific cluster from Insights") |
| 37 | + listKyvernoPoliciesCmd.Flags().StringVar(&listFormat, "format", "tree", "Output format: tree, yaml") |
| 38 | + listCmd.AddCommand(listKyvernoPoliciesCmd) |
| 39 | +} |
| 40 | + |
| 41 | +var listKyvernoPoliciesCmd = &cobra.Command{ |
| 42 | + Use: "kyverno-policies", |
| 43 | + Short: "List Kyverno policies.", |
| 44 | + Long: "List Kyverno policies from local files or Insights. Use --local for local files, --cluster for cluster-specific policies.", |
| 45 | + Example: ` |
| 46 | + # List all policies from Insights |
| 47 | + insights-cli list kyverno-policies |
| 48 | +
|
| 49 | + # List local policy files |
| 50 | + insights-cli list kyverno-policies --local |
| 51 | +
|
| 52 | + # List policies for specific cluster |
| 53 | + insights-cli list kyverno-policies --cluster production |
| 54 | +
|
| 55 | + # Export cluster policies as YAML |
| 56 | + insights-cli list kyverno-policies --cluster production --format yaml`, |
| 57 | + PreRun: func(cmd *cobra.Command, args []string) { |
| 58 | + // Only require API config if not listing local files |
| 59 | + if !listLocal { |
| 60 | + validateAndLoadInsightsAPIConfigWrapper(cmd, args) |
| 61 | + } |
| 62 | + }, |
| 63 | + Run: func(cmd *cobra.Command, args []string) { |
| 64 | + if listLocal { |
| 65 | + // Local file system listing |
| 66 | + if _, err := os.Stat("kyverno-policies"); os.IsNotExist(err) { |
| 67 | + logrus.Fatalf("Directory kyverno-policies does not exist") |
| 68 | + } |
| 69 | + tree := treeprint.New() |
| 70 | + err := addLocalKyvernoPoliciesBranch("kyverno-policies", tree) |
| 71 | + if err != nil { |
| 72 | + logrus.Fatalf("Unable to list local policies: %v", err) |
| 73 | + } |
| 74 | + fmt.Println(tree.String()) |
| 75 | + } else { |
| 76 | + // API listing |
| 77 | + org := configurationObject.Options.Organization |
| 78 | + |
| 79 | + if listClusterName != "" { |
| 80 | + // Cluster-specific listing |
| 81 | + if listFormat == "yaml" { |
| 82 | + // Export as YAML |
| 83 | + yamlContent, err := kyverno.ExportClusterKyvernoPoliciesYaml(client, org, listClusterName) |
| 84 | + if err != nil { |
| 85 | + logrus.Fatalf("Unable to export cluster policies: %v", err) |
| 86 | + } |
| 87 | + fmt.Print(yamlContent) |
| 88 | + } else { |
| 89 | + // List as tree (with app groups applied by default) |
| 90 | + tree := treeprint.New() |
| 91 | + err := kyverno.AddClusterKyvernoPoliciesWithAppGroupsBranch(client, org, listClusterName, tree) |
| 92 | + if err != nil { |
| 93 | + logrus.Fatalf("Unable to get cluster policies: %v", err) |
| 94 | + } |
| 95 | + fmt.Println(tree.String()) |
| 96 | + } |
| 97 | + } else { |
| 98 | + // General API listing (existing functionality) |
| 99 | + tree := treeprint.New() |
| 100 | + err := kyverno.AddKyvernoPoliciesBranch(client, org, tree) |
| 101 | + if err != nil { |
| 102 | + logrus.Fatalf("Unable to get Kyverno policies from insights: %v", err) |
| 103 | + } |
| 104 | + fmt.Println(tree.String()) |
| 105 | + } |
| 106 | + } |
| 107 | + }, |
| 108 | +} |
| 109 | + |
| 110 | +// addLocalKyvernoPoliciesBranch builds a tree for local Kyverno policy files |
| 111 | +func addLocalKyvernoPoliciesBranch(dir string, tree treeprint.Tree) error { |
| 112 | + policiesBranch := tree.AddBranch("kyverno-policies (local)") |
| 113 | + |
| 114 | + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + if info.IsDir() { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + filename := filepath.Base(path) |
| 124 | + |
| 125 | + // Only process policy files, exclude test case files |
| 126 | + if isPolicyFile(filename) && !isTestCaseFile(filename) { |
| 127 | + policyName := extractPolicyNameFromFile(filename) |
| 128 | + policyNode := policiesBranch.AddBranch(policyName) |
| 129 | + policyNode.AddNode(fmt.Sprintf("File: %s", filename)) |
| 130 | + |
| 131 | + // Try to read and display basic policy info |
| 132 | + policy, err := kyverno.ReadPolicyFromFile(path) |
| 133 | + if err == nil { |
| 134 | + if policy.Kind != "" { |
| 135 | + policyNode.AddNode(fmt.Sprintf("Kind: %s", policy.Kind)) |
| 136 | + } |
| 137 | + if policy.APIVersion != "" { |
| 138 | + policyNode.AddNode(fmt.Sprintf("API Version: %s", policy.APIVersion)) |
| 139 | + } |
| 140 | + } |
| 141 | + } else if isTestCaseFile(filename) { |
| 142 | + // Add test case files as nodes under their policy |
| 143 | + policyName := extractPolicyNameFromTestCase(filename) |
| 144 | + testCaseName := extractTestCaseName(filename) |
| 145 | + expectedOutcome := determineExpectedOutcome(filename) |
| 146 | + |
| 147 | + // Find or create the policy node |
| 148 | + // For simplicity, always create a new policy node for test cases |
| 149 | + // In a more sophisticated implementation, we'd track existing nodes |
| 150 | + policyNode := policiesBranch.AddBranch(policyName) |
| 151 | + |
| 152 | + testNode := policyNode.AddBranch("test-cases") |
| 153 | + testCaseNode := testNode.AddBranch(testCaseName) |
| 154 | + testCaseNode.AddNode(fmt.Sprintf("File: %s", filename)) |
| 155 | + testCaseNode.AddNode(fmt.Sprintf("Expected: %s", expectedOutcome)) |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | + }) |
| 160 | + |
| 161 | + return err |
| 162 | +} |
| 163 | + |
| 164 | +// Helper functions for local policy file processing |
| 165 | +func isPolicyFile(filename string) bool { |
| 166 | + return (strings.HasSuffix(filename, ".yaml") || strings.HasSuffix(filename, ".yml")) && |
| 167 | + !strings.Contains(filename, ".testcase") |
| 168 | +} |
| 169 | + |
| 170 | +func isTestCaseFile(filename string) bool { |
| 171 | + return strings.Contains(filename, ".testcase") |
| 172 | +} |
| 173 | + |
| 174 | +func extractPolicyNameFromFile(filename string) string { |
| 175 | + name := strings.TrimSuffix(filename, ".yaml") |
| 176 | + name = strings.TrimSuffix(name, ".yml") |
| 177 | + return name |
| 178 | +} |
| 179 | + |
| 180 | +func extractPolicyNameFromTestCase(filename string) string { |
| 181 | + parts := strings.Split(filename, ".") |
| 182 | + if len(parts) >= 2 { |
| 183 | + return parts[0] |
| 184 | + } |
| 185 | + return "" |
| 186 | +} |
| 187 | + |
| 188 | +func extractTestCaseName(filename string) string { |
| 189 | + parts := strings.Split(filename, ".") |
| 190 | + for _, part := range parts { |
| 191 | + if strings.HasPrefix(part, "testcase") { |
| 192 | + return part |
| 193 | + } |
| 194 | + } |
| 195 | + return "" |
| 196 | +} |
| 197 | + |
| 198 | +func determineExpectedOutcome(filename string) string { |
| 199 | + if strings.Contains(filename, ".success.") { |
| 200 | + return "success" |
| 201 | + } |
| 202 | + if strings.Contains(filename, ".failure.") { |
| 203 | + return "failure" |
| 204 | + } |
| 205 | + return "unknown" |
| 206 | +} |
0 commit comments