ValidateVSAAndComparePolicy performs optimized VSA validation with single retrieval
(ctx context.Context, identifier string, data *VSAValidationConfig)
| 40 | |
| 41 | // ValidateVSAAndComparePolicy performs optimized VSA validation with single retrieval |
| 42 | func ValidateVSAAndComparePolicy(ctx context.Context, identifier string, data *VSAValidationConfig) (*ValidationResult, error) { |
| 43 | if data == nil { |
| 44 | return nil, fmt.Errorf("validation data cannot be nil") |
| 45 | } |
| 46 | |
| 47 | if data.Retriever == nil { |
| 48 | return nil, fmt.Errorf("VSA retriever cannot be nil") |
| 49 | } |
| 50 | |
| 51 | // Use VSA library's VSAChecker for efficient VSA validation |
| 52 | checker := NewVSAChecker(data.Retriever) |
| 53 | |
| 54 | // SINGLE VSA RETRIEVAL with optional signature verification |
| 55 | result, err := checker.CheckExistingVSAWithVerification( |
| 56 | ctx, |
| 57 | identifier, |
| 58 | data.VSAExpiration, |
| 59 | !data.IgnoreSignatureVerification, // Whether to verify signature (inverse of ignore flag) |
| 60 | data.PublicKeyPath, // Public key path (if signature verification requested) |
| 61 | ) |
| 62 | if err != nil { |
| 63 | return nil, fmt.Errorf("failed to check existing VSA: %w", err) |
| 64 | } |
| 65 | |
| 66 | if !result.Found { |
| 67 | return &ValidationResult{ |
| 68 | Passed: false, |
| 69 | Message: "No VSA found for the specified identifier", |
| 70 | SignatureVerified: result.SignatureVerified, |
| 71 | PredicateOutcome: "", |
| 72 | ReasonCode: "no_vsa", |
| 73 | }, nil |
| 74 | } |
| 75 | |
| 76 | if result.Expired { |
| 77 | days := int(math.Ceil(time.Since(result.Timestamp).Hours() / 24)) |
| 78 | predicateStatus := "" |
| 79 | if result.VSA != nil { |
| 80 | predicateStatus = result.VSA.Status |
| 81 | } |
| 82 | return &ValidationResult{ |
| 83 | Passed: false, |
| 84 | Message: fmt.Sprintf("VSA expired %d day(s) ago", days), |
| 85 | SignatureVerified: result.SignatureVerified, |
| 86 | PredicateOutcome: predicateStatus, |
| 87 | ReasonCode: "expired", |
| 88 | }, nil |
| 89 | } |
| 90 | |
| 91 | // Extract predicate status FIRST to avoid unnecessary policy comparison |
| 92 | predicateStatus := "" |
| 93 | if result.VSA != nil { |
| 94 | predicateStatus = result.VSA.Status |
| 95 | } |
| 96 | |
| 97 | // If predicate status is not "passed", return early to avoid expensive policy comparison |
| 98 | // This is the key optimization: check predicate status before doing policy work |
| 99 | if predicateStatus != "" && predicateStatus != "passed" { |