|
| 1 | +// Package outcomerouting is the single source of truth for the 9-outcome |
| 2 | +// policy-discovery routing table. |
| 3 | +// Migrated from src/apm_cli/policy/outcome_routing.py. |
| 4 | +package outcomerouting |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/githubnext/apm/internal/policy/schema" |
| 10 | +) |
| 11 | + |
| 12 | +// PolicyViolationError is raised when a policy demands fail-closed behaviour. |
| 13 | +type PolicyViolationError struct { |
| 14 | + Message string |
| 15 | + PolicySource string |
| 16 | +} |
| 17 | + |
| 18 | +func (e *PolicyViolationError) Error() string { |
| 19 | + return e.Message |
| 20 | +} |
| 21 | + |
| 22 | +// PolicyFetchResult holds the result of a discover_policy call. |
| 23 | +type PolicyFetchResult struct { |
| 24 | + Outcome string |
| 25 | + Source string |
| 26 | + Cached bool |
| 27 | + Error string |
| 28 | + FetchError string |
| 29 | + CacheAgeSeconds int |
| 30 | + Policy *schema.ApmPolicy |
| 31 | +} |
| 32 | + |
| 33 | +// PolicyLogger is the minimal interface expected of a logger for routing. |
| 34 | +type PolicyLogger interface { |
| 35 | + PolicyResolved(source string, cached bool, enforcement string, ageSeconds int) |
| 36 | + PolicyDiscoveryMiss(outcome string, source string, err string) |
| 37 | +} |
| 38 | + |
| 39 | +// outcomesHonoringFetchFailureDefault is the set of outcomes that respect the |
| 40 | +// project-side policy.fetch_failure_default knob. |
| 41 | +var outcomesHonoringFetchFailureDefault = map[string]bool{ |
| 42 | + "malformed": true, |
| 43 | + "cache_miss_fetch_fail": true, |
| 44 | + "garbage_response": true, |
| 45 | + "no_git_remote": true, |
| 46 | + "absent": true, |
| 47 | + "empty": true, |
| 48 | +} |
| 49 | + |
| 50 | +// nonFoundLoggedOutcomes is the set of outcomes routed through the canonical |
| 51 | +// policy_discovery_miss logger helper. |
| 52 | +var nonFoundLoggedOutcomes = map[string]bool{ |
| 53 | + "absent": true, |
| 54 | + "no_git_remote": true, |
| 55 | + "empty": true, |
| 56 | + "malformed": true, |
| 57 | + "cache_miss_fetch_fail": true, |
| 58 | + "garbage_response": true, |
| 59 | +} |
| 60 | + |
| 61 | +// RouteDiscoveryOutcome routes a PolicyFetchResult to logging and fail-closed |
| 62 | +// decisions. |
| 63 | +// |
| 64 | +// Parameters: |
| 65 | +// - fetchResult: result of discover_policy_with_chain |
| 66 | +// - logger: logger implementing PolicyLogger (nil is tolerated) |
| 67 | +// - fetchFailureDefault: project-side policy.fetch_failure_default ("warn" or "block") |
| 68 | +// - raiseBlockingErrors: when true, return a PolicyViolationError for blocking outcomes |
| 69 | +// |
| 70 | +// Returns the effective ApmPolicy when enforcement should proceed, nil otherwise. |
| 71 | +// When raiseBlockingErrors is true and a blocking condition is met, a non-nil error |
| 72 | +// is returned alongside a nil policy. |
| 73 | +func RouteDiscoveryOutcome( |
| 74 | + fetchResult PolicyFetchResult, |
| 75 | + logger PolicyLogger, |
| 76 | + fetchFailureDefault string, |
| 77 | + raiseBlockingErrors bool, |
| 78 | +) (*schema.ApmPolicy, error) { |
| 79 | + outcome := fetchResult.Outcome |
| 80 | + source := fetchResult.Source |
| 81 | + |
| 82 | + if outcome == "disabled" { |
| 83 | + return nil, nil |
| 84 | + } |
| 85 | + |
| 86 | + // hash_mismatch: ALWAYS fail closed regardless of fetch_failure_default. |
| 87 | + if outcome == "hash_mismatch" { |
| 88 | + errStr := fetchResult.Error |
| 89 | + if errStr == "" { |
| 90 | + errStr = fetchResult.FetchError |
| 91 | + } |
| 92 | + if logger != nil { |
| 93 | + logger.PolicyDiscoveryMiss("hash_mismatch", source, errStr) |
| 94 | + } |
| 95 | + if raiseBlockingErrors { |
| 96 | + return nil, &PolicyViolationError{ |
| 97 | + Message: fmt.Sprintf( |
| 98 | + "Install blocked: policy hash mismatch -- pinned policy.hash "+ |
| 99 | + "does not match fetched policy bytes (source=%s). "+ |
| 100 | + "Update apm.yml policy.hash or contact your org admin.", |
| 101 | + sourceOrUnknown(source), |
| 102 | + ), |
| 103 | + PolicySource: sourceOrUnknown(source), |
| 104 | + } |
| 105 | + } |
| 106 | + return nil, nil |
| 107 | + } |
| 108 | + |
| 109 | + // 6 of 9 non-found outcomes route through the canonical logger helper. |
| 110 | + if nonFoundLoggedOutcomes[outcome] { |
| 111 | + errStr := fetchResult.Error |
| 112 | + if errStr == "" { |
| 113 | + errStr = fetchResult.FetchError |
| 114 | + } |
| 115 | + if logger != nil { |
| 116 | + logger.PolicyDiscoveryMiss(outcome, source, errStr) |
| 117 | + } |
| 118 | + if raiseBlockingErrors && |
| 119 | + outcomesHonoringFetchFailureDefault[outcome] && |
| 120 | + fetchFailureDefault == "block" { |
| 121 | + return nil, &PolicyViolationError{ |
| 122 | + Message: fmt.Sprintf( |
| 123 | + "Install blocked: no enforceable org policy was resolved "+ |
| 124 | + "(outcome=%s) and project apm.yml has "+ |
| 125 | + "policy.fetch_failure_default=block (source=%s)", |
| 126 | + outcome, |
| 127 | + sourceOrUnknown(source), |
| 128 | + ), |
| 129 | + PolicySource: sourceOrUnknown(source), |
| 130 | + } |
| 131 | + } |
| 132 | + return nil, nil |
| 133 | + } |
| 134 | + |
| 135 | + // cached_stale: log, enforce with the cached policy, potentially fail closed. |
| 136 | + if outcome == "cached_stale" { |
| 137 | + policy := fetchResult.Policy |
| 138 | + if logger != nil { |
| 139 | + if policy != nil { |
| 140 | + enforcement := policy.Enforcement |
| 141 | + if enforcement == "" { |
| 142 | + enforcement = "warn" |
| 143 | + } |
| 144 | + logger.PolicyResolved(source, true, enforcement, fetchResult.CacheAgeSeconds) |
| 145 | + } |
| 146 | + logger.PolicyDiscoveryMiss("cached_stale", source, fetchResult.FetchError) |
| 147 | + } |
| 148 | + if raiseBlockingErrors && policy != nil { |
| 149 | + ff := policy.FetchFailure |
| 150 | + if ff == "" { |
| 151 | + ff = "warn" |
| 152 | + } |
| 153 | + if ff == "block" { |
| 154 | + return nil, &PolicyViolationError{ |
| 155 | + Message: fmt.Sprintf( |
| 156 | + "Install blocked: org policy refresh failed and the cached "+ |
| 157 | + "policy declares fetch_failure=block (source=%s)", |
| 158 | + sourceOrUnknown(source), |
| 159 | + ), |
| 160 | + PolicySource: sourceOrUnknown(source), |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + return policy, nil |
| 165 | + } |
| 166 | + |
| 167 | + // found: normal path |
| 168 | + if outcome == "found" { |
| 169 | + policy := fetchResult.Policy |
| 170 | + if logger != nil && policy != nil { |
| 171 | + enforcement := policy.Enforcement |
| 172 | + if enforcement == "" { |
| 173 | + enforcement = "warn" |
| 174 | + } |
| 175 | + logger.PolicyResolved(source, fetchResult.Cached, enforcement, fetchResult.CacheAgeSeconds) |
| 176 | + } |
| 177 | + return policy, nil |
| 178 | + } |
| 179 | + |
| 180 | + // Defensive: unrecognised outcome -- skip enforcement. |
| 181 | + return nil, nil |
| 182 | +} |
| 183 | + |
| 184 | +func sourceOrUnknown(s string) string { |
| 185 | + if s == "" { |
| 186 | + return "unknown" |
| 187 | + } |
| 188 | + return s |
| 189 | +} |
0 commit comments