| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package snmp |
| 4 | |
| 5 | import ( |
| 6 | "strings" |
| 7 | "unicode" |
| 8 | ) |
| 9 | |
| 10 | var ( |
| 11 | licenseStateIgnoredHints = newLicenseStateHintSet( |
| 12 | "ignored", |
| 13 | "not_subscribed", |
| 14 | "not subscribed", |
| 15 | "not-applicable", |
| 16 | "not_applicable", |
| 17 | "not applicable", |
| 18 | "none", |
| 19 | "n/a", |
| 20 | ) |
| 21 | licenseStateBrokenHints = newLicenseStateHintSet( |
| 22 | "expired", |
| 23 | "expired_in_use", |
| 24 | "expired_not_in_use", |
| 25 | "authorization_expired", |
| 26 | "grace_period_expired", |
| 27 | "evaluation_expired", |
| 28 | "usage_count_consumed", |
| 29 | "invalid", |
| 30 | "invalid_tag", |
| 31 | "unauthorized", |
| 32 | "not_authorized", |
| 33 | "not authorized", |
| 34 | "out-of-compliance", |
| 35 | "out_of_compliance", |
| 36 | "not-associated", |
| 37 | "not_associated", |
| 38 | "not associated", |
| 39 | "disabled", |
| 40 | "deactivated", |
| 41 | "failed", |
| 42 | "error", |
| 43 | "violation", |
| 44 | "broken", |
| 45 | ) |
| 46 | licenseStateDegradedHints = newLicenseStateHintSet( |
| 47 | "about-to-expire", |
| 48 | "about_to_expire", |
| 49 | "about to expire", |
| 50 | "warning", |
| 51 | "degrade", |
| 52 | "degraded", |
| 53 | "grace", |
| 54 | "overage", |
| 55 | "partial", |
| 56 | "unknown", |
| 57 | ) |
| 58 | licenseStateInformationalHints = newLicenseStateHintSet( |
| 59 | "evaluation", |
| 60 | "evaluation_subscription", |
| 61 | "evaluation subscription", |
| 62 | "evaluation_period", |
| 63 | "evaluation period", |
| 64 | "eval", |
| 65 | "trial", |
| 66 | "initialized", |
| 67 | "waiting", |
| 68 | ) |
| 69 | licenseStateHealthyHints = newLicenseStateHintSet( |
| 70 | "valid", |
| 71 | "active", |
| 72 | "authorized", |
| 73 | "reserved_authorized", |
| 74 | "reserved authorized", |
| 75 | "compliant", |
| 76 | "ok", |
| 77 | "subscribed", |
| 78 | "registered", |
| 79 | "in_use", |
| 80 | "in-use", |
| 81 | "in use", |
| 82 | "up-to-date", |
| 83 | "up_to_date", |
| 84 | "up to date", |
| 85 | ) |
| 86 | ) |
| 87 | |
| 88 | func newLicenseStateHintSet(hints ...string) map[string]struct{} { |
| 89 | set := make(map[string]struct{}, len(hints)) |
| 90 | for _, hint := range hints { |
| 91 | if normalized := normalizeLicenseStateText(hint); normalized != "" { |
| 92 | set[normalized] = struct{}{} |
| 93 | } |
| 94 | } |
| 95 | return set |
| 96 | } |
| 97 | |
| 98 | func normalizeLicenseStateText(raw string) string { |
| 99 | raw = strings.ToLower(strings.TrimSpace(raw)) |
| 100 | if raw == "" { |
| 101 | return "" |
| 102 | } |
| 103 | |
| 104 | normalized := strings.Map(func(r rune) rune { |
| 105 | if unicode.IsLetter(r) || unicode.IsDigit(r) { |
| 106 | return r |
| 107 | } |
| 108 | return ' ' |
| 109 | }, raw) |
| 110 | |
| 111 | return strings.Join(strings.Fields(normalized), " ") |
| 112 | } |
| 113 | |
| 114 | func licenseStateMatchesAny(raw string, hints map[string]struct{}) bool { |
| 115 | if len(hints) == 0 { |
| 116 | return false |
| 117 | } |
| 118 | |
| 119 | normalized := normalizeLicenseStateText(raw) |
| 120 | if normalized == "" { |
| 121 | return false |
| 122 | } |
| 123 | |
| 124 | if _, ok := hints[normalized]; ok { |
| 125 | return true |
| 126 | } |
| 127 | |
| 128 | // Match normalized phrases on token boundaries so "authorization expired due to policy" |
| 129 | // still matches the broken hint "authorization expired", without reintroducing false |
| 130 | // positives such as "inactive" matching "active". |
| 131 | padded := " " + normalized + " " |
| 132 | for hint := range hints { |
| 133 | if hint == "" || hint == normalized { |
| 134 | continue |
| 135 | } |
| 136 | if strings.Contains(padded, " "+hint+" ") { |
| 137 | return true |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return false |
| 142 | } |