|
| 1 | +// Copyright The Conforma Contributors |
| 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 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package compare |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "time" |
| 24 | + |
| 25 | + ecc "github.com/conforma/crds/api/v1alpha1" |
| 26 | + "github.com/spf13/cobra" |
| 27 | + "sigs.k8s.io/yaml" |
| 28 | + |
| 29 | + "github.com/conforma/cli/internal/policy/equivalence" |
| 30 | +) |
| 31 | + |
| 32 | +var ( |
| 33 | + effectiveTime string |
| 34 | + imageDigest string |
| 35 | + imageRef string |
| 36 | + imageURL string |
| 37 | + outputFormat string |
| 38 | +) |
| 39 | + |
| 40 | +var CompareCmd *cobra.Command |
| 41 | + |
| 42 | +func init() { |
| 43 | + CompareCmd = NewCompareCmd() |
| 44 | +} |
| 45 | + |
| 46 | +func NewCompareCmd() *cobra.Command { |
| 47 | + compareCmd := &cobra.Command{ |
| 48 | + Use: "compare <policy1> <policy2>", |
| 49 | + Short: "Compare two Conforma Policy specs for equivalence", |
| 50 | + Long: `Compare two Conforma Policy specs to determine if they would |
| 51 | +produce the same evaluation result for a given image at a specific time. |
| 52 | +
|
| 53 | +The comparison is based on: |
| 54 | +- Policy and data source URIs (treated as sets) |
| 55 | +- RuleData content (canonicalized JSON comparison) |
| 56 | +- Include/exclude matchers (normalized and deduplicated) |
| 57 | +- Active volatile configuration (filtered by effective time and image matching) |
| 58 | +- Global configuration merging |
| 59 | +
|
| 60 | +Examples: |
| 61 | + # Compare two policy files |
| 62 | + ec compare policy1.yaml policy2.yaml |
| 63 | +
|
| 64 | + # Compare with specific effective time |
| 65 | + ec compare policy1.yaml policy2.yaml --effective-time "2024-01-15T12:00:00Z" |
| 66 | +
|
| 67 | + # Compare with image information for volatile config matching |
| 68 | + ec compare policy1.yaml policy2.yaml --image-digest "sha256:abc123" --image-ref "registry.redhat.io/ubi8/ubi:latest" |
| 69 | +
|
| 70 | + # Compare with JSON output |
| 71 | + ec compare policy1.yaml policy2.yaml --output json`, |
| 72 | + Args: cobra.ExactArgs(2), |
| 73 | + RunE: runCompare, |
| 74 | + } |
| 75 | + |
| 76 | + compareCmd.Flags().StringVar(&effectiveTime, "effective-time", "now", "Effective time for policy evaluation (RFC3339 format, 'now')") |
| 77 | + compareCmd.Flags().StringVar(&imageDigest, "image-digest", "", "Image digest for volatile config matching") |
| 78 | + compareCmd.Flags().StringVar(&imageRef, "image-ref", "", "Image reference for volatile config matching") |
| 79 | + compareCmd.Flags().StringVar(&imageURL, "image-url", "", "Image URL for volatile config matching") |
| 80 | + compareCmd.Flags().StringVar(&outputFormat, "output", "text", "Output format (text, json)") |
| 81 | + |
| 82 | + return compareCmd |
| 83 | +} |
| 84 | + |
| 85 | +func runCompare(cmd *cobra.Command, args []string) error { |
| 86 | + |
| 87 | + // Parse effective time |
| 88 | + var effectiveTimeValue time.Time |
| 89 | + switch effectiveTime { |
| 90 | + case "now": |
| 91 | + effectiveTimeValue = time.Now().UTC() |
| 92 | + case "attestation": |
| 93 | + // For now, use current time as default for attestation time |
| 94 | + effectiveTimeValue = time.Now().UTC() |
| 95 | + default: |
| 96 | + var err error |
| 97 | + effectiveTimeValue, err = time.Parse(time.RFC3339, effectiveTime) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("invalid effective time format: %w", err) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Create image info if provided |
| 104 | + var imageInfo *equivalence.ImageInfo |
| 105 | + if imageDigest != "" || imageRef != "" || imageURL != "" { |
| 106 | + imageInfo = &equivalence.ImageInfo{ |
| 107 | + Digest: imageDigest, |
| 108 | + Ref: imageRef, |
| 109 | + URL: imageURL, |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Load first policy |
| 114 | + spec1, err := loadPolicySpec(args[0]) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("failed to load first policy: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + // Load second policy |
| 120 | + spec2, err := loadPolicySpec(args[1]) |
| 121 | + if err != nil { |
| 122 | + return fmt.Errorf("failed to load second policy: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + // Create equivalence checker |
| 126 | + checker := equivalence.NewEquivalenceChecker(effectiveTimeValue, imageInfo) |
| 127 | + |
| 128 | + // Compare policies |
| 129 | + equivalent, err := checker.AreEquivalent(spec1, spec2) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("failed to compare policies: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + // Output result |
| 135 | + if outputFormat == "json" { |
| 136 | + result := map[string]interface{}{ |
| 137 | + "equivalent": equivalent, |
| 138 | + "effective_time": effectiveTimeValue.Format(time.RFC3339), |
| 139 | + "policy1": args[0], |
| 140 | + "policy2": args[1], |
| 141 | + } |
| 142 | + if imageInfo != nil { |
| 143 | + result["image_info"] = imageInfo |
| 144 | + } |
| 145 | + encoder := json.NewEncoder(os.Stdout) |
| 146 | + encoder.SetIndent("", " ") |
| 147 | + return encoder.Encode(result) |
| 148 | + } |
| 149 | + |
| 150 | + // Text output |
| 151 | + if equivalent { |
| 152 | + fmt.Println("✅ Policies are equivalent") |
| 153 | + } else { |
| 154 | + fmt.Println("❌ Policies are not equivalent") |
| 155 | + } |
| 156 | + |
| 157 | + fmt.Printf("Effective time: %s\n", effectiveTimeValue.Format(time.RFC3339)) |
| 158 | + if imageInfo != nil { |
| 159 | + fmt.Printf("Image digest: %s\n", imageInfo.Digest) |
| 160 | + fmt.Printf("Image ref: %s\n", imageInfo.Ref) |
| 161 | + fmt.Printf("Image URL: %s\n", imageInfo.URL) |
| 162 | + } |
| 163 | + |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func loadPolicySpec(policyRef string) (ecc.EnterpriseContractPolicySpec, error) { |
| 168 | + content, err := os.ReadFile(policyRef) |
| 169 | + if err != nil { |
| 170 | + return ecc.EnterpriseContractPolicySpec{}, fmt.Errorf("failed to read policy file %q: %w", policyRef, err) |
| 171 | + } |
| 172 | + |
| 173 | + var ecp ecc.EnterpriseContractPolicy |
| 174 | + if err := yaml.Unmarshal(content, &ecp); err != nil { |
| 175 | + // If parsing as EnterpriseContractPolicy fails, try as EnterpriseContractPolicySpec |
| 176 | + var spec ecc.EnterpriseContractPolicySpec |
| 177 | + if err := yaml.Unmarshal(content, &spec); err != nil { |
| 178 | + return ecc.EnterpriseContractPolicySpec{}, fmt.Errorf("unable to parse EnterpriseContractPolicySpec: %w", err) |
| 179 | + } |
| 180 | + return spec, nil |
| 181 | + } |
| 182 | + |
| 183 | + // Check if this is actually a valid CRD (has required fields) |
| 184 | + if ecp.APIVersion == "" || ecp.Kind == "" { |
| 185 | + // This is not a valid CRD, try parsing as EnterpriseContractPolicySpec |
| 186 | + var spec ecc.EnterpriseContractPolicySpec |
| 187 | + if err := yaml.Unmarshal(content, &spec); err != nil { |
| 188 | + return ecc.EnterpriseContractPolicySpec{}, fmt.Errorf("unable to parse EnterpriseContractPolicySpec: %w", err) |
| 189 | + } |
| 190 | + return spec, nil |
| 191 | + } |
| 192 | + |
| 193 | + return ecp.Spec, nil |
| 194 | +} |
0 commit comments